initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / ieee1394 / nodemgr.c
blobcc9c999f9a8bc565d8311620569431248d7dfb1c
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 <linux/suspend.h>
23 #include <asm/atomic.h>
25 #include "ieee1394_types.h"
26 #include "ieee1394.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 = 0;
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 = 0; i < 3; i++) {
68 ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
69 buffer, length);
70 if (!ret)
71 break;
73 set_current_state(TASK_INTERRUPTIBLE);
74 if (schedule_timeout (HZ/3))
75 return -EINTR;
78 return ret;
81 static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
83 return (CSR1212_BE32_TO_CPU(bus_info_data[2]) >> 8) & 0x3;
86 static struct csr1212_bus_ops nodemgr_csr_ops = {
87 .bus_read = nodemgr_bus_read,
88 .get_max_rom = nodemgr_get_max_rom
93 * Basically what we do here is start off retrieving the bus_info block.
94 * From there will fill in some info about the node, verify it is of IEEE
95 * 1394 type, and that the crc checks out ok. After that we start off with
96 * the root directory, and subdirectories. To do this, we retrieve the
97 * quadlet header for a directory, find out the length, and retrieve the
98 * complete directory entry (be it a leaf or a directory). We then process
99 * it and add the info to our structure for that particular node.
101 * We verify CRC's along the way for each directory/block/leaf. The entire
102 * node structure is generic, and simply stores the information in a way
103 * that's easy to parse by the protocol interface.
107 * The nodemgr relies heavily on the Driver Model for device callbacks and
108 * driver/device mappings. The old nodemgr used to handle all this itself,
109 * but now we are much simpler because of the LDM.
112 static DECLARE_MUTEX(nodemgr_serialize);
114 struct host_info {
115 struct hpsb_host *host;
116 struct list_head list;
117 struct completion exited;
118 struct semaphore reset_sem;
119 int pid;
120 char daemon_name[15];
121 int kill_me;
124 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
125 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
126 char *buffer, int buffer_size);
127 static void nodemgr_resume_ne(struct node_entry *ne);
128 static void nodemgr_remove_ne(struct node_entry *ne);
129 static struct node_entry *find_entry_by_guid(u64 guid);
131 struct bus_type ieee1394_bus_type = {
132 .name = "ieee1394",
133 .match = nodemgr_bus_match,
136 static void host_cls_release(struct class_device *class_dev)
138 put_device(&container_of((class_dev), struct hpsb_host, class_dev)->device);
141 struct class hpsb_host_class = {
142 .name = "ieee1394_host",
143 .release = host_cls_release,
146 static void ne_cls_release(struct class_device *class_dev)
148 put_device(&container_of((class_dev), struct node_entry, class_dev)->device);
151 struct class nodemgr_ne_class = {
152 .name = "ieee1394_node",
153 .release = ne_cls_release,
156 static void ud_cls_release(struct class_device *class_dev)
158 put_device(&container_of((class_dev), struct unit_directory, class_dev)->device);
161 /* The name here is only so that unit directory hotplug works with old
162 * style hotplug, which only ever did unit directories anyway. */
163 struct class nodemgr_ud_class = {
164 .name = "ieee1394",
165 .release = ud_cls_release,
166 .hotplug = nodemgr_hotplug,
169 static struct hpsb_highlevel nodemgr_highlevel;
172 static void nodemgr_release_ud(struct device *dev)
174 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
176 if (ud->vendor_name_kv)
177 csr1212_release_keyval(ud->vendor_name_kv);
178 if (ud->model_name_kv)
179 csr1212_release_keyval(ud->model_name_kv);
181 kfree(ud);
184 static void nodemgr_release_ne(struct device *dev)
186 struct node_entry *ne = container_of(dev, struct node_entry, device);
188 if (ne->vendor_name_kv)
189 csr1212_release_keyval(ne->vendor_name_kv);
191 kfree(ne);
195 static void nodemgr_release_host(struct device *dev)
197 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
199 csr1212_destroy_csr(host->csr.rom);
201 kfree(host);
204 static int nodemgr_ud_platform_data;
206 static struct device nodemgr_dev_template_ud = {
207 .bus = &ieee1394_bus_type,
208 .release = nodemgr_release_ud,
209 .platform_data = &nodemgr_ud_platform_data,
212 static struct device nodemgr_dev_template_ne = {
213 .bus = &ieee1394_bus_type,
214 .release = nodemgr_release_ne,
217 struct device nodemgr_dev_template_host = {
218 .bus = &ieee1394_bus_type,
219 .release = nodemgr_release_host,
223 #define fw_attr(class, class_type, field, type, format_string) \
224 static ssize_t fw_show_##class##_##field (struct device *dev, char *buf)\
226 class_type *class; \
227 class = container_of(dev, class_type, device); \
228 return sprintf(buf, format_string, (type)class->field); \
230 static struct device_attribute dev_attr_##class##_##field = { \
231 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
232 .show = fw_show_##class##_##field, \
235 #define fw_attr_td(class, class_type, td_kv) \
236 static ssize_t fw_show_##class##_##td_kv (struct device *dev, char *buf)\
238 int len; \
239 class_type *class = container_of(dev, class_type, device); \
240 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
241 memcpy(buf, \
242 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
243 len); \
244 while ((buf + len - 1) == '\0') \
245 len--; \
246 buf[len++] = '\n'; \
247 buf[len] = '\0'; \
248 return len; \
250 static struct device_attribute dev_attr_##class##_##td_kv = { \
251 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
252 .show = fw_show_##class##_##td_kv, \
256 #define fw_drv_attr(field, type, format_string) \
257 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
259 struct hpsb_protocol_driver *driver; \
260 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
261 return sprintf(buf, format_string, (type)driver->field);\
263 static struct driver_attribute driver_attr_drv_##field = { \
264 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
265 .show = fw_drv_show_##field, \
269 static ssize_t fw_show_ne_bus_options(struct device *dev, char *buf)
271 struct node_entry *ne = container_of(dev, struct node_entry, device);
273 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
274 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
275 ne->busopt.irmc,
276 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
277 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
278 ne->busopt.max_rec,
279 ne->busopt.max_rom,
280 ne->busopt.cyc_clk_acc);
282 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
285 static ssize_t fw_show_ne_tlabels_free(struct device *dev, char *buf)
287 struct node_entry *ne = container_of(dev, struct node_entry, device);
288 return sprintf(buf, "%d\n", atomic_read(&ne->tpool->count.count) + 1);
290 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
293 static ssize_t fw_show_ne_tlabels_allocations(struct device *dev, char *buf)
295 struct node_entry *ne = container_of(dev, struct node_entry, device);
296 return sprintf(buf, "%u\n", ne->tpool->allocations);
298 static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
301 static ssize_t fw_show_ne_tlabels_mask(struct device *dev, char *buf)
303 struct node_entry *ne = container_of(dev, struct node_entry, device);
304 #if (BITS_PER_LONG <= 32)
305 return sprintf(buf, "0x%08lx%08lx\n", ne->tpool->pool[0], ne->tpool->pool[1]);
306 #else
307 return sprintf(buf, "0x%016lx\n", ne->tpool->pool[0]);
308 #endif
310 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
313 static ssize_t fw_set_ignore_driver(struct device *dev, const char *buf, size_t count)
315 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
316 int state = simple_strtoul(buf, NULL, 10);
318 if (state == 1) {
319 down_write(&dev->bus->subsys.rwsem);
320 device_release_driver(dev);
321 ud->ignore_driver = 1;
322 up_write(&dev->bus->subsys.rwsem);
323 } else if (!state)
324 ud->ignore_driver = 0;
326 return count;
328 static ssize_t fw_get_ignore_driver(struct device *dev, char *buf)
330 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
332 return sprintf(buf, "%d\n", ud->ignore_driver);
334 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
337 static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
339 struct node_entry *ne;
340 u64 guid = (u64)simple_strtoull(buf, NULL, 16);
342 ne = find_entry_by_guid(guid);
344 if (ne == NULL || !ne->in_limbo)
345 return -EINVAL;
347 nodemgr_remove_ne(ne);
349 return count;
351 static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
353 return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
355 static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
357 static int nodemgr_rescan_bus_thread(void *__unused)
359 /* No userlevel access needed */
360 daemonize("kfwrescan");
362 bus_rescan_devices(&ieee1394_bus_type);
364 return 0;
367 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf, size_t count)
369 int state = simple_strtoul(buf, NULL, 10);
371 /* Don't wait for this, or care about errors. Root could do
372 * something stupid and spawn this a lot of times, but that's
373 * root's fault. */
374 if (state == 1)
375 kernel_thread(nodemgr_rescan_bus_thread, NULL, CLONE_KERNEL);
377 return count;
379 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
381 return sprintf(buf, "You can force a rescan of the bus for "
382 "drivers by writing a 1 to this file\n");
384 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
387 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
389 int state = simple_strtoul(buf, NULL, 10);
391 if (state == 1)
392 ignore_drivers = 1;
393 else if (!state)
394 ignore_drivers = 0;
396 return count;
398 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
400 return sprintf(buf, "%d\n", ignore_drivers);
402 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
405 struct bus_attribute *const fw_bus_attrs[] = {
406 &bus_attr_destroy_node,
407 &bus_attr_rescan,
408 &bus_attr_ignore_drivers,
409 NULL
413 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
414 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
416 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
417 fw_attr_td(ne, struct node_entry, vendor_name_kv)
418 fw_attr(ne, struct node_entry, vendor_oui, const char *, "%s\n")
420 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
421 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
422 fw_attr(ne, struct node_entry, guid_vendor_oui, const char *, "%s\n")
423 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
425 static struct device_attribute *const fw_ne_attrs[] = {
426 &dev_attr_ne_guid,
427 &dev_attr_ne_guid_vendor_id,
428 &dev_attr_ne_capabilities,
429 &dev_attr_ne_vendor_id,
430 &dev_attr_ne_nodeid,
431 &dev_attr_bus_options,
432 &dev_attr_tlabels_free,
433 &dev_attr_tlabels_allocations,
434 &dev_attr_tlabels_mask,
439 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
440 fw_attr(ud, struct unit_directory, length, int, "%d\n")
441 /* These are all dependent on the value being provided */
442 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
443 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
444 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
445 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
446 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
447 fw_attr(ud, struct unit_directory, vendor_oui, const char *, "%s\n")
448 fw_attr_td(ud, struct unit_directory, model_name_kv)
450 static struct device_attribute *const fw_ud_attrs[] = {
451 &dev_attr_ud_address,
452 &dev_attr_ud_length,
453 &dev_attr_ignore_driver,
457 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
458 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
459 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
460 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
461 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
462 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
463 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
464 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
466 static struct device_attribute *const fw_host_attrs[] = {
467 &dev_attr_host_node_count,
468 &dev_attr_host_selfid_count,
469 &dev_attr_host_nodes_active,
470 &dev_attr_host_in_bus_reset,
471 &dev_attr_host_is_root,
472 &dev_attr_host_is_cycmst,
473 &dev_attr_host_is_irm,
474 &dev_attr_host_is_busmgr,
478 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
480 struct hpsb_protocol_driver *driver;
481 struct ieee1394_device_id *id;
482 int length = 0;
483 char *scratch = buf;
485 driver = container_of(drv, struct hpsb_protocol_driver, driver);
487 for (id = driver->id_table; id->match_flags != 0; id++) {
488 int need_coma = 0;
490 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
491 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
492 scratch = buf + length;
493 need_coma++;
496 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
497 length += sprintf(scratch, "%smodel_id=0x%06x",
498 need_coma++ ? "," : "",
499 id->model_id);
500 scratch = buf + length;
503 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
504 length += sprintf(scratch, "%sspecifier_id=0x%06x",
505 need_coma++ ? "," : "",
506 id->specifier_id);
507 scratch = buf + length;
510 if (id->match_flags & IEEE1394_MATCH_VERSION) {
511 length += sprintf(scratch, "%sversion=0x%06x",
512 need_coma++ ? "," : "",
513 id->version);
514 scratch = buf + length;
517 if (need_coma) {
518 *scratch++ = '\n';
519 length++;
523 return length;
525 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
528 fw_drv_attr(name, const char *, "%s\n")
530 static struct driver_attribute *const fw_drv_attrs[] = {
531 &driver_attr_drv_name,
532 &driver_attr_device_ids,
536 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
538 struct device_driver *drv = &driver->driver;
539 int i;
541 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
542 driver_create_file(drv, fw_drv_attrs[i]);
546 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
548 struct device_driver *drv = &driver->driver;
549 int i;
551 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
552 driver_remove_file(drv, fw_drv_attrs[i]);
556 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
558 struct device *dev = &ne->device;
559 int i;
561 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
562 device_create_file(dev, fw_ne_attrs[i]);
566 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
568 struct device *dev = &host->device;
569 int i;
571 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
572 device_create_file(dev, fw_host_attrs[i]);
576 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid);
578 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
580 struct device *dev = &host->device;
581 struct node_entry *ne;
583 sysfs_remove_link(&dev->kobj, "irm_id");
584 sysfs_remove_link(&dev->kobj, "busmgr_id");
585 sysfs_remove_link(&dev->kobj, "host_id");
587 if ((ne = find_entry_by_nodeid(host, host->irm_id)))
588 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id");
589 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)))
590 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id");
591 if ((ne = find_entry_by_nodeid(host, host->node_id)))
592 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id");
595 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
597 struct device *dev = &ud->device;
598 int i;
600 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
601 device_create_file(dev, fw_ud_attrs[i]);
603 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
604 device_create_file(dev, &dev_attr_ud_specifier_id);
606 if (ud->flags & UNIT_DIRECTORY_VERSION)
607 device_create_file(dev, &dev_attr_ud_version);
609 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
610 device_create_file(dev, &dev_attr_ud_vendor_id);
611 if (ud->vendor_name_kv)
612 device_create_file(dev, &dev_attr_ud_vendor_name_kv);
615 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
616 device_create_file(dev, &dev_attr_ud_model_id);
617 if (ud->model_name_kv)
618 device_create_file(dev, &dev_attr_ud_model_name_kv);
623 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
625 struct hpsb_protocol_driver *driver;
626 struct unit_directory *ud;
627 struct ieee1394_device_id *id;
629 /* We only match unit directories */
630 if (dev->platform_data != &nodemgr_ud_platform_data)
631 return 0;
633 ud = container_of(dev, struct unit_directory, device);
634 driver = container_of(drv, struct hpsb_protocol_driver, driver);
636 if (ud->ne->in_limbo || ud->ignore_driver)
637 return 0;
639 for (id = driver->id_table; id->match_flags != 0; id++) {
640 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
641 id->vendor_id != ud->vendor_id)
642 continue;
644 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
645 id->model_id != ud->model_id)
646 continue;
648 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
649 id->specifier_id != ud->specifier_id)
650 continue;
652 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
653 id->version != ud->version)
654 continue;
656 return 1;
659 return 0;
663 static void nodemgr_remove_uds(struct node_entry *ne)
665 struct class_device *cdev, *next;
666 struct unit_directory *ud;
668 list_for_each_entry_safe(cdev, next, &nodemgr_ud_class.children, node) {
669 ud = container_of(cdev, struct unit_directory, class_dev);
671 if (ud->ne != ne)
672 continue;
674 class_device_unregister(&ud->class_dev);
675 device_unregister(&ud->device);
680 static void nodemgr_remove_ne(struct node_entry *ne)
682 struct device *dev = &ne->device;
684 dev = get_device(&ne->device);
685 if (!dev)
686 return;
688 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
689 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
691 nodemgr_remove_uds(ne);
693 class_device_unregister(&ne->class_dev);
694 device_unregister(dev);
696 put_device(dev);
700 static void nodemgr_remove_host_dev(struct device *dev)
702 struct device *ne_dev, *next;
704 list_for_each_entry_safe(ne_dev, next, &dev->children, node)
705 nodemgr_remove_ne(container_of(ne_dev, struct node_entry, device));
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;
837 /* This implementation currently only scans the config rom and its
838 * immediate unit directories looking for software_id and
839 * software_version entries, in order to get driver autoloading working. */
840 static struct unit_directory *nodemgr_process_unit_directory
841 (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
842 unsigned int *id, struct unit_directory *parent)
844 struct unit_directory *ud;
845 struct unit_directory *ud_temp = NULL;
846 struct csr1212_dentry *dentry;
847 struct csr1212_keyval *kv;
848 u8 last_key_id = 0;
850 ud = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
851 if (!ud)
852 goto unit_directory_error;
854 memset (ud, 0, sizeof(struct unit_directory));
856 ud->ne = ne;
857 ud->ignore_driver = ignore_drivers;
858 ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
859 ud->ud_kv = ud_kv;
860 ud->id = (*id)++;
862 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
863 switch (kv->key.id) {
864 case CSR1212_KV_ID_VENDOR:
865 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
866 ud->vendor_id = kv->value.immediate;
867 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
869 if (ud->vendor_id)
870 ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
872 break;
874 case CSR1212_KV_ID_MODEL:
875 ud->model_id = kv->value.immediate;
876 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
877 break;
879 case CSR1212_KV_ID_SPECIFIER_ID:
880 ud->specifier_id = kv->value.immediate;
881 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
882 break;
884 case CSR1212_KV_ID_VERSION:
885 ud->version = kv->value.immediate;
886 ud->flags |= UNIT_DIRECTORY_VERSION;
887 break;
889 case CSR1212_KV_ID_DESCRIPTOR:
890 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
891 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
892 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
893 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
894 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
895 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
896 switch (last_key_id) {
897 case CSR1212_KV_ID_VENDOR:
898 ud->vendor_name_kv = kv;
899 csr1212_keep_keyval(kv);
900 break;
902 case CSR1212_KV_ID_MODEL:
903 ud->model_name_kv = kv;
904 csr1212_keep_keyval(kv);
905 break;
908 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
909 break;
911 case CSR1212_KV_ID_DEPENDENT_INFO:
912 if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
913 /* This should really be done in SBP2 as this is
914 * doing SBP2 specific parsing. */
915 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
916 ud_temp = nodemgr_process_unit_directory(hi, ne, kv, id,
917 parent);
919 if (ud_temp == NULL)
920 break;
922 /* inherit unspecified values */
923 if ((ud->flags & UNIT_DIRECTORY_VENDOR_ID) &&
924 !(ud_temp->flags & UNIT_DIRECTORY_VENDOR_ID))
926 ud_temp->flags |= UNIT_DIRECTORY_VENDOR_ID;
927 ud_temp->vendor_id = ud->vendor_id;
928 ud_temp->vendor_oui = ud->vendor_oui;
930 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
931 !(ud_temp->flags & UNIT_DIRECTORY_MODEL_ID))
933 ud_temp->flags |= UNIT_DIRECTORY_MODEL_ID;
934 ud_temp->model_id = ud->model_id;
936 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
937 !(ud_temp->flags & UNIT_DIRECTORY_SPECIFIER_ID))
939 ud_temp->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
940 ud_temp->specifier_id = ud->specifier_id;
942 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
943 !(ud_temp->flags & UNIT_DIRECTORY_VERSION))
945 ud_temp->flags |= UNIT_DIRECTORY_VERSION;
946 ud_temp->version = ud->version;
950 break;
952 default:
953 break;
955 last_key_id = kv->key.id;
958 memcpy(&ud->device, &nodemgr_dev_template_ud,
959 sizeof(ud->device));
961 if (parent) {
962 ud->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
963 ud->device.parent = &parent->device;
964 } else
965 ud->device.parent = &ne->device;
967 snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
968 ne->device.bus_id, ud->id);
970 ud->class_dev.dev = &ud->device;
971 ud->class_dev.class = &nodemgr_ud_class;
972 snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
973 ne->device.bus_id, ud->id);
975 device_register(&ud->device);
976 class_device_register(&ud->class_dev);
977 get_device(&ud->device);
979 if (ud->vendor_oui)
980 device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
981 nodemgr_create_ud_dev_files(ud);
983 return ud;
985 unit_directory_error:
986 if (ud != NULL)
987 kfree(ud);
988 return NULL;
992 static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
994 unsigned int ud_id = 0;
995 struct csr1212_dentry *dentry;
996 struct csr1212_keyval *kv;
997 u8 last_key_id = 0;
999 ne->needs_probe = 0;
1001 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1002 switch (kv->key.id) {
1003 case CSR1212_KV_ID_VENDOR:
1004 ne->vendor_id = kv->value.immediate;
1006 if (ne->vendor_id)
1007 ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
1008 break;
1010 case CSR1212_KV_ID_NODE_CAPABILITIES:
1011 ne->capabilities = kv->value.immediate;
1012 break;
1014 case CSR1212_KV_ID_UNIT:
1015 nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
1016 break;
1018 case CSR1212_KV_ID_DESCRIPTOR:
1019 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1020 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1021 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1022 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1023 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1024 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1025 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1026 ne->vendor_name_kv = kv;
1027 csr1212_keep_keyval(kv);
1030 break;
1032 last_key_id = kv->key.id;
1035 if (ne->vendor_oui)
1036 device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
1037 if (ne->vendor_name_kv)
1038 device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
1041 #ifdef CONFIG_HOTPLUG
1043 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
1044 char *buffer, int buffer_size)
1046 struct unit_directory *ud;
1047 int i = 0;
1048 int length = 0;
1050 if (!cdev)
1051 return -ENODEV;
1053 ud = container_of(cdev, struct unit_directory, class_dev);
1055 if (ud->ne->in_limbo || ud->ignore_driver)
1056 return -ENODEV;
1058 #define PUT_ENVP(fmt,val) \
1059 do { \
1060 int printed; \
1061 envp[i++] = buffer; \
1062 printed = snprintf(buffer, buffer_size - length, \
1063 fmt, val); \
1064 if ((buffer_size - (length+printed) <= 0) || (i >= num_envp)) \
1065 return -ENOMEM; \
1066 length += printed+1; \
1067 buffer += printed+1; \
1068 } while (0)
1070 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1071 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1072 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1073 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1074 PUT_ENVP("VERSION=%06x", ud->version);
1076 #undef PUT_ENVP
1078 envp[i] = NULL;
1080 return 0;
1083 #else
1085 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
1086 char *buffer, int buffer_size)
1088 return -ENODEV;
1091 #endif /* CONFIG_HOTPLUG */
1094 int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
1096 int ret;
1098 /* This will cause a probe for devices */
1099 ret = driver_register(&driver->driver);
1100 if (!ret)
1101 nodemgr_create_drv_files(driver);
1103 return ret;
1106 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1108 nodemgr_remove_drv_files(driver);
1109 /* This will subsequently disconnect all devices that our driver
1110 * is attached to. */
1111 driver_unregister(&driver->driver);
1116 * This function updates nodes that were present on the bus before the
1117 * reset and still are after the reset. The nodeid and the config rom
1118 * may have changed, and the drivers managing this device must be
1119 * informed that this device just went through a bus reset, to allow
1120 * the to take whatever actions required.
1122 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1123 struct host_info *hi, nodeid_t nodeid,
1124 unsigned int generation)
1126 if (ne->nodeid != nodeid) {
1127 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1128 NODE_BUS_ARGS(ne->host, ne->nodeid),
1129 NODE_BUS_ARGS(ne->host, nodeid));
1130 ne->nodeid = nodeid;
1133 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1134 kfree(ne->csr->private);
1135 csr1212_destroy_csr(ne->csr);
1136 ne->csr = csr;
1138 /* If the node's configrom generation has changed, we
1139 * unregister all the unit directories. */
1140 nodemgr_remove_uds(ne);
1142 nodemgr_update_bus_options(ne);
1144 /* Mark the node as new, so it gets re-probed */
1145 ne->needs_probe = 1;
1148 if (ne->in_limbo)
1149 nodemgr_resume_ne(ne);
1151 /* Mark the node current */
1152 ne->generation = generation;
1157 static void nodemgr_node_scan_one(struct host_info *hi,
1158 nodeid_t nodeid, int generation)
1160 struct hpsb_host *host = hi->host;
1161 struct node_entry *ne;
1162 octlet_t guid;
1163 struct csr1212_csr *csr;
1164 struct nodemgr_csr_info *ci;
1166 ci = kmalloc(sizeof(struct nodemgr_csr_info), GFP_KERNEL);
1167 if (!ci)
1168 return;
1170 ci->host = host;
1171 ci->nodeid = nodeid;
1172 ci->generation = generation;
1174 /* We need to detect when the ConfigROM's generation has changed,
1175 * so we only update the node's info when it needs to be. */
1177 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1178 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1179 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1180 NODE_BUS_ARGS(host, nodeid));
1181 if (csr)
1182 csr1212_destroy_csr(csr);
1183 kfree(ci);
1184 return;
1187 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1188 /* This isn't a 1394 device, but we let it slide. There
1189 * was a report of a device with broken firmware which
1190 * reported '2394' instead of '1394', which is obviously a
1191 * mistake. One would hope that a non-1394 device never
1192 * gets connected to Firewire bus. If someone does, we
1193 * shouldn't be held responsible, so we'll allow it with a
1194 * warning. */
1195 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1196 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1199 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1200 ne = find_entry_by_guid(guid);
1202 if (ne && ne->host != host && ne->in_limbo) {
1203 /* Must have moved this device from one host to another */
1204 nodemgr_remove_ne(ne);
1205 ne = NULL;
1208 if (!ne)
1209 nodemgr_create_node(guid, csr, hi, nodeid, generation);
1210 else
1211 nodemgr_update_node(ne, csr, hi, nodeid, generation);
1213 return;
1217 static void nodemgr_node_scan(struct host_info *hi, int generation)
1219 int count;
1220 struct hpsb_host *host = hi->host;
1221 struct selfid *sid = (struct selfid *)host->topology_map;
1222 nodeid_t nodeid = LOCAL_BUS;
1224 /* Scan each node on the bus */
1225 for (count = host->selfid_count; count; count--, sid++) {
1226 if (sid->extended)
1227 continue;
1229 if (!sid->link_active) {
1230 nodeid++;
1231 continue;
1233 nodemgr_node_scan_one(hi, nodeid++, generation);
1238 static void nodemgr_suspend_ne(struct node_entry *ne)
1240 struct class_device *cdev;
1241 struct unit_directory *ud;
1243 HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1244 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1246 ne->in_limbo = 1;
1247 device_create_file(&ne->device, &dev_attr_ne_in_limbo);
1249 down_write(&ne->device.bus->subsys.rwsem);
1250 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1251 ud = container_of(cdev, struct unit_directory, class_dev);
1253 if (ud->ne != ne)
1254 continue;
1256 if (ud->device.driver &&
1257 (!ud->device.driver->suspend ||
1258 ud->device.driver->suspend(&ud->device, 0, 0)))
1259 device_release_driver(&ud->device);
1261 up_write(&ne->device.bus->subsys.rwsem);
1265 static void nodemgr_resume_ne(struct node_entry *ne)
1267 struct class_device *cdev;
1268 struct unit_directory *ud;
1270 ne->in_limbo = 0;
1271 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1273 down_read(&ne->device.bus->subsys.rwsem);
1274 list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1275 ud = container_of(cdev, struct unit_directory, class_dev);
1277 if (ud->ne != ne)
1278 continue;
1280 if (ud->device.driver && ud->device.driver->resume)
1281 ud->device.driver->resume(&ud->device, 0);
1283 up_read(&ne->device.bus->subsys.rwsem);
1285 HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1286 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1290 static void nodemgr_update_pdrv(struct node_entry *ne)
1292 struct unit_directory *ud;
1293 struct hpsb_protocol_driver *pdrv;
1294 struct class *class = &nodemgr_ud_class;
1295 struct class_device *cdev;
1297 down_read(&class->subsys.rwsem);
1298 list_for_each_entry(cdev, &class->children, node) {
1299 ud = container_of(cdev, struct unit_directory, class_dev);
1300 if (ud->ne != ne || !ud->device.driver)
1301 continue;
1303 pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
1305 if (pdrv->update && pdrv->update(ud)) {
1306 down_write(&ud->device.bus->subsys.rwsem);
1307 device_release_driver(&ud->device);
1308 up_write(&ud->device.bus->subsys.rwsem);
1311 up_read(&class->subsys.rwsem);
1315 static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
1317 struct device *dev;
1319 if (ne->host != hi->host || ne->in_limbo)
1320 return;
1322 dev = get_device(&ne->device);
1323 if (!dev)
1324 return;
1326 /* If "needs_probe", then this is either a new or changed node we
1327 * rescan totally. If the generation matches for an existing node
1328 * (one that existed prior to the bus reset) we send update calls
1329 * down to the drivers. Otherwise, this is a dead node and we
1330 * suspend it. */
1331 if (ne->needs_probe)
1332 nodemgr_process_root_directory(hi, ne);
1333 else if (ne->generation == generation)
1334 nodemgr_update_pdrv(ne);
1335 else
1336 nodemgr_suspend_ne(ne);
1338 put_device(dev);
1342 static void nodemgr_node_probe(struct host_info *hi, int generation)
1344 struct hpsb_host *host = hi->host;
1345 struct class *class = &nodemgr_ne_class;
1346 struct class_device *cdev;
1348 /* Do some processing of the nodes we've probed. This pulls them
1349 * into the sysfs layer if needed, and can result in processing of
1350 * unit-directories, or just updating the node and it's
1351 * unit-directories. */
1352 down_read(&class->subsys.rwsem);
1353 list_for_each_entry(cdev, &class->children, node)
1354 nodemgr_probe_ne(hi, container_of(cdev, struct node_entry, class_dev), generation);
1355 up_read(&class->subsys.rwsem);
1358 /* If we had a bus reset while we were scanning the bus, it is
1359 * possible that we did not probe all nodes. In that case, we
1360 * skip the clean up for now, since we could remove nodes that
1361 * were still on the bus. The bus reset increased hi->reset_sem,
1362 * so there's a bus scan pending which will do the clean up
1363 * eventually.
1365 * Now let's tell the bus to rescan our devices. This may seem
1366 * like overhead, but the driver-model core will only scan a
1367 * device for a driver when either the device is added, or when a
1368 * new driver is added. A bus reset is a good reason to rescan
1369 * devices that were there before. For example, an sbp2 device
1370 * may become available for login, if the host that held it was
1371 * just removed. */
1373 if (generation == get_hpsb_generation(host))
1374 bus_rescan_devices(&ieee1394_bus_type);
1376 return;
1379 /* Because we are a 1394a-2000 compliant IRM, we need to inform all the other
1380 * nodes of the broadcast channel. (Really we're only setting the validity
1381 * bit). Other IRM responsibilities go in here as well. */
1382 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1384 quadlet_t bc;
1386 /* if irm_id == -1 then there is no IRM on this bus */
1387 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1388 return 1;
1390 host->csr.broadcast_channel |= 0x40000000; /* set validity bit */
1392 bc = cpu_to_be32(host->csr.broadcast_channel);
1394 hpsb_write(host, LOCAL_BUS | ALL_NODES, get_hpsb_generation(host),
1395 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1396 &bc, sizeof(quadlet_t));
1398 /* If there is no bus manager then we should set the root node's
1399 * force_root bit to promote bus stability per the 1394
1400 * spec. (8.4.2.6) */
1401 if (host->busmgr_id == 0xffff && host->node_count > 1)
1403 u16 root_node = host->node_count - 1;
1404 struct node_entry *ne = find_entry_by_nodeid(host, root_node | LOCAL_BUS);
1406 if (ne && ne->busopt.cmc)
1407 hpsb_send_phy_config(host, root_node, -1);
1408 else {
1409 HPSB_DEBUG("The root node is not cycle master capable; "
1410 "selecting a new root node and resetting...");
1412 if (cycles >= 5) {
1413 /* Oh screw it! Just leave the bus as it is */
1414 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1415 return 1;
1418 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1419 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1421 return 0;
1425 return 1;
1428 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1429 * everything we can do, otherwise issue a bus reset and try to become the IRM
1430 * ourselves. */
1431 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1433 quadlet_t bc;
1434 int status;
1436 if (host->is_irm)
1437 return 1;
1439 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1440 get_hpsb_generation(host),
1441 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1442 &bc, sizeof(quadlet_t));
1444 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1445 /* The current irm node does not have a valid BROADCAST_CHANNEL
1446 * register and we do, so reset the bus with force_root set */
1447 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1449 if (cycles >= 5) {
1450 /* Oh screw it! Just leave the bus as it is */
1451 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1452 return 1;
1455 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1456 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1458 return 0;
1461 return 1;
1464 static int nodemgr_host_thread(void *__hi)
1466 struct host_info *hi = (struct host_info *)__hi;
1467 struct hpsb_host *host = hi->host;
1468 int reset_cycles = 0;
1470 /* No userlevel access needed */
1471 daemonize(hi->daemon_name);
1473 /* Setup our device-model entries */
1474 nodemgr_create_host_dev_files(host);
1476 /* Sit and wait for a signal to probe the nodes on the bus. This
1477 * happens when we get a bus reset. */
1478 while (1) {
1479 unsigned int generation = 0;
1480 int i;
1482 if (down_interruptible(&hi->reset_sem) ||
1483 down_interruptible(&nodemgr_serialize)) {
1484 if (current->flags & PF_FREEZE) {
1485 refrigerator(0);
1486 continue;
1488 printk("NodeMgr: received unexpected signal?!\n" );
1489 break;
1492 if (hi->kill_me)
1493 break;
1495 /* Pause for 1/4 second in 1/16 second intervals,
1496 * to make sure things settle down. */
1497 for (i = 0; i < 4 ; i++) {
1498 set_current_state(TASK_INTERRUPTIBLE);
1499 if (schedule_timeout(HZ/16)) {
1500 up(&nodemgr_serialize);
1501 goto caught_signal;
1504 /* Now get the generation in which the node ID's we collect
1505 * are valid. During the bus scan we will use this generation
1506 * for the read transactions, so that if another reset occurs
1507 * during the scan the transactions will fail instead of
1508 * returning bogus data. */
1509 generation = get_hpsb_generation(host);
1511 /* If we get a reset before we are done waiting, then
1512 * start the the waiting over again */
1513 while (!down_trylock(&hi->reset_sem))
1514 i = 0;
1516 /* Check the kill_me again */
1517 if (hi->kill_me)
1518 goto caught_signal;
1521 if (!nodemgr_check_irm_capability(host, reset_cycles)) {
1522 reset_cycles++;
1523 up(&nodemgr_serialize);
1524 continue;
1527 /* Scan our nodes to get the bus options and create node
1528 * entries. This does not do the sysfs stuff, since that
1529 * would trigger hotplug callbacks and such, which is a
1530 * bad idea at this point. */
1531 nodemgr_node_scan(hi, generation);
1532 if (!nodemgr_do_irm_duties(host, reset_cycles)) {
1533 reset_cycles++;
1534 up(&nodemgr_serialize);
1535 continue;
1538 reset_cycles = 0;
1540 /* This actually does the full probe, with sysfs
1541 * registration. */
1542 nodemgr_node_probe(hi, generation);
1544 /* Update some of our sysfs symlinks */
1545 nodemgr_update_host_dev_links(host);
1547 up(&nodemgr_serialize);
1550 caught_signal:
1551 HPSB_VERBOSE("NodeMgr: Exiting thread");
1553 complete_and_exit(&hi->exited, 0);
1556 struct node_entry *hpsb_guid_get_entry(u64 guid)
1558 struct node_entry *ne;
1560 down(&nodemgr_serialize);
1561 ne = find_entry_by_guid(guid);
1562 up(&nodemgr_serialize);
1564 return ne;
1567 struct node_entry *hpsb_nodeid_get_entry(struct hpsb_host *host, nodeid_t nodeid)
1569 struct node_entry *ne;
1571 down(&nodemgr_serialize);
1572 ne = find_entry_by_nodeid(host, nodeid);
1573 up(&nodemgr_serialize);
1575 return ne;
1579 int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
1581 struct class *class = &hpsb_host_class;
1582 struct class_device *cdev;
1583 struct hpsb_host *host;
1584 int error = 0;
1586 down_read(&class->subsys.rwsem);
1587 list_for_each_entry(cdev, &class->children, node) {
1588 host = container_of(cdev, struct hpsb_host, class_dev);
1590 if ((error = cb(host, __data)))
1591 break;
1593 up_read(&class->subsys.rwsem);
1595 return error;
1598 /* The following four convenience functions use a struct node_entry
1599 * for addressing a node on the bus. They are intended for use by any
1600 * process context, not just the nodemgr thread, so we need to be a
1601 * little careful when reading out the node ID and generation. The
1602 * thing that can go wrong is that we get the node ID, then a bus
1603 * reset occurs, and then we read the generation. The node ID is
1604 * possibly invalid, but the generation is current, and we end up
1605 * sending a packet to a the wrong node.
1607 * The solution is to make sure we read the generation first, so that
1608 * if a reset occurs in the process, we end up with a stale generation
1609 * and the transactions will fail instead of silently using wrong node
1610 * ID's.
1613 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
1615 pkt->host = ne->host;
1616 pkt->generation = ne->generation;
1617 barrier();
1618 pkt->node_id = ne->nodeid;
1621 int hpsb_node_read(struct node_entry *ne, u64 addr,
1622 quadlet_t *buffer, size_t length)
1624 unsigned int generation = ne->generation;
1626 barrier();
1627 return hpsb_read(ne->host, ne->nodeid, generation,
1628 addr, buffer, length);
1631 int hpsb_node_write(struct node_entry *ne, u64 addr,
1632 quadlet_t *buffer, size_t length)
1634 unsigned int generation = ne->generation;
1636 barrier();
1637 return hpsb_write(ne->host, ne->nodeid, generation,
1638 addr, buffer, length);
1641 int hpsb_node_lock(struct node_entry *ne, u64 addr,
1642 int extcode, quadlet_t *data, quadlet_t arg)
1644 unsigned int generation = ne->generation;
1646 barrier();
1647 return hpsb_lock(ne->host, ne->nodeid, generation,
1648 addr, extcode, data, arg);
1651 static void nodemgr_add_host(struct hpsb_host *host)
1653 struct host_info *hi;
1655 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1657 if (!hi) {
1658 HPSB_ERR ("NodeMgr: out of memory in add host");
1659 return;
1662 hi->host = host;
1663 init_completion(&hi->exited);
1664 sema_init(&hi->reset_sem, 0);
1666 sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
1668 hi->pid = kernel_thread(nodemgr_host_thread, hi, CLONE_KERNEL);
1670 if (hi->pid < 0) {
1671 HPSB_ERR ("NodeMgr: failed to start %s thread for %s",
1672 hi->daemon_name, host->driver->name);
1673 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
1674 return;
1677 return;
1680 static void nodemgr_host_reset(struct hpsb_host *host)
1682 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1684 if (hi != NULL) {
1685 HPSB_VERBOSE("NodeMgr: Processing host reset for %s", hi->daemon_name);
1686 up(&hi->reset_sem);
1687 } else
1688 HPSB_ERR ("NodeMgr: could not process reset of unused host");
1690 return;
1693 static void nodemgr_remove_host(struct hpsb_host *host)
1695 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1697 if (hi) {
1698 if (hi->pid >= 0) {
1699 hi->kill_me = 1;
1700 mb();
1701 up(&hi->reset_sem);
1702 wait_for_completion(&hi->exited);
1703 nodemgr_remove_host_dev(&host->device);
1705 } else
1706 HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
1707 host->driver->name);
1709 return;
1712 static struct hpsb_highlevel nodemgr_highlevel = {
1713 .name = "Node manager",
1714 .add_host = nodemgr_add_host,
1715 .host_reset = nodemgr_host_reset,
1716 .remove_host = nodemgr_remove_host,
1719 int init_ieee1394_nodemgr(void)
1721 int ret;
1723 ret = class_register(&nodemgr_ne_class);
1724 if (ret < 0)
1725 return ret;
1727 ret = class_register(&nodemgr_ud_class);
1728 if (ret < 0) {
1729 class_unregister(&nodemgr_ne_class);
1730 return ret;
1733 hpsb_register_highlevel(&nodemgr_highlevel);
1735 return 0;
1738 void cleanup_ieee1394_nodemgr(void)
1740 hpsb_unregister_highlevel(&nodemgr_highlevel);
1742 class_unregister(&nodemgr_ud_class);
1743 class_unregister(&nodemgr_ne_class);