blob: 79ef5fd928ae42c0d8252f1d8bf07c479782be9c [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
Stefan Richter25a41b22008-12-13 01:43:59 +0100118#define OUI_FREECOM_TECHNOLOGIES_GMBH 0x0001db
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
121{
Stefan Richter25a41b22008-12-13 01:43:59 +0100122 /* Freecom FireWire Hard Drive firmware bug */
123 if (be32_to_cpu(bus_info_data[3]) >> 8 == OUI_FREECOM_TECHNOLOGIES_GMBH)
124 return 0;
125
Stefan Richter7fb9add2007-03-11 22:49:05 +0100126 return (be32_to_cpu(bus_info_data[2]) >> 8) & 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static struct csr1212_bus_ops nodemgr_csr_ops = {
130 .bus_read = nodemgr_bus_read,
131 .get_max_rom = nodemgr_get_max_rom
132};
133
134
135/*
136 * Basically what we do here is start off retrieving the bus_info block.
137 * From there will fill in some info about the node, verify it is of IEEE
138 * 1394 type, and that the crc checks out ok. After that we start off with
139 * the root directory, and subdirectories. To do this, we retrieve the
140 * quadlet header for a directory, find out the length, and retrieve the
141 * complete directory entry (be it a leaf or a directory). We then process
142 * it and add the info to our structure for that particular node.
143 *
144 * We verify CRC's along the way for each directory/block/leaf. The entire
145 * node structure is generic, and simply stores the information in a way
146 * that's easy to parse by the protocol interface.
147 */
148
149/*
150 * The nodemgr relies heavily on the Driver Model for device callbacks and
151 * driver/device mappings. The old nodemgr used to handle all this itself,
152 * but now we are much simpler because of the LDM.
153 */
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155struct host_info {
156 struct hpsb_host *host;
157 struct list_head list;
Stefan Richterd2f119f2006-07-03 12:02:35 -0400158 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
161static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200162static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164struct bus_type ieee1394_bus_type = {
165 .name = "ieee1394",
166 .match = nodemgr_bus_match,
167};
168
Kay Sieversdd7f2922007-05-25 11:50:53 +0200169static void host_cls_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200171 put_device(&container_of((dev), struct hpsb_host, host_dev)->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174struct class hpsb_host_class = {
175 .name = "ieee1394_host",
Kay Sieversdd7f2922007-05-25 11:50:53 +0200176 .dev_release = host_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Kay Sieversdd7f2922007-05-25 11:50:53 +0200179static void ne_cls_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200181 put_device(&container_of((dev), struct node_entry, node_dev)->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static struct class nodemgr_ne_class = {
185 .name = "ieee1394_node",
Kay Sieversdd7f2922007-05-25 11:50:53 +0200186 .dev_release = ne_cls_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Kay Sieversdd7f2922007-05-25 11:50:53 +0200189static void ud_cls_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200191 put_device(&container_of((dev), struct unit_directory, unit_dev)->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/* The name here is only so that unit directory hotplug works with old
Kay Sieversdd7f2922007-05-25 11:50:53 +0200195 * style hotplug, which only ever did unit directories anyway.
196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static struct class nodemgr_ud_class = {
198 .name = "ieee1394",
Kay Sieversdd7f2922007-05-25 11:50:53 +0200199 .dev_release = ud_cls_release,
200 .dev_uevent = nodemgr_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
203static struct hpsb_highlevel nodemgr_highlevel;
204
205
206static void nodemgr_release_ud(struct device *dev)
207{
208 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
209
210 if (ud->vendor_name_kv)
211 csr1212_release_keyval(ud->vendor_name_kv);
212 if (ud->model_name_kv)
213 csr1212_release_keyval(ud->model_name_kv);
214
215 kfree(ud);
216}
217
218static void nodemgr_release_ne(struct device *dev)
219{
220 struct node_entry *ne = container_of(dev, struct node_entry, device);
221
222 if (ne->vendor_name_kv)
223 csr1212_release_keyval(ne->vendor_name_kv);
224
225 kfree(ne);
226}
227
228
229static void nodemgr_release_host(struct device *dev)
230{
231 struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
232
233 csr1212_destroy_csr(host->csr.rom);
234
235 kfree(host);
236}
237
238static int nodemgr_ud_platform_data;
239
240static struct device nodemgr_dev_template_ud = {
241 .bus = &ieee1394_bus_type,
242 .release = nodemgr_release_ud,
243 .platform_data = &nodemgr_ud_platform_data,
244};
245
246static struct device nodemgr_dev_template_ne = {
247 .bus = &ieee1394_bus_type,
248 .release = nodemgr_release_ne,
249};
250
Stefan Richter8252bbb2006-11-22 21:09:42 +0100251/* This dummy driver prevents the host devices from being scanned. We have no
252 * useful drivers for them yet, and there would be a deadlock possible if the
253 * driver core scans the host device while the host's low-level driver (i.e.
254 * the host's parent device) is being removed. */
255static struct device_driver nodemgr_mid_layer_driver = {
256 .bus = &ieee1394_bus_type,
257 .name = "nodemgr",
258 .owner = THIS_MODULE,
259};
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261struct device nodemgr_dev_template_host = {
262 .bus = &ieee1394_bus_type,
263 .release = nodemgr_release_host,
264};
265
266
267#define fw_attr(class, class_type, field, type, format_string) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400268static ssize_t fw_show_##class##_##field (struct device *dev, struct device_attribute *attr, char *buf)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{ \
270 class_type *class; \
271 class = container_of(dev, class_type, device); \
272 return sprintf(buf, format_string, (type)class->field); \
273} \
274static struct device_attribute dev_attr_##class##_##field = { \
275 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
276 .show = fw_show_##class##_##field, \
277};
278
279#define fw_attr_td(class, class_type, td_kv) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400280static ssize_t fw_show_##class##_##td_kv (struct device *dev, struct device_attribute *attr, char *buf)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{ \
282 int len; \
283 class_type *class = container_of(dev, class_type, device); \
284 len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
285 memcpy(buf, \
286 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
287 len); \
Al Viro51ec1382007-07-15 20:59:51 +0100288 while (buf[len - 1] == '\0') \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 len--; \
290 buf[len++] = '\n'; \
291 buf[len] = '\0'; \
292 return len; \
293} \
294static struct device_attribute dev_attr_##class##_##td_kv = { \
295 .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
296 .show = fw_show_##class##_##td_kv, \
297};
298
299
300#define fw_drv_attr(field, type, format_string) \
301static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
302{ \
303 struct hpsb_protocol_driver *driver; \
304 driver = container_of(drv, struct hpsb_protocol_driver, driver); \
305 return sprintf(buf, format_string, (type)driver->field);\
306} \
307static struct driver_attribute driver_attr_drv_##field = { \
Stefan Richterd41bba22006-11-22 21:28:19 +0100308 .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
309 .show = fw_drv_show_##field, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312
Yani Ioannoue404e272005-05-17 06:42:58 -0400313static ssize_t fw_show_ne_bus_options(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct node_entry *ne = container_of(dev, struct node_entry, device);
316
317 return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
318 "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
319 ne->busopt.irmc,
320 ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
321 ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
322 ne->busopt.max_rec,
323 ne->busopt.max_rom,
324 ne->busopt.cyc_clk_acc);
325}
326static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
327
328
Stefan Richter99519032006-07-02 14:17:00 +0200329#ifdef HPSB_DEBUG_TLABELS
330static ssize_t fw_show_ne_tlabels_free(struct device *dev,
331 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct node_entry *ne = container_of(dev, struct node_entry, device);
Stefan Richter99519032006-07-02 14:17:00 +0200334 unsigned long flags;
335 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
336 int tf;
337
338 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
339 tf = 64 - bitmap_weight(tp, 64);
340 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
341
342 return sprintf(buf, "%d\n", tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
345
346
Stefan Richter99519032006-07-02 14:17:00 +0200347static ssize_t fw_show_ne_tlabels_mask(struct device *dev,
348 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct node_entry *ne = container_of(dev, struct node_entry, device);
Stefan Richter99519032006-07-02 14:17:00 +0200351 unsigned long flags;
352 unsigned long *tp = ne->host->tl_pool[NODEID_TO_NODE(ne->nodeid)].map;
353 u64 tm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Stefan Richter99519032006-07-02 14:17:00 +0200355 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#if (BITS_PER_LONG <= 32)
Stefan Richter99519032006-07-02 14:17:00 +0200357 tm = ((u64)tp[0] << 32) + tp[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#else
Stefan Richter99519032006-07-02 14:17:00 +0200359 tm = tp[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#endif
Stefan Richter99519032006-07-02 14:17:00 +0200361 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
362
Randy Dunlap7f588032006-10-23 21:44:36 -0700363 return sprintf(buf, "0x%016llx\n", (unsigned long long)tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
Stefan Richter99519032006-07-02 14:17:00 +0200366#endif /* HPSB_DEBUG_TLABELS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368
Yani Ioannoue404e272005-05-17 06:42:58 -0400369static 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 -0700370{
371 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
372 int state = simple_strtoul(buf, NULL, 10);
373
374 if (state == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ud->ignore_driver = 1;
Stefan Richterb07375b2006-10-22 16:16:27 +0200376 device_release_driver(dev);
Stefan Richterb07375b2006-10-22 16:16:27 +0200377 } else if (state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 ud->ignore_driver = 0;
379
380 return count;
381}
Yani Ioannoue404e272005-05-17 06:42:58 -0400382static ssize_t fw_get_ignore_driver(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct unit_directory *ud = container_of(dev, struct unit_directory, device);
385
386 return sprintf(buf, "%d\n", ud->ignore_driver);
387}
388static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
389
390
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200391static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf,
392 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200394 int error = 0;
395
Stefan Richter40fd89c2006-07-03 12:02:34 -0400396 if (simple_strtoul(buf, NULL, 10) == 1)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200397 error = bus_rescan_devices(&ieee1394_bus_type);
398 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
401{
402 return sprintf(buf, "You can force a rescan of the bus for "
403 "drivers by writing a 1 to this file\n");
404}
405static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
406
407
408static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
409{
410 int state = simple_strtoul(buf, NULL, 10);
411
412 if (state == 1)
413 ignore_drivers = 1;
Stefan Richterb07375b2006-10-22 16:16:27 +0200414 else if (state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ignore_drivers = 0;
416
417 return count;
418}
419static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
420{
421 return sprintf(buf, "%d\n", ignore_drivers);
422}
423static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
424
425
426struct bus_attribute *const fw_bus_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 &bus_attr_rescan,
428 &bus_attr_ignore_drivers,
429 NULL
430};
431
432
433fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
434fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
435
436fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
437fw_attr_td(ne, struct node_entry, vendor_name_kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
440fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
442
443static struct device_attribute *const fw_ne_attrs[] = {
444 &dev_attr_ne_guid,
445 &dev_attr_ne_guid_vendor_id,
446 &dev_attr_ne_capabilities,
447 &dev_attr_ne_vendor_id,
448 &dev_attr_ne_nodeid,
449 &dev_attr_bus_options,
Stefan Richter99519032006-07-02 14:17:00 +0200450#ifdef HPSB_DEBUG_TLABELS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 &dev_attr_tlabels_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 &dev_attr_tlabels_mask,
Stefan Richter99519032006-07-02 14:17:00 +0200453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
456
457
458fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
459fw_attr(ud, struct unit_directory, length, int, "%d\n")
460/* These are all dependent on the value being provided */
461fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
462fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
463fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
464fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
465fw_attr_td(ud, struct unit_directory, vendor_name_kv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466fw_attr_td(ud, struct unit_directory, model_name_kv)
467
468static struct device_attribute *const fw_ud_attrs[] = {
469 &dev_attr_ud_address,
470 &dev_attr_ud_length,
471 &dev_attr_ignore_driver,
472};
473
474
475fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
476fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
477fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
478fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
479fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
480fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
481fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
482fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
483
484static struct device_attribute *const fw_host_attrs[] = {
485 &dev_attr_host_node_count,
486 &dev_attr_host_selfid_count,
487 &dev_attr_host_nodes_active,
488 &dev_attr_host_in_bus_reset,
489 &dev_attr_host_is_root,
490 &dev_attr_host_is_cycmst,
491 &dev_attr_host_is_irm,
492 &dev_attr_host_is_busmgr,
493};
494
495
496static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
497{
498 struct hpsb_protocol_driver *driver;
499 struct ieee1394_device_id *id;
500 int length = 0;
501 char *scratch = buf;
502
Stefan Richterd41bba22006-11-22 21:28:19 +0100503 driver = container_of(drv, struct hpsb_protocol_driver, driver);
Stefan Richter07c72242008-05-01 10:43:04 +0200504 id = driver->id_table;
505 if (!id)
506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Stefan Richter07c72242008-05-01 10:43:04 +0200508 for (; id->match_flags != 0; id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int need_coma = 0;
510
511 if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
512 length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
513 scratch = buf + length;
514 need_coma++;
515 }
516
517 if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
518 length += sprintf(scratch, "%smodel_id=0x%06x",
519 need_coma++ ? "," : "",
520 id->model_id);
521 scratch = buf + length;
522 }
523
524 if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
525 length += sprintf(scratch, "%sspecifier_id=0x%06x",
526 need_coma++ ? "," : "",
527 id->specifier_id);
528 scratch = buf + length;
529 }
530
531 if (id->match_flags & IEEE1394_MATCH_VERSION) {
532 length += sprintf(scratch, "%sversion=0x%06x",
533 need_coma++ ? "," : "",
534 id->version);
535 scratch = buf + length;
536 }
537
538 if (need_coma) {
539 *scratch++ = '\n';
540 length++;
541 }
542 }
543
544 return length;
545}
546static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
547
548
549fw_drv_attr(name, const char *, "%s\n")
550
551static struct driver_attribute *const fw_drv_attrs[] = {
552 &driver_attr_drv_name,
553 &driver_attr_device_ids,
554};
555
556
557static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
558{
559 struct device_driver *drv = &driver->driver;
560 int i;
561
562 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200563 if (driver_create_file(drv, fw_drv_attrs[i]))
564 goto fail;
565 return;
566fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200567 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570
571static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
572{
573 struct device_driver *drv = &driver->driver;
574 int i;
575
576 for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
577 driver_remove_file(drv, fw_drv_attrs[i]);
578}
579
580
581static void nodemgr_create_ne_dev_files(struct node_entry *ne)
582{
583 struct device *dev = &ne->device;
584 int i;
585
586 for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200587 if (device_create_file(dev, fw_ne_attrs[i]))
588 goto fail;
589 return;
590fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200591 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594
595static void nodemgr_create_host_dev_files(struct hpsb_host *host)
596{
597 struct device *dev = &host->device;
598 int i;
599
600 for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200601 if (device_create_file(dev, fw_host_attrs[i]))
602 goto fail;
603 return;
604fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200605 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200609static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
610 nodeid_t nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612static void nodemgr_update_host_dev_links(struct hpsb_host *host)
613{
614 struct device *dev = &host->device;
615 struct node_entry *ne;
616
617 sysfs_remove_link(&dev->kobj, "irm_id");
618 sysfs_remove_link(&dev->kobj, "busmgr_id");
619 sysfs_remove_link(&dev->kobj, "host_id");
620
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200621 if ((ne = find_entry_by_nodeid(host, host->irm_id)) &&
622 sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id"))
623 goto fail;
624 if ((ne = find_entry_by_nodeid(host, host->busmgr_id)) &&
625 sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id"))
626 goto fail;
627 if ((ne = find_entry_by_nodeid(host, host->node_id)) &&
628 sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id"))
629 goto fail;
630 return;
631fail:
632 HPSB_ERR("Failed to update sysfs attributes for host %d", host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
636{
637 struct device *dev = &ud->device;
638 int i;
639
640 for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200641 if (device_create_file(dev, fw_ud_attrs[i]))
642 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200644 if (device_create_file(dev, &dev_attr_ud_specifier_id))
645 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (ud->flags & UNIT_DIRECTORY_VERSION)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200647 if (device_create_file(dev, &dev_attr_ud_version))
648 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200650 if (device_create_file(dev, &dev_attr_ud_vendor_id))
651 goto fail;
652 if (ud->vendor_name_kv &&
653 device_create_file(dev, &dev_attr_ud_vendor_name_kv))
654 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200657 if (device_create_file(dev, &dev_attr_ud_model_id))
658 goto fail;
659 if (ud->model_name_kv &&
660 device_create_file(dev, &dev_attr_ud_model_name_kv))
661 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200663 return;
664fail:
Stefan Richter9be51c52007-03-30 19:21:05 +0200665 HPSB_ERR("Failed to add sysfs attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668
669static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
670{
Stefan Richterd41bba22006-11-22 21:28:19 +0100671 struct hpsb_protocol_driver *driver;
672 struct unit_directory *ud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct ieee1394_device_id *id;
674
675 /* We only match unit directories */
676 if (dev->platform_data != &nodemgr_ud_platform_data)
677 return 0;
678
679 ud = container_of(dev, struct unit_directory, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (ud->ne->in_limbo || ud->ignore_driver)
681 return 0;
682
Stefan Richter8252bbb2006-11-22 21:09:42 +0100683 /* We only match drivers of type hpsb_protocol_driver */
684 if (drv == &nodemgr_mid_layer_driver)
685 return 0;
686
687 driver = container_of(drv, struct hpsb_protocol_driver, driver);
Stefan Richterd2ace292008-02-18 21:11:07 +0100688 id = driver->id_table;
689 if (!id)
690 return 0;
691
692 for (; id->match_flags != 0; id++) {
Stefan Richterd41bba22006-11-22 21:28:19 +0100693 if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
694 id->vendor_id != ud->vendor_id)
695 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Stefan Richterd41bba22006-11-22 21:28:19 +0100697 if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
698 id->model_id != ud->model_id)
699 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Stefan Richterd41bba22006-11-22 21:28:19 +0100701 if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
702 id->specifier_id != ud->specifier_id)
703 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Stefan Richterd41bba22006-11-22 21:28:19 +0100705 if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
706 id->version != ud->version)
707 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 return 1;
Stefan Richterd41bba22006-11-22 21:28:19 +0100710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 return 0;
713}
714
715
Stefan Richterb07375b2006-10-22 16:16:27 +0200716static DEFINE_MUTEX(nodemgr_serialize_remove_uds);
717
Stefan Richter11305c32008-08-19 21:29:23 +0200718static int match_ne(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +0800719{
720 struct unit_directory *ud;
Stefan Richter11305c32008-08-19 21:29:23 +0200721 struct node_entry *ne = data;
Dave Young73cf6022008-01-22 13:56:32 +0800722
723 ud = container_of(dev, struct unit_directory, unit_dev);
724 return ud->ne == ne;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static void nodemgr_remove_uds(struct node_entry *ne)
728{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200729 struct device *dev;
Dave Young73cf6022008-01-22 13:56:32 +0800730 struct unit_directory *ud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dave Young73cf6022008-01-22 13:56:32 +0800732 /* Use class_find device to iterate the devices. Since this code
733 * may be called from other contexts besides the knodemgrds,
734 * protect it by nodemgr_serialize_remove_uds.
Stefan Richterb07375b2006-10-22 16:16:27 +0200735 */
Stefan Richterb07375b2006-10-22 16:16:27 +0200736 mutex_lock(&nodemgr_serialize_remove_uds);
Stefan Richter1e4f7bc2006-12-02 22:23:34 +0100737 for (;;) {
Stefan Richter11305c32008-08-19 21:29:23 +0200738 dev = class_find_device(&nodemgr_ud_class, NULL, ne, match_ne);
Dave Young73cf6022008-01-22 13:56:32 +0800739 if (!dev)
Stefan Richter1e4f7bc2006-12-02 22:23:34 +0100740 break;
Dave Young73cf6022008-01-22 13:56:32 +0800741 ud = container_of(dev, struct unit_directory, unit_dev);
742 put_device(dev);
Kay Sieversdd7f2922007-05-25 11:50:53 +0200743 device_unregister(&ud->unit_dev);
Stefan Richter1e4f7bc2006-12-02 22:23:34 +0100744 device_unregister(&ud->device);
Stefan Richterb07375b2006-10-22 16:16:27 +0200745 }
Stefan Richterb07375b2006-10-22 16:16:27 +0200746 mutex_unlock(&nodemgr_serialize_remove_uds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749
750static void nodemgr_remove_ne(struct node_entry *ne)
751{
Stefan Richtercec1a312006-11-18 23:16:11 +0100752 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 dev = get_device(&ne->device);
755 if (!dev)
756 return;
757
758 HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
759 NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 nodemgr_remove_uds(ne);
761
Kay Sieversdd7f2922007-05-25 11:50:53 +0200762 device_unregister(&ne->node_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 device_unregister(dev);
764
765 put_device(dev);
766}
767
Stefan Richter11305c32008-08-19 21:29:23 +0200768static int remove_host_dev(struct device *dev, void *data)
gregkh@suse.de64360322005-03-25 11:45:31 -0800769{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200770 if (dev->bus == &ieee1394_bus_type)
771 nodemgr_remove_ne(container_of(dev, struct node_entry,
772 device));
gregkh@suse.de64360322005-03-25 11:45:31 -0800773 return 0;
774}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776static void nodemgr_remove_host_dev(struct device *dev)
777{
Stefan Richter11305c32008-08-19 21:29:23 +0200778 device_for_each_child(dev, NULL, remove_host_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 sysfs_remove_link(&dev->kobj, "irm_id");
780 sysfs_remove_link(&dev->kobj, "busmgr_id");
781 sysfs_remove_link(&dev->kobj, "host_id");
782}
783
784
785static void nodemgr_update_bus_options(struct node_entry *ne)
786{
787#ifdef CONFIG_IEEE1394_VERBOSEDEBUG
788 static const u16 mr[] = { 4, 64, 1024, 0};
789#endif
790 quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
791
Stefan Richterd41bba22006-11-22 21:28:19 +0100792 ne->busopt.irmc = (busoptions >> 31) & 1;
793 ne->busopt.cmc = (busoptions >> 30) & 1;
794 ne->busopt.isc = (busoptions >> 29) & 1;
795 ne->busopt.bmc = (busoptions >> 28) & 1;
796 ne->busopt.pmc = (busoptions >> 27) & 1;
797 ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
798 ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ne->busopt.max_rom = (busoptions >> 8) & 0x3;
Stefan Richterd41bba22006-11-22 21:28:19 +0100800 ne->busopt.generation = (busoptions >> 4) & 0xf;
801 ne->busopt.lnkspd = busoptions & 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
804 "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
805 busoptions, ne->busopt.irmc, ne->busopt.cmc,
806 ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
807 ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
808 mr[ne->busopt.max_rom],
809 ne->busopt.generation, ne->busopt.lnkspd);
810}
811
812
Stefan Richter11305c32008-08-19 21:29:23 +0200813static struct node_entry *nodemgr_create_node(octlet_t guid,
814 struct csr1212_csr *csr, struct hpsb_host *host,
815 nodeid_t nodeid, unsigned int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Stefan Richter85511582005-11-07 06:31:45 -0500817 struct node_entry *ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Stefan Richter85511582005-11-07 06:31:45 -0500819 ne = kzalloc(sizeof(*ne), GFP_KERNEL);
820 if (!ne)
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200821 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Stefan Richter85511582005-11-07 06:31:45 -0500823 ne->host = host;
824 ne->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ne->generation = generation;
Stefan Richter68484082008-08-16 13:36:47 +0200826 ne->needs_probe = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Stefan Richter85511582005-11-07 06:31:45 -0500828 ne->guid = guid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ne->guid_vendor_id = (guid >> 40) & 0xffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 ne->csr = csr;
831
832 memcpy(&ne->device, &nodemgr_dev_template_ne,
833 sizeof(ne->device));
834 ne->device.parent = &host->device;
Kay Sievers233976e2008-10-30 01:49:20 +0100835 dev_set_name(&ne->device, "%016Lx", (unsigned long long)(ne->guid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Kay Sieversdd7f2922007-05-25 11:50:53 +0200837 ne->node_dev.parent = &ne->device;
838 ne->node_dev.class = &nodemgr_ne_class;
Kay Sievers233976e2008-10-30 01:49:20 +0100839 dev_set_name(&ne->node_dev, "%016Lx", (unsigned long long)(ne->guid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200841 if (device_register(&ne->device))
842 goto fail_devreg;
Kay Sieversdd7f2922007-05-25 11:50:53 +0200843 if (device_register(&ne->node_dev))
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200844 goto fail_classdevreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 get_device(&ne->device);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 nodemgr_create_ne_dev_files(ne);
848
849 nodemgr_update_bus_options(ne);
850
851 HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
852 (host->node_id == nodeid) ? "Host" : "Node",
853 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
854
Stefan Richter85511582005-11-07 06:31:45 -0500855 return ne;
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200856
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200857fail_classdevreg:
858 device_unregister(&ne->device);
859fail_devreg:
860 kfree(ne);
861fail_alloc:
862 HPSB_ERR("Failed to create node ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
863 NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
864
865 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Stefan Richter11305c32008-08-19 21:29:23 +0200868static int match_ne_guid(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +0800869{
870 struct node_entry *ne;
Stefan Richter11305c32008-08-19 21:29:23 +0200871 u64 *guid = data;
Dave Young73cf6022008-01-22 13:56:32 +0800872
873 ne = container_of(dev, struct node_entry, node_dev);
874 return ne->guid == *guid;
875}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877static struct node_entry *find_entry_by_guid(u64 guid)
878{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200879 struct device *dev;
Dave Young73cf6022008-01-22 13:56:32 +0800880 struct node_entry *ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Stefan Richter11305c32008-08-19 21:29:23 +0200882 dev = class_find_device(&nodemgr_ne_class, NULL, &guid, match_ne_guid);
Dave Young73cf6022008-01-22 13:56:32 +0800883 if (!dev)
884 return NULL;
885 ne = container_of(dev, struct node_entry, node_dev);
886 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Dave Young73cf6022008-01-22 13:56:32 +0800888 return ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Stefan Richter11305c32008-08-19 21:29:23 +0200891struct match_nodeid_parameter {
Dave Young73cf6022008-01-22 13:56:32 +0800892 struct hpsb_host *host;
893 nodeid_t nodeid;
894};
895
Stefan Richter11305c32008-08-19 21:29:23 +0200896static int match_ne_nodeid(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +0800897{
898 int found = 0;
899 struct node_entry *ne;
Stefan Richter11305c32008-08-19 21:29:23 +0200900 struct match_nodeid_parameter *p = data;
Dave Young73cf6022008-01-22 13:56:32 +0800901
902 if (!dev)
903 goto ret;
904 ne = container_of(dev, struct node_entry, node_dev);
Stefan Richter11305c32008-08-19 21:29:23 +0200905 if (ne->host == p->host && ne->nodeid == p->nodeid)
Dave Young73cf6022008-01-22 13:56:32 +0800906 found = 1;
907ret:
908 return found;
909}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Stefan Richterb07375b2006-10-22 16:16:27 +0200911static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
912 nodeid_t nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Kay Sieversdd7f2922007-05-25 11:50:53 +0200914 struct device *dev;
Dave Young73cf6022008-01-22 13:56:32 +0800915 struct node_entry *ne;
Stefan Richter11305c32008-08-19 21:29:23 +0200916 struct match_nodeid_parameter p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Stefan Richter11305c32008-08-19 21:29:23 +0200918 p.host = host;
919 p.nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Stefan Richter11305c32008-08-19 21:29:23 +0200921 dev = class_find_device(&nodemgr_ne_class, NULL, &p, match_ne_nodeid);
Dave Young73cf6022008-01-22 13:56:32 +0800922 if (!dev)
923 return NULL;
924 ne = container_of(dev, struct node_entry, node_dev);
925 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dave Young73cf6022008-01-22 13:56:32 +0800927 return ne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930
931static void nodemgr_register_device(struct node_entry *ne,
932 struct unit_directory *ud, struct device *parent)
933{
934 memcpy(&ud->device, &nodemgr_dev_template_ud,
935 sizeof(ud->device));
936
937 ud->device.parent = parent;
938
Kay Sievers233976e2008-10-30 01:49:20 +0100939 dev_set_name(&ud->device, "%s-%u", dev_name(&ne->device), ud->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Kay Sieversdd7f2922007-05-25 11:50:53 +0200941 ud->unit_dev.parent = &ud->device;
942 ud->unit_dev.class = &nodemgr_ud_class;
Kay Sievers233976e2008-10-30 01:49:20 +0100943 dev_set_name(&ud->unit_dev, "%s-%u", dev_name(&ne->device), ud->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200945 if (device_register(&ud->device))
946 goto fail_devreg;
Kay Sieversdd7f2922007-05-25 11:50:53 +0200947 if (device_register(&ud->unit_dev))
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200948 goto fail_classdevreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 get_device(&ud->device);
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 nodemgr_create_ud_dev_files(ud);
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200952
953 return;
954
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200955fail_classdevreg:
956 device_unregister(&ud->device);
957fail_devreg:
Kay Sievers233976e2008-10-30 01:49:20 +0100958 HPSB_ERR("Failed to create unit %s", dev_name(&ud->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961
962/* This implementation currently only scans the config rom and its
963 * immediate unit directories looking for software_id and
964 * software_version entries, in order to get driver autoloading working. */
965static struct unit_directory *nodemgr_process_unit_directory
Stefan Richter11305c32008-08-19 21:29:23 +0200966 (struct node_entry *ne, struct csr1212_keyval *ud_kv,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 unsigned int *id, struct unit_directory *parent)
968{
969 struct unit_directory *ud;
970 struct unit_directory *ud_child = NULL;
971 struct csr1212_dentry *dentry;
972 struct csr1212_keyval *kv;
973 u8 last_key_id = 0;
974
Stefan Richter85511582005-11-07 06:31:45 -0500975 ud = kzalloc(sizeof(*ud), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!ud)
977 goto unit_directory_error;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 ud->ne = ne;
980 ud->ignore_driver = ignore_drivers;
Stefan Richtera52938f2007-05-27 13:11:47 +0200981 ud->address = ud_kv->offset + CSR1212_REGISTER_SPACE_BASE;
Stefan Richterd7794c82007-05-27 13:17:15 +0200982 ud->directory_id = ud->address & 0xffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ud->ud_kv = ud_kv;
984 ud->id = (*id)++;
985
986 csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
987 switch (kv->key.id) {
988 case CSR1212_KV_ID_VENDOR:
989 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
990 ud->vendor_id = kv->value.immediate;
991 ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 break;
994
995 case CSR1212_KV_ID_MODEL:
996 ud->model_id = kv->value.immediate;
997 ud->flags |= UNIT_DIRECTORY_MODEL_ID;
998 break;
999
1000 case CSR1212_KV_ID_SPECIFIER_ID:
1001 ud->specifier_id = kv->value.immediate;
1002 ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1003 break;
1004
1005 case CSR1212_KV_ID_VERSION:
1006 ud->version = kv->value.immediate;
1007 ud->flags |= UNIT_DIRECTORY_VERSION;
1008 break;
1009
1010 case CSR1212_KV_ID_DESCRIPTOR:
1011 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1012 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1013 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1014 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1015 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1016 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
1017 switch (last_key_id) {
1018 case CSR1212_KV_ID_VENDOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 csr1212_keep_keyval(kv);
Stefan Richter17a19b72007-09-15 14:50:25 +02001020 ud->vendor_name_kv = kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
1022
1023 case CSR1212_KV_ID_MODEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 csr1212_keep_keyval(kv);
Stefan Richter17a19b72007-09-15 14:50:25 +02001025 ud->model_name_kv = kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
1027
1028 }
1029 } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
1030 break;
1031
1032 case CSR1212_KV_ID_DEPENDENT_INFO:
1033 /* Logical Unit Number */
1034 if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1035 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
Eric Sesterhennbfe89d72006-10-29 23:13:40 +03001036 ud_child = kmemdup(ud, sizeof(*ud_child), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!ud_child)
1038 goto unit_directory_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 nodemgr_register_device(ne, ud_child, &ne->device);
1040 ud_child = NULL;
1041
1042 ud->id = (*id)++;
1043 }
1044 ud->lun = kv->value.immediate;
1045 ud->flags |= UNIT_DIRECTORY_HAS_LUN;
1046
1047 /* Logical Unit Directory */
1048 } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
1049 /* This should really be done in SBP2 as this is
1050 * doing SBP2 specific parsing.
1051 */
1052
1053 /* first register the parent unit */
1054 ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
1055 if (ud->device.bus != &ieee1394_bus_type)
1056 nodemgr_register_device(ne, ud, &ne->device);
1057
1058 /* process the child unit */
Stefan Richter11305c32008-08-19 21:29:23 +02001059 ud_child = nodemgr_process_unit_directory(ne, kv, id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (ud_child == NULL)
1062 break;
1063
Kay Sievers312c0042005-11-16 09:00:00 +01001064 /* inherit unspecified values, the driver core picks it up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
1066 !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
1067 {
1068 ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
1069 ud_child->model_id = ud->model_id;
1070 }
1071 if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
1072 !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
1073 {
1074 ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
1075 ud_child->specifier_id = ud->specifier_id;
1076 }
1077 if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
1078 !(ud_child->flags & UNIT_DIRECTORY_VERSION))
1079 {
1080 ud_child->flags |= UNIT_DIRECTORY_VERSION;
1081 ud_child->version = ud->version;
1082 }
1083
1084 /* register the child unit */
1085 ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
1086 nodemgr_register_device(ne, ud_child, &ud->device);
1087 }
1088
1089 break;
1090
Stefan Richterd7794c82007-05-27 13:17:15 +02001091 case CSR1212_KV_ID_DIRECTORY_ID:
1092 ud->directory_id = kv->value.immediate;
1093 break;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 default:
1096 break;
1097 }
1098 last_key_id = kv->key.id;
1099 }
1100
1101 /* do not process child units here and only if not already registered */
1102 if (!parent && ud->device.bus != &ieee1394_bus_type)
1103 nodemgr_register_device(ne, ud, &ne->device);
1104
1105 return ud;
1106
1107unit_directory_error:
Jody McIntyre616b8592005-05-16 21:54:01 -07001108 kfree(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return NULL;
1110}
1111
1112
Stefan Richter11305c32008-08-19 21:29:23 +02001113static void nodemgr_process_root_directory(struct node_entry *ne)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 unsigned int ud_id = 0;
1116 struct csr1212_dentry *dentry;
Stefan Richter638d5bb2007-09-15 14:45:53 +02001117 struct csr1212_keyval *kv, *vendor_name_kv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 u8 last_key_id = 0;
1119
Stefan Richter68484082008-08-16 13:36:47 +02001120 ne->needs_probe = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
1123 switch (kv->key.id) {
1124 case CSR1212_KV_ID_VENDOR:
1125 ne->vendor_id = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 break;
1127
1128 case CSR1212_KV_ID_NODE_CAPABILITIES:
1129 ne->capabilities = kv->value.immediate;
1130 break;
1131
1132 case CSR1212_KV_ID_UNIT:
Stefan Richter11305c32008-08-19 21:29:23 +02001133 nodemgr_process_unit_directory(ne, kv, &ud_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135
1136 case CSR1212_KV_ID_DESCRIPTOR:
1137 if (last_key_id == CSR1212_KV_ID_VENDOR) {
1138 if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
1139 CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
1140 CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
1141 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
1142 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
1143 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 csr1212_keep_keyval(kv);
Stefan Richter638d5bb2007-09-15 14:45:53 +02001145 vendor_name_kv = kv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 }
1148 break;
1149 }
1150 last_key_id = kv->key.id;
1151 }
1152
Stefan Richter93245472007-03-30 19:19:55 +02001153 if (ne->vendor_name_kv) {
Stefan Richter638d5bb2007-09-15 14:45:53 +02001154 kv = ne->vendor_name_kv;
1155 ne->vendor_name_kv = vendor_name_kv;
1156 csr1212_release_keyval(kv);
1157 } else if (vendor_name_kv) {
1158 ne->vendor_name_kv = vendor_name_kv;
1159 if (device_create_file(&ne->device,
1160 &dev_attr_ne_vendor_name_kv) != 0)
Stefan Richter9be51c52007-03-30 19:21:05 +02001161 HPSB_ERR("Failed to add sysfs attribute");
Stefan Richter93245472007-03-30 19:19:55 +02001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165#ifdef CONFIG_HOTPLUG
1166
Kay Sievers7eff2e72007-08-14 15:15:12 +02001167static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct unit_directory *ud;
Eric Rannaudbf624562007-03-30 22:23:12 -07001170 int retval = 0;
Olaf Hering9b19d852005-09-06 15:18:18 -07001171 /* ieee1394:venNmoNspNverN */
1172 char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Kay Sieversdd7f2922007-05-25 11:50:53 +02001174 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -ENODEV;
1176
Kay Sieversdd7f2922007-05-25 11:50:53 +02001177 ud = container_of(dev, struct unit_directory, unit_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (ud->ne->in_limbo || ud->ignore_driver)
1180 return -ENODEV;
1181
1182#define PUT_ENVP(fmt,val) \
1183do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001184 retval = add_uevent_var(env, fmt, val); \
Eric Rannaudbf624562007-03-30 22:23:12 -07001185 if (retval) \
1186 return retval; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187} while (0)
1188
1189 PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
1190 PUT_ENVP("MODEL_ID=%06x", ud->model_id);
1191 PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
1192 PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
1193 PUT_ENVP("VERSION=%06x", ud->version);
Olaf Hering9b19d852005-09-06 15:18:18 -07001194 snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
1195 ud->vendor_id,
1196 ud->model_id,
1197 ud->specifier_id,
1198 ud->version);
1199 PUT_ENVP("MODALIAS=%s", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201#undef PUT_ENVP
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return 0;
1204}
1205
1206#else
1207
Kay Sievers7eff2e72007-08-14 15:15:12 +02001208static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 return -ENODEV;
1211}
1212
1213#endif /* CONFIG_HOTPLUG */
1214
1215
Ben Collinsed30c262006-11-23 13:59:48 -05001216int __hpsb_register_protocol(struct hpsb_protocol_driver *drv,
1217 struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Ben Collinsed30c262006-11-23 13:59:48 -05001219 int error;
1220
1221 drv->driver.bus = &ieee1394_bus_type;
1222 drv->driver.owner = owner;
1223 drv->driver.name = drv->name;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* This will cause a probe for devices */
Ben Collinsed30c262006-11-23 13:59:48 -05001226 error = driver_register(&drv->driver);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001227 if (!error)
Ben Collinsed30c262006-11-23 13:59:48 -05001228 nodemgr_create_drv_files(drv);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001229 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
1233{
1234 nodemgr_remove_drv_files(driver);
1235 /* This will subsequently disconnect all devices that our driver
1236 * is attached to. */
1237 driver_unregister(&driver->driver);
1238}
1239
1240
1241/*
1242 * This function updates nodes that were present on the bus before the
1243 * reset and still are after the reset. The nodeid and the config rom
1244 * may have changed, and the drivers managing this device must be
1245 * informed that this device just went through a bus reset, to allow
1246 * the to take whatever actions required.
1247 */
1248static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
Stefan Richter11305c32008-08-19 21:29:23 +02001249 nodeid_t nodeid, unsigned int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 if (ne->nodeid != nodeid) {
1252 HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
1253 NODE_BUS_ARGS(ne->host, ne->nodeid),
1254 NODE_BUS_ARGS(ne->host, nodeid));
1255 ne->nodeid = nodeid;
1256 }
1257
1258 if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
1259 kfree(ne->csr->private);
1260 csr1212_destroy_csr(ne->csr);
1261 ne->csr = csr;
1262
1263 /* If the node's configrom generation has changed, we
1264 * unregister all the unit directories. */
1265 nodemgr_remove_uds(ne);
1266
1267 nodemgr_update_bus_options(ne);
1268
1269 /* Mark the node as new, so it gets re-probed */
Stefan Richter68484082008-08-16 13:36:47 +02001270 ne->needs_probe = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else {
1272 /* old cache is valid, so update its generation */
1273 struct nodemgr_csr_info *ci = ne->csr->private;
1274 ci->generation = generation;
1275 /* free the partially filled now unneeded new cache */
1276 kfree(csr->private);
1277 csr1212_destroy_csr(csr);
1278 }
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* Mark the node current */
1281 ne->generation = generation;
Stefan Richterfc392fe2008-08-19 21:30:17 +02001282
1283 if (ne->in_limbo) {
1284 device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
1285 ne->in_limbo = false;
1286
1287 HPSB_DEBUG("Node reactivated: "
1288 "ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
1289 NODE_BUS_ARGS(ne->host, ne->nodeid),
1290 (unsigned long long)ne->guid);
1291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Stefan Richter11305c32008-08-19 21:29:23 +02001294static void nodemgr_node_scan_one(struct hpsb_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 nodeid_t nodeid, int generation)
1296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct node_entry *ne;
1298 octlet_t guid;
1299 struct csr1212_csr *csr;
1300 struct nodemgr_csr_info *ci;
Stefan Richterd7530a12006-07-03 00:58:01 +02001301 u8 *speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Stefan Richter85511582005-11-07 06:31:45 -05001303 ci = kmalloc(sizeof(*ci), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!ci)
1305 return;
1306
1307 ci->host = host;
1308 ci->nodeid = nodeid;
1309 ci->generation = generation;
Stefan Richterd7530a12006-07-03 00:58:01 +02001310
1311 /* Prepare for speed probe which occurs when reading the ROM */
1312 speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
1313 if (*speed > host->csr.lnk_spd)
1314 *speed = host->csr.lnk_spd;
1315 ci->speed_unverified = *speed > IEEE1394_SPEED_100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* We need to detect when the ConfigROM's generation has changed,
1318 * so we only update the node's info when it needs to be. */
1319
1320 csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
1321 if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
1322 HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
1323 NODE_BUS_ARGS(host, nodeid));
1324 if (csr)
1325 csr1212_destroy_csr(csr);
1326 kfree(ci);
1327 return;
1328 }
1329
1330 if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
1331 /* This isn't a 1394 device, but we let it slide. There
1332 * was a report of a device with broken firmware which
1333 * reported '2394' instead of '1394', which is obviously a
1334 * mistake. One would hope that a non-1394 device never
1335 * gets connected to Firewire bus. If someone does, we
1336 * shouldn't be held responsible, so we'll allow it with a
1337 * warning. */
1338 HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
1339 NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
1340 }
1341
1342 guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
1343 ne = find_entry_by_guid(guid);
1344
1345 if (ne && ne->host != host && ne->in_limbo) {
1346 /* Must have moved this device from one host to another */
1347 nodemgr_remove_ne(ne);
1348 ne = NULL;
1349 }
1350
1351 if (!ne)
Stefan Richter11305c32008-08-19 21:29:23 +02001352 nodemgr_create_node(guid, csr, host, nodeid, generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 else
Stefan Richter11305c32008-08-19 21:29:23 +02001354 nodemgr_update_node(ne, csr, nodeid, generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357
Stefan Richter11305c32008-08-19 21:29:23 +02001358static void nodemgr_node_scan(struct hpsb_host *host, int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Stefan Richterd41bba22006-11-22 21:28:19 +01001360 int count;
Stefan Richterd41bba22006-11-22 21:28:19 +01001361 struct selfid *sid = (struct selfid *)host->topology_map;
1362 nodeid_t nodeid = LOCAL_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Stefan Richterd41bba22006-11-22 21:28:19 +01001364 /* Scan each node on the bus */
1365 for (count = host->selfid_count; count; count--, sid++) {
1366 if (sid->extended)
1367 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Stefan Richterd41bba22006-11-22 21:28:19 +01001369 if (!sid->link_active) {
1370 nodeid++;
1371 continue;
1372 }
Stefan Richter11305c32008-08-19 21:29:23 +02001373 nodemgr_node_scan_one(host, nodeid++, generation);
Stefan Richterd41bba22006-11-22 21:28:19 +01001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Stefan Richter11305c32008-08-19 21:29:23 +02001377static void nodemgr_pause_ne(struct node_entry *ne)
1378{
Stefan Richterfc392fe2008-08-19 21:30:17 +02001379 HPSB_DEBUG("Node paused: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
Stefan Richter11305c32008-08-19 21:29:23 +02001380 NODE_BUS_ARGS(ne->host, ne->nodeid),
1381 (unsigned long long)ne->guid);
1382
Stefan Richterfc392fe2008-08-19 21:30:17 +02001383 ne->in_limbo = true;
Stefan Richter11305c32008-08-19 21:29:23 +02001384 WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Stefan Richter11305c32008-08-19 21:29:23 +02001387static int update_pdrv(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Stefan Richtera0e857e2007-06-17 23:47:45 +02001389 struct unit_directory *ud;
1390 struct device_driver *drv;
1391 struct hpsb_protocol_driver *pdrv;
Stefan Richter11305c32008-08-19 21:29:23 +02001392 struct node_entry *ne = data;
Stefan Richtera0e857e2007-06-17 23:47:45 +02001393 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Dave Young73cf6022008-01-22 13:56:32 +08001395 ud = container_of(dev, struct unit_directory, unit_dev);
1396 if (ud->ne == ne) {
Stefan Richtera0e857e2007-06-17 23:47:45 +02001397 drv = get_driver(ud->device.driver);
Dave Young73cf6022008-01-22 13:56:32 +08001398 if (drv) {
1399 error = 0;
1400 pdrv = container_of(drv, struct hpsb_protocol_driver,
1401 driver);
1402 if (pdrv->update) {
1403 down(&ud->device.sem);
1404 error = pdrv->update(ud);
1405 up(&ud->device.sem);
1406 }
1407 if (error)
1408 device_release_driver(&ud->device);
1409 put_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 }
Dave Young73cf6022008-01-22 13:56:32 +08001412
1413 return 0;
1414}
1415
1416static void nodemgr_update_pdrv(struct node_entry *ne)
1417{
Stefan Richter11305c32008-08-19 21:29:23 +02001418 class_for_each_device(&nodemgr_ud_class, NULL, ne, update_pdrv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Stefan Richter61c7f772005-12-05 16:28:59 -05001421/* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
1422 * seems like an optional service but in the end it is practically mandatory
1423 * as a consequence of these clauses.
1424 *
1425 * Note that we cannot do a broadcast write to all nodes at once because some
1426 * pre-1394a devices would hang. */
1427static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
1428{
1429 const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
1430 quadlet_t bc_remote, bc_local;
Stefan Richter7fdfc902006-10-21 01:23:56 +02001431 int error;
Stefan Richter61c7f772005-12-05 16:28:59 -05001432
1433 if (!ne->host->is_irm || ne->generation != generation ||
1434 ne->nodeid == ne->host->node_id)
1435 return;
1436
1437 bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
1438
1439 /* Check if the register is implemented and 1394a compliant. */
Stefan Richter7fdfc902006-10-21 01:23:56 +02001440 error = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
1441 sizeof(bc_remote));
1442 if (!error && bc_remote & cpu_to_be32(0x80000000) &&
Stefan Richter61c7f772005-12-05 16:28:59 -05001443 bc_remote != bc_local)
1444 hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
1445}
1446
1447
Stefan Richter11305c32008-08-19 21:29:23 +02001448static void nodemgr_probe_ne(struct hpsb_host *host, struct node_entry *ne,
1449 int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct device *dev;
1452
Stefan Richter11305c32008-08-19 21:29:23 +02001453 if (ne->host != host || ne->in_limbo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return;
1455
1456 dev = get_device(&ne->device);
1457 if (!dev)
1458 return;
1459
Stefan Richter61c7f772005-12-05 16:28:59 -05001460 nodemgr_irm_write_bc(ne, generation);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* If "needs_probe", then this is either a new or changed node we
1463 * rescan totally. If the generation matches for an existing node
1464 * (one that existed prior to the bus reset) we send update calls
1465 * down to the drivers. Otherwise, this is a dead node and we
1466 * suspend it. */
1467 if (ne->needs_probe)
Stefan Richter11305c32008-08-19 21:29:23 +02001468 nodemgr_process_root_directory(ne);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 else if (ne->generation == generation)
1470 nodemgr_update_pdrv(ne);
1471 else
Stefan Richter11305c32008-08-19 21:29:23 +02001472 nodemgr_pause_ne(ne);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 put_device(dev);
1475}
1476
Stefan Richter11305c32008-08-19 21:29:23 +02001477struct node_probe_parameter {
1478 struct hpsb_host *host;
Dave Young73cf6022008-01-22 13:56:32 +08001479 int generation;
Stefan Richter68484082008-08-16 13:36:47 +02001480 bool probe_now;
Dave Young73cf6022008-01-22 13:56:32 +08001481};
1482
Stefan Richter68484082008-08-16 13:36:47 +02001483static int node_probe(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +08001484{
Stefan Richter11305c32008-08-19 21:29:23 +02001485 struct node_probe_parameter *p = data;
Dave Young73cf6022008-01-22 13:56:32 +08001486 struct node_entry *ne;
1487
Stefan Richter11305c32008-08-19 21:29:23 +02001488 if (p->generation != get_hpsb_generation(p->host))
Stefan Richterc921a972008-08-16 13:38:11 +02001489 return -EAGAIN;
1490
Dave Young73cf6022008-01-22 13:56:32 +08001491 ne = container_of(dev, struct node_entry, node_dev);
Stefan Richter68484082008-08-16 13:36:47 +02001492 if (ne->needs_probe == p->probe_now)
Stefan Richter11305c32008-08-19 21:29:23 +02001493 nodemgr_probe_ne(p->host, ne, p->generation);
Dave Young73cf6022008-01-22 13:56:32 +08001494 return 0;
1495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Stefan Richterfc392fe2008-08-19 21:30:17 +02001497static int nodemgr_node_probe(struct hpsb_host *host, int generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Stefan Richter11305c32008-08-19 21:29:23 +02001499 struct node_probe_parameter p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Stefan Richter11305c32008-08-19 21:29:23 +02001501 p.host = host;
Stefan Richter68484082008-08-16 13:36:47 +02001502 p.generation = generation;
Stefan Richterc921a972008-08-16 13:38:11 +02001503 /*
1504 * Do some processing of the nodes we've probed. This pulls them
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * into the sysfs layer if needed, and can result in processing of
1506 * unit-directories, or just updating the node and it's
Stefan Richter51c1d802005-12-12 23:03:19 -05001507 * unit-directories.
1508 *
1509 * Run updates before probes. Usually, updates are time-critical
Stefan Richterc921a972008-08-16 13:38:11 +02001510 * while probes are time-consuming.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 *
Stefan Richterc921a972008-08-16 13:38:11 +02001512 * Meanwhile, another bus reset may have happened. In this case we
1513 * skip everything here and let the next bus scan handle it.
1514 * Otherwise we may prematurely remove nodes which are still there.
1515 */
1516 p.probe_now = false;
1517 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
Stefan Richterfc392fe2008-08-19 21:30:17 +02001518 return 0;
Stefan Richterc921a972008-08-16 13:38:11 +02001519
1520 p.probe_now = true;
1521 if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
Stefan Richterfc392fe2008-08-19 21:30:17 +02001522 return 0;
Stefan Richterc921a972008-08-16 13:38:11 +02001523 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 * Now let's tell the bus to rescan our devices. This may seem
1525 * like overhead, but the driver-model core will only scan a
1526 * device for a driver when either the device is added, or when a
1527 * new driver is added. A bus reset is a good reason to rescan
1528 * devices that were there before. For example, an sbp2 device
1529 * may become available for login, if the host that held it was
Stefan Richterc921a972008-08-16 13:38:11 +02001530 * just removed.
1531 */
1532 if (bus_rescan_devices(&ieee1394_bus_type) != 0)
1533 HPSB_DEBUG("bus_rescan_devices had an error");
Stefan Richterfc392fe2008-08-19 21:30:17 +02001534
1535 return 1;
1536}
1537
1538static int remove_nodes_in_limbo(struct device *dev, void *data)
1539{
1540 struct node_entry *ne;
1541
1542 if (dev->bus != &ieee1394_bus_type)
1543 return 0;
1544
1545 ne = container_of(dev, struct node_entry, device);
1546 if (ne->in_limbo)
1547 nodemgr_remove_ne(ne);
1548
1549 return 0;
1550}
1551
1552static void nodemgr_remove_nodes_in_limbo(struct hpsb_host *host)
1553{
1554 device_for_each_child(&host->device, NULL, remove_nodes_in_limbo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Stefan Richter14c0fa22005-12-01 18:51:52 -05001557static int nodemgr_send_resume_packet(struct hpsb_host *host)
1558{
1559 struct hpsb_packet *packet;
Stefan Richter7fdfc902006-10-21 01:23:56 +02001560 int error = -ENOMEM;
Stefan Richter14c0fa22005-12-01 18:51:52 -05001561
1562 packet = hpsb_make_phypacket(host,
Stefan Richterd7758462005-12-01 18:51:56 -05001563 EXTPHYPACKET_TYPE_RESUME |
1564 NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
Stefan Richter14c0fa22005-12-01 18:51:52 -05001565 if (packet) {
1566 packet->no_waiter = 1;
1567 packet->generation = get_hpsb_generation(host);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001568 error = hpsb_send_packet(packet);
Stefan Richter14c0fa22005-12-01 18:51:52 -05001569 }
Stefan Richter7fdfc902006-10-21 01:23:56 +02001570 if (error)
Stefan Richter14c0fa22005-12-01 18:51:52 -05001571 HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
1572 host->id);
Stefan Richter7fdfc902006-10-21 01:23:56 +02001573 return error;
Stefan Richter14c0fa22005-12-01 18:51:52 -05001574}
1575
Stefan Richter61c7f772005-12-05 16:28:59 -05001576/* Perform a few high-level IRM responsibilities. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
1578{
1579 quadlet_t bc;
1580
1581 /* if irm_id == -1 then there is no IRM on this bus */
1582 if (!host->is_irm || host->irm_id == (nodeid_t)-1)
1583 return 1;
1584
Stefan Richter61c7f772005-12-05 16:28:59 -05001585 /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
1586 host->csr.broadcast_channel |= 0x40000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* If there is no bus manager then we should set the root node's
1589 * force_root bit to promote bus stability per the 1394
1590 * spec. (8.4.2.6) */
1591 if (host->busmgr_id == 0xffff && host->node_count > 1)
1592 {
1593 u16 root_node = host->node_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Jody McIntyre328699b2005-09-30 11:59:08 -07001595 /* get cycle master capability flag from root node */
1596 if (host->is_cycmst ||
1597 (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
1598 (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
1599 &bc, sizeof(quadlet_t)) &&
1600 be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 hpsb_send_phy_config(host, root_node, -1);
1602 else {
1603 HPSB_DEBUG("The root node is not cycle master capable; "
1604 "selecting a new root node and resetting...");
1605
1606 if (cycles >= 5) {
1607 /* Oh screw it! Just leave the bus as it is */
1608 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1609 return 1;
1610 }
1611
1612 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1613 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1614
1615 return 0;
1616 }
1617 }
1618
Stefan Richter14c0fa22005-12-01 18:51:52 -05001619 /* Some devices suspend their ports while being connected to an inactive
1620 * host adapter, i.e. if connected before the low-level driver is
1621 * loaded. They become visible either when physically unplugged and
1622 * replugged, or when receiving a resume packet. Send one once. */
1623 if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
1624 host->resume_packet_sent = 1;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 1;
1627}
1628
1629/* We need to ensure that if we are not the IRM, that the IRM node is capable of
1630 * everything we can do, otherwise issue a bus reset and try to become the IRM
1631 * ourselves. */
1632static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
1633{
1634 quadlet_t bc;
1635 int status;
1636
1637 if (hpsb_disable_irm || host->is_irm)
1638 return 1;
1639
1640 status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
1641 get_hpsb_generation(host),
1642 (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
1643 &bc, sizeof(quadlet_t));
1644
1645 if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
1646 /* The current irm node does not have a valid BROADCAST_CHANNEL
1647 * register and we do, so reset the bus with force_root set */
1648 HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
1649
1650 if (cycles >= 5) {
1651 /* Oh screw it! Just leave the bus as it is */
1652 HPSB_DEBUG("Stopping reset loop for IRM sanity");
1653 return 1;
1654 }
1655
1656 hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
1657 hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
1658
1659 return 0;
1660 }
1661
1662 return 1;
1663}
1664
Stefan Richter11305c32008-08-19 21:29:23 +02001665static int nodemgr_host_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Stefan Richter11305c32008-08-19 21:29:23 +02001667 struct hpsb_host *host = data;
Stefan Richterb7a71792006-10-06 19:49:52 +02001668 unsigned int g, generation = 0;
Stefan Richterd2f119f2006-07-03 12:02:35 -04001669 int i, reset_cycles = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001671 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /* Setup our device-model entries */
1673 nodemgr_create_host_dev_files(host);
1674
Stefan Richterd2f119f2006-07-03 12:02:35 -04001675 for (;;) {
1676 /* Sleep until next bus reset */
1677 set_current_state(TASK_INTERRUPTIBLE);
Stefan Richtera65421e2007-02-10 22:06:18 +01001678 if (get_hpsb_generation(host) == generation &&
1679 !kthread_should_stop())
Stefan Richterd2f119f2006-07-03 12:02:35 -04001680 schedule();
1681 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Stefan Richterd2f119f2006-07-03 12:02:35 -04001683 /* Thread may have been woken up to freeze or to exit */
1684 if (try_to_freeze())
1685 continue;
1686 if (kthread_should_stop())
1687 goto exit;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* Pause for 1/4 second in 1/16 second intervals,
1690 * to make sure things settle down. */
Stefan Richterd2f119f2006-07-03 12:02:35 -04001691 g = get_hpsb_generation(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 for (i = 0; i < 4 ; i++) {
Satyam Sharma69e2b602007-08-15 20:05:38 +05301693 msleep_interruptible(63);
Nigel Cunninghamec9a13c2008-12-09 22:40:20 +11001694 try_to_freeze();
Satyam Sharma69e2b602007-08-15 20:05:38 +05301695 if (kthread_should_stop())
Stefan Richtera0e857e2007-06-17 23:47:45 +02001696 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 /* Now get the generation in which the node ID's we collect
1699 * are valid. During the bus scan we will use this generation
1700 * for the read transactions, so that if another reset occurs
1701 * during the scan the transactions will fail instead of
1702 * returning bogus data. */
1703 generation = get_hpsb_generation(host);
1704
1705 /* If we get a reset before we are done waiting, then
Michael Opdenacker59c51592007-05-09 08:57:56 +02001706 * start the waiting over again */
Stefan Richterd2f119f2006-07-03 12:02:35 -04001707 if (generation != g)
1708 g = generation, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Jody McIntyre328699b2005-09-30 11:59:08 -07001711 if (!nodemgr_check_irm_capability(host, reset_cycles) ||
1712 !nodemgr_do_irm_duties(host, reset_cycles)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 reset_cycles++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 continue;
1715 }
Jody McIntyre328699b2005-09-30 11:59:08 -07001716 reset_cycles = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 /* Scan our nodes to get the bus options and create node
1719 * entries. This does not do the sysfs stuff, since that
Kay Sievers312c0042005-11-16 09:00:00 +01001720 * would trigger uevents and such, which is a bad idea at
1721 * this point. */
Stefan Richter11305c32008-08-19 21:29:23 +02001722 nodemgr_node_scan(host, generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* This actually does the full probe, with sysfs
1725 * registration. */
Stefan Richterfc392fe2008-08-19 21:30:17 +02001726 if (!nodemgr_node_probe(host, generation))
1727 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 /* Update some of our sysfs symlinks */
1730 nodemgr_update_host_dev_links(host);
Stefan Richterfc392fe2008-08-19 21:30:17 +02001731
1732 /* Sleep 3 seconds */
1733 for (i = 3000/200; i; i--) {
1734 msleep_interruptible(200);
Nigel Cunninghamec9a13c2008-12-09 22:40:20 +11001735 try_to_freeze();
Stefan Richterfc392fe2008-08-19 21:30:17 +02001736 if (kthread_should_stop())
1737 goto exit;
1738
1739 if (generation != get_hpsb_generation(host))
1740 break;
1741 }
1742 /* Remove nodes which are gone, unless a bus reset happened */
1743 if (!i)
1744 nodemgr_remove_nodes_in_limbo(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Stefan Richterd2f119f2006-07-03 12:02:35 -04001746exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 HPSB_VERBOSE("NodeMgr: Exiting thread");
Stefan Richterd2f119f2006-07-03 12:02:35 -04001748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Stefan Richter11305c32008-08-19 21:29:23 +02001751struct per_host_parameter {
Dave Young73cf6022008-01-22 13:56:32 +08001752 void *data;
1753 int (*cb)(struct hpsb_host *, void *);
1754};
1755
Stefan Richter11305c32008-08-19 21:29:23 +02001756static int per_host(struct device *dev, void *data)
Dave Young73cf6022008-01-22 13:56:32 +08001757{
1758 struct hpsb_host *host;
Stefan Richter11305c32008-08-19 21:29:23 +02001759 struct per_host_parameter *p = data;
Dave Young73cf6022008-01-22 13:56:32 +08001760
1761 host = container_of(dev, struct hpsb_host, host_dev);
Stefan Richter11305c32008-08-19 21:29:23 +02001762 return p->cb(host, p->data);
Dave Young73cf6022008-01-22 13:56:32 +08001763}
Stefan Richter11305c32008-08-19 21:29:23 +02001764
Stefan Richterafd65462007-03-05 03:06:23 +01001765/**
1766 * nodemgr_for_each_host - call a function for each IEEE 1394 host
1767 * @data: an address to supply to the callback
1768 * @cb: function to call for each host
1769 *
1770 * Iterate the hosts, calling a given function with supplied data for each host.
1771 * If the callback fails on a host, i.e. if it returns a non-zero value, the
1772 * iteration is stopped.
1773 *
1774 * Return value: 0 on success, non-zero on failure (same as returned by last run
1775 * of the callback).
1776 */
1777int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Stefan Richter11305c32008-08-19 21:29:23 +02001779 struct per_host_parameter p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Stefan Richter11305c32008-08-19 21:29:23 +02001781 p.cb = cb;
1782 p.data = data;
1783 return class_for_each_device(&hpsb_host_class, NULL, &p, per_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Stefan Richteref815332007-03-05 03:05:32 +01001786/* The following two convenience functions use a struct node_entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * for addressing a node on the bus. They are intended for use by any
1788 * process context, not just the nodemgr thread, so we need to be a
1789 * little careful when reading out the node ID and generation. The
1790 * thing that can go wrong is that we get the node ID, then a bus
1791 * reset occurs, and then we read the generation. The node ID is
1792 * possibly invalid, but the generation is current, and we end up
1793 * sending a packet to a the wrong node.
1794 *
1795 * The solution is to make sure we read the generation first, so that
1796 * if a reset occurs in the process, we end up with a stale generation
1797 * and the transactions will fail instead of silently using wrong node
1798 * ID's.
1799 */
1800
Stefan Richterafd65462007-03-05 03:06:23 +01001801/**
1802 * hpsb_node_fill_packet - fill some destination information into a packet
1803 * @ne: destination node
1804 * @packet: packet to fill in
1805 *
1806 * This will fill in the given, pre-initialised hpsb_packet with the current
1807 * information from the node entry (host, node ID, bus generation number).
1808 */
1809void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Stefan Richterafd65462007-03-05 03:06:23 +01001811 packet->host = ne->host;
1812 packet->generation = ne->generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 barrier();
Stefan Richterafd65462007-03-05 03:06:23 +01001814 packet->node_id = ne->nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
1817int hpsb_node_write(struct node_entry *ne, u64 addr,
1818 quadlet_t *buffer, size_t length)
1819{
1820 unsigned int generation = ne->generation;
1821
1822 barrier();
1823 return hpsb_write(ne->host, ne->nodeid, generation,
1824 addr, buffer, length);
1825}
1826
1827static void nodemgr_add_host(struct hpsb_host *host)
1828{
1829 struct host_info *hi;
1830
1831 hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (!hi) {
Stefan Richterd2f119f2006-07-03 12:02:35 -04001833 HPSB_ERR("NodeMgr: out of memory in add host");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 hi->host = host;
Stefan Richter11305c32008-08-19 21:29:23 +02001837 hi->thread = kthread_run(nodemgr_host_thread, host, "knodemgrd_%d",
Stefan Richterd2f119f2006-07-03 12:02:35 -04001838 host->id);
1839 if (IS_ERR(hi->thread)) {
1840 HPSB_ERR("NodeMgr: cannot start thread for host %d", host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845static void nodemgr_host_reset(struct hpsb_host *host)
1846{
1847 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1848
Stefan Richterd2f119f2006-07-03 12:02:35 -04001849 if (hi) {
1850 HPSB_VERBOSE("NodeMgr: Processing reset for host %d", host->id);
1851 wake_up_process(hi->thread);
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
1855static void nodemgr_remove_host(struct hpsb_host *host)
1856{
1857 struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
1858
1859 if (hi) {
Stefan Richterd2f119f2006-07-03 12:02:35 -04001860 kthread_stop(hi->thread);
1861 nodemgr_remove_host_dev(&host->device);
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865static struct hpsb_highlevel nodemgr_highlevel = {
1866 .name = "Node manager",
1867 .add_host = nodemgr_add_host,
1868 .host_reset = nodemgr_host_reset,
1869 .remove_host = nodemgr_remove_host,
1870};
1871
1872int init_ieee1394_nodemgr(void)
1873{
Stefan Richter7fdfc902006-10-21 01:23:56 +02001874 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Stefan Richter7fdfc902006-10-21 01:23:56 +02001876 error = class_register(&nodemgr_ne_class);
1877 if (error)
Stefan Richter91efa462007-02-06 02:34:45 +01001878 goto fail_ne;
Stefan Richter7fdfc902006-10-21 01:23:56 +02001879 error = class_register(&nodemgr_ud_class);
Stefan Richter91efa462007-02-06 02:34:45 +01001880 if (error)
1881 goto fail_ud;
Stefan Richter8252bbb2006-11-22 21:09:42 +01001882 error = driver_register(&nodemgr_mid_layer_driver);
Stefan Richter91efa462007-02-06 02:34:45 +01001883 if (error)
1884 goto fail_ml;
1885 /* This driver is not used if nodemgr is off (disable_nodemgr=1). */
1886 nodemgr_dev_template_host.driver = &nodemgr_mid_layer_driver;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 hpsb_register_highlevel(&nodemgr_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 0;
Stefan Richter91efa462007-02-06 02:34:45 +01001890
1891fail_ml:
1892 class_unregister(&nodemgr_ud_class);
1893fail_ud:
1894 class_unregister(&nodemgr_ne_class);
1895fail_ne:
1896 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899void cleanup_ieee1394_nodemgr(void)
1900{
Stefan Richterd41bba22006-11-22 21:28:19 +01001901 hpsb_unregister_highlevel(&nodemgr_highlevel);
Stefan Richter91efa462007-02-06 02:34:45 +01001902 driver_unregister(&nodemgr_mid_layer_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 class_unregister(&nodemgr_ud_class);
1904 class_unregister(&nodemgr_ne_class);
1905}