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