core-topology.c 15 KB

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