Linux v2.6.15-rc7
[pohmelfs.git] / drivers / ieee1394 / nodemgr.c
blob0ea37b1bccb27c13d1ae81392fad02c48dbd8923
1 /*
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.
9 */
11 #include <linux/kernel.h>
12 #include <linux/config.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/smp_lock.h>
16 #include <linux/interrupt.h>
17 #include <linux/kmod.h>
18 #include <linux/completion.h>
19 #include <linux/delay.h>
20 #include <linux/pci.h>
21 #include <linux/moduleparam.h>
22 #include <asm/atomic.h>
24 #include "ieee1394_types.h"
25 #include "ieee1394.h"
26 #include "ieee1394_core.h"
27 #include "hosts.h"
28 #include "ieee1394_transactions.h"
29 #include "highlevel.h"
30 #include "csr.h"
31 #include "nodemgr.h"
33 static int ignore_drivers;
34 module_param(ignore_drivers, int, 0444);
35 MODULE_PARM_DESC(ignore_drivers, "Disable automatic probing for drivers.");
37 struct nodemgr_csr_info {
38 struct hpsb_host *host;
39 nodeid_t nodeid;
40 unsigned int generation;
44 static char *nodemgr_find_oui_name(int oui)
46 #ifdef CONFIG_IEEE1394_OUI_DB
47 extern struct oui_list_struct {
48 int oui;
49 char *name;
50 } oui_list[];
51 int i;
53 for (i = 0; oui_list[i].name; i++)
54 if (oui_list[i].oui == oui)
55 return oui_list[i].name;
56 #endif
57 return NULL;
61 static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
62 void *buffer, void *__ci)
64 struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
65 int i, ret = 0;
67 for (i = 1; ; i++) {
68 ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
69 buffer, length);
70 if (!ret || i == 3)
71 break;
73 if (msleep_interruptible(334))
74 return -EINTR;
77 return ret;
80 static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
82 return (CSR1212_BE32_TO_CPU(bus_info_data[2]) >> 8) & 0x3;
85 static struct csr1212_bus_ops nodemgr_csr_ops = {
86 .bus_read = nodemgr_bus_read,
87 .get_max_rom = nodemgr_get_max_rom
92 * Basically what we do here is start off retrieving the bus_info block.
93 * From there will fill in some info about the node, verify it is of IEEE
94 * 1394 type, and that the crc checks out ok. After that we start off with
95 * the root directory, and subdirectories. To do this, we retrieve the
96 * quadlet header for a directory, find out the length, and retrieve the
97 * complete directory entry (be it a leaf or a directory). We then process
98 * it and add the info to our structure for that particular node.
100 * We verify CRC's along the way for each directory/block/leaf. The entire
101 * node structure is generic, and simply stores the information in a way
102 * that's easy to parse by the protocol interface.
106 * The nodemgr relies heavily on the Driver Model for device callbacks and
107 * driver/device mappings. The old nodemgr used to handle all this itself,
108 * but now we are much simpler because of the LDM.
111 static DECLARE_MUTEX(nodemgr_serialize);
113 struct host_info {
114 struct hpsb_host *host;
115 struct list_head list;
116 struct completion exited;
117 struct semaphore reset_sem;
118 int pid;
119 char daemon_name[15];
120 int kill_me;
123 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
124 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
125 char *buffer, int buffer_size);
126 static void nodemgr_resume_ne(struct node_entry *ne);
127 static void nodemgr_remove_ne(struct node_entry *ne);
128 static struct node_entry *find_entry_by_guid(u64 guid);
130 struct bus_type ieee1394_bus_type = {
131 .name = "ieee1394",
132 .match = nodemgr_bus_match,
135 static void host_cls_release(struct class_device *class_dev)
137 put_device(&container_of((class_dev), struct hpsb_host, class_dev)->device);
140 struct class hpsb_host_class = {
141 .name = "ieee1394_host",
142 .release = host_cls_release,
145 static void ne_cls_release(struct class_device *class_dev)
147 put_device(&container_of((class_dev), struct node_entry, class_dev)->device);
150 static struct class nodemgr_ne_class = {
151 .name = "ieee1394_node",
152 .release = ne_cls_release,
155 static void ud_cls_release(struct class_device *class_dev)
157 put_device(&container_of((class_dev), struct unit_directory, class_dev)->device);
160 /* The name here is only so that unit directory hotplug works with old
161 * style hotplug, which only ever did unit directories anyway. */
162 static struct class nodemgr_ud_class = {
163 .name = "ieee1394",
164 .release = ud_cls_release,
165 .hotplug = nodemgr_hotplug,
168 static struct hpsb_highlevel nodemgr_highlevel;
171 static void nodemgr_release_ud(struct device *dev)
173 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
175 if (ud->vendor_name_kv)
176 csr1212_release_keyval(ud->vendor_name_kv);
177 if (ud->model_name_kv)
178 csr1212_release_keyval(ud->model_name_kv);
180 kfree(ud);
183 static void nodemgr_release_ne(struct device *dev)
185 struct node_entry *ne = container_of(dev, struct node_entry, device);
187 if (ne->vendor_name_kv)
188 csr1212_release_keyval(ne->vendor_name_kv);
190 kfree(ne);
194 static void nodemgr_release_host(struct device *dev)
196 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
198 csr1212_destroy_csr(host->csr.rom);
200 kfree(host);
203 static int nodemgr_ud_platform_data;
205 static struct device nodemgr_dev_template_ud = {
206 .bus = &ieee1394_bus_type,
207 .release = nodemgr_release_ud,
208 .platform_data = &nodemgr_ud_platform_data,
211 static struct device nodemgr_dev_template_ne = {
212 .bus = &ieee1394_bus_type,
213 .release = nodemgr_release_ne,
216 struct device nodemgr_dev_template_host = {
217 .bus = &ieee1394_bus_type,
218 .release = nodemgr_release_host,
222 #define fw_attr(class, class_type, field, type, format_string) \
223 static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
225 class_type *class; \
226 class = container_of(dev, class_type, device); \
227 return sprintf(buf, format_string, (type)class->field); \
229 static struct device_attribute dev_attr_##class##_##field = { \
230 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
231 .show = fw_show_##class##_##field, \
234 #define fw_attr_td(class, class_type, td_kv) \
235 static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
237 int len; \
238 class_type *class = container_of(dev, class_type, device); \
239 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
240 memcpy(buf, \
241 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
242 len); \
243 while ((buf + len - 1) == '\0') \
244 len--; \
245 buf[len++] = '\n'; \
246 buf[len] = '\0'; \
247 return len; \
249 static struct device_attribute dev_attr_##class##_##td_kv = { \
250 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
251 .show = fw_show_##class##_##td_kv, \
255 #define fw_drv_attr(field, type, format_string) \
256 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
258 struct hpsb_protocol_driver *driver; \
259 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
260 return sprintf(buf, format_string, (type)driver->field);\
262 static struct driver_attribute driver_attr_drv_##field = { \
263 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
264 .show = fw_drv_show_##field, \
268 static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
270 struct node_entry *ne = container_of(dev, struct node_entry, device);
272 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
273 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
274 ne->busopt.irmc,
275 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
276 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
277 ne->busopt.max_rec,
278 ne->busopt.max_rom,
279 ne->busopt.cyc_clk_acc);
281 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
284 static ssize_t fw_show_ne_tlabels_free(struct device *dev, struct device_attribute *attr, char *buf)
286 struct node_entry *ne = container_of(dev, struct node_entry, device);
287 return sprintf(buf, "%d\n", atomic_read(&ne->tpool->count.count) + 1);
289 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
292 static ssize_t fw_show_ne_tlabels_allocations(struct device *dev, struct device_attribute *attr, char *buf)
294 struct node_entry *ne = container_of(dev, struct node_entry, device);
295 return sprintf(buf, "%u\n", ne->tpool->allocations);
297 static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
300 static ssize_t fw_show_ne_tlabels_mask(struct device *dev, struct device_attribute *attr, char *buf)
302 struct node_entry *ne = container_of(dev, struct node_entry, device);
303 #if (BITS_PER_LONG <= 32)
304 return sprintf(buf, "0x%08lx%08lx\n", ne->tpool->pool[0], ne->tpool->pool[1]);
305 #else
306 return sprintf(buf, "0x%016lx\n", ne->tpool->pool[0]);
307 #endif
309 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
312 static ssize_t fw_set_ignore_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
314 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
315 int state = simple_strtoul(buf, NULL, 10);
317 if (state == 1) {
318 down_write(&dev->bus->subsys.rwsem);
319 device_release_driver(dev);
320 ud->ignore_driver = 1;
321 up_write(&dev->bus->subsys.rwsem);
322 } else if (!state)
323 ud->ignore_driver = 0;
325 return count;
327 static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
329 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
331 return sprintf(buf, "%d\n", ud->ignore_driver);
333 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
336 static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
338 struct node_entry *ne;
339 u64 guid = (u64)simple_strtoull(buf, NULL, 16);
341 ne = find_entry_by_guid(guid);
343 if (ne == NULL || !ne->in_limbo)
344 return -EINVAL;
346 nodemgr_remove_ne(ne);
348 return count;
350 static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
352 return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
354 static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
356 static int nodemgr_rescan_bus_thread(void *__unused)
358 /* No userlevel access needed */
359 daemonize("kfwrescan");
361 bus_rescan_devices(&ieee1394_bus_type);
363 return 0;
366 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf, size_t count)
368 int state = simple_strtoul(buf, NULL, 10);
370 /* Don't wait for this, or care about errors. Root could do
371 * something stupid and spawn this a lot of times, but that's
372 * root's fault. */
373 if (state == 1)
374 kernel_thread(nodemgr_rescan_bus_thread, NULL, CLONE_KERNEL);
376 return count;
378 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
380 return sprintf(buf, "You can force a rescan of the bus for "
381 "drivers by writing a 1 to this file\n");
383 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
386 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
388 int state = simple_strtoul(buf, NULL, 10);
390 if (state == 1)
391 ignore_drivers = 1;
392 else if (!state)
393 ignore_drivers = 0;
395 return count;
397 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
399 return sprintf(buf, "%d\n", ignore_drivers);
401 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
404 struct bus_attribute *const fw_bus_attrs[] = {
405 &bus_attr_destroy_node,
406 &bus_attr_rescan,
407 &bus_attr_ignore_drivers,
408 NULL
412 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
413 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
415 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
416 fw_attr_td(ne, struct node_entry, vendor_name_kv)
417 fw_attr(ne, struct node_entry, vendor_oui, const char *, "%s\n")
419 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
420 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
421 fw_attr(ne, struct node_entry, guid_vendor_oui, const char *, "%s\n")
422 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
424 static struct device_attribute *const fw_ne_attrs[] = {
425 &dev_attr_ne_guid,
426 &dev_attr_ne_guid_vendor_id,
427 &dev_attr_ne_capabilities,
428 &dev_attr_ne_vendor_id,
429 &dev_attr_ne_nodeid,
430 &dev_attr_bus_options,
431 &dev_attr_tlabels_free,
432 &dev_attr_tlabels_allocations,
433 &dev_attr_tlabels_mask,
438 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
439 fw_attr(ud, struct unit_directory, length, int, "%d\n")
440 /* These are all dependent on the value being provided */
441 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
442 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
443 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
444 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
445 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
446 fw_attr(ud, struct unit_directory, vendor_oui, const char *, "%s\n")
447 fw_attr_td(ud, struct unit_directory, model_name_kv)
449 static struct device_attribute *const fw_ud_attrs[] = {
450 &dev_attr_ud_address,
451 &dev_attr_ud_length,
452 &dev_attr_ignore_driver,
456 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
457 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
458 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
459 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
460 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
461 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
462 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
463 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
465 static struct device_attribute *const fw_host_attrs[] = {
466 &dev_attr_host_node_count,
467 &dev_attr_host_selfid_count,
468 &dev_attr_host_nodes_active,
469 &dev_attr_host_in_bus_reset,
470 &dev_attr_host_is_root,
471 &dev_attr_host_is_cycmst,
472 &dev_attr_host_is_irm,
473 &dev_attr_host_is_busmgr,
477 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
479 struct hpsb_protocol_driver *driver;
480 struct ieee1394_device_id *id;
481 int length = 0;
482 char *scratch = buf;
484 driver = container_of(drv, struct hpsb_protocol_driver, driver);
486 for (id = driver->id_table; id->match_flags != 0; id++) {
487 int need_coma = 0;
489 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
490 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
491 scratch = buf + length;
492 need_coma++;
495 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
496 length += sprintf(scratch, "%smodel_id=0x%06x",
497 need_coma++ ? "," : "",
498 id->model_id);
499 scratch = buf + length;
502 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
503 length += sprintf(scratch, "%sspecifier_id=0x%06x",
504 need_coma++ ? "," : "",
505 id->specifier_id);
506 scratch = buf + length;
509 if (id->match_flags & IEEE1394_MATCH_VERSION) {
510 length += sprintf(scratch, "%sversion=0x%06x",
511 need_coma++ ? "," : "",
512 id->version);
513 scratch = buf + length;
516 if (need_coma) {
517 *scratch++ = '\n';
518 length++;
522 return length;
524 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
527 fw_drv_attr(name, const char *, "%s\n")
529 static struct driver_attribute *const fw_drv_attrs[] = {
530 &driver_attr_drv_name,
531 &driver_attr_device_ids,
535 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
537 struct device_driver *drv = &driver->driver;
538 int i;
540 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
541 driver_create_file(drv, fw_drv_attrs[i]);
545 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
547 struct device_driver *drv = &driver->driver;
548 int i;
550 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
551 driver_remove_file(drv, fw_drv_attrs[i]);
555 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
557 struct device *dev = &ne->device;
558 int i;
560 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
561 device_create_file(dev, fw_ne_attrs[i]);
565 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
567 struct device *dev = &host->device;
568 int i;
570 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
571 device_create_file(dev, fw_host_attrs[i]);
575 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid);
577 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
579 struct device *dev = &host->device;
580 struct node_entry *ne;
582 sysfs_remove_link(&dev->kobj, "irm_id");
583 sysfs_remove_link(&dev->kobj, "busmgr_id");
584 sysfs_remove_link(&dev->kobj, "host_id");
586 if ((ne = find_entry_by_nodeid(host, host->irm_id)))
587 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id");
588 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)))
589 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id");
590 if ((ne = find_entry_by_nodeid(host, host->node_id)))
591 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id");
594 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
596 struct device *dev = &ud->device;
597 int i;
599 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
600 device_create_file(dev, fw_ud_attrs[i]);
602 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
603 device_create_file(dev, &dev_attr_ud_specifier_id);
605 if (ud->flags & UNIT_DIRECTORY_VERSION)
606 device_create_file(dev, &dev_attr_ud_version);
608 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
609 device_create_file(dev, &dev_attr_ud_vendor_id);
610 if (ud->vendor_name_kv)
611 device_create_file(dev, &dev_attr_ud_vendor_name_kv);
614 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
615 device_create_file(dev, &dev_attr_ud_model_id);
616 if (ud->model_name_kv)
617 device_create_file(dev, &dev_attr_ud_model_name_kv);
622 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
624 struct hpsb_protocol_driver *driver;
625 struct unit_directory *ud;
626 struct ieee1394_device_id *id;
628 /* We only match unit directories */
629 if (dev->platform_data != &nodemgr_ud_platform_data)
630 return 0;
632 ud = container_of(dev, struct unit_directory, device);
633 driver = container_of(drv, struct hpsb_protocol_driver, driver);
635 if (ud->ne->in_limbo || ud->ignore_driver)
636 return 0;
638 for (id = driver->id_table; id->match_flags != 0; id++) {
639 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
640 id->vendor_id != ud->vendor_id)
641 continue;
643 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
644 id->model_id != ud->model_id)
645 continue;
647 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
648 id->specifier_id != ud->specifier_id)
649 continue;
651 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
652 id->version != ud->version)
653 continue;
655 return 1;
658 return 0;
662 static void nodemgr_remove_uds(struct node_entry *ne)
664 struct class_device *cdev, *next;
665 struct unit_directory *ud;
667 list_for_each_entry_safe(cdev, next, &nodemgr_ud_class.children, node) {
668 ud = container_of(cdev, struct unit_directory, class_dev);
670 if (ud->ne != ne)
671 continue;
673 class_device_unregister(&ud->class_dev);
674 device_unregister(&ud->device);
679 static void nodemgr_remove_ne(struct node_entry *ne)
681 struct device *dev = &ne->device;
683 dev = get_device(&ne->device);
684 if (!dev)
685 return;
687 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
688 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
690 nodemgr_remove_uds(ne);
692 class_device_unregister(&ne->class_dev);
693 device_unregister(dev);
695 put_device(dev);
698 static int __nodemgr_remove_host_dev(struct device *dev, void *data)
700 nodemgr_remove_ne(container_of(dev, struct node_entry, device));
701 return 0;
704 static void nodemgr_remove_host_dev(struct device *dev)
706 device_for_each_child(dev, NULL, __nodemgr_remove_host_dev);
707 sysfs_remove_link(&dev->kobj, "irm_id");
708 sysfs_remove_link(&dev->kobj, "busmgr_id");
709 sysfs_remove_link(&dev->kobj, "host_id");
713 static void nodemgr_update_bus_options(struct node_entry *ne)
715 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
716 static const u16 mr[] = { 4, 64, 1024, 0};
717 #endif
718 quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
720 ne->busopt.irmc = (busoptions >> 31) & 1;
721 ne->busopt.cmc = (busoptions >> 30) & 1;
722 ne->busopt.isc = (busoptions >> 29) & 1;
723 ne->busopt.bmc = (busoptions >> 28) & 1;
724 ne->busopt.pmc = (busoptions >> 27) & 1;
725 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
726 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
727 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
728 ne->busopt.generation = (busoptions >> 4) & 0xf;
729 ne->busopt.lnkspd = busoptions & 0x7;
731 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
732 "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
733 busoptions, ne->busopt.irmc, ne->busopt.cmc,
734 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
735 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
736 mr[ne->busopt.max_rom],
737 ne->busopt.generation, ne->busopt.lnkspd);
741 static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
742 struct host_info *hi, nodeid_t nodeid,
743 unsigned int generation)
745 struct hpsb_host *host = hi->host;
746 struct node_entry *ne;
748 ne = kmalloc(sizeof(struct node_entry), GFP_KERNEL);
749 if (!ne) return NULL;
751 memset(ne, 0, sizeof(struct node_entry));
753 ne->tpool = &host->tpool[nodeid & NODE_MASK];
755 ne->host = host;
756 ne->nodeid = nodeid;
757 ne->generation = generation;
758 ne->needs_probe = 1;
760 ne->guid = guid;
761 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
762 ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
763 ne->csr = csr;
765 memcpy(&ne->device, &nodemgr_dev_template_ne,
766 sizeof(ne->device));
767 ne->device.parent = &host->device;
768 snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
769 (unsigned long long)(ne->guid));
771 ne->class_dev.dev = &ne->device;
772 ne->class_dev.class = &nodemgr_ne_class;
773 snprintf(ne->class_dev.class_id, BUS_ID_SIZE, "%016Lx",
774 (unsigned long long)(ne->guid));
776 device_register(&ne->device);
777 class_device_register(&ne->class_dev);
778 get_device(&ne->device);
780 if (ne->guid_vendor_oui)
781 device_create_file(&ne->device, &dev_attr_ne_guid_vendor_oui);
782 nodemgr_create_ne_dev_files(ne);
784 nodemgr_update_bus_options(ne);
786 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
787 (host->node_id == nodeid) ? "Host" : "Node",
788 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
790 return ne;
794 static struct node_entry *find_entry_by_guid(u64 guid)
796 struct class *class = &nodemgr_ne_class;
797 struct class_device *cdev;
798 struct node_entry *ne, *ret_ne = NULL;
800 down_read(&class->subsys.rwsem);
801 list_for_each_entry(cdev, &class->children, node) {
802 ne = container_of(cdev, struct node_entry, class_dev);
804 if (ne->guid == guid) {
805 ret_ne = ne;
806 break;
809 up_read(&class->subsys.rwsem);
811 return ret_ne;
815 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
817 struct class *class = &nodemgr_ne_class;
818 struct class_device *cdev;
819 struct node_entry *ne, *ret_ne = NULL;
821 down_read(&class->subsys.rwsem);
822 list_for_each_entry(cdev, &class->children, node) {
823 ne = container_of(cdev, struct node_entry, class_dev);
825 if (ne->host == host && ne->nodeid == nodeid) {
826 ret_ne = ne;
827 break;
830 up_read(&class->subsys.rwsem);
832 return ret_ne;
836 static void nodemgr_register_device(struct node_entry *ne,
837 struct unit_directory *ud, struct device *parent)
839 memcpy(&ud->device, &nodemgr_dev_template_ud,
840 sizeof(ud->device));
842 ud->device.parent = parent;
844 snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
845 ne->device.bus_id, ud->id);
847 ud->class_dev.dev = &ud->device;
848 ud->class_dev.class = &nodemgr_ud_class;
849 snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
850 ne->device.bus_id, ud->id);
852 device_register(&ud->device);
853 class_device_register(&ud->class_dev);
854 get_device(&ud->device);
856 if (ud->vendor_oui)
857 device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
858 nodemgr_create_ud_dev_files(ud);
862 /* This implementation currently only scans the config rom and its
863 * immediate unit directories looking for software_id and
864 * software_version entries, in order to get driver autoloading working. */
865 static struct unit_directory *nodemgr_process_unit_directory
866 (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
867 unsigned int *id, struct unit_directory *parent)
869 struct unit_directory *ud;
870 struct unit_directory *ud_child = NULL;
871 struct csr1212_dentry *dentry;
872 struct csr1212_keyval *kv;
873 u8 last_key_id = 0;
875 ud = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
876 if (!ud)
877 goto unit_directory_error;
879 memset (ud, 0, sizeof(struct unit_directory));
881 ud->ne = ne;
882 ud->ignore_driver = ignore_drivers;
883 ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
884 ud->ud_kv = ud_kv;
885 ud->id = (*id)++;
887 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
888 switch (kv->key.id) {
889 case CSR1212_KV_ID_VENDOR:
890 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
891 ud->vendor_id = kv->value.immediate;
892 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
894 if (ud->vendor_id)
895 ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
897 break;
899 case CSR1212_KV_ID_MODEL:
900 ud->model_id = kv->value.immediate;
901 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
902 break;
904 case CSR1212_KV_ID_SPECIFIER_ID:
905 ud->specifier_id = kv->value.immediate;
906 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
907 break;
909 case CSR1212_KV_ID_VERSION:
910 ud->version = kv->value.immediate;
911 ud->flags |= UNIT_DIRECTORY_VERSION;
912 break;
914 case CSR1212_KV_ID_DESCRIPTOR:
915 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
916 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
917 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
918 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
919 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
920 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
921 switch (last_key_id) {
922 case CSR1212_KV_ID_VENDOR:
923 ud->vendor_name_kv = kv;
924 csr1212_keep_keyval(kv);
925 break;
927 case CSR1212_KV_ID_MODEL:
928 ud->model_name_kv = kv;
929 csr1212_keep_keyval(kv);
930 break;
933 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
934 break;
936 case CSR1212_KV_ID_DEPENDENT_INFO:
937 /* Logical Unit Number */
938 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
939 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
940 ud_child = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
941 if (!ud_child)
942 goto unit_directory_error;
943 memcpy(ud_child, ud, sizeof(struct unit_directory));
944 nodemgr_register_device(ne, ud_child, &ne->device);
945 ud_child = NULL;
947 ud->id = (*id)++;
949 ud->lun = kv->value.immediate;
950 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
952 /* Logical Unit Directory */
953 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
954 /* This should really be done in SBP2 as this is
955 * doing SBP2 specific parsing.
958 /* first register the parent unit */
959 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
960 if (ud->device.bus != &ieee1394_bus_type)
961 nodemgr_register_device(ne, ud, &ne->device);
963 /* process the child unit */
964 ud_child = nodemgr_process_unit_directory(hi, ne, kv, id, ud);
966 if (ud_child == NULL)
967 break;
969 /* inherit unspecified values so hotplug picks it up */
970 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
971 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
973 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
974 ud_child->model_id = ud->model_id;
976 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
977 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
979 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
980 ud_child->specifier_id = ud->specifier_id;
982 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
983 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
985 ud_child->flags |= UNIT_DIRECTORY_VERSION;
986 ud_child->version = ud->version;
989 /* register the child unit */
990 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
991 nodemgr_register_device(ne, ud_child, &ud->device);
994 break;
996 default:
997 break;
999 last_key_id = kv->key.id;
1002 /* do not process child units here and only if not already registered */
1003 if (!parent && ud->device.bus != &ieee1394_bus_type)
1004 nodemgr_register_device(ne, ud, &ne->device);
1006 return ud;
1008 unit_directory_error:
1009 kfree(ud);
1010 return NULL;
1014 static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
1016 unsigned int ud_id = 0;
1017 struct csr1212_dentry *dentry;
1018 struct csr1212_keyval *kv;
1019 u8 last_key_id = 0;
1021 ne->needs_probe = 0;
1023 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1024 switch (kv->key.id) {
1025 case CSR1212_KV_ID_VENDOR:
1026 ne->vendor_id = kv->value.immediate;
1028 if (ne->vendor_id)
1029 ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
1030 break;
1032 case CSR1212_KV_ID_NODE_CAPABILITIES:
1033 ne->capabilities = kv->value.immediate;
1034 break;
1036 case CSR1212_KV_ID_UNIT:
1037 nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
1038 break;
1040 case CSR1212_KV_ID_DESCRIPTOR:
1041 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1042 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1043 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1044 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1045 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1046 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1047 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1048 ne->vendor_name_kv = kv;
1049 csr1212_keep_keyval(kv);
1052 break;
1054 last_key_id = kv->key.id;
1057 if (ne->vendor_oui)
1058 device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
1059 if (ne->vendor_name_kv)
1060 device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
1063 #ifdef CONFIG_HOTPLUG
1065 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
1066 char *buffer, int buffer_size)
1068 struct unit_directory *ud;
1069 int i = 0;
1070 int length = 0;
1071 /* ieee1394:venNmoNspNverN */
1072 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
1074 if (!cdev)
1075 return -ENODEV;
1077 ud = container_of(cdev, struct unit_directory, class_dev);
1079 if (ud->ne->in_limbo || ud->ignore_driver)
1080 return -ENODEV;
1082 #define PUT_ENVP(fmt,val) \
1083 do { \
1084 int printed; \
1085 envp[i++] = buffer; \
1086 printed = snprintf(buffer, buffer_size - length, \
1087 fmt, val); \
1088 if ((buffer_size - (length+printed) <= 0) || (i >= num_envp)) \
1089 return -ENOMEM; \
1090 length += printed+1; \
1091 buffer += printed+1; \
1092 } while (0)
1094 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1095 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1096 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1097 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1098 PUT_ENVP("VERSION=%06x", ud->version);
1099 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1100 ud->vendor_id,
1101 ud->model_id,
1102 ud->specifier_id,
1103 ud->version);
1104 PUT_ENVP("MODALIAS=%s", buf);
1106 #undef PUT_ENVP
1108 envp[i] = NULL;
1110 return 0;
1113 #else
1115 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
1116 char *buffer, int buffer_size)
1118 return -ENODEV;
1121 #endif /* CONFIG_HOTPLUG */
1124 int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
1126 int ret;
1128 /* This will cause a probe for devices */
1129 ret = driver_register(&driver->driver);
1130 if (!ret)
1131 nodemgr_create_drv_files(driver);
1133 return ret;
1136 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1138 nodemgr_remove_drv_files(driver);
1139 /* This will subsequently disconnect all devices that our driver
1140 * is attached to. */
1141 driver_unregister(&driver->driver);
1146 * This function updates nodes that were present on the bus before the
1147 * reset and still are after the reset. The nodeid and the config rom
1148 * may have changed, and the drivers managing this device must be
1149 * informed that this device just went through a bus reset, to allow
1150 * the to take whatever actions required.
1152 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1153 struct host_info *hi, nodeid_t nodeid,
1154 unsigned int generation)
1156 if (ne->nodeid != nodeid) {
1157 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1158 NODE_BUS_ARGS(ne->host, ne->nodeid),
1159 NODE_BUS_ARGS(ne->host, nodeid));
1160 ne->nodeid = nodeid;
1163 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1164 kfree(ne->csr->private);
1165 csr1212_destroy_csr(ne->csr);
1166 ne->csr = csr;
1168 /* If the node's configrom generation has changed, we
1169 * unregister all the unit directories. */
1170 nodemgr_remove_uds(ne);
1172 nodemgr_update_bus_options(ne);
1174 /* Mark the node as new, so it gets re-probed */
1175 ne->needs_probe = 1;
1176 } else {
1177 /* old cache is valid, so update its generation */
1178 struct nodemgr_csr_info *ci = ne->csr->private;
1179 ci->generation = generation;
1180 /* free the partially filled now unneeded new cache */
1181 kfree(csr->private);
1182 csr1212_destroy_csr(csr);
1185 if (ne->in_limbo)
1186 nodemgr_resume_ne(ne);
1188 /* Mark the node current */
1189 ne->generation = generation;
1194 static void nodemgr_node_scan_one(struct host_info *hi,
1195 nodeid_t nodeid, int generation)
1197 struct hpsb_host *host = hi->host;
1198 struct node_entry *ne;
1199 octlet_t guid;
1200 struct csr1212_csr *csr;
1201 struct nodemgr_csr_info *ci;
1203 ci = kmalloc(sizeof(struct nodemgr_csr_info), GFP_KERNEL);
1204 if (!ci)
1205 return;
1207 ci->host = host;
1208 ci->nodeid = nodeid;
1209 ci->generation = generation;
1211 /* We need to detect when the ConfigROM's generation has changed,
1212 * so we only update the node's info when it needs to be. */
1214 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1215 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1216 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1217 NODE_BUS_ARGS(host, nodeid));
1218 if (csr)
1219 csr1212_destroy_csr(csr);
1220 kfree(ci);
1221 return;
1224 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1225 /* This isn't a 1394 device, but we let it slide. There
1226 * was a report of a device with broken firmware which
1227 * reported '2394' instead of '1394', which is obviously a
1228 * mistake. One would hope that a non-1394 device never
1229 * gets connected to Firewire bus. If someone does, we
1230 * shouldn't be held responsible, so we'll allow it with a
1231 * warning. */
1232 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1233 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1236 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1237 ne = find_entry_by_guid(guid);
1239 if (ne && ne->host != host && ne->in_limbo) {
1240 /* Must have moved this device from one host to another */
1241 nodemgr_remove_ne(ne);
1242 ne = NULL;
1245 if (!ne)
1246 nodemgr_create_node(guid, csr, hi, nodeid, generation);
1247 else
1248 nodemgr_update_node(ne, csr, hi, nodeid, generation);
1250 return;
1254 static void nodemgr_node_scan(struct host_info *hi, int generation)
1256 int count;
1257 struct hpsb_host *host = hi->host;
1258 struct selfid *sid = (struct selfid *)host->topology_map;
1259 nodeid_t nodeid = LOCAL_BUS;
1261 /* Scan each node on the bus */
1262 for (count = host->selfid_count; count; count--, sid++) {
1263 if (sid->extended)
1264 continue;
1266 if (!sid->link_active) {
1267 nodeid++;
1268 continue;
1270 nodemgr_node_scan_one(hi, nodeid++, generation);
1275 static void nodemgr_suspend_ne(struct node_entry *ne)
1277 struct class_device *cdev;
1278 struct unit_directory *ud;
1280 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1281 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1283 ne->in_limbo = 1;
1284 device_create_file(&ne->device, &dev_attr_ne_in_limbo);
1286 down_write(&ne->device.bus->subsys.rwsem);
1287 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1288 ud = container_of(cdev, struct unit_directory, class_dev);
1290 if (ud->ne != ne)
1291 continue;
1293 if (ud->device.driver &&
1294 (!ud->device.driver->suspend ||
1295 ud->device.driver->suspend(&ud->device, PMSG_SUSPEND)))
1296 device_release_driver(&ud->device);
1298 up_write(&ne->device.bus->subsys.rwsem);
1302 static void nodemgr_resume_ne(struct node_entry *ne)
1304 struct class_device *cdev;
1305 struct unit_directory *ud;
1307 ne->in_limbo = 0;
1308 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1310 down_read(&ne->device.bus->subsys.rwsem);
1311 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1312 ud = container_of(cdev, struct unit_directory, class_dev);
1314 if (ud->ne != ne)
1315 continue;
1317 if (ud->device.driver && ud->device.driver->resume)
1318 ud->device.driver->resume(&ud->device);
1320 up_read(&ne->device.bus->subsys.rwsem);
1322 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1323 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1327 static void nodemgr_update_pdrv(struct node_entry *ne)
1329 struct unit_directory *ud;
1330 struct hpsb_protocol_driver *pdrv;
1331 struct class *class = &nodemgr_ud_class;
1332 struct class_device *cdev;
1334 down_read(&class->subsys.rwsem);
1335 list_for_each_entry(cdev, &class->children, node) {
1336 ud = container_of(cdev, struct unit_directory, class_dev);
1337 if (ud->ne != ne || !ud->device.driver)
1338 continue;
1340 pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
1342 if (pdrv->update && pdrv->update(ud)) {
1343 down_write(&ud->device.bus->subsys.rwsem);
1344 device_release_driver(&ud->device);
1345 up_write(&ud->device.bus->subsys.rwsem);
1348 up_read(&class->subsys.rwsem);
1352 /* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1353 * seems like an optional service but in the end it is practically mandatory
1354 * as a consequence of these clauses.
1356 * Note that we cannot do a broadcast write to all nodes at once because some
1357 * pre-1394a devices would hang. */
1358 static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1360 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1361 quadlet_t bc_remote, bc_local;
1362 int ret;
1364 if (!ne->host->is_irm || ne->generation != generation ||
1365 ne->nodeid == ne->host->node_id)
1366 return;
1368 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1370 /* Check if the register is implemented and 1394a compliant. */
1371 ret = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1372 sizeof(bc_remote));
1373 if (!ret && bc_remote & cpu_to_be32(0x80000000) &&
1374 bc_remote != bc_local)
1375 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1379 static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
1381 struct device *dev;
1383 if (ne->host != hi->host || ne->in_limbo)
1384 return;
1386 dev = get_device(&ne->device);
1387 if (!dev)
1388 return;
1390 nodemgr_irm_write_bc(ne, generation);
1392 /* If "needs_probe", then this is either a new or changed node we
1393 * rescan totally. If the generation matches for an existing node
1394 * (one that existed prior to the bus reset) we send update calls
1395 * down to the drivers. Otherwise, this is a dead node and we
1396 * suspend it. */
1397 if (ne->needs_probe)
1398 nodemgr_process_root_directory(hi, ne);
1399 else if (ne->generation == generation)
1400 nodemgr_update_pdrv(ne);
1401 else
1402 nodemgr_suspend_ne(ne);
1404 put_device(dev);
1408 static void nodemgr_node_probe(struct host_info *hi, int generation)
1410 struct hpsb_host *host = hi->host;
1411 struct class *class = &nodemgr_ne_class;
1412 struct class_device *cdev;
1414 /* Do some processing of the nodes we've probed. This pulls them
1415 * into the sysfs layer if needed, and can result in processing of
1416 * unit-directories, or just updating the node and it's
1417 * unit-directories. */
1418 down_read(&class->subsys.rwsem);
1419 list_for_each_entry(cdev, &class->children, node)
1420 nodemgr_probe_ne(hi, container_of(cdev, struct node_entry, class_dev), generation);
1421 up_read(&class->subsys.rwsem);
1424 /* If we had a bus reset while we were scanning the bus, it is
1425 * possible that we did not probe all nodes. In that case, we
1426 * skip the clean up for now, since we could remove nodes that
1427 * were still on the bus. The bus reset increased hi->reset_sem,
1428 * so there's a bus scan pending which will do the clean up
1429 * eventually.
1431 * Now let's tell the bus to rescan our devices. This may seem
1432 * like overhead, but the driver-model core will only scan a
1433 * device for a driver when either the device is added, or when a
1434 * new driver is added. A bus reset is a good reason to rescan
1435 * devices that were there before. For example, an sbp2 device
1436 * may become available for login, if the host that held it was
1437 * just removed. */
1439 if (generation == get_hpsb_generation(host))
1440 bus_rescan_devices(&ieee1394_bus_type);
1442 return;
1445 static int nodemgr_send_resume_packet(struct hpsb_host *host)
1447 struct hpsb_packet *packet;
1448 int ret = 1;
1450 packet = hpsb_make_phypacket(host,
1451 0x003c0000 | NODEID_TO_NODE(host->node_id) << 24);
1452 if (packet) {
1453 packet->no_waiter = 1;
1454 packet->generation = get_hpsb_generation(host);
1455 ret = hpsb_send_packet(packet);
1457 if (ret)
1458 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1459 host->id);
1460 return ret;
1463 /* Perform a few high-level IRM responsibilities. */
1464 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1466 quadlet_t bc;
1468 /* if irm_id == -1 then there is no IRM on this bus */
1469 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1470 return 1;
1472 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1473 host->csr.broadcast_channel |= 0x40000000;
1475 /* If there is no bus manager then we should set the root node's
1476 * force_root bit to promote bus stability per the 1394
1477 * spec. (8.4.2.6) */
1478 if (host->busmgr_id == 0xffff && host->node_count > 1)
1480 u16 root_node = host->node_count - 1;
1482 /* get cycle master capability flag from root node */
1483 if (host->is_cycmst ||
1484 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1485 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1486 &bc, sizeof(quadlet_t)) &&
1487 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
1488 hpsb_send_phy_config(host, root_node, -1);
1489 else {
1490 HPSB_DEBUG("The root node is not cycle master capable; "
1491 "selecting a new root node and resetting...");
1493 if (cycles >= 5) {
1494 /* Oh screw it! Just leave the bus as it is */
1495 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1496 return 1;
1499 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1500 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1502 return 0;
1506 /* Some devices suspend their ports while being connected to an inactive
1507 * host adapter, i.e. if connected before the low-level driver is
1508 * loaded. They become visible either when physically unplugged and
1509 * replugged, or when receiving a resume packet. Send one once. */
1510 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1511 host->resume_packet_sent = 1;
1513 return 1;
1516 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1517 * everything we can do, otherwise issue a bus reset and try to become the IRM
1518 * ourselves. */
1519 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1521 quadlet_t bc;
1522 int status;
1524 if (hpsb_disable_irm || host->is_irm)
1525 return 1;
1527 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1528 get_hpsb_generation(host),
1529 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1530 &bc, sizeof(quadlet_t));
1532 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1533 /* The current irm node does not have a valid BROADCAST_CHANNEL
1534 * register and we do, so reset the bus with force_root set */
1535 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1537 if (cycles >= 5) {
1538 /* Oh screw it! Just leave the bus as it is */
1539 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1540 return 1;
1543 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1544 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1546 return 0;
1549 return 1;
1552 static int nodemgr_host_thread(void *__hi)
1554 struct host_info *hi = (struct host_info *)__hi;
1555 struct hpsb_host *host = hi->host;
1556 int reset_cycles = 0;
1558 /* No userlevel access needed */
1559 daemonize(hi->daemon_name);
1561 /* Setup our device-model entries */
1562 nodemgr_create_host_dev_files(host);
1564 /* Sit and wait for a signal to probe the nodes on the bus. This
1565 * happens when we get a bus reset. */
1566 while (1) {
1567 unsigned int generation = 0;
1568 int i;
1570 if (down_interruptible(&hi->reset_sem) ||
1571 down_interruptible(&nodemgr_serialize)) {
1572 if (try_to_freeze())
1573 continue;
1574 printk("NodeMgr: received unexpected signal?!\n" );
1575 break;
1578 if (hi->kill_me) {
1579 up(&nodemgr_serialize);
1580 break;
1583 /* Pause for 1/4 second in 1/16 second intervals,
1584 * to make sure things settle down. */
1585 for (i = 0; i < 4 ; i++) {
1586 set_current_state(TASK_INTERRUPTIBLE);
1587 if (msleep_interruptible(63)) {
1588 up(&nodemgr_serialize);
1589 goto caught_signal;
1592 /* Now get the generation in which the node ID's we collect
1593 * are valid. During the bus scan we will use this generation
1594 * for the read transactions, so that if another reset occurs
1595 * during the scan the transactions will fail instead of
1596 * returning bogus data. */
1597 generation = get_hpsb_generation(host);
1599 /* If we get a reset before we are done waiting, then
1600 * start the the waiting over again */
1601 while (!down_trylock(&hi->reset_sem))
1602 i = 0;
1604 /* Check the kill_me again */
1605 if (hi->kill_me) {
1606 up(&nodemgr_serialize);
1607 goto caught_signal;
1611 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1612 !nodemgr_do_irm_duties(host, reset_cycles)) {
1613 reset_cycles++;
1614 up(&nodemgr_serialize);
1615 continue;
1617 reset_cycles = 0;
1619 /* Scan our nodes to get the bus options and create node
1620 * entries. This does not do the sysfs stuff, since that
1621 * would trigger hotplug callbacks and such, which is a
1622 * bad idea at this point. */
1623 nodemgr_node_scan(hi, generation);
1625 /* This actually does the full probe, with sysfs
1626 * registration. */
1627 nodemgr_node_probe(hi, generation);
1629 /* Update some of our sysfs symlinks */
1630 nodemgr_update_host_dev_links(host);
1632 up(&nodemgr_serialize);
1635 caught_signal:
1636 HPSB_VERBOSE("NodeMgr: Exiting thread");
1638 complete_and_exit(&hi->exited, 0);
1641 int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
1643 struct class *class = &hpsb_host_class;
1644 struct class_device *cdev;
1645 struct hpsb_host *host;
1646 int error = 0;
1648 down_read(&class->subsys.rwsem);
1649 list_for_each_entry(cdev, &class->children, node) {
1650 host = container_of(cdev, struct hpsb_host, class_dev);
1652 if ((error = cb(host, __data)))
1653 break;
1655 up_read(&class->subsys.rwsem);
1657 return error;
1660 /* The following four convenience functions use a struct node_entry
1661 * for addressing a node on the bus. They are intended for use by any
1662 * process context, not just the nodemgr thread, so we need to be a
1663 * little careful when reading out the node ID and generation. The
1664 * thing that can go wrong is that we get the node ID, then a bus
1665 * reset occurs, and then we read the generation. The node ID is
1666 * possibly invalid, but the generation is current, and we end up
1667 * sending a packet to a the wrong node.
1669 * The solution is to make sure we read the generation first, so that
1670 * if a reset occurs in the process, we end up with a stale generation
1671 * and the transactions will fail instead of silently using wrong node
1672 * ID's.
1675 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
1677 pkt->host = ne->host;
1678 pkt->generation = ne->generation;
1679 barrier();
1680 pkt->node_id = ne->nodeid;
1683 int hpsb_node_write(struct node_entry *ne, u64 addr,
1684 quadlet_t *buffer, size_t length)
1686 unsigned int generation = ne->generation;
1688 barrier();
1689 return hpsb_write(ne->host, ne->nodeid, generation,
1690 addr, buffer, length);
1693 static void nodemgr_add_host(struct hpsb_host *host)
1695 struct host_info *hi;
1697 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1699 if (!hi) {
1700 HPSB_ERR ("NodeMgr: out of memory in add host");
1701 return;
1704 hi->host = host;
1705 init_completion(&hi->exited);
1706 sema_init(&hi->reset_sem, 0);
1708 sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
1710 hi->pid = kernel_thread(nodemgr_host_thread, hi, CLONE_KERNEL);
1712 if (hi->pid < 0) {
1713 HPSB_ERR ("NodeMgr: failed to start %s thread for %s",
1714 hi->daemon_name, host->driver->name);
1715 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1716 return;
1719 return;
1722 static void nodemgr_host_reset(struct hpsb_host *host)
1724 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1726 if (hi != NULL) {
1727 HPSB_VERBOSE("NodeMgr: Processing host reset for %s", hi->daemon_name);
1728 up(&hi->reset_sem);
1729 } else
1730 HPSB_ERR ("NodeMgr: could not process reset of unused host");
1732 return;
1735 static void nodemgr_remove_host(struct hpsb_host *host)
1737 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1739 if (hi) {
1740 if (hi->pid >= 0) {
1741 hi->kill_me = 1;
1742 mb();
1743 up(&hi->reset_sem);
1744 wait_for_completion(&hi->exited);
1745 nodemgr_remove_host_dev(&host->device);
1747 } else
1748 HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
1749 host->driver->name);
1751 return;
1754 static struct hpsb_highlevel nodemgr_highlevel = {
1755 .name = "Node manager",
1756 .add_host = nodemgr_add_host,
1757 .host_reset = nodemgr_host_reset,
1758 .remove_host = nodemgr_remove_host,
1761 int init_ieee1394_nodemgr(void)
1763 int ret;
1765 ret = class_register(&nodemgr_ne_class);
1766 if (ret < 0)
1767 return ret;
1769 ret = class_register(&nodemgr_ud_class);
1770 if (ret < 0) {
1771 class_unregister(&nodemgr_ne_class);
1772 return ret;
1775 hpsb_register_highlevel(&nodemgr_highlevel);
1777 return 0;
1780 void cleanup_ieee1394_nodemgr(void)
1782 hpsb_unregister_highlevel(&nodemgr_highlevel);
1784 class_unregister(&nodemgr_ud_class);
1785 class_unregister(&nodemgr_ne_class);