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/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/mutex.h>
20 #include <linux/freezer.h>
21 #include <linux/semaphore.h>
22 #include <asm/atomic.h>
25 #include "highlevel.h"
28 #include "ieee1394_core.h"
29 #include "ieee1394_hotplug.h"
30 #include "ieee1394_types.h"
31 #include "ieee1394_transactions.h"
34 static int ignore_drivers;
35 module_param(ignore_drivers, int, S_IRUGO | S_IWUSR);
36 MODULE_PARM_DESC(ignore_drivers, "Disable automatic probing for drivers.");
38 struct nodemgr_csr_info {
39 struct hpsb_host *host;
41 unsigned int generation;
42 unsigned int speed_unverified:1;
47 * Correct the speed map entry. This is necessary
48 * - for nodes with link speed < phy speed,
49 * - for 1394b nodes with negotiated phy port speed < IEEE1394_SPEED_MAX.
50 * A possible speed is determined by trial and error, using quadlet reads.
52 static int nodemgr_check_speed(struct nodemgr_csr_info *ci, u64 addr,
56 u8 i, *speed, old_speed, good_speed;
59 speed = &(ci->host->speed[NODEID_TO_NODE(ci->nodeid)]);
61 good_speed = IEEE1394_SPEED_MAX + 1;
63 /* Try every speed from S100 to old_speed.
64 * If we did it the other way around, a too low speed could be caught
65 * if the retry succeeded for some other reason, e.g. because the link
66 * just finished its initialization. */
67 for (i = IEEE1394_SPEED_100; i <= old_speed; i++) {
69 error = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
70 &q, sizeof(quadlet_t));
76 if (good_speed <= IEEE1394_SPEED_MAX) {
77 HPSB_DEBUG("Speed probe of node " NODE_BUS_FMT " yields %s",
78 NODE_BUS_ARGS(ci->host, ci->nodeid),
79 hpsb_speedto_str[good_speed]);
81 ci->speed_unverified = 0;
88 static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
89 void *buffer, void *__ci)
91 struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
95 error = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
98 ci->speed_unverified = 0;
101 /* Give up after 3rd failure. */
105 /* The ieee1394_core guessed the node's speed capability from
106 * the self ID. Check whether a lower speed works. */
107 if (ci->speed_unverified && length == sizeof(quadlet_t)) {
108 error = nodemgr_check_speed(ci, addr, buffer);
112 if (msleep_interruptible(334))
118 static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
120 return (be32_to_cpu(bus_info_data[2]) >> 8) & 0x3;
123 static struct csr1212_bus_ops nodemgr_csr_ops = {
124 .bus_read = nodemgr_bus_read,
125 .get_max_rom = nodemgr_get_max_rom
130 * Basically what we do here is start off retrieving the bus_info block.
131 * From there will fill in some info about the node, verify it is of IEEE
132 * 1394 type, and that the crc checks out ok. After that we start off with
133 * the root directory, and subdirectories. To do this, we retrieve the
134 * quadlet header for a directory, find out the length, and retrieve the
135 * complete directory entry (be it a leaf or a directory). We then process
136 * it and add the info to our structure for that particular node.
138 * We verify CRC's along the way for each directory/block/leaf. The entire
139 * node structure is generic, and simply stores the information in a way
140 * that's easy to parse by the protocol interface.
144 * The nodemgr relies heavily on the Driver Model for device callbacks and
145 * driver/device mappings. The old nodemgr used to handle all this itself,
146 * but now we are much simpler because of the LDM.
150 struct hpsb_host *host;
151 struct list_head list;
152 struct task_struct *thread;
155 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
156 static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env);
158 struct bus_type ieee1394_bus_type = {
160 .match = nodemgr_bus_match,
163 static void host_cls_release(struct device *dev)
165 put_device(&container_of((dev), struct hpsb_host, host_dev)->device);
168 struct class hpsb_host_class = {
169 .name = "ieee1394_host",
170 .dev_release = host_cls_release,
173 static void ne_cls_release(struct device *dev)
175 put_device(&container_of((dev), struct node_entry, node_dev)->device);
178 static struct class nodemgr_ne_class = {
179 .name = "ieee1394_node",
180 .dev_release = ne_cls_release,
183 static void ud_cls_release(struct device *dev)
185 put_device(&container_of((dev), struct unit_directory, unit_dev)->device);
188 /* The name here is only so that unit directory hotplug works with old
189 * style hotplug, which only ever did unit directories anyway.
191 static struct class nodemgr_ud_class = {
193 .dev_release = ud_cls_release,
194 .dev_uevent = nodemgr_uevent,
197 static struct hpsb_highlevel nodemgr_highlevel;
200 static void nodemgr_release_ud(struct device *dev)
202 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
204 if (ud->vendor_name_kv)
205 csr1212_release_keyval(ud->vendor_name_kv);
206 if (ud->model_name_kv)
207 csr1212_release_keyval(ud->model_name_kv);
212 static void nodemgr_release_ne(struct device *dev)
214 struct node_entry *ne = container_of(dev, struct node_entry, device);
216 if (ne->vendor_name_kv)
217 csr1212_release_keyval(ne->vendor_name_kv);
223 static void nodemgr_release_host(struct device *dev)
225 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
227 csr1212_destroy_csr(host->csr.rom);
232 static int nodemgr_ud_platform_data;
234 static struct device nodemgr_dev_template_ud = {
235 .bus = &ieee1394_bus_type,
236 .release = nodemgr_release_ud,
237 .platform_data = &nodemgr_ud_platform_data,
240 static struct device nodemgr_dev_template_ne = {
241 .bus = &ieee1394_bus_type,
242 .release = nodemgr_release_ne,
245 /* This dummy driver prevents the host devices from being scanned. We have no
246 * useful drivers for them yet, and there would be a deadlock possible if the
247 * driver core scans the host device while the host's low-level driver (i.e.
248 * the host's parent device) is being removed. */
249 static struct device_driver nodemgr_mid_layer_driver = {
250 .bus = &ieee1394_bus_type,
252 .owner = THIS_MODULE,
255 struct device nodemgr_dev_template_host = {
256 .bus = &ieee1394_bus_type,
257 .release = nodemgr_release_host,
261 #define fw_attr(class, class_type, field, type, format_string) \
262 static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
265 class = container_of(dev, class_type, device); \
266 return sprintf(buf, format_string, (type)class->field); \
268 static struct device_attribute dev_attr_##class##_##field = { \
269 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
270 .show = fw_show_##class##_##field, \
273 #define fw_attr_td(class, class_type, td_kv) \
274 static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
277 class_type *class = container_of(dev, class_type, device); \
278 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
280 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
282 while (buf[len - 1] == '\0') \
288 static struct device_attribute dev_attr_##class##_##td_kv = { \
289 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
290 .show = fw_show_##class##_##td_kv, \
294 #define fw_drv_attr(field, type, format_string) \
295 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
297 struct hpsb_protocol_driver *driver; \
298 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
299 return sprintf(buf, format_string, (type)driver->field);\
301 static struct driver_attribute driver_attr_drv_##field = { \
302 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
303 .show = fw_drv_show_##field, \
307 static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
309 struct node_entry *ne = container_of(dev, struct node_entry, device);
311 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
312 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
314 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
315 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
318 ne->busopt.cyc_clk_acc);
320 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
323 #ifdef HPSB_DEBUG_TLABELS
324 static ssize_t fw_show_ne_tlabels_free(struct device *dev,
325 struct device_attribute *attr, char *buf)
327 struct node_entry *ne = container_of(dev, struct node_entry, device);
329 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
332 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
333 tf = 64 - bitmap_weight(tp, 64);
334 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
336 return sprintf(buf, "%d\n", tf);
338 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
341 static ssize_t fw_show_ne_tlabels_mask(struct device *dev,
342 struct device_attribute *attr, char *buf)
344 struct node_entry *ne = container_of(dev, struct node_entry, device);
346 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
349 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
350 #if (BITS_PER_LONG <= 32)
351 tm = ((u64)tp[0] << 32) + tp[1];
355 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
357 return sprintf(buf, "0x%016llx\n", (unsigned long long)tm);
359 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
360 #endif /* HPSB_DEBUG_TLABELS */
363 static ssize_t fw_set_ignore_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
365 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
366 int state = simple_strtoul(buf, NULL, 10);
369 ud->ignore_driver = 1;
370 device_release_driver(dev);
371 } else if (state == 0)
372 ud->ignore_driver = 0;
376 static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
378 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
380 return sprintf(buf, "%d\n", ud->ignore_driver);
382 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
385 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf,
390 if (simple_strtoul(buf, NULL, 10) == 1)
391 error = bus_rescan_devices(&ieee1394_bus_type);
392 return error ? error : count;
394 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
396 return sprintf(buf, "You can force a rescan of the bus for "
397 "drivers by writing a 1 to this file\n");
399 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
402 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
404 int state = simple_strtoul(buf, NULL, 10);
413 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
415 return sprintf(buf, "%d\n", ignore_drivers);
417 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
420 struct bus_attribute *const fw_bus_attrs[] = {
422 &bus_attr_ignore_drivers,
427 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
428 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
430 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
431 fw_attr_td(ne, struct node_entry, vendor_name_kv)
433 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
434 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
435 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
437 static struct device_attribute *const fw_ne_attrs[] = {
439 &dev_attr_ne_guid_vendor_id,
440 &dev_attr_ne_capabilities,
441 &dev_attr_ne_vendor_id,
443 &dev_attr_bus_options,
444 #ifdef HPSB_DEBUG_TLABELS
445 &dev_attr_tlabels_free,
446 &dev_attr_tlabels_mask,
452 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
453 fw_attr(ud, struct unit_directory, length, int, "%d\n")
454 /* These are all dependent on the value being provided */
455 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
456 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
457 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
458 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
459 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
460 fw_attr_td(ud, struct unit_directory, model_name_kv)
462 static struct device_attribute *const fw_ud_attrs[] = {
463 &dev_attr_ud_address,
465 &dev_attr_ignore_driver,
469 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
470 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
471 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
472 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
473 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
474 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
475 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
476 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
478 static struct device_attribute *const fw_host_attrs[] = {
479 &dev_attr_host_node_count,
480 &dev_attr_host_selfid_count,
481 &dev_attr_host_nodes_active,
482 &dev_attr_host_in_bus_reset,
483 &dev_attr_host_is_root,
484 &dev_attr_host_is_cycmst,
485 &dev_attr_host_is_irm,
486 &dev_attr_host_is_busmgr,
490 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
492 struct hpsb_protocol_driver *driver;
493 struct ieee1394_device_id *id;
497 driver = container_of(drv, struct hpsb_protocol_driver, driver);
498 id = driver->id_table;
502 for (; id->match_flags != 0; id++) {
505 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
506 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
507 scratch = buf + length;
511 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
512 length += sprintf(scratch, "%smodel_id=0x%06x",
513 need_coma++ ? "," : "",
515 scratch = buf + length;
518 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
519 length += sprintf(scratch, "%sspecifier_id=0x%06x",
520 need_coma++ ? "," : "",
522 scratch = buf + length;
525 if (id->match_flags & IEEE1394_MATCH_VERSION) {
526 length += sprintf(scratch, "%sversion=0x%06x",
527 need_coma++ ? "," : "",
529 scratch = buf + length;
540 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
543 fw_drv_attr(name, const char *, "%s\n")
545 static struct driver_attribute *const fw_drv_attrs[] = {
546 &driver_attr_drv_name,
547 &driver_attr_device_ids,
551 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
553 struct device_driver *drv = &driver->driver;
556 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
557 if (driver_create_file(drv, fw_drv_attrs[i]))
561 HPSB_ERR("Failed to add sysfs attribute");
565 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
567 struct device_driver *drv = &driver->driver;
570 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
571 driver_remove_file(drv, fw_drv_attrs[i]);
575 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
577 struct device *dev = &ne->device;
580 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
581 if (device_create_file(dev, fw_ne_attrs[i]))
585 HPSB_ERR("Failed to add sysfs attribute");
589 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
591 struct device *dev = &host->device;
594 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
595 if (device_create_file(dev, fw_host_attrs[i]))
599 HPSB_ERR("Failed to add sysfs attribute");
603 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
606 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
608 struct device *dev = &host->device;
609 struct node_entry *ne;
611 sysfs_remove_link(&dev->kobj, "irm_id");
612 sysfs_remove_link(&dev->kobj, "busmgr_id");
613 sysfs_remove_link(&dev->kobj, "host_id");
615 if ((ne = find_entry_by_nodeid(host, host->irm_id)) &&
616 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id"))
618 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)) &&
619 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id"))
621 if ((ne = find_entry_by_nodeid(host, host->node_id)) &&
622 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id"))
626 HPSB_ERR("Failed to update sysfs attributes for host %d", host->id);
629 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
631 struct device *dev = &ud->device;
634 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
635 if (device_create_file(dev, fw_ud_attrs[i]))
637 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
638 if (device_create_file(dev, &dev_attr_ud_specifier_id))
640 if (ud->flags & UNIT_DIRECTORY_VERSION)
641 if (device_create_file(dev, &dev_attr_ud_version))
643 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
644 if (device_create_file(dev, &dev_attr_ud_vendor_id))
646 if (ud->vendor_name_kv &&
647 device_create_file(dev, &dev_attr_ud_vendor_name_kv))
650 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
651 if (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 HPSB_ERR("Failed to add sysfs attribute");
663 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
665 struct hpsb_protocol_driver *driver;
666 struct unit_directory *ud;
667 struct ieee1394_device_id *id;
669 /* We only match unit directories */
670 if (dev->platform_data != &nodemgr_ud_platform_data)
673 ud = container_of(dev, struct unit_directory, device);
674 if (ud->ne->in_limbo || ud->ignore_driver)
677 /* We only match drivers of type hpsb_protocol_driver */
678 if (drv == &nodemgr_mid_layer_driver)
681 driver = container_of(drv, struct hpsb_protocol_driver, driver);
682 id = driver->id_table;
686 for (; id->match_flags != 0; id++) {
687 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
688 id->vendor_id != ud->vendor_id)
691 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
692 id->model_id != ud->model_id)
695 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
696 id->specifier_id != ud->specifier_id)
699 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
700 id->version != ud->version)
710 static DEFINE_MUTEX(nodemgr_serialize_remove_uds);
712 static int match_ne(struct device *dev, void *data)
714 struct unit_directory *ud;
715 struct node_entry *ne = data;
717 ud = container_of(dev, struct unit_directory, unit_dev);
721 static void nodemgr_remove_uds(struct node_entry *ne)
724 struct unit_directory *ud;
726 /* Use class_find device to iterate the devices. Since this code
727 * may be called from other contexts besides the knodemgrds,
728 * protect it by nodemgr_serialize_remove_uds.
730 mutex_lock(&nodemgr_serialize_remove_uds);
732 dev = class_find_device(&nodemgr_ud_class, NULL, ne, match_ne);
735 ud = container_of(dev, struct unit_directory, unit_dev);
737 device_unregister(&ud->unit_dev);
738 device_unregister(&ud->device);
740 mutex_unlock(&nodemgr_serialize_remove_uds);
744 static void nodemgr_remove_ne(struct node_entry *ne)
748 dev = get_device(&ne->device);
752 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
753 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
754 nodemgr_remove_uds(ne);
756 device_unregister(&ne->node_dev);
757 device_unregister(dev);
762 static int remove_host_dev(struct device *dev, void *data)
764 if (dev->bus == &ieee1394_bus_type)
765 nodemgr_remove_ne(container_of(dev, struct node_entry,
770 static void nodemgr_remove_host_dev(struct device *dev)
772 device_for_each_child(dev, NULL, remove_host_dev);
773 sysfs_remove_link(&dev->kobj, "irm_id");
774 sysfs_remove_link(&dev->kobj, "busmgr_id");
775 sysfs_remove_link(&dev->kobj, "host_id");
779 static void nodemgr_update_bus_options(struct node_entry *ne)
781 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
782 static const u16 mr[] = { 4, 64, 1024, 0};
784 quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
786 ne->busopt.irmc = (busoptions >> 31) & 1;
787 ne->busopt.cmc = (busoptions >> 30) & 1;
788 ne->busopt.isc = (busoptions >> 29) & 1;
789 ne->busopt.bmc = (busoptions >> 28) & 1;
790 ne->busopt.pmc = (busoptions >> 27) & 1;
791 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
792 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
793 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
794 ne->busopt.generation = (busoptions >> 4) & 0xf;
795 ne->busopt.lnkspd = busoptions & 0x7;
797 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
798 "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
799 busoptions, ne->busopt.irmc, ne->busopt.cmc,
800 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
801 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
802 mr[ne->busopt.max_rom],
803 ne->busopt.generation, ne->busopt.lnkspd);
807 static struct node_entry *nodemgr_create_node(octlet_t guid,
808 struct csr1212_csr *csr, struct hpsb_host *host,
809 nodeid_t nodeid, unsigned int generation)
811 struct node_entry *ne;
813 ne = kzalloc(sizeof(*ne), GFP_KERNEL);
819 ne->generation = generation;
820 ne->needs_probe = true;
823 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
826 memcpy(&ne->device, &nodemgr_dev_template_ne,
828 ne->device.parent = &host->device;
829 dev_set_name(&ne->device, "%016Lx", (unsigned long long)(ne->guid));
831 ne->node_dev.parent = &ne->device;
832 ne->node_dev.class = &nodemgr_ne_class;
833 dev_set_name(&ne->node_dev, "%016Lx", (unsigned long long)(ne->guid));
835 if (device_register(&ne->device))
837 if (device_register(&ne->node_dev))
838 goto fail_classdevreg;
839 get_device(&ne->device);
841 nodemgr_create_ne_dev_files(ne);
843 nodemgr_update_bus_options(ne);
845 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
846 (host->node_id == nodeid) ? "Host" : "Node",
847 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
852 device_unregister(&ne->device);
856 HPSB_ERR("Failed to create node ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
857 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
862 static int match_ne_guid(struct device *dev, void *data)
864 struct node_entry *ne;
867 ne = container_of(dev, struct node_entry, node_dev);
868 return ne->guid == *guid;
871 static struct node_entry *find_entry_by_guid(u64 guid)
874 struct node_entry *ne;
876 dev = class_find_device(&nodemgr_ne_class, NULL, &guid, match_ne_guid);
879 ne = container_of(dev, struct node_entry, node_dev);
885 struct match_nodeid_parameter {
886 struct hpsb_host *host;
890 static int match_ne_nodeid(struct device *dev, void *data)
893 struct node_entry *ne;
894 struct match_nodeid_parameter *p = data;
898 ne = container_of(dev, struct node_entry, node_dev);
899 if (ne->host == p->host && ne->nodeid == p->nodeid)
905 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
909 struct node_entry *ne;
910 struct match_nodeid_parameter p;
915 dev = class_find_device(&nodemgr_ne_class, NULL, &p, match_ne_nodeid);
918 ne = container_of(dev, struct node_entry, node_dev);
925 static void nodemgr_register_device(struct node_entry *ne,
926 struct unit_directory *ud, struct device *parent)
928 memcpy(&ud->device, &nodemgr_dev_template_ud,
931 ud->device.parent = parent;
933 dev_set_name(&ud->device, "%s-%u", dev_name(&ne->device), ud->id);
935 ud->unit_dev.parent = &ud->device;
936 ud->unit_dev.class = &nodemgr_ud_class;
937 dev_set_name(&ud->unit_dev, "%s-%u", dev_name(&ne->device), ud->id);
939 if (device_register(&ud->device))
941 if (device_register(&ud->unit_dev))
942 goto fail_classdevreg;
943 get_device(&ud->device);
945 nodemgr_create_ud_dev_files(ud);
950 device_unregister(&ud->device);
952 HPSB_ERR("Failed to create unit %s", dev_name(&ud->device));
956 /* This implementation currently only scans the config rom and its
957 * immediate unit directories looking for software_id and
958 * software_version entries, in order to get driver autoloading working. */
959 static struct unit_directory *nodemgr_process_unit_directory
960 (struct node_entry *ne, struct csr1212_keyval *ud_kv,
961 unsigned int *id, struct unit_directory *parent)
963 struct unit_directory *ud;
964 struct unit_directory *ud_child = NULL;
965 struct csr1212_dentry *dentry;
966 struct csr1212_keyval *kv;
969 ud = kzalloc(sizeof(*ud), GFP_KERNEL);
971 goto unit_directory_error;
974 ud->ignore_driver = ignore_drivers;
975 ud->address = ud_kv->offset + CSR1212_REGISTER_SPACE_BASE;
976 ud->directory_id = ud->address & 0xffffff;
980 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
981 switch (kv->key.id) {
982 case CSR1212_KV_ID_VENDOR:
983 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
984 ud->vendor_id = kv->value.immediate;
985 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
989 case CSR1212_KV_ID_MODEL:
990 ud->model_id = kv->value.immediate;
991 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
994 case CSR1212_KV_ID_SPECIFIER_ID:
995 ud->specifier_id = kv->value.immediate;
996 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
999 case CSR1212_KV_ID_VERSION:
1000 ud->version = kv->value.immediate;
1001 ud->flags |= UNIT_DIRECTORY_VERSION;
1004 case CSR1212_KV_ID_DESCRIPTOR:
1005 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1006 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1007 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1008 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1009 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1010 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1011 switch (last_key_id) {
1012 case CSR1212_KV_ID_VENDOR:
1013 csr1212_keep_keyval(kv);
1014 ud->vendor_name_kv = kv;
1017 case CSR1212_KV_ID_MODEL:
1018 csr1212_keep_keyval(kv);
1019 ud->model_name_kv = kv;
1023 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
1026 case CSR1212_KV_ID_DEPENDENT_INFO:
1027 /* Logical Unit Number */
1028 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1029 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
1030 ud_child = kmemdup(ud, sizeof(*ud_child), GFP_KERNEL);
1032 goto unit_directory_error;
1033 nodemgr_register_device(ne, ud_child, &ne->device);
1038 ud->lun = kv->value.immediate;
1039 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
1041 /* Logical Unit Directory */
1042 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
1043 /* This should really be done in SBP2 as this is
1044 * doing SBP2 specific parsing.
1047 /* first register the parent unit */
1048 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
1049 if (ud->device.bus != &ieee1394_bus_type)
1050 nodemgr_register_device(ne, ud, &ne->device);
1052 /* process the child unit */
1053 ud_child = nodemgr_process_unit_directory(ne, kv, id, ud);
1055 if (ud_child == NULL)
1058 /* inherit unspecified values, the driver core picks it up */
1059 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
1060 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
1062 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
1063 ud_child->model_id = ud->model_id;
1065 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
1066 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
1068 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1069 ud_child->specifier_id = ud->specifier_id;
1071 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
1072 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
1074 ud_child->flags |= UNIT_DIRECTORY_VERSION;
1075 ud_child->version = ud->version;
1078 /* register the child unit */
1079 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
1080 nodemgr_register_device(ne, ud_child, &ud->device);
1085 case CSR1212_KV_ID_DIRECTORY_ID:
1086 ud->directory_id = kv->value.immediate;
1092 last_key_id = kv->key.id;
1095 /* do not process child units here and only if not already registered */
1096 if (!parent && ud->device.bus != &ieee1394_bus_type)
1097 nodemgr_register_device(ne, ud, &ne->device);
1101 unit_directory_error:
1107 static void nodemgr_process_root_directory(struct node_entry *ne)
1109 unsigned int ud_id = 0;
1110 struct csr1212_dentry *dentry;
1111 struct csr1212_keyval *kv, *vendor_name_kv = NULL;
1114 ne->needs_probe = false;
1116 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1117 switch (kv->key.id) {
1118 case CSR1212_KV_ID_VENDOR:
1119 ne->vendor_id = kv->value.immediate;
1122 case CSR1212_KV_ID_NODE_CAPABILITIES:
1123 ne->capabilities = kv->value.immediate;
1126 case CSR1212_KV_ID_UNIT:
1127 nodemgr_process_unit_directory(ne, kv, &ud_id, NULL);
1130 case CSR1212_KV_ID_DESCRIPTOR:
1131 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1132 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1133 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1134 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1135 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1136 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1137 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1138 csr1212_keep_keyval(kv);
1139 vendor_name_kv = kv;
1144 last_key_id = kv->key.id;
1147 if (ne->vendor_name_kv) {
1148 kv = ne->vendor_name_kv;
1149 ne->vendor_name_kv = vendor_name_kv;
1150 csr1212_release_keyval(kv);
1151 } else if (vendor_name_kv) {
1152 ne->vendor_name_kv = vendor_name_kv;
1153 if (device_create_file(&ne->device,
1154 &dev_attr_ne_vendor_name_kv) != 0)
1155 HPSB_ERR("Failed to add sysfs attribute");
1159 #ifdef CONFIG_HOTPLUG
1161 static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
1163 struct unit_directory *ud;
1165 /* ieee1394:venNmoNspNverN */
1166 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
1171 ud = container_of(dev, struct unit_directory, unit_dev);
1173 if (ud->ne->in_limbo || ud->ignore_driver)
1176 #define PUT_ENVP(fmt,val) \
1178 retval = add_uevent_var(env, fmt, val); \
1183 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1184 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1185 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1186 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1187 PUT_ENVP("VERSION=%06x", ud->version);
1188 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1193 PUT_ENVP("MODALIAS=%s", buf);
1202 static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
1207 #endif /* CONFIG_HOTPLUG */
1210 int __hpsb_register_protocol(struct hpsb_protocol_driver *drv,
1211 struct module *owner)
1215 drv->driver.bus = &ieee1394_bus_type;
1216 drv->driver.owner = owner;
1217 drv->driver.name = drv->name;
1219 /* This will cause a probe for devices */
1220 error = driver_register(&drv->driver);
1222 nodemgr_create_drv_files(drv);
1226 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1228 nodemgr_remove_drv_files(driver);
1229 /* This will subsequently disconnect all devices that our driver
1230 * is attached to. */
1231 driver_unregister(&driver->driver);
1236 * This function updates nodes that were present on the bus before the
1237 * reset and still are after the reset. The nodeid and the config rom
1238 * may have changed, and the drivers managing this device must be
1239 * informed that this device just went through a bus reset, to allow
1240 * the to take whatever actions required.
1242 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1243 nodeid_t nodeid, unsigned int generation)
1245 if (ne->nodeid != nodeid) {
1246 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1247 NODE_BUS_ARGS(ne->host, ne->nodeid),
1248 NODE_BUS_ARGS(ne->host, nodeid));
1249 ne->nodeid = nodeid;
1252 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1253 kfree(ne->csr->private);
1254 csr1212_destroy_csr(ne->csr);
1257 /* If the node's configrom generation has changed, we
1258 * unregister all the unit directories. */
1259 nodemgr_remove_uds(ne);
1261 nodemgr_update_bus_options(ne);
1263 /* Mark the node as new, so it gets re-probed */
1264 ne->needs_probe = true;
1266 /* old cache is valid, so update its generation */
1267 struct nodemgr_csr_info *ci = ne->csr->private;
1268 ci->generation = generation;
1269 /* free the partially filled now unneeded new cache */
1270 kfree(csr->private);
1271 csr1212_destroy_csr(csr);
1274 /* Mark the node current */
1275 ne->generation = generation;
1278 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1279 ne->in_limbo = false;
1281 HPSB_DEBUG("Node reactivated: "
1282 "ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1283 NODE_BUS_ARGS(ne->host, ne->nodeid),
1284 (unsigned long long)ne->guid);
1288 static void nodemgr_node_scan_one(struct hpsb_host *host,
1289 nodeid_t nodeid, int generation)
1291 struct node_entry *ne;
1293 struct csr1212_csr *csr;
1294 struct nodemgr_csr_info *ci;
1297 ci = kmalloc(sizeof(*ci), GFP_KERNEL);
1302 ci->nodeid = nodeid;
1303 ci->generation = generation;
1305 /* Prepare for speed probe which occurs when reading the ROM */
1306 speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
1307 if (*speed > host->csr.lnk_spd)
1308 *speed = host->csr.lnk_spd;
1309 ci->speed_unverified = *speed > IEEE1394_SPEED_100;
1311 /* We need to detect when the ConfigROM's generation has changed,
1312 * so we only update the node's info when it needs to be. */
1314 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1315 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1316 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1317 NODE_BUS_ARGS(host, nodeid));
1319 csr1212_destroy_csr(csr);
1324 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1325 /* This isn't a 1394 device, but we let it slide. There
1326 * was a report of a device with broken firmware which
1327 * reported '2394' instead of '1394', which is obviously a
1328 * mistake. One would hope that a non-1394 device never
1329 * gets connected to Firewire bus. If someone does, we
1330 * shouldn't be held responsible, so we'll allow it with a
1332 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1333 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1336 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1337 ne = find_entry_by_guid(guid);
1339 if (ne && ne->host != host && ne->in_limbo) {
1340 /* Must have moved this device from one host to another */
1341 nodemgr_remove_ne(ne);
1346 nodemgr_create_node(guid, csr, host, nodeid, generation);
1348 nodemgr_update_node(ne, csr, nodeid, generation);
1352 static void nodemgr_node_scan(struct hpsb_host *host, int generation)
1355 struct selfid *sid = (struct selfid *)host->topology_map;
1356 nodeid_t nodeid = LOCAL_BUS;
1358 /* Scan each node on the bus */
1359 for (count = host->selfid_count; count; count--, sid++) {
1363 if (!sid->link_active) {
1367 nodemgr_node_scan_one(host, nodeid++, generation);
1371 static void nodemgr_pause_ne(struct node_entry *ne)
1373 HPSB_DEBUG("Node paused: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1374 NODE_BUS_ARGS(ne->host, ne->nodeid),
1375 (unsigned long long)ne->guid);
1377 ne->in_limbo = true;
1378 WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo));
1381 static int update_pdrv(struct device *dev, void *data)
1383 struct unit_directory *ud;
1384 struct device_driver *drv;
1385 struct hpsb_protocol_driver *pdrv;
1386 struct node_entry *ne = data;
1389 ud = container_of(dev, struct unit_directory, unit_dev);
1391 drv = get_driver(ud->device.driver);
1394 pdrv = container_of(drv, struct hpsb_protocol_driver,
1397 down(&ud->device.sem);
1398 error = pdrv->update(ud);
1399 up(&ud->device.sem);
1402 device_release_driver(&ud->device);
1410 static void nodemgr_update_pdrv(struct node_entry *ne)
1412 class_for_each_device(&nodemgr_ud_class, NULL, ne, update_pdrv);
1415 /* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1416 * seems like an optional service but in the end it is practically mandatory
1417 * as a consequence of these clauses.
1419 * Note that we cannot do a broadcast write to all nodes at once because some
1420 * pre-1394a devices would hang. */
1421 static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1423 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1424 quadlet_t bc_remote, bc_local;
1427 if (!ne->host->is_irm || ne->generation != generation ||
1428 ne->nodeid == ne->host->node_id)
1431 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1433 /* Check if the register is implemented and 1394a compliant. */
1434 error = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1436 if (!error && bc_remote & cpu_to_be32(0x80000000) &&
1437 bc_remote != bc_local)
1438 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1442 static void nodemgr_probe_ne(struct hpsb_host *host, struct node_entry *ne,
1447 if (ne->host != host || ne->in_limbo)
1450 dev = get_device(&ne->device);
1454 nodemgr_irm_write_bc(ne, generation);
1456 /* If "needs_probe", then this is either a new or changed node we
1457 * rescan totally. If the generation matches for an existing node
1458 * (one that existed prior to the bus reset) we send update calls
1459 * down to the drivers. Otherwise, this is a dead node and we
1461 if (ne->needs_probe)
1462 nodemgr_process_root_directory(ne);
1463 else if (ne->generation == generation)
1464 nodemgr_update_pdrv(ne);
1466 nodemgr_pause_ne(ne);
1471 struct node_probe_parameter {
1472 struct hpsb_host *host;
1477 static int node_probe(struct device *dev, void *data)
1479 struct node_probe_parameter *p = data;
1480 struct node_entry *ne;
1482 if (p->generation != get_hpsb_generation(p->host))
1485 ne = container_of(dev, struct node_entry, node_dev);
1486 if (ne->needs_probe == p->probe_now)
1487 nodemgr_probe_ne(p->host, ne, p->generation);
1491 static int nodemgr_node_probe(struct hpsb_host *host, int generation)
1493 struct node_probe_parameter p;
1496 p.generation = generation;
1498 * Do some processing of the nodes we've probed. This pulls them
1499 * into the sysfs layer if needed, and can result in processing of
1500 * unit-directories, or just updating the node and it's
1503 * Run updates before probes. Usually, updates are time-critical
1504 * while probes are time-consuming.
1506 * Meanwhile, another bus reset may have happened. In this case we
1507 * skip everything here and let the next bus scan handle it.
1508 * Otherwise we may prematurely remove nodes which are still there.
1510 p.probe_now = false;
1511 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
1515 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
1518 * Now let's tell the bus to rescan our devices. This may seem
1519 * like overhead, but the driver-model core will only scan a
1520 * device for a driver when either the device is added, or when a
1521 * new driver is added. A bus reset is a good reason to rescan
1522 * devices that were there before. For example, an sbp2 device
1523 * may become available for login, if the host that held it was
1526 if (bus_rescan_devices(&ieee1394_bus_type) != 0)
1527 HPSB_DEBUG("bus_rescan_devices had an error");
1532 static int remove_nodes_in_limbo(struct device *dev, void *data)
1534 struct node_entry *ne;
1536 if (dev->bus != &ieee1394_bus_type)
1539 ne = container_of(dev, struct node_entry, device);
1541 nodemgr_remove_ne(ne);
1546 static void nodemgr_remove_nodes_in_limbo(struct hpsb_host *host)
1548 device_for_each_child(&host->device, NULL, remove_nodes_in_limbo);
1551 static int nodemgr_send_resume_packet(struct hpsb_host *host)
1553 struct hpsb_packet *packet;
1554 int error = -ENOMEM;
1556 packet = hpsb_make_phypacket(host,
1557 EXTPHYPACKET_TYPE_RESUME |
1558 NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
1560 packet->no_waiter = 1;
1561 packet->generation = get_hpsb_generation(host);
1562 error = hpsb_send_packet(packet);
1565 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1570 /* Perform a few high-level IRM responsibilities. */
1571 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1575 /* if irm_id == -1 then there is no IRM on this bus */
1576 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1579 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1580 host->csr.broadcast_channel |= 0x40000000;
1582 /* If there is no bus manager then we should set the root node's
1583 * force_root bit to promote bus stability per the 1394
1584 * spec. (8.4.2.6) */
1585 if (host->busmgr_id == 0xffff && host->node_count > 1)
1587 u16 root_node = host->node_count - 1;
1589 /* get cycle master capability flag from root node */
1590 if (host->is_cycmst ||
1591 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1592 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1593 &bc, sizeof(quadlet_t)) &&
1594 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
1595 hpsb_send_phy_config(host, root_node, -1);
1597 HPSB_DEBUG("The root node is not cycle master capable; "
1598 "selecting a new root node and resetting...");
1601 /* Oh screw it! Just leave the bus as it is */
1602 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1606 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1607 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1613 /* Some devices suspend their ports while being connected to an inactive
1614 * host adapter, i.e. if connected before the low-level driver is
1615 * loaded. They become visible either when physically unplugged and
1616 * replugged, or when receiving a resume packet. Send one once. */
1617 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1618 host->resume_packet_sent = 1;
1623 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1624 * everything we can do, otherwise issue a bus reset and try to become the IRM
1626 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1631 if (hpsb_disable_irm || host->is_irm)
1634 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1635 get_hpsb_generation(host),
1636 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1637 &bc, sizeof(quadlet_t));
1639 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1640 /* The current irm node does not have a valid BROADCAST_CHANNEL
1641 * register and we do, so reset the bus with force_root set */
1642 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1645 /* Oh screw it! Just leave the bus as it is */
1646 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1650 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1651 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1659 static int nodemgr_host_thread(void *data)
1661 struct hpsb_host *host = data;
1662 unsigned int g, generation = 0;
1663 int i, reset_cycles = 0;
1666 /* Setup our device-model entries */
1667 nodemgr_create_host_dev_files(host);
1670 /* Sleep until next bus reset */
1671 set_current_state(TASK_INTERRUPTIBLE);
1672 if (get_hpsb_generation(host) == generation &&
1673 !kthread_should_stop())
1675 __set_current_state(TASK_RUNNING);
1677 /* Thread may have been woken up to freeze or to exit */
1678 if (try_to_freeze())
1680 if (kthread_should_stop())
1683 /* Pause for 1/4 second in 1/16 second intervals,
1684 * to make sure things settle down. */
1685 g = get_hpsb_generation(host);
1686 for (i = 0; i < 4 ; i++) {
1687 msleep_interruptible(63);
1689 if (kthread_should_stop())
1692 /* Now get the generation in which the node ID's we collect
1693 * are valid. During the bus scan we will use this generation
1694 * for the read transactions, so that if another reset occurs
1695 * during the scan the transactions will fail instead of
1696 * returning bogus data. */
1697 generation = get_hpsb_generation(host);
1699 /* If we get a reset before we are done waiting, then
1700 * start the waiting over again */
1701 if (generation != g)
1702 g = generation, i = 0;
1705 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1706 !nodemgr_do_irm_duties(host, reset_cycles)) {
1712 /* Scan our nodes to get the bus options and create node
1713 * entries. This does not do the sysfs stuff, since that
1714 * would trigger uevents and such, which is a bad idea at
1716 nodemgr_node_scan(host, generation);
1718 /* This actually does the full probe, with sysfs
1720 if (!nodemgr_node_probe(host, generation))
1723 /* Update some of our sysfs symlinks */
1724 nodemgr_update_host_dev_links(host);
1726 /* Sleep 3 seconds */
1727 for (i = 3000/200; i; i--) {
1728 msleep_interruptible(200);
1730 if (kthread_should_stop())
1733 if (generation != get_hpsb_generation(host))
1736 /* Remove nodes which are gone, unless a bus reset happened */
1738 nodemgr_remove_nodes_in_limbo(host);
1741 HPSB_VERBOSE("NodeMgr: Exiting thread");
1745 struct per_host_parameter {
1747 int (*cb)(struct hpsb_host *, void *);
1750 static int per_host(struct device *dev, void *data)
1752 struct hpsb_host *host;
1753 struct per_host_parameter *p = data;
1755 host = container_of(dev, struct hpsb_host, host_dev);
1756 return p->cb(host, p->data);
1760 * nodemgr_for_each_host - call a function for each IEEE 1394 host
1761 * @data: an address to supply to the callback
1762 * @cb: function to call for each host
1764 * Iterate the hosts, calling a given function with supplied data for each host.
1765 * If the callback fails on a host, i.e. if it returns a non-zero value, the
1766 * iteration is stopped.
1768 * Return value: 0 on success, non-zero on failure (same as returned by last run
1771 int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *))
1773 struct per_host_parameter p;
1777 return class_for_each_device(&hpsb_host_class, NULL, &p, per_host);
1780 /* The following two convenience functions use a struct node_entry
1781 * for addressing a node on the bus. They are intended for use by any
1782 * process context, not just the nodemgr thread, so we need to be a
1783 * little careful when reading out the node ID and generation. The
1784 * thing that can go wrong is that we get the node ID, then a bus
1785 * reset occurs, and then we read the generation. The node ID is
1786 * possibly invalid, but the generation is current, and we end up
1787 * sending a packet to a the wrong node.
1789 * The solution is to make sure we read the generation first, so that
1790 * if a reset occurs in the process, we end up with a stale generation
1791 * and the transactions will fail instead of silently using wrong node
1796 * hpsb_node_fill_packet - fill some destination information into a packet
1797 * @ne: destination node
1798 * @packet: packet to fill in
1800 * This will fill in the given, pre-initialised hpsb_packet with the current
1801 * information from the node entry (host, node ID, bus generation number).
1803 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *packet)
1805 packet->host = ne->host;
1806 packet->generation = ne->generation;
1808 packet->node_id = ne->nodeid;
1811 int hpsb_node_write(struct node_entry *ne, u64 addr,
1812 quadlet_t *buffer, size_t length)
1814 unsigned int generation = ne->generation;
1817 return hpsb_write(ne->host, ne->nodeid, generation,
1818 addr, buffer, length);
1821 static void nodemgr_add_host(struct hpsb_host *host)
1823 struct host_info *hi;
1825 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1827 HPSB_ERR("NodeMgr: out of memory in add host");
1831 hi->thread = kthread_run(nodemgr_host_thread, host, "knodemgrd_%d",
1833 if (IS_ERR(hi->thread)) {
1834 HPSB_ERR("NodeMgr: cannot start thread for host %d", host->id);
1835 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1839 static void nodemgr_host_reset(struct hpsb_host *host)
1841 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1844 HPSB_VERBOSE("NodeMgr: Processing reset for host %d", host->id);
1845 wake_up_process(hi->thread);
1849 static void nodemgr_remove_host(struct hpsb_host *host)
1851 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1854 kthread_stop(hi->thread);
1855 nodemgr_remove_host_dev(&host->device);
1859 static struct hpsb_highlevel nodemgr_highlevel = {
1860 .name = "Node manager",
1861 .add_host = nodemgr_add_host,
1862 .host_reset = nodemgr_host_reset,
1863 .remove_host = nodemgr_remove_host,
1866 int init_ieee1394_nodemgr(void)
1870 error = class_register(&nodemgr_ne_class);
1873 error = class_register(&nodemgr_ud_class);
1876 error = driver_register(&nodemgr_mid_layer_driver);
1879 /* This driver is not used if nodemgr is off (disable_nodemgr=1). */
1880 nodemgr_dev_template_host.driver = &nodemgr_mid_layer_driver;
1882 hpsb_register_highlevel(&nodemgr_highlevel);
1886 class_unregister(&nodemgr_ud_class);
1888 class_unregister(&nodemgr_ne_class);
1893 void cleanup_ieee1394_nodemgr(void)
1895 hpsb_unregister_highlevel(&nodemgr_highlevel);
1896 driver_unregister(&nodemgr_mid_layer_driver);
1897 class_unregister(&nodemgr_ud_class);
1898 class_unregister(&nodemgr_ne_class);