fw-topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. 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 = 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. /**
  97. * build_tree - Build the tree representation of the topology
  98. * @self_ids: array of self IDs to create the tree from
  99. * @self_id_count: the length of the self_ids array
  100. * @local_id: the node ID of the local node
  101. *
  102. * This function builds the tree representation of the topology given
  103. * by the self IDs from the latest bus reset. During the construction
  104. * of the tree, the function checks that the self IDs are valid and
  105. * internally consistent. On succcess this funtions returns the
  106. * fw_node corresponding to the local card otherwise NULL.
  107. */
  108. static struct fw_node *build_tree(struct fw_card *card)
  109. {
  110. struct fw_node *node, *child, *local_node;
  111. struct list_head stack, *h;
  112. u32 *sid, *next_sid, *end, q;
  113. int i, port_count, child_port_count, phy_id, parent_count, stack_depth;
  114. local_node = NULL;
  115. node = NULL;
  116. INIT_LIST_HEAD(&stack);
  117. stack_depth = 0;
  118. sid = card->self_ids;
  119. end = sid + card->self_id_count;
  120. phy_id = 0;
  121. card->irm_node = NULL;
  122. while (sid < end) {
  123. next_sid = count_ports(sid, &port_count, &child_port_count);
  124. if (next_sid == NULL) {
  125. fw_error("Inconsistent extended self IDs.\n");
  126. return NULL;
  127. }
  128. q = *sid;
  129. if (phy_id != self_id_phy_id(q)) {
  130. fw_error("PHY ID mismatch in self ID: %d != %d.\n",
  131. phy_id, self_id_phy_id(q));
  132. return NULL;
  133. }
  134. if (child_port_count > stack_depth) {
  135. fw_error("Topology stack underflow\n");
  136. return NULL;
  137. }
  138. /* Seek back from the top of our stack to find the
  139. * start of the child nodes for this node. */
  140. for (i = 0, h = &stack; i < child_port_count; i++)
  141. h = h->prev;
  142. child = fw_node(h);
  143. node = fw_node_create(q, port_count, card->color);
  144. if (node == NULL) {
  145. fw_error("Out of memory while building topology.");
  146. return NULL;
  147. }
  148. if (phy_id == (card->node_id & 0x3f))
  149. local_node = node;
  150. if (self_id_contender(q))
  151. card->irm_node = node;
  152. parent_count = 0;
  153. for (i = 0; i < port_count; i++) {
  154. switch (get_port_type(sid, i)) {
  155. case SELFID_PORT_PARENT:
  156. /* Who's your daddy? We dont know the
  157. * parent node at this time, so we
  158. * temporarily abuse node->color for
  159. * remembering the entry in the
  160. * node->ports array where the parent
  161. * node should be. Later, when we
  162. * handle the parent node, we fix up
  163. * the reference.
  164. */
  165. parent_count++;
  166. node->color = i;
  167. break;
  168. case SELFID_PORT_CHILD:
  169. node->ports[i].node = child;
  170. /* Fix up parent reference for this
  171. * child node. */
  172. child->ports[child->color].node = node;
  173. child->color = card->color;
  174. child = fw_node(child->link.next);
  175. break;
  176. }
  177. }
  178. /* Check that the node reports exactly one parent
  179. * port, except for the root, which of course should
  180. * have no parents. */
  181. if ((next_sid == end && parent_count != 0) ||
  182. (next_sid < end && parent_count != 1)) {
  183. fw_error("Parent port inconsistency for node %d: "
  184. "parent_count=%d\n", phy_id, parent_count);
  185. return NULL;
  186. }
  187. /* Pop the child nodes off the stack and push the new node. */
  188. __list_del(h->prev, &stack);
  189. list_add_tail(&node->link, &stack);
  190. stack_depth += 1 - child_port_count;
  191. sid = next_sid;
  192. phy_id++;
  193. }
  194. card->root_node = node;
  195. return local_node;
  196. }
  197. typedef void (*fw_node_callback_t) (struct fw_card * card,
  198. struct fw_node * node,
  199. struct fw_node * parent);
  200. static void
  201. for_each_fw_node(struct fw_card *card, struct fw_node *root,
  202. fw_node_callback_t callback)
  203. {
  204. struct list_head list;
  205. struct fw_node *node, *next, *child, *parent;
  206. int i;
  207. INIT_LIST_HEAD(&list);
  208. fw_node_get(root);
  209. list_add_tail(&root->link, &list);
  210. parent = NULL;
  211. list_for_each_entry(node, &list, link) {
  212. node->color = card->color;
  213. for (i = 0; i < node->port_count; i++) {
  214. child = node->ports[i].node;
  215. if (!child)
  216. continue;
  217. if (child->color == card->color)
  218. parent = child;
  219. else {
  220. fw_node_get(child);
  221. list_add_tail(&child->link, &list);
  222. }
  223. }
  224. callback(card, node, parent);
  225. }
  226. list_for_each_entry_safe(node, next, &list, link)
  227. fw_node_put(node);
  228. }
  229. static void
  230. report_lost_node(struct fw_card *card,
  231. struct fw_node *node, struct fw_node *parent)
  232. {
  233. fw_node_event(card, node, FW_NODE_DESTROYED);
  234. fw_node_put(node);
  235. }
  236. static void
  237. report_found_node(struct fw_card *card,
  238. struct fw_node *node, struct fw_node *parent)
  239. {
  240. int b_path = (node->phy_speed == SCODE_BETA);
  241. if (parent != NULL) {
  242. node->max_speed = min(parent->max_speed, node->phy_speed);
  243. node->b_path = parent->b_path && b_path;
  244. } else {
  245. node->max_speed = node->phy_speed;
  246. node->b_path = b_path;
  247. }
  248. fw_node_event(card, node, FW_NODE_CREATED);
  249. }
  250. void fw_destroy_nodes(struct fw_card *card)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&card->lock, flags);
  254. card->color++;
  255. if (card->local_node != NULL)
  256. for_each_fw_node(card, card->local_node, report_lost_node);
  257. spin_unlock_irqrestore(&card->lock, flags);
  258. }
  259. static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)
  260. {
  261. struct fw_node *tree;
  262. int i;
  263. tree = node1->ports[port].node;
  264. node0->ports[port].node = tree;
  265. for (i = 0; i < tree->port_count; i++) {
  266. if (tree->ports[i].node == node1) {
  267. tree->ports[i].node = node0;
  268. break;
  269. }
  270. }
  271. }
  272. /**
  273. * update_tree - compare the old topology tree for card with the new
  274. * one specified by root. Queue the nodes and mark them as either
  275. * found, lost or updated. Update the nodes in the card topology tree
  276. * as we go.
  277. */
  278. static void
  279. update_tree(struct fw_card *card, struct fw_node *root, int *changed)
  280. {
  281. struct list_head list0, list1;
  282. struct fw_node *node0, *node1;
  283. int i, event;
  284. INIT_LIST_HEAD(&list0);
  285. list_add_tail(&card->local_node->link, &list0);
  286. INIT_LIST_HEAD(&list1);
  287. list_add_tail(&root->link, &list1);
  288. node0 = fw_node(list0.next);
  289. node1 = fw_node(list1.next);
  290. *changed = 0;
  291. while (&node0->link != &list0) {
  292. /* assert(node0->port_count == node1->port_count); */
  293. if (node0->link_on && !node1->link_on)
  294. event = FW_NODE_LINK_OFF;
  295. else if (!node0->link_on && node1->link_on)
  296. event = FW_NODE_LINK_ON;
  297. else
  298. event = FW_NODE_UPDATED;
  299. node0->node_id = node1->node_id;
  300. node0->color = card->color;
  301. node0->link_on = node1->link_on;
  302. node0->initiated_reset = node1->initiated_reset;
  303. node1->color = card->color;
  304. fw_node_event(card, node0, event);
  305. if (card->root_node == node1)
  306. card->root_node = node0;
  307. if (card->irm_node == node1)
  308. card->irm_node = node0;
  309. for (i = 0; i < node0->port_count; i++) {
  310. if (node0->ports[i].node && node1->ports[i].node) {
  311. /* This port didn't change, queue the
  312. * connected node for further
  313. * investigation. */
  314. if (node0->ports[i].node->color == card->color)
  315. continue;
  316. list_add_tail(&node0->ports[i].node->link,
  317. &list0);
  318. list_add_tail(&node1->ports[i].node->link,
  319. &list1);
  320. } else if (node0->ports[i].node) {
  321. /* The nodes connected here were
  322. * unplugged; unref the lost nodes and
  323. * queue FW_NODE_LOST callbacks for
  324. * them. */
  325. for_each_fw_node(card, node0->ports[i].node,
  326. report_lost_node);
  327. node0->ports[i].node = NULL;
  328. *changed = 1;
  329. } else if (node1->ports[i].node) {
  330. /* One or more node were connected to
  331. * this port. Move the new nodes into
  332. * the tree and queue FW_NODE_CREATED
  333. * callbacks for them. */
  334. move_tree(node0, node1, i);
  335. for_each_fw_node(card, node0->ports[i].node,
  336. report_found_node);
  337. *changed = 1;
  338. }
  339. }
  340. node0 = fw_node(node0->link.next);
  341. node1 = fw_node(node1->link.next);
  342. }
  343. }
  344. void
  345. fw_core_handle_bus_reset(struct fw_card *card,
  346. int node_id, int generation,
  347. int self_id_count, u32 * self_ids)
  348. {
  349. struct fw_node *local_node;
  350. unsigned long flags;
  351. int changed;
  352. fw_flush_transactions(card);
  353. spin_lock_irqsave(&card->lock, flags);
  354. card->node_id = node_id;
  355. card->self_id_count = self_id_count;
  356. card->generation = generation;
  357. memcpy(card->self_ids, self_ids, self_id_count * 4);
  358. local_node = build_tree(card);
  359. card->color++;
  360. if (local_node == NULL) {
  361. fw_error("topology build failed\n");
  362. /* FIXME: We need to issue a bus reset in this case. */
  363. } else if (card->local_node == NULL) {
  364. card->local_node = local_node;
  365. for_each_fw_node(card, local_node, report_found_node);
  366. } else {
  367. update_tree(card, local_node, &changed);
  368. }
  369. spin_unlock_irqrestore(&card->lock, flags);
  370. }
  371. EXPORT_SYMBOL(fw_core_handle_bus_reset);
  372. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  373. {
  374. }