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