fw-topology.c 14 KB

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