blob: 9e39f73282ee6dd439e76647af5ab5c4755e711a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/delay.h>
Stefan Richterd2f119f2006-07-03 12:02:35 -040016#include <linux/kthread.h>
Stefan Richter8252bbb2006-11-22 21:09:42 +010017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/moduleparam.h>
Stefan Richter9543a932007-04-10 02:39:07 +020019#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080020#include <linux/freezer.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040021#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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;
Stefan Richter3a632fe2006-07-03 12:02:35 -040035module_param(ignore_drivers, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036MODULE_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
Ben Collins647dcb52006-06-12 18:12:37 -040046/*
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;
Stefan Richter7fdfc902006-10-21 01:23:56 +020057 int error;
Ben Collins647dcb52006-06-12 18:12:37 -040058
Stefan Richterd7530a12006-07-03 00:58:01 +020059 speed = &(ci->host->speed[NODEID_TO_NODE(ci->nodeid)]);
Ben Collins647dcb52006-06-12 18:12:37 -040060 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;
Stefan Richter7fdfc902006-10-21 01:23:56 +020069 error = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
70 &q, sizeof(quadlet_t));
71 if (error)
Ben Collins647dcb52006-06-12 18:12:37 -040072 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;
Stefan Richter7fdfc902006-10-21 01:23:56 +020085 return error;
Ben Collins647dcb52006-06-12 18:12:37 -040086}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
Stefan Richterd41bba22006-11-22 21:28:19 +010089 void *buffer, void *__ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
Stefan Richter7fdfc902006-10-21 01:23:56 +020092 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Jody McIntyree31a1272005-09-30 11:59:09 -070094 for (i = 1; ; i++) {
Stefan Richter7fdfc902006-10-21 01:23:56 +020095 error = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
96 buffer, length);
97 if (!error) {
Ben Collins647dcb52006-06-12 18:12:37 -040098 ci->speed_unverified = 0;
99 break;
100 }
101 /* Give up after 3rd failure. */
102 if (i == 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 break;
104
Ben Collins647dcb52006-06-12 18:12:37 -0400105 /* 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)) {
Stefan Richter7fdfc902006-10-21 01:23:56 +0200108 error = nodemgr_check_speed(ci, addr, buffer);
109 if (!error)
Ben Collins647dcb52006-06-12 18:12:37 -0400110 break;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (msleep_interruptible(334))
113 return -EINTR;
114 }
Stefan Richter7fdfc902006-10-21 01:23:56 +0200115 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
119{
Stefan Richter7fb9add2007-03-11 22:49:05 +0100120 return (be32_to_cpu(bus_info_data[2]) >> 8) & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct host_info {
150 struct hpsb_host *host;
151 struct list_head list;
Stefan Richterd2f119f2006-07-03 12:02:35 -0400152 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
155static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200156static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158struct bus_type ieee1394_bus_type = {
159 .name = "ieee1394",
160 .match = nodemgr_bus_match,
161};
162
Kay Sieversdd7f2922007-05-25 11:50:53 +0200163static void host_cls_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200165 put_device(&container_of((dev), struct hpsb_host, host_dev)->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168struct class hpsb_host_class = {
169 .name = "ieee1394_host",
Kay Sieversdd7f2922007-05-25 11:50:53 +0200170 .dev_release = host_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Kay Sieversdd7f2922007-05-25 11:50:53 +0200173static void ne_cls_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200175 put_device(&container_of((dev), struct node_entry, node_dev)->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static struct class nodemgr_ne_class = {
179 .name = "ieee1394_node",
Kay Sieversdd7f2922007-05-25 11:50:53 +0200180 .dev_release = ne_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Kay Sieversdd7f2922007-05-25 11:50:53 +0200183static void ud_cls_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200185 put_device(&container_of((dev), struct unit_directory, unit_dev)->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/* The name here is only so that unit directory hotplug works with old
Kay Sieversdd7f2922007-05-25 11:50:53 +0200189 * style hotplug, which only ever did unit directories anyway.
190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static struct class nodemgr_ud_class = {
192 .name = "ieee1394",
Kay Sieversdd7f2922007-05-25 11:50:53 +0200193 .dev_release = ud_cls_release,
194 .dev_uevent = nodemgr_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
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
Stefan Richter8252bbb2006-11-22 21:09:42 +0100245/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255struct 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) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400262static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{ \
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) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400274static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{ \
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); \
Al Viro51ec1382007-07-15 20:59:51 +0100282 while (buf[len - 1] == '\0') \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 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 = { \
Stefan Richterd41bba22006-11-22 21:28:19 +0100302 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
303 .show = fw_drv_show_##field, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
306
Yani Ioannoue404e272005-05-17 06:42:58 -0400307static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
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
Stefan Richter99519032006-07-02 14:17:00 +0200323#ifdef HPSB_DEBUG_TLABELS
324static ssize_t fw_show_ne_tlabels_free(struct device *dev,
325 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct node_entry *ne = container_of(dev, struct node_entry, device);
Stefan Richter99519032006-07-02 14:17:00 +0200328 unsigned long flags;
329 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
330 int tf;
331
332 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
333 tf = 64 - bitmap_weight(tp, 64);
334 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
335
336 return sprintf(buf, "%d\n", tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
339
340
Stefan Richter99519032006-07-02 14:17:00 +0200341static ssize_t fw_show_ne_tlabels_mask(struct device *dev,
342 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct node_entry *ne = container_of(dev, struct node_entry, device);
Stefan Richter99519032006-07-02 14:17:00 +0200345 unsigned long flags;
346 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
347 u64 tm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Stefan Richter99519032006-07-02 14:17:00 +0200349 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#if (BITS_PER_LONG <= 32)
Stefan Richter99519032006-07-02 14:17:00 +0200351 tm = ((u64)tp[0] << 32) + tp[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#else
Stefan Richter99519032006-07-02 14:17:00 +0200353 tm = tp[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
Stefan Richter99519032006-07-02 14:17:00 +0200355 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
356
Randy Dunlap7f588032006-10-23 21:44:36 -0700357 return sprintf(buf, "0x%016llx\n", (unsigned long long)tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
Stefan Richter99519032006-07-02 14:17:00 +0200360#endif /* HPSB_DEBUG_TLABELS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362
Yani Ioannoue404e272005-05-17 06:42:58 -0400363static 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 -0700364{
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ud->ignore_driver = 1;
Stefan Richterb07375b2006-10-22 16:16:27 +0200370 device_release_driver(dev);
Stefan Richterb07375b2006-10-22 16:16:27 +0200371 } else if (state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ud->ignore_driver = 0;
373
374 return count;
375}
Yani Ioannoue404e272005-05-17 06:42:58 -0400376static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
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
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200385static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf,
386 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200388 int error = 0;
389
Stefan Richter40fd89c2006-07-03 12:02:34 -0400390 if (simple_strtoul(buf, NULL, 10) == 1)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200391 error = bus_rescan_devices(&ieee1394_bus_type);
392 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
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;
Stefan Richterb07375b2006-10-22 16:16:27 +0200408 else if (state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 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[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 &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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
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")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435fw_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,
Stefan Richter99519032006-07-02 14:17:00 +0200444#ifdef HPSB_DEBUG_TLABELS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 &dev_attr_tlabels_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 &dev_attr_tlabels_mask,
Stefan Richter99519032006-07-02 14:17:00 +0200447#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448};
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460fw_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
Stefan Richterd41bba22006-11-22 21:28:19 +0100497 driver = container_of(drv, struct hpsb_protocol_driver, driver);
Stefan Richter07c72242008-05-01 10:43:04 +0200498 id = driver->id_table;
499 if (!id)
500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Stefan Richter07c72242008-05-01 10:43:04 +0200502 for (; id->match_flags != 0; id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 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++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200557 if (driver_create_file(drv, fw_drv_attrs[i]))
558 goto fail;
559 return;
560fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200561 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
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++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200581 if (device_create_file(dev, fw_ne_attrs[i]))
582 goto fail;
583 return;
584fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200585 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
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++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200595 if (device_create_file(dev, fw_host_attrs[i]))
596 goto fail;
597 return;
598fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200599 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200603static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
604 nodeid_t nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
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
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200615 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
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++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200635 if (device_create_file(dev, fw_ud_attrs[i]))
636 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200638 if (device_create_file(dev, &dev_attr_ud_specifier_id))
639 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ud->flags & UNIT_DIRECTORY_VERSION)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200641 if (device_create_file(dev, &dev_attr_ud_version))
642 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200644 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200651 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200657 return;
658fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200659 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662
663static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
664{
Stefan Richterd41bba22006-11-22 21:28:19 +0100665 struct hpsb_protocol_driver *driver;
666 struct unit_directory *ud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (ud->ne->in_limbo || ud->ignore_driver)
675 return 0;
676
Stefan Richter8252bbb2006-11-22 21:09:42 +0100677 /* 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);
Stefan Richterd2ace292008-02-18 21:11:07 +0100682 id = driver->id_table;
683 if (!id)
684 return 0;
685
686 for (; id->match_flags != 0; id++) {
Stefan Richterd41bba22006-11-22 21:28:19 +0100687 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
688 id->vendor_id != ud->vendor_id)
689 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Stefan Richterd41bba22006-11-22 21:28:19 +0100691 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
692 id->model_id != ud->model_id)
693 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Stefan Richterd41bba22006-11-22 21:28:19 +0100695 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
696 id->specifier_id != ud->specifier_id)
697 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Stefan Richterd41bba22006-11-22 21:28:19 +0100699 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
700 id->version != ud->version)
701 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 return 1;
Stefan Richterd41bba22006-11-22 21:28:19 +0100704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 return 0;
707}
708
709
Stefan Richterb07375b2006-10-22 16:16:27 +0200710static DEFINE_MUTEX(nodemgr_serialize_remove_uds);
711
Stefan Richter11305c32008-08-19 21:29:23 +0200712static int match_ne(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +0800713{
714 struct unit_directory *ud;
Stefan Richter11305c32008-08-19 21:29:23 +0200715 struct node_entry *ne = data;
Dave Young73cf6022008-01-22 13:56:32 +0800716
717 ud = container_of(dev, struct unit_directory, unit_dev);
718 return ud->ne == ne;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721static void nodemgr_remove_uds(struct node_entry *ne)
722{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200723 struct device *dev;
Dave Young73cf6022008-01-22 13:56:32 +0800724 struct unit_directory *ud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Young73cf6022008-01-22 13:56:32 +0800726 /* 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.
Stefan Richterb07375b2006-10-22 16:16:27 +0200729 */
Stefan Richterb07375b2006-10-22 16:16:27 +0200730 mutex_lock(&nodemgr_serialize_remove_uds);
Stefan Richter1e4f7bc2006-12-02 22:23:34 +0100731 for (;;) {
Stefan Richter11305c32008-08-19 21:29:23 +0200732 dev = class_find_device(&nodemgr_ud_class, NULL, ne, match_ne);
Dave Young73cf6022008-01-22 13:56:32 +0800733 if (!dev)
Stefan Richter1e4f7bc2006-12-02 22:23:34 +0100734 break;
Dave Young73cf6022008-01-22 13:56:32 +0800735 ud = container_of(dev, struct unit_directory, unit_dev);
736 put_device(dev);
Kay Sieversdd7f2922007-05-25 11:50:53 +0200737 device_unregister(&ud->unit_dev);
Stefan Richter1e4f7bc2006-12-02 22:23:34 +0100738 device_unregister(&ud->device);
Stefan Richterb07375b2006-10-22 16:16:27 +0200739 }
Stefan Richterb07375b2006-10-22 16:16:27 +0200740 mutex_unlock(&nodemgr_serialize_remove_uds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743
744static void nodemgr_remove_ne(struct node_entry *ne)
745{
Stefan Richtercec1a312006-11-18 23:16:11 +0100746 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 nodemgr_remove_uds(ne);
755
Kay Sieversdd7f2922007-05-25 11:50:53 +0200756 device_unregister(&ne->node_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 device_unregister(dev);
758
759 put_device(dev);
760}
761
Stefan Richter11305c32008-08-19 21:29:23 +0200762static int remove_host_dev(struct device *dev, void *data)
gregkh@suse.de643603222005-03-25 11:45:31 -0800763{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200764 if (dev->bus == &ieee1394_bus_type)
765 nodemgr_remove_ne(container_of(dev, struct node_entry,
766 device));
gregkh@suse.de643603222005-03-25 11:45:31 -0800767 return 0;
768}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770static void nodemgr_remove_host_dev(struct device *dev)
771{
Stefan Richter11305c32008-08-19 21:29:23 +0200772 device_for_each_child(dev, NULL, remove_host_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 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
Stefan Richterd41bba22006-11-22 21:28:19 +0100786 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
Stefan Richterd41bba22006-11-22 21:28:19 +0100794 ne->busopt.generation = (busoptions >> 4) & 0xf;
795 ne->busopt.lnkspd = busoptions & 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
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
Stefan Richter11305c32008-08-19 21:29:23 +0200807static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Stefan Richter85511582005-11-07 06:31:45 -0500811 struct node_entry *ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Stefan Richter85511582005-11-07 06:31:45 -0500813 ne = kzalloc(sizeof(*ne), GFP_KERNEL);
814 if (!ne)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200815 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Stefan Richter85511582005-11-07 06:31:45 -0500817 ne->host = host;
818 ne->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ne->generation = generation;
Stefan Richter68484082008-08-16 13:36:47 +0200820 ne->needs_probe = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Stefan Richter85511582005-11-07 06:31:45 -0500822 ne->guid = guid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ne->csr = csr;
825
826 memcpy(&ne->device, &nodemgr_dev_template_ne,
827 sizeof(ne->device));
828 ne->device.parent = &host->device;
Kay Sievers233976e2008-10-30 01:49:20 +0100829 dev_set_name(&ne->device, "%016Lx", (unsigned long long)(ne->guid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Kay Sieversdd7f2922007-05-25 11:50:53 +0200831 ne->node_dev.parent = &ne->device;
832 ne->node_dev.class = &nodemgr_ne_class;
Kay Sievers233976e2008-10-30 01:49:20 +0100833 dev_set_name(&ne->node_dev, "%016Lx", (unsigned long long)(ne->guid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200835 if (device_register(&ne->device))
836 goto fail_devreg;
Kay Sieversdd7f2922007-05-25 11:50:53 +0200837 if (device_register(&ne->node_dev))
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200838 goto fail_classdevreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 get_device(&ne->device);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 nodemgr_create_ne_dev_files(ne);
842
843 nodemgr_update_bus_options(ne);
844
845 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
846 (host->node_id == nodeid) ? "Host" : "Node",
847 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
848
Stefan Richter85511582005-11-07 06:31:45 -0500849 return ne;
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200850
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200851fail_classdevreg:
852 device_unregister(&ne->device);
853fail_devreg:
854 kfree(ne);
855fail_alloc:
856 HPSB_ERR("Failed to create node ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
857 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
858
859 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Stefan Richter11305c32008-08-19 21:29:23 +0200862static int match_ne_guid(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +0800863{
864 struct node_entry *ne;
Stefan Richter11305c32008-08-19 21:29:23 +0200865 u64 *guid = data;
Dave Young73cf6022008-01-22 13:56:32 +0800866
867 ne = container_of(dev, struct node_entry, node_dev);
868 return ne->guid == *guid;
869}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871static struct node_entry *find_entry_by_guid(u64 guid)
872{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200873 struct device *dev;
Dave Young73cf6022008-01-22 13:56:32 +0800874 struct node_entry *ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Stefan Richter11305c32008-08-19 21:29:23 +0200876 dev = class_find_device(&nodemgr_ne_class, NULL, &guid, match_ne_guid);
Dave Young73cf6022008-01-22 13:56:32 +0800877 if (!dev)
878 return NULL;
879 ne = container_of(dev, struct node_entry, node_dev);
880 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Dave Young73cf6022008-01-22 13:56:32 +0800882 return ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Stefan Richter11305c32008-08-19 21:29:23 +0200885struct match_nodeid_parameter {
Dave Young73cf6022008-01-22 13:56:32 +0800886 struct hpsb_host *host;
887 nodeid_t nodeid;
888};
889
Stefan Richter11305c32008-08-19 21:29:23 +0200890static int match_ne_nodeid(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +0800891{
892 int found = 0;
893 struct node_entry *ne;
Stefan Richter11305c32008-08-19 21:29:23 +0200894 struct match_nodeid_parameter *p = data;
Dave Young73cf6022008-01-22 13:56:32 +0800895
896 if (!dev)
897 goto ret;
898 ne = container_of(dev, struct node_entry, node_dev);
Stefan Richter11305c32008-08-19 21:29:23 +0200899 if (ne->host == p->host && ne->nodeid == p->nodeid)
Dave Young73cf6022008-01-22 13:56:32 +0800900 found = 1;
901ret:
902 return found;
903}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Stefan Richterb07375b2006-10-22 16:16:27 +0200905static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
906 nodeid_t nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200908 struct device *dev;
Dave Young73cf6022008-01-22 13:56:32 +0800909 struct node_entry *ne;
Stefan Richter11305c32008-08-19 21:29:23 +0200910 struct match_nodeid_parameter p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Stefan Richter11305c32008-08-19 21:29:23 +0200912 p.host = host;
913 p.nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Stefan Richter11305c32008-08-19 21:29:23 +0200915 dev = class_find_device(&nodemgr_ne_class, NULL, &p, match_ne_nodeid);
Dave Young73cf6022008-01-22 13:56:32 +0800916 if (!dev)
917 return NULL;
918 ne = container_of(dev, struct node_entry, node_dev);
919 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Dave Young73cf6022008-01-22 13:56:32 +0800921 return ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924
925static void nodemgr_register_device(struct node_entry *ne,
926 struct unit_directory *ud, struct device *parent)
927{
928 memcpy(&ud->device, &nodemgr_dev_template_ud,
929 sizeof(ud->device));
930
931 ud->device.parent = parent;
932
Kay Sievers233976e2008-10-30 01:49:20 +0100933 dev_set_name(&ud->device, "%s-%u", dev_name(&ne->device), ud->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Kay Sieversdd7f2922007-05-25 11:50:53 +0200935 ud->unit_dev.parent = &ud->device;
936 ud->unit_dev.class = &nodemgr_ud_class;
Kay Sievers233976e2008-10-30 01:49:20 +0100937 dev_set_name(&ud->unit_dev, "%s-%u", dev_name(&ne->device), ud->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200939 if (device_register(&ud->device))
940 goto fail_devreg;
Kay Sieversdd7f2922007-05-25 11:50:53 +0200941 if (device_register(&ud->unit_dev))
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200942 goto fail_classdevreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 get_device(&ud->device);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 nodemgr_create_ud_dev_files(ud);
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200946
947 return;
948
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200949fail_classdevreg:
950 device_unregister(&ud->device);
951fail_devreg:
Kay Sievers233976e2008-10-30 01:49:20 +0100952 HPSB_ERR("Failed to create unit %s", dev_name(&ud->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955
956/* This implementation currently only scans the config rom and its
957 * immediate unit directories looking for software_id and
958 * software_version entries, in order to get driver autoloading working. */
959static struct unit_directory *nodemgr_process_unit_directory
Stefan Richter11305c32008-08-19 21:29:23 +0200960 (struct node_entry *ne, struct csr1212_keyval *ud_kv,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 unsigned int *id, struct unit_directory *parent)
962{
963 struct unit_directory *ud;
964 struct unit_directory *ud_child = NULL;
965 struct csr1212_dentry *dentry;
966 struct csr1212_keyval *kv;
967 u8 last_key_id = 0;
968
Stefan Richter85511582005-11-07 06:31:45 -0500969 ud = kzalloc(sizeof(*ud), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!ud)
971 goto unit_directory_error;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ud->ne = ne;
974 ud->ignore_driver = ignore_drivers;
Stefan Richtera52938f2007-05-27 13:11:47 +0200975 ud->address = ud_kv->offset + CSR1212_REGISTER_SPACE_BASE;
Stefan Richterd7794c82007-05-27 13:17:15 +0200976 ud->directory_id = ud->address & 0xffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 ud->ud_kv = ud_kv;
978 ud->id = (*id)++;
979
980 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
981 switch (kv->key.id) {
982 case CSR1212_KV_ID_VENDOR:
983 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
984 ud->vendor_id = kv->value.immediate;
985 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 break;
988
989 case CSR1212_KV_ID_MODEL:
990 ud->model_id = kv->value.immediate;
991 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
992 break;
993
994 case CSR1212_KV_ID_SPECIFIER_ID:
995 ud->specifier_id = kv->value.immediate;
996 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
997 break;
998
999 case CSR1212_KV_ID_VERSION:
1000 ud->version = kv->value.immediate;
1001 ud->flags |= UNIT_DIRECTORY_VERSION;
1002 break;
1003
1004 case CSR1212_KV_ID_DESCRIPTOR:
1005 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1006 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1007 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1008 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1009 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1010 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1011 switch (last_key_id) {
1012 case CSR1212_KV_ID_VENDOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 csr1212_keep_keyval(kv);
Stefan Richter17a19b72007-09-15 14:50:25 +02001014 ud->vendor_name_kv = kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
1016
1017 case CSR1212_KV_ID_MODEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 csr1212_keep_keyval(kv);
Stefan Richter17a19b72007-09-15 14:50:25 +02001019 ud->model_name_kv = kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021
1022 }
1023 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
1024 break;
1025
1026 case CSR1212_KV_ID_DEPENDENT_INFO:
1027 /* Logical Unit Number */
1028 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1029 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
Eric Sesterhennbfe89d72006-10-29 23:13:40 +03001030 ud_child = kmemdup(ud, sizeof(*ud_child), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!ud_child)
1032 goto unit_directory_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 nodemgr_register_device(ne, ud_child, &ne->device);
1034 ud_child = NULL;
1035
1036 ud->id = (*id)++;
1037 }
1038 ud->lun = kv->value.immediate;
1039 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
1040
1041 /* Logical Unit Directory */
1042 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
1043 /* This should really be done in SBP2 as this is
1044 * doing SBP2 specific parsing.
1045 */
1046
1047 /* first register the parent unit */
1048 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
1049 if (ud->device.bus != &ieee1394_bus_type)
1050 nodemgr_register_device(ne, ud, &ne->device);
1051
1052 /* process the child unit */
Stefan Richter11305c32008-08-19 21:29:23 +02001053 ud_child = nodemgr_process_unit_directory(ne, kv, id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (ud_child == NULL)
1056 break;
1057
Kay Sievers312c0042005-11-16 09:00:00 +01001058 /* inherit unspecified values, the driver core picks it up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
1060 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
1061 {
1062 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
1063 ud_child->model_id = ud->model_id;
1064 }
1065 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
1066 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
1067 {
1068 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1069 ud_child->specifier_id = ud->specifier_id;
1070 }
1071 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
1072 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
1073 {
1074 ud_child->flags |= UNIT_DIRECTORY_VERSION;
1075 ud_child->version = ud->version;
1076 }
1077
1078 /* register the child unit */
1079 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
1080 nodemgr_register_device(ne, ud_child, &ud->device);
1081 }
1082
1083 break;
1084
Stefan Richterd7794c82007-05-27 13:17:15 +02001085 case CSR1212_KV_ID_DIRECTORY_ID:
1086 ud->directory_id = kv->value.immediate;
1087 break;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 default:
1090 break;
1091 }
1092 last_key_id = kv->key.id;
1093 }
1094
1095 /* do not process child units here and only if not already registered */
1096 if (!parent && ud->device.bus != &ieee1394_bus_type)
1097 nodemgr_register_device(ne, ud, &ne->device);
1098
1099 return ud;
1100
1101unit_directory_error:
Jody McIntyre616b8592005-05-16 21:54:01 -07001102 kfree(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return NULL;
1104}
1105
1106
Stefan Richter11305c32008-08-19 21:29:23 +02001107static void nodemgr_process_root_directory(struct node_entry *ne)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 unsigned int ud_id = 0;
1110 struct csr1212_dentry *dentry;
Stefan Richter638d5bb2007-09-15 14:45:53 +02001111 struct csr1212_keyval *kv, *vendor_name_kv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 u8 last_key_id = 0;
1113
Stefan Richter68484082008-08-16 13:36:47 +02001114 ne->needs_probe = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1117 switch (kv->key.id) {
1118 case CSR1212_KV_ID_VENDOR:
1119 ne->vendor_id = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 break;
1121
1122 case CSR1212_KV_ID_NODE_CAPABILITIES:
1123 ne->capabilities = kv->value.immediate;
1124 break;
1125
1126 case CSR1212_KV_ID_UNIT:
Stefan Richter11305c32008-08-19 21:29:23 +02001127 nodemgr_process_unit_directory(ne, kv, &ud_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129
1130 case CSR1212_KV_ID_DESCRIPTOR:
1131 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1132 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1133 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1134 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1135 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1136 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1137 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 csr1212_keep_keyval(kv);
Stefan Richter638d5bb2007-09-15 14:45:53 +02001139 vendor_name_kv = kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
1142 break;
1143 }
1144 last_key_id = kv->key.id;
1145 }
1146
Stefan Richter93245472007-03-30 19:19:55 +02001147 if (ne->vendor_name_kv) {
Stefan Richter638d5bb2007-09-15 14:45:53 +02001148 kv = ne->vendor_name_kv;
1149 ne->vendor_name_kv = vendor_name_kv;
1150 csr1212_release_keyval(kv);
1151 } else if (vendor_name_kv) {
1152 ne->vendor_name_kv = vendor_name_kv;
1153 if (device_create_file(&ne->device,
1154 &dev_attr_ne_vendor_name_kv) != 0)
Stefan Richter9be51c52007-03-30 19:21:05 +02001155 HPSB_ERR("Failed to add sysfs attribute");
Stefan Richter93245472007-03-30 19:19:55 +02001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159#ifdef CONFIG_HOTPLUG
1160
Kay Sievers7eff2e72007-08-14 15:15:12 +02001161static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct unit_directory *ud;
Eric Rannaudbf624562007-03-30 22:23:12 -07001164 int retval = 0;
Olaf Hering9b19d852005-09-06 15:18:18 -07001165 /* ieee1394:venNmoNspNverN */
1166 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Kay Sieversdd7f2922007-05-25 11:50:53 +02001168 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -ENODEV;
1170
Kay Sieversdd7f2922007-05-25 11:50:53 +02001171 ud = container_of(dev, struct unit_directory, unit_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (ud->ne->in_limbo || ud->ignore_driver)
1174 return -ENODEV;
1175
1176#define PUT_ENVP(fmt,val) \
1177do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001178 retval = add_uevent_var(env, fmt, val); \
Eric Rannaudbf624562007-03-30 22:23:12 -07001179 if (retval) \
1180 return retval; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181} while (0)
1182
1183 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1184 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1185 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1186 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1187 PUT_ENVP("VERSION=%06x", ud->version);
Olaf Hering9b19d852005-09-06 15:18:18 -07001188 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1189 ud->vendor_id,
1190 ud->model_id,
1191 ud->specifier_id,
1192 ud->version);
1193 PUT_ENVP("MODALIAS=%s", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195#undef PUT_ENVP
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198}
1199
1200#else
1201
Kay Sievers7eff2e72007-08-14 15:15:12 +02001202static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 return -ENODEV;
1205}
1206
1207#endif /* CONFIG_HOTPLUG */
1208
1209
Ben Collinsed30c262006-11-23 13:59:48 -05001210int __hpsb_register_protocol(struct hpsb_protocol_driver *drv,
1211 struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Ben Collinsed30c262006-11-23 13:59:48 -05001213 int error;
1214
1215 drv->driver.bus = &ieee1394_bus_type;
1216 drv->driver.owner = owner;
1217 drv->driver.name = drv->name;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* This will cause a probe for devices */
Ben Collinsed30c262006-11-23 13:59:48 -05001220 error = driver_register(&drv->driver);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001221 if (!error)
Ben Collinsed30c262006-11-23 13:59:48 -05001222 nodemgr_create_drv_files(drv);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001223 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1227{
1228 nodemgr_remove_drv_files(driver);
1229 /* This will subsequently disconnect all devices that our driver
1230 * is attached to. */
1231 driver_unregister(&driver->driver);
1232}
1233
1234
1235/*
1236 * This function updates nodes that were present on the bus before the
1237 * reset and still are after the reset. The nodeid and the config rom
1238 * may have changed, and the drivers managing this device must be
1239 * informed that this device just went through a bus reset, to allow
1240 * the to take whatever actions required.
1241 */
1242static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
Stefan Richter11305c32008-08-19 21:29:23 +02001243 nodeid_t nodeid, unsigned int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 if (ne->nodeid != nodeid) {
1246 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1247 NODE_BUS_ARGS(ne->host, ne->nodeid),
1248 NODE_BUS_ARGS(ne->host, nodeid));
1249 ne->nodeid = nodeid;
1250 }
1251
1252 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1253 kfree(ne->csr->private);
1254 csr1212_destroy_csr(ne->csr);
1255 ne->csr = csr;
1256
1257 /* If the node's configrom generation has changed, we
1258 * unregister all the unit directories. */
1259 nodemgr_remove_uds(ne);
1260
1261 nodemgr_update_bus_options(ne);
1262
1263 /* Mark the node as new, so it gets re-probed */
Stefan Richter68484082008-08-16 13:36:47 +02001264 ne->needs_probe = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 } else {
1266 /* old cache is valid, so update its generation */
1267 struct nodemgr_csr_info *ci = ne->csr->private;
1268 ci->generation = generation;
1269 /* free the partially filled now unneeded new cache */
1270 kfree(csr->private);
1271 csr1212_destroy_csr(csr);
1272 }
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* Mark the node current */
1275 ne->generation = generation;
Stefan Richterfc392fe2008-08-19 21:30:17 +02001276
1277 if (ne->in_limbo) {
1278 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1279 ne->in_limbo = false;
1280
1281 HPSB_DEBUG("Node reactivated: "
1282 "ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1283 NODE_BUS_ARGS(ne->host, ne->nodeid),
1284 (unsigned long long)ne->guid);
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Stefan Richter11305c32008-08-19 21:29:23 +02001288static void nodemgr_node_scan_one(struct hpsb_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 nodeid_t nodeid, int generation)
1290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 struct node_entry *ne;
1292 octlet_t guid;
1293 struct csr1212_csr *csr;
1294 struct nodemgr_csr_info *ci;
Stefan Richterd7530a12006-07-03 00:58:01 +02001295 u8 *speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Stefan Richter85511582005-11-07 06:31:45 -05001297 ci = kmalloc(sizeof(*ci), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!ci)
1299 return;
1300
1301 ci->host = host;
1302 ci->nodeid = nodeid;
1303 ci->generation = generation;
Stefan Richterd7530a12006-07-03 00:58:01 +02001304
1305 /* Prepare for speed probe which occurs when reading the ROM */
1306 speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
1307 if (*speed > host->csr.lnk_spd)
1308 *speed = host->csr.lnk_spd;
1309 ci->speed_unverified = *speed > IEEE1394_SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* We need to detect when the ConfigROM's generation has changed,
1312 * so we only update the node's info when it needs to be. */
1313
1314 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1315 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1316 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1317 NODE_BUS_ARGS(host, nodeid));
1318 if (csr)
1319 csr1212_destroy_csr(csr);
1320 kfree(ci);
1321 return;
1322 }
1323
1324 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1325 /* This isn't a 1394 device, but we let it slide. There
1326 * was a report of a device with broken firmware which
1327 * reported '2394' instead of '1394', which is obviously a
1328 * mistake. One would hope that a non-1394 device never
1329 * gets connected to Firewire bus. If someone does, we
1330 * shouldn't be held responsible, so we'll allow it with a
1331 * warning. */
1332 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1333 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1334 }
1335
1336 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1337 ne = find_entry_by_guid(guid);
1338
1339 if (ne && ne->host != host && ne->in_limbo) {
1340 /* Must have moved this device from one host to another */
1341 nodemgr_remove_ne(ne);
1342 ne = NULL;
1343 }
1344
1345 if (!ne)
Stefan Richter11305c32008-08-19 21:29:23 +02001346 nodemgr_create_node(guid, csr, host, nodeid, generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 else
Stefan Richter11305c32008-08-19 21:29:23 +02001348 nodemgr_update_node(ne, csr, nodeid, generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351
Stefan Richter11305c32008-08-19 21:29:23 +02001352static void nodemgr_node_scan(struct hpsb_host *host, int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Stefan Richterd41bba22006-11-22 21:28:19 +01001354 int count;
Stefan Richterd41bba22006-11-22 21:28:19 +01001355 struct selfid *sid = (struct selfid *)host->topology_map;
1356 nodeid_t nodeid = LOCAL_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Stefan Richterd41bba22006-11-22 21:28:19 +01001358 /* Scan each node on the bus */
1359 for (count = host->selfid_count; count; count--, sid++) {
1360 if (sid->extended)
1361 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Stefan Richterd41bba22006-11-22 21:28:19 +01001363 if (!sid->link_active) {
1364 nodeid++;
1365 continue;
1366 }
Stefan Richter11305c32008-08-19 21:29:23 +02001367 nodemgr_node_scan_one(host, nodeid++, generation);
Stefan Richterd41bba22006-11-22 21:28:19 +01001368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Stefan Richter11305c32008-08-19 21:29:23 +02001371static void nodemgr_pause_ne(struct node_entry *ne)
1372{
Stefan Richterfc392fe2008-08-19 21:30:17 +02001373 HPSB_DEBUG("Node paused: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
Stefan Richter11305c32008-08-19 21:29:23 +02001374 NODE_BUS_ARGS(ne->host, ne->nodeid),
1375 (unsigned long long)ne->guid);
1376
Stefan Richterfc392fe2008-08-19 21:30:17 +02001377 ne->in_limbo = true;
Stefan Richter11305c32008-08-19 21:29:23 +02001378 WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Stefan Richter11305c32008-08-19 21:29:23 +02001381static int update_pdrv(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Stefan Richtera0e857e2007-06-17 23:47:45 +02001383 struct unit_directory *ud;
1384 struct device_driver *drv;
1385 struct hpsb_protocol_driver *pdrv;
Stefan Richter11305c32008-08-19 21:29:23 +02001386 struct node_entry *ne = data;
Stefan Richtera0e857e2007-06-17 23:47:45 +02001387 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Dave Young73cf6022008-01-22 13:56:32 +08001389 ud = container_of(dev, struct unit_directory, unit_dev);
1390 if (ud->ne == ne) {
Stefan Richtera0e857e2007-06-17 23:47:45 +02001391 drv = get_driver(ud->device.driver);
Dave Young73cf6022008-01-22 13:56:32 +08001392 if (drv) {
1393 error = 0;
1394 pdrv = container_of(drv, struct hpsb_protocol_driver,
1395 driver);
1396 if (pdrv->update) {
1397 down(&ud->device.sem);
1398 error = pdrv->update(ud);
1399 up(&ud->device.sem);
1400 }
1401 if (error)
1402 device_release_driver(&ud->device);
1403 put_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 }
Dave Young73cf6022008-01-22 13:56:32 +08001406
1407 return 0;
1408}
1409
1410static void nodemgr_update_pdrv(struct node_entry *ne)
1411{
Stefan Richter11305c32008-08-19 21:29:23 +02001412 class_for_each_device(&nodemgr_ud_class, NULL, ne, update_pdrv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Stefan Richter61c7f772005-12-05 16:28:59 -05001415/* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1416 * seems like an optional service but in the end it is practically mandatory
1417 * as a consequence of these clauses.
1418 *
1419 * Note that we cannot do a broadcast write to all nodes at once because some
1420 * pre-1394a devices would hang. */
1421static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1422{
1423 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1424 quadlet_t bc_remote, bc_local;
Stefan Richter7fdfc902006-10-21 01:23:56 +02001425 int error;
Stefan Richter61c7f772005-12-05 16:28:59 -05001426
1427 if (!ne->host->is_irm || ne->generation != generation ||
1428 ne->nodeid == ne->host->node_id)
1429 return;
1430
1431 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1432
1433 /* Check if the register is implemented and 1394a compliant. */
Stefan Richter7fdfc902006-10-21 01:23:56 +02001434 error = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1435 sizeof(bc_remote));
1436 if (!error && bc_remote & cpu_to_be32(0x80000000) &&
Stefan Richter61c7f772005-12-05 16:28:59 -05001437 bc_remote != bc_local)
1438 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1439}
1440
1441
Stefan Richter11305c32008-08-19 21:29:23 +02001442static void nodemgr_probe_ne(struct hpsb_host *host, struct node_entry *ne,
1443 int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct device *dev;
1446
Stefan Richter11305c32008-08-19 21:29:23 +02001447 if (ne->host != host || ne->in_limbo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return;
1449
1450 dev = get_device(&ne->device);
1451 if (!dev)
1452 return;
1453
Stefan Richter61c7f772005-12-05 16:28:59 -05001454 nodemgr_irm_write_bc(ne, generation);
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /* If "needs_probe", then this is either a new or changed node we
1457 * rescan totally. If the generation matches for an existing node
1458 * (one that existed prior to the bus reset) we send update calls
1459 * down to the drivers. Otherwise, this is a dead node and we
1460 * suspend it. */
1461 if (ne->needs_probe)
Stefan Richter11305c32008-08-19 21:29:23 +02001462 nodemgr_process_root_directory(ne);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 else if (ne->generation == generation)
1464 nodemgr_update_pdrv(ne);
1465 else
Stefan Richter11305c32008-08-19 21:29:23 +02001466 nodemgr_pause_ne(ne);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 put_device(dev);
1469}
1470
Stefan Richter11305c32008-08-19 21:29:23 +02001471struct node_probe_parameter {
1472 struct hpsb_host *host;
Dave Young73cf6022008-01-22 13:56:32 +08001473 int generation;
Stefan Richter68484082008-08-16 13:36:47 +02001474 bool probe_now;
Dave Young73cf6022008-01-22 13:56:32 +08001475};
1476
Stefan Richter68484082008-08-16 13:36:47 +02001477static int node_probe(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +08001478{
Stefan Richter11305c32008-08-19 21:29:23 +02001479 struct node_probe_parameter *p = data;
Dave Young73cf6022008-01-22 13:56:32 +08001480 struct node_entry *ne;
1481
Stefan Richter11305c32008-08-19 21:29:23 +02001482 if (p->generation != get_hpsb_generation(p->host))
Stefan Richterc921a972008-08-16 13:38:11 +02001483 return -EAGAIN;
1484
Dave Young73cf6022008-01-22 13:56:32 +08001485 ne = container_of(dev, struct node_entry, node_dev);
Stefan Richter68484082008-08-16 13:36:47 +02001486 if (ne->needs_probe == p->probe_now)
Stefan Richter11305c32008-08-19 21:29:23 +02001487 nodemgr_probe_ne(p->host, ne, p->generation);
Dave Young73cf6022008-01-22 13:56:32 +08001488 return 0;
1489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Stefan Richterfc392fe2008-08-19 21:30:17 +02001491static int nodemgr_node_probe(struct hpsb_host *host, int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Stefan Richter11305c32008-08-19 21:29:23 +02001493 struct node_probe_parameter p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Stefan Richter11305c32008-08-19 21:29:23 +02001495 p.host = host;
Stefan Richter68484082008-08-16 13:36:47 +02001496 p.generation = generation;
Stefan Richterc921a972008-08-16 13:38:11 +02001497 /*
1498 * Do some processing of the nodes we've probed. This pulls them
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * into the sysfs layer if needed, and can result in processing of
1500 * unit-directories, or just updating the node and it's
Stefan Richter51c1d802005-12-12 23:03:19 -05001501 * unit-directories.
1502 *
1503 * Run updates before probes. Usually, updates are time-critical
Stefan Richterc921a972008-08-16 13:38:11 +02001504 * while probes are time-consuming.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
Stefan Richterc921a972008-08-16 13:38:11 +02001506 * Meanwhile, another bus reset may have happened. In this case we
1507 * skip everything here and let the next bus scan handle it.
1508 * Otherwise we may prematurely remove nodes which are still there.
1509 */
1510 p.probe_now = false;
1511 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
Stefan Richterfc392fe2008-08-19 21:30:17 +02001512 return 0;
Stefan Richterc921a972008-08-16 13:38:11 +02001513
1514 p.probe_now = true;
1515 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
Stefan Richterfc392fe2008-08-19 21:30:17 +02001516 return 0;
Stefan Richterc921a972008-08-16 13:38:11 +02001517 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * Now let's tell the bus to rescan our devices. This may seem
1519 * like overhead, but the driver-model core will only scan a
1520 * device for a driver when either the device is added, or when a
1521 * new driver is added. A bus reset is a good reason to rescan
1522 * devices that were there before. For example, an sbp2 device
1523 * may become available for login, if the host that held it was
Stefan Richterc921a972008-08-16 13:38:11 +02001524 * just removed.
1525 */
1526 if (bus_rescan_devices(&ieee1394_bus_type) != 0)
1527 HPSB_DEBUG("bus_rescan_devices had an error");
Stefan Richterfc392fe2008-08-19 21:30:17 +02001528
1529 return 1;
1530}
1531
1532static int remove_nodes_in_limbo(struct device *dev, void *data)
1533{
1534 struct node_entry *ne;
1535
1536 if (dev->bus != &ieee1394_bus_type)
1537 return 0;
1538
1539 ne = container_of(dev, struct node_entry, device);
1540 if (ne->in_limbo)
1541 nodemgr_remove_ne(ne);
1542
1543 return 0;
1544}
1545
1546static void nodemgr_remove_nodes_in_limbo(struct hpsb_host *host)
1547{
1548 device_for_each_child(&host->device, NULL, remove_nodes_in_limbo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Stefan Richter14c0fa22005-12-01 18:51:52 -05001551static int nodemgr_send_resume_packet(struct hpsb_host *host)
1552{
1553 struct hpsb_packet *packet;
Stefan Richter7fdfc902006-10-21 01:23:56 +02001554 int error = -ENOMEM;
Stefan Richter14c0fa22005-12-01 18:51:52 -05001555
1556 packet = hpsb_make_phypacket(host,
Stefan Richterd7758462005-12-01 18:51:56 -05001557 EXTPHYPACKET_TYPE_RESUME |
1558 NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
Stefan Richter14c0fa22005-12-01 18:51:52 -05001559 if (packet) {
1560 packet->no_waiter = 1;
1561 packet->generation = get_hpsb_generation(host);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001562 error = hpsb_send_packet(packet);
Stefan Richter14c0fa22005-12-01 18:51:52 -05001563 }
Stefan Richter7fdfc902006-10-21 01:23:56 +02001564 if (error)
Stefan Richter14c0fa22005-12-01 18:51:52 -05001565 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1566 host->id);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001567 return error;
Stefan Richter14c0fa22005-12-01 18:51:52 -05001568}
1569
Stefan Richter61c7f772005-12-05 16:28:59 -05001570/* Perform a few high-level IRM responsibilities. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1572{
1573 quadlet_t bc;
1574
1575 /* if irm_id == -1 then there is no IRM on this bus */
1576 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1577 return 1;
1578
Stefan Richter61c7f772005-12-05 16:28:59 -05001579 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1580 host->csr.broadcast_channel |= 0x40000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* If there is no bus manager then we should set the root node's
1583 * force_root bit to promote bus stability per the 1394
1584 * spec. (8.4.2.6) */
1585 if (host->busmgr_id == 0xffff && host->node_count > 1)
1586 {
1587 u16 root_node = host->node_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Jody McIntyre328699b2005-09-30 11:59:08 -07001589 /* get cycle master capability flag from root node */
1590 if (host->is_cycmst ||
1591 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1592 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1593 &bc, sizeof(quadlet_t)) &&
1594 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 hpsb_send_phy_config(host, root_node, -1);
1596 else {
1597 HPSB_DEBUG("The root node is not cycle master capable; "
1598 "selecting a new root node and resetting...");
1599
1600 if (cycles >= 5) {
1601 /* Oh screw it! Just leave the bus as it is */
1602 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1603 return 1;
1604 }
1605
1606 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1607 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1608
1609 return 0;
1610 }
1611 }
1612
Stefan Richter14c0fa22005-12-01 18:51:52 -05001613 /* Some devices suspend their ports while being connected to an inactive
1614 * host adapter, i.e. if connected before the low-level driver is
1615 * loaded. They become visible either when physically unplugged and
1616 * replugged, or when receiving a resume packet. Send one once. */
1617 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1618 host->resume_packet_sent = 1;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return 1;
1621}
1622
1623/* We need to ensure that if we are not the IRM, that the IRM node is capable of
1624 * everything we can do, otherwise issue a bus reset and try to become the IRM
1625 * ourselves. */
1626static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1627{
1628 quadlet_t bc;
1629 int status;
1630
1631 if (hpsb_disable_irm || host->is_irm)
1632 return 1;
1633
1634 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1635 get_hpsb_generation(host),
1636 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1637 &bc, sizeof(quadlet_t));
1638
1639 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1640 /* The current irm node does not have a valid BROADCAST_CHANNEL
1641 * register and we do, so reset the bus with force_root set */
1642 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1643
1644 if (cycles >= 5) {
1645 /* Oh screw it! Just leave the bus as it is */
1646 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1647 return 1;
1648 }
1649
1650 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1651 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1652
1653 return 0;
1654 }
1655
1656 return 1;
1657}
1658
Stefan Richter11305c32008-08-19 21:29:23 +02001659static int nodemgr_host_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Stefan Richter11305c32008-08-19 21:29:23 +02001661 struct hpsb_host *host = data;
Stefan Richterb7a71792006-10-06 19:49:52 +02001662 unsigned int g, generation = 0;
Stefan Richterd2f119f2006-07-03 12:02:35 -04001663 int i, reset_cycles = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001665 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /* Setup our device-model entries */
1667 nodemgr_create_host_dev_files(host);
1668
Stefan Richterd2f119f2006-07-03 12:02:35 -04001669 for (;;) {
1670 /* Sleep until next bus reset */
1671 set_current_state(TASK_INTERRUPTIBLE);
Stefan Richtera65421e2007-02-10 22:06:18 +01001672 if (get_hpsb_generation(host) == generation &&
1673 !kthread_should_stop())
Stefan Richterd2f119f2006-07-03 12:02:35 -04001674 schedule();
1675 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Stefan Richterd2f119f2006-07-03 12:02:35 -04001677 /* Thread may have been woken up to freeze or to exit */
1678 if (try_to_freeze())
1679 continue;
1680 if (kthread_should_stop())
1681 goto exit;
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* Pause for 1/4 second in 1/16 second intervals,
1684 * to make sure things settle down. */
Stefan Richterd2f119f2006-07-03 12:02:35 -04001685 g = get_hpsb_generation(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 for (i = 0; i < 4 ; i++) {
Satyam Sharma69e2b602007-08-15 20:05:38 +05301687 msleep_interruptible(63);
1688 if (kthread_should_stop())
Stefan Richtera0e857e2007-06-17 23:47:45 +02001689 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* Now get the generation in which the node ID's we collect
1692 * are valid. During the bus scan we will use this generation
1693 * for the read transactions, so that if another reset occurs
1694 * during the scan the transactions will fail instead of
1695 * returning bogus data. */
1696 generation = get_hpsb_generation(host);
1697
1698 /* If we get a reset before we are done waiting, then
Michael Opdenacker59c51592007-05-09 08:57:56 +02001699 * start the waiting over again */
Stefan Richterd2f119f2006-07-03 12:02:35 -04001700 if (generation != g)
1701 g = generation, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Jody McIntyre328699b2005-09-30 11:59:08 -07001704 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1705 !nodemgr_do_irm_duties(host, reset_cycles)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 reset_cycles++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 continue;
1708 }
Jody McIntyre328699b2005-09-30 11:59:08 -07001709 reset_cycles = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 /* Scan our nodes to get the bus options and create node
1712 * entries. This does not do the sysfs stuff, since that
Kay Sievers312c0042005-11-16 09:00:00 +01001713 * would trigger uevents and such, which is a bad idea at
1714 * this point. */
Stefan Richter11305c32008-08-19 21:29:23 +02001715 nodemgr_node_scan(host, generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /* This actually does the full probe, with sysfs
1718 * registration. */
Stefan Richterfc392fe2008-08-19 21:30:17 +02001719 if (!nodemgr_node_probe(host, generation))
1720 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /* Update some of our sysfs symlinks */
1723 nodemgr_update_host_dev_links(host);
Stefan Richterfc392fe2008-08-19 21:30:17 +02001724
1725 /* Sleep 3 seconds */
1726 for (i = 3000/200; i; i--) {
1727 msleep_interruptible(200);
1728 if (kthread_should_stop())
1729 goto exit;
1730
1731 if (generation != get_hpsb_generation(host))
1732 break;
1733 }
1734 /* Remove nodes which are gone, unless a bus reset happened */
1735 if (!i)
1736 nodemgr_remove_nodes_in_limbo(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Stefan Richterd2f119f2006-07-03 12:02:35 -04001738exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 HPSB_VERBOSE("NodeMgr: Exiting thread");
Stefan Richterd2f119f2006-07-03 12:02:35 -04001740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Stefan Richter11305c32008-08-19 21:29:23 +02001743struct per_host_parameter {
Dave Young73cf6022008-01-22 13:56:32 +08001744 void *data;
1745 int (*cb)(struct hpsb_host *, void *);
1746};
1747
Stefan Richter11305c32008-08-19 21:29:23 +02001748static int per_host(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +08001749{
1750 struct hpsb_host *host;
Stefan Richter11305c32008-08-19 21:29:23 +02001751 struct per_host_parameter *p = data;
Dave Young73cf6022008-01-22 13:56:32 +08001752
1753 host = container_of(dev, struct hpsb_host, host_dev);
Stefan Richter11305c32008-08-19 21:29:23 +02001754 return p->cb(host, p->data);
Dave Young73cf6022008-01-22 13:56:32 +08001755}
Stefan Richter11305c32008-08-19 21:29:23 +02001756
Stefan Richterafd65462007-03-05 03:06:23 +01001757/**
1758 * nodemgr_for_each_host - call a function for each IEEE 1394 host
1759 * @data: an address to supply to the callback
1760 * @cb: function to call for each host
1761 *
1762 * Iterate the hosts, calling a given function with supplied data for each host.
1763 * If the callback fails on a host, i.e. if it returns a non-zero value, the
1764 * iteration is stopped.
1765 *
1766 * Return value: 0 on success, non-zero on failure (same as returned by last run
1767 * of the callback).
1768 */
1769int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Stefan Richter11305c32008-08-19 21:29:23 +02001771 struct per_host_parameter p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Stefan Richter11305c32008-08-19 21:29:23 +02001773 p.cb = cb;
1774 p.data = data;
1775 return class_for_each_device(&hpsb_host_class, NULL, &p, per_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Stefan Richteref815332007-03-05 03:05:32 +01001778/* The following two convenience functions use a struct node_entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 * for addressing a node on the bus. They are intended for use by any
1780 * process context, not just the nodemgr thread, so we need to be a
1781 * little careful when reading out the node ID and generation. The
1782 * thing that can go wrong is that we get the node ID, then a bus
1783 * reset occurs, and then we read the generation. The node ID is
1784 * possibly invalid, but the generation is current, and we end up
1785 * sending a packet to a the wrong node.
1786 *
1787 * The solution is to make sure we read the generation first, so that
1788 * if a reset occurs in the process, we end up with a stale generation
1789 * and the transactions will fail instead of silently using wrong node
1790 * ID's.
1791 */
1792
Stefan Richterafd65462007-03-05 03:06:23 +01001793/**
1794 * hpsb_node_fill_packet - fill some destination information into a packet
1795 * @ne: destination node
1796 * @packet: packet to fill in
1797 *
1798 * This will fill in the given, pre-initialised hpsb_packet with the current
1799 * information from the node entry (host, node ID, bus generation number).
1800 */
1801void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Stefan Richterafd65462007-03-05 03:06:23 +01001803 packet->host = ne->host;
1804 packet->generation = ne->generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 barrier();
Stefan Richterafd65462007-03-05 03:06:23 +01001806 packet->node_id = ne->nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
1809int hpsb_node_write(struct node_entry *ne, u64 addr,
1810 quadlet_t *buffer, size_t length)
1811{
1812 unsigned int generation = ne->generation;
1813
1814 barrier();
1815 return hpsb_write(ne->host, ne->nodeid, generation,
1816 addr, buffer, length);
1817}
1818
1819static void nodemgr_add_host(struct hpsb_host *host)
1820{
1821 struct host_info *hi;
1822
1823 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (!hi) {
Stefan Richterd2f119f2006-07-03 12:02:35 -04001825 HPSB_ERR("NodeMgr: out of memory in add host");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 hi->host = host;
Stefan Richter11305c32008-08-19 21:29:23 +02001829 hi->thread = kthread_run(nodemgr_host_thread, host, "knodemgrd_%d",
Stefan Richterd2f119f2006-07-03 12:02:35 -04001830 host->id);
1831 if (IS_ERR(hi->thread)) {
1832 HPSB_ERR("NodeMgr: cannot start thread for host %d", host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837static void nodemgr_host_reset(struct hpsb_host *host)
1838{
1839 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1840
Stefan Richterd2f119f2006-07-03 12:02:35 -04001841 if (hi) {
1842 HPSB_VERBOSE("NodeMgr: Processing reset for host %d", host->id);
1843 wake_up_process(hi->thread);
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847static void nodemgr_remove_host(struct hpsb_host *host)
1848{
1849 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1850
1851 if (hi) {
Stefan Richterd2f119f2006-07-03 12:02:35 -04001852 kthread_stop(hi->thread);
1853 nodemgr_remove_host_dev(&host->device);
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857static struct hpsb_highlevel nodemgr_highlevel = {
1858 .name = "Node manager",
1859 .add_host = nodemgr_add_host,
1860 .host_reset = nodemgr_host_reset,
1861 .remove_host = nodemgr_remove_host,
1862};
1863
1864int init_ieee1394_nodemgr(void)
1865{
Stefan Richter7fdfc902006-10-21 01:23:56 +02001866 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Stefan Richter7fdfc902006-10-21 01:23:56 +02001868 error = class_register(&nodemgr_ne_class);
1869 if (error)
Stefan Richter91efa462007-02-06 02:34:45 +01001870 goto fail_ne;
Stefan Richter7fdfc902006-10-21 01:23:56 +02001871 error = class_register(&nodemgr_ud_class);
Stefan Richter91efa462007-02-06 02:34:45 +01001872 if (error)
1873 goto fail_ud;
Stefan Richter8252bbb2006-11-22 21:09:42 +01001874 error = driver_register(&nodemgr_mid_layer_driver);
Stefan Richter91efa462007-02-06 02:34:45 +01001875 if (error)
1876 goto fail_ml;
1877 /* This driver is not used if nodemgr is off (disable_nodemgr=1). */
1878 nodemgr_dev_template_host.driver = &nodemgr_mid_layer_driver;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 hpsb_register_highlevel(&nodemgr_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
Stefan Richter91efa462007-02-06 02:34:45 +01001882
1883fail_ml:
1884 class_unregister(&nodemgr_ud_class);
1885fail_ud:
1886 class_unregister(&nodemgr_ne_class);
1887fail_ne:
1888 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
1891void cleanup_ieee1394_nodemgr(void)
1892{
Stefan Richterd41bba22006-11-22 21:28:19 +01001893 hpsb_unregister_highlevel(&nodemgr_highlevel);
Stefan Richter91efa462007-02-06 02:34:45 +01001894 driver_unregister(&nodemgr_mid_layer_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 class_unregister(&nodemgr_ud_class);
1896 class_unregister(&nodemgr_ne_class);
1897}