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