vserver 1.9.5.x5
[linux-2.6.git] / drivers / ieee1394 / nodemgr.c
1 /*
2  * Node information (ConfigROM) collection and management.
3  *
4  * Copyright (C) 2000           Andreas E. Bombe
5  *               2001-2003      Ben Collins <bcollins@debian.net>
6  *
7  * This code is licensed under the GPL.  See the file COPYING in the root
8  * directory of the kernel sources for details.
9  */
10
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>
23
24 #include "ieee1394_types.h"
25 #include "ieee1394.h"
26 #include "hosts.h"
27 #include "ieee1394_transactions.h"
28 #include "highlevel.h"
29 #include "csr.h"
30 #include "nodemgr.h"
31
32 static int ignore_drivers = 0;
33 module_param(ignore_drivers, int, 0444);
34 MODULE_PARM_DESC(ignore_drivers, "Disable automatic probing for drivers.");
35
36 struct nodemgr_csr_info {
37         struct hpsb_host *host;
38         nodeid_t nodeid;
39         unsigned int generation;
40 };
41
42
43 static char *nodemgr_find_oui_name(int oui)
44 {
45 #ifdef CONFIG_IEEE1394_OUI_DB
46         extern struct oui_list_struct {
47                 int oui;
48                 char *name;
49         } oui_list[];
50         int i;
51
52         for (i = 0; oui_list[i].name; i++)
53                 if (oui_list[i].oui == oui)
54                         return oui_list[i].name;
55 #endif
56         return NULL;
57 }
58
59
60 static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
61                             void *buffer, void *__ci)
62 {
63         struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
64         int i, ret = 0;
65
66         for (i = 0; i < 3; i++) {
67                 ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
68                                 buffer, length);
69                 if (!ret)
70                         break;
71
72                 if (msleep_interruptible(334))
73                         return -EINTR;
74         }
75
76         return ret;
77 }
78
79 static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
80 {
81         return (CSR1212_BE32_TO_CPU(bus_info_data[2]) >> 8) & 0x3;
82 }
83
84 static struct csr1212_bus_ops nodemgr_csr_ops = {
85         .bus_read =     nodemgr_bus_read,
86         .get_max_rom =  nodemgr_get_max_rom
87 };
88
89
90 /*
91  * Basically what we do here is start off retrieving the bus_info block.
92  * From there will fill in some info about the node, verify it is of IEEE
93  * 1394 type, and that the crc checks out ok. After that we start off with
94  * the root directory, and subdirectories. To do this, we retrieve the
95  * quadlet header for a directory, find out the length, and retrieve the
96  * complete directory entry (be it a leaf or a directory). We then process
97  * it and add the info to our structure for that particular node.
98  *
99  * We verify CRC's along the way for each directory/block/leaf. The entire
100  * node structure is generic, and simply stores the information in a way
101  * that's easy to parse by the protocol interface.
102  */
103
104 /*
105  * The nodemgr relies heavily on the Driver Model for device callbacks and
106  * driver/device mappings. The old nodemgr used to handle all this itself,
107  * but now we are much simpler because of the LDM.
108  */
109
110 static DECLARE_MUTEX(nodemgr_serialize);
111
112 struct host_info {
113         struct hpsb_host *host;
114         struct list_head list;
115         struct completion exited;
116         struct semaphore reset_sem;
117         int pid;
118         char daemon_name[15];
119         int kill_me;
120 };
121
122 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
123 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
124                            char *buffer, int buffer_size);
125 static void nodemgr_resume_ne(struct node_entry *ne);
126 static void nodemgr_remove_ne(struct node_entry *ne);
127 static struct node_entry *find_entry_by_guid(u64 guid);
128
129 struct bus_type ieee1394_bus_type = {
130         .name           = "ieee1394",
131         .match          = nodemgr_bus_match,
132 };
133
134 static void host_cls_release(struct class_device *class_dev)
135 {
136         put_device(&container_of((class_dev), struct hpsb_host, class_dev)->device);
137 }
138
139 struct class hpsb_host_class = {
140         .name           = "ieee1394_host",
141         .release        = host_cls_release,
142 };
143
144 static void ne_cls_release(struct class_device *class_dev)
145 {
146         put_device(&container_of((class_dev), struct node_entry, class_dev)->device);
147 }
148
149 struct class nodemgr_ne_class = {
150         .name           = "ieee1394_node",
151         .release        = ne_cls_release,
152 };
153
154 static void ud_cls_release(struct class_device *class_dev)
155 {
156         put_device(&container_of((class_dev), struct unit_directory, class_dev)->device);
157 }
158
159 /* The name here is only so that unit directory hotplug works with old
160  * style hotplug, which only ever did unit directories anyway. */
161 struct class nodemgr_ud_class = {
162         .name           = "ieee1394",
163         .release        = ud_cls_release,
164         .hotplug        = nodemgr_hotplug,
165 };
166
167 static struct hpsb_highlevel nodemgr_highlevel;
168
169
170 static void nodemgr_release_ud(struct device *dev)
171 {
172         struct unit_directory *ud = container_of(dev, struct unit_directory, device);
173
174         if (ud->vendor_name_kv)
175                 csr1212_release_keyval(ud->vendor_name_kv);
176         if (ud->model_name_kv)
177                 csr1212_release_keyval(ud->model_name_kv);
178
179         kfree(ud);
180 }
181
182 static void nodemgr_release_ne(struct device *dev)
183 {
184         struct node_entry *ne = container_of(dev, struct node_entry, device);
185
186         if (ne->vendor_name_kv)
187                 csr1212_release_keyval(ne->vendor_name_kv);
188
189         kfree(ne);
190 }
191
192
193 static void nodemgr_release_host(struct device *dev)
194 {
195         struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
196
197         csr1212_destroy_csr(host->csr.rom);
198
199         kfree(host);
200 }
201
202 static int nodemgr_ud_platform_data;
203
204 static struct device nodemgr_dev_template_ud = {
205         .bus            = &ieee1394_bus_type,
206         .release        = nodemgr_release_ud,
207         .platform_data  = &nodemgr_ud_platform_data,
208 };
209
210 static struct device nodemgr_dev_template_ne = {
211         .bus            = &ieee1394_bus_type,
212         .release        = nodemgr_release_ne,
213 };
214
215 struct device nodemgr_dev_template_host = {
216         .bus            = &ieee1394_bus_type,
217         .release        = nodemgr_release_host,
218 };
219
220
221 #define fw_attr(class, class_type, field, type, format_string)          \
222 static ssize_t fw_show_##class##_##field (struct device *dev, char *buf)\
223 {                                                                       \
224         class_type *class;                                              \
225         class = container_of(dev, class_type, device);                  \
226         return sprintf(buf, format_string, (type)class->field);         \
227 }                                                                       \
228 static struct device_attribute dev_attr_##class##_##field = {           \
229         .attr = {.name = __stringify(field), .mode = S_IRUGO },         \
230         .show   = fw_show_##class##_##field,                            \
231 };
232
233 #define fw_attr_td(class, class_type, td_kv)                            \
234 static ssize_t fw_show_##class##_##td_kv (struct device *dev, char *buf)\
235 {                                                                       \
236         int len;                                                        \
237         class_type *class = container_of(dev, class_type, device);      \
238         len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t);   \
239         memcpy(buf,                                                     \
240                CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv),      \
241                len);                                                    \
242         while ((buf + len - 1) == '\0')                                 \
243                 len--;                                                  \
244         buf[len++] = '\n';                                              \
245         buf[len] = '\0';                                                \
246         return len;                                                     \
247 }                                                                       \
248 static struct device_attribute dev_attr_##class##_##td_kv = {           \
249         .attr = {.name = __stringify(td_kv), .mode = S_IRUGO },         \
250         .show   = fw_show_##class##_##td_kv,                            \
251 };
252
253
254 #define fw_drv_attr(field, type, format_string)                 \
255 static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
256 {                                                               \
257         struct hpsb_protocol_driver *driver;                    \
258         driver = container_of(drv, struct hpsb_protocol_driver, driver); \
259         return sprintf(buf, format_string, (type)driver->field);\
260 }                                                               \
261 static struct driver_attribute driver_attr_drv_##field = {      \
262         .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
263         .show   = fw_drv_show_##field,                          \
264 };
265
266
267 static ssize_t fw_show_ne_bus_options(struct device *dev, char *buf)
268 {
269         struct node_entry *ne = container_of(dev, struct node_entry, device);
270
271         return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
272                        "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
273                        ne->busopt.irmc,
274                        ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
275                        ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
276                        ne->busopt.max_rec,
277                        ne->busopt.max_rom,
278                        ne->busopt.cyc_clk_acc);
279 }
280 static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
281
282
283 static ssize_t fw_show_ne_tlabels_free(struct device *dev, char *buf)
284 {
285         struct node_entry *ne = container_of(dev, struct node_entry, device);
286         return sprintf(buf, "%d\n", atomic_read(&ne->tpool->count.count) + 1);
287 }
288 static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
289
290
291 static ssize_t fw_show_ne_tlabels_allocations(struct device *dev, char *buf)
292 {
293         struct node_entry *ne = container_of(dev, struct node_entry, device);
294         return sprintf(buf, "%u\n", ne->tpool->allocations);
295 }
296 static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
297
298
299 static ssize_t fw_show_ne_tlabels_mask(struct device *dev, char *buf)
300 {
301         struct node_entry *ne = container_of(dev, struct node_entry, device);
302 #if (BITS_PER_LONG <= 32)
303         return sprintf(buf, "0x%08lx%08lx\n", ne->tpool->pool[0], ne->tpool->pool[1]);
304 #else
305         return sprintf(buf, "0x%016lx\n", ne->tpool->pool[0]);
306 #endif
307 }
308 static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
309
310
311 static ssize_t fw_set_ignore_driver(struct device *dev, const char *buf, size_t count)
312 {
313         struct unit_directory *ud = container_of(dev, struct unit_directory, device);
314         int state = simple_strtoul(buf, NULL, 10);
315
316         if (state == 1) {
317                 down_write(&dev->bus->subsys.rwsem);
318                 device_release_driver(dev);
319                 ud->ignore_driver = 1;
320                 up_write(&dev->bus->subsys.rwsem);
321         } else if (!state)
322                 ud->ignore_driver = 0;
323
324         return count;
325 }
326 static ssize_t fw_get_ignore_driver(struct device *dev, char *buf)
327 {
328         struct unit_directory *ud = container_of(dev, struct unit_directory, device);
329
330         return sprintf(buf, "%d\n", ud->ignore_driver);
331 }
332 static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
333
334
335 static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
336 {
337         struct node_entry *ne;
338         u64 guid = (u64)simple_strtoull(buf, NULL, 16);
339
340         ne = find_entry_by_guid(guid);
341
342         if (ne == NULL || !ne->in_limbo)
343                 return -EINVAL;
344
345         nodemgr_remove_ne(ne);
346
347         return count;
348 }
349 static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
350 {
351         return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
352 }
353 static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
354
355 static int nodemgr_rescan_bus_thread(void *__unused)
356 {
357         /* No userlevel access needed */
358         daemonize("kfwrescan");
359
360         bus_rescan_devices(&ieee1394_bus_type);
361
362         return 0;
363 }
364
365 static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf, size_t count)
366 {
367         int state = simple_strtoul(buf, NULL, 10);
368
369         /* Don't wait for this, or care about errors. Root could do
370          * something stupid and spawn this a lot of times, but that's
371          * root's fault. */
372         if (state == 1)
373                 kernel_thread(nodemgr_rescan_bus_thread, NULL, CLONE_KERNEL);
374
375         return count;
376 }
377 static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
378 {
379         return sprintf(buf, "You can force a rescan of the bus for "
380                         "drivers by writing a 1 to this file\n");
381 }
382 static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
383
384
385 static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
386 {
387         int state = simple_strtoul(buf, NULL, 10);
388
389         if (state == 1)
390                 ignore_drivers = 1;
391         else if (!state)
392                 ignore_drivers = 0;
393
394         return count;
395 }
396 static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
397 {
398         return sprintf(buf, "%d\n", ignore_drivers);
399 }
400 static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
401
402
403 struct bus_attribute *const fw_bus_attrs[] = {
404         &bus_attr_destroy_node,
405         &bus_attr_rescan,
406         &bus_attr_ignore_drivers,
407         NULL
408 };
409
410
411 fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
412 fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
413
414 fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
415 fw_attr_td(ne, struct node_entry, vendor_name_kv)
416 fw_attr(ne, struct node_entry, vendor_oui, const char *, "%s\n")
417
418 fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
419 fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
420 fw_attr(ne, struct node_entry, guid_vendor_oui, const char *, "%s\n")
421 fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
422
423 static struct device_attribute *const fw_ne_attrs[] = {
424         &dev_attr_ne_guid,
425         &dev_attr_ne_guid_vendor_id,
426         &dev_attr_ne_capabilities,
427         &dev_attr_ne_vendor_id,
428         &dev_attr_ne_nodeid,
429         &dev_attr_bus_options,
430         &dev_attr_tlabels_free,
431         &dev_attr_tlabels_allocations,
432         &dev_attr_tlabels_mask,
433 };
434
435
436
437 fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
438 fw_attr(ud, struct unit_directory, length, int, "%d\n")
439 /* These are all dependent on the value being provided */
440 fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
441 fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
442 fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
443 fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
444 fw_attr_td(ud, struct unit_directory, vendor_name_kv)
445 fw_attr(ud, struct unit_directory, vendor_oui, const char *, "%s\n")
446 fw_attr_td(ud, struct unit_directory, model_name_kv)
447
448 static struct device_attribute *const fw_ud_attrs[] = {
449         &dev_attr_ud_address,
450         &dev_attr_ud_length,
451         &dev_attr_ignore_driver,
452 };
453
454
455 fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
456 fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
457 fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
458 fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
459 fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
460 fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
461 fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
462 fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
463
464 static struct device_attribute *const fw_host_attrs[] = {
465         &dev_attr_host_node_count,
466         &dev_attr_host_selfid_count,
467         &dev_attr_host_nodes_active,
468         &dev_attr_host_in_bus_reset,
469         &dev_attr_host_is_root,
470         &dev_attr_host_is_cycmst,
471         &dev_attr_host_is_irm,
472         &dev_attr_host_is_busmgr,
473 };
474
475
476 static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
477 {
478         struct hpsb_protocol_driver *driver;
479         struct ieee1394_device_id *id;
480         int length = 0;
481         char *scratch = buf;
482
483         driver = container_of(drv, struct hpsb_protocol_driver, driver);
484
485         for (id = driver->id_table; id->match_flags != 0; id++) {
486                 int need_coma = 0;
487
488                 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
489                         length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
490                         scratch = buf + length;
491                         need_coma++;
492                 }
493
494                 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
495                         length += sprintf(scratch, "%smodel_id=0x%06x",
496                                           need_coma++ ? "," : "",
497                                           id->model_id);
498                         scratch = buf + length;
499                 }
500
501                 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
502                         length += sprintf(scratch, "%sspecifier_id=0x%06x",
503                                           need_coma++ ? "," : "",
504                                           id->specifier_id);
505                         scratch = buf + length;
506                 }
507
508                 if (id->match_flags & IEEE1394_MATCH_VERSION) {
509                         length += sprintf(scratch, "%sversion=0x%06x",
510                                           need_coma++ ? "," : "",
511                                           id->version);
512                         scratch = buf + length;
513                 }
514
515                 if (need_coma) {
516                         *scratch++ = '\n';
517                         length++;
518                 }
519         }
520
521         return length;
522 }
523 static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
524
525
526 fw_drv_attr(name, const char *, "%s\n")
527
528 static struct driver_attribute *const fw_drv_attrs[] = {
529         &driver_attr_drv_name,
530         &driver_attr_device_ids,
531 };
532
533
534 static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
535 {
536         struct device_driver *drv = &driver->driver;
537         int i;
538
539         for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
540                 driver_create_file(drv, fw_drv_attrs[i]);
541 }
542
543
544 static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
545 {
546         struct device_driver *drv = &driver->driver;
547         int i;
548
549         for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
550                 driver_remove_file(drv, fw_drv_attrs[i]);
551 }
552
553
554 static void nodemgr_create_ne_dev_files(struct node_entry *ne)
555 {
556         struct device *dev = &ne->device;
557         int i;
558
559         for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
560                 device_create_file(dev, fw_ne_attrs[i]);
561 }
562
563
564 static void nodemgr_create_host_dev_files(struct hpsb_host *host)
565 {
566         struct device *dev = &host->device;
567         int i;
568
569         for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
570                 device_create_file(dev, fw_host_attrs[i]);
571 }
572
573
574 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid);
575
576 static void nodemgr_update_host_dev_links(struct hpsb_host *host)
577 {
578         struct device *dev = &host->device;
579         struct node_entry *ne;
580
581         sysfs_remove_link(&dev->kobj, "irm_id");
582         sysfs_remove_link(&dev->kobj, "busmgr_id");
583         sysfs_remove_link(&dev->kobj, "host_id");
584
585         if ((ne = find_entry_by_nodeid(host, host->irm_id)))
586                 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id");
587         if ((ne = find_entry_by_nodeid(host, host->busmgr_id)))
588                 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id");
589         if ((ne = find_entry_by_nodeid(host, host->node_id)))
590                 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id");
591 }
592
593 static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
594 {
595         struct device *dev = &ud->device;
596         int i;
597
598         for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
599                 device_create_file(dev, fw_ud_attrs[i]);
600
601         if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
602                 device_create_file(dev, &dev_attr_ud_specifier_id);
603
604         if (ud->flags & UNIT_DIRECTORY_VERSION)
605                 device_create_file(dev, &dev_attr_ud_version);
606
607         if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
608                 device_create_file(dev, &dev_attr_ud_vendor_id);
609                 if (ud->vendor_name_kv)
610                         device_create_file(dev, &dev_attr_ud_vendor_name_kv);
611         }
612
613         if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
614                 device_create_file(dev, &dev_attr_ud_model_id);
615                 if (ud->model_name_kv)
616                         device_create_file(dev, &dev_attr_ud_model_name_kv);
617         }
618 }
619
620
621 static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
622 {
623         struct hpsb_protocol_driver *driver;
624         struct unit_directory *ud;
625         struct ieee1394_device_id *id;
626
627         /* We only match unit directories */
628         if (dev->platform_data != &nodemgr_ud_platform_data)
629                 return 0;
630
631         ud = container_of(dev, struct unit_directory, device);
632         driver = container_of(drv, struct hpsb_protocol_driver, driver);
633
634         if (ud->ne->in_limbo || ud->ignore_driver)
635                 return 0;
636
637         for (id = driver->id_table; id->match_flags != 0; id++) {
638                 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
639                     id->vendor_id != ud->vendor_id)
640                         continue;
641
642                 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
643                     id->model_id != ud->model_id)
644                         continue;
645
646                 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
647                     id->specifier_id != ud->specifier_id)
648                         continue;
649
650                 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
651                     id->version != ud->version)
652                         continue;
653
654                 return 1;
655         }
656
657         return 0;
658 }
659
660
661 static void nodemgr_remove_uds(struct node_entry *ne)
662 {
663         struct class_device *cdev, *next;
664         struct unit_directory *ud;
665
666         list_for_each_entry_safe(cdev, next, &nodemgr_ud_class.children, node) {
667                 ud = container_of(cdev, struct unit_directory, class_dev);
668
669                 if (ud->ne != ne)
670                         continue;
671
672                 class_device_unregister(&ud->class_dev);
673                 device_unregister(&ud->device);
674         }
675 }
676
677
678 static void nodemgr_remove_ne(struct node_entry *ne)
679 {
680         struct device *dev = &ne->device;
681
682         dev = get_device(&ne->device);
683         if (!dev)
684                 return;
685
686         HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "]  GUID[%016Lx]",
687                    NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
688
689         nodemgr_remove_uds(ne);
690
691         class_device_unregister(&ne->class_dev);
692         device_unregister(dev);
693
694         put_device(dev);
695 }
696
697
698 static void nodemgr_remove_host_dev(struct device *dev)
699 {
700         struct device *ne_dev, *next;
701
702         list_for_each_entry_safe(ne_dev, next, &dev->children, node)
703                 nodemgr_remove_ne(container_of(ne_dev, struct node_entry, device));
704
705         sysfs_remove_link(&dev->kobj, "irm_id");
706         sysfs_remove_link(&dev->kobj, "busmgr_id");
707         sysfs_remove_link(&dev->kobj, "host_id");
708 }
709
710
711 static void nodemgr_update_bus_options(struct node_entry *ne)
712 {
713 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
714         static const u16 mr[] = { 4, 64, 1024, 0};
715 #endif
716         quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
717
718         ne->busopt.irmc         = (busoptions >> 31) & 1;
719         ne->busopt.cmc          = (busoptions >> 30) & 1;
720         ne->busopt.isc          = (busoptions >> 29) & 1;
721         ne->busopt.bmc          = (busoptions >> 28) & 1;
722         ne->busopt.pmc          = (busoptions >> 27) & 1;
723         ne->busopt.cyc_clk_acc  = (busoptions >> 16) & 0xff;
724         ne->busopt.max_rec      = 1 << (((busoptions >> 12) & 0xf) + 1);
725         ne->busopt.max_rom      = (busoptions >> 8) & 0x3;
726         ne->busopt.generation   = (busoptions >> 4) & 0xf;
727         ne->busopt.lnkspd       = busoptions & 0x7;
728
729         HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
730                      "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
731                      busoptions, ne->busopt.irmc, ne->busopt.cmc,
732                      ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
733                      ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
734                      mr[ne->busopt.max_rom],
735                      ne->busopt.generation, ne->busopt.lnkspd);
736 }
737
738
739 static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
740                                               struct host_info *hi, nodeid_t nodeid,
741                                               unsigned int generation)
742 {
743         struct hpsb_host *host = hi->host;
744         struct node_entry *ne;
745
746         ne = kmalloc(sizeof(struct node_entry), GFP_KERNEL);
747         if (!ne) return NULL;
748
749         memset(ne, 0, sizeof(struct node_entry));
750
751         ne->tpool = &host->tpool[nodeid & NODE_MASK];
752
753         ne->host = host;
754         ne->nodeid = nodeid;
755         ne->generation = generation;
756         ne->needs_probe = 1;
757
758         ne->guid = guid;
759         ne->guid_vendor_id = (guid >> 40) & 0xffffff;
760         ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
761         ne->csr = csr;
762
763         memcpy(&ne->device, &nodemgr_dev_template_ne,
764                sizeof(ne->device));
765         ne->device.parent = &host->device;
766         snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
767                  (unsigned long long)(ne->guid));
768
769         ne->class_dev.dev = &ne->device;
770         ne->class_dev.class = &nodemgr_ne_class;
771         snprintf(ne->class_dev.class_id, BUS_ID_SIZE, "%016Lx",
772                  (unsigned long long)(ne->guid));
773
774         device_register(&ne->device);
775         class_device_register(&ne->class_dev);
776         get_device(&ne->device);
777
778         if (ne->guid_vendor_oui)
779                 device_create_file(&ne->device, &dev_attr_ne_guid_vendor_oui);
780         nodemgr_create_ne_dev_files(ne);
781
782         nodemgr_update_bus_options(ne);
783
784         HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "]  GUID[%016Lx]",
785                    (host->node_id == nodeid) ? "Host" : "Node",
786                    NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
787
788         return ne;
789 }
790
791
792 static struct node_entry *find_entry_by_guid(u64 guid)
793 {
794         struct class *class = &nodemgr_ne_class;
795         struct class_device *cdev;
796         struct node_entry *ne, *ret_ne = NULL;
797
798         down_read(&class->subsys.rwsem);
799         list_for_each_entry(cdev, &class->children, node) {
800                 ne = container_of(cdev, struct node_entry, class_dev);
801
802                 if (ne->guid == guid) {
803                         ret_ne = ne;
804                         break;
805                 }
806         }
807         up_read(&class->subsys.rwsem);
808
809         return ret_ne;
810 }
811
812
813 static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
814 {
815         struct class *class = &nodemgr_ne_class;
816         struct class_device *cdev;
817         struct node_entry *ne, *ret_ne = NULL;
818
819         down_read(&class->subsys.rwsem);
820         list_for_each_entry(cdev, &class->children, node) {
821                 ne = container_of(cdev, struct node_entry, class_dev);
822
823                 if (ne->host == host && ne->nodeid == nodeid) {
824                         ret_ne = ne;
825                         break;
826                 }
827         }
828         up_read(&class->subsys.rwsem);
829
830         return ret_ne;
831 }
832
833
834
835 /* This implementation currently only scans the config rom and its
836  * immediate unit directories looking for software_id and
837  * software_version entries, in order to get driver autoloading working. */
838 static struct unit_directory *nodemgr_process_unit_directory
839         (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
840          unsigned int *id, struct unit_directory *parent)
841 {
842         struct unit_directory *ud;
843         struct unit_directory *ud_temp = NULL;
844         struct csr1212_dentry *dentry;
845         struct csr1212_keyval *kv;
846         u8 last_key_id = 0;
847
848         ud = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
849         if (!ud)
850                 goto unit_directory_error;
851
852         memset (ud, 0, sizeof(struct unit_directory));
853
854         ud->ne = ne;
855         ud->ignore_driver = ignore_drivers;
856         ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
857         ud->ud_kv = ud_kv;
858         ud->id = (*id)++;
859
860         csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
861                 switch (kv->key.id) {
862                 case CSR1212_KV_ID_VENDOR:
863                         if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
864                                 ud->vendor_id = kv->value.immediate;
865                                 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
866
867                                 if (ud->vendor_id)
868                                         ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
869                         }
870                         break;
871
872                 case CSR1212_KV_ID_MODEL:
873                         ud->model_id = kv->value.immediate;
874                         ud->flags |= UNIT_DIRECTORY_MODEL_ID;
875                         break;
876
877                 case CSR1212_KV_ID_SPECIFIER_ID:
878                         ud->specifier_id = kv->value.immediate;
879                         ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
880                         break;
881
882                 case CSR1212_KV_ID_VERSION:
883                         ud->version = kv->value.immediate;
884                         ud->flags |= UNIT_DIRECTORY_VERSION;
885                         break;
886
887                 case CSR1212_KV_ID_DESCRIPTOR:
888                         if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
889                             CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
890                             CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
891                             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
892                             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
893                             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
894                                 switch (last_key_id) {
895                                 case CSR1212_KV_ID_VENDOR:
896                                         ud->vendor_name_kv = kv;
897                                         csr1212_keep_keyval(kv);
898                                         break;
899
900                                 case CSR1212_KV_ID_MODEL:
901                                         ud->model_name_kv = kv;
902                                         csr1212_keep_keyval(kv);
903                                         break;
904
905                                 }
906                         } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
907                         break;
908
909                 case CSR1212_KV_ID_DEPENDENT_INFO:
910                         if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
911                                 /* This should really be done in SBP2 as this is
912                                  * doing SBP2 specific parsing. */
913                                 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
914                                 ud_temp = nodemgr_process_unit_directory(hi, ne, kv, id,
915                                                                          parent);
916
917                                 if (ud_temp == NULL)
918                                         break;
919
920                                 /* inherit unspecified values */
921                                 if ((ud->flags & UNIT_DIRECTORY_VENDOR_ID) &&
922                                     !(ud_temp->flags & UNIT_DIRECTORY_VENDOR_ID))
923                                 {
924                                         ud_temp->flags |=  UNIT_DIRECTORY_VENDOR_ID;
925                                         ud_temp->vendor_id = ud->vendor_id;
926                                         ud_temp->vendor_oui = ud->vendor_oui;
927                                 }
928                                 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
929                                     !(ud_temp->flags & UNIT_DIRECTORY_MODEL_ID))
930                                 {
931                                         ud_temp->flags |=  UNIT_DIRECTORY_MODEL_ID;
932                                         ud_temp->model_id = ud->model_id;
933                                 }
934                                 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
935                                     !(ud_temp->flags & UNIT_DIRECTORY_SPECIFIER_ID))
936                                 {
937                                         ud_temp->flags |=  UNIT_DIRECTORY_SPECIFIER_ID;
938                                         ud_temp->specifier_id = ud->specifier_id;
939                                 }
940                                 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
941                                     !(ud_temp->flags & UNIT_DIRECTORY_VERSION))
942                                 {
943                                         ud_temp->flags |=  UNIT_DIRECTORY_VERSION;
944                                         ud_temp->version = ud->version;
945                                 }
946                         }
947
948                         break;
949
950                 default:
951                         break;
952                 }
953                 last_key_id = kv->key.id;
954         }
955
956         memcpy(&ud->device, &nodemgr_dev_template_ud,
957                sizeof(ud->device));
958
959         if (parent) {
960                 ud->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
961                 ud->device.parent = &parent->device;
962         } else
963                 ud->device.parent = &ne->device;
964
965         snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
966                  ne->device.bus_id, ud->id);
967
968         ud->class_dev.dev = &ud->device;
969         ud->class_dev.class = &nodemgr_ud_class;
970         snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
971                  ne->device.bus_id, ud->id);
972
973         device_register(&ud->device);
974         class_device_register(&ud->class_dev);
975         get_device(&ud->device);
976
977         if (ud->vendor_oui)
978                 device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
979         nodemgr_create_ud_dev_files(ud);
980
981         return ud;
982
983 unit_directory_error:
984         if (ud != NULL)
985                 kfree(ud);
986         return NULL;
987 }
988
989
990 static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
991 {
992         unsigned int ud_id = 0;
993         struct csr1212_dentry *dentry;
994         struct csr1212_keyval *kv;
995         u8 last_key_id = 0;
996
997         ne->needs_probe = 0;
998
999         csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1000                 switch (kv->key.id) {
1001                 case CSR1212_KV_ID_VENDOR:
1002                         ne->vendor_id = kv->value.immediate;
1003
1004                         if (ne->vendor_id)
1005                                 ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
1006                         break;
1007
1008                 case CSR1212_KV_ID_NODE_CAPABILITIES:
1009                         ne->capabilities = kv->value.immediate;
1010                         break;
1011
1012                 case CSR1212_KV_ID_UNIT:
1013                         nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
1014                         break;
1015
1016                 case CSR1212_KV_ID_DESCRIPTOR:
1017                         if (last_key_id == CSR1212_KV_ID_VENDOR) {
1018                                 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1019                                     CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1020                                     CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1021                                     CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1022                                     CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1023                                     CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1024                                         ne->vendor_name_kv = kv;
1025                                         csr1212_keep_keyval(kv);
1026                                 }
1027                         }
1028                         break;
1029                 }
1030                 last_key_id = kv->key.id;
1031         }
1032
1033         if (ne->vendor_oui)
1034                 device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
1035         if (ne->vendor_name_kv)
1036                 device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
1037 }
1038
1039 #ifdef CONFIG_HOTPLUG
1040
1041 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
1042                            char *buffer, int buffer_size)
1043 {
1044         struct unit_directory *ud;
1045         int i = 0;
1046         int length = 0;
1047
1048         if (!cdev)
1049                 return -ENODEV;
1050
1051         ud = container_of(cdev, struct unit_directory, class_dev);
1052
1053         if (ud->ne->in_limbo || ud->ignore_driver)
1054                 return -ENODEV;
1055
1056 #define PUT_ENVP(fmt,val)                                       \
1057 do {                                                            \
1058         int printed;                                            \
1059         envp[i++] = buffer;                                     \
1060         printed = snprintf(buffer, buffer_size - length,        \
1061                            fmt, val);                           \
1062         if ((buffer_size - (length+printed) <= 0) || (i >= num_envp))   \
1063                 return -ENOMEM;                                 \
1064         length += printed+1;                                    \
1065         buffer += printed+1;                                    \
1066 } while (0)
1067
1068         PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1069         PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1070         PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1071         PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1072         PUT_ENVP("VERSION=%06x", ud->version);
1073
1074 #undef PUT_ENVP
1075
1076         envp[i] = NULL;
1077
1078         return 0;
1079 }
1080
1081 #else
1082
1083 static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
1084                            char *buffer, int buffer_size)
1085 {
1086         return -ENODEV;
1087 }
1088
1089 #endif /* CONFIG_HOTPLUG */
1090
1091
1092 int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
1093 {
1094         int ret;
1095
1096         /* This will cause a probe for devices */
1097         ret = driver_register(&driver->driver);
1098         if (!ret)
1099                 nodemgr_create_drv_files(driver);
1100
1101         return ret;
1102 }
1103
1104 void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1105 {
1106         nodemgr_remove_drv_files(driver);
1107         /* This will subsequently disconnect all devices that our driver
1108          * is attached to. */
1109         driver_unregister(&driver->driver);
1110 }
1111
1112
1113 /*
1114  * This function updates nodes that were present on the bus before the
1115  * reset and still are after the reset.  The nodeid and the config rom
1116  * may have changed, and the drivers managing this device must be
1117  * informed that this device just went through a bus reset, to allow
1118  * the to take whatever actions required.
1119  */
1120 static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
1121                                 struct host_info *hi, nodeid_t nodeid,
1122                                 unsigned int generation)
1123 {
1124         if (ne->nodeid != nodeid) {
1125                 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1126                            NODE_BUS_ARGS(ne->host, ne->nodeid),
1127                            NODE_BUS_ARGS(ne->host, nodeid));
1128                 ne->nodeid = nodeid;
1129         }
1130
1131         if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1132                 kfree(ne->csr->private);
1133                 csr1212_destroy_csr(ne->csr);
1134                 ne->csr = csr;
1135
1136                 /* If the node's configrom generation has changed, we
1137                  * unregister all the unit directories. */
1138                 nodemgr_remove_uds(ne);
1139
1140                 nodemgr_update_bus_options(ne);
1141
1142                 /* Mark the node as new, so it gets re-probed */
1143                 ne->needs_probe = 1;
1144         }
1145
1146         if (ne->in_limbo)
1147                 nodemgr_resume_ne(ne);
1148
1149         /* Mark the node current */
1150         ne->generation = generation;
1151 }
1152
1153
1154
1155 static void nodemgr_node_scan_one(struct host_info *hi,
1156                                   nodeid_t nodeid, int generation)
1157 {
1158         struct hpsb_host *host = hi->host;
1159         struct node_entry *ne;
1160         octlet_t guid;
1161         struct csr1212_csr *csr;
1162         struct nodemgr_csr_info *ci;
1163
1164         ci = kmalloc(sizeof(struct nodemgr_csr_info), GFP_KERNEL);
1165         if (!ci)
1166                 return;
1167
1168         ci->host = host;
1169         ci->nodeid = nodeid;
1170         ci->generation = generation;
1171
1172         /* We need to detect when the ConfigROM's generation has changed,
1173          * so we only update the node's info when it needs to be.  */
1174
1175         csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1176         if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1177                 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1178                          NODE_BUS_ARGS(host, nodeid));
1179                 if (csr)
1180                         csr1212_destroy_csr(csr);
1181                 kfree(ci);
1182                 return;
1183         }
1184
1185         if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1186                 /* This isn't a 1394 device, but we let it slide. There
1187                  * was a report of a device with broken firmware which
1188                  * reported '2394' instead of '1394', which is obviously a
1189                  * mistake. One would hope that a non-1394 device never
1190                  * gets connected to Firewire bus. If someone does, we
1191                  * shouldn't be held responsible, so we'll allow it with a
1192                  * warning.  */
1193                 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1194                           NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1195         }
1196
1197         guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1198         ne = find_entry_by_guid(guid);
1199
1200         if (ne && ne->host != host && ne->in_limbo) {
1201                 /* Must have moved this device from one host to another */
1202                 nodemgr_remove_ne(ne);
1203                 ne = NULL;
1204         }
1205
1206         if (!ne)
1207                 nodemgr_create_node(guid, csr, hi, nodeid, generation);
1208         else
1209                 nodemgr_update_node(ne, csr, hi, nodeid, generation);
1210
1211         return;
1212 }
1213
1214
1215 static void nodemgr_node_scan(struct host_info *hi, int generation)
1216 {
1217         int count;
1218         struct hpsb_host *host = hi->host;
1219         struct selfid *sid = (struct selfid *)host->topology_map;
1220         nodeid_t nodeid = LOCAL_BUS;
1221
1222         /* Scan each node on the bus */
1223         for (count = host->selfid_count; count; count--, sid++) {
1224                 if (sid->extended)
1225                         continue;
1226
1227                 if (!sid->link_active) {
1228                         nodeid++;
1229                         continue;
1230                 }
1231                 nodemgr_node_scan_one(hi, nodeid++, generation);
1232         }
1233 }
1234
1235
1236 static void nodemgr_suspend_ne(struct node_entry *ne)
1237 {
1238         struct class_device *cdev;
1239         struct unit_directory *ud;
1240
1241         HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "]  GUID[%016Lx]",
1242                    NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1243
1244         ne->in_limbo = 1;
1245         device_create_file(&ne->device, &dev_attr_ne_in_limbo);
1246
1247         down_write(&ne->device.bus->subsys.rwsem);
1248         list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1249                 ud = container_of(cdev, struct unit_directory, class_dev);
1250
1251                 if (ud->ne != ne)
1252                         continue;
1253
1254                 if (ud->device.driver &&
1255                     (!ud->device.driver->suspend ||
1256                       ud->device.driver->suspend(&ud->device, 0, 0)))
1257                         device_release_driver(&ud->device);
1258         }
1259         up_write(&ne->device.bus->subsys.rwsem);
1260 }
1261
1262
1263 static void nodemgr_resume_ne(struct node_entry *ne)
1264 {
1265         struct class_device *cdev;
1266         struct unit_directory *ud;
1267
1268         ne->in_limbo = 0;
1269         device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1270
1271         down_read(&ne->device.bus->subsys.rwsem);
1272         list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
1273                 ud = container_of(cdev, struct unit_directory, class_dev);
1274
1275                 if (ud->ne != ne)
1276                         continue;
1277
1278                 if (ud->device.driver && ud->device.driver->resume)
1279                         ud->device.driver->resume(&ud->device, 0);
1280         }
1281         up_read(&ne->device.bus->subsys.rwsem);
1282
1283         HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "]  GUID[%016Lx]",
1284                    NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
1285 }
1286
1287
1288 static void nodemgr_update_pdrv(struct node_entry *ne)
1289 {
1290         struct unit_directory *ud;
1291         struct hpsb_protocol_driver *pdrv;
1292         struct class *class = &nodemgr_ud_class;
1293         struct class_device *cdev;
1294
1295         down_read(&class->subsys.rwsem);
1296         list_for_each_entry(cdev, &class->children, node) {
1297                 ud = container_of(cdev, struct unit_directory, class_dev);
1298                 if (ud->ne != ne || !ud->device.driver)
1299                         continue;
1300
1301                 pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
1302
1303                 if (pdrv->update && pdrv->update(ud)) {
1304                         down_write(&ud->device.bus->subsys.rwsem);
1305                         device_release_driver(&ud->device);
1306                         up_write(&ud->device.bus->subsys.rwsem);
1307                 }
1308         }
1309         up_read(&class->subsys.rwsem);
1310 }
1311
1312
1313 static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
1314 {
1315         struct device *dev;
1316
1317         if (ne->host != hi->host || ne->in_limbo)
1318                 return;
1319
1320         dev = get_device(&ne->device);
1321         if (!dev)
1322                 return;
1323
1324         /* If "needs_probe", then this is either a new or changed node we
1325          * rescan totally. If the generation matches for an existing node
1326          * (one that existed prior to the bus reset) we send update calls
1327          * down to the drivers. Otherwise, this is a dead node and we
1328          * suspend it. */
1329         if (ne->needs_probe)
1330                 nodemgr_process_root_directory(hi, ne);
1331         else if (ne->generation == generation)
1332                 nodemgr_update_pdrv(ne);
1333         else
1334                 nodemgr_suspend_ne(ne);
1335
1336         put_device(dev);
1337 }
1338
1339
1340 static void nodemgr_node_probe(struct host_info *hi, int generation)
1341 {
1342         struct hpsb_host *host = hi->host;
1343         struct class *class = &nodemgr_ne_class;
1344         struct class_device *cdev;
1345
1346         /* Do some processing of the nodes we've probed. This pulls them
1347          * into the sysfs layer if needed, and can result in processing of
1348          * unit-directories, or just updating the node and it's
1349          * unit-directories. */
1350         down_read(&class->subsys.rwsem);
1351         list_for_each_entry(cdev, &class->children, node)
1352                 nodemgr_probe_ne(hi, container_of(cdev, struct node_entry, class_dev), generation);
1353         up_read(&class->subsys.rwsem);
1354
1355
1356         /* If we had a bus reset while we were scanning the bus, it is
1357          * possible that we did not probe all nodes.  In that case, we
1358          * skip the clean up for now, since we could remove nodes that
1359          * were still on the bus.  The bus reset increased hi->reset_sem,
1360          * so there's a bus scan pending which will do the clean up
1361          * eventually.
1362          *
1363          * Now let's tell the bus to rescan our devices. This may seem
1364          * like overhead, but the driver-model core will only scan a
1365          * device for a driver when either the device is added, or when a
1366          * new driver is added. A bus reset is a good reason to rescan
1367          * devices that were there before.  For example, an sbp2 device
1368          * may become available for login, if the host that held it was
1369          * just removed.  */
1370
1371         if (generation == get_hpsb_generation(host))
1372                 bus_rescan_devices(&ieee1394_bus_type);
1373
1374         return;
1375 }
1376
1377 /* Because we are a 1394a-2000 compliant IRM, we need to inform all the other
1378  * nodes of the broadcast channel.  (Really we're only setting the validity
1379  * bit). Other IRM responsibilities go in here as well. */
1380 static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1381 {
1382         quadlet_t bc;
1383
1384         /* if irm_id == -1 then there is no IRM on this bus */
1385         if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1386                 return 1;
1387
1388         host->csr.broadcast_channel |= 0x40000000;  /* set validity bit */
1389
1390         bc = cpu_to_be32(host->csr.broadcast_channel);
1391
1392         hpsb_write(host, LOCAL_BUS | ALL_NODES, get_hpsb_generation(host),
1393                    (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1394                    &bc, sizeof(quadlet_t));
1395
1396         /* If there is no bus manager then we should set the root node's
1397          * force_root bit to promote bus stability per the 1394
1398          * spec. (8.4.2.6) */
1399         if (host->busmgr_id == 0xffff && host->node_count > 1)
1400         {
1401                 u16 root_node = host->node_count - 1;
1402                 struct node_entry *ne = find_entry_by_nodeid(host, root_node | LOCAL_BUS);
1403
1404                 if (ne && ne->busopt.cmc)
1405                         hpsb_send_phy_config(host, root_node, -1);
1406                 else {
1407                         HPSB_DEBUG("The root node is not cycle master capable; "
1408                                    "selecting a new root node and resetting...");
1409
1410                         if (cycles >= 5) {
1411                                 /* Oh screw it! Just leave the bus as it is */
1412                                 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1413                                 return 1;
1414                         }
1415
1416                         hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1417                         hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1418
1419                         return 0;
1420                 }
1421         }
1422
1423         return 1;
1424 }
1425
1426 /* We need to ensure that if we are not the IRM, that the IRM node is capable of
1427  * everything we can do, otherwise issue a bus reset and try to become the IRM
1428  * ourselves. */
1429 static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1430 {
1431         quadlet_t bc;
1432         int status;
1433
1434         if (host->is_irm)
1435                 return 1;
1436
1437         status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1438                            get_hpsb_generation(host),
1439                            (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1440                            &bc, sizeof(quadlet_t));
1441
1442         if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1443                 /* The current irm node does not have a valid BROADCAST_CHANNEL
1444                  * register and we do, so reset the bus with force_root set */
1445                 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1446
1447                 if (cycles >= 5) {
1448                         /* Oh screw it! Just leave the bus as it is */
1449                         HPSB_DEBUG("Stopping reset loop for IRM sanity");
1450                         return 1;
1451                 }
1452
1453                 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1454                 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1455
1456                 return 0;
1457         }
1458
1459         return 1;
1460 }
1461
1462 static int nodemgr_host_thread(void *__hi)
1463 {
1464         struct host_info *hi = (struct host_info *)__hi;
1465         struct hpsb_host *host = hi->host;
1466         int reset_cycles = 0;
1467
1468         /* No userlevel access needed */
1469         daemonize(hi->daemon_name);
1470
1471         /* Setup our device-model entries */
1472         nodemgr_create_host_dev_files(host);
1473
1474         /* Sit and wait for a signal to probe the nodes on the bus. This
1475          * happens when we get a bus reset. */
1476         while (1) {
1477                 unsigned int generation = 0;
1478                 int i;
1479
1480                 if (down_interruptible(&hi->reset_sem) ||
1481                     down_interruptible(&nodemgr_serialize)) {
1482                         if (try_to_freeze(PF_FREEZE))
1483                                 continue;
1484                         printk("NodeMgr: received unexpected signal?!\n" );
1485                         break;
1486                 }
1487
1488                 if (hi->kill_me) {
1489                         up(&nodemgr_serialize);
1490                         break;
1491                 }
1492
1493                 /* Pause for 1/4 second in 1/16 second intervals,
1494                  * to make sure things settle down. */
1495                 for (i = 0; i < 4 ; i++) {
1496                         set_current_state(TASK_INTERRUPTIBLE);
1497                         if (msleep_interruptible(63)) {
1498                                 up(&nodemgr_serialize);
1499                                 goto caught_signal;
1500                         }
1501
1502                         /* Now get the generation in which the node ID's we collect
1503                          * are valid.  During the bus scan we will use this generation
1504                          * for the read transactions, so that if another reset occurs
1505                          * during the scan the transactions will fail instead of
1506                          * returning bogus data. */
1507                         generation = get_hpsb_generation(host);
1508
1509                         /* If we get a reset before we are done waiting, then
1510                          * start the the waiting over again */
1511                         while (!down_trylock(&hi->reset_sem))
1512                                 i = 0;
1513
1514                         /* Check the kill_me again */
1515                         if (hi->kill_me) {
1516                                 up(&nodemgr_serialize);
1517                                 goto caught_signal;
1518                         }
1519                 }
1520
1521                 if (!nodemgr_check_irm_capability(host, reset_cycles)) {
1522                         reset_cycles++;
1523                         up(&nodemgr_serialize);
1524                         continue;
1525                 }
1526
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;
1536                 }
1537
1538                 reset_cycles = 0;
1539
1540                 /* This actually does the full probe, with sysfs
1541                  * registration. */
1542                 nodemgr_node_probe(hi, generation);
1543
1544                 /* Update some of our sysfs symlinks */
1545                 nodemgr_update_host_dev_links(host);
1546
1547                 up(&nodemgr_serialize);
1548         }
1549
1550 caught_signal:
1551         HPSB_VERBOSE("NodeMgr: Exiting thread");
1552
1553         complete_and_exit(&hi->exited, 0);
1554 }
1555
1556 struct node_entry *hpsb_guid_get_entry(u64 guid)
1557 {
1558         struct node_entry *ne;
1559
1560         down(&nodemgr_serialize);
1561         ne = find_entry_by_guid(guid);
1562         up(&nodemgr_serialize);
1563
1564         return ne;
1565 }
1566
1567 struct node_entry *hpsb_nodeid_get_entry(struct hpsb_host *host, nodeid_t nodeid)
1568 {
1569         struct node_entry *ne;
1570
1571         down(&nodemgr_serialize);
1572         ne = find_entry_by_nodeid(host, nodeid);
1573         up(&nodemgr_serialize);
1574
1575         return ne;
1576 }
1577
1578
1579 int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
1580 {
1581         struct class *class = &hpsb_host_class;
1582         struct class_device *cdev;
1583         struct hpsb_host *host;
1584         int error = 0;
1585
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);
1589
1590                 if ((error = cb(host, __data)))
1591                         break;
1592         }
1593         up_read(&class->subsys.rwsem);
1594
1595         return error;
1596 }
1597
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.
1606  *
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.
1611  */
1612
1613 void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
1614 {
1615         pkt->host = ne->host;
1616         pkt->generation = ne->generation;
1617         barrier();
1618         pkt->node_id = ne->nodeid;
1619 }
1620
1621 int hpsb_node_read(struct node_entry *ne, u64 addr,
1622                    quadlet_t *buffer, size_t length)
1623 {
1624         unsigned int generation = ne->generation;
1625
1626         barrier();
1627         return hpsb_read(ne->host, ne->nodeid, generation,
1628                          addr, buffer, length);
1629 }
1630
1631 int hpsb_node_write(struct node_entry *ne, u64 addr,
1632                     quadlet_t *buffer, size_t length)
1633 {
1634         unsigned int generation = ne->generation;
1635
1636         barrier();
1637         return hpsb_write(ne->host, ne->nodeid, generation,
1638                           addr, buffer, length);
1639 }
1640
1641 int hpsb_node_lock(struct node_entry *ne, u64 addr,
1642                    int extcode, quadlet_t *data, quadlet_t arg)
1643 {
1644         unsigned int generation = ne->generation;
1645
1646         barrier();
1647         return hpsb_lock(ne->host, ne->nodeid, generation,
1648                          addr, extcode, data, arg);
1649 }
1650
1651 static void nodemgr_add_host(struct hpsb_host *host)
1652 {
1653         struct host_info *hi;
1654
1655         hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
1656
1657         if (!hi) {
1658                 HPSB_ERR ("NodeMgr: out of memory in add host");
1659                 return;
1660         }
1661
1662         hi->host = host;
1663         init_completion(&hi->exited);
1664         sema_init(&hi->reset_sem, 0);
1665
1666         sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
1667
1668         hi->pid = kernel_thread(nodemgr_host_thread, hi, CLONE_KERNEL);
1669
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;
1675         }
1676
1677         return;
1678 }
1679
1680 static void nodemgr_host_reset(struct hpsb_host *host)
1681 {
1682         struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1683
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");
1689
1690         return;
1691 }
1692
1693 static void nodemgr_remove_host(struct hpsb_host *host)
1694 {
1695         struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1696
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);
1704                 }
1705         } else
1706                 HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
1707                          host->driver->name);
1708
1709         return;
1710 }
1711
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,
1717 };
1718
1719 int init_ieee1394_nodemgr(void)
1720 {
1721         int ret;
1722
1723         ret = class_register(&nodemgr_ne_class);
1724         if (ret < 0)
1725                 return ret;
1726
1727         ret = class_register(&nodemgr_ud_class);
1728         if (ret < 0) {
1729                 class_unregister(&nodemgr_ne_class);
1730                 return ret;
1731         }
1732
1733         hpsb_register_highlevel(&nodemgr_highlevel);
1734
1735         return 0;
1736 }
1737
1738 void cleanup_ieee1394_nodemgr(void)
1739 {
1740         hpsb_unregister_highlevel(&nodemgr_highlevel);
1741
1742         class_unregister(&nodemgr_ud_class);
1743         class_unregister(&nodemgr_ne_class);
1744 }