core-topology.c 15 KB

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