xref: /OK3568_Linux_fs/kernel/drivers/firewire/core-device.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Device probing and sysfs code.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005-2006  Kristian Hoegsberg <krh@bitplanet.net>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/bug.h>
9*4882a593Smuzhiyun #include <linux/ctype.h>
10*4882a593Smuzhiyun #include <linux/delay.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <linux/firewire.h>
14*4882a593Smuzhiyun #include <linux/firewire-constants.h>
15*4882a593Smuzhiyun #include <linux/idr.h>
16*4882a593Smuzhiyun #include <linux/jiffies.h>
17*4882a593Smuzhiyun #include <linux/kobject.h>
18*4882a593Smuzhiyun #include <linux/list.h>
19*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/mutex.h>
22*4882a593Smuzhiyun #include <linux/random.h>
23*4882a593Smuzhiyun #include <linux/rwsem.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/spinlock.h>
26*4882a593Smuzhiyun #include <linux/string.h>
27*4882a593Smuzhiyun #include <linux/workqueue.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <linux/atomic.h>
30*4882a593Smuzhiyun #include <asm/byteorder.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include "core.h"
33*4882a593Smuzhiyun 
fw_csr_iterator_init(struct fw_csr_iterator * ci,const u32 * p)34*4882a593Smuzhiyun void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	ci->p = p + 1;
37*4882a593Smuzhiyun 	ci->end = ci->p + (p[0] >> 16);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun EXPORT_SYMBOL(fw_csr_iterator_init);
40*4882a593Smuzhiyun 
fw_csr_iterator_next(struct fw_csr_iterator * ci,int * key,int * value)41*4882a593Smuzhiyun int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	*key = *ci->p >> 24;
44*4882a593Smuzhiyun 	*value = *ci->p & 0xffffff;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	return ci->p++ < ci->end;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun EXPORT_SYMBOL(fw_csr_iterator_next);
49*4882a593Smuzhiyun 
search_leaf(const u32 * directory,int search_key)50*4882a593Smuzhiyun static const u32 *search_leaf(const u32 *directory, int search_key)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct fw_csr_iterator ci;
53*4882a593Smuzhiyun 	int last_key = 0, key, value;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	fw_csr_iterator_init(&ci, directory);
56*4882a593Smuzhiyun 	while (fw_csr_iterator_next(&ci, &key, &value)) {
57*4882a593Smuzhiyun 		if (last_key == search_key &&
58*4882a593Smuzhiyun 		    key == (CSR_DESCRIPTOR | CSR_LEAF))
59*4882a593Smuzhiyun 			return ci.p - 1 + value;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 		last_key = key;
62*4882a593Smuzhiyun 	}
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	return NULL;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
textual_leaf_to_string(const u32 * block,char * buf,size_t size)67*4882a593Smuzhiyun static int textual_leaf_to_string(const u32 *block, char *buf, size_t size)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	unsigned int quadlets, i;
70*4882a593Smuzhiyun 	char c;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (!size || !buf)
73*4882a593Smuzhiyun 		return -EINVAL;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	quadlets = min(block[0] >> 16, 256U);
76*4882a593Smuzhiyun 	if (quadlets < 2)
77*4882a593Smuzhiyun 		return -ENODATA;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	if (block[1] != 0 || block[2] != 0)
80*4882a593Smuzhiyun 		/* unknown language/character set */
81*4882a593Smuzhiyun 		return -ENODATA;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	block += 3;
84*4882a593Smuzhiyun 	quadlets -= 2;
85*4882a593Smuzhiyun 	for (i = 0; i < quadlets * 4 && i < size - 1; i++) {
86*4882a593Smuzhiyun 		c = block[i / 4] >> (24 - 8 * (i % 4));
87*4882a593Smuzhiyun 		if (c == '\0')
88*4882a593Smuzhiyun 			break;
89*4882a593Smuzhiyun 		buf[i] = c;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 	buf[i] = '\0';
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return i;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /**
97*4882a593Smuzhiyun  * fw_csr_string() - reads a string from the configuration ROM
98*4882a593Smuzhiyun  * @directory:	e.g. root directory or unit directory
99*4882a593Smuzhiyun  * @key:	the key of the preceding directory entry
100*4882a593Smuzhiyun  * @buf:	where to put the string
101*4882a593Smuzhiyun  * @size:	size of @buf, in bytes
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * The string is taken from a minimal ASCII text descriptor leaf after
104*4882a593Smuzhiyun  * the immediate entry with @key.  The string is zero-terminated.
105*4882a593Smuzhiyun  * An overlong string is silently truncated such that it and the
106*4882a593Smuzhiyun  * zero byte fit into @size.
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * Returns strlen(buf) or a negative error code.
109*4882a593Smuzhiyun  */
fw_csr_string(const u32 * directory,int key,char * buf,size_t size)110*4882a593Smuzhiyun int fw_csr_string(const u32 *directory, int key, char *buf, size_t size)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	const u32 *leaf = search_leaf(directory, key);
113*4882a593Smuzhiyun 	if (!leaf)
114*4882a593Smuzhiyun 		return -ENOENT;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return textual_leaf_to_string(leaf, buf, size);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun EXPORT_SYMBOL(fw_csr_string);
119*4882a593Smuzhiyun 
get_ids(const u32 * directory,int * id)120*4882a593Smuzhiyun static void get_ids(const u32 *directory, int *id)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	struct fw_csr_iterator ci;
123*4882a593Smuzhiyun 	int key, value;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	fw_csr_iterator_init(&ci, directory);
126*4882a593Smuzhiyun 	while (fw_csr_iterator_next(&ci, &key, &value)) {
127*4882a593Smuzhiyun 		switch (key) {
128*4882a593Smuzhiyun 		case CSR_VENDOR:	id[0] = value; break;
129*4882a593Smuzhiyun 		case CSR_MODEL:		id[1] = value; break;
130*4882a593Smuzhiyun 		case CSR_SPECIFIER_ID:	id[2] = value; break;
131*4882a593Smuzhiyun 		case CSR_VERSION:	id[3] = value; break;
132*4882a593Smuzhiyun 		}
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
get_modalias_ids(struct fw_unit * unit,int * id)136*4882a593Smuzhiyun static void get_modalias_ids(struct fw_unit *unit, int *id)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	get_ids(&fw_parent_device(unit)->config_rom[5], id);
139*4882a593Smuzhiyun 	get_ids(unit->directory, id);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
match_ids(const struct ieee1394_device_id * id_table,int * id)142*4882a593Smuzhiyun static bool match_ids(const struct ieee1394_device_id *id_table, int *id)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	int match = 0;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (id[0] == id_table->vendor_id)
147*4882a593Smuzhiyun 		match |= IEEE1394_MATCH_VENDOR_ID;
148*4882a593Smuzhiyun 	if (id[1] == id_table->model_id)
149*4882a593Smuzhiyun 		match |= IEEE1394_MATCH_MODEL_ID;
150*4882a593Smuzhiyun 	if (id[2] == id_table->specifier_id)
151*4882a593Smuzhiyun 		match |= IEEE1394_MATCH_SPECIFIER_ID;
152*4882a593Smuzhiyun 	if (id[3] == id_table->version)
153*4882a593Smuzhiyun 		match |= IEEE1394_MATCH_VERSION;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	return (match & id_table->match_flags) == id_table->match_flags;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
unit_match(struct device * dev,struct device_driver * drv)158*4882a593Smuzhiyun static const struct ieee1394_device_id *unit_match(struct device *dev,
159*4882a593Smuzhiyun 						   struct device_driver *drv)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	const struct ieee1394_device_id *id_table =
162*4882a593Smuzhiyun 			container_of(drv, struct fw_driver, driver)->id_table;
163*4882a593Smuzhiyun 	int id[] = {0, 0, 0, 0};
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	get_modalias_ids(fw_unit(dev), id);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	for (; id_table->match_flags != 0; id_table++)
168*4882a593Smuzhiyun 		if (match_ids(id_table, id))
169*4882a593Smuzhiyun 			return id_table;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	return NULL;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static bool is_fw_unit(struct device *dev);
175*4882a593Smuzhiyun 
fw_unit_match(struct device * dev,struct device_driver * drv)176*4882a593Smuzhiyun static int fw_unit_match(struct device *dev, struct device_driver *drv)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	/* We only allow binding to fw_units. */
179*4882a593Smuzhiyun 	return is_fw_unit(dev) && unit_match(dev, drv) != NULL;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
fw_unit_probe(struct device * dev)182*4882a593Smuzhiyun static int fw_unit_probe(struct device *dev)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct fw_driver *driver =
185*4882a593Smuzhiyun 			container_of(dev->driver, struct fw_driver, driver);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	return driver->probe(fw_unit(dev), unit_match(dev, dev->driver));
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
fw_unit_remove(struct device * dev)190*4882a593Smuzhiyun static int fw_unit_remove(struct device *dev)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct fw_driver *driver =
193*4882a593Smuzhiyun 			container_of(dev->driver, struct fw_driver, driver);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	return driver->remove(fw_unit(dev)), 0;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
get_modalias(struct fw_unit * unit,char * buffer,size_t buffer_size)198*4882a593Smuzhiyun static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	int id[] = {0, 0, 0, 0};
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	get_modalias_ids(unit, id);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	return snprintf(buffer, buffer_size,
205*4882a593Smuzhiyun 			"ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
206*4882a593Smuzhiyun 			id[0], id[1], id[2], id[3]);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
fw_unit_uevent(struct device * dev,struct kobj_uevent_env * env)209*4882a593Smuzhiyun static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	struct fw_unit *unit = fw_unit(dev);
212*4882a593Smuzhiyun 	char modalias[64];
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	get_modalias(unit, modalias, sizeof(modalias));
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (add_uevent_var(env, "MODALIAS=%s", modalias))
217*4882a593Smuzhiyun 		return -ENOMEM;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun struct bus_type fw_bus_type = {
223*4882a593Smuzhiyun 	.name = "firewire",
224*4882a593Smuzhiyun 	.match = fw_unit_match,
225*4882a593Smuzhiyun 	.probe = fw_unit_probe,
226*4882a593Smuzhiyun 	.remove = fw_unit_remove,
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun EXPORT_SYMBOL(fw_bus_type);
229*4882a593Smuzhiyun 
fw_device_enable_phys_dma(struct fw_device * device)230*4882a593Smuzhiyun int fw_device_enable_phys_dma(struct fw_device *device)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	int generation = device->generation;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	/* device->node_id, accessed below, must not be older than generation */
235*4882a593Smuzhiyun 	smp_rmb();
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	return device->card->driver->enable_phys_dma(device->card,
238*4882a593Smuzhiyun 						     device->node_id,
239*4882a593Smuzhiyun 						     generation);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun EXPORT_SYMBOL(fw_device_enable_phys_dma);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun struct config_rom_attribute {
244*4882a593Smuzhiyun 	struct device_attribute attr;
245*4882a593Smuzhiyun 	u32 key;
246*4882a593Smuzhiyun };
247*4882a593Smuzhiyun 
show_immediate(struct device * dev,struct device_attribute * dattr,char * buf)248*4882a593Smuzhiyun static ssize_t show_immediate(struct device *dev,
249*4882a593Smuzhiyun 			      struct device_attribute *dattr, char *buf)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	struct config_rom_attribute *attr =
252*4882a593Smuzhiyun 		container_of(dattr, struct config_rom_attribute, attr);
253*4882a593Smuzhiyun 	struct fw_csr_iterator ci;
254*4882a593Smuzhiyun 	const u32 *dir;
255*4882a593Smuzhiyun 	int key, value, ret = -ENOENT;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	down_read(&fw_device_rwsem);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	if (is_fw_unit(dev))
260*4882a593Smuzhiyun 		dir = fw_unit(dev)->directory;
261*4882a593Smuzhiyun 	else
262*4882a593Smuzhiyun 		dir = fw_device(dev)->config_rom + 5;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	fw_csr_iterator_init(&ci, dir);
265*4882a593Smuzhiyun 	while (fw_csr_iterator_next(&ci, &key, &value))
266*4882a593Smuzhiyun 		if (attr->key == key) {
267*4882a593Smuzhiyun 			ret = snprintf(buf, buf ? PAGE_SIZE : 0,
268*4882a593Smuzhiyun 				       "0x%06x\n", value);
269*4882a593Smuzhiyun 			break;
270*4882a593Smuzhiyun 		}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return ret;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun #define IMMEDIATE_ATTR(name, key)				\
278*4882a593Smuzhiyun 	{ __ATTR(name, S_IRUGO, show_immediate, NULL), key }
279*4882a593Smuzhiyun 
show_text_leaf(struct device * dev,struct device_attribute * dattr,char * buf)280*4882a593Smuzhiyun static ssize_t show_text_leaf(struct device *dev,
281*4882a593Smuzhiyun 			      struct device_attribute *dattr, char *buf)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	struct config_rom_attribute *attr =
284*4882a593Smuzhiyun 		container_of(dattr, struct config_rom_attribute, attr);
285*4882a593Smuzhiyun 	const u32 *dir;
286*4882a593Smuzhiyun 	size_t bufsize;
287*4882a593Smuzhiyun 	char dummy_buf[2];
288*4882a593Smuzhiyun 	int ret;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	down_read(&fw_device_rwsem);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	if (is_fw_unit(dev))
293*4882a593Smuzhiyun 		dir = fw_unit(dev)->directory;
294*4882a593Smuzhiyun 	else
295*4882a593Smuzhiyun 		dir = fw_device(dev)->config_rom + 5;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (buf) {
298*4882a593Smuzhiyun 		bufsize = PAGE_SIZE - 1;
299*4882a593Smuzhiyun 	} else {
300*4882a593Smuzhiyun 		buf = dummy_buf;
301*4882a593Smuzhiyun 		bufsize = 1;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	ret = fw_csr_string(dir, attr->key, buf, bufsize);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (ret >= 0) {
307*4882a593Smuzhiyun 		/* Strip trailing whitespace and add newline. */
308*4882a593Smuzhiyun 		while (ret > 0 && isspace(buf[ret - 1]))
309*4882a593Smuzhiyun 			ret--;
310*4882a593Smuzhiyun 		strcpy(buf + ret, "\n");
311*4882a593Smuzhiyun 		ret++;
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	return ret;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun #define TEXT_LEAF_ATTR(name, key)				\
320*4882a593Smuzhiyun 	{ __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun static struct config_rom_attribute config_rom_attributes[] = {
323*4882a593Smuzhiyun 	IMMEDIATE_ATTR(vendor, CSR_VENDOR),
324*4882a593Smuzhiyun 	IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
325*4882a593Smuzhiyun 	IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
326*4882a593Smuzhiyun 	IMMEDIATE_ATTR(version, CSR_VERSION),
327*4882a593Smuzhiyun 	IMMEDIATE_ATTR(model, CSR_MODEL),
328*4882a593Smuzhiyun 	TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
329*4882a593Smuzhiyun 	TEXT_LEAF_ATTR(model_name, CSR_MODEL),
330*4882a593Smuzhiyun 	TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
331*4882a593Smuzhiyun };
332*4882a593Smuzhiyun 
init_fw_attribute_group(struct device * dev,struct device_attribute * attrs,struct fw_attribute_group * group)333*4882a593Smuzhiyun static void init_fw_attribute_group(struct device *dev,
334*4882a593Smuzhiyun 				    struct device_attribute *attrs,
335*4882a593Smuzhiyun 				    struct fw_attribute_group *group)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	struct device_attribute *attr;
338*4882a593Smuzhiyun 	int i, j;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	for (j = 0; attrs[j].attr.name != NULL; j++)
341*4882a593Smuzhiyun 		group->attrs[j] = &attrs[j].attr;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
344*4882a593Smuzhiyun 		attr = &config_rom_attributes[i].attr;
345*4882a593Smuzhiyun 		if (attr->show(dev, attr, NULL) < 0)
346*4882a593Smuzhiyun 			continue;
347*4882a593Smuzhiyun 		group->attrs[j++] = &attr->attr;
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	group->attrs[j] = NULL;
351*4882a593Smuzhiyun 	group->groups[0] = &group->group;
352*4882a593Smuzhiyun 	group->groups[1] = NULL;
353*4882a593Smuzhiyun 	group->group.attrs = group->attrs;
354*4882a593Smuzhiyun 	dev->groups = (const struct attribute_group **) group->groups;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)357*4882a593Smuzhiyun static ssize_t modalias_show(struct device *dev,
358*4882a593Smuzhiyun 			     struct device_attribute *attr, char *buf)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	struct fw_unit *unit = fw_unit(dev);
361*4882a593Smuzhiyun 	int length;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	length = get_modalias(unit, buf, PAGE_SIZE);
364*4882a593Smuzhiyun 	strcpy(buf + length, "\n");
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	return length + 1;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
rom_index_show(struct device * dev,struct device_attribute * attr,char * buf)369*4882a593Smuzhiyun static ssize_t rom_index_show(struct device *dev,
370*4882a593Smuzhiyun 			      struct device_attribute *attr, char *buf)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	struct fw_device *device = fw_device(dev->parent);
373*4882a593Smuzhiyun 	struct fw_unit *unit = fw_unit(dev);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "%d\n",
376*4882a593Smuzhiyun 			(int)(unit->directory - device->config_rom));
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun static struct device_attribute fw_unit_attributes[] = {
380*4882a593Smuzhiyun 	__ATTR_RO(modalias),
381*4882a593Smuzhiyun 	__ATTR_RO(rom_index),
382*4882a593Smuzhiyun 	__ATTR_NULL,
383*4882a593Smuzhiyun };
384*4882a593Smuzhiyun 
config_rom_show(struct device * dev,struct device_attribute * attr,char * buf)385*4882a593Smuzhiyun static ssize_t config_rom_show(struct device *dev,
386*4882a593Smuzhiyun 			       struct device_attribute *attr, char *buf)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun 	struct fw_device *device = fw_device(dev);
389*4882a593Smuzhiyun 	size_t length;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	down_read(&fw_device_rwsem);
392*4882a593Smuzhiyun 	length = device->config_rom_length * 4;
393*4882a593Smuzhiyun 	memcpy(buf, device->config_rom, length);
394*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	return length;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
guid_show(struct device * dev,struct device_attribute * attr,char * buf)399*4882a593Smuzhiyun static ssize_t guid_show(struct device *dev,
400*4882a593Smuzhiyun 			 struct device_attribute *attr, char *buf)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	struct fw_device *device = fw_device(dev);
403*4882a593Smuzhiyun 	int ret;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	down_read(&fw_device_rwsem);
406*4882a593Smuzhiyun 	ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
407*4882a593Smuzhiyun 		       device->config_rom[3], device->config_rom[4]);
408*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	return ret;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
is_local_show(struct device * dev,struct device_attribute * attr,char * buf)413*4882a593Smuzhiyun static ssize_t is_local_show(struct device *dev,
414*4882a593Smuzhiyun 			     struct device_attribute *attr, char *buf)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	struct fw_device *device = fw_device(dev);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", device->is_local);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
units_sprintf(char * buf,const u32 * directory)421*4882a593Smuzhiyun static int units_sprintf(char *buf, const u32 *directory)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	struct fw_csr_iterator ci;
424*4882a593Smuzhiyun 	int key, value;
425*4882a593Smuzhiyun 	int specifier_id = 0;
426*4882a593Smuzhiyun 	int version = 0;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	fw_csr_iterator_init(&ci, directory);
429*4882a593Smuzhiyun 	while (fw_csr_iterator_next(&ci, &key, &value)) {
430*4882a593Smuzhiyun 		switch (key) {
431*4882a593Smuzhiyun 		case CSR_SPECIFIER_ID:
432*4882a593Smuzhiyun 			specifier_id = value;
433*4882a593Smuzhiyun 			break;
434*4882a593Smuzhiyun 		case CSR_VERSION:
435*4882a593Smuzhiyun 			version = value;
436*4882a593Smuzhiyun 			break;
437*4882a593Smuzhiyun 		}
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
units_show(struct device * dev,struct device_attribute * attr,char * buf)443*4882a593Smuzhiyun static ssize_t units_show(struct device *dev,
444*4882a593Smuzhiyun 			  struct device_attribute *attr, char *buf)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	struct fw_device *device = fw_device(dev);
447*4882a593Smuzhiyun 	struct fw_csr_iterator ci;
448*4882a593Smuzhiyun 	int key, value, i = 0;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	down_read(&fw_device_rwsem);
451*4882a593Smuzhiyun 	fw_csr_iterator_init(&ci, &device->config_rom[5]);
452*4882a593Smuzhiyun 	while (fw_csr_iterator_next(&ci, &key, &value)) {
453*4882a593Smuzhiyun 		if (key != (CSR_UNIT | CSR_DIRECTORY))
454*4882a593Smuzhiyun 			continue;
455*4882a593Smuzhiyun 		i += units_sprintf(&buf[i], ci.p + value - 1);
456*4882a593Smuzhiyun 		if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
457*4882a593Smuzhiyun 			break;
458*4882a593Smuzhiyun 	}
459*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (i)
462*4882a593Smuzhiyun 		buf[i - 1] = '\n';
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	return i;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun static struct device_attribute fw_device_attributes[] = {
468*4882a593Smuzhiyun 	__ATTR_RO(config_rom),
469*4882a593Smuzhiyun 	__ATTR_RO(guid),
470*4882a593Smuzhiyun 	__ATTR_RO(is_local),
471*4882a593Smuzhiyun 	__ATTR_RO(units),
472*4882a593Smuzhiyun 	__ATTR_NULL,
473*4882a593Smuzhiyun };
474*4882a593Smuzhiyun 
read_rom(struct fw_device * device,int generation,int index,u32 * data)475*4882a593Smuzhiyun static int read_rom(struct fw_device *device,
476*4882a593Smuzhiyun 		    int generation, int index, u32 *data)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	u64 offset = (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4;
479*4882a593Smuzhiyun 	int i, rcode;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/* device->node_id, accessed below, must not be older than generation */
482*4882a593Smuzhiyun 	smp_rmb();
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	for (i = 10; i < 100; i += 10) {
485*4882a593Smuzhiyun 		rcode = fw_run_transaction(device->card,
486*4882a593Smuzhiyun 				TCODE_READ_QUADLET_REQUEST, device->node_id,
487*4882a593Smuzhiyun 				generation, device->max_speed, offset, data, 4);
488*4882a593Smuzhiyun 		if (rcode != RCODE_BUSY)
489*4882a593Smuzhiyun 			break;
490*4882a593Smuzhiyun 		msleep(i);
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 	be32_to_cpus(data);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	return rcode;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun #define MAX_CONFIG_ROM_SIZE 256
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun /*
500*4882a593Smuzhiyun  * Read the bus info block, perform a speed probe, and read all of the rest of
501*4882a593Smuzhiyun  * the config ROM.  We do all this with a cached bus generation.  If the bus
502*4882a593Smuzhiyun  * generation changes under us, read_config_rom will fail and get retried.
503*4882a593Smuzhiyun  * It's better to start all over in this case because the node from which we
504*4882a593Smuzhiyun  * are reading the ROM may have changed the ROM during the reset.
505*4882a593Smuzhiyun  * Returns either a result code or a negative error code.
506*4882a593Smuzhiyun  */
read_config_rom(struct fw_device * device,int generation)507*4882a593Smuzhiyun static int read_config_rom(struct fw_device *device, int generation)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun 	struct fw_card *card = device->card;
510*4882a593Smuzhiyun 	const u32 *old_rom, *new_rom;
511*4882a593Smuzhiyun 	u32 *rom, *stack;
512*4882a593Smuzhiyun 	u32 sp, key;
513*4882a593Smuzhiyun 	int i, end, length, ret;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	rom = kmalloc(sizeof(*rom) * MAX_CONFIG_ROM_SIZE +
516*4882a593Smuzhiyun 		      sizeof(*stack) * MAX_CONFIG_ROM_SIZE, GFP_KERNEL);
517*4882a593Smuzhiyun 	if (rom == NULL)
518*4882a593Smuzhiyun 		return -ENOMEM;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	stack = &rom[MAX_CONFIG_ROM_SIZE];
521*4882a593Smuzhiyun 	memset(rom, 0, sizeof(*rom) * MAX_CONFIG_ROM_SIZE);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	device->max_speed = SCODE_100;
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	/* First read the bus info block. */
526*4882a593Smuzhiyun 	for (i = 0; i < 5; i++) {
527*4882a593Smuzhiyun 		ret = read_rom(device, generation, i, &rom[i]);
528*4882a593Smuzhiyun 		if (ret != RCODE_COMPLETE)
529*4882a593Smuzhiyun 			goto out;
530*4882a593Smuzhiyun 		/*
531*4882a593Smuzhiyun 		 * As per IEEE1212 7.2, during initialization, devices can
532*4882a593Smuzhiyun 		 * reply with a 0 for the first quadlet of the config
533*4882a593Smuzhiyun 		 * rom to indicate that they are booting (for example,
534*4882a593Smuzhiyun 		 * if the firmware is on the disk of a external
535*4882a593Smuzhiyun 		 * harddisk).  In that case we just fail, and the
536*4882a593Smuzhiyun 		 * retry mechanism will try again later.
537*4882a593Smuzhiyun 		 */
538*4882a593Smuzhiyun 		if (i == 0 && rom[i] == 0) {
539*4882a593Smuzhiyun 			ret = RCODE_BUSY;
540*4882a593Smuzhiyun 			goto out;
541*4882a593Smuzhiyun 		}
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	device->max_speed = device->node->max_speed;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	/*
547*4882a593Smuzhiyun 	 * Determine the speed of
548*4882a593Smuzhiyun 	 *   - devices with link speed less than PHY speed,
549*4882a593Smuzhiyun 	 *   - devices with 1394b PHY (unless only connected to 1394a PHYs),
550*4882a593Smuzhiyun 	 *   - all devices if there are 1394b repeaters.
551*4882a593Smuzhiyun 	 * Note, we cannot use the bus info block's link_spd as starting point
552*4882a593Smuzhiyun 	 * because some buggy firmwares set it lower than necessary and because
553*4882a593Smuzhiyun 	 * 1394-1995 nodes do not have the field.
554*4882a593Smuzhiyun 	 */
555*4882a593Smuzhiyun 	if ((rom[2] & 0x7) < device->max_speed ||
556*4882a593Smuzhiyun 	    device->max_speed == SCODE_BETA ||
557*4882a593Smuzhiyun 	    card->beta_repeaters_present) {
558*4882a593Smuzhiyun 		u32 dummy;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 		/* for S1600 and S3200 */
561*4882a593Smuzhiyun 		if (device->max_speed == SCODE_BETA)
562*4882a593Smuzhiyun 			device->max_speed = card->link_speed;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 		while (device->max_speed > SCODE_100) {
565*4882a593Smuzhiyun 			if (read_rom(device, generation, 0, &dummy) ==
566*4882a593Smuzhiyun 			    RCODE_COMPLETE)
567*4882a593Smuzhiyun 				break;
568*4882a593Smuzhiyun 			device->max_speed--;
569*4882a593Smuzhiyun 		}
570*4882a593Smuzhiyun 	}
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	/*
573*4882a593Smuzhiyun 	 * Now parse the config rom.  The config rom is a recursive
574*4882a593Smuzhiyun 	 * directory structure so we parse it using a stack of
575*4882a593Smuzhiyun 	 * references to the blocks that make up the structure.  We
576*4882a593Smuzhiyun 	 * push a reference to the root directory on the stack to
577*4882a593Smuzhiyun 	 * start things off.
578*4882a593Smuzhiyun 	 */
579*4882a593Smuzhiyun 	length = i;
580*4882a593Smuzhiyun 	sp = 0;
581*4882a593Smuzhiyun 	stack[sp++] = 0xc0000005;
582*4882a593Smuzhiyun 	while (sp > 0) {
583*4882a593Smuzhiyun 		/*
584*4882a593Smuzhiyun 		 * Pop the next block reference of the stack.  The
585*4882a593Smuzhiyun 		 * lower 24 bits is the offset into the config rom,
586*4882a593Smuzhiyun 		 * the upper 8 bits are the type of the reference the
587*4882a593Smuzhiyun 		 * block.
588*4882a593Smuzhiyun 		 */
589*4882a593Smuzhiyun 		key = stack[--sp];
590*4882a593Smuzhiyun 		i = key & 0xffffff;
591*4882a593Smuzhiyun 		if (WARN_ON(i >= MAX_CONFIG_ROM_SIZE)) {
592*4882a593Smuzhiyun 			ret = -ENXIO;
593*4882a593Smuzhiyun 			goto out;
594*4882a593Smuzhiyun 		}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 		/* Read header quadlet for the block to get the length. */
597*4882a593Smuzhiyun 		ret = read_rom(device, generation, i, &rom[i]);
598*4882a593Smuzhiyun 		if (ret != RCODE_COMPLETE)
599*4882a593Smuzhiyun 			goto out;
600*4882a593Smuzhiyun 		end = i + (rom[i] >> 16) + 1;
601*4882a593Smuzhiyun 		if (end > MAX_CONFIG_ROM_SIZE) {
602*4882a593Smuzhiyun 			/*
603*4882a593Smuzhiyun 			 * This block extends outside the config ROM which is
604*4882a593Smuzhiyun 			 * a firmware bug.  Ignore this whole block, i.e.
605*4882a593Smuzhiyun 			 * simply set a fake block length of 0.
606*4882a593Smuzhiyun 			 */
607*4882a593Smuzhiyun 			fw_err(card, "skipped invalid ROM block %x at %llx\n",
608*4882a593Smuzhiyun 			       rom[i],
609*4882a593Smuzhiyun 			       i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
610*4882a593Smuzhiyun 			rom[i] = 0;
611*4882a593Smuzhiyun 			end = i;
612*4882a593Smuzhiyun 		}
613*4882a593Smuzhiyun 		i++;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 		/*
616*4882a593Smuzhiyun 		 * Now read in the block.  If this is a directory
617*4882a593Smuzhiyun 		 * block, check the entries as we read them to see if
618*4882a593Smuzhiyun 		 * it references another block, and push it in that case.
619*4882a593Smuzhiyun 		 */
620*4882a593Smuzhiyun 		for (; i < end; i++) {
621*4882a593Smuzhiyun 			ret = read_rom(device, generation, i, &rom[i]);
622*4882a593Smuzhiyun 			if (ret != RCODE_COMPLETE)
623*4882a593Smuzhiyun 				goto out;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 			if ((key >> 30) != 3 || (rom[i] >> 30) < 2)
626*4882a593Smuzhiyun 				continue;
627*4882a593Smuzhiyun 			/*
628*4882a593Smuzhiyun 			 * Offset points outside the ROM.  May be a firmware
629*4882a593Smuzhiyun 			 * bug or an Extended ROM entry (IEEE 1212-2001 clause
630*4882a593Smuzhiyun 			 * 7.7.18).  Simply overwrite this pointer here by a
631*4882a593Smuzhiyun 			 * fake immediate entry so that later iterators over
632*4882a593Smuzhiyun 			 * the ROM don't have to check offsets all the time.
633*4882a593Smuzhiyun 			 */
634*4882a593Smuzhiyun 			if (i + (rom[i] & 0xffffff) >= MAX_CONFIG_ROM_SIZE) {
635*4882a593Smuzhiyun 				fw_err(card,
636*4882a593Smuzhiyun 				       "skipped unsupported ROM entry %x at %llx\n",
637*4882a593Smuzhiyun 				       rom[i],
638*4882a593Smuzhiyun 				       i * 4 | CSR_REGISTER_BASE | CSR_CONFIG_ROM);
639*4882a593Smuzhiyun 				rom[i] = 0;
640*4882a593Smuzhiyun 				continue;
641*4882a593Smuzhiyun 			}
642*4882a593Smuzhiyun 			stack[sp++] = i + rom[i];
643*4882a593Smuzhiyun 		}
644*4882a593Smuzhiyun 		if (length < i)
645*4882a593Smuzhiyun 			length = i;
646*4882a593Smuzhiyun 	}
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	old_rom = device->config_rom;
649*4882a593Smuzhiyun 	new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
650*4882a593Smuzhiyun 	if (new_rom == NULL) {
651*4882a593Smuzhiyun 		ret = -ENOMEM;
652*4882a593Smuzhiyun 		goto out;
653*4882a593Smuzhiyun 	}
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	down_write(&fw_device_rwsem);
656*4882a593Smuzhiyun 	device->config_rom = new_rom;
657*4882a593Smuzhiyun 	device->config_rom_length = length;
658*4882a593Smuzhiyun 	up_write(&fw_device_rwsem);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	kfree(old_rom);
661*4882a593Smuzhiyun 	ret = RCODE_COMPLETE;
662*4882a593Smuzhiyun 	device->max_rec	= rom[2] >> 12 & 0xf;
663*4882a593Smuzhiyun 	device->cmc	= rom[2] >> 30 & 1;
664*4882a593Smuzhiyun 	device->irmc	= rom[2] >> 31 & 1;
665*4882a593Smuzhiyun  out:
666*4882a593Smuzhiyun 	kfree(rom);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	return ret;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
fw_unit_release(struct device * dev)671*4882a593Smuzhiyun static void fw_unit_release(struct device *dev)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun 	struct fw_unit *unit = fw_unit(dev);
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	fw_device_put(fw_parent_device(unit));
676*4882a593Smuzhiyun 	kfree(unit);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun static struct device_type fw_unit_type = {
680*4882a593Smuzhiyun 	.uevent		= fw_unit_uevent,
681*4882a593Smuzhiyun 	.release	= fw_unit_release,
682*4882a593Smuzhiyun };
683*4882a593Smuzhiyun 
is_fw_unit(struct device * dev)684*4882a593Smuzhiyun static bool is_fw_unit(struct device *dev)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	return dev->type == &fw_unit_type;
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
create_units(struct fw_device * device)689*4882a593Smuzhiyun static void create_units(struct fw_device *device)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun 	struct fw_csr_iterator ci;
692*4882a593Smuzhiyun 	struct fw_unit *unit;
693*4882a593Smuzhiyun 	int key, value, i;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	i = 0;
696*4882a593Smuzhiyun 	fw_csr_iterator_init(&ci, &device->config_rom[5]);
697*4882a593Smuzhiyun 	while (fw_csr_iterator_next(&ci, &key, &value)) {
698*4882a593Smuzhiyun 		if (key != (CSR_UNIT | CSR_DIRECTORY))
699*4882a593Smuzhiyun 			continue;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 		/*
702*4882a593Smuzhiyun 		 * Get the address of the unit directory and try to
703*4882a593Smuzhiyun 		 * match the drivers id_tables against it.
704*4882a593Smuzhiyun 		 */
705*4882a593Smuzhiyun 		unit = kzalloc(sizeof(*unit), GFP_KERNEL);
706*4882a593Smuzhiyun 		if (unit == NULL)
707*4882a593Smuzhiyun 			continue;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 		unit->directory = ci.p + value - 1;
710*4882a593Smuzhiyun 		unit->device.bus = &fw_bus_type;
711*4882a593Smuzhiyun 		unit->device.type = &fw_unit_type;
712*4882a593Smuzhiyun 		unit->device.parent = &device->device;
713*4882a593Smuzhiyun 		dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 		BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
716*4882a593Smuzhiyun 				ARRAY_SIZE(fw_unit_attributes) +
717*4882a593Smuzhiyun 				ARRAY_SIZE(config_rom_attributes));
718*4882a593Smuzhiyun 		init_fw_attribute_group(&unit->device,
719*4882a593Smuzhiyun 					fw_unit_attributes,
720*4882a593Smuzhiyun 					&unit->attribute_group);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 		if (device_register(&unit->device) < 0)
723*4882a593Smuzhiyun 			goto skip_unit;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 		fw_device_get(device);
726*4882a593Smuzhiyun 		continue;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	skip_unit:
729*4882a593Smuzhiyun 		kfree(unit);
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
shutdown_unit(struct device * device,void * data)733*4882a593Smuzhiyun static int shutdown_unit(struct device *device, void *data)
734*4882a593Smuzhiyun {
735*4882a593Smuzhiyun 	device_unregister(device);
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	return 0;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun /*
741*4882a593Smuzhiyun  * fw_device_rwsem acts as dual purpose mutex:
742*4882a593Smuzhiyun  *   - serializes accesses to fw_device_idr,
743*4882a593Smuzhiyun  *   - serializes accesses to fw_device.config_rom/.config_rom_length and
744*4882a593Smuzhiyun  *     fw_unit.directory, unless those accesses happen at safe occasions
745*4882a593Smuzhiyun  */
746*4882a593Smuzhiyun DECLARE_RWSEM(fw_device_rwsem);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun DEFINE_IDR(fw_device_idr);
749*4882a593Smuzhiyun int fw_cdev_major;
750*4882a593Smuzhiyun 
fw_device_get_by_devt(dev_t devt)751*4882a593Smuzhiyun struct fw_device *fw_device_get_by_devt(dev_t devt)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	struct fw_device *device;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	down_read(&fw_device_rwsem);
756*4882a593Smuzhiyun 	device = idr_find(&fw_device_idr, MINOR(devt));
757*4882a593Smuzhiyun 	if (device)
758*4882a593Smuzhiyun 		fw_device_get(device);
759*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	return device;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun struct workqueue_struct *fw_workqueue;
765*4882a593Smuzhiyun EXPORT_SYMBOL(fw_workqueue);
766*4882a593Smuzhiyun 
fw_schedule_device_work(struct fw_device * device,unsigned long delay)767*4882a593Smuzhiyun static void fw_schedule_device_work(struct fw_device *device,
768*4882a593Smuzhiyun 				    unsigned long delay)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun 	queue_delayed_work(fw_workqueue, &device->work, delay);
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun /*
774*4882a593Smuzhiyun  * These defines control the retry behavior for reading the config
775*4882a593Smuzhiyun  * rom.  It shouldn't be necessary to tweak these; if the device
776*4882a593Smuzhiyun  * doesn't respond to a config rom read within 10 seconds, it's not
777*4882a593Smuzhiyun  * going to respond at all.  As for the initial delay, a lot of
778*4882a593Smuzhiyun  * devices will be able to respond within half a second after bus
779*4882a593Smuzhiyun  * reset.  On the other hand, it's not really worth being more
780*4882a593Smuzhiyun  * aggressive than that, since it scales pretty well; if 10 devices
781*4882a593Smuzhiyun  * are plugged in, they're all getting read within one second.
782*4882a593Smuzhiyun  */
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun #define MAX_RETRIES	10
785*4882a593Smuzhiyun #define RETRY_DELAY	(3 * HZ)
786*4882a593Smuzhiyun #define INITIAL_DELAY	(HZ / 2)
787*4882a593Smuzhiyun #define SHUTDOWN_DELAY	(2 * HZ)
788*4882a593Smuzhiyun 
fw_device_shutdown(struct work_struct * work)789*4882a593Smuzhiyun static void fw_device_shutdown(struct work_struct *work)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun 	struct fw_device *device =
792*4882a593Smuzhiyun 		container_of(work, struct fw_device, work.work);
793*4882a593Smuzhiyun 	int minor = MINOR(device->device.devt);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	if (time_before64(get_jiffies_64(),
796*4882a593Smuzhiyun 			  device->card->reset_jiffies + SHUTDOWN_DELAY)
797*4882a593Smuzhiyun 	    && !list_empty(&device->card->link)) {
798*4882a593Smuzhiyun 		fw_schedule_device_work(device, SHUTDOWN_DELAY);
799*4882a593Smuzhiyun 		return;
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	if (atomic_cmpxchg(&device->state,
803*4882a593Smuzhiyun 			   FW_DEVICE_GONE,
804*4882a593Smuzhiyun 			   FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
805*4882a593Smuzhiyun 		return;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	fw_device_cdev_remove(device);
808*4882a593Smuzhiyun 	device_for_each_child(&device->device, NULL, shutdown_unit);
809*4882a593Smuzhiyun 	device_unregister(&device->device);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	down_write(&fw_device_rwsem);
812*4882a593Smuzhiyun 	idr_remove(&fw_device_idr, minor);
813*4882a593Smuzhiyun 	up_write(&fw_device_rwsem);
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	fw_device_put(device);
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun 
fw_device_release(struct device * dev)818*4882a593Smuzhiyun static void fw_device_release(struct device *dev)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	struct fw_device *device = fw_device(dev);
821*4882a593Smuzhiyun 	struct fw_card *card = device->card;
822*4882a593Smuzhiyun 	unsigned long flags;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	/*
825*4882a593Smuzhiyun 	 * Take the card lock so we don't set this to NULL while a
826*4882a593Smuzhiyun 	 * FW_NODE_UPDATED callback is being handled or while the
827*4882a593Smuzhiyun 	 * bus manager work looks at this node.
828*4882a593Smuzhiyun 	 */
829*4882a593Smuzhiyun 	spin_lock_irqsave(&card->lock, flags);
830*4882a593Smuzhiyun 	device->node->data = NULL;
831*4882a593Smuzhiyun 	spin_unlock_irqrestore(&card->lock, flags);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	fw_node_put(device->node);
834*4882a593Smuzhiyun 	kfree(device->config_rom);
835*4882a593Smuzhiyun 	kfree(device);
836*4882a593Smuzhiyun 	fw_card_put(card);
837*4882a593Smuzhiyun }
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun static struct device_type fw_device_type = {
840*4882a593Smuzhiyun 	.release = fw_device_release,
841*4882a593Smuzhiyun };
842*4882a593Smuzhiyun 
is_fw_device(struct device * dev)843*4882a593Smuzhiyun static bool is_fw_device(struct device *dev)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	return dev->type == &fw_device_type;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun 
update_unit(struct device * dev,void * data)848*4882a593Smuzhiyun static int update_unit(struct device *dev, void *data)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun 	struct fw_unit *unit = fw_unit(dev);
851*4882a593Smuzhiyun 	struct fw_driver *driver = (struct fw_driver *)dev->driver;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
854*4882a593Smuzhiyun 		device_lock(dev);
855*4882a593Smuzhiyun 		driver->update(unit);
856*4882a593Smuzhiyun 		device_unlock(dev);
857*4882a593Smuzhiyun 	}
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	return 0;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun 
fw_device_update(struct work_struct * work)862*4882a593Smuzhiyun static void fw_device_update(struct work_struct *work)
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	struct fw_device *device =
865*4882a593Smuzhiyun 		container_of(work, struct fw_device, work.work);
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	fw_device_cdev_update(device);
868*4882a593Smuzhiyun 	device_for_each_child(&device->device, NULL, update_unit);
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun /*
872*4882a593Smuzhiyun  * If a device was pending for deletion because its node went away but its
873*4882a593Smuzhiyun  * bus info block and root directory header matches that of a newly discovered
874*4882a593Smuzhiyun  * device, revive the existing fw_device.
875*4882a593Smuzhiyun  * The newly allocated fw_device becomes obsolete instead.
876*4882a593Smuzhiyun  */
lookup_existing_device(struct device * dev,void * data)877*4882a593Smuzhiyun static int lookup_existing_device(struct device *dev, void *data)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	struct fw_device *old = fw_device(dev);
880*4882a593Smuzhiyun 	struct fw_device *new = data;
881*4882a593Smuzhiyun 	struct fw_card *card = new->card;
882*4882a593Smuzhiyun 	int match = 0;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	if (!is_fw_device(dev))
885*4882a593Smuzhiyun 		return 0;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	down_read(&fw_device_rwsem); /* serialize config_rom access */
888*4882a593Smuzhiyun 	spin_lock_irq(&card->lock);  /* serialize node access */
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
891*4882a593Smuzhiyun 	    atomic_cmpxchg(&old->state,
892*4882a593Smuzhiyun 			   FW_DEVICE_GONE,
893*4882a593Smuzhiyun 			   FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
894*4882a593Smuzhiyun 		struct fw_node *current_node = new->node;
895*4882a593Smuzhiyun 		struct fw_node *obsolete_node = old->node;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		new->node = obsolete_node;
898*4882a593Smuzhiyun 		new->node->data = new;
899*4882a593Smuzhiyun 		old->node = current_node;
900*4882a593Smuzhiyun 		old->node->data = old;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 		old->max_speed = new->max_speed;
903*4882a593Smuzhiyun 		old->node_id = current_node->node_id;
904*4882a593Smuzhiyun 		smp_wmb();  /* update node_id before generation */
905*4882a593Smuzhiyun 		old->generation = card->generation;
906*4882a593Smuzhiyun 		old->config_rom_retries = 0;
907*4882a593Smuzhiyun 		fw_notice(card, "rediscovered device %s\n", dev_name(dev));
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 		old->workfn = fw_device_update;
910*4882a593Smuzhiyun 		fw_schedule_device_work(old, 0);
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 		if (current_node == card->root_node)
913*4882a593Smuzhiyun 			fw_schedule_bm_work(card, 0);
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 		match = 1;
916*4882a593Smuzhiyun 	}
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	spin_unlock_irq(&card->lock);
919*4882a593Smuzhiyun 	up_read(&fw_device_rwsem);
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	return match;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
925*4882a593Smuzhiyun 
set_broadcast_channel(struct fw_device * device,int generation)926*4882a593Smuzhiyun static void set_broadcast_channel(struct fw_device *device, int generation)
927*4882a593Smuzhiyun {
928*4882a593Smuzhiyun 	struct fw_card *card = device->card;
929*4882a593Smuzhiyun 	__be32 data;
930*4882a593Smuzhiyun 	int rcode;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	if (!card->broadcast_channel_allocated)
933*4882a593Smuzhiyun 		return;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	/*
936*4882a593Smuzhiyun 	 * The Broadcast_Channel Valid bit is required by nodes which want to
937*4882a593Smuzhiyun 	 * transmit on this channel.  Such transmissions are practically
938*4882a593Smuzhiyun 	 * exclusive to IP over 1394 (RFC 2734).  IP capable nodes are required
939*4882a593Smuzhiyun 	 * to be IRM capable and have a max_rec of 8 or more.  We use this fact
940*4882a593Smuzhiyun 	 * to narrow down to which nodes we send Broadcast_Channel updates.
941*4882a593Smuzhiyun 	 */
942*4882a593Smuzhiyun 	if (!device->irmc || device->max_rec < 8)
943*4882a593Smuzhiyun 		return;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/*
946*4882a593Smuzhiyun 	 * Some 1394-1995 nodes crash if this 1394a-2000 register is written.
947*4882a593Smuzhiyun 	 * Perform a read test first.
948*4882a593Smuzhiyun 	 */
949*4882a593Smuzhiyun 	if (device->bc_implemented == BC_UNKNOWN) {
950*4882a593Smuzhiyun 		rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
951*4882a593Smuzhiyun 				device->node_id, generation, device->max_speed,
952*4882a593Smuzhiyun 				CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
953*4882a593Smuzhiyun 				&data, 4);
954*4882a593Smuzhiyun 		switch (rcode) {
955*4882a593Smuzhiyun 		case RCODE_COMPLETE:
956*4882a593Smuzhiyun 			if (data & cpu_to_be32(1 << 31)) {
957*4882a593Smuzhiyun 				device->bc_implemented = BC_IMPLEMENTED;
958*4882a593Smuzhiyun 				break;
959*4882a593Smuzhiyun 			}
960*4882a593Smuzhiyun 			fallthrough;	/* to case address error */
961*4882a593Smuzhiyun 		case RCODE_ADDRESS_ERROR:
962*4882a593Smuzhiyun 			device->bc_implemented = BC_UNIMPLEMENTED;
963*4882a593Smuzhiyun 		}
964*4882a593Smuzhiyun 	}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	if (device->bc_implemented == BC_IMPLEMENTED) {
967*4882a593Smuzhiyun 		data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
968*4882a593Smuzhiyun 				   BROADCAST_CHANNEL_VALID);
969*4882a593Smuzhiyun 		fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
970*4882a593Smuzhiyun 				device->node_id, generation, device->max_speed,
971*4882a593Smuzhiyun 				CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
972*4882a593Smuzhiyun 				&data, 4);
973*4882a593Smuzhiyun 	}
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
fw_device_set_broadcast_channel(struct device * dev,void * gen)976*4882a593Smuzhiyun int fw_device_set_broadcast_channel(struct device *dev, void *gen)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	if (is_fw_device(dev))
979*4882a593Smuzhiyun 		set_broadcast_channel(fw_device(dev), (long)gen);
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	return 0;
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun 
fw_device_init(struct work_struct * work)984*4882a593Smuzhiyun static void fw_device_init(struct work_struct *work)
985*4882a593Smuzhiyun {
986*4882a593Smuzhiyun 	struct fw_device *device =
987*4882a593Smuzhiyun 		container_of(work, struct fw_device, work.work);
988*4882a593Smuzhiyun 	struct fw_card *card = device->card;
989*4882a593Smuzhiyun 	struct device *revived_dev;
990*4882a593Smuzhiyun 	int minor, ret;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	/*
993*4882a593Smuzhiyun 	 * All failure paths here set node->data to NULL, so that we
994*4882a593Smuzhiyun 	 * don't try to do device_for_each_child() on a kfree()'d
995*4882a593Smuzhiyun 	 * device.
996*4882a593Smuzhiyun 	 */
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	ret = read_config_rom(device, device->generation);
999*4882a593Smuzhiyun 	if (ret != RCODE_COMPLETE) {
1000*4882a593Smuzhiyun 		if (device->config_rom_retries < MAX_RETRIES &&
1001*4882a593Smuzhiyun 		    atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1002*4882a593Smuzhiyun 			device->config_rom_retries++;
1003*4882a593Smuzhiyun 			fw_schedule_device_work(device, RETRY_DELAY);
1004*4882a593Smuzhiyun 		} else {
1005*4882a593Smuzhiyun 			if (device->node->link_on)
1006*4882a593Smuzhiyun 				fw_notice(card, "giving up on node %x: reading config rom failed: %s\n",
1007*4882a593Smuzhiyun 					  device->node_id,
1008*4882a593Smuzhiyun 					  fw_rcode_string(ret));
1009*4882a593Smuzhiyun 			if (device->node == card->root_node)
1010*4882a593Smuzhiyun 				fw_schedule_bm_work(card, 0);
1011*4882a593Smuzhiyun 			fw_device_release(&device->device);
1012*4882a593Smuzhiyun 		}
1013*4882a593Smuzhiyun 		return;
1014*4882a593Smuzhiyun 	}
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	revived_dev = device_find_child(card->device,
1017*4882a593Smuzhiyun 					device, lookup_existing_device);
1018*4882a593Smuzhiyun 	if (revived_dev) {
1019*4882a593Smuzhiyun 		put_device(revived_dev);
1020*4882a593Smuzhiyun 		fw_device_release(&device->device);
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 		return;
1023*4882a593Smuzhiyun 	}
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	device_initialize(&device->device);
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	fw_device_get(device);
1028*4882a593Smuzhiyun 	down_write(&fw_device_rwsem);
1029*4882a593Smuzhiyun 	minor = idr_alloc(&fw_device_idr, device, 0, 1 << MINORBITS,
1030*4882a593Smuzhiyun 			GFP_KERNEL);
1031*4882a593Smuzhiyun 	up_write(&fw_device_rwsem);
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	if (minor < 0)
1034*4882a593Smuzhiyun 		goto error;
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	device->device.bus = &fw_bus_type;
1037*4882a593Smuzhiyun 	device->device.type = &fw_device_type;
1038*4882a593Smuzhiyun 	device->device.parent = card->device;
1039*4882a593Smuzhiyun 	device->device.devt = MKDEV(fw_cdev_major, minor);
1040*4882a593Smuzhiyun 	dev_set_name(&device->device, "fw%d", minor);
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
1043*4882a593Smuzhiyun 			ARRAY_SIZE(fw_device_attributes) +
1044*4882a593Smuzhiyun 			ARRAY_SIZE(config_rom_attributes));
1045*4882a593Smuzhiyun 	init_fw_attribute_group(&device->device,
1046*4882a593Smuzhiyun 				fw_device_attributes,
1047*4882a593Smuzhiyun 				&device->attribute_group);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	if (device_add(&device->device)) {
1050*4882a593Smuzhiyun 		fw_err(card, "failed to add device\n");
1051*4882a593Smuzhiyun 		goto error_with_cdev;
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	create_units(device);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	/*
1057*4882a593Smuzhiyun 	 * Transition the device to running state.  If it got pulled
1058*4882a593Smuzhiyun 	 * out from under us while we did the initialization work, we
1059*4882a593Smuzhiyun 	 * have to shut down the device again here.  Normally, though,
1060*4882a593Smuzhiyun 	 * fw_node_event will be responsible for shutting it down when
1061*4882a593Smuzhiyun 	 * necessary.  We have to use the atomic cmpxchg here to avoid
1062*4882a593Smuzhiyun 	 * racing with the FW_NODE_DESTROYED case in
1063*4882a593Smuzhiyun 	 * fw_node_event().
1064*4882a593Smuzhiyun 	 */
1065*4882a593Smuzhiyun 	if (atomic_cmpxchg(&device->state,
1066*4882a593Smuzhiyun 			   FW_DEVICE_INITIALIZING,
1067*4882a593Smuzhiyun 			   FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
1068*4882a593Smuzhiyun 		device->workfn = fw_device_shutdown;
1069*4882a593Smuzhiyun 		fw_schedule_device_work(device, SHUTDOWN_DELAY);
1070*4882a593Smuzhiyun 	} else {
1071*4882a593Smuzhiyun 		fw_notice(card, "created device %s: GUID %08x%08x, S%d00\n",
1072*4882a593Smuzhiyun 			  dev_name(&device->device),
1073*4882a593Smuzhiyun 			  device->config_rom[3], device->config_rom[4],
1074*4882a593Smuzhiyun 			  1 << device->max_speed);
1075*4882a593Smuzhiyun 		device->config_rom_retries = 0;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 		set_broadcast_channel(device, device->generation);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 		add_device_randomness(&device->config_rom[3], 8);
1080*4882a593Smuzhiyun 	}
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	/*
1083*4882a593Smuzhiyun 	 * Reschedule the IRM work if we just finished reading the
1084*4882a593Smuzhiyun 	 * root node config rom.  If this races with a bus reset we
1085*4882a593Smuzhiyun 	 * just end up running the IRM work a couple of extra times -
1086*4882a593Smuzhiyun 	 * pretty harmless.
1087*4882a593Smuzhiyun 	 */
1088*4882a593Smuzhiyun 	if (device->node == card->root_node)
1089*4882a593Smuzhiyun 		fw_schedule_bm_work(card, 0);
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 	return;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun  error_with_cdev:
1094*4882a593Smuzhiyun 	down_write(&fw_device_rwsem);
1095*4882a593Smuzhiyun 	idr_remove(&fw_device_idr, minor);
1096*4882a593Smuzhiyun 	up_write(&fw_device_rwsem);
1097*4882a593Smuzhiyun  error:
1098*4882a593Smuzhiyun 	fw_device_put(device);		/* fw_device_idr's reference */
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	put_device(&device->device);	/* our reference */
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun /* Reread and compare bus info block and header of root directory */
reread_config_rom(struct fw_device * device,int generation,bool * changed)1104*4882a593Smuzhiyun static int reread_config_rom(struct fw_device *device, int generation,
1105*4882a593Smuzhiyun 			     bool *changed)
1106*4882a593Smuzhiyun {
1107*4882a593Smuzhiyun 	u32 q;
1108*4882a593Smuzhiyun 	int i, rcode;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	for (i = 0; i < 6; i++) {
1111*4882a593Smuzhiyun 		rcode = read_rom(device, generation, i, &q);
1112*4882a593Smuzhiyun 		if (rcode != RCODE_COMPLETE)
1113*4882a593Smuzhiyun 			return rcode;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 		if (i == 0 && q == 0)
1116*4882a593Smuzhiyun 			/* inaccessible (see read_config_rom); retry later */
1117*4882a593Smuzhiyun 			return RCODE_BUSY;
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 		if (q != device->config_rom[i]) {
1120*4882a593Smuzhiyun 			*changed = true;
1121*4882a593Smuzhiyun 			return RCODE_COMPLETE;
1122*4882a593Smuzhiyun 		}
1123*4882a593Smuzhiyun 	}
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	*changed = false;
1126*4882a593Smuzhiyun 	return RCODE_COMPLETE;
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun 
fw_device_refresh(struct work_struct * work)1129*4882a593Smuzhiyun static void fw_device_refresh(struct work_struct *work)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun 	struct fw_device *device =
1132*4882a593Smuzhiyun 		container_of(work, struct fw_device, work.work);
1133*4882a593Smuzhiyun 	struct fw_card *card = device->card;
1134*4882a593Smuzhiyun 	int ret, node_id = device->node_id;
1135*4882a593Smuzhiyun 	bool changed;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	ret = reread_config_rom(device, device->generation, &changed);
1138*4882a593Smuzhiyun 	if (ret != RCODE_COMPLETE)
1139*4882a593Smuzhiyun 		goto failed_config_rom;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	if (!changed) {
1142*4882a593Smuzhiyun 		if (atomic_cmpxchg(&device->state,
1143*4882a593Smuzhiyun 				   FW_DEVICE_INITIALIZING,
1144*4882a593Smuzhiyun 				   FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1145*4882a593Smuzhiyun 			goto gone;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 		fw_device_update(work);
1148*4882a593Smuzhiyun 		device->config_rom_retries = 0;
1149*4882a593Smuzhiyun 		goto out;
1150*4882a593Smuzhiyun 	}
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	/*
1153*4882a593Smuzhiyun 	 * Something changed.  We keep things simple and don't investigate
1154*4882a593Smuzhiyun 	 * further.  We just destroy all previous units and create new ones.
1155*4882a593Smuzhiyun 	 */
1156*4882a593Smuzhiyun 	device_for_each_child(&device->device, NULL, shutdown_unit);
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	ret = read_config_rom(device, device->generation);
1159*4882a593Smuzhiyun 	if (ret != RCODE_COMPLETE)
1160*4882a593Smuzhiyun 		goto failed_config_rom;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	fw_device_cdev_update(device);
1163*4882a593Smuzhiyun 	create_units(device);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	/* Userspace may want to re-read attributes. */
1166*4882a593Smuzhiyun 	kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	if (atomic_cmpxchg(&device->state,
1169*4882a593Smuzhiyun 			   FW_DEVICE_INITIALIZING,
1170*4882a593Smuzhiyun 			   FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
1171*4882a593Smuzhiyun 		goto gone;
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	fw_notice(card, "refreshed device %s\n", dev_name(&device->device));
1174*4882a593Smuzhiyun 	device->config_rom_retries = 0;
1175*4882a593Smuzhiyun 	goto out;
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun  failed_config_rom:
1178*4882a593Smuzhiyun 	if (device->config_rom_retries < MAX_RETRIES &&
1179*4882a593Smuzhiyun 	    atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1180*4882a593Smuzhiyun 		device->config_rom_retries++;
1181*4882a593Smuzhiyun 		fw_schedule_device_work(device, RETRY_DELAY);
1182*4882a593Smuzhiyun 		return;
1183*4882a593Smuzhiyun 	}
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	fw_notice(card, "giving up on refresh of device %s: %s\n",
1186*4882a593Smuzhiyun 		  dev_name(&device->device), fw_rcode_string(ret));
1187*4882a593Smuzhiyun  gone:
1188*4882a593Smuzhiyun 	atomic_set(&device->state, FW_DEVICE_GONE);
1189*4882a593Smuzhiyun 	device->workfn = fw_device_shutdown;
1190*4882a593Smuzhiyun 	fw_schedule_device_work(device, SHUTDOWN_DELAY);
1191*4882a593Smuzhiyun  out:
1192*4882a593Smuzhiyun 	if (node_id == card->root_node->node_id)
1193*4882a593Smuzhiyun 		fw_schedule_bm_work(card, 0);
1194*4882a593Smuzhiyun }
1195*4882a593Smuzhiyun 
fw_device_workfn(struct work_struct * work)1196*4882a593Smuzhiyun static void fw_device_workfn(struct work_struct *work)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun 	struct fw_device *device = container_of(to_delayed_work(work),
1199*4882a593Smuzhiyun 						struct fw_device, work);
1200*4882a593Smuzhiyun 	device->workfn(work);
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
fw_node_event(struct fw_card * card,struct fw_node * node,int event)1203*4882a593Smuzhiyun void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun 	struct fw_device *device;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	switch (event) {
1208*4882a593Smuzhiyun 	case FW_NODE_CREATED:
1209*4882a593Smuzhiyun 		/*
1210*4882a593Smuzhiyun 		 * Attempt to scan the node, regardless whether its self ID has
1211*4882a593Smuzhiyun 		 * the L (link active) flag set or not.  Some broken devices
1212*4882a593Smuzhiyun 		 * send L=0 but have an up-and-running link; others send L=1
1213*4882a593Smuzhiyun 		 * without actually having a link.
1214*4882a593Smuzhiyun 		 */
1215*4882a593Smuzhiyun  create:
1216*4882a593Smuzhiyun 		device = kzalloc(sizeof(*device), GFP_ATOMIC);
1217*4882a593Smuzhiyun 		if (device == NULL)
1218*4882a593Smuzhiyun 			break;
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 		/*
1221*4882a593Smuzhiyun 		 * Do minimal initialization of the device here, the
1222*4882a593Smuzhiyun 		 * rest will happen in fw_device_init().
1223*4882a593Smuzhiyun 		 *
1224*4882a593Smuzhiyun 		 * Attention:  A lot of things, even fw_device_get(),
1225*4882a593Smuzhiyun 		 * cannot be done before fw_device_init() finished!
1226*4882a593Smuzhiyun 		 * You can basically just check device->state and
1227*4882a593Smuzhiyun 		 * schedule work until then, but only while holding
1228*4882a593Smuzhiyun 		 * card->lock.
1229*4882a593Smuzhiyun 		 */
1230*4882a593Smuzhiyun 		atomic_set(&device->state, FW_DEVICE_INITIALIZING);
1231*4882a593Smuzhiyun 		device->card = fw_card_get(card);
1232*4882a593Smuzhiyun 		device->node = fw_node_get(node);
1233*4882a593Smuzhiyun 		device->node_id = node->node_id;
1234*4882a593Smuzhiyun 		device->generation = card->generation;
1235*4882a593Smuzhiyun 		device->is_local = node == card->local_node;
1236*4882a593Smuzhiyun 		mutex_init(&device->client_list_mutex);
1237*4882a593Smuzhiyun 		INIT_LIST_HEAD(&device->client_list);
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 		/*
1240*4882a593Smuzhiyun 		 * Set the node data to point back to this device so
1241*4882a593Smuzhiyun 		 * FW_NODE_UPDATED callbacks can update the node_id
1242*4882a593Smuzhiyun 		 * and generation for the device.
1243*4882a593Smuzhiyun 		 */
1244*4882a593Smuzhiyun 		node->data = device;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 		/*
1247*4882a593Smuzhiyun 		 * Many devices are slow to respond after bus resets,
1248*4882a593Smuzhiyun 		 * especially if they are bus powered and go through
1249*4882a593Smuzhiyun 		 * power-up after getting plugged in.  We schedule the
1250*4882a593Smuzhiyun 		 * first config rom scan half a second after bus reset.
1251*4882a593Smuzhiyun 		 */
1252*4882a593Smuzhiyun 		device->workfn = fw_device_init;
1253*4882a593Smuzhiyun 		INIT_DELAYED_WORK(&device->work, fw_device_workfn);
1254*4882a593Smuzhiyun 		fw_schedule_device_work(device, INITIAL_DELAY);
1255*4882a593Smuzhiyun 		break;
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	case FW_NODE_INITIATED_RESET:
1258*4882a593Smuzhiyun 	case FW_NODE_LINK_ON:
1259*4882a593Smuzhiyun 		device = node->data;
1260*4882a593Smuzhiyun 		if (device == NULL)
1261*4882a593Smuzhiyun 			goto create;
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 		device->node_id = node->node_id;
1264*4882a593Smuzhiyun 		smp_wmb();  /* update node_id before generation */
1265*4882a593Smuzhiyun 		device->generation = card->generation;
1266*4882a593Smuzhiyun 		if (atomic_cmpxchg(&device->state,
1267*4882a593Smuzhiyun 			    FW_DEVICE_RUNNING,
1268*4882a593Smuzhiyun 			    FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1269*4882a593Smuzhiyun 			device->workfn = fw_device_refresh;
1270*4882a593Smuzhiyun 			fw_schedule_device_work(device,
1271*4882a593Smuzhiyun 				device->is_local ? 0 : INITIAL_DELAY);
1272*4882a593Smuzhiyun 		}
1273*4882a593Smuzhiyun 		break;
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	case FW_NODE_UPDATED:
1276*4882a593Smuzhiyun 		device = node->data;
1277*4882a593Smuzhiyun 		if (device == NULL)
1278*4882a593Smuzhiyun 			break;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 		device->node_id = node->node_id;
1281*4882a593Smuzhiyun 		smp_wmb();  /* update node_id before generation */
1282*4882a593Smuzhiyun 		device->generation = card->generation;
1283*4882a593Smuzhiyun 		if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1284*4882a593Smuzhiyun 			device->workfn = fw_device_update;
1285*4882a593Smuzhiyun 			fw_schedule_device_work(device, 0);
1286*4882a593Smuzhiyun 		}
1287*4882a593Smuzhiyun 		break;
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 	case FW_NODE_DESTROYED:
1290*4882a593Smuzhiyun 	case FW_NODE_LINK_OFF:
1291*4882a593Smuzhiyun 		if (!node->data)
1292*4882a593Smuzhiyun 			break;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 		/*
1295*4882a593Smuzhiyun 		 * Destroy the device associated with the node.  There
1296*4882a593Smuzhiyun 		 * are two cases here: either the device is fully
1297*4882a593Smuzhiyun 		 * initialized (FW_DEVICE_RUNNING) or we're in the
1298*4882a593Smuzhiyun 		 * process of reading its config rom
1299*4882a593Smuzhiyun 		 * (FW_DEVICE_INITIALIZING).  If it is fully
1300*4882a593Smuzhiyun 		 * initialized we can reuse device->work to schedule a
1301*4882a593Smuzhiyun 		 * full fw_device_shutdown().  If not, there's work
1302*4882a593Smuzhiyun 		 * scheduled to read it's config rom, and we just put
1303*4882a593Smuzhiyun 		 * the device in shutdown state to have that code fail
1304*4882a593Smuzhiyun 		 * to create the device.
1305*4882a593Smuzhiyun 		 */
1306*4882a593Smuzhiyun 		device = node->data;
1307*4882a593Smuzhiyun 		if (atomic_xchg(&device->state,
1308*4882a593Smuzhiyun 				FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
1309*4882a593Smuzhiyun 			device->workfn = fw_device_shutdown;
1310*4882a593Smuzhiyun 			fw_schedule_device_work(device,
1311*4882a593Smuzhiyun 				list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
1312*4882a593Smuzhiyun 		}
1313*4882a593Smuzhiyun 		break;
1314*4882a593Smuzhiyun 	}
1315*4882a593Smuzhiyun }
1316