2 * Node information (ConfigROM) collection and management.
4 * Copyright (C) 2000 Andreas E. Bombe
5 * 2001-2003 Ben Collins <bcollins@debian.net>
7 * This code is licensed under the GPL. See the file COPYING in the root
8 * directory of the kernel sources for details.
11 #include <linux/kernel.h>
12 #include <linux/config.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/smp_lock.h>
16 #include <linux/interrupt.h>
17 #include <linux/kmod.h>
18 #include <linux/completion.h>
19 #include <linux/delay.h>
21 #include <linux/proc_fs.h>
24 #include "ieee1394_types.h"
28 #include "ieee1394_transactions.h"
29 #include "highlevel.h"
35 static char *nodemgr_find_oui_name(int oui)
37 #ifdef CONFIG_IEEE1394_OUI_DB
38 extern struct oui_list_struct {
44 for (i = 0; oui_list[i].name; i++)
45 if (oui_list[i].oui == oui)
46 return oui_list[i].name;
53 * Basically what we do here is start off retrieving the bus_info block.
54 * From there will fill in some info about the node, verify it is of IEEE
55 * 1394 type, and that the crc checks out ok. After that we start off with
56 * the root directory, and subdirectories. To do this, we retrieve the
57 * quadlet header for a directory, find out the length, and retrieve the
58 * complete directory entry (be it a leaf or a directory). We then process
59 * it and add the info to our structure for that particular node.
61 * We verify CRC's along the way for each directory/block/leaf. The entire
62 * node structure is generic, and simply stores the information in a way
63 * that's easy to parse by the protocol interface.
66 /* The nodemgr maintains a number of data structures: the node list,
67 * the driver list, unit directory list and the host info list. The
68 * first three lists are accessed from process context only: /proc
69 * readers, insmod and rmmod, and the nodemgr thread. Access to these
70 * lists are serialized by means of the nodemgr_serialize mutex, which
71 * must be taken before accessing the structures and released
72 * afterwards. The host info list is only accessed during insmod,
73 * rmmod and from interrupt and allways only for a short period of
74 * time, so a spinlock is used to protect this list.
77 static DECLARE_MUTEX(nodemgr_serialize);
78 static LIST_HEAD(node_list);
79 static LIST_HEAD(driver_list);
80 static LIST_HEAD(unit_directory_list);
84 struct hpsb_host *host;
85 struct completion exited;
86 struct semaphore reset_sem;
91 static struct hpsb_highlevel nodemgr_highlevel;
95 #define PUTF(fmt, args...) \
97 len += sprintf(page + len, fmt, ## args); \
103 if (pos > off + count) \
108 static int raw1394_read_proc(char *page, char **start, off_t off,
109 int count, int *eof, void *data)
111 struct list_head *lh;
112 struct node_entry *ne;
113 off_t begin = 0, pos = 0;
116 if (down_interruptible(&nodemgr_serialize))
119 list_for_each(lh, &node_list) {
121 int ud_count = 0, lud_count = 0;
123 ne = list_entry(lh, struct node_entry, list);
127 PUTF("Node[" NODE_BUS_FMT "] GUID[%016Lx]:\n",
128 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
130 /* Generic Node information */
131 PUTF(" Vendor ID: `%s' [0x%06x]\n",
132 ne->vendor_name ?: "Unknown", ne->vendor_id);
133 PUTF(" Capabilities: 0x%06x\n", ne->capabilities);
134 PUTF(" Bus Options:\n");
135 PUTF(" IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d)\n"
136 " LSPD(%d) MAX_REC(%d) CYC_CLK_ACC(%d)\n",
137 ne->busopt.irmc, ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
138 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
139 ne->busopt.max_rec, ne->busopt.cyc_clk_acc);
141 /* If this is the host entry, output some info about it aswell */
142 if (ne->host != NULL && ne->host->node_id == ne->nodeid) {
143 PUTF(" Host Node Status:\n");
144 PUTF(" Host Driver : %s\n", ne->host->driver->name);
145 PUTF(" Nodes connected : %d\n", ne->host->node_count);
146 PUTF(" Nodes active : %d\n", ne->host->nodes_active);
147 PUTF(" SelfIDs received: %d\n", ne->host->selfid_count);
148 PUTF(" Irm ID : [" NODE_BUS_FMT "]\n",
149 NODE_BUS_ARGS(ne->host, ne->host->irm_id));
150 PUTF(" BusMgr ID : [" NODE_BUS_FMT "]\n",
151 NODE_BUS_ARGS(ne->host, ne->host->busmgr_id));
152 PUTF(" In Bus Reset : %s\n", ne->host->in_bus_reset ? "yes" : "no");
153 PUTF(" Root : %s\n", ne->host->is_root ? "yes" : "no");
154 PUTF(" Cycle Master : %s\n", ne->host->is_cycmst ? "yes" : "no");
155 PUTF(" IRM : %s\n", ne->host->is_irm ? "yes" : "no");
156 PUTF(" Bus Manager : %s\n", ne->host->is_busmgr ? "yes" : "no");
159 /* Now the unit directories */
160 list_for_each (l, &ne->unit_directories) {
161 struct unit_directory *ud = list_entry (l, struct unit_directory, node_list);
162 int printed = 0; // small hack
164 if (ud->parent == NULL)
165 PUTF(" Unit Directory %d:\n", lud_count++);
167 PUTF(" Logical Unit Directory %d:\n", ud_count++);
168 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
169 PUTF(" Vendor/Model ID: %s [%06x]",
170 ud->vendor_name ?: "Unknown", ud->vendor_id);
173 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
175 PUTF(" Vendor/Model ID: %s [%06x]",
176 ne->vendor_name ?: "Unknown", ne->vendor_id);
177 PUTF(" / %s [%06x]", ud->model_name ?: "Unknown", ud->model_id);
183 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
184 PUTF(" Software Specifier ID: %06x\n", ud->specifier_id);
185 if (ud->flags & UNIT_DIRECTORY_VERSION)
186 PUTF(" Software Version: %06x\n", ud->version);
188 PUTF(" Driver: %s\n", ud->driver->name);
189 PUTF(" Length (in quads): %d\n", ud->length);
195 up(&nodemgr_serialize);
197 *start = page + (off - begin);
198 len -= (off - begin);
211 #endif /* CONFIG_PROC_FS */
213 static void nodemgr_process_config_rom(struct node_entry *ne,
214 quadlet_t busoptions);
216 static int nodemgr_read_quadlet(struct hpsb_host *host,
217 nodeid_t nodeid, unsigned int generation,
218 octlet_t address, quadlet_t *quad)
223 for (i = 0; i < 3; i++) {
224 ret = hpsb_read(host, nodeid, generation, address, quad, 4);
228 set_current_state(TASK_INTERRUPTIBLE);
229 if (schedule_timeout (HZ/3))
232 *quad = be32_to_cpu(*quad);
237 static int nodemgr_size_text_leaf(struct hpsb_host *host,
238 nodeid_t nodeid, unsigned int generation,
244 if (nodemgr_read_quadlet(host, nodeid, generation, address, &quad))
247 if (CONFIG_ROM_KEY(quad) == CONFIG_ROM_DESCRIPTOR_LEAF) {
248 /* This is the offset. */
249 address += 4 * CONFIG_ROM_VALUE(quad);
250 if (nodemgr_read_quadlet(host, nodeid, generation, address, &quad))
252 /* Now we got the size of the text descriptor leaf. */
253 size = CONFIG_ROM_LEAF_LENGTH(quad);
259 static int nodemgr_read_text_leaf(struct node_entry *ne,
266 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation, address, &quad)
267 || CONFIG_ROM_KEY(quad) != CONFIG_ROM_DESCRIPTOR_LEAF)
270 /* This is the offset. */
271 address += 4 * CONFIG_ROM_VALUE(quad);
272 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation, address, &quad))
275 /* Now we got the size of the text descriptor leaf. */
276 size = CONFIG_ROM_LEAF_LENGTH(quad) - 2;
281 for (i = 0; i < 2; i++, address += 4, quadp++) {
282 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation, address, quadp))
286 /* Now read the text string. */
288 for (; size > 0; size--, address += 4, quadp++) {
289 for (i = 0; i < 3; i++) {
290 ret = hpsb_node_read(ne, address, quadp, 4);
301 static struct node_entry *nodemgr_scan_root_directory
302 (struct hpsb_host *host, nodeid_t nodeid, unsigned int generation)
307 int code, size, total_size;
308 struct node_entry *ne;
310 address = CSR_REGISTER_BASE + CSR_CONFIG_ROM;
312 if (nodemgr_read_quadlet(host, nodeid, generation, address, &quad))
315 if (CONFIG_ROM_BUS_INFO_LENGTH(quad) == 1) /* minimal config rom */
318 address += 4 + CONFIG_ROM_BUS_INFO_LENGTH(quad) * 4;
320 if (nodemgr_read_quadlet(host, nodeid, generation, address, &quad))
322 length = CONFIG_ROM_ROOT_LENGTH(quad);
326 total_size = sizeof(struct node_entry);
327 for (; length > 0; length--, address += 4) {
328 if (nodemgr_read_quadlet(host, nodeid, generation, address, &quad))
330 code = CONFIG_ROM_KEY(quad);
332 if (code == CONFIG_ROM_VENDOR_ID && length > 0) {
333 /* Check if there is a text descriptor leaf
334 immediately after this. */
335 size = nodemgr_size_text_leaf(host, nodeid, generation,
340 total_size += (size + 1) * sizeof (quadlet_t);
345 ne = kmalloc(total_size, GFP_KERNEL);
350 memset(ne, 0, total_size);
353 ne->vendor_name = (const char *) &(ne->quadlets[2]);
354 ne->quadlets[size] = 0;
356 ne->vendor_name = NULL;
362 static struct node_entry *nodemgr_create_node(octlet_t guid, quadlet_t busoptions,
363 struct host_info *hi,
364 nodeid_t nodeid, unsigned int generation)
366 struct hpsb_host *host = hi->host;
367 struct node_entry *ne;
369 ne = nodemgr_scan_root_directory (host, nodeid, generation);
370 if (!ne) return NULL;
372 INIT_LIST_HEAD(&ne->list);
373 INIT_LIST_HEAD(&ne->unit_directories);
376 ne->generation = generation;
378 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
379 ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
381 list_add_tail(&ne->list, &node_list);
383 nodemgr_process_config_rom (ne, busoptions);
385 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
386 (host->node_id == nodeid) ? "Host" : "Node",
387 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
392 static struct node_entry *find_entry_by_guid(u64 guid)
394 struct list_head *lh;
395 struct node_entry *ne;
397 list_for_each(lh, &node_list) {
398 ne = list_entry(lh, struct node_entry, list);
399 if (ne->guid == guid) return ne;
405 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
407 struct list_head *lh;
408 struct node_entry *ne;
410 list_for_each(lh, &node_list) {
411 ne = list_entry(lh, struct node_entry, list);
412 if (ne->nodeid == nodeid && ne->host == host)
419 static struct unit_directory *nodemgr_scan_unit_directory
420 (struct node_entry *ne, octlet_t address)
422 struct unit_directory *ud;
425 int length, size, total_size, count;
426 int vendor_name_size, model_name_size;
428 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation, address, &quad))
430 length = CONFIG_ROM_DIRECTORY_LENGTH(quad) ;
434 total_size = sizeof (struct unit_directory);
437 vendor_name_size = 0;
439 for (; length > 0; length--, address += 4) {
443 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation,
446 code = CONFIG_ROM_KEY(quad);
447 value = CONFIG_ROM_VALUE(quad);
451 case CONFIG_ROM_VENDOR_ID:
452 todo = UNIT_DIRECTORY_VENDOR_TEXT;
455 case CONFIG_ROM_MODEL_ID:
456 todo = UNIT_DIRECTORY_MODEL_TEXT;
459 case CONFIG_ROM_SPECIFIER_ID:
460 case CONFIG_ROM_UNIT_SW_VERSION:
463 case CONFIG_ROM_DESCRIPTOR_LEAF:
464 case CONFIG_ROM_DESCRIPTOR_DIRECTORY:
465 /* TODO: read strings... icons? */
469 /* Which types of quadlets do we want to
470 store? Only count immediate values and
471 CSR offsets for now. */
472 code &= CONFIG_ROM_KEY_TYPE_MASK;
473 if ((code & CONFIG_ROM_KEY_TYPE_LEAF) == 0)
478 if (todo && length > 0) {
479 /* Check if there is a text descriptor leaf
480 immediately after this. */
481 size = nodemgr_size_text_leaf(ne->host,
486 if (todo == UNIT_DIRECTORY_VENDOR_TEXT)
487 vendor_name_size = size;
489 model_name_size = size;
495 total_size += (size + 1) * sizeof (quadlet_t);
502 total_size += count * sizeof (quadlet_t);
503 ud = kmalloc (total_size, GFP_KERNEL);
506 memset (ud, 0, total_size);
509 ud->vendor_name_size = vendor_name_size;
510 ud->model_name_size = model_name_size;
517 /* This implementation currently only scans the config rom and its
518 * immediate unit directories looking for software_id and
519 * software_version entries, in order to get driver autoloading working. */
520 static struct unit_directory * nodemgr_process_unit_directory
521 (struct node_entry *ne, octlet_t address, struct unit_directory *parent)
523 struct unit_directory *ud;
527 struct unit_directory *ud_temp = NULL;
529 if (!(ud = nodemgr_scan_unit_directory(ne, address)))
530 goto unit_directory_error;
533 ud->address = address;
536 ud->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
540 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation,
542 goto unit_directory_error;
543 length = CONFIG_ROM_DIRECTORY_LENGTH(quad) ;
546 infop = (quadlet_t *) ud->quadlets;
547 for (; length > 0; length--, address += 4) {
552 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation,
554 goto unit_directory_error;
555 code = CONFIG_ROM_KEY(quad) ;
556 value = CONFIG_ROM_VALUE(quad);
559 case CONFIG_ROM_VENDOR_ID:
560 ud->vendor_id = value;
561 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
564 ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
566 if ((ud->flags & UNIT_DIRECTORY_VENDOR_TEXT) != 0) {
569 quadp = &(ud->quadlets[ud->length]);
570 if (nodemgr_read_text_leaf(ne, address, quadp) == 0
571 && quadp[0] == 0 && quadp[1] == 0) {
572 /* We only support minimal
573 ASCII and English. */
574 quadp[ud->vendor_name_size] = 0;
576 = (const char *) &(quadp[2]);
581 case CONFIG_ROM_MODEL_ID:
582 ud->model_id = value;
583 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
584 if ((ud->flags & UNIT_DIRECTORY_MODEL_TEXT) != 0) {
587 quadp = &(ud->quadlets[ud->length + ud->vendor_name_size + 1]);
588 if (nodemgr_read_text_leaf(ne, address, quadp) == 0
589 && quadp[0] == 0 && quadp[1] == 0) {
590 /* We only support minimal
591 ASCII and English. */
592 quadp[ud->model_name_size] = 0;
594 = (const char *) &(quadp[2]);
599 case CONFIG_ROM_SPECIFIER_ID:
600 ud->specifier_id = value;
601 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
604 case CONFIG_ROM_UNIT_SW_VERSION:
606 ud->flags |= UNIT_DIRECTORY_VERSION;
609 case CONFIG_ROM_DESCRIPTOR_LEAF:
610 case CONFIG_ROM_DESCRIPTOR_DIRECTORY:
611 /* TODO: read strings... icons? */
614 case CONFIG_ROM_LOGICAL_UNIT_DIRECTORY:
615 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
616 ud_temp = nodemgr_process_unit_directory(ne, address + value * 4, ud);
621 /* inherit unspecified values */
622 if ((ud->flags & UNIT_DIRECTORY_VENDOR_ID) &&
623 !(ud_temp->flags & UNIT_DIRECTORY_VENDOR_ID))
625 ud_temp->flags |= UNIT_DIRECTORY_VENDOR_ID;
626 ud_temp->vendor_id = ud->vendor_id;
628 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
629 !(ud_temp->flags & UNIT_DIRECTORY_MODEL_ID))
631 ud_temp->flags |= UNIT_DIRECTORY_MODEL_ID;
632 ud_temp->model_id = ud->model_id;
634 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
635 !(ud_temp->flags & UNIT_DIRECTORY_SPECIFIER_ID))
637 ud_temp->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
638 ud_temp->specifier_id = ud->specifier_id;
640 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
641 !(ud_temp->flags & UNIT_DIRECTORY_VERSION))
643 ud_temp->flags |= UNIT_DIRECTORY_VERSION;
644 ud_temp->version = ud->version;
650 /* Which types of quadlets do we want to
651 store? Only count immediate values and
652 CSR offsets for now. */
653 code &= CONFIG_ROM_KEY_TYPE_MASK;
654 if ((code & CONFIG_ROM_KEY_TYPE_LEAF) == 0)
660 list_add_tail(&ud->node_list, &ne->unit_directories);
661 list_add_tail(&ud->driver_list, &unit_directory_list);
665 unit_directory_error:
672 static void nodemgr_process_root_directory(struct node_entry *ne)
678 address = CSR_REGISTER_BASE + CSR_CONFIG_ROM;
680 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation,
683 address += 4 + CONFIG_ROM_BUS_INFO_LENGTH(quad) * 4;
685 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation,
688 length = CONFIG_ROM_ROOT_LENGTH(quad);
691 for (; length > 0; length--, address += 4) {
694 if (nodemgr_read_quadlet(ne->host, ne->nodeid, ne->generation,
697 code = CONFIG_ROM_KEY(quad);
698 value = CONFIG_ROM_VALUE(quad);
701 case CONFIG_ROM_VENDOR_ID:
702 ne->vendor_id = value;
705 ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
707 /* Now check if there is a vendor name text
709 if (ne->vendor_name != NULL) {
712 if (nodemgr_read_text_leaf(ne, address, ne->quadlets) != 0
713 || ne->quadlets[0] != 0 || ne->quadlets[1] != 0)
714 /* We only support minimal
715 ASCII and English. */
716 ne->vendor_name = NULL;
720 case CONFIG_ROM_NODE_CAPABILITES:
721 ne->capabilities = value;
724 case CONFIG_ROM_UNIT_DIRECTORY:
725 nodemgr_process_unit_directory(ne, address + value * 4, NULL);
728 case CONFIG_ROM_DESCRIPTOR_LEAF:
729 case CONFIG_ROM_DESCRIPTOR_DIRECTORY:
730 /* TODO: read strings... icons? */
736 #ifdef CONFIG_HOTPLUG
738 static void nodemgr_call_policy(char *verb, struct unit_directory *ud)
740 char *argv [3], **envp, *buf, *scratch;
743 if (!hotplug_path [0])
745 if (!current->fs->root)
747 if (!(envp = (char **) kmalloc(20 * sizeof (char *), GFP_KERNEL))) {
748 HPSB_DEBUG ("ENOMEM");
751 if (!(buf = kmalloc(256, GFP_KERNEL))) {
753 HPSB_DEBUG("ENOMEM2");
757 /* only one standardized param to hotplug command: type */
758 argv[0] = hotplug_path;
759 argv[1] = "ieee1394";
762 /* minimal command environment */
763 envp[i++] = "HOME=/";
764 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
766 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
767 /* hint that policy agent should enter no-stdout debug mode */
768 envp[i++] = "DEBUG=kernel";
770 /* extensible set of named bus-specific parameters,
771 * supporting multiple driver selection algorithms.
776 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
778 scratch += sprintf(scratch, "VENDOR_ID=%06x", ud->vendor_id) + 1;
780 scratch += sprintf(scratch, "GUID=%016Lx", (long long unsigned)ud->ne->guid) + 1;
782 scratch += sprintf(scratch, "SPECIFIER_ID=%06x", ud->specifier_id) + 1;
784 scratch += sprintf(scratch, "VERSION=%06x", ud->version) + 1;
787 /* NOTE: user mode daemons can call the agents too */
788 HPSB_VERBOSE("NodeMgr: %s %s %016Lx", argv[0], verb, (long long unsigned)ud->ne->guid);
790 value = call_usermodehelper(argv[0], argv, envp);
794 HPSB_DEBUG("NodeMgr: hotplug policy returned %d", value);
800 nodemgr_call_policy(char *verb, struct unit_directory *ud)
802 HPSB_VERBOSE("NodeMgr: nodemgr_call_policy(): hotplug not enabled");
806 #endif /* CONFIG_HOTPLUG */
808 static void nodemgr_claim_unit_directory(struct unit_directory *ud,
809 struct hpsb_protocol_driver *driver)
812 list_move_tail(&ud->driver_list, &driver->unit_directories);
815 static void nodemgr_release_unit_directory(struct unit_directory *ud)
818 list_move_tail(&ud->driver_list, &unit_directory_list);
821 void hpsb_release_unit_directory(struct unit_directory *ud)
823 down(&nodemgr_serialize);
824 nodemgr_release_unit_directory(ud);
825 up(&nodemgr_serialize);
828 static void nodemgr_free_unit_directories(struct node_entry *ne)
830 struct list_head *lh, *next;
831 struct unit_directory *ud;
833 list_for_each_safe(lh, next, &ne->unit_directories) {
834 ud = list_entry(lh, struct unit_directory, node_list);
836 if (ud->driver && ud->driver->disconnect)
837 ud->driver->disconnect(ud);
839 nodemgr_release_unit_directory(ud);
840 nodemgr_call_policy("remove", ud);
842 list_del(&ud->driver_list);
843 list_del(&ud->node_list);
849 static struct ieee1394_device_id *
850 nodemgr_match_driver(struct hpsb_protocol_driver *driver,
851 struct unit_directory *ud)
853 struct ieee1394_device_id *id;
855 for (id = driver->id_table; id->match_flags != 0; id++) {
856 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
857 id->vendor_id != ud->vendor_id)
860 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
861 id->model_id != ud->model_id)
864 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
865 id->specifier_id != ud->specifier_id)
868 /* software version does a bitwise comparison instead of equality */
869 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
870 !(id->version & ud->version))
879 static struct hpsb_protocol_driver *
880 nodemgr_find_driver(struct unit_directory *ud)
883 struct hpsb_protocol_driver *match, *driver;
884 struct ieee1394_device_id *device_id;
887 list_for_each(l, &driver_list) {
888 driver = list_entry(l, struct hpsb_protocol_driver, list);
889 device_id = nodemgr_match_driver(driver, ud);
891 if (device_id != NULL) {
900 static void nodemgr_bind_drivers (struct node_entry *ne)
902 struct list_head *lh;
903 struct hpsb_protocol_driver *driver;
904 struct unit_directory *ud;
906 list_for_each(lh, &ne->unit_directories) {
907 ud = list_entry(lh, struct unit_directory, node_list);
908 driver = nodemgr_find_driver(ud);
909 if (driver && (!driver->probe || driver->probe(ud) == 0))
910 nodemgr_claim_unit_directory(ud, driver);
911 nodemgr_call_policy("add", ud);
916 int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
918 struct unit_directory *ud;
919 struct list_head *lh, *next;
921 if (down_interruptible(&nodemgr_serialize))
924 list_add_tail(&driver->list, &driver_list);
926 INIT_LIST_HEAD(&driver->unit_directories);
928 list_for_each_safe (lh, next, &unit_directory_list) {
929 ud = list_entry(lh, struct unit_directory, driver_list);
931 if (nodemgr_match_driver(driver, ud) &&
932 (!driver->probe || driver->probe(ud) == 0))
933 nodemgr_claim_unit_directory(ud, driver);
936 up(&nodemgr_serialize);
939 * Right now registration always succeeds, but maybe we should
940 * detect clashes in protocols handled by other drivers.
941 * DRD> No because multiple drivers are needed to handle certain devices.
942 * For example, a DV camera is an IEC 61883 device (dv1394) and AV/C (raw1394).
943 * This will become less an issue with libiec61883 using raw1394.
945 * BenC: But can we handle this with an ALLOW_SHARED flag for a
946 * protocol? When we get an SBP-3 driver, it will be nice if they were
947 * mutually exclusive, since SBP-3 can handle SBP-2 protocol.
949 * Not to mention that we currently do not seem to support multiple
950 * drivers claiming the same unitdirectory. If we implement both of
951 * those, then we'll need to keep probing when a driver claims a
952 * unitdirectory, but is sharable.
958 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
960 struct list_head *lh, *next;
961 struct unit_directory *ud;
963 down(&nodemgr_serialize);
965 list_del(&driver->list);
967 list_for_each_safe (lh, next, &driver->unit_directories) {
968 ud = list_entry(lh, struct unit_directory, driver_list);
970 if (ud->driver && ud->driver->disconnect)
971 ud->driver->disconnect(ud);
973 nodemgr_release_unit_directory(ud);
976 up(&nodemgr_serialize);
979 static void nodemgr_process_config_rom(struct node_entry *ne,
980 quadlet_t busoptions)
982 ne->busopt.irmc = (busoptions >> 31) & 1;
983 ne->busopt.cmc = (busoptions >> 30) & 1;
984 ne->busopt.isc = (busoptions >> 29) & 1;
985 ne->busopt.bmc = (busoptions >> 28) & 1;
986 ne->busopt.pmc = (busoptions >> 27) & 1;
987 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
988 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
989 ne->busopt.generation = (busoptions >> 4) & 0xf;
990 ne->busopt.lnkspd = busoptions & 0x7;
992 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
993 "cyc_clk_acc=%d max_rec=%d gen=%d lspd=%d",
994 busoptions, ne->busopt.irmc, ne->busopt.cmc,
995 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
996 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
997 ne->busopt.generation, ne->busopt.lnkspd);
1000 * When the config rom changes we disconnect all drivers and
1001 * free the cached unit directories and reread the whole
1002 * thing. If this was a new device, the call to
1003 * nodemgr_disconnect_drivers is a no-op and all is well.
1005 nodemgr_free_unit_directories(ne);
1006 nodemgr_process_root_directory(ne);
1007 nodemgr_bind_drivers(ne);
1011 * This function updates nodes that were present on the bus before the
1012 * reset and still are after the reset. The nodeid and the config rom
1013 * may have changed, and the drivers managing this device must be
1014 * informed that this device just went through a bus reset, to allow
1015 * the to take whatever actions required.
1017 static void nodemgr_update_node(struct node_entry *ne, quadlet_t busoptions,
1018 struct host_info *hi, nodeid_t nodeid,
1019 unsigned int generation)
1021 struct list_head *lh;
1022 struct unit_directory *ud;
1024 if (ne->nodeid != nodeid) {
1025 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1026 NODE_BUS_ARGS(ne->host, ne->nodeid),
1027 NODE_BUS_ARGS(ne->host, nodeid));
1028 ne->nodeid = nodeid;
1031 ne->generation = generation;
1033 if (ne->busopt.generation != ((busoptions >> 4) & 0xf))
1034 nodemgr_process_config_rom (ne, busoptions);
1036 list_for_each (lh, &ne->unit_directories) {
1037 ud = list_entry (lh, struct unit_directory, node_list);
1038 if (ud->driver && ud->driver->update != NULL)
1039 ud->driver->update(ud);
1043 static int read_businfo_block(struct hpsb_host *host, nodeid_t nodeid, unsigned int generation,
1044 quadlet_t *buffer, int buffer_length)
1046 octlet_t addr = CSR_REGISTER_BASE + CSR_CONFIG_ROM;
1047 unsigned header_size;
1050 /* IEEE P1212 says that devices should support 64byte block
1051 * reads, aligned on 64byte boundaries. That doesn't seem to
1052 * work though, and we are forced to doing quadlet sized
1055 HPSB_VERBOSE("Initiating ConfigROM request for node " NODE_BUS_FMT,
1056 NODE_BUS_ARGS(host, nodeid));
1059 * Must retry a few times if config rom read returns zero (how long?). Will
1060 * not normally occur, but we should do the right thing. For example, with
1061 * some sbp2 devices, the bridge chipset cannot return valid config rom reads
1062 * immediately after power-on, since they need to detect the type of
1063 * device attached (disk or CD-ROM).
1065 for (i = 0; i < 4; i++) {
1066 if (nodemgr_read_quadlet(host, nodeid, generation,
1067 addr, &buffer[0]) < 0) {
1068 HPSB_ERR("ConfigROM quadlet transaction error for node "
1069 NODE_BUS_FMT, NODE_BUS_ARGS(host, nodeid));
1075 set_current_state(TASK_INTERRUPTIBLE);
1076 if (schedule_timeout (HZ/4))
1080 header_size = buffer[0] >> 24;
1083 if (header_size == 1) {
1084 HPSB_INFO("Node " NODE_BUS_FMT " has a minimal ROM. "
1086 NODE_BUS_ARGS(host, nodeid), buffer[0] & 0x00ffffff);
1090 if (header_size < 4) {
1091 HPSB_INFO("Node " NODE_BUS_FMT " has non-standard ROM "
1092 "format (%d quads), cannot parse",
1093 NODE_BUS_ARGS(host, nodeid), header_size);
1097 for (i = 1; i < buffer_length; i++, addr += 4) {
1098 if (nodemgr_read_quadlet(host, nodeid, generation,
1099 addr, &buffer[i]) < 0) {
1100 HPSB_ERR("ConfigROM quadlet transaction "
1101 "error for node " NODE_BUS_FMT,
1102 NODE_BUS_ARGS(host, nodeid));
1110 static void nodemgr_remove_node(struct node_entry *ne)
1112 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1113 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1115 nodemgr_free_unit_directories(ne);
1116 list_del(&ne->list);
1122 /* This is where we probe the nodes for their information and provided
1124 static void nodemgr_node_probe_one(struct host_info *hi,
1125 nodeid_t nodeid, int generation)
1127 struct hpsb_host *host = hi->host;
1128 struct node_entry *ne;
1129 quadlet_t buffer[5];
1132 /* We need to detect when the ConfigROM's generation has changed,
1133 * so we only update the node's info when it needs to be. */
1135 if (read_businfo_block (host, nodeid, generation,
1136 buffer, sizeof(buffer) >> 2))
1139 if (buffer[1] != IEEE1394_BUSID_MAGIC) {
1140 /* This isn't a 1394 device, but we let it slide. There
1141 * was a report of a device with broken firmware which
1142 * reported '2394' instead of '1394', which is obviously a
1143 * mistake. One would hope that a non-1394 device never
1144 * gets connected to Firewire bus. If someone does, we
1145 * shouldn't be held responsible, so we'll allow it with a
1147 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1148 NODE_BUS_ARGS(host, nodeid), buffer[1]);
1151 guid = ((u64)buffer[3] << 32) | buffer[4];
1152 ne = find_entry_by_guid(guid);
1155 nodemgr_create_node(guid, buffer[2], hi, nodeid, generation);
1157 nodemgr_update_node(ne, buffer[2], hi, nodeid, generation);
1162 static void nodemgr_node_probe_cleanup(struct hpsb_host *host, unsigned int generation)
1164 struct list_head *lh, *next;
1165 struct node_entry *ne;
1167 /* Now check to see if we have any nodes that aren't referenced
1169 list_for_each_safe(lh, next, &node_list) {
1170 ne = list_entry(lh, struct node_entry, list);
1172 /* Only checking this host */
1173 if (ne->host != host)
1176 /* If the generation didn't get updated, then either the
1177 * node was removed, or it failed the above probe. Either
1178 * way, we remove references to it, since they are
1180 if (ne->generation != generation)
1181 nodemgr_remove_node(ne);
1187 static void nodemgr_node_probe(struct host_info *hi, int generation)
1190 struct hpsb_host *host = hi->host;
1191 struct selfid *sid = (struct selfid *)host->topology_map;
1192 nodeid_t nodeid = LOCAL_BUS;
1194 /* Scan each node on the bus */
1195 for (count = host->selfid_count; count; count--, sid++) {
1199 if (!sid->link_active) {
1203 nodemgr_node_probe_one(hi, nodeid++, generation);
1206 /* If we had a bus reset while we were scanning the bus, it is
1207 * possible that we did not probe all nodes. In that case, we
1208 * skip the clean up for now, since we could remove nodes that
1209 * were still on the bus. The bus reset increased hi->reset_sem,
1210 * so there's a bus scan pending which will do the clean up
1212 if (generation == get_hpsb_generation(host))
1213 nodemgr_node_probe_cleanup(host, generation);
1218 /* Because we are a 1394a-2000 compliant IRM, we need to inform all the other
1219 * nodes of the broadcast channel. (Really we're only setting the validity
1220 * bit). Other IRM responsibilities go in here as well. */
1221 static void nodemgr_do_irm_duties(struct hpsb_host *host)
1228 host->csr.broadcast_channel |= 0x40000000; /* set validity bit */
1230 bc = cpu_to_be32(host->csr.broadcast_channel);
1232 hpsb_write(host, LOCAL_BUS | ALL_NODES, get_hpsb_generation(host),
1233 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1234 &bc, sizeof(quadlet_t));
1236 /* If there is no bus manager then we should set the root node's
1237 * force_root bit to promote bus stability per the 1394
1238 * spec. (8.4.2.6) */
1239 if (host->busmgr_id == 0xffff && host->node_count > 1)
1241 u16 root_node = host->node_count - 1;
1242 struct node_entry *ne = find_entry_by_nodeid(host, root_node | LOCAL_BUS);
1244 if (ne && ne->busopt.cmc)
1245 hpsb_send_phy_config(host, root_node, -1);
1247 HPSB_DEBUG("The root node is not cycle master capable; "
1248 "selecting a new root node and resetting...");
1249 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1250 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1255 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1256 * everything we can do, otherwise issue a bus reset and try to become the IRM
1258 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1266 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1267 get_hpsb_generation(host),
1268 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1269 &bc, sizeof(quadlet_t));
1271 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1272 /* The current irm node does not have a valid BROADCAST_CHANNEL
1273 * register and we do, so reset the bus with force_root set */
1274 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1277 /* Oh screw it! Just leave the bus as it is */
1278 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1282 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1283 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1291 static int nodemgr_host_thread(void *__hi)
1293 struct host_info *hi = (struct host_info *)__hi;
1294 struct hpsb_host *host = hi->host;
1295 int reset_cycles = 0;
1297 /* No userlevel access needed */
1300 strcpy(current->comm, hi->daemon_name);
1302 /* Sit and wait for a signal to probe the nodes on the bus. This
1303 * happens when we get a bus reset. */
1304 while (!down_interruptible(&hi->reset_sem) &&
1305 !down_interruptible(&nodemgr_serialize)) {
1306 unsigned int generation = 0;
1309 /* Pause for 1/4 second in 1/16 second intervals,
1310 * to make sure things settle down. */
1311 for (i = 0; i < 4 ; i++) {
1312 set_current_state(TASK_INTERRUPTIBLE);
1313 if (schedule_timeout(HZ/16)) {
1314 up(&nodemgr_serialize);
1318 /* Now get the generation in which the node ID's we collect
1319 * are valid. During the bus scan we will use this generation
1320 * for the read transactions, so that if another reset occurs
1321 * during the scan the transactions will fail instead of
1322 * returning bogus data. */
1323 generation = get_hpsb_generation(host);
1325 /* If we get a reset before we are done waiting, then
1326 * start the the waiting over again */
1327 while (!down_trylock(&hi->reset_sem))
1331 if (!nodemgr_check_irm_capability(host, reset_cycles++)) {
1332 /* Do nothing, we are resetting */
1333 up(&nodemgr_serialize);
1339 nodemgr_node_probe(hi, generation);
1340 nodemgr_do_irm_duties(host);
1342 up(&nodemgr_serialize);
1346 HPSB_VERBOSE("NodeMgr: Exiting thread");
1348 complete_and_exit(&hi->exited, 0);
1351 struct node_entry *hpsb_guid_get_entry(u64 guid)
1353 struct node_entry *ne;
1355 down(&nodemgr_serialize);
1356 ne = find_entry_by_guid(guid);
1357 up(&nodemgr_serialize);
1362 struct node_entry *hpsb_nodeid_get_entry(struct hpsb_host *host, nodeid_t nodeid)
1364 struct node_entry *ne;
1366 down(&nodemgr_serialize);
1367 ne = find_entry_by_nodeid(host, nodeid);
1368 up(&nodemgr_serialize);
1373 /* The following four convenience functions use a struct node_entry
1374 * for addressing a node on the bus. They are intended for use by any
1375 * process context, not just the nodemgr thread, so we need to be a
1376 * little careful when reading out the node ID and generation. The
1377 * thing that can go wrong is that we get the node ID, then a bus
1378 * reset occurs, and then we read the generation. The node ID is
1379 * possibly invalid, but the generation is current, and we end up
1380 * sending a packet to a the wrong node.
1382 * The solution is to make sure we read the generation first, so that
1383 * if a reset occurs in the process, we end up with a stale generation
1384 * and the transactions will fail instead of silently using wrong node
1388 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
1390 pkt->host = ne->host;
1391 pkt->generation = ne->generation;
1393 pkt->node_id = ne->nodeid;
1396 int hpsb_node_read(struct node_entry *ne, u64 addr,
1397 quadlet_t *buffer, size_t length)
1399 unsigned int generation = ne->generation;
1402 return hpsb_read(ne->host, ne->nodeid, generation,
1403 addr, buffer, length);
1406 int hpsb_node_write(struct node_entry *ne, u64 addr,
1407 quadlet_t *buffer, size_t length)
1409 unsigned int generation = ne->generation;
1412 return hpsb_write(ne->host, ne->nodeid, generation,
1413 addr, buffer, length);
1416 int hpsb_node_lock(struct node_entry *ne, u64 addr,
1417 int extcode, quadlet_t *data, quadlet_t arg)
1419 unsigned int generation = ne->generation;
1422 return hpsb_lock(ne->host, ne->nodeid, generation,
1423 addr, extcode, data, arg);
1426 static void nodemgr_add_host(struct hpsb_host *host)
1428 struct host_info *hi;
1430 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1433 HPSB_ERR ("NodeMgr: out of memory in add host");
1438 init_completion(&hi->exited);
1439 sema_init(&hi->reset_sem, 0);
1441 sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
1443 hi->pid = kernel_thread(nodemgr_host_thread, hi,
1444 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1447 HPSB_ERR ("NodeMgr: failed to start %s thread for %s",
1448 hi->daemon_name, host->driver->name);
1449 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1456 static void nodemgr_host_reset(struct hpsb_host *host)
1458 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1461 HPSB_VERBOSE("NodeMgr: Processing host reset for %s", hi->daemon_name);
1464 HPSB_ERR ("NodeMgr: could not process reset of unused host");
1469 static void nodemgr_remove_host(struct hpsb_host *host)
1471 struct list_head *lh, *next;
1472 struct node_entry *ne;
1473 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1477 kill_proc(hi->pid, SIGTERM, 1);
1478 wait_for_completion(&hi->exited);
1481 HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
1482 host->driver->name);
1484 down(&nodemgr_serialize);
1486 /* Even if we fail the host_info part, remove all the node
1488 list_for_each_safe(lh, next, &node_list) {
1489 ne = list_entry(lh, struct node_entry, list);
1491 if (ne->host == host)
1492 nodemgr_remove_node(ne);
1495 up(&nodemgr_serialize);
1500 static struct hpsb_highlevel nodemgr_highlevel = {
1501 .name = "Node manager",
1502 .add_host = nodemgr_add_host,
1503 .host_reset = nodemgr_host_reset,
1504 .remove_host = nodemgr_remove_host,
1507 #define PROC_ENTRY "devices"
1509 void init_ieee1394_nodemgr(void)
1511 #ifdef CONFIG_PROC_FS
1512 if (!create_proc_read_entry(PROC_ENTRY, 0444, ieee1394_procfs_entry, raw1394_read_proc, NULL))
1513 HPSB_ERR("Can't create devices procfs entry");
1515 hpsb_register_highlevel(&nodemgr_highlevel);
1518 void cleanup_ieee1394_nodemgr(void)
1520 hpsb_unregister_highlevel(&nodemgr_highlevel);
1521 #ifdef CONFIG_PROC_FS
1522 remove_proc_entry(PROC_ENTRY, ieee1394_procfs_entry);