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