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