1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Incremental bus scan, based on bus topology 4 * 5 * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net> 6 */ 7 8 #include <linux/bug.h> 9 #include <linux/errno.h> 10 #include <linux/firewire.h> 11 #include <linux/firewire-constants.h> 12 #include <linux/jiffies.h> 13 #include <linux/kernel.h> 14 #include <linux/list.h> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/spinlock.h> 18 19 #include <linux/atomic.h> 20 #include <asm/byteorder.h> 21 22 #include "core.h" 23 #include "phy-packet-definitions.h" 24 #include <trace/events/firewire.h> 25 26 static struct fw_node *fw_node_create(u32 sid, int port_count, int color) 27 { 28 struct fw_node *node; 29 30 node = kzalloc_flex(*node, ports, port_count, GFP_ATOMIC); 31 if (node == NULL) 32 return NULL; 33 34 node->color = color; 35 node->node_id = LOCAL_BUS | phy_packet_self_id_get_phy_id(sid); 36 node->link_on = phy_packet_self_id_zero_get_link_active(sid); 37 // NOTE: Only two bits, thus only for SCODE_100, SCODE_200, SCODE_400, and SCODE_BETA. 38 node->phy_speed = phy_packet_self_id_zero_get_scode(sid); 39 node->initiated_reset = phy_packet_self_id_zero_get_initiated_reset(sid); 40 node->port_count = port_count; 41 42 kref_init(&node->kref); 43 INIT_LIST_HEAD(&node->link); 44 45 return node; 46 } 47 48 /* 49 * Compute the maximum hop count for this node and it's children. The 50 * maximum hop count is the maximum number of connections between any 51 * two nodes in the subtree rooted at this node. We need this for 52 * setting the gap count. As we build the tree bottom up in 53 * build_tree() below, this is fairly easy to do: for each node we 54 * maintain the max hop count and the max depth, ie the number of hops 55 * to the furthest leaf. Computing the max hop count breaks down into 56 * two cases: either the path goes through this node, in which case 57 * the hop count is the sum of the two biggest child depths plus 2. 58 * Or it could be the case that the max hop path is entirely 59 * contained in a child tree, in which case the max hop count is just 60 * the max hop count of this child. 61 */ 62 static void update_hop_count(struct fw_node *node) 63 { 64 int depths[2] = { -1, -1 }; 65 int max_child_hops = 0; 66 int i; 67 68 for (i = 0; i < node->port_count; i++) { 69 if (node->ports[i] == NULL) 70 continue; 71 72 if (node->ports[i]->max_hops > max_child_hops) 73 max_child_hops = node->ports[i]->max_hops; 74 75 if (node->ports[i]->max_depth > depths[0]) { 76 depths[1] = depths[0]; 77 depths[0] = node->ports[i]->max_depth; 78 } else if (node->ports[i]->max_depth > depths[1]) 79 depths[1] = node->ports[i]->max_depth; 80 } 81 82 node->max_depth = depths[0] + 1; 83 node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2); 84 } 85 86 static inline struct fw_node *fw_node(struct list_head *l) 87 { 88 return list_entry(l, struct fw_node, link); 89 } 90 91 typedef void (*fw_node_callback_t)(struct fw_card *card, struct fw_node *node, 92 struct fw_node *parent); 93 94 static void for_each_fw_node(struct fw_card *card, struct fw_node *root, 95 fw_node_callback_t callback); 96 97 static void free_fw_node(struct fw_card *card, struct fw_node *node, struct fw_node *parent) 98 { 99 kfree(node); 100 } 101 102 /* 103 * This function builds the tree representation of the topology given 104 * by the self IDs from the latest bus reset. During the construction 105 * of the tree, the function checks that the self IDs are valid and 106 * internally consistent. On success this function returns the 107 * fw_node corresponding to the local card otherwise NULL. 108 */ 109 static struct fw_node *build_tree(struct fw_card *card, const u32 *sid, int self_id_count, 110 unsigned int generation) 111 { 112 struct self_id_sequence_enumerator enumerator = { 113 .cursor = sid, 114 .quadlet_count = self_id_count, 115 }; 116 struct fw_node *node, *child, *local_node, *irm_node; 117 struct list_head stack; 118 int phy_id, stack_depth; 119 int gap_count; 120 bool beta_repeaters_present; 121 122 local_node = NULL; 123 node = NULL; 124 INIT_LIST_HEAD(&stack); 125 stack_depth = 0; 126 phy_id = 0; 127 irm_node = NULL; 128 gap_count = phy_packet_self_id_zero_get_gap_count(*sid); 129 beta_repeaters_present = false; 130 131 while (enumerator.quadlet_count > 0) { 132 unsigned int child_port_count = 0; 133 unsigned int parent_port_count = 0; 134 unsigned int total_port_count = 0; 135 unsigned int quadlet_count; 136 const u32 *self_id_sequence; 137 unsigned int port_capacity; 138 enum phy_packet_self_id_port_status port_status; 139 unsigned int port_index; 140 struct list_head *h; 141 int i; 142 143 self_id_sequence = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 144 if (IS_ERR(self_id_sequence)) { 145 if (PTR_ERR(self_id_sequence) != -ENODATA) { 146 fw_err(card, "inconsistent extended self IDs: %ld\n", 147 PTR_ERR(self_id_sequence)); 148 goto error; 149 } 150 break; 151 } 152 153 port_capacity = self_id_sequence_get_port_capacity(quadlet_count); 154 trace_self_id_sequence(card->index, self_id_sequence, quadlet_count, generation); 155 156 for (port_index = 0; port_index < port_capacity; ++port_index) { 157 port_status = self_id_sequence_get_port_status(self_id_sequence, quadlet_count, 158 port_index); 159 switch (port_status) { 160 case PHY_PACKET_SELF_ID_PORT_STATUS_CHILD: 161 ++child_port_count; 162 break; 163 case PHY_PACKET_SELF_ID_PORT_STATUS_PARENT: 164 ++parent_port_count; 165 break; 166 case PHY_PACKET_SELF_ID_PORT_STATUS_NCONN: 167 ++total_port_count; 168 break; 169 case PHY_PACKET_SELF_ID_PORT_STATUS_NONE: 170 default: 171 break; 172 } 173 } 174 total_port_count += child_port_count + parent_port_count; 175 176 // Check that the node reports exactly one parent port, except for the root, which 177 // of course should have no parents. 178 if ((enumerator.quadlet_count == 0 && parent_port_count != 0) || 179 (enumerator.quadlet_count > 0 && parent_port_count != 1)) { 180 fw_err(card, "parent port inconsistency for node %d: parent_count=%d\n", 181 phy_id, parent_port_count); 182 goto error; 183 } 184 185 if (phy_id != phy_packet_self_id_get_phy_id(self_id_sequence[0])) { 186 fw_err(card, "PHY ID mismatch in self ID: %d != %d\n", 187 phy_id, phy_packet_self_id_get_phy_id(self_id_sequence[0])); 188 goto error; 189 } 190 191 if (child_port_count > stack_depth) { 192 fw_err(card, "topology stack underflow\n"); 193 goto error; 194 } 195 196 /* 197 * Seek back from the top of our stack to find the 198 * start of the child nodes for this node. 199 */ 200 for (i = 0, h = &stack; i < child_port_count; i++) 201 h = h->prev; 202 /* 203 * When the stack is empty, this yields an invalid value, 204 * but that pointer will never be dereferenced. 205 */ 206 child = fw_node(h); 207 208 node = fw_node_create(self_id_sequence[0], total_port_count, card->color); 209 if (node == NULL) { 210 fw_err(card, "out of memory while building topology\n"); 211 goto error; 212 } 213 214 if (phy_id == (card->node_id & 0x3f)) 215 local_node = node; 216 217 if (phy_packet_self_id_zero_get_contender(self_id_sequence[0])) 218 irm_node = node; 219 220 for (port_index = 0; port_index < total_port_count; ++port_index) { 221 port_status = self_id_sequence_get_port_status(self_id_sequence, quadlet_count, 222 port_index); 223 switch (port_status) { 224 case PHY_PACKET_SELF_ID_PORT_STATUS_PARENT: 225 // Who's your daddy? We dont know the parent node at this time, so 226 // we temporarily abuse node->color for remembering the entry in 227 // the node->ports array where the parent node should be. Later, 228 // when we handle the parent node, we fix up the reference. 229 node->color = port_index; 230 break; 231 232 case PHY_PACKET_SELF_ID_PORT_STATUS_CHILD: 233 node->ports[port_index] = child; 234 // Fix up parent reference for this child node. 235 child->ports[child->color] = node; 236 child->color = card->color; 237 child = fw_node(child->link.next); 238 break; 239 case PHY_PACKET_SELF_ID_PORT_STATUS_NCONN: 240 case PHY_PACKET_SELF_ID_PORT_STATUS_NONE: 241 default: 242 break; 243 } 244 } 245 246 /* Pop the child nodes off the stack and push the new node. */ 247 __list_del(h->prev, &stack); 248 list_add_tail(&node->link, &stack); 249 stack_depth += 1 - child_port_count; 250 251 if (node->phy_speed == SCODE_BETA && parent_port_count + child_port_count > 1) 252 beta_repeaters_present = true; 253 254 // If PHYs report different gap counts, set an invalid count which will force a gap 255 // count reconfiguration and a reset. 256 if (phy_packet_self_id_zero_get_gap_count(self_id_sequence[0]) != gap_count) 257 gap_count = GAP_COUNT_MISMATCHED; 258 259 update_hop_count(node); 260 261 phy_id++; 262 } 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 269 return local_node; 270 error: 271 ++card->color; 272 list_for_each_entry_safe(node, child, &stack, link) 273 for_each_fw_node(card, node, free_fw_node); 274 return NULL; 275 } 276 277 static void for_each_fw_node(struct fw_card *card, struct fw_node *root, 278 fw_node_callback_t callback) 279 { 280 struct list_head list; 281 struct fw_node *node, *next, *child, *parent; 282 int i; 283 284 INIT_LIST_HEAD(&list); 285 286 fw_node_get(root); 287 list_add_tail(&root->link, &list); 288 parent = NULL; 289 for (node = list_first_entry(&list, typeof(*node), link); 290 !list_entry_is_head(node, &list, link); 291 node = list_next_entry(node, link)) { 292 node->color = card->color; 293 294 for (i = 0; i < node->port_count; i++) { 295 child = node->ports[i]; 296 if (!child) 297 continue; 298 if (child->color == card->color) 299 parent = child; 300 else { 301 fw_node_get(child); 302 list_add_tail(&child->link, &list); 303 } 304 } 305 306 callback(card, node, parent); 307 } 308 309 list_for_each_entry_safe(node, next, &list, link) 310 fw_node_put(node); 311 } 312 313 static void report_lost_node(struct fw_card *card, 314 struct fw_node *node, struct fw_node *parent) 315 { 316 fw_node_event(card, node, FW_NODE_DESTROYED); 317 fw_node_put(node); 318 319 /* Topology has changed - reset bus manager retry counter */ 320 card->bm_retries = 0; 321 } 322 323 static void report_found_node(struct fw_card *card, 324 struct fw_node *node, struct fw_node *parent) 325 { 326 int b_path = (node->phy_speed == SCODE_BETA); 327 328 if (parent != NULL) { 329 /* min() macro doesn't work here with gcc 3.4 */ 330 node->max_speed = parent->max_speed < node->phy_speed ? 331 parent->max_speed : node->phy_speed; 332 node->b_path = parent->b_path && b_path; 333 } else { 334 node->max_speed = node->phy_speed; 335 node->b_path = b_path; 336 } 337 338 fw_node_event(card, node, FW_NODE_CREATED); 339 340 /* Topology has changed - reset bus manager retry counter */ 341 card->bm_retries = 0; 342 } 343 344 void fw_destroy_nodes(struct fw_card *card) 345 __must_hold(&card->lock) 346 { 347 lockdep_assert_held(&card->lock); 348 349 card->color++; 350 if (card->local_node != NULL) 351 for_each_fw_node(card, card->local_node, report_lost_node); 352 card->local_node = NULL; 353 } 354 355 static void move_tree(struct fw_node *node0, struct fw_node *node1, int port) 356 { 357 struct fw_node *tree; 358 int i; 359 360 tree = node1->ports[port]; 361 node0->ports[port] = tree; 362 for (i = 0; i < tree->port_count; i++) { 363 if (tree->ports[i] == node1) { 364 tree->ports[i] = node0; 365 break; 366 } 367 } 368 } 369 370 /* 371 * Compare the old topology tree for card with the new one specified by root. 372 * Queue the nodes and mark them as either found, lost or updated. 373 * Update the nodes in the card topology tree as we go. 374 */ 375 static void update_tree(struct fw_card *card, struct fw_node *root) 376 { 377 struct list_head list0, list1; 378 struct fw_node *node0, *node1, *next1; 379 int i, event; 380 381 INIT_LIST_HEAD(&list0); 382 list_add_tail(&card->local_node->link, &list0); 383 INIT_LIST_HEAD(&list1); 384 list_add_tail(&root->link, &list1); 385 386 node0 = fw_node(list0.next); 387 node1 = fw_node(list1.next); 388 389 while (&node0->link != &list0) { 390 WARN_ON(node0->port_count != node1->port_count); 391 392 if (node0->link_on && !node1->link_on) 393 event = FW_NODE_LINK_OFF; 394 else if (!node0->link_on && node1->link_on) 395 event = FW_NODE_LINK_ON; 396 else if (node1->initiated_reset && node1->link_on) 397 event = FW_NODE_INITIATED_RESET; 398 else 399 event = FW_NODE_UPDATED; 400 401 node0->node_id = node1->node_id; 402 node0->color = card->color; 403 node0->link_on = node1->link_on; 404 node0->initiated_reset = node1->initiated_reset; 405 node0->max_hops = node1->max_hops; 406 node1->color = card->color; 407 fw_node_event(card, node0, event); 408 409 if (card->root_node == node1) 410 card->root_node = node0; 411 if (card->irm_node == node1) 412 card->irm_node = node0; 413 414 for (i = 0; i < node0->port_count; i++) { 415 if (node0->ports[i] && node1->ports[i]) { 416 /* 417 * This port didn't change, queue the 418 * connected node for further 419 * investigation. 420 */ 421 if (node0->ports[i]->color == card->color) 422 continue; 423 list_add_tail(&node0->ports[i]->link, &list0); 424 list_add_tail(&node1->ports[i]->link, &list1); 425 } else if (node0->ports[i]) { 426 /* 427 * The nodes connected here were 428 * unplugged; unref the lost nodes and 429 * queue FW_NODE_LOST callbacks for 430 * them. 431 */ 432 433 for_each_fw_node(card, node0->ports[i], 434 report_lost_node); 435 node0->ports[i] = NULL; 436 } else if (node1->ports[i]) { 437 /* 438 * One or more node were connected to 439 * this port. Move the new nodes into 440 * the tree and queue FW_NODE_CREATED 441 * callbacks for them. 442 */ 443 move_tree(node0, node1, i); 444 for_each_fw_node(card, node0->ports[i], 445 report_found_node); 446 } 447 } 448 449 node0 = fw_node(node0->link.next); 450 next1 = fw_node(node1->link.next); 451 fw_node_put(node1); 452 node1 = next1; 453 } 454 } 455 456 static void update_topology_map(__be32 *buffer, size_t buffer_size, int root_node_id, 457 const u32 *self_ids, int self_id_count) 458 { 459 __be32 *map = buffer; 460 u32 next_generation = be32_to_cpu(buffer[1]) + 1; 461 int node_count = (root_node_id & 0x3f) + 1; 462 463 memset(map, 0, buffer_size); 464 465 *map++ = cpu_to_be32((self_id_count + 2) << 16); 466 *map++ = cpu_to_be32(next_generation); 467 *map++ = cpu_to_be32((node_count << 16) | self_id_count); 468 469 while (self_id_count--) 470 *map++ = cpu_to_be32p(self_ids++); 471 472 fw_compute_block_crc(buffer); 473 } 474 475 void fw_core_handle_bus_reset(struct fw_card *card, int node_id, int generation, 476 int self_id_count, u32 *self_ids, bool bm_abdicate) 477 { 478 struct fw_node *local_node; 479 480 trace_bus_reset_handle(card->index, generation, node_id, bm_abdicate, self_ids, self_id_count); 481 482 scoped_guard(spinlock, &card->lock) { 483 // If the selfID buffer is not the immediate successor of the 484 // previously processed one, we cannot reliably compare the 485 // old and new topologies. 486 if (!is_next_generation(generation, card->generation) && card->local_node != NULL) { 487 fw_destroy_nodes(card); 488 card->bm_retries = 0; 489 } 490 card->broadcast_channel_allocated = card->broadcast_channel_auto_allocated; 491 card->node_id = node_id; 492 // Update node_id before generation to prevent anybody from using 493 // a stale node_id together with a current generation. 494 smp_wmb(); 495 card->generation = generation; 496 card->reset_jiffies = get_jiffies_64(); 497 card->bm_node_id = 0xffff; 498 card->bm_abdicate = bm_abdicate; 499 500 local_node = build_tree(card, self_ids, self_id_count, generation); 501 502 card->color++; 503 504 if (local_node == NULL) { 505 fw_err(card, "topology build failed\n"); 506 // FIXME: We need to issue a bus reset in this case. 507 } else if (card->local_node == NULL) { 508 card->local_node = local_node; 509 for_each_fw_node(card, local_node, report_found_node); 510 } else { 511 update_tree(card, local_node); 512 } 513 } 514 515 fw_schedule_bm_work(card, 0); 516 517 // Just used by transaction layer. 518 scoped_guard(spinlock, &card->topology_map.lock) { 519 update_topology_map(card->topology_map.buffer, sizeof(card->topology_map.buffer), 520 card->root_node->node_id, self_ids, self_id_count); 521 } 522 } 523 EXPORT_SYMBOL(fw_core_handle_bus_reset); 524 525 #ifdef CONFIG_FIREWIRE_KUNIT_NODE_TREE_TEST 526 #include "node-tree-test.c" 527 #endif 528