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