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/bitmap.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/moduleparam.h>
18 #include <asm/atomic.h>
21 #include "highlevel.h"
24 #include "ieee1394_core.h"
25 #include "ieee1394_hotplug.h"
26 #include "ieee1394_types.h"
27 #include "ieee1394_transactions.h"
30 static int ignore_drivers;
31 module_param(ignore_drivers, int, S_IRUGO | S_IWUSR);
32 MODULE_PARM_DESC(ignore_drivers, "Disable automatic probing for drivers.");
34 struct nodemgr_csr_info {
35 struct hpsb_host *host;
37 unsigned int generation;
38 unsigned int speed_unverified:1;
42 static char *nodemgr_find_oui_name(int oui)
44 #ifdef CONFIG_IEEE1394_OUI_DB
45 extern struct oui_list_struct {
51 for (i = 0; oui_list[i].name; i++)
52 if (oui_list[i].oui == oui)
53 return oui_list[i].name;
59 * Correct the speed map entry. This is necessary
60 * - for nodes with link speed < phy speed,
61 * - for 1394b nodes with negotiated phy port speed < IEEE1394_SPEED_MAX.
62 * A possible speed is determined by trial and error, using quadlet reads.
64 static int nodemgr_check_speed(struct nodemgr_csr_info *ci, u64 addr,
68 u8 i, *speed, old_speed, good_speed;
71 speed = &(ci->host->speed[NODEID_TO_NODE(ci->nodeid)]);
73 good_speed = IEEE1394_SPEED_MAX + 1;
75 /* Try every speed from S100 to old_speed.
76 * If we did it the other way around, a too low speed could be caught
77 * if the retry succeeded for some other reason, e.g. because the link
78 * just finished its initialization. */
79 for (i = IEEE1394_SPEED_100; i <= old_speed; i++) {
81 ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
82 &q, sizeof(quadlet_t));
88 if (good_speed <= IEEE1394_SPEED_MAX) {
89 HPSB_DEBUG("Speed probe of node " NODE_BUS_FMT " yields %s",
90 NODE_BUS_ARGS(ci->host, ci->nodeid),
91 hpsb_speedto_str[good_speed]);
93 ci->speed_unverified = 0;
100 static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
101 void *buffer, void *__ci)
103 struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
107 ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
110 ci->speed_unverified = 0;
113 /* Give up after 3rd failure. */
117 /* The ieee1394_core guessed the node's speed capability from
118 * the self ID. Check whether a lower speed works. */
119 if (ci->speed_unverified && length == sizeof(quadlet_t)) {
120 ret = nodemgr_check_speed(ci, addr, buffer);
124 if (msleep_interruptible(334))
130 static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
132 return (CSR1212_BE32_TO_CPU(bus_info_data[2]) >> 8) & 0x3;
135 static struct csr1212_bus_ops nodemgr_csr_ops = {
136 .bus_read = nodemgr_bus_read,
137 .get_max_rom = nodemgr_get_max_rom
142 * Basically what we do here is start off retrieving the bus_info block.
143 * From there will fill in some info about the node, verify it is of IEEE
144 * 1394 type, and that the crc checks out ok. After that we start off with
145 * the root directory, and subdirectories. To do this, we retrieve the
146 * quadlet header for a directory, find out the length, and retrieve the
147 * complete directory entry (be it a leaf or a directory). We then process
148 * it and add the info to our structure for that particular node.
150 * We verify CRC's along the way for each directory/block/leaf. The entire
151 * node structure is generic, and simply stores the information in a way
152 * that's easy to parse by the protocol interface.
156 * The nodemgr relies heavily on the Driver Model for device callbacks and
157 * driver/device mappings. The old nodemgr used to handle all this itself,
158 * but now we are much simpler because of the LDM.
161 static DECLARE_MUTEX(nodemgr_serialize);
164 struct hpsb_host *host;
165 struct list_head list;
166 struct completion exited;
167 struct semaphore reset_sem;
169 char daemon_name[15];
173 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
174 static int nodemgr_uevent(struct class_device *cdev, char **envp, int num_envp,
175 char *buffer, int buffer_size);
176 static void nodemgr_resume_ne(struct node_entry *ne);
177 static void nodemgr_remove_ne(struct node_entry *ne);
178 static struct node_entry *find_entry_by_guid(u64 guid);
180 struct bus_type ieee1394_bus_type = {
182 .match = nodemgr_bus_match,
185 static void host_cls_release(struct class_device *class_dev)
187 put_device(&container_of((class_dev), struct hpsb_host, class_dev)->device);
190 struct class hpsb_host_class = {
191 .name = "ieee1394_host",
192 .release = host_cls_release,
195 static void ne_cls_release(struct class_device *class_dev)
197 put_device(&container_of((class_dev), struct node_entry, class_dev)->device);
200 static struct class nodemgr_ne_class = {
201 .name = "ieee1394_node",
202 .release = ne_cls_release,
205 static void ud_cls_release(struct class_device *class_dev)
207 put_device(&container_of((class_dev), struct unit_directory, class_dev)->device);
210 /* The name here is only so that unit directory hotplug works with old
211 * style hotplug, which only ever did unit directories anyway. */
212 static struct class nodemgr_ud_class = {
214 .release = ud_cls_release,
215 .uevent = nodemgr_uevent,
218 static struct hpsb_highlevel nodemgr_highlevel;
221 static void nodemgr_release_ud(struct device *dev)
223 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
225 if (ud->vendor_name_kv)
226 csr1212_release_keyval(ud->vendor_name_kv);
227 if (ud->model_name_kv)
228 csr1212_release_keyval(ud->model_name_kv);
233 static void nodemgr_release_ne(struct device *dev)
235 struct node_entry *ne = container_of(dev, struct node_entry, device);
237 if (ne->vendor_name_kv)
238 csr1212_release_keyval(ne->vendor_name_kv);
244 static void nodemgr_release_host(struct device *dev)
246 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
248 csr1212_destroy_csr(host->csr.rom);
253 static int nodemgr_ud_platform_data;
255 static struct device nodemgr_dev_template_ud = {
256 .bus = &ieee1394_bus_type,
257 .release = nodemgr_release_ud,
258 .platform_data = &nodemgr_ud_platform_data,
261 static struct device nodemgr_dev_template_ne = {
262 .bus = &ieee1394_bus_type,
263 .release = nodemgr_release_ne,
266 struct device nodemgr_dev_template_host = {
267 .bus = &ieee1394_bus_type,
268 .release = nodemgr_release_host,
272 #define fw_attr(class, class_type, field, type, format_string) \
273 static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
276 class = container_of(dev, class_type, device); \
277 return sprintf(buf, format_string, (type)class->field); \
279 static struct device_attribute dev_attr_##class##_##field = { \
280 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
281 .show = fw_show_##class##_##field, \
284 #define fw_attr_td(class, class_type, td_kv) \
285 static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
288 class_type *class = container_of(dev, class_type, device); \
289 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
291 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
293 while ((buf + len - 1) == '\0') \
299 static struct device_attribute dev_attr_##class##_##td_kv = { \
300 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
301 .show = fw_show_##class##_##td_kv, \
305 #define fw_drv_attr(field, type, format_string) \
306 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
308 struct hpsb_protocol_driver *driver; \
309 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
310 return sprintf(buf, format_string, (type)driver->field);\
312 static struct driver_attribute driver_attr_drv_##field = { \
313 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
314 .show = fw_drv_show_##field, \
318 static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
320 struct node_entry *ne = container_of(dev, struct node_entry, device);
322 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
323 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
325 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
326 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
329 ne->busopt.cyc_clk_acc);
331 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
334 /* tlabels_free, tlabels_allocations, tlabels_mask are read non-atomically
335 * here, therefore displayed values may be occasionally wrong. */
336 static ssize_t fw_show_ne_tlabels_free(struct device *dev, struct device_attribute *attr, char *buf)
338 struct node_entry *ne = container_of(dev, struct node_entry, device);
339 return sprintf(buf, "%d\n", 64 - bitmap_weight(ne->tpool->pool, 64));
341 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
344 static ssize_t fw_show_ne_tlabels_allocations(struct device *dev, struct device_attribute *attr, char *buf)
346 struct node_entry *ne = container_of(dev, struct node_entry, device);
347 return sprintf(buf, "%u\n", ne->tpool->allocations);
349 static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
352 static ssize_t fw_show_ne_tlabels_mask(struct device *dev, struct device_attribute *attr, char *buf)
354 struct node_entry *ne = container_of(dev, struct node_entry, device);
355 #if (BITS_PER_LONG <= 32)
356 return sprintf(buf, "0x%08lx%08lx\n", ne->tpool->pool[0], ne->tpool->pool[1]);
358 return sprintf(buf, "0x%016lx\n", ne->tpool->pool[0]);
361 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
364 static ssize_t fw_set_ignore_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
366 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
367 int state = simple_strtoul(buf, NULL, 10);
370 down_write(&dev->bus->subsys.rwsem);
371 device_release_driver(dev);
372 ud->ignore_driver = 1;
373 up_write(&dev->bus->subsys.rwsem);
375 ud->ignore_driver = 0;
379 static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
381 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
383 return sprintf(buf, "%d\n", ud->ignore_driver);
385 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
388 static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
390 struct node_entry *ne;
391 u64 guid = (u64)simple_strtoull(buf, NULL, 16);
393 ne = find_entry_by_guid(guid);
395 if (ne == NULL || !ne->in_limbo)
398 nodemgr_remove_ne(ne);
402 static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
404 return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
406 static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
409 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf, size_t count)
411 if (simple_strtoul(buf, NULL, 10) == 1)
412 bus_rescan_devices(&ieee1394_bus_type);
415 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
417 return sprintf(buf, "You can force a rescan of the bus for "
418 "drivers by writing a 1 to this file\n");
420 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
423 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
425 int state = simple_strtoul(buf, NULL, 10);
434 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
436 return sprintf(buf, "%d\n", ignore_drivers);
438 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
441 struct bus_attribute *const fw_bus_attrs[] = {
442 &bus_attr_destroy_node,
444 &bus_attr_ignore_drivers,
449 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
450 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
452 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
453 fw_attr_td(ne, struct node_entry, vendor_name_kv)
454 fw_attr(ne, struct node_entry, vendor_oui, const char *, "%s\n")
456 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
457 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
458 fw_attr(ne, struct node_entry, guid_vendor_oui, const char *, "%s\n")
459 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
461 static struct device_attribute *const fw_ne_attrs[] = {
463 &dev_attr_ne_guid_vendor_id,
464 &dev_attr_ne_capabilities,
465 &dev_attr_ne_vendor_id,
467 &dev_attr_bus_options,
468 &dev_attr_tlabels_free,
469 &dev_attr_tlabels_allocations,
470 &dev_attr_tlabels_mask,
475 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
476 fw_attr(ud, struct unit_directory, length, int, "%d\n")
477 /* These are all dependent on the value being provided */
478 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
479 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
480 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
481 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
482 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
483 fw_attr(ud, struct unit_directory, vendor_oui, const char *, "%s\n")
484 fw_attr_td(ud, struct unit_directory, model_name_kv)
486 static struct device_attribute *const fw_ud_attrs[] = {
487 &dev_attr_ud_address,
489 &dev_attr_ignore_driver,
493 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
494 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
495 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
496 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
497 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
498 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
499 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
500 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
502 static struct device_attribute *const fw_host_attrs[] = {
503 &dev_attr_host_node_count,
504 &dev_attr_host_selfid_count,
505 &dev_attr_host_nodes_active,
506 &dev_attr_host_in_bus_reset,
507 &dev_attr_host_is_root,
508 &dev_attr_host_is_cycmst,
509 &dev_attr_host_is_irm,
510 &dev_attr_host_is_busmgr,
514 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
516 struct hpsb_protocol_driver *driver;
517 struct ieee1394_device_id *id;
521 driver = container_of(drv, struct hpsb_protocol_driver, driver);
523 for (id = driver->id_table; id->match_flags != 0; id++) {
526 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
527 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
528 scratch = buf + length;
532 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
533 length += sprintf(scratch, "%smodel_id=0x%06x",
534 need_coma++ ? "," : "",
536 scratch = buf + length;
539 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
540 length += sprintf(scratch, "%sspecifier_id=0x%06x",
541 need_coma++ ? "," : "",
543 scratch = buf + length;
546 if (id->match_flags & IEEE1394_MATCH_VERSION) {
547 length += sprintf(scratch, "%sversion=0x%06x",
548 need_coma++ ? "," : "",
550 scratch = buf + length;
561 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
564 fw_drv_attr(name, const char *, "%s\n")
566 static struct driver_attribute *const fw_drv_attrs[] = {
567 &driver_attr_drv_name,
568 &driver_attr_device_ids,
572 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
574 struct device_driver *drv = &driver->driver;
577 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
578 driver_create_file(drv, fw_drv_attrs[i]);
582 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
584 struct device_driver *drv = &driver->driver;
587 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
588 driver_remove_file(drv, fw_drv_attrs[i]);
592 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
594 struct device *dev = &ne->device;
597 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
598 device_create_file(dev, fw_ne_attrs[i]);
602 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
604 struct device *dev = &host->device;
607 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
608 device_create_file(dev, fw_host_attrs[i]);
612 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid);
614 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
616 struct device *dev = &host->device;
617 struct node_entry *ne;
619 sysfs_remove_link(&dev->kobj, "irm_id");
620 sysfs_remove_link(&dev->kobj, "busmgr_id");
621 sysfs_remove_link(&dev->kobj, "host_id");
623 if ((ne = find_entry_by_nodeid(host, host->irm_id)))
624 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id");
625 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)))
626 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id");
627 if ((ne = find_entry_by_nodeid(host, host->node_id)))
628 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id");
631 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
633 struct device *dev = &ud->device;
636 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
637 device_create_file(dev, fw_ud_attrs[i]);
639 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
640 device_create_file(dev, &dev_attr_ud_specifier_id);
642 if (ud->flags & UNIT_DIRECTORY_VERSION)
643 device_create_file(dev, &dev_attr_ud_version);
645 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
646 device_create_file(dev, &dev_attr_ud_vendor_id);
647 if (ud->vendor_name_kv)
648 device_create_file(dev, &dev_attr_ud_vendor_name_kv);
651 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
652 device_create_file(dev, &dev_attr_ud_model_id);
653 if (ud->model_name_kv)
654 device_create_file(dev, &dev_attr_ud_model_name_kv);
659 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
661 struct hpsb_protocol_driver *driver;
662 struct unit_directory *ud;
663 struct ieee1394_device_id *id;
665 /* We only match unit directories */
666 if (dev->platform_data != &nodemgr_ud_platform_data)
669 ud = container_of(dev, struct unit_directory, device);
670 driver = container_of(drv, struct hpsb_protocol_driver, driver);
672 if (ud->ne->in_limbo || ud->ignore_driver)
675 for (id = driver->id_table; id->match_flags != 0; id++) {
676 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
677 id->vendor_id != ud->vendor_id)
680 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
681 id->model_id != ud->model_id)
684 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
685 id->specifier_id != ud->specifier_id)
688 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
689 id->version != ud->version)
699 static void nodemgr_remove_uds(struct node_entry *ne)
701 struct class_device *cdev, *next;
702 struct unit_directory *ud;
704 list_for_each_entry_safe(cdev, next, &nodemgr_ud_class.children, node) {
705 ud = container_of(cdev, struct unit_directory, class_dev);
710 class_device_unregister(&ud->class_dev);
711 device_unregister(&ud->device);
716 static void nodemgr_remove_ne(struct node_entry *ne)
718 struct device *dev = &ne->device;
720 dev = get_device(&ne->device);
724 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
725 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
727 nodemgr_remove_uds(ne);
729 class_device_unregister(&ne->class_dev);
730 device_unregister(dev);
735 static int __nodemgr_remove_host_dev(struct device *dev, void *data)
737 nodemgr_remove_ne(container_of(dev, struct node_entry, device));
741 static void nodemgr_remove_host_dev(struct device *dev)
743 device_for_each_child(dev, NULL, __nodemgr_remove_host_dev);
744 sysfs_remove_link(&dev->kobj, "irm_id");
745 sysfs_remove_link(&dev->kobj, "busmgr_id");
746 sysfs_remove_link(&dev->kobj, "host_id");
750 static void nodemgr_update_bus_options(struct node_entry *ne)
752 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
753 static const u16 mr[] = { 4, 64, 1024, 0};
755 quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
757 ne->busopt.irmc = (busoptions >> 31) & 1;
758 ne->busopt.cmc = (busoptions >> 30) & 1;
759 ne->busopt.isc = (busoptions >> 29) & 1;
760 ne->busopt.bmc = (busoptions >> 28) & 1;
761 ne->busopt.pmc = (busoptions >> 27) & 1;
762 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
763 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
764 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
765 ne->busopt.generation = (busoptions >> 4) & 0xf;
766 ne->busopt.lnkspd = busoptions & 0x7;
768 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
769 "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
770 busoptions, ne->busopt.irmc, ne->busopt.cmc,
771 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
772 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
773 mr[ne->busopt.max_rom],
774 ne->busopt.generation, ne->busopt.lnkspd);
778 static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
779 struct host_info *hi, nodeid_t nodeid,
780 unsigned int generation)
782 struct hpsb_host *host = hi->host;
783 struct node_entry *ne;
785 ne = kzalloc(sizeof(*ne), GFP_KERNEL);
789 ne->tpool = &host->tpool[nodeid & NODE_MASK];
793 ne->generation = generation;
797 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
798 ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
801 memcpy(&ne->device, &nodemgr_dev_template_ne,
803 ne->device.parent = &host->device;
804 snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
805 (unsigned long long)(ne->guid));
807 ne->class_dev.dev = &ne->device;
808 ne->class_dev.class = &nodemgr_ne_class;
809 snprintf(ne->class_dev.class_id, BUS_ID_SIZE, "%016Lx",
810 (unsigned long long)(ne->guid));
812 device_register(&ne->device);
813 class_device_register(&ne->class_dev);
814 get_device(&ne->device);
816 if (ne->guid_vendor_oui)
817 device_create_file(&ne->device, &dev_attr_ne_guid_vendor_oui);
818 nodemgr_create_ne_dev_files(ne);
820 nodemgr_update_bus_options(ne);
822 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
823 (host->node_id == nodeid) ? "Host" : "Node",
824 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
830 static struct node_entry *find_entry_by_guid(u64 guid)
832 struct class *class = &nodemgr_ne_class;
833 struct class_device *cdev;
834 struct node_entry *ne, *ret_ne = NULL;
836 down_read(&class->subsys.rwsem);
837 list_for_each_entry(cdev, &class->children, node) {
838 ne = container_of(cdev, struct node_entry, class_dev);
840 if (ne->guid == guid) {
845 up_read(&class->subsys.rwsem);
851 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
853 struct class *class = &nodemgr_ne_class;
854 struct class_device *cdev;
855 struct node_entry *ne, *ret_ne = NULL;
857 down_read(&class->subsys.rwsem);
858 list_for_each_entry(cdev, &class->children, node) {
859 ne = container_of(cdev, struct node_entry, class_dev);
861 if (ne->host == host && ne->nodeid == nodeid) {
866 up_read(&class->subsys.rwsem);
872 static void nodemgr_register_device(struct node_entry *ne,
873 struct unit_directory *ud, struct device *parent)
875 memcpy(&ud->device, &nodemgr_dev_template_ud,
878 ud->device.parent = parent;
880 snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
881 ne->device.bus_id, ud->id);
883 ud->class_dev.dev = &ud->device;
884 ud->class_dev.class = &nodemgr_ud_class;
885 snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
886 ne->device.bus_id, ud->id);
888 device_register(&ud->device);
889 class_device_register(&ud->class_dev);
890 get_device(&ud->device);
893 device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
894 nodemgr_create_ud_dev_files(ud);
898 /* This implementation currently only scans the config rom and its
899 * immediate unit directories looking for software_id and
900 * software_version entries, in order to get driver autoloading working. */
901 static struct unit_directory *nodemgr_process_unit_directory
902 (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
903 unsigned int *id, struct unit_directory *parent)
905 struct unit_directory *ud;
906 struct unit_directory *ud_child = NULL;
907 struct csr1212_dentry *dentry;
908 struct csr1212_keyval *kv;
911 ud = kzalloc(sizeof(*ud), GFP_KERNEL);
913 goto unit_directory_error;
916 ud->ignore_driver = ignore_drivers;
917 ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
921 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
922 switch (kv->key.id) {
923 case CSR1212_KV_ID_VENDOR:
924 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
925 ud->vendor_id = kv->value.immediate;
926 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
929 ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
933 case CSR1212_KV_ID_MODEL:
934 ud->model_id = kv->value.immediate;
935 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
938 case CSR1212_KV_ID_SPECIFIER_ID:
939 ud->specifier_id = kv->value.immediate;
940 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
943 case CSR1212_KV_ID_VERSION:
944 ud->version = kv->value.immediate;
945 ud->flags |= UNIT_DIRECTORY_VERSION;
948 case CSR1212_KV_ID_DESCRIPTOR:
949 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
950 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
951 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
952 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
953 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
954 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
955 switch (last_key_id) {
956 case CSR1212_KV_ID_VENDOR:
957 ud->vendor_name_kv = kv;
958 csr1212_keep_keyval(kv);
961 case CSR1212_KV_ID_MODEL:
962 ud->model_name_kv = kv;
963 csr1212_keep_keyval(kv);
967 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
970 case CSR1212_KV_ID_DEPENDENT_INFO:
971 /* Logical Unit Number */
972 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
973 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
974 ud_child = kmalloc(sizeof(*ud_child), GFP_KERNEL);
976 goto unit_directory_error;
977 memcpy(ud_child, ud, sizeof(*ud_child));
978 nodemgr_register_device(ne, ud_child, &ne->device);
983 ud->lun = kv->value.immediate;
984 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
986 /* Logical Unit Directory */
987 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
988 /* This should really be done in SBP2 as this is
989 * doing SBP2 specific parsing.
992 /* first register the parent unit */
993 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
994 if (ud->device.bus != &ieee1394_bus_type)
995 nodemgr_register_device(ne, ud, &ne->device);
997 /* process the child unit */
998 ud_child = nodemgr_process_unit_directory(hi, ne, kv, id, ud);
1000 if (ud_child == NULL)
1003 /* inherit unspecified values, the driver core picks it up */
1004 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
1005 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
1007 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
1008 ud_child->model_id = ud->model_id;
1010 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
1011 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
1013 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1014 ud_child->specifier_id = ud->specifier_id;
1016 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
1017 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
1019 ud_child->flags |= UNIT_DIRECTORY_VERSION;
1020 ud_child->version = ud->version;
1023 /* register the child unit */
1024 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
1025 nodemgr_register_device(ne, ud_child, &ud->device);
1033 last_key_id = kv->key.id;
1036 /* do not process child units here and only if not already registered */
1037 if (!parent && ud->device.bus != &ieee1394_bus_type)
1038 nodemgr_register_device(ne, ud, &ne->device);
1042 unit_directory_error:
1048 static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
1050 unsigned int ud_id = 0;
1051 struct csr1212_dentry *dentry;
1052 struct csr1212_keyval *kv;
1055 ne->needs_probe = 0;
1057 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1058 switch (kv->key.id) {
1059 case CSR1212_KV_ID_VENDOR:
1060 ne->vendor_id = kv->value.immediate;
1063 ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
1066 case CSR1212_KV_ID_NODE_CAPABILITIES:
1067 ne->capabilities = kv->value.immediate;
1070 case CSR1212_KV_ID_UNIT:
1071 nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
1074 case CSR1212_KV_ID_DESCRIPTOR:
1075 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1076 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1077 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1078 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1079 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1080 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1081 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1082 ne->vendor_name_kv = kv;
1083 csr1212_keep_keyval(kv);
1088 last_key_id = kv->key.id;
1092 device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
1093 if (ne->vendor_name_kv)
1094 device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
1097 #ifdef CONFIG_HOTPLUG
1099 static int nodemgr_uevent(struct class_device *cdev, char **envp, int num_envp,
1100 char *buffer, int buffer_size)
1102 struct unit_directory *ud;
1105 /* ieee1394:venNmoNspNverN */
1106 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
1111 ud = container_of(cdev, struct unit_directory, class_dev);
1113 if (ud->ne->in_limbo || ud->ignore_driver)
1116 #define PUT_ENVP(fmt,val) \
1119 envp[i++] = buffer; \
1120 printed = snprintf(buffer, buffer_size - length, \
1122 if ((buffer_size - (length+printed) <= 0) || (i >= num_envp)) \
1124 length += printed+1; \
1125 buffer += printed+1; \
1128 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1129 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1130 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1131 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1132 PUT_ENVP("VERSION=%06x", ud->version);
1133 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1138 PUT_ENVP("MODALIAS=%s", buf);
1149 static int nodemgr_uevent(struct class_device *cdev, char **envp, int num_envp,
1150 char *buffer, int buffer_size)
1155 #endif /* CONFIG_HOTPLUG */
1158 int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
1162 /* This will cause a probe for devices */
1163 ret = driver_register(&driver->driver);
1165 nodemgr_create_drv_files(driver);
1170 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1172 nodemgr_remove_drv_files(driver);
1173 /* This will subsequently disconnect all devices that our driver
1174 * is attached to. */
1175 driver_unregister(&driver->driver);
1180 * This function updates nodes that were present on the bus before the
1181 * reset and still are after the reset. The nodeid and the config rom
1182 * may have changed, and the drivers managing this device must be
1183 * informed that this device just went through a bus reset, to allow
1184 * the to take whatever actions required.
1186 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1187 struct host_info *hi, nodeid_t nodeid,
1188 unsigned int generation)
1190 if (ne->nodeid != nodeid) {
1191 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1192 NODE_BUS_ARGS(ne->host, ne->nodeid),
1193 NODE_BUS_ARGS(ne->host, nodeid));
1194 ne->nodeid = nodeid;
1197 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1198 kfree(ne->csr->private);
1199 csr1212_destroy_csr(ne->csr);
1202 /* If the node's configrom generation has changed, we
1203 * unregister all the unit directories. */
1204 nodemgr_remove_uds(ne);
1206 nodemgr_update_bus_options(ne);
1208 /* Mark the node as new, so it gets re-probed */
1209 ne->needs_probe = 1;
1211 /* old cache is valid, so update its generation */
1212 struct nodemgr_csr_info *ci = ne->csr->private;
1213 ci->generation = generation;
1214 /* free the partially filled now unneeded new cache */
1215 kfree(csr->private);
1216 csr1212_destroy_csr(csr);
1220 nodemgr_resume_ne(ne);
1222 /* Mark the node current */
1223 ne->generation = generation;
1228 static void nodemgr_node_scan_one(struct host_info *hi,
1229 nodeid_t nodeid, int generation)
1231 struct hpsb_host *host = hi->host;
1232 struct node_entry *ne;
1234 struct csr1212_csr *csr;
1235 struct nodemgr_csr_info *ci;
1238 ci = kmalloc(sizeof(*ci), GFP_KERNEL);
1243 ci->nodeid = nodeid;
1244 ci->generation = generation;
1246 /* Prepare for speed probe which occurs when reading the ROM */
1247 speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
1248 if (*speed > host->csr.lnk_spd)
1249 *speed = host->csr.lnk_spd;
1250 ci->speed_unverified = *speed > IEEE1394_SPEED_100;
1252 /* We need to detect when the ConfigROM's generation has changed,
1253 * so we only update the node's info when it needs to be. */
1255 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1256 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1257 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1258 NODE_BUS_ARGS(host, nodeid));
1260 csr1212_destroy_csr(csr);
1265 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1266 /* This isn't a 1394 device, but we let it slide. There
1267 * was a report of a device with broken firmware which
1268 * reported '2394' instead of '1394', which is obviously a
1269 * mistake. One would hope that a non-1394 device never
1270 * gets connected to Firewire bus. If someone does, we
1271 * shouldn't be held responsible, so we'll allow it with a
1273 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1274 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1277 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1278 ne = find_entry_by_guid(guid);
1280 if (ne && ne->host != host && ne->in_limbo) {
1281 /* Must have moved this device from one host to another */
1282 nodemgr_remove_ne(ne);
1287 nodemgr_create_node(guid, csr, hi, nodeid, generation);
1289 nodemgr_update_node(ne, csr, hi, nodeid, generation);
1293 static void nodemgr_node_scan(struct host_info *hi, int generation)
1296 struct hpsb_host *host = hi->host;
1297 struct selfid *sid = (struct selfid *)host->topology_map;
1298 nodeid_t nodeid = LOCAL_BUS;
1300 /* Scan each node on the bus */
1301 for (count = host->selfid_count; count; count--, sid++) {
1305 if (!sid->link_active) {
1309 nodemgr_node_scan_one(hi, nodeid++, generation);
1314 static void nodemgr_suspend_ne(struct node_entry *ne)
1316 struct class_device *cdev;
1317 struct unit_directory *ud;
1319 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1320 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1323 device_create_file(&ne->device, &dev_attr_ne_in_limbo);
1325 down_write(&ne->device.bus->subsys.rwsem);
1326 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1327 ud = container_of(cdev, struct unit_directory, class_dev);
1332 if (ud->device.driver &&
1333 (!ud->device.driver->suspend ||
1334 ud->device.driver->suspend(&ud->device, PMSG_SUSPEND)))
1335 device_release_driver(&ud->device);
1337 up_write(&ne->device.bus->subsys.rwsem);
1341 static void nodemgr_resume_ne(struct node_entry *ne)
1343 struct class_device *cdev;
1344 struct unit_directory *ud;
1347 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1349 down_read(&ne->device.bus->subsys.rwsem);
1350 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1351 ud = container_of(cdev, struct unit_directory, class_dev);
1356 if (ud->device.driver && ud->device.driver->resume)
1357 ud->device.driver->resume(&ud->device);
1359 up_read(&ne->device.bus->subsys.rwsem);
1361 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1362 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1366 static void nodemgr_update_pdrv(struct node_entry *ne)
1368 struct unit_directory *ud;
1369 struct hpsb_protocol_driver *pdrv;
1370 struct class *class = &nodemgr_ud_class;
1371 struct class_device *cdev;
1373 down_read(&class->subsys.rwsem);
1374 list_for_each_entry(cdev, &class->children, node) {
1375 ud = container_of(cdev, struct unit_directory, class_dev);
1376 if (ud->ne != ne || !ud->device.driver)
1379 pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
1381 if (pdrv->update && pdrv->update(ud)) {
1382 down_write(&ud->device.bus->subsys.rwsem);
1383 device_release_driver(&ud->device);
1384 up_write(&ud->device.bus->subsys.rwsem);
1387 up_read(&class->subsys.rwsem);
1391 /* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1392 * seems like an optional service but in the end it is practically mandatory
1393 * as a consequence of these clauses.
1395 * Note that we cannot do a broadcast write to all nodes at once because some
1396 * pre-1394a devices would hang. */
1397 static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1399 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1400 quadlet_t bc_remote, bc_local;
1403 if (!ne->host->is_irm || ne->generation != generation ||
1404 ne->nodeid == ne->host->node_id)
1407 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1409 /* Check if the register is implemented and 1394a compliant. */
1410 ret = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1412 if (!ret && bc_remote & cpu_to_be32(0x80000000) &&
1413 bc_remote != bc_local)
1414 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1418 static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
1422 if (ne->host != hi->host || ne->in_limbo)
1425 dev = get_device(&ne->device);
1429 nodemgr_irm_write_bc(ne, generation);
1431 /* If "needs_probe", then this is either a new or changed node we
1432 * rescan totally. If the generation matches for an existing node
1433 * (one that existed prior to the bus reset) we send update calls
1434 * down to the drivers. Otherwise, this is a dead node and we
1436 if (ne->needs_probe)
1437 nodemgr_process_root_directory(hi, ne);
1438 else if (ne->generation == generation)
1439 nodemgr_update_pdrv(ne);
1441 nodemgr_suspend_ne(ne);
1447 static void nodemgr_node_probe(struct host_info *hi, int generation)
1449 struct hpsb_host *host = hi->host;
1450 struct class *class = &nodemgr_ne_class;
1451 struct class_device *cdev;
1452 struct node_entry *ne;
1454 /* Do some processing of the nodes we've probed. This pulls them
1455 * into the sysfs layer if needed, and can result in processing of
1456 * unit-directories, or just updating the node and it's
1459 * Run updates before probes. Usually, updates are time-critical
1460 * while probes are time-consuming. (Well, those probes need some
1461 * improvement...) */
1463 down_read(&class->subsys.rwsem);
1464 list_for_each_entry(cdev, &class->children, node) {
1465 ne = container_of(cdev, struct node_entry, class_dev);
1466 if (!ne->needs_probe)
1467 nodemgr_probe_ne(hi, ne, generation);
1469 list_for_each_entry(cdev, &class->children, node) {
1470 ne = container_of(cdev, struct node_entry, class_dev);
1471 if (ne->needs_probe)
1472 nodemgr_probe_ne(hi, ne, generation);
1474 up_read(&class->subsys.rwsem);
1477 /* If we had a bus reset while we were scanning the bus, it is
1478 * possible that we did not probe all nodes. In that case, we
1479 * skip the clean up for now, since we could remove nodes that
1480 * were still on the bus. The bus reset increased hi->reset_sem,
1481 * so there's a bus scan pending which will do the clean up
1484 * Now let's tell the bus to rescan our devices. This may seem
1485 * like overhead, but the driver-model core will only scan a
1486 * device for a driver when either the device is added, or when a
1487 * new driver is added. A bus reset is a good reason to rescan
1488 * devices that were there before. For example, an sbp2 device
1489 * may become available for login, if the host that held it was
1492 if (generation == get_hpsb_generation(host))
1493 bus_rescan_devices(&ieee1394_bus_type);
1498 static int nodemgr_send_resume_packet(struct hpsb_host *host)
1500 struct hpsb_packet *packet;
1503 packet = hpsb_make_phypacket(host,
1504 EXTPHYPACKET_TYPE_RESUME |
1505 NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
1507 packet->no_waiter = 1;
1508 packet->generation = get_hpsb_generation(host);
1509 ret = hpsb_send_packet(packet);
1512 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1517 /* Perform a few high-level IRM responsibilities. */
1518 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1522 /* if irm_id == -1 then there is no IRM on this bus */
1523 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1526 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1527 host->csr.broadcast_channel |= 0x40000000;
1529 /* If there is no bus manager then we should set the root node's
1530 * force_root bit to promote bus stability per the 1394
1531 * spec. (8.4.2.6) */
1532 if (host->busmgr_id == 0xffff && host->node_count > 1)
1534 u16 root_node = host->node_count - 1;
1536 /* get cycle master capability flag from root node */
1537 if (host->is_cycmst ||
1538 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1539 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1540 &bc, sizeof(quadlet_t)) &&
1541 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
1542 hpsb_send_phy_config(host, root_node, -1);
1544 HPSB_DEBUG("The root node is not cycle master capable; "
1545 "selecting a new root node and resetting...");
1548 /* Oh screw it! Just leave the bus as it is */
1549 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1553 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1554 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1560 /* Some devices suspend their ports while being connected to an inactive
1561 * host adapter, i.e. if connected before the low-level driver is
1562 * loaded. They become visible either when physically unplugged and
1563 * replugged, or when receiving a resume packet. Send one once. */
1564 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1565 host->resume_packet_sent = 1;
1570 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1571 * everything we can do, otherwise issue a bus reset and try to become the IRM
1573 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1578 if (hpsb_disable_irm || host->is_irm)
1581 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1582 get_hpsb_generation(host),
1583 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1584 &bc, sizeof(quadlet_t));
1586 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1587 /* The current irm node does not have a valid BROADCAST_CHANNEL
1588 * register and we do, so reset the bus with force_root set */
1589 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1592 /* Oh screw it! Just leave the bus as it is */
1593 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1597 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1598 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1606 static int nodemgr_host_thread(void *__hi)
1608 struct host_info *hi = (struct host_info *)__hi;
1609 struct hpsb_host *host = hi->host;
1610 int reset_cycles = 0;
1612 /* No userlevel access needed */
1613 daemonize(hi->daemon_name);
1615 /* Setup our device-model entries */
1616 nodemgr_create_host_dev_files(host);
1618 /* Sit and wait for a signal to probe the nodes on the bus. This
1619 * happens when we get a bus reset. */
1621 unsigned int generation = 0;
1624 if (down_interruptible(&hi->reset_sem) ||
1625 down_interruptible(&nodemgr_serialize)) {
1626 if (try_to_freeze())
1628 printk("NodeMgr: received unexpected signal?!\n" );
1633 up(&nodemgr_serialize);
1637 /* Pause for 1/4 second in 1/16 second intervals,
1638 * to make sure things settle down. */
1639 for (i = 0; i < 4 ; i++) {
1640 set_current_state(TASK_INTERRUPTIBLE);
1641 if (msleep_interruptible(63)) {
1642 up(&nodemgr_serialize);
1646 /* Now get the generation in which the node ID's we collect
1647 * are valid. During the bus scan we will use this generation
1648 * for the read transactions, so that if another reset occurs
1649 * during the scan the transactions will fail instead of
1650 * returning bogus data. */
1651 generation = get_hpsb_generation(host);
1653 /* If we get a reset before we are done waiting, then
1654 * start the the waiting over again */
1655 while (!down_trylock(&hi->reset_sem))
1658 /* Check the kill_me again */
1660 up(&nodemgr_serialize);
1665 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1666 !nodemgr_do_irm_duties(host, reset_cycles)) {
1668 up(&nodemgr_serialize);
1673 /* Scan our nodes to get the bus options and create node
1674 * entries. This does not do the sysfs stuff, since that
1675 * would trigger uevents and such, which is a bad idea at
1677 nodemgr_node_scan(hi, generation);
1679 /* This actually does the full probe, with sysfs
1681 nodemgr_node_probe(hi, generation);
1683 /* Update some of our sysfs symlinks */
1684 nodemgr_update_host_dev_links(host);
1686 up(&nodemgr_serialize);
1690 HPSB_VERBOSE("NodeMgr: Exiting thread");
1692 complete_and_exit(&hi->exited, 0);
1695 int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
1697 struct class *class = &hpsb_host_class;
1698 struct class_device *cdev;
1699 struct hpsb_host *host;
1702 down_read(&class->subsys.rwsem);
1703 list_for_each_entry(cdev, &class->children, node) {
1704 host = container_of(cdev, struct hpsb_host, class_dev);
1706 if ((error = cb(host, __data)))
1709 up_read(&class->subsys.rwsem);
1714 /* The following four convenience functions use a struct node_entry
1715 * for addressing a node on the bus. They are intended for use by any
1716 * process context, not just the nodemgr thread, so we need to be a
1717 * little careful when reading out the node ID and generation. The
1718 * thing that can go wrong is that we get the node ID, then a bus
1719 * reset occurs, and then we read the generation. The node ID is
1720 * possibly invalid, but the generation is current, and we end up
1721 * sending a packet to a the wrong node.
1723 * The solution is to make sure we read the generation first, so that
1724 * if a reset occurs in the process, we end up with a stale generation
1725 * and the transactions will fail instead of silently using wrong node
1729 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
1731 pkt->host = ne->host;
1732 pkt->generation = ne->generation;
1734 pkt->node_id = ne->nodeid;
1737 int hpsb_node_write(struct node_entry *ne, u64 addr,
1738 quadlet_t *buffer, size_t length)
1740 unsigned int generation = ne->generation;
1743 return hpsb_write(ne->host, ne->nodeid, generation,
1744 addr, buffer, length);
1747 static void nodemgr_add_host(struct hpsb_host *host)
1749 struct host_info *hi;
1751 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1754 HPSB_ERR ("NodeMgr: out of memory in add host");
1759 init_completion(&hi->exited);
1760 sema_init(&hi->reset_sem, 0);
1762 sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
1764 hi->pid = kernel_thread(nodemgr_host_thread, hi, CLONE_KERNEL);
1767 HPSB_ERR ("NodeMgr: failed to start %s thread for %s",
1768 hi->daemon_name, host->driver->name);
1769 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1776 static void nodemgr_host_reset(struct hpsb_host *host)
1778 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1781 HPSB_VERBOSE("NodeMgr: Processing host reset for %s", hi->daemon_name);
1784 HPSB_ERR ("NodeMgr: could not process reset of unused host");
1789 static void nodemgr_remove_host(struct hpsb_host *host)
1791 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1798 wait_for_completion(&hi->exited);
1799 nodemgr_remove_host_dev(&host->device);
1802 HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
1803 host->driver->name);
1808 static struct hpsb_highlevel nodemgr_highlevel = {
1809 .name = "Node manager",
1810 .add_host = nodemgr_add_host,
1811 .host_reset = nodemgr_host_reset,
1812 .remove_host = nodemgr_remove_host,
1815 int init_ieee1394_nodemgr(void)
1819 ret = class_register(&nodemgr_ne_class);
1823 ret = class_register(&nodemgr_ud_class);
1825 class_unregister(&nodemgr_ne_class);
1829 hpsb_register_highlevel(&nodemgr_highlevel);
1834 void cleanup_ieee1394_nodemgr(void)
1836 hpsb_unregister_highlevel(&nodemgr_highlevel);
1838 class_unregister(&nodemgr_ud_class);
1839 class_unregister(&nodemgr_ne_class);