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