fw-topology.c 14 KB

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