core-topology.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Incremental bus scan, based on bus topology
  3. *
  4. * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #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>
  30. #include <linux/string.h>
  31. #include <asm/atomic.h>
  32. #include <asm/system.h>
  33. #include "core.h"
  34. #define SELF_ID_PHY_ID(q) (((q) >> 24) & 0x3f)
  35. #define SELF_ID_EXTENDED(q) (((q) >> 23) & 0x01)
  36. #define SELF_ID_LINK_ON(q) (((q) >> 22) & 0x01)
  37. #define SELF_ID_GAP_COUNT(q) (((q) >> 16) & 0x3f)
  38. #define SELF_ID_PHY_SPEED(q) (((q) >> 14) & 0x03)
  39. #define SELF_ID_CONTENDER(q) (((q) >> 11) & 0x01)
  40. #define SELF_ID_PHY_INITIATOR(q) (((q) >> 1) & 0x01)
  41. #define SELF_ID_MORE_PACKETS(q) (((q) >> 0) & 0x01)
  42. #define SELF_ID_EXT_SEQUENCE(q) (((q) >> 20) & 0x07)
  43. #define SELFID_PORT_CHILD 0x3
  44. #define SELFID_PORT_PARENT 0x2
  45. #define SELFID_PORT_NCONN 0x1
  46. #define SELFID_PORT_NONE 0x0
  47. static u32 *count_ports(u32 *sid, int *total_port_count, int *child_port_count)
  48. {
  49. u32 q;
  50. int port_type, shift, seq;
  51. *total_port_count = 0;
  52. *child_port_count = 0;
  53. shift = 6;
  54. q = *sid;
  55. seq = 0;
  56. while (1) {
  57. port_type = (q >> shift) & 0x03;
  58. switch (port_type) {
  59. case SELFID_PORT_CHILD:
  60. (*child_port_count)++;
  61. case SELFID_PORT_PARENT:
  62. case SELFID_PORT_NCONN:
  63. (*total_port_count)++;
  64. case SELFID_PORT_NONE:
  65. break;
  66. }
  67. shift -= 2;
  68. if (shift == 0) {
  69. if (!SELF_ID_MORE_PACKETS(q))
  70. return sid + 1;
  71. shift = 16;
  72. sid++;
  73. q = *sid;
  74. /*
  75. * Check that the extra packets actually are
  76. * extended self ID packets and that the
  77. * sequence numbers in the extended self ID
  78. * packets increase as expected.
  79. */
  80. if (!SELF_ID_EXTENDED(q) ||
  81. seq != SELF_ID_EXT_SEQUENCE(q))
  82. return NULL;
  83. seq++;
  84. }
  85. }
  86. }
  87. static int get_port_type(u32 *sid, int port_index)
  88. {
  89. int index, shift;
  90. index = (port_index + 5) / 8;
  91. shift = 16 - ((port_index + 5) & 7) * 2;
  92. return (sid[index] >> shift) & 0x03;
  93. }
  94. static struct fw_node *fw_node_create(u32 sid, int port_count, int color)
  95. {
  96. struct fw_node *node;
  97. node = kzalloc(sizeof(*node) + port_count * sizeof(node->ports[0]),
  98. GFP_ATOMIC);
  99. if (node == NULL)
  100. return NULL;
  101. node->color = color;
  102. node->node_id = LOCAL_BUS | SELF_ID_PHY_ID(sid);
  103. node->link_on = SELF_ID_LINK_ON(sid);
  104. node->phy_speed = SELF_ID_PHY_SPEED(sid);
  105. node->initiated_reset = SELF_ID_PHY_INITIATOR(sid);
  106. node->port_count = port_count;
  107. atomic_set(&node->ref_count, 1);
  108. INIT_LIST_HEAD(&node->link);
  109. return node;
  110. }
  111. /*
  112. * Compute the maximum hop count for this node and it's children. The
  113. * maximum hop count is the maximum number of connections between any
  114. * two nodes in the subtree rooted at this node. We need this for
  115. * setting the gap count. As we build the tree bottom up in
  116. * build_tree() below, this is fairly easy to do: for each node we
  117. * maintain the max hop count and the max depth, ie the number of hops
  118. * to the furthest leaf. Computing the max hop count breaks down into
  119. * two cases: either the path goes through this node, in which case
  120. * the hop count is the sum of the two biggest child depths plus 2.
  121. * Or it could be the case that the max hop path is entirely
  122. * containted in a child tree, in which case the max hop count is just
  123. * the max hop count of this child.
  124. */
  125. static void update_hop_count(struct fw_node *node)
  126. {
  127. int depths[2] = { -1, -1 };
  128. int max_child_hops = 0;
  129. int i;
  130. for (i = 0; i < node->port_count; i++) {
  131. if (node->ports[i] == NULL)
  132. continue;
  133. if (node->ports[i]->max_hops > max_child_hops)
  134. max_child_hops = node->ports[i]->max_hops;
  135. if (node->ports[i]->max_depth > depths[0]) {
  136. depths[1] = depths[0];
  137. depths[0] = node->ports[i]->max_depth;
  138. } else if (node->ports[i]->max_depth > depths[1])
  139. depths[1] = node->ports[i]->max_depth;
  140. }
  141. node->max_depth = depths[0] + 1;
  142. node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2);
  143. }
  144. static inline struct fw_node *fw_node(struct list_head *l)
  145. {
  146. return list_entry(l, struct fw_node, link);
  147. }
  148. /**
  149. * build_tree - Build the tree representation of the topology
  150. * @self_ids: array of self IDs to create the tree from
  151. * @self_id_count: the length of the self_ids array
  152. * @local_id: the node ID of the local node
  153. *
  154. * This function builds the tree representation of the topology given
  155. * by the self IDs from the latest bus reset. During the construction
  156. * of the tree, the function checks that the self IDs are valid and
  157. * internally consistent. On succcess this function returns the
  158. * fw_node corresponding to the local card otherwise NULL.
  159. */
  160. static struct fw_node *build_tree(struct fw_card *card,
  161. u32 *sid, int self_id_count)
  162. {
  163. struct fw_node *node, *child, *local_node, *irm_node;
  164. struct list_head stack, *h;
  165. u32 *next_sid, *end, q;
  166. int i, port_count, child_port_count, phy_id, parent_count, stack_depth;
  167. int gap_count;
  168. bool beta_repeaters_present;
  169. local_node = NULL;
  170. node = NULL;
  171. INIT_LIST_HEAD(&stack);
  172. stack_depth = 0;
  173. end = sid + self_id_count;
  174. phy_id = 0;
  175. irm_node = NULL;
  176. gap_count = SELF_ID_GAP_COUNT(*sid);
  177. beta_repeaters_present = false;
  178. while (sid < end) {
  179. next_sid = count_ports(sid, &port_count, &child_port_count);
  180. if (next_sid == NULL) {
  181. fw_error("Inconsistent extended self IDs.\n");
  182. return NULL;
  183. }
  184. q = *sid;
  185. if (phy_id != SELF_ID_PHY_ID(q)) {
  186. fw_error("PHY ID mismatch in self ID: %d != %d.\n",
  187. phy_id, SELF_ID_PHY_ID(q));
  188. return NULL;
  189. }
  190. if (child_port_count > stack_depth) {
  191. fw_error("Topology stack underflow\n");
  192. return NULL;
  193. }
  194. /*
  195. * Seek back from the top of our stack to find the
  196. * start of the child nodes for this node.
  197. */
  198. for (i = 0, h = &stack; i < child_port_count; i++)
  199. h = h->prev;
  200. /*
  201. * When the stack is empty, this yields an invalid value,
  202. * but that pointer will never be dereferenced.
  203. */
  204. child = fw_node(h);
  205. node = fw_node_create(q, port_count, card->color);
  206. if (node == NULL) {
  207. fw_error("Out of memory while building topology.\n");
  208. return NULL;
  209. }
  210. if (phy_id == (card->node_id & 0x3f))
  211. local_node = node;
  212. if (SELF_ID_CONTENDER(q))
  213. irm_node = node;
  214. parent_count = 0;
  215. for (i = 0; i < port_count; i++) {
  216. switch (get_port_type(sid, i)) {
  217. case SELFID_PORT_PARENT:
  218. /*
  219. * Who's your daddy? We dont know the
  220. * parent node at this time, so we
  221. * temporarily abuse node->color for
  222. * remembering the entry in the
  223. * node->ports array where the parent
  224. * node should be. Later, when we
  225. * handle the parent node, we fix up
  226. * the reference.
  227. */
  228. parent_count++;
  229. node->color = i;
  230. break;
  231. case SELFID_PORT_CHILD:
  232. node->ports[i] = child;
  233. /*
  234. * Fix up parent reference for this
  235. * child node.
  236. */
  237. child->ports[child->color] = node;
  238. child->color = card->color;
  239. child = fw_node(child->link.next);
  240. break;
  241. }
  242. }
  243. /*
  244. * Check that the node reports exactly one parent
  245. * port, except for the root, which of course should
  246. * have no parents.
  247. */
  248. if ((next_sid == end && parent_count != 0) ||
  249. (next_sid < end && parent_count != 1)) {
  250. fw_error("Parent port inconsistency for node %d: "
  251. "parent_count=%d\n", phy_id, parent_count);
  252. return NULL;
  253. }
  254. /* Pop the child nodes off the stack and push the new node. */
  255. __list_del(h->prev, &stack);
  256. list_add_tail(&node->link, &stack);
  257. stack_depth += 1 - child_port_count;
  258. if (node->phy_speed == SCODE_BETA &&
  259. parent_count + child_port_count > 1)
  260. beta_repeaters_present = true;
  261. /*
  262. * If PHYs report different gap counts, set an invalid count
  263. * which will force a gap count reconfiguration and a reset.
  264. */
  265. if (SELF_ID_GAP_COUNT(q) != gap_count)
  266. gap_count = 0;
  267. update_hop_count(node);
  268. sid = next_sid;
  269. phy_id++;
  270. }
  271. card->root_node = node;
  272. card->irm_node = irm_node;
  273. card->gap_count = gap_count;
  274. card->beta_repeaters_present = beta_repeaters_present;
  275. return local_node;
  276. }
  277. typedef void (*fw_node_callback_t)(struct fw_card * card,
  278. struct fw_node * node,
  279. struct fw_node * parent);
  280. static void for_each_fw_node(struct fw_card *card, struct fw_node *root,
  281. fw_node_callback_t callback)
  282. {
  283. struct list_head list;
  284. struct fw_node *node, *next, *child, *parent;
  285. int i;
  286. INIT_LIST_HEAD(&list);
  287. fw_node_get(root);
  288. list_add_tail(&root->link, &list);
  289. parent = NULL;
  290. list_for_each_entry(node, &list, link) {
  291. node->color = card->color;
  292. for (i = 0; i < node->port_count; i++) {
  293. child = node->ports[i];
  294. if (!child)
  295. continue;
  296. if (child->color == card->color)
  297. parent = child;
  298. else {
  299. fw_node_get(child);
  300. list_add_tail(&child->link, &list);
  301. }
  302. }
  303. callback(card, node, parent);
  304. }
  305. list_for_each_entry_safe(node, next, &list, link)
  306. fw_node_put(node);
  307. }
  308. static void report_lost_node(struct fw_card *card,
  309. struct fw_node *node, struct fw_node *parent)
  310. {
  311. fw_node_event(card, node, FW_NODE_DESTROYED);
  312. fw_node_put(node);
  313. /* Topology has changed - reset bus manager retry counter */
  314. card->bm_retries = 0;
  315. }
  316. static void report_found_node(struct fw_card *card,
  317. struct fw_node *node, struct fw_node *parent)
  318. {
  319. int b_path = (node->phy_speed == SCODE_BETA);
  320. if (parent != NULL) {
  321. /* min() macro doesn't work here with gcc 3.4 */
  322. node->max_speed = parent->max_speed < node->phy_speed ?
  323. parent->max_speed : node->phy_speed;
  324. node->b_path = parent->b_path && b_path;
  325. } else {
  326. node->max_speed = node->phy_speed;
  327. node->b_path = b_path;
  328. }
  329. fw_node_event(card, node, FW_NODE_CREATED);
  330. /* Topology has changed - reset bus manager retry counter */
  331. card->bm_retries = 0;
  332. }
  333. void fw_destroy_nodes(struct fw_card *card)
  334. {
  335. unsigned long flags;
  336. spin_lock_irqsave(&card->lock, flags);
  337. card->color++;
  338. if (card->local_node != NULL)
  339. for_each_fw_node(card, card->local_node, report_lost_node);
  340. card->local_node = NULL;
  341. spin_unlock_irqrestore(&card->lock, flags);
  342. }
  343. static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)
  344. {
  345. struct fw_node *tree;
  346. int i;
  347. tree = node1->ports[port];
  348. node0->ports[port] = tree;
  349. for (i = 0; i < tree->port_count; i++) {
  350. if (tree->ports[i] == node1) {
  351. tree->ports[i] = node0;
  352. break;
  353. }
  354. }
  355. }
  356. /**
  357. * update_tree - compare the old topology tree for card with the new
  358. * one specified by root. Queue the nodes and mark them as either
  359. * found, lost or updated. Update the nodes in the card topology tree
  360. * as we go.
  361. */
  362. static void update_tree(struct fw_card *card, struct fw_node *root)
  363. {
  364. struct list_head list0, list1;
  365. struct fw_node *node0, *node1, *next1;
  366. int i, event;
  367. INIT_LIST_HEAD(&list0);
  368. list_add_tail(&card->local_node->link, &list0);
  369. INIT_LIST_HEAD(&list1);
  370. list_add_tail(&root->link, &list1);
  371. node0 = fw_node(list0.next);
  372. node1 = fw_node(list1.next);
  373. while (&node0->link != &list0) {
  374. WARN_ON(node0->port_count != node1->port_count);
  375. if (node0->link_on && !node1->link_on)
  376. event = FW_NODE_LINK_OFF;
  377. else if (!node0->link_on && node1->link_on)
  378. event = FW_NODE_LINK_ON;
  379. else if (node1->initiated_reset && node1->link_on)
  380. event = FW_NODE_INITIATED_RESET;
  381. else
  382. event = FW_NODE_UPDATED;
  383. node0->node_id = node1->node_id;
  384. node0->color = card->color;
  385. node0->link_on = node1->link_on;
  386. node0->initiated_reset = node1->initiated_reset;
  387. node0->max_hops = node1->max_hops;
  388. node1->color = card->color;
  389. fw_node_event(card, node0, event);
  390. if (card->root_node == node1)
  391. card->root_node = node0;
  392. if (card->irm_node == node1)
  393. card->irm_node = node0;
  394. for (i = 0; i < node0->port_count; i++) {
  395. if (node0->ports[i] && node1->ports[i]) {
  396. /*
  397. * This port didn't change, queue the
  398. * connected node for further
  399. * investigation.
  400. */
  401. if (node0->ports[i]->color == card->color)
  402. continue;
  403. list_add_tail(&node0->ports[i]->link, &list0);
  404. list_add_tail(&node1->ports[i]->link, &list1);
  405. } else if (node0->ports[i]) {
  406. /*
  407. * The nodes connected here were
  408. * unplugged; unref the lost nodes and
  409. * queue FW_NODE_LOST callbacks for
  410. * them.
  411. */
  412. for_each_fw_node(card, node0->ports[i],
  413. report_lost_node);
  414. node0->ports[i] = NULL;
  415. } else if (node1->ports[i]) {
  416. /*
  417. * One or more node were connected to
  418. * this port. Move the new nodes into
  419. * the tree and queue FW_NODE_CREATED
  420. * callbacks for them.
  421. */
  422. move_tree(node0, node1, i);
  423. for_each_fw_node(card, node0->ports[i],
  424. report_found_node);
  425. }
  426. }
  427. node0 = fw_node(node0->link.next);
  428. next1 = fw_node(node1->link.next);
  429. fw_node_put(node1);
  430. node1 = next1;
  431. }
  432. }
  433. static void update_topology_map(struct fw_card *card,
  434. u32 *self_ids, int self_id_count)
  435. {
  436. int node_count;
  437. card->topology_map[1]++;
  438. node_count = (card->root_node->node_id & 0x3f) + 1;
  439. card->topology_map[2] = (node_count << 16) | self_id_count;
  440. card->topology_map[0] = (self_id_count + 2) << 16;
  441. memcpy(&card->topology_map[3], self_ids, self_id_count * 4);
  442. fw_compute_block_crc(card->topology_map);
  443. }
  444. void fw_core_handle_bus_reset(struct fw_card *card, int node_id, int generation,
  445. int self_id_count, u32 *self_ids)
  446. {
  447. struct fw_node *local_node;
  448. unsigned long flags;
  449. /*
  450. * If the selfID buffer is not the immediate successor of the
  451. * previously processed one, we cannot reliably compare the
  452. * old and new topologies.
  453. */
  454. if (!is_next_generation(generation, card->generation) &&
  455. card->local_node != NULL) {
  456. fw_notify("skipped bus generations, destroying all nodes\n");
  457. fw_destroy_nodes(card);
  458. card->bm_retries = 0;
  459. }
  460. spin_lock_irqsave(&card->lock, flags);
  461. card->broadcast_channel_allocated = false;
  462. card->node_id = node_id;
  463. /*
  464. * Update node_id before generation to prevent anybody from using
  465. * a stale node_id together with a current generation.
  466. */
  467. smp_wmb();
  468. card->generation = generation;
  469. card->reset_jiffies = jiffies;
  470. fw_schedule_bm_work(card, 0);
  471. local_node = build_tree(card, self_ids, self_id_count);
  472. update_topology_map(card, self_ids, self_id_count);
  473. card->color++;
  474. if (local_node == NULL) {
  475. fw_error("topology build failed\n");
  476. /* FIXME: We need to issue a bus reset in this case. */
  477. } else if (card->local_node == NULL) {
  478. card->local_node = local_node;
  479. for_each_fw_node(card, local_node, report_found_node);
  480. } else {
  481. update_tree(card, local_node);
  482. }
  483. spin_unlock_irqrestore(&card->lock, flags);
  484. }
  485. EXPORT_SYMBOL(fw_core_handle_bus_reset);