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