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