core-topology.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <asm/atomic.h>
  31. #include <asm/byteorder.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. * This function builds the tree representation of the topology given
  150. * by the self IDs from the latest bus reset. During the construction
  151. * of the tree, the function checks that the self IDs are valid and
  152. * internally consistent. On success this function returns the
  153. * fw_node corresponding to the local card otherwise NULL.
  154. */
  155. static struct fw_node *build_tree(struct fw_card *card,
  156. u32 *sid, int self_id_count)
  157. {
  158. struct fw_node *node, *child, *local_node, *irm_node;
  159. struct list_head stack, *h;
  160. u32 *next_sid, *end, q;
  161. int i, port_count, child_port_count, phy_id, parent_count, stack_depth;
  162. int gap_count;
  163. bool beta_repeaters_present;
  164. local_node = NULL;
  165. node = NULL;
  166. INIT_LIST_HEAD(&stack);
  167. stack_depth = 0;
  168. end = sid + self_id_count;
  169. phy_id = 0;
  170. irm_node = NULL;
  171. gap_count = SELF_ID_GAP_COUNT(*sid);
  172. beta_repeaters_present = false;
  173. while (sid < end) {
  174. next_sid = count_ports(sid, &port_count, &child_port_count);
  175. if (next_sid == NULL) {
  176. fw_error("Inconsistent extended self IDs.\n");
  177. return NULL;
  178. }
  179. q = *sid;
  180. if (phy_id != SELF_ID_PHY_ID(q)) {
  181. fw_error("PHY ID mismatch in self ID: %d != %d.\n",
  182. phy_id, SELF_ID_PHY_ID(q));
  183. return NULL;
  184. }
  185. if (child_port_count > stack_depth) {
  186. fw_error("Topology stack underflow\n");
  187. return NULL;
  188. }
  189. /*
  190. * Seek back from the top of our stack to find the
  191. * start of the child nodes for this node.
  192. */
  193. for (i = 0, h = &stack; i < child_port_count; i++)
  194. h = h->prev;
  195. /*
  196. * When the stack is empty, this yields an invalid value,
  197. * but that pointer will never be dereferenced.
  198. */
  199. child = fw_node(h);
  200. node = fw_node_create(q, port_count, card->color);
  201. if (node == NULL) {
  202. fw_error("Out of memory while building topology.\n");
  203. return NULL;
  204. }
  205. if (phy_id == (card->node_id & 0x3f))
  206. local_node = node;
  207. if (SELF_ID_CONTENDER(q))
  208. irm_node = node;
  209. parent_count = 0;
  210. for (i = 0; i < port_count; i++) {
  211. switch (get_port_type(sid, i)) {
  212. case SELFID_PORT_PARENT:
  213. /*
  214. * Who's your daddy? We dont know the
  215. * parent node at this time, so we
  216. * temporarily abuse node->color for
  217. * remembering the entry in the
  218. * node->ports array where the parent
  219. * node should be. Later, when we
  220. * handle the parent node, we fix up
  221. * the reference.
  222. */
  223. parent_count++;
  224. node->color = i;
  225. break;
  226. case SELFID_PORT_CHILD:
  227. node->ports[i] = child;
  228. /*
  229. * Fix up parent reference for this
  230. * child node.
  231. */
  232. child->ports[child->color] = node;
  233. child->color = card->color;
  234. child = fw_node(child->link.next);
  235. break;
  236. }
  237. }
  238. /*
  239. * Check that the node reports exactly one parent
  240. * port, except for the root, which of course should
  241. * have no parents.
  242. */
  243. if ((next_sid == end && parent_count != 0) ||
  244. (next_sid < end && parent_count != 1)) {
  245. fw_error("Parent port inconsistency for node %d: "
  246. "parent_count=%d\n", phy_id, parent_count);
  247. return NULL;
  248. }
  249. /* Pop the child nodes off the stack and push the new node. */
  250. __list_del(h->prev, &stack);
  251. list_add_tail(&node->link, &stack);
  252. stack_depth += 1 - child_port_count;
  253. if (node->phy_speed == SCODE_BETA &&
  254. parent_count + child_port_count > 1)
  255. beta_repeaters_present = true;
  256. /*
  257. * If PHYs report different gap counts, set an invalid count
  258. * which will force a gap count reconfiguration and a reset.
  259. */
  260. if (SELF_ID_GAP_COUNT(q) != gap_count)
  261. gap_count = 0;
  262. update_hop_count(node);
  263. sid = next_sid;
  264. phy_id++;
  265. }
  266. card->root_node = node;
  267. card->irm_node = irm_node;
  268. card->gap_count = gap_count;
  269. card->beta_repeaters_present = beta_repeaters_present;
  270. return local_node;
  271. }
  272. typedef void (*fw_node_callback_t)(struct fw_card * card,
  273. struct fw_node * node,
  274. struct fw_node * parent);
  275. static void for_each_fw_node(struct fw_card *card, struct fw_node *root,
  276. fw_node_callback_t callback)
  277. {
  278. struct list_head list;
  279. struct fw_node *node, *next, *child, *parent;
  280. int i;
  281. INIT_LIST_HEAD(&list);
  282. fw_node_get(root);
  283. list_add_tail(&root->link, &list);
  284. parent = NULL;
  285. list_for_each_entry(node, &list, link) {
  286. node->color = card->color;
  287. for (i = 0; i < node->port_count; i++) {
  288. child = node->ports[i];
  289. if (!child)
  290. continue;
  291. if (child->color == card->color)
  292. parent = child;
  293. else {
  294. fw_node_get(child);
  295. list_add_tail(&child->link, &list);
  296. }
  297. }
  298. callback(card, node, parent);
  299. }
  300. list_for_each_entry_safe(node, next, &list, link)
  301. fw_node_put(node);
  302. }
  303. static void report_lost_node(struct fw_card *card,
  304. struct fw_node *node, struct fw_node *parent)
  305. {
  306. fw_node_event(card, node, FW_NODE_DESTROYED);
  307. fw_node_put(node);
  308. /* Topology has changed - reset bus manager retry counter */
  309. card->bm_retries = 0;
  310. }
  311. static void report_found_node(struct fw_card *card,
  312. struct fw_node *node, struct fw_node *parent)
  313. {
  314. int b_path = (node->phy_speed == SCODE_BETA);
  315. if (parent != NULL) {
  316. /* min() macro doesn't work here with gcc 3.4 */
  317. node->max_speed = parent->max_speed < node->phy_speed ?
  318. parent->max_speed : node->phy_speed;
  319. node->b_path = parent->b_path && b_path;
  320. } else {
  321. node->max_speed = node->phy_speed;
  322. node->b_path = b_path;
  323. }
  324. fw_node_event(card, node, FW_NODE_CREATED);
  325. /* Topology has changed - reset bus manager retry counter */
  326. card->bm_retries = 0;
  327. }
  328. void fw_destroy_nodes(struct fw_card *card)
  329. {
  330. unsigned long flags;
  331. spin_lock_irqsave(&card->lock, flags);
  332. card->color++;
  333. if (card->local_node != NULL)
  334. for_each_fw_node(card, card->local_node, report_lost_node);
  335. card->local_node = NULL;
  336. spin_unlock_irqrestore(&card->lock, flags);
  337. }
  338. static void move_tree(struct fw_node *node0, struct fw_node *node1, int port)
  339. {
  340. struct fw_node *tree;
  341. int i;
  342. tree = node1->ports[port];
  343. node0->ports[port] = tree;
  344. for (i = 0; i < tree->port_count; i++) {
  345. if (tree->ports[i] == node1) {
  346. tree->ports[i] = node0;
  347. break;
  348. }
  349. }
  350. }
  351. /*
  352. * Compare the old topology tree for card with the new one specified by root.
  353. * Queue the nodes and mark them as either found, lost or updated.
  354. * Update the nodes in the card topology tree as we go.
  355. */
  356. static void update_tree(struct fw_card *card, struct fw_node *root)
  357. {
  358. struct list_head list0, list1;
  359. struct fw_node *node0, *node1, *next1;
  360. int i, event;
  361. INIT_LIST_HEAD(&list0);
  362. list_add_tail(&card->local_node->link, &list0);
  363. INIT_LIST_HEAD(&list1);
  364. list_add_tail(&root->link, &list1);
  365. node0 = fw_node(list0.next);
  366. node1 = fw_node(list1.next);
  367. while (&node0->link != &list0) {
  368. WARN_ON(node0->port_count != node1->port_count);
  369. if (node0->link_on && !node1->link_on)
  370. event = FW_NODE_LINK_OFF;
  371. else if (!node0->link_on && node1->link_on)
  372. event = FW_NODE_LINK_ON;
  373. else if (node1->initiated_reset && node1->link_on)
  374. event = FW_NODE_INITIATED_RESET;
  375. else
  376. event = FW_NODE_UPDATED;
  377. node0->node_id = node1->node_id;
  378. node0->color = card->color;
  379. node0->link_on = node1->link_on;
  380. node0->initiated_reset = node1->initiated_reset;
  381. node0->max_hops = node1->max_hops;
  382. node1->color = card->color;
  383. fw_node_event(card, node0, event);
  384. if (card->root_node == node1)
  385. card->root_node = node0;
  386. if (card->irm_node == node1)
  387. card->irm_node = node0;
  388. for (i = 0; i < node0->port_count; i++) {
  389. if (node0->ports[i] && node1->ports[i]) {
  390. /*
  391. * This port didn't change, queue the
  392. * connected node for further
  393. * investigation.
  394. */
  395. if (node0->ports[i]->color == card->color)
  396. continue;
  397. list_add_tail(&node0->ports[i]->link, &list0);
  398. list_add_tail(&node1->ports[i]->link, &list1);
  399. } else if (node0->ports[i]) {
  400. /*
  401. * The nodes connected here were
  402. * unplugged; unref the lost nodes and
  403. * queue FW_NODE_LOST callbacks for
  404. * them.
  405. */
  406. for_each_fw_node(card, node0->ports[i],
  407. report_lost_node);
  408. node0->ports[i] = NULL;
  409. } else if (node1->ports[i]) {
  410. /*
  411. * One or more node were connected to
  412. * this port. Move the new nodes into
  413. * the tree and queue FW_NODE_CREATED
  414. * callbacks for them.
  415. */
  416. move_tree(node0, node1, i);
  417. for_each_fw_node(card, node0->ports[i],
  418. report_found_node);
  419. }
  420. }
  421. node0 = fw_node(node0->link.next);
  422. next1 = fw_node(node1->link.next);
  423. fw_node_put(node1);
  424. node1 = next1;
  425. }
  426. }
  427. static void update_topology_map(struct fw_card *card,
  428. u32 *self_ids, int self_id_count)
  429. {
  430. int node_count = (card->root_node->node_id & 0x3f) + 1;
  431. __be32 *map = card->topology_map;
  432. *map++ = cpu_to_be32((self_id_count + 2) << 16);
  433. *map++ = cpu_to_be32(be32_to_cpu(card->topology_map[1]) + 1);
  434. *map++ = cpu_to_be32((node_count << 16) | self_id_count);
  435. while (self_id_count--)
  436. *map++ = cpu_to_be32p(self_ids++);
  437. fw_compute_block_crc(card->topology_map);
  438. }
  439. void fw_core_handle_bus_reset(struct fw_card *card, int node_id, int generation,
  440. int self_id_count, u32 *self_ids, bool bm_abdicate)
  441. {
  442. struct fw_node *local_node;
  443. unsigned long flags;
  444. /*
  445. * If the selfID buffer is not the immediate successor of the
  446. * previously processed one, we cannot reliably compare the
  447. * old and new topologies.
  448. */
  449. if (!is_next_generation(generation, card->generation) &&
  450. card->local_node != NULL) {
  451. fw_notify("skipped bus generations, destroying all nodes\n");
  452. fw_destroy_nodes(card);
  453. card->bm_retries = 0;
  454. }
  455. spin_lock_irqsave(&card->lock, flags);
  456. card->broadcast_channel_allocated = card->broadcast_channel_auto_allocated;
  457. card->node_id = node_id;
  458. /*
  459. * Update node_id before generation to prevent anybody from using
  460. * a stale node_id together with a current generation.
  461. */
  462. smp_wmb();
  463. card->generation = generation;
  464. card->reset_jiffies = get_jiffies_64();
  465. card->bm_node_id = 0xffff;
  466. card->bm_abdicate = bm_abdicate;
  467. fw_schedule_bm_work(card, 0);
  468. local_node = build_tree(card, self_ids, self_id_count);
  469. update_topology_map(card, self_ids, self_id_count);
  470. card->color++;
  471. if (local_node == NULL) {
  472. fw_error("topology build failed\n");
  473. /* FIXME: We need to issue a bus reset in this case. */
  474. } else if (card->local_node == NULL) {
  475. card->local_node = local_node;
  476. for_each_fw_node(card, local_node, report_found_node);
  477. } else {
  478. update_tree(card, local_node);
  479. }
  480. spin_unlock_irqrestore(&card->lock, flags);
  481. }
  482. EXPORT_SYMBOL(fw_core_handle_bus_reset);