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