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 #define OUI_FREECOM_TECHNOLOGIES_GMBH 0x0001db
120 static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
122 /* Freecom FireWire Hard Drive firmware bug */
123 if (be32_to_cpu(bus_info_data[3]) >> 8 == OUI_FREECOM_TECHNOLOGIES_GMBH)
126 return (be32_to_cpu(bus_info_data[2]) >> 8) & 0x3;
129 static struct csr1212_bus_ops nodemgr_csr_ops = {
130 .bus_read = nodemgr_bus_read,
131 .get_max_rom = nodemgr_get_max_rom
136 * Basically what we do here is start off retrieving the bus_info block.
137 * From there will fill in some info about the node, verify it is of IEEE
138 * 1394 type, and that the crc checks out ok. After that we start off with
139 * the root directory, and subdirectories. To do this, we retrieve the
140 * quadlet header for a directory, find out the length, and retrieve the
141 * complete directory entry (be it a leaf or a directory). We then process
142 * it and add the info to our structure for that particular node.
144 * We verify CRC's along the way for each directory/block/leaf. The entire
145 * node structure is generic, and simply stores the information in a way
146 * that's easy to parse by the protocol interface.
150 * The nodemgr relies heavily on the Driver Model for device callbacks and
151 * driver/device mappings. The old nodemgr used to handle all this itself,
152 * but now we are much simpler because of the LDM.
156 struct hpsb_host *host;
157 struct list_head list;
158 struct task_struct *thread;
161 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
162 static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env);
163 static void nodemgr_resume_ne(struct node_entry *ne);
164 static void nodemgr_remove_ne(struct node_entry *ne);
165 static struct node_entry *find_entry_by_guid(u64 guid);
167 struct bus_type ieee1394_bus_type = {
169 .match = nodemgr_bus_match,
172 static void host_cls_release(struct device *dev)
174 put_device(&container_of((dev), struct hpsb_host, host_dev)->device);
177 struct class hpsb_host_class = {
178 .name = "ieee1394_host",
179 .dev_release = host_cls_release,
182 static void ne_cls_release(struct device *dev)
184 put_device(&container_of((dev), struct node_entry, node_dev)->device);
187 static struct class nodemgr_ne_class = {
188 .name = "ieee1394_node",
189 .dev_release = ne_cls_release,
192 static void ud_cls_release(struct device *dev)
194 put_device(&container_of((dev), struct unit_directory, unit_dev)->device);
197 /* The name here is only so that unit directory hotplug works with old
198 * style hotplug, which only ever did unit directories anyway.
200 static struct class nodemgr_ud_class = {
202 .dev_release = ud_cls_release,
203 .dev_uevent = nodemgr_uevent,
206 static struct hpsb_highlevel nodemgr_highlevel;
209 static void nodemgr_release_ud(struct device *dev)
211 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
213 if (ud->vendor_name_kv)
214 csr1212_release_keyval(ud->vendor_name_kv);
215 if (ud->model_name_kv)
216 csr1212_release_keyval(ud->model_name_kv);
221 static void nodemgr_release_ne(struct device *dev)
223 struct node_entry *ne = container_of(dev, struct node_entry, device);
225 if (ne->vendor_name_kv)
226 csr1212_release_keyval(ne->vendor_name_kv);
232 static void nodemgr_release_host(struct device *dev)
234 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
236 csr1212_destroy_csr(host->csr.rom);
241 static int nodemgr_ud_platform_data;
243 static struct device nodemgr_dev_template_ud = {
244 .bus = &ieee1394_bus_type,
245 .release = nodemgr_release_ud,
246 .platform_data = &nodemgr_ud_platform_data,
249 static struct device nodemgr_dev_template_ne = {
250 .bus = &ieee1394_bus_type,
251 .release = nodemgr_release_ne,
254 /* This dummy driver prevents the host devices from being scanned. We have no
255 * useful drivers for them yet, and there would be a deadlock possible if the
256 * driver core scans the host device while the host's low-level driver (i.e.
257 * the host's parent device) is being removed. */
258 static struct device_driver nodemgr_mid_layer_driver = {
259 .bus = &ieee1394_bus_type,
261 .owner = THIS_MODULE,
264 struct device nodemgr_dev_template_host = {
265 .bus = &ieee1394_bus_type,
266 .release = nodemgr_release_host,
270 #define fw_attr(class, class_type, field, type, format_string) \
271 static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
274 class = container_of(dev, class_type, device); \
275 return sprintf(buf, format_string, (type)class->field); \
277 static struct device_attribute dev_attr_##class##_##field = { \
278 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
279 .show = fw_show_##class##_##field, \
282 #define fw_attr_td(class, class_type, td_kv) \
283 static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
286 class_type *class = container_of(dev, class_type, device); \
287 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
289 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
291 while (buf[len - 1] == '\0') \
297 static struct device_attribute dev_attr_##class##_##td_kv = { \
298 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
299 .show = fw_show_##class##_##td_kv, \
303 #define fw_drv_attr(field, type, format_string) \
304 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
306 struct hpsb_protocol_driver *driver; \
307 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
308 return sprintf(buf, format_string, (type)driver->field);\
310 static struct driver_attribute driver_attr_drv_##field = { \
311 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
312 .show = fw_drv_show_##field, \
316 static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
318 struct node_entry *ne = container_of(dev, struct node_entry, device);
320 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
321 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
323 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
324 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
327 ne->busopt.cyc_clk_acc);
329 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
332 #ifdef HPSB_DEBUG_TLABELS
333 static ssize_t fw_show_ne_tlabels_free(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 struct node_entry *ne = container_of(dev, struct node_entry, device);
338 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
341 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
342 tf = 64 - bitmap_weight(tp, 64);
343 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
345 return sprintf(buf, "%d\n", tf);
347 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
350 static ssize_t fw_show_ne_tlabels_mask(struct device *dev,
351 struct device_attribute *attr, char *buf)
353 struct node_entry *ne = container_of(dev, struct node_entry, device);
355 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
358 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
359 #if (BITS_PER_LONG <= 32)
360 tm = ((u64)tp[0] << 32) + tp[1];
364 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
366 return sprintf(buf, "0x%016llx\n", (unsigned long long)tm);
368 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
369 #endif /* HPSB_DEBUG_TLABELS */
372 static ssize_t fw_set_ignore_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
374 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
375 int state = simple_strtoul(buf, NULL, 10);
378 ud->ignore_driver = 1;
379 device_release_driver(dev);
380 } else if (state == 0)
381 ud->ignore_driver = 0;
385 static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
387 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
389 return sprintf(buf, "%d\n", ud->ignore_driver);
391 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
394 static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
396 struct node_entry *ne;
397 u64 guid = (u64)simple_strtoull(buf, NULL, 16);
399 ne = find_entry_by_guid(guid);
401 if (ne == NULL || !ne->in_limbo)
404 nodemgr_remove_ne(ne);
408 static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
410 return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
412 static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
415 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf,
420 if (simple_strtoul(buf, NULL, 10) == 1)
421 error = bus_rescan_devices(&ieee1394_bus_type);
422 return error ? error : count;
424 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
426 return sprintf(buf, "You can force a rescan of the bus for "
427 "drivers by writing a 1 to this file\n");
429 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
432 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
434 int state = simple_strtoul(buf, NULL, 10);
443 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
445 return sprintf(buf, "%d\n", ignore_drivers);
447 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
450 struct bus_attribute *const fw_bus_attrs[] = {
451 &bus_attr_destroy_node,
453 &bus_attr_ignore_drivers,
458 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
459 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
461 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
462 fw_attr_td(ne, struct node_entry, vendor_name_kv)
464 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
465 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
466 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
468 static struct device_attribute *const fw_ne_attrs[] = {
470 &dev_attr_ne_guid_vendor_id,
471 &dev_attr_ne_capabilities,
472 &dev_attr_ne_vendor_id,
474 &dev_attr_bus_options,
475 #ifdef HPSB_DEBUG_TLABELS
476 &dev_attr_tlabels_free,
477 &dev_attr_tlabels_mask,
483 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
484 fw_attr(ud, struct unit_directory, length, int, "%d\n")
485 /* These are all dependent on the value being provided */
486 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
487 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
488 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
489 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
490 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
491 fw_attr_td(ud, struct unit_directory, model_name_kv)
493 static struct device_attribute *const fw_ud_attrs[] = {
494 &dev_attr_ud_address,
496 &dev_attr_ignore_driver,
500 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
501 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
502 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
503 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
504 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
505 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
506 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
507 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
509 static struct device_attribute *const fw_host_attrs[] = {
510 &dev_attr_host_node_count,
511 &dev_attr_host_selfid_count,
512 &dev_attr_host_nodes_active,
513 &dev_attr_host_in_bus_reset,
514 &dev_attr_host_is_root,
515 &dev_attr_host_is_cycmst,
516 &dev_attr_host_is_irm,
517 &dev_attr_host_is_busmgr,
521 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
523 struct hpsb_protocol_driver *driver;
524 struct ieee1394_device_id *id;
528 driver = container_of(drv, struct hpsb_protocol_driver, driver);
529 id = driver->id_table;
533 for (; id->match_flags != 0; id++) {
536 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
537 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
538 scratch = buf + length;
542 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
543 length += sprintf(scratch, "%smodel_id=0x%06x",
544 need_coma++ ? "," : "",
546 scratch = buf + length;
549 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
550 length += sprintf(scratch, "%sspecifier_id=0x%06x",
551 need_coma++ ? "," : "",
553 scratch = buf + length;
556 if (id->match_flags & IEEE1394_MATCH_VERSION) {
557 length += sprintf(scratch, "%sversion=0x%06x",
558 need_coma++ ? "," : "",
560 scratch = buf + length;
571 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
574 fw_drv_attr(name, const char *, "%s\n")
576 static struct driver_attribute *const fw_drv_attrs[] = {
577 &driver_attr_drv_name,
578 &driver_attr_device_ids,
582 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
584 struct device_driver *drv = &driver->driver;
587 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
588 if (driver_create_file(drv, fw_drv_attrs[i]))
592 HPSB_ERR("Failed to add sysfs attribute");
596 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
598 struct device_driver *drv = &driver->driver;
601 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
602 driver_remove_file(drv, fw_drv_attrs[i]);
606 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
608 struct device *dev = &ne->device;
611 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
612 if (device_create_file(dev, fw_ne_attrs[i]))
616 HPSB_ERR("Failed to add sysfs attribute");
620 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
622 struct device *dev = &host->device;
625 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
626 if (device_create_file(dev, fw_host_attrs[i]))
630 HPSB_ERR("Failed to add sysfs attribute");
634 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
637 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
639 struct device *dev = &host->device;
640 struct node_entry *ne;
642 sysfs_remove_link(&dev->kobj, "irm_id");
643 sysfs_remove_link(&dev->kobj, "busmgr_id");
644 sysfs_remove_link(&dev->kobj, "host_id");
646 if ((ne = find_entry_by_nodeid(host, host->irm_id)) &&
647 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id"))
649 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)) &&
650 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id"))
652 if ((ne = find_entry_by_nodeid(host, host->node_id)) &&
653 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id"))
657 HPSB_ERR("Failed to update sysfs attributes for host %d", host->id);
660 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
662 struct device *dev = &ud->device;
665 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
666 if (device_create_file(dev, fw_ud_attrs[i]))
668 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
669 if (device_create_file(dev, &dev_attr_ud_specifier_id))
671 if (ud->flags & UNIT_DIRECTORY_VERSION)
672 if (device_create_file(dev, &dev_attr_ud_version))
674 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
675 if (device_create_file(dev, &dev_attr_ud_vendor_id))
677 if (ud->vendor_name_kv &&
678 device_create_file(dev, &dev_attr_ud_vendor_name_kv))
681 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
682 if (device_create_file(dev, &dev_attr_ud_model_id))
684 if (ud->model_name_kv &&
685 device_create_file(dev, &dev_attr_ud_model_name_kv))
690 HPSB_ERR("Failed to add sysfs attribute");
694 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
696 struct hpsb_protocol_driver *driver;
697 struct unit_directory *ud;
698 struct ieee1394_device_id *id;
700 /* We only match unit directories */
701 if (dev->platform_data != &nodemgr_ud_platform_data)
704 ud = container_of(dev, struct unit_directory, device);
705 if (ud->ne->in_limbo || ud->ignore_driver)
708 /* We only match drivers of type hpsb_protocol_driver */
709 if (drv == &nodemgr_mid_layer_driver)
712 driver = container_of(drv, struct hpsb_protocol_driver, driver);
713 id = driver->id_table;
717 for (; id->match_flags != 0; id++) {
718 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
719 id->vendor_id != ud->vendor_id)
722 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
723 id->model_id != ud->model_id)
726 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
727 id->specifier_id != ud->specifier_id)
730 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
731 id->version != ud->version)
741 static DEFINE_MUTEX(nodemgr_serialize_remove_uds);
743 static int __match_ne(struct device *dev, void *data)
745 struct unit_directory *ud;
746 struct node_entry *ne = (struct node_entry *)data;
748 ud = container_of(dev, struct unit_directory, unit_dev);
752 static void nodemgr_remove_uds(struct node_entry *ne)
755 struct unit_directory *ud;
757 /* Use class_find device to iterate the devices. Since this code
758 * may be called from other contexts besides the knodemgrds,
759 * protect it by nodemgr_serialize_remove_uds.
761 mutex_lock(&nodemgr_serialize_remove_uds);
763 dev = class_find_device(&nodemgr_ud_class, NULL, ne,
767 ud = container_of(dev, struct unit_directory, unit_dev);
769 device_unregister(&ud->unit_dev);
770 device_unregister(&ud->device);
772 mutex_unlock(&nodemgr_serialize_remove_uds);
776 static void nodemgr_remove_ne(struct node_entry *ne)
780 dev = get_device(&ne->device);
784 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
785 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
786 nodemgr_remove_uds(ne);
788 device_unregister(&ne->node_dev);
789 device_unregister(dev);
794 static int __nodemgr_remove_host_dev(struct device *dev, void *data)
796 if (dev->bus == &ieee1394_bus_type)
797 nodemgr_remove_ne(container_of(dev, struct node_entry,
802 static void nodemgr_remove_host_dev(struct device *dev)
804 WARN_ON(device_for_each_child(dev, NULL, __nodemgr_remove_host_dev));
805 sysfs_remove_link(&dev->kobj, "irm_id");
806 sysfs_remove_link(&dev->kobj, "busmgr_id");
807 sysfs_remove_link(&dev->kobj, "host_id");
811 static void nodemgr_update_bus_options(struct node_entry *ne)
813 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
814 static const u16 mr[] = { 4, 64, 1024, 0};
816 quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
818 ne->busopt.irmc = (busoptions >> 31) & 1;
819 ne->busopt.cmc = (busoptions >> 30) & 1;
820 ne->busopt.isc = (busoptions >> 29) & 1;
821 ne->busopt.bmc = (busoptions >> 28) & 1;
822 ne->busopt.pmc = (busoptions >> 27) & 1;
823 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
824 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
825 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
826 ne->busopt.generation = (busoptions >> 4) & 0xf;
827 ne->busopt.lnkspd = busoptions & 0x7;
829 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
830 "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
831 busoptions, ne->busopt.irmc, ne->busopt.cmc,
832 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
833 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
834 mr[ne->busopt.max_rom],
835 ne->busopt.generation, ne->busopt.lnkspd);
839 static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
840 struct host_info *hi, nodeid_t nodeid,
841 unsigned int generation)
843 struct hpsb_host *host = hi->host;
844 struct node_entry *ne;
846 ne = kzalloc(sizeof(*ne), GFP_KERNEL);
852 ne->generation = generation;
853 ne->needs_probe = true;
856 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
859 memcpy(&ne->device, &nodemgr_dev_template_ne,
861 ne->device.parent = &host->device;
862 snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
863 (unsigned long long)(ne->guid));
865 ne->node_dev.parent = &ne->device;
866 ne->node_dev.class = &nodemgr_ne_class;
867 snprintf(ne->node_dev.bus_id, BUS_ID_SIZE, "%016Lx",
868 (unsigned long long)(ne->guid));
870 if (device_register(&ne->device))
872 if (device_register(&ne->node_dev))
873 goto fail_classdevreg;
874 get_device(&ne->device);
876 nodemgr_create_ne_dev_files(ne);
878 nodemgr_update_bus_options(ne);
880 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
881 (host->node_id == nodeid) ? "Host" : "Node",
882 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
887 device_unregister(&ne->device);
891 HPSB_ERR("Failed to create node ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
892 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
897 static int __match_ne_guid(struct device *dev, void *data)
899 struct node_entry *ne;
900 u64 *guid = (u64 *)data;
902 ne = container_of(dev, struct node_entry, node_dev);
903 return ne->guid == *guid;
906 static struct node_entry *find_entry_by_guid(u64 guid)
909 struct node_entry *ne;
911 dev = class_find_device(&nodemgr_ne_class, NULL, &guid,
915 ne = container_of(dev, struct node_entry, node_dev);
921 struct match_nodeid_param {
922 struct hpsb_host *host;
926 static int __match_ne_nodeid(struct device *dev, void *data)
929 struct node_entry *ne;
930 struct match_nodeid_param *param = (struct match_nodeid_param *)data;
934 ne = container_of(dev, struct node_entry, node_dev);
935 if (ne->host == param->host && ne->nodeid == param->nodeid)
941 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
945 struct node_entry *ne;
946 struct match_nodeid_param param;
949 param.nodeid = nodeid;
951 dev = class_find_device(&nodemgr_ne_class, NULL, ¶m,
955 ne = container_of(dev, struct node_entry, node_dev);
962 static void nodemgr_register_device(struct node_entry *ne,
963 struct unit_directory *ud, struct device *parent)
965 memcpy(&ud->device, &nodemgr_dev_template_ud,
968 ud->device.parent = parent;
970 snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
971 ne->device.bus_id, ud->id);
973 ud->unit_dev.parent = &ud->device;
974 ud->unit_dev.class = &nodemgr_ud_class;
975 snprintf(ud->unit_dev.bus_id, BUS_ID_SIZE, "%s-%u",
976 ne->device.bus_id, ud->id);
978 if (device_register(&ud->device))
980 if (device_register(&ud->unit_dev))
981 goto fail_classdevreg;
982 get_device(&ud->device);
984 nodemgr_create_ud_dev_files(ud);
989 device_unregister(&ud->device);
991 HPSB_ERR("Failed to create unit %s", ud->device.bus_id);
995 /* This implementation currently only scans the config rom and its
996 * immediate unit directories looking for software_id and
997 * software_version entries, in order to get driver autoloading working. */
998 static struct unit_directory *nodemgr_process_unit_directory
999 (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
1000 unsigned int *id, struct unit_directory *parent)
1002 struct unit_directory *ud;
1003 struct unit_directory *ud_child = NULL;
1004 struct csr1212_dentry *dentry;
1005 struct csr1212_keyval *kv;
1008 ud = kzalloc(sizeof(*ud), GFP_KERNEL);
1010 goto unit_directory_error;
1013 ud->ignore_driver = ignore_drivers;
1014 ud->address = ud_kv->offset + CSR1212_REGISTER_SPACE_BASE;
1015 ud->directory_id = ud->address & 0xffffff;
1019 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
1020 switch (kv->key.id) {
1021 case CSR1212_KV_ID_VENDOR:
1022 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1023 ud->vendor_id = kv->value.immediate;
1024 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
1028 case CSR1212_KV_ID_MODEL:
1029 ud->model_id = kv->value.immediate;
1030 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
1033 case CSR1212_KV_ID_SPECIFIER_ID:
1034 ud->specifier_id = kv->value.immediate;
1035 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1038 case CSR1212_KV_ID_VERSION:
1039 ud->version = kv->value.immediate;
1040 ud->flags |= UNIT_DIRECTORY_VERSION;
1043 case CSR1212_KV_ID_DESCRIPTOR:
1044 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1045 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1046 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1047 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1048 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1049 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1050 switch (last_key_id) {
1051 case CSR1212_KV_ID_VENDOR:
1052 csr1212_keep_keyval(kv);
1053 ud->vendor_name_kv = kv;
1056 case CSR1212_KV_ID_MODEL:
1057 csr1212_keep_keyval(kv);
1058 ud->model_name_kv = kv;
1062 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
1065 case CSR1212_KV_ID_DEPENDENT_INFO:
1066 /* Logical Unit Number */
1067 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1068 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
1069 ud_child = kmemdup(ud, sizeof(*ud_child), GFP_KERNEL);
1071 goto unit_directory_error;
1072 nodemgr_register_device(ne, ud_child, &ne->device);
1077 ud->lun = kv->value.immediate;
1078 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
1080 /* Logical Unit Directory */
1081 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
1082 /* This should really be done in SBP2 as this is
1083 * doing SBP2 specific parsing.
1086 /* first register the parent unit */
1087 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
1088 if (ud->device.bus != &ieee1394_bus_type)
1089 nodemgr_register_device(ne, ud, &ne->device);
1091 /* process the child unit */
1092 ud_child = nodemgr_process_unit_directory(hi, ne, kv, id, ud);
1094 if (ud_child == NULL)
1097 /* inherit unspecified values, the driver core picks it up */
1098 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
1099 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
1101 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
1102 ud_child->model_id = ud->model_id;
1104 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
1105 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
1107 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1108 ud_child->specifier_id = ud->specifier_id;
1110 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
1111 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
1113 ud_child->flags |= UNIT_DIRECTORY_VERSION;
1114 ud_child->version = ud->version;
1117 /* register the child unit */
1118 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
1119 nodemgr_register_device(ne, ud_child, &ud->device);
1124 case CSR1212_KV_ID_DIRECTORY_ID:
1125 ud->directory_id = kv->value.immediate;
1131 last_key_id = kv->key.id;
1134 /* do not process child units here and only if not already registered */
1135 if (!parent && ud->device.bus != &ieee1394_bus_type)
1136 nodemgr_register_device(ne, ud, &ne->device);
1140 unit_directory_error:
1146 static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
1148 unsigned int ud_id = 0;
1149 struct csr1212_dentry *dentry;
1150 struct csr1212_keyval *kv, *vendor_name_kv = NULL;
1153 ne->needs_probe = false;
1155 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1156 switch (kv->key.id) {
1157 case CSR1212_KV_ID_VENDOR:
1158 ne->vendor_id = kv->value.immediate;
1161 case CSR1212_KV_ID_NODE_CAPABILITIES:
1162 ne->capabilities = kv->value.immediate;
1165 case CSR1212_KV_ID_UNIT:
1166 nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
1169 case CSR1212_KV_ID_DESCRIPTOR:
1170 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1171 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1172 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1173 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1174 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1175 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1176 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1177 csr1212_keep_keyval(kv);
1178 vendor_name_kv = kv;
1183 last_key_id = kv->key.id;
1186 if (ne->vendor_name_kv) {
1187 kv = ne->vendor_name_kv;
1188 ne->vendor_name_kv = vendor_name_kv;
1189 csr1212_release_keyval(kv);
1190 } else if (vendor_name_kv) {
1191 ne->vendor_name_kv = vendor_name_kv;
1192 if (device_create_file(&ne->device,
1193 &dev_attr_ne_vendor_name_kv) != 0)
1194 HPSB_ERR("Failed to add sysfs attribute");
1198 #ifdef CONFIG_HOTPLUG
1200 static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
1202 struct unit_directory *ud;
1204 /* ieee1394:venNmoNspNverN */
1205 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
1210 ud = container_of(dev, struct unit_directory, unit_dev);
1212 if (ud->ne->in_limbo || ud->ignore_driver)
1215 #define PUT_ENVP(fmt,val) \
1217 retval = add_uevent_var(env, fmt, val); \
1222 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1223 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1224 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1225 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1226 PUT_ENVP("VERSION=%06x", ud->version);
1227 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1232 PUT_ENVP("MODALIAS=%s", buf);
1241 static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
1246 #endif /* CONFIG_HOTPLUG */
1249 int __hpsb_register_protocol(struct hpsb_protocol_driver *drv,
1250 struct module *owner)
1254 drv->driver.bus = &ieee1394_bus_type;
1255 drv->driver.owner = owner;
1256 drv->driver.name = drv->name;
1258 /* This will cause a probe for devices */
1259 error = driver_register(&drv->driver);
1261 nodemgr_create_drv_files(drv);
1265 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1267 nodemgr_remove_drv_files(driver);
1268 /* This will subsequently disconnect all devices that our driver
1269 * is attached to. */
1270 driver_unregister(&driver->driver);
1275 * This function updates nodes that were present on the bus before the
1276 * reset and still are after the reset. The nodeid and the config rom
1277 * may have changed, and the drivers managing this device must be
1278 * informed that this device just went through a bus reset, to allow
1279 * the to take whatever actions required.
1281 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1282 struct host_info *hi, nodeid_t nodeid,
1283 unsigned int generation)
1285 if (ne->nodeid != nodeid) {
1286 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1287 NODE_BUS_ARGS(ne->host, ne->nodeid),
1288 NODE_BUS_ARGS(ne->host, nodeid));
1289 ne->nodeid = nodeid;
1292 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1293 kfree(ne->csr->private);
1294 csr1212_destroy_csr(ne->csr);
1297 /* If the node's configrom generation has changed, we
1298 * unregister all the unit directories. */
1299 nodemgr_remove_uds(ne);
1301 nodemgr_update_bus_options(ne);
1303 /* Mark the node as new, so it gets re-probed */
1304 ne->needs_probe = true;
1306 /* old cache is valid, so update its generation */
1307 struct nodemgr_csr_info *ci = ne->csr->private;
1308 ci->generation = generation;
1309 /* free the partially filled now unneeded new cache */
1310 kfree(csr->private);
1311 csr1212_destroy_csr(csr);
1315 nodemgr_resume_ne(ne);
1317 /* Mark the node current */
1318 ne->generation = generation;
1323 static void nodemgr_node_scan_one(struct host_info *hi,
1324 nodeid_t nodeid, int generation)
1326 struct hpsb_host *host = hi->host;
1327 struct node_entry *ne;
1329 struct csr1212_csr *csr;
1330 struct nodemgr_csr_info *ci;
1333 ci = kmalloc(sizeof(*ci), GFP_KERNEL);
1338 ci->nodeid = nodeid;
1339 ci->generation = generation;
1341 /* Prepare for speed probe which occurs when reading the ROM */
1342 speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
1343 if (*speed > host->csr.lnk_spd)
1344 *speed = host->csr.lnk_spd;
1345 ci->speed_unverified = *speed > IEEE1394_SPEED_100;
1347 /* We need to detect when the ConfigROM's generation has changed,
1348 * so we only update the node's info when it needs to be. */
1350 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1351 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1352 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1353 NODE_BUS_ARGS(host, nodeid));
1355 csr1212_destroy_csr(csr);
1360 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1361 /* This isn't a 1394 device, but we let it slide. There
1362 * was a report of a device with broken firmware which
1363 * reported '2394' instead of '1394', which is obviously a
1364 * mistake. One would hope that a non-1394 device never
1365 * gets connected to Firewire bus. If someone does, we
1366 * shouldn't be held responsible, so we'll allow it with a
1368 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1369 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1372 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1373 ne = find_entry_by_guid(guid);
1375 if (ne && ne->host != host && ne->in_limbo) {
1376 /* Must have moved this device from one host to another */
1377 nodemgr_remove_ne(ne);
1382 nodemgr_create_node(guid, csr, hi, nodeid, generation);
1384 nodemgr_update_node(ne, csr, hi, nodeid, generation);
1388 static void nodemgr_node_scan(struct host_info *hi, int generation)
1391 struct hpsb_host *host = hi->host;
1392 struct selfid *sid = (struct selfid *)host->topology_map;
1393 nodeid_t nodeid = LOCAL_BUS;
1395 /* Scan each node on the bus */
1396 for (count = host->selfid_count; count; count--, sid++) {
1400 if (!sid->link_active) {
1404 nodemgr_node_scan_one(hi, nodeid++, generation);
1408 static int __nodemgr_driver_suspend(struct device *dev, void *data)
1410 struct unit_directory *ud;
1411 struct device_driver *drv;
1412 struct node_entry *ne = (struct node_entry *)data;
1415 ud = container_of(dev, struct unit_directory, unit_dev);
1417 drv = get_driver(ud->device.driver);
1419 error = 1; /* release if suspend is not implemented */
1421 down(&ud->device.sem);
1422 error = drv->suspend(&ud->device, PMSG_SUSPEND);
1423 up(&ud->device.sem);
1426 device_release_driver(&ud->device);
1434 static int __nodemgr_driver_resume(struct device *dev, void *data)
1436 struct unit_directory *ud;
1437 struct device_driver *drv;
1438 struct node_entry *ne = (struct node_entry *)data;
1440 ud = container_of(dev, struct unit_directory, unit_dev);
1442 drv = get_driver(ud->device.driver);
1445 down(&ud->device.sem);
1446 drv->resume(&ud->device);
1447 up(&ud->device.sem);
1456 static void nodemgr_suspend_ne(struct node_entry *ne)
1458 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1459 NODE_BUS_ARGS(ne->host, ne->nodeid),
1460 (unsigned long long)ne->guid);
1463 WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo));
1465 class_for_each_device(&nodemgr_ud_class, NULL, ne,
1466 __nodemgr_driver_suspend);
1470 static void nodemgr_resume_ne(struct node_entry *ne)
1473 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1475 class_for_each_device(&nodemgr_ud_class, NULL, ne,
1476 __nodemgr_driver_resume);
1477 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1478 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1481 static int __nodemgr_update_pdrv(struct device *dev, void *data)
1483 struct unit_directory *ud;
1484 struct device_driver *drv;
1485 struct hpsb_protocol_driver *pdrv;
1486 struct node_entry *ne = (struct node_entry *)data;
1489 ud = container_of(dev, struct unit_directory, unit_dev);
1491 drv = get_driver(ud->device.driver);
1494 pdrv = container_of(drv, struct hpsb_protocol_driver,
1497 down(&ud->device.sem);
1498 error = pdrv->update(ud);
1499 up(&ud->device.sem);
1502 device_release_driver(&ud->device);
1510 static void nodemgr_update_pdrv(struct node_entry *ne)
1512 class_for_each_device(&nodemgr_ud_class, NULL, ne,
1513 __nodemgr_update_pdrv);
1517 /* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1518 * seems like an optional service but in the end it is practically mandatory
1519 * as a consequence of these clauses.
1521 * Note that we cannot do a broadcast write to all nodes at once because some
1522 * pre-1394a devices would hang. */
1523 static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1525 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1526 quadlet_t bc_remote, bc_local;
1529 if (!ne->host->is_irm || ne->generation != generation ||
1530 ne->nodeid == ne->host->node_id)
1533 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1535 /* Check if the register is implemented and 1394a compliant. */
1536 error = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1538 if (!error && bc_remote & cpu_to_be32(0x80000000) &&
1539 bc_remote != bc_local)
1540 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1544 static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
1548 if (ne->host != hi->host || ne->in_limbo)
1551 dev = get_device(&ne->device);
1555 nodemgr_irm_write_bc(ne, generation);
1557 /* If "needs_probe", then this is either a new or changed node we
1558 * rescan totally. If the generation matches for an existing node
1559 * (one that existed prior to the bus reset) we send update calls
1560 * down to the drivers. Otherwise, this is a dead node and we
1562 if (ne->needs_probe)
1563 nodemgr_process_root_directory(hi, ne);
1564 else if (ne->generation == generation)
1565 nodemgr_update_pdrv(ne);
1567 nodemgr_suspend_ne(ne);
1572 struct probe_param {
1573 struct host_info *hi;
1578 static int node_probe(struct device *dev, void *data)
1580 struct probe_param *p = data;
1581 struct node_entry *ne;
1583 if (p->generation != get_hpsb_generation(p->hi->host))
1586 ne = container_of(dev, struct node_entry, node_dev);
1587 if (ne->needs_probe == p->probe_now)
1588 nodemgr_probe_ne(p->hi, ne, p->generation);
1592 static void nodemgr_node_probe(struct host_info *hi, int generation)
1594 struct probe_param p;
1597 p.generation = generation;
1599 * Do some processing of the nodes we've probed. This pulls them
1600 * into the sysfs layer if needed, and can result in processing of
1601 * unit-directories, or just updating the node and it's
1604 * Run updates before probes. Usually, updates are time-critical
1605 * while probes are time-consuming.
1607 * Meanwhile, another bus reset may have happened. In this case we
1608 * skip everything here and let the next bus scan handle it.
1609 * Otherwise we may prematurely remove nodes which are still there.
1611 p.probe_now = false;
1612 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
1616 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
1619 * Now let's tell the bus to rescan our devices. This may seem
1620 * like overhead, but the driver-model core will only scan a
1621 * device for a driver when either the device is added, or when a
1622 * new driver is added. A bus reset is a good reason to rescan
1623 * devices that were there before. For example, an sbp2 device
1624 * may become available for login, if the host that held it was
1627 if (bus_rescan_devices(&ieee1394_bus_type) != 0)
1628 HPSB_DEBUG("bus_rescan_devices had an error");
1631 static int nodemgr_send_resume_packet(struct hpsb_host *host)
1633 struct hpsb_packet *packet;
1634 int error = -ENOMEM;
1636 packet = hpsb_make_phypacket(host,
1637 EXTPHYPACKET_TYPE_RESUME |
1638 NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
1640 packet->no_waiter = 1;
1641 packet->generation = get_hpsb_generation(host);
1642 error = hpsb_send_packet(packet);
1645 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1650 /* Perform a few high-level IRM responsibilities. */
1651 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1655 /* if irm_id == -1 then there is no IRM on this bus */
1656 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1659 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1660 host->csr.broadcast_channel |= 0x40000000;
1662 /* If there is no bus manager then we should set the root node's
1663 * force_root bit to promote bus stability per the 1394
1664 * spec. (8.4.2.6) */
1665 if (host->busmgr_id == 0xffff && host->node_count > 1)
1667 u16 root_node = host->node_count - 1;
1669 /* get cycle master capability flag from root node */
1670 if (host->is_cycmst ||
1671 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1672 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1673 &bc, sizeof(quadlet_t)) &&
1674 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
1675 hpsb_send_phy_config(host, root_node, -1);
1677 HPSB_DEBUG("The root node is not cycle master capable; "
1678 "selecting a new root node and resetting...");
1681 /* Oh screw it! Just leave the bus as it is */
1682 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1686 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1687 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1693 /* Some devices suspend their ports while being connected to an inactive
1694 * host adapter, i.e. if connected before the low-level driver is
1695 * loaded. They become visible either when physically unplugged and
1696 * replugged, or when receiving a resume packet. Send one once. */
1697 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1698 host->resume_packet_sent = 1;
1703 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1704 * everything we can do, otherwise issue a bus reset and try to become the IRM
1706 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1711 if (hpsb_disable_irm || host->is_irm)
1714 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1715 get_hpsb_generation(host),
1716 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1717 &bc, sizeof(quadlet_t));
1719 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1720 /* The current irm node does not have a valid BROADCAST_CHANNEL
1721 * register and we do, so reset the bus with force_root set */
1722 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1725 /* Oh screw it! Just leave the bus as it is */
1726 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1730 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1731 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1739 static int nodemgr_host_thread(void *__hi)
1741 struct host_info *hi = (struct host_info *)__hi;
1742 struct hpsb_host *host = hi->host;
1743 unsigned int g, generation = 0;
1744 int i, reset_cycles = 0;
1747 /* Setup our device-model entries */
1748 nodemgr_create_host_dev_files(host);
1751 /* Sleep until next bus reset */
1752 set_current_state(TASK_INTERRUPTIBLE);
1753 if (get_hpsb_generation(host) == generation &&
1754 !kthread_should_stop())
1756 __set_current_state(TASK_RUNNING);
1758 /* Thread may have been woken up to freeze or to exit */
1759 if (try_to_freeze())
1761 if (kthread_should_stop())
1764 /* Pause for 1/4 second in 1/16 second intervals,
1765 * to make sure things settle down. */
1766 g = get_hpsb_generation(host);
1767 for (i = 0; i < 4 ; i++) {
1768 msleep_interruptible(63);
1769 if (kthread_should_stop())
1772 /* Now get the generation in which the node ID's we collect
1773 * are valid. During the bus scan we will use this generation
1774 * for the read transactions, so that if another reset occurs
1775 * during the scan the transactions will fail instead of
1776 * returning bogus data. */
1777 generation = get_hpsb_generation(host);
1779 /* If we get a reset before we are done waiting, then
1780 * start the waiting over again */
1781 if (generation != g)
1782 g = generation, i = 0;
1785 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1786 !nodemgr_do_irm_duties(host, reset_cycles)) {
1792 /* Scan our nodes to get the bus options and create node
1793 * entries. This does not do the sysfs stuff, since that
1794 * would trigger uevents and such, which is a bad idea at
1796 nodemgr_node_scan(hi, generation);
1798 /* This actually does the full probe, with sysfs
1800 nodemgr_node_probe(hi, generation);
1802 /* Update some of our sysfs symlinks */
1803 nodemgr_update_host_dev_links(host);
1806 HPSB_VERBOSE("NodeMgr: Exiting thread");
1810 struct host_iter_param {
1812 int (*cb)(struct hpsb_host *, void *);
1815 static int __nodemgr_for_each_host(struct device *dev, void *data)
1817 struct hpsb_host *host;
1818 struct host_iter_param *hip = (struct host_iter_param *)data;
1821 host = container_of(dev, struct hpsb_host, host_dev);
1822 error = hip->cb(host, hip->data);
1827 * nodemgr_for_each_host - call a function for each IEEE 1394 host
1828 * @data: an address to supply to the callback
1829 * @cb: function to call for each host
1831 * Iterate the hosts, calling a given function with supplied data for each host.
1832 * If the callback fails on a host, i.e. if it returns a non-zero value, the
1833 * iteration is stopped.
1835 * Return value: 0 on success, non-zero on failure (same as returned by last run
1838 int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *))
1840 struct host_iter_param hip;
1845 error = class_for_each_device(&hpsb_host_class, NULL, &hip,
1846 __nodemgr_for_each_host);
1851 /* The following two convenience functions use a struct node_entry
1852 * for addressing a node on the bus. They are intended for use by any
1853 * process context, not just the nodemgr thread, so we need to be a
1854 * little careful when reading out the node ID and generation. The
1855 * thing that can go wrong is that we get the node ID, then a bus
1856 * reset occurs, and then we read the generation. The node ID is
1857 * possibly invalid, but the generation is current, and we end up
1858 * sending a packet to a the wrong node.
1860 * The solution is to make sure we read the generation first, so that
1861 * if a reset occurs in the process, we end up with a stale generation
1862 * and the transactions will fail instead of silently using wrong node
1867 * hpsb_node_fill_packet - fill some destination information into a packet
1868 * @ne: destination node
1869 * @packet: packet to fill in
1871 * This will fill in the given, pre-initialised hpsb_packet with the current
1872 * information from the node entry (host, node ID, bus generation number).
1874 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *packet)
1876 packet->host = ne->host;
1877 packet->generation = ne->generation;
1879 packet->node_id = ne->nodeid;
1882 int hpsb_node_write(struct node_entry *ne, u64 addr,
1883 quadlet_t *buffer, size_t length)
1885 unsigned int generation = ne->generation;
1888 return hpsb_write(ne->host, ne->nodeid, generation,
1889 addr, buffer, length);
1892 static void nodemgr_add_host(struct hpsb_host *host)
1894 struct host_info *hi;
1896 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1898 HPSB_ERR("NodeMgr: out of memory in add host");
1902 hi->thread = kthread_run(nodemgr_host_thread, hi, "knodemgrd_%d",
1904 if (IS_ERR(hi->thread)) {
1905 HPSB_ERR("NodeMgr: cannot start thread for host %d", host->id);
1906 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1910 static void nodemgr_host_reset(struct hpsb_host *host)
1912 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1915 HPSB_VERBOSE("NodeMgr: Processing reset for host %d", host->id);
1916 wake_up_process(hi->thread);
1920 static void nodemgr_remove_host(struct hpsb_host *host)
1922 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1925 kthread_stop(hi->thread);
1926 nodemgr_remove_host_dev(&host->device);
1930 static struct hpsb_highlevel nodemgr_highlevel = {
1931 .name = "Node manager",
1932 .add_host = nodemgr_add_host,
1933 .host_reset = nodemgr_host_reset,
1934 .remove_host = nodemgr_remove_host,
1937 int init_ieee1394_nodemgr(void)
1941 error = class_register(&nodemgr_ne_class);
1944 error = class_register(&nodemgr_ud_class);
1947 error = driver_register(&nodemgr_mid_layer_driver);
1950 /* This driver is not used if nodemgr is off (disable_nodemgr=1). */
1951 nodemgr_dev_template_host.driver = &nodemgr_mid_layer_driver;
1953 hpsb_register_highlevel(&nodemgr_highlevel);
1957 class_unregister(&nodemgr_ud_class);
1959 class_unregister(&nodemgr_ne_class);
1964 void cleanup_ieee1394_nodemgr(void)
1966 hpsb_unregister_highlevel(&nodemgr_highlevel);
1967 driver_unregister(&nodemgr_mid_layer_driver);
1968 class_unregister(&nodemgr_ud_class);
1969 class_unregister(&nodemgr_ne_class);