fw-topology.c 14 KB

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