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