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