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/delay.h>
16 #include <linux/kthread.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 DEFINE_MUTEX(nodemgr_serialize);
164 struct hpsb_host *host;
165 struct list_head list;
166 struct task_struct *thread;
169 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
170 static int nodemgr_uevent(struct class_device *cdev, char **envp, int num_envp,
171 char *buffer, int buffer_size);
172 static void nodemgr_resume_ne(struct node_entry *ne);
173 static void nodemgr_remove_ne(struct node_entry *ne);
174 static struct node_entry *find_entry_by_guid(u64 guid);
176 struct bus_type ieee1394_bus_type = {
178 .match = nodemgr_bus_match,
181 static void host_cls_release(struct class_device *class_dev)
183 put_device(&container_of((class_dev), struct hpsb_host, class_dev)->device);
186 struct class hpsb_host_class = {
187 .name = "ieee1394_host",
188 .release = host_cls_release,
191 static void ne_cls_release(struct class_device *class_dev)
193 put_device(&container_of((class_dev), struct node_entry, class_dev)->device);
196 static struct class nodemgr_ne_class = {
197 .name = "ieee1394_node",
198 .release = ne_cls_release,
201 static void ud_cls_release(struct class_device *class_dev)
203 put_device(&container_of((class_dev), struct unit_directory, class_dev)->device);
206 /* The name here is only so that unit directory hotplug works with old
207 * style hotplug, which only ever did unit directories anyway. */
208 static struct class nodemgr_ud_class = {
210 .release = ud_cls_release,
211 .uevent = nodemgr_uevent,
214 static struct hpsb_highlevel nodemgr_highlevel;
217 static void nodemgr_release_ud(struct device *dev)
219 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
221 if (ud->vendor_name_kv)
222 csr1212_release_keyval(ud->vendor_name_kv);
223 if (ud->model_name_kv)
224 csr1212_release_keyval(ud->model_name_kv);
229 static void nodemgr_release_ne(struct device *dev)
231 struct node_entry *ne = container_of(dev, struct node_entry, device);
233 if (ne->vendor_name_kv)
234 csr1212_release_keyval(ne->vendor_name_kv);
240 static void nodemgr_release_host(struct device *dev)
242 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
244 csr1212_destroy_csr(host->csr.rom);
249 static int nodemgr_ud_platform_data;
251 static struct device nodemgr_dev_template_ud = {
252 .bus = &ieee1394_bus_type,
253 .release = nodemgr_release_ud,
254 .platform_data = &nodemgr_ud_platform_data,
257 static struct device nodemgr_dev_template_ne = {
258 .bus = &ieee1394_bus_type,
259 .release = nodemgr_release_ne,
262 struct device nodemgr_dev_template_host = {
263 .bus = &ieee1394_bus_type,
264 .release = nodemgr_release_host,
268 #define fw_attr(class, class_type, field, type, format_string) \
269 static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
272 class = container_of(dev, class_type, device); \
273 return sprintf(buf, format_string, (type)class->field); \
275 static struct device_attribute dev_attr_##class##_##field = { \
276 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
277 .show = fw_show_##class##_##field, \
280 #define fw_attr_td(class, class_type, td_kv) \
281 static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
284 class_type *class = container_of(dev, class_type, device); \
285 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
287 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
289 while ((buf + len - 1) == '\0') \
295 static struct device_attribute dev_attr_##class##_##td_kv = { \
296 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
297 .show = fw_show_##class##_##td_kv, \
301 #define fw_drv_attr(field, type, format_string) \
302 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
304 struct hpsb_protocol_driver *driver; \
305 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
306 return sprintf(buf, format_string, (type)driver->field);\
308 static struct driver_attribute driver_attr_drv_##field = { \
309 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
310 .show = fw_drv_show_##field, \
314 static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
316 struct node_entry *ne = container_of(dev, struct node_entry, device);
318 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
319 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
321 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
322 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
325 ne->busopt.cyc_clk_acc);
327 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
330 /* tlabels_free, tlabels_allocations, tlabels_mask are read non-atomically
331 * here, therefore displayed values may be occasionally wrong. */
332 static ssize_t fw_show_ne_tlabels_free(struct device *dev, struct device_attribute *attr, char *buf)
334 struct node_entry *ne = container_of(dev, struct node_entry, device);
335 return sprintf(buf, "%d\n", 64 - bitmap_weight(ne->tpool->pool, 64));
337 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
340 static ssize_t fw_show_ne_tlabels_allocations(struct device *dev, struct device_attribute *attr, char *buf)
342 struct node_entry *ne = container_of(dev, struct node_entry, device);
343 return sprintf(buf, "%u\n", ne->tpool->allocations);
345 static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
348 static ssize_t fw_show_ne_tlabels_mask(struct device *dev, struct device_attribute *attr, char *buf)
350 struct node_entry *ne = container_of(dev, struct node_entry, device);
351 #if (BITS_PER_LONG <= 32)
352 return sprintf(buf, "0x%08lx%08lx\n", ne->tpool->pool[0], ne->tpool->pool[1]);
354 return sprintf(buf, "0x%016lx\n", ne->tpool->pool[0]);
357 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
360 static ssize_t fw_set_ignore_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
362 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
363 int state = simple_strtoul(buf, NULL, 10);
366 down_write(&dev->bus->subsys.rwsem);
367 device_release_driver(dev);
368 ud->ignore_driver = 1;
369 up_write(&dev->bus->subsys.rwsem);
371 ud->ignore_driver = 0;
375 static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
377 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
379 return sprintf(buf, "%d\n", ud->ignore_driver);
381 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
384 static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
386 struct node_entry *ne;
387 u64 guid = (u64)simple_strtoull(buf, NULL, 16);
389 ne = find_entry_by_guid(guid);
391 if (ne == NULL || !ne->in_limbo)
394 nodemgr_remove_ne(ne);
398 static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
400 return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
402 static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
405 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf, size_t count)
407 if (simple_strtoul(buf, NULL, 10) == 1)
408 bus_rescan_devices(&ieee1394_bus_type);
411 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
413 return sprintf(buf, "You can force a rescan of the bus for "
414 "drivers by writing a 1 to this file\n");
416 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
419 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
421 int state = simple_strtoul(buf, NULL, 10);
430 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
432 return sprintf(buf, "%d\n", ignore_drivers);
434 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
437 struct bus_attribute *const fw_bus_attrs[] = {
438 &bus_attr_destroy_node,
440 &bus_attr_ignore_drivers,
445 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
446 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
448 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
449 fw_attr_td(ne, struct node_entry, vendor_name_kv)
450 fw_attr(ne, struct node_entry, vendor_oui, const char *, "%s\n")
452 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
453 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
454 fw_attr(ne, struct node_entry, guid_vendor_oui, const char *, "%s\n")
455 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
457 static struct device_attribute *const fw_ne_attrs[] = {
459 &dev_attr_ne_guid_vendor_id,
460 &dev_attr_ne_capabilities,
461 &dev_attr_ne_vendor_id,
463 &dev_attr_bus_options,
464 &dev_attr_tlabels_free,
465 &dev_attr_tlabels_allocations,
466 &dev_attr_tlabels_mask,
471 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
472 fw_attr(ud, struct unit_directory, length, int, "%d\n")
473 /* These are all dependent on the value being provided */
474 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
475 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
476 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
477 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
478 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
479 fw_attr(ud, struct unit_directory, vendor_oui, const char *, "%s\n")
480 fw_attr_td(ud, struct unit_directory, model_name_kv)
482 static struct device_attribute *const fw_ud_attrs[] = {
483 &dev_attr_ud_address,
485 &dev_attr_ignore_driver,
489 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
490 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
491 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
492 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
493 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
494 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
495 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
496 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
498 static struct device_attribute *const fw_host_attrs[] = {
499 &dev_attr_host_node_count,
500 &dev_attr_host_selfid_count,
501 &dev_attr_host_nodes_active,
502 &dev_attr_host_in_bus_reset,
503 &dev_attr_host_is_root,
504 &dev_attr_host_is_cycmst,
505 &dev_attr_host_is_irm,
506 &dev_attr_host_is_busmgr,
510 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
512 struct hpsb_protocol_driver *driver;
513 struct ieee1394_device_id *id;
517 driver = container_of(drv, struct hpsb_protocol_driver, driver);
519 for (id = driver->id_table; id->match_flags != 0; id++) {
522 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
523 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
524 scratch = buf + length;
528 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
529 length += sprintf(scratch, "%smodel_id=0x%06x",
530 need_coma++ ? "," : "",
532 scratch = buf + length;
535 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
536 length += sprintf(scratch, "%sspecifier_id=0x%06x",
537 need_coma++ ? "," : "",
539 scratch = buf + length;
542 if (id->match_flags & IEEE1394_MATCH_VERSION) {
543 length += sprintf(scratch, "%sversion=0x%06x",
544 need_coma++ ? "," : "",
546 scratch = buf + length;
557 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
560 fw_drv_attr(name, const char *, "%s\n")
562 static struct driver_attribute *const fw_drv_attrs[] = {
563 &driver_attr_drv_name,
564 &driver_attr_device_ids,
568 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
570 struct device_driver *drv = &driver->driver;
573 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
574 driver_create_file(drv, fw_drv_attrs[i]);
578 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
580 struct device_driver *drv = &driver->driver;
583 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
584 driver_remove_file(drv, fw_drv_attrs[i]);
588 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
590 struct device *dev = &ne->device;
593 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
594 device_create_file(dev, fw_ne_attrs[i]);
598 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
600 struct device *dev = &host->device;
603 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
604 device_create_file(dev, fw_host_attrs[i]);
608 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid);
610 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
612 struct device *dev = &host->device;
613 struct node_entry *ne;
615 sysfs_remove_link(&dev->kobj, "irm_id");
616 sysfs_remove_link(&dev->kobj, "busmgr_id");
617 sysfs_remove_link(&dev->kobj, "host_id");
619 if ((ne = find_entry_by_nodeid(host, host->irm_id)))
620 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id");
621 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)))
622 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id");
623 if ((ne = find_entry_by_nodeid(host, host->node_id)))
624 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id");
627 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
629 struct device *dev = &ud->device;
632 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
633 device_create_file(dev, fw_ud_attrs[i]);
635 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
636 device_create_file(dev, &dev_attr_ud_specifier_id);
638 if (ud->flags & UNIT_DIRECTORY_VERSION)
639 device_create_file(dev, &dev_attr_ud_version);
641 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
642 device_create_file(dev, &dev_attr_ud_vendor_id);
643 if (ud->vendor_name_kv)
644 device_create_file(dev, &dev_attr_ud_vendor_name_kv);
647 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
648 device_create_file(dev, &dev_attr_ud_model_id);
649 if (ud->model_name_kv)
650 device_create_file(dev, &dev_attr_ud_model_name_kv);
655 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
657 struct hpsb_protocol_driver *driver;
658 struct unit_directory *ud;
659 struct ieee1394_device_id *id;
661 /* We only match unit directories */
662 if (dev->platform_data != &nodemgr_ud_platform_data)
665 ud = container_of(dev, struct unit_directory, device);
666 driver = container_of(drv, struct hpsb_protocol_driver, driver);
668 if (ud->ne->in_limbo || ud->ignore_driver)
671 for (id = driver->id_table; id->match_flags != 0; id++) {
672 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
673 id->vendor_id != ud->vendor_id)
676 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
677 id->model_id != ud->model_id)
680 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
681 id->specifier_id != ud->specifier_id)
684 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
685 id->version != ud->version)
695 static void nodemgr_remove_uds(struct node_entry *ne)
697 struct class_device *cdev, *next;
698 struct unit_directory *ud;
700 list_for_each_entry_safe(cdev, next, &nodemgr_ud_class.children, node) {
701 ud = container_of(cdev, struct unit_directory, class_dev);
706 class_device_unregister(&ud->class_dev);
707 device_unregister(&ud->device);
712 static void nodemgr_remove_ne(struct node_entry *ne)
714 struct device *dev = &ne->device;
716 dev = get_device(&ne->device);
720 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
721 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
723 nodemgr_remove_uds(ne);
725 class_device_unregister(&ne->class_dev);
726 device_unregister(dev);
731 static int __nodemgr_remove_host_dev(struct device *dev, void *data)
733 nodemgr_remove_ne(container_of(dev, struct node_entry, device));
737 static void nodemgr_remove_host_dev(struct device *dev)
739 device_for_each_child(dev, NULL, __nodemgr_remove_host_dev);
740 sysfs_remove_link(&dev->kobj, "irm_id");
741 sysfs_remove_link(&dev->kobj, "busmgr_id");
742 sysfs_remove_link(&dev->kobj, "host_id");
746 static void nodemgr_update_bus_options(struct node_entry *ne)
748 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
749 static const u16 mr[] = { 4, 64, 1024, 0};
751 quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
753 ne->busopt.irmc = (busoptions >> 31) & 1;
754 ne->busopt.cmc = (busoptions >> 30) & 1;
755 ne->busopt.isc = (busoptions >> 29) & 1;
756 ne->busopt.bmc = (busoptions >> 28) & 1;
757 ne->busopt.pmc = (busoptions >> 27) & 1;
758 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
759 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
760 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
761 ne->busopt.generation = (busoptions >> 4) & 0xf;
762 ne->busopt.lnkspd = busoptions & 0x7;
764 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
765 "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
766 busoptions, ne->busopt.irmc, ne->busopt.cmc,
767 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
768 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
769 mr[ne->busopt.max_rom],
770 ne->busopt.generation, ne->busopt.lnkspd);
774 static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
775 struct host_info *hi, nodeid_t nodeid,
776 unsigned int generation)
778 struct hpsb_host *host = hi->host;
779 struct node_entry *ne;
781 ne = kzalloc(sizeof(*ne), GFP_KERNEL);
785 ne->tpool = &host->tpool[nodeid & NODE_MASK];
789 ne->generation = generation;
793 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
794 ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
797 memcpy(&ne->device, &nodemgr_dev_template_ne,
799 ne->device.parent = &host->device;
800 snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
801 (unsigned long long)(ne->guid));
803 ne->class_dev.dev = &ne->device;
804 ne->class_dev.class = &nodemgr_ne_class;
805 snprintf(ne->class_dev.class_id, BUS_ID_SIZE, "%016Lx",
806 (unsigned long long)(ne->guid));
808 device_register(&ne->device);
809 class_device_register(&ne->class_dev);
810 get_device(&ne->device);
812 if (ne->guid_vendor_oui)
813 device_create_file(&ne->device, &dev_attr_ne_guid_vendor_oui);
814 nodemgr_create_ne_dev_files(ne);
816 nodemgr_update_bus_options(ne);
818 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
819 (host->node_id == nodeid) ? "Host" : "Node",
820 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
826 static struct node_entry *find_entry_by_guid(u64 guid)
828 struct class *class = &nodemgr_ne_class;
829 struct class_device *cdev;
830 struct node_entry *ne, *ret_ne = NULL;
832 down_read(&class->subsys.rwsem);
833 list_for_each_entry(cdev, &class->children, node) {
834 ne = container_of(cdev, struct node_entry, class_dev);
836 if (ne->guid == guid) {
841 up_read(&class->subsys.rwsem);
847 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
849 struct class *class = &nodemgr_ne_class;
850 struct class_device *cdev;
851 struct node_entry *ne, *ret_ne = NULL;
853 down_read(&class->subsys.rwsem);
854 list_for_each_entry(cdev, &class->children, node) {
855 ne = container_of(cdev, struct node_entry, class_dev);
857 if (ne->host == host && ne->nodeid == nodeid) {
862 up_read(&class->subsys.rwsem);
868 static void nodemgr_register_device(struct node_entry *ne,
869 struct unit_directory *ud, struct device *parent)
871 memcpy(&ud->device, &nodemgr_dev_template_ud,
874 ud->device.parent = parent;
876 snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
877 ne->device.bus_id, ud->id);
879 ud->class_dev.dev = &ud->device;
880 ud->class_dev.class = &nodemgr_ud_class;
881 snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
882 ne->device.bus_id, ud->id);
884 device_register(&ud->device);
885 class_device_register(&ud->class_dev);
886 get_device(&ud->device);
889 device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
890 nodemgr_create_ud_dev_files(ud);
894 /* This implementation currently only scans the config rom and its
895 * immediate unit directories looking for software_id and
896 * software_version entries, in order to get driver autoloading working. */
897 static struct unit_directory *nodemgr_process_unit_directory
898 (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
899 unsigned int *id, struct unit_directory *parent)
901 struct unit_directory *ud;
902 struct unit_directory *ud_child = NULL;
903 struct csr1212_dentry *dentry;
904 struct csr1212_keyval *kv;
907 ud = kzalloc(sizeof(*ud), GFP_KERNEL);
909 goto unit_directory_error;
912 ud->ignore_driver = ignore_drivers;
913 ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
917 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
918 switch (kv->key.id) {
919 case CSR1212_KV_ID_VENDOR:
920 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
921 ud->vendor_id = kv->value.immediate;
922 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
925 ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
929 case CSR1212_KV_ID_MODEL:
930 ud->model_id = kv->value.immediate;
931 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
934 case CSR1212_KV_ID_SPECIFIER_ID:
935 ud->specifier_id = kv->value.immediate;
936 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
939 case CSR1212_KV_ID_VERSION:
940 ud->version = kv->value.immediate;
941 ud->flags |= UNIT_DIRECTORY_VERSION;
944 case CSR1212_KV_ID_DESCRIPTOR:
945 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
946 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
947 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
948 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
949 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
950 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
951 switch (last_key_id) {
952 case CSR1212_KV_ID_VENDOR:
953 ud->vendor_name_kv = kv;
954 csr1212_keep_keyval(kv);
957 case CSR1212_KV_ID_MODEL:
958 ud->model_name_kv = kv;
959 csr1212_keep_keyval(kv);
963 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
966 case CSR1212_KV_ID_DEPENDENT_INFO:
967 /* Logical Unit Number */
968 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
969 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
970 ud_child = kmalloc(sizeof(*ud_child), GFP_KERNEL);
972 goto unit_directory_error;
973 memcpy(ud_child, ud, sizeof(*ud_child));
974 nodemgr_register_device(ne, ud_child, &ne->device);
979 ud->lun = kv->value.immediate;
980 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
982 /* Logical Unit Directory */
983 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
984 /* This should really be done in SBP2 as this is
985 * doing SBP2 specific parsing.
988 /* first register the parent unit */
989 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
990 if (ud->device.bus != &ieee1394_bus_type)
991 nodemgr_register_device(ne, ud, &ne->device);
993 /* process the child unit */
994 ud_child = nodemgr_process_unit_directory(hi, ne, kv, id, ud);
996 if (ud_child == NULL)
999 /* inherit unspecified values, the driver core picks it up */
1000 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
1001 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
1003 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
1004 ud_child->model_id = ud->model_id;
1006 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
1007 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
1009 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1010 ud_child->specifier_id = ud->specifier_id;
1012 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
1013 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
1015 ud_child->flags |= UNIT_DIRECTORY_VERSION;
1016 ud_child->version = ud->version;
1019 /* register the child unit */
1020 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
1021 nodemgr_register_device(ne, ud_child, &ud->device);
1029 last_key_id = kv->key.id;
1032 /* do not process child units here and only if not already registered */
1033 if (!parent && ud->device.bus != &ieee1394_bus_type)
1034 nodemgr_register_device(ne, ud, &ne->device);
1038 unit_directory_error:
1044 static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
1046 unsigned int ud_id = 0;
1047 struct csr1212_dentry *dentry;
1048 struct csr1212_keyval *kv;
1051 ne->needs_probe = 0;
1053 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1054 switch (kv->key.id) {
1055 case CSR1212_KV_ID_VENDOR:
1056 ne->vendor_id = kv->value.immediate;
1059 ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
1062 case CSR1212_KV_ID_NODE_CAPABILITIES:
1063 ne->capabilities = kv->value.immediate;
1066 case CSR1212_KV_ID_UNIT:
1067 nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
1070 case CSR1212_KV_ID_DESCRIPTOR:
1071 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1072 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1073 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1074 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1075 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1076 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1077 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1078 ne->vendor_name_kv = kv;
1079 csr1212_keep_keyval(kv);
1084 last_key_id = kv->key.id;
1088 device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
1089 if (ne->vendor_name_kv)
1090 device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
1093 #ifdef CONFIG_HOTPLUG
1095 static int nodemgr_uevent(struct class_device *cdev, char **envp, int num_envp,
1096 char *buffer, int buffer_size)
1098 struct unit_directory *ud;
1101 /* ieee1394:venNmoNspNverN */
1102 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
1107 ud = container_of(cdev, struct unit_directory, class_dev);
1109 if (ud->ne->in_limbo || ud->ignore_driver)
1112 #define PUT_ENVP(fmt,val) \
1115 envp[i++] = buffer; \
1116 printed = snprintf(buffer, buffer_size - length, \
1118 if ((buffer_size - (length+printed) <= 0) || (i >= num_envp)) \
1120 length += printed+1; \
1121 buffer += printed+1; \
1124 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1125 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1126 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1127 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1128 PUT_ENVP("VERSION=%06x", ud->version);
1129 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1134 PUT_ENVP("MODALIAS=%s", buf);
1145 static int nodemgr_uevent(struct class_device *cdev, char **envp, int num_envp,
1146 char *buffer, int buffer_size)
1151 #endif /* CONFIG_HOTPLUG */
1154 int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
1158 /* This will cause a probe for devices */
1159 ret = driver_register(&driver->driver);
1161 nodemgr_create_drv_files(driver);
1166 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1168 nodemgr_remove_drv_files(driver);
1169 /* This will subsequently disconnect all devices that our driver
1170 * is attached to. */
1171 driver_unregister(&driver->driver);
1176 * This function updates nodes that were present on the bus before the
1177 * reset and still are after the reset. The nodeid and the config rom
1178 * may have changed, and the drivers managing this device must be
1179 * informed that this device just went through a bus reset, to allow
1180 * the to take whatever actions required.
1182 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1183 struct host_info *hi, nodeid_t nodeid,
1184 unsigned int generation)
1186 if (ne->nodeid != nodeid) {
1187 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1188 NODE_BUS_ARGS(ne->host, ne->nodeid),
1189 NODE_BUS_ARGS(ne->host, nodeid));
1190 ne->nodeid = nodeid;
1193 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1194 kfree(ne->csr->private);
1195 csr1212_destroy_csr(ne->csr);
1198 /* If the node's configrom generation has changed, we
1199 * unregister all the unit directories. */
1200 nodemgr_remove_uds(ne);
1202 nodemgr_update_bus_options(ne);
1204 /* Mark the node as new, so it gets re-probed */
1205 ne->needs_probe = 1;
1207 /* old cache is valid, so update its generation */
1208 struct nodemgr_csr_info *ci = ne->csr->private;
1209 ci->generation = generation;
1210 /* free the partially filled now unneeded new cache */
1211 kfree(csr->private);
1212 csr1212_destroy_csr(csr);
1216 nodemgr_resume_ne(ne);
1218 /* Mark the node current */
1219 ne->generation = generation;
1224 static void nodemgr_node_scan_one(struct host_info *hi,
1225 nodeid_t nodeid, int generation)
1227 struct hpsb_host *host = hi->host;
1228 struct node_entry *ne;
1230 struct csr1212_csr *csr;
1231 struct nodemgr_csr_info *ci;
1234 ci = kmalloc(sizeof(*ci), GFP_KERNEL);
1239 ci->nodeid = nodeid;
1240 ci->generation = generation;
1242 /* Prepare for speed probe which occurs when reading the ROM */
1243 speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
1244 if (*speed > host->csr.lnk_spd)
1245 *speed = host->csr.lnk_spd;
1246 ci->speed_unverified = *speed > IEEE1394_SPEED_100;
1248 /* We need to detect when the ConfigROM's generation has changed,
1249 * so we only update the node's info when it needs to be. */
1251 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1252 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1253 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1254 NODE_BUS_ARGS(host, nodeid));
1256 csr1212_destroy_csr(csr);
1261 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1262 /* This isn't a 1394 device, but we let it slide. There
1263 * was a report of a device with broken firmware which
1264 * reported '2394' instead of '1394', which is obviously a
1265 * mistake. One would hope that a non-1394 device never
1266 * gets connected to Firewire bus. If someone does, we
1267 * shouldn't be held responsible, so we'll allow it with a
1269 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1270 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1273 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1274 ne = find_entry_by_guid(guid);
1276 if (ne && ne->host != host && ne->in_limbo) {
1277 /* Must have moved this device from one host to another */
1278 nodemgr_remove_ne(ne);
1283 nodemgr_create_node(guid, csr, hi, nodeid, generation);
1285 nodemgr_update_node(ne, csr, hi, nodeid, generation);
1289 static void nodemgr_node_scan(struct host_info *hi, int generation)
1292 struct hpsb_host *host = hi->host;
1293 struct selfid *sid = (struct selfid *)host->topology_map;
1294 nodeid_t nodeid = LOCAL_BUS;
1296 /* Scan each node on the bus */
1297 for (count = host->selfid_count; count; count--, sid++) {
1301 if (!sid->link_active) {
1305 nodemgr_node_scan_one(hi, nodeid++, generation);
1310 static void nodemgr_suspend_ne(struct node_entry *ne)
1312 struct class_device *cdev;
1313 struct unit_directory *ud;
1315 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1316 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1319 device_create_file(&ne->device, &dev_attr_ne_in_limbo);
1321 down_write(&ne->device.bus->subsys.rwsem);
1322 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1323 ud = container_of(cdev, struct unit_directory, class_dev);
1328 if (ud->device.driver &&
1329 (!ud->device.driver->suspend ||
1330 ud->device.driver->suspend(&ud->device, PMSG_SUSPEND)))
1331 device_release_driver(&ud->device);
1333 up_write(&ne->device.bus->subsys.rwsem);
1337 static void nodemgr_resume_ne(struct node_entry *ne)
1339 struct class_device *cdev;
1340 struct unit_directory *ud;
1343 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1345 down_read(&ne->device.bus->subsys.rwsem);
1346 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1347 ud = container_of(cdev, struct unit_directory, class_dev);
1352 if (ud->device.driver && ud->device.driver->resume)
1353 ud->device.driver->resume(&ud->device);
1355 up_read(&ne->device.bus->subsys.rwsem);
1357 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1358 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1362 static void nodemgr_update_pdrv(struct node_entry *ne)
1364 struct unit_directory *ud;
1365 struct hpsb_protocol_driver *pdrv;
1366 struct class *class = &nodemgr_ud_class;
1367 struct class_device *cdev;
1369 down_read(&class->subsys.rwsem);
1370 list_for_each_entry(cdev, &class->children, node) {
1371 ud = container_of(cdev, struct unit_directory, class_dev);
1372 if (ud->ne != ne || !ud->device.driver)
1375 pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
1377 if (pdrv->update && pdrv->update(ud)) {
1378 down_write(&ud->device.bus->subsys.rwsem);
1379 device_release_driver(&ud->device);
1380 up_write(&ud->device.bus->subsys.rwsem);
1383 up_read(&class->subsys.rwsem);
1387 /* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1388 * seems like an optional service but in the end it is practically mandatory
1389 * as a consequence of these clauses.
1391 * Note that we cannot do a broadcast write to all nodes at once because some
1392 * pre-1394a devices would hang. */
1393 static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1395 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1396 quadlet_t bc_remote, bc_local;
1399 if (!ne->host->is_irm || ne->generation != generation ||
1400 ne->nodeid == ne->host->node_id)
1403 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1405 /* Check if the register is implemented and 1394a compliant. */
1406 ret = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1408 if (!ret && bc_remote & cpu_to_be32(0x80000000) &&
1409 bc_remote != bc_local)
1410 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1414 static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
1418 if (ne->host != hi->host || ne->in_limbo)
1421 dev = get_device(&ne->device);
1425 nodemgr_irm_write_bc(ne, generation);
1427 /* If "needs_probe", then this is either a new or changed node we
1428 * rescan totally. If the generation matches for an existing node
1429 * (one that existed prior to the bus reset) we send update calls
1430 * down to the drivers. Otherwise, this is a dead node and we
1432 if (ne->needs_probe)
1433 nodemgr_process_root_directory(hi, ne);
1434 else if (ne->generation == generation)
1435 nodemgr_update_pdrv(ne);
1437 nodemgr_suspend_ne(ne);
1443 static void nodemgr_node_probe(struct host_info *hi, int generation)
1445 struct hpsb_host *host = hi->host;
1446 struct class *class = &nodemgr_ne_class;
1447 struct class_device *cdev;
1448 struct node_entry *ne;
1450 /* Do some processing of the nodes we've probed. This pulls them
1451 * into the sysfs layer if needed, and can result in processing of
1452 * unit-directories, or just updating the node and it's
1455 * Run updates before probes. Usually, updates are time-critical
1456 * while probes are time-consuming. (Well, those probes need some
1457 * improvement...) */
1459 down_read(&class->subsys.rwsem);
1460 list_for_each_entry(cdev, &class->children, node) {
1461 ne = container_of(cdev, struct node_entry, class_dev);
1462 if (!ne->needs_probe)
1463 nodemgr_probe_ne(hi, ne, generation);
1465 list_for_each_entry(cdev, &class->children, node) {
1466 ne = container_of(cdev, struct node_entry, class_dev);
1467 if (ne->needs_probe)
1468 nodemgr_probe_ne(hi, ne, generation);
1470 up_read(&class->subsys.rwsem);
1473 /* If we had a bus reset while we were scanning the bus, it is
1474 * possible that we did not probe all nodes. In that case, we
1475 * skip the clean up for now, since we could remove nodes that
1476 * were still on the bus. Another bus scan is pending which will
1477 * do the clean up eventually.
1479 * Now let's tell the bus to rescan our devices. This may seem
1480 * like overhead, but the driver-model core will only scan a
1481 * device for a driver when either the device is added, or when a
1482 * new driver is added. A bus reset is a good reason to rescan
1483 * devices that were there before. For example, an sbp2 device
1484 * may become available for login, if the host that held it was
1487 if (generation == get_hpsb_generation(host))
1488 bus_rescan_devices(&ieee1394_bus_type);
1493 static int nodemgr_send_resume_packet(struct hpsb_host *host)
1495 struct hpsb_packet *packet;
1498 packet = hpsb_make_phypacket(host,
1499 EXTPHYPACKET_TYPE_RESUME |
1500 NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
1502 packet->no_waiter = 1;
1503 packet->generation = get_hpsb_generation(host);
1504 ret = hpsb_send_packet(packet);
1507 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1512 /* Perform a few high-level IRM responsibilities. */
1513 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1517 /* if irm_id == -1 then there is no IRM on this bus */
1518 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1521 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1522 host->csr.broadcast_channel |= 0x40000000;
1524 /* If there is no bus manager then we should set the root node's
1525 * force_root bit to promote bus stability per the 1394
1526 * spec. (8.4.2.6) */
1527 if (host->busmgr_id == 0xffff && host->node_count > 1)
1529 u16 root_node = host->node_count - 1;
1531 /* get cycle master capability flag from root node */
1532 if (host->is_cycmst ||
1533 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1534 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1535 &bc, sizeof(quadlet_t)) &&
1536 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
1537 hpsb_send_phy_config(host, root_node, -1);
1539 HPSB_DEBUG("The root node is not cycle master capable; "
1540 "selecting a new root node and resetting...");
1543 /* Oh screw it! Just leave the bus as it is */
1544 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1548 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1549 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1555 /* Some devices suspend their ports while being connected to an inactive
1556 * host adapter, i.e. if connected before the low-level driver is
1557 * loaded. They become visible either when physically unplugged and
1558 * replugged, or when receiving a resume packet. Send one once. */
1559 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1560 host->resume_packet_sent = 1;
1565 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1566 * everything we can do, otherwise issue a bus reset and try to become the IRM
1568 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1573 if (hpsb_disable_irm || host->is_irm)
1576 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1577 get_hpsb_generation(host),
1578 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1579 &bc, sizeof(quadlet_t));
1581 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1582 /* The current irm node does not have a valid BROADCAST_CHANNEL
1583 * register and we do, so reset the bus with force_root set */
1584 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1587 /* Oh screw it! Just leave the bus as it is */
1588 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1592 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1593 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1601 static int nodemgr_host_thread(void *__hi)
1603 struct host_info *hi = (struct host_info *)__hi;
1604 struct hpsb_host *host = hi->host;
1605 unsigned int g, generation = get_hpsb_generation(host) - 1;
1606 int i, reset_cycles = 0;
1608 /* Setup our device-model entries */
1609 nodemgr_create_host_dev_files(host);
1612 /* Sleep until next bus reset */
1613 set_current_state(TASK_INTERRUPTIBLE);
1614 if (get_hpsb_generation(host) == generation)
1616 __set_current_state(TASK_RUNNING);
1618 /* Thread may have been woken up to freeze or to exit */
1619 if (try_to_freeze())
1621 if (kthread_should_stop())
1624 if (mutex_lock_interruptible(&nodemgr_serialize)) {
1625 if (try_to_freeze())
1630 /* Pause for 1/4 second in 1/16 second intervals,
1631 * to make sure things settle down. */
1632 g = get_hpsb_generation(host);
1633 for (i = 0; i < 4 ; i++) {
1634 if (msleep_interruptible(63) || kthread_should_stop())
1637 /* Now get the generation in which the node ID's we collect
1638 * are valid. During the bus scan we will use this generation
1639 * for the read transactions, so that if another reset occurs
1640 * during the scan the transactions will fail instead of
1641 * returning bogus data. */
1642 generation = get_hpsb_generation(host);
1644 /* If we get a reset before we are done waiting, then
1645 * start the the waiting over again */
1646 if (generation != g)
1647 g = generation, i = 0;
1650 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1651 !nodemgr_do_irm_duties(host, reset_cycles)) {
1653 mutex_unlock(&nodemgr_serialize);
1658 /* Scan our nodes to get the bus options and create node
1659 * entries. This does not do the sysfs stuff, since that
1660 * would trigger uevents and such, which is a bad idea at
1662 nodemgr_node_scan(hi, generation);
1664 /* This actually does the full probe, with sysfs
1666 nodemgr_node_probe(hi, generation);
1668 /* Update some of our sysfs symlinks */
1669 nodemgr_update_host_dev_links(host);
1671 mutex_unlock(&nodemgr_serialize);
1674 mutex_unlock(&nodemgr_serialize);
1676 HPSB_VERBOSE("NodeMgr: Exiting thread");
1680 int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
1682 struct class *class = &hpsb_host_class;
1683 struct class_device *cdev;
1684 struct hpsb_host *host;
1687 down_read(&class->subsys.rwsem);
1688 list_for_each_entry(cdev, &class->children, node) {
1689 host = container_of(cdev, struct hpsb_host, class_dev);
1691 if ((error = cb(host, __data)))
1694 up_read(&class->subsys.rwsem);
1699 /* The following four convenience functions use a struct node_entry
1700 * for addressing a node on the bus. They are intended for use by any
1701 * process context, not just the nodemgr thread, so we need to be a
1702 * little careful when reading out the node ID and generation. The
1703 * thing that can go wrong is that we get the node ID, then a bus
1704 * reset occurs, and then we read the generation. The node ID is
1705 * possibly invalid, but the generation is current, and we end up
1706 * sending a packet to a the wrong node.
1708 * The solution is to make sure we read the generation first, so that
1709 * if a reset occurs in the process, we end up with a stale generation
1710 * and the transactions will fail instead of silently using wrong node
1714 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
1716 pkt->host = ne->host;
1717 pkt->generation = ne->generation;
1719 pkt->node_id = ne->nodeid;
1722 int hpsb_node_write(struct node_entry *ne, u64 addr,
1723 quadlet_t *buffer, size_t length)
1725 unsigned int generation = ne->generation;
1728 return hpsb_write(ne->host, ne->nodeid, generation,
1729 addr, buffer, length);
1732 static void nodemgr_add_host(struct hpsb_host *host)
1734 struct host_info *hi;
1736 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1738 HPSB_ERR("NodeMgr: out of memory in add host");
1742 hi->thread = kthread_run(nodemgr_host_thread, hi, "knodemgrd_%d",
1744 if (IS_ERR(hi->thread)) {
1745 HPSB_ERR("NodeMgr: cannot start thread for host %d", host->id);
1746 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1750 static void nodemgr_host_reset(struct hpsb_host *host)
1752 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1755 HPSB_VERBOSE("NodeMgr: Processing reset for host %d", host->id);
1756 wake_up_process(hi->thread);
1760 static void nodemgr_remove_host(struct hpsb_host *host)
1762 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1765 kthread_stop(hi->thread);
1766 nodemgr_remove_host_dev(&host->device);
1770 static struct hpsb_highlevel nodemgr_highlevel = {
1771 .name = "Node manager",
1772 .add_host = nodemgr_add_host,
1773 .host_reset = nodemgr_host_reset,
1774 .remove_host = nodemgr_remove_host,
1777 int init_ieee1394_nodemgr(void)
1781 ret = class_register(&nodemgr_ne_class);
1785 ret = class_register(&nodemgr_ud_class);
1787 class_unregister(&nodemgr_ne_class);
1791 hpsb_register_highlevel(&nodemgr_highlevel);
1796 void cleanup_ieee1394_nodemgr(void)
1798 hpsb_unregister_highlevel(&nodemgr_highlevel);
1800 class_unregister(&nodemgr_ud_class);
1801 class_unregister(&nodemgr_ne_class);