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