firewire: standardize a variable name
[linux-block.git] / drivers / firewire / fw-device.c
CommitLineData
c781c06d
KH
1/*
2 * Device probing and sysfs code.
19a15b93
KH
3 *
4 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/wait.h>
23#include <linux/errno.h>
24#include <linux/kthread.h>
25#include <linux/device.h>
26#include <linux/delay.h>
a3aca3da 27#include <linux/idr.h>
3d36a0df 28#include <linux/jiffies.h>
c9755e14 29#include <linux/string.h>
d67cfb96 30#include <linux/mutex.h>
6188e10d
MW
31#include <linux/rwsem.h>
32#include <linux/semaphore.h>
cf417e54 33#include <linux/spinlock.h>
b5d2a5e0 34#include <asm/system.h>
7feb9cce 35#include <linux/ctype.h>
19a15b93
KH
36#include "fw-transaction.h"
37#include "fw-topology.h"
38#include "fw-device.h"
39
40void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
41{
42 ci->p = p + 1;
43 ci->end = ci->p + (p[0] >> 16);
44}
19a15b93
KH
45EXPORT_SYMBOL(fw_csr_iterator_init);
46
47int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
48{
49 *key = *ci->p >> 24;
50 *value = *ci->p & 0xffffff;
51
52 return ci->p++ < ci->end;
53}
19a15b93
KH
54EXPORT_SYMBOL(fw_csr_iterator_next);
55
56static int is_fw_unit(struct device *dev);
57
21ebcd12 58static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
19a15b93
KH
59{
60 struct fw_csr_iterator ci;
61 int key, value, match;
62
63 match = 0;
64 fw_csr_iterator_init(&ci, directory);
65 while (fw_csr_iterator_next(&ci, &key, &value)) {
66 if (key == CSR_VENDOR && value == id->vendor)
67 match |= FW_MATCH_VENDOR;
68 if (key == CSR_MODEL && value == id->model)
69 match |= FW_MATCH_MODEL;
70 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
71 match |= FW_MATCH_SPECIFIER_ID;
72 if (key == CSR_VERSION && value == id->version)
73 match |= FW_MATCH_VERSION;
74 }
75
76 return (match & id->match_flags) == id->match_flags;
77}
78
79static int fw_unit_match(struct device *dev, struct device_driver *drv)
80{
81 struct fw_unit *unit = fw_unit(dev);
82 struct fw_driver *driver = fw_driver(drv);
83 int i;
84
85 /* We only allow binding to fw_units. */
86 if (!is_fw_unit(dev))
87 return 0;
88
89 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
90 if (match_unit_directory(unit->directory, &driver->id_table[i]))
91 return 1;
92 }
93
94 return 0;
95}
96
97static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
98{
99 struct fw_device *device = fw_device(unit->device.parent);
100 struct fw_csr_iterator ci;
101
102 int key, value;
103 int vendor = 0;
104 int model = 0;
105 int specifier_id = 0;
106 int version = 0;
107
108 fw_csr_iterator_init(&ci, &device->config_rom[5]);
109 while (fw_csr_iterator_next(&ci, &key, &value)) {
110 switch (key) {
111 case CSR_VENDOR:
112 vendor = value;
113 break;
114 case CSR_MODEL:
115 model = value;
116 break;
117 }
118 }
119
120 fw_csr_iterator_init(&ci, unit->directory);
121 while (fw_csr_iterator_next(&ci, &key, &value)) {
122 switch (key) {
123 case CSR_SPECIFIER_ID:
124 specifier_id = value;
125 break;
126 case CSR_VERSION:
127 version = value;
128 break;
129 }
130 }
131
132 return snprintf(buffer, buffer_size,
133 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
134 vendor, model, specifier_id, version);
135}
136
137static int
7eff2e7a 138fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
19a15b93
KH
139{
140 struct fw_unit *unit = fw_unit(dev);
141 char modalias[64];
19a15b93 142
2d826cc5 143 get_modalias(unit, modalias, sizeof(modalias));
19a15b93 144
7eff2e7a 145 if (add_uevent_var(env, "MODALIAS=%s", modalias))
19a15b93
KH
146 return -ENOMEM;
147
19a15b93
KH
148 return 0;
149}
150
151struct bus_type fw_bus_type = {
362c2c8c 152 .name = "firewire",
19a15b93 153 .match = fw_unit_match,
19a15b93 154};
19a15b93
KH
155EXPORT_SYMBOL(fw_bus_type);
156
19a15b93
KH
157static void fw_device_release(struct device *dev)
158{
159 struct fw_device *device = fw_device(dev);
855c603d 160 struct fw_card *card = device->card;
19a15b93
KH
161 unsigned long flags;
162
c781c06d
KH
163 /*
164 * Take the card lock so we don't set this to NULL while a
62305823
SR
165 * FW_NODE_UPDATED callback is being handled or while the
166 * bus manager work looks at this node.
c781c06d 167 */
c9755e14 168 spin_lock_irqsave(&card->lock, flags);
19a15b93 169 device->node->data = NULL;
c9755e14 170 spin_unlock_irqrestore(&card->lock, flags);
19a15b93
KH
171
172 fw_node_put(device->node);
19a15b93
KH
173 kfree(device->config_rom);
174 kfree(device);
459f7923 175 fw_card_put(card);
19a15b93
KH
176}
177
178int fw_device_enable_phys_dma(struct fw_device *device)
179{
b5d2a5e0
SR
180 int generation = device->generation;
181
182 /* device->node_id, accessed below, must not be older than generation */
183 smp_rmb();
184
19a15b93
KH
185 return device->card->driver->enable_phys_dma(device->card,
186 device->node_id,
b5d2a5e0 187 generation);
19a15b93 188}
19a15b93
KH
189EXPORT_SYMBOL(fw_device_enable_phys_dma);
190
7feb9cce
KH
191struct config_rom_attribute {
192 struct device_attribute attr;
193 u32 key;
194};
195
196static ssize_t
197show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
198{
199 struct config_rom_attribute *attr =
200 container_of(dattr, struct config_rom_attribute, attr);
201 struct fw_csr_iterator ci;
202 u32 *dir;
c9755e14
SR
203 int key, value, ret = -ENOENT;
204
205 down_read(&fw_device_rwsem);
7feb9cce
KH
206
207 if (is_fw_unit(dev))
208 dir = fw_unit(dev)->directory;
209 else
210 dir = fw_device(dev)->config_rom + 5;
211
212 fw_csr_iterator_init(&ci, dir);
213 while (fw_csr_iterator_next(&ci, &key, &value))
c9755e14
SR
214 if (attr->key == key) {
215 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
216 "0x%06x\n", value);
217 break;
218 }
219
220 up_read(&fw_device_rwsem);
7feb9cce 221
c9755e14 222 return ret;
7feb9cce
KH
223}
224
225#define IMMEDIATE_ATTR(name, key) \
226 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
227
228static ssize_t
229show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
230{
231 struct config_rom_attribute *attr =
232 container_of(dattr, struct config_rom_attribute, attr);
233 struct fw_csr_iterator ci;
234 u32 *dir, *block = NULL, *p, *end;
c9755e14 235 int length, key, value, last_key = 0, ret = -ENOENT;
7feb9cce
KH
236 char *b;
237
c9755e14
SR
238 down_read(&fw_device_rwsem);
239
7feb9cce
KH
240 if (is_fw_unit(dev))
241 dir = fw_unit(dev)->directory;
242 else
243 dir = fw_device(dev)->config_rom + 5;
244
245 fw_csr_iterator_init(&ci, dir);
246 while (fw_csr_iterator_next(&ci, &key, &value)) {
247 if (attr->key == last_key &&
248 key == (CSR_DESCRIPTOR | CSR_LEAF))
249 block = ci.p - 1 + value;
250 last_key = key;
251 }
252
253 if (block == NULL)
c9755e14 254 goto out;
7feb9cce
KH
255
256 length = min(block[0] >> 16, 256U);
257 if (length < 3)
c9755e14 258 goto out;
7feb9cce
KH
259
260 if (block[1] != 0 || block[2] != 0)
261 /* Unknown encoding. */
c9755e14 262 goto out;
7feb9cce 263
c9755e14
SR
264 if (buf == NULL) {
265 ret = length * 4;
266 goto out;
267 }
7feb9cce
KH
268
269 b = buf;
270 end = &block[length + 1];
271 for (p = &block[3]; p < end; p++, b += 4)
272 * (u32 *) b = (__force u32) __cpu_to_be32(*p);
273
274 /* Strip trailing whitespace and add newline. */
275 while (b--, (isspace(*b) || *b == '\0') && b > buf);
276 strcpy(b + 1, "\n");
c9755e14
SR
277 ret = b + 2 - buf;
278 out:
279 up_read(&fw_device_rwsem);
7feb9cce 280
c9755e14 281 return ret;
7feb9cce
KH
282}
283
284#define TEXT_LEAF_ATTR(name, key) \
285 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
286
287static struct config_rom_attribute config_rom_attributes[] = {
288 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
289 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
290 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
291 IMMEDIATE_ATTR(version, CSR_VERSION),
292 IMMEDIATE_ATTR(model, CSR_MODEL),
293 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
294 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
295 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
296};
297
298static void
6f2e53d5
KH
299init_fw_attribute_group(struct device *dev,
300 struct device_attribute *attrs,
301 struct fw_attribute_group *group)
7feb9cce
KH
302{
303 struct device_attribute *attr;
6f2e53d5
KH
304 int i, j;
305
306 for (j = 0; attrs[j].attr.name != NULL; j++)
307 group->attrs[j] = &attrs[j].attr;
7feb9cce
KH
308
309 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
310 attr = &config_rom_attributes[i].attr;
311 if (attr->show(dev, attr, NULL) < 0)
312 continue;
6f2e53d5 313 group->attrs[j++] = &attr->attr;
7feb9cce
KH
314 }
315
6f2e53d5
KH
316 BUG_ON(j >= ARRAY_SIZE(group->attrs));
317 group->attrs[j++] = NULL;
318 group->groups[0] = &group->group;
319 group->groups[1] = NULL;
320 group->group.attrs = group->attrs;
321 dev->groups = group->groups;
7feb9cce
KH
322}
323
19a15b93 324static ssize_t
21351dbe
KH
325modalias_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
19a15b93
KH
327{
328 struct fw_unit *unit = fw_unit(dev);
329 int length;
330
331 length = get_modalias(unit, buf, PAGE_SIZE);
332 strcpy(buf + length, "\n");
333
334 return length + 1;
335}
336
19a15b93 337static ssize_t
21351dbe
KH
338rom_index_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
19a15b93 340{
21351dbe
KH
341 struct fw_device *device = fw_device(dev->parent);
342 struct fw_unit *unit = fw_unit(dev);
19a15b93 343
21351dbe
KH
344 return snprintf(buf, PAGE_SIZE, "%d\n",
345 (int)(unit->directory - device->config_rom));
19a15b93
KH
346}
347
21351dbe
KH
348static struct device_attribute fw_unit_attributes[] = {
349 __ATTR_RO(modalias),
350 __ATTR_RO(rom_index),
351 __ATTR_NULL,
19a15b93
KH
352};
353
048961ef 354static ssize_t
bbd14945 355config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
048961ef 356{
21351dbe 357 struct fw_device *device = fw_device(dev);
c9755e14 358 size_t length;
048961ef 359
c9755e14
SR
360 down_read(&fw_device_rwsem);
361 length = device->config_rom_length * 4;
362 memcpy(buf, device->config_rom, length);
363 up_read(&fw_device_rwsem);
21351dbe 364
c9755e14 365 return length;
048961ef
KH
366}
367
bbd14945
KH
368static ssize_t
369guid_show(struct device *dev, struct device_attribute *attr, char *buf)
370{
371 struct fw_device *device = fw_device(dev);
c9755e14
SR
372 int ret;
373
374 down_read(&fw_device_rwsem);
375 ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
376 device->config_rom[3], device->config_rom[4]);
377 up_read(&fw_device_rwsem);
bbd14945 378
c9755e14 379 return ret;
bbd14945
KH
380}
381
21351dbe
KH
382static struct device_attribute fw_device_attributes[] = {
383 __ATTR_RO(config_rom),
bbd14945 384 __ATTR_RO(guid),
21351dbe 385 __ATTR_NULL,
048961ef
KH
386};
387
f8d2dc39
SR
388static int
389read_rom(struct fw_device *device, int generation, int index, u32 *data)
19a15b93 390{
1e119fa9 391 int rcode;
b5d2a5e0
SR
392
393 /* device->node_id, accessed below, must not be older than generation */
394 smp_rmb();
19a15b93 395
1e119fa9 396 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
b5d2a5e0 397 device->node_id, generation, device->max_speed,
1e119fa9
JF
398 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
399 data, 4);
400 be32_to_cpus(data);
19a15b93 401
1e119fa9 402 return rcode;
19a15b93
KH
403}
404
1dadff71
SR
405#define READ_BIB_ROM_SIZE 256
406#define READ_BIB_STACK_SIZE 16
407
f8d2dc39
SR
408/*
409 * Read the bus info block, perform a speed probe, and read all of the rest of
410 * the config ROM. We do all this with a cached bus generation. If the bus
411 * generation changes under us, read_bus_info_block will fail and get retried.
412 * It's better to start all over in this case because the node from which we
413 * are reading the ROM may have changed the ROM during the reset.
414 */
415static int read_bus_info_block(struct fw_device *device, int generation)
19a15b93 416{
c9755e14 417 u32 *rom, *stack, *old_rom, *new_rom;
1dadff71
SR
418 u32 sp, key;
419 int i, end, length, ret = -1;
420
421 rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
422 sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
423 if (rom == NULL)
424 return -ENOMEM;
425
426 stack = &rom[READ_BIB_ROM_SIZE];
19a15b93 427
f1397490
SR
428 device->max_speed = SCODE_100;
429
19a15b93
KH
430 /* First read the bus info block. */
431 for (i = 0; i < 5; i++) {
f8d2dc39 432 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
1dadff71 433 goto out;
c781c06d
KH
434 /*
435 * As per IEEE1212 7.2, during power-up, devices can
19a15b93
KH
436 * reply with a 0 for the first quadlet of the config
437 * rom to indicate that they are booting (for example,
438 * if the firmware is on the disk of a external
439 * harddisk). In that case we just fail, and the
c781c06d
KH
440 * retry mechanism will try again later.
441 */
19a15b93 442 if (i == 0 && rom[i] == 0)
1dadff71 443 goto out;
19a15b93
KH
444 }
445
f1397490
SR
446 device->max_speed = device->node->max_speed;
447
448 /*
449 * Determine the speed of
450 * - devices with link speed less than PHY speed,
451 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
452 * - all devices if there are 1394b repeaters.
453 * Note, we cannot use the bus info block's link_spd as starting point
454 * because some buggy firmwares set it lower than necessary and because
455 * 1394-1995 nodes do not have the field.
456 */
457 if ((rom[2] & 0x7) < device->max_speed ||
458 device->max_speed == SCODE_BETA ||
459 device->card->beta_repeaters_present) {
460 u32 dummy;
461
462 /* for S1600 and S3200 */
463 if (device->max_speed == SCODE_BETA)
464 device->max_speed = device->card->link_speed;
465
466 while (device->max_speed > SCODE_100) {
f8d2dc39
SR
467 if (read_rom(device, generation, 0, &dummy) ==
468 RCODE_COMPLETE)
f1397490
SR
469 break;
470 device->max_speed--;
471 }
472 }
473
c781c06d
KH
474 /*
475 * Now parse the config rom. The config rom is a recursive
19a15b93
KH
476 * directory structure so we parse it using a stack of
477 * references to the blocks that make up the structure. We
478 * push a reference to the root directory on the stack to
c781c06d
KH
479 * start things off.
480 */
19a15b93
KH
481 length = i;
482 sp = 0;
483 stack[sp++] = 0xc0000005;
484 while (sp > 0) {
c781c06d
KH
485 /*
486 * Pop the next block reference of the stack. The
19a15b93
KH
487 * lower 24 bits is the offset into the config rom,
488 * the upper 8 bits are the type of the reference the
c781c06d
KH
489 * block.
490 */
19a15b93
KH
491 key = stack[--sp];
492 i = key & 0xffffff;
1dadff71 493 if (i >= READ_BIB_ROM_SIZE)
c781c06d
KH
494 /*
495 * The reference points outside the standard
496 * config rom area, something's fishy.
497 */
1dadff71 498 goto out;
19a15b93
KH
499
500 /* Read header quadlet for the block to get the length. */
f8d2dc39 501 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
1dadff71 502 goto out;
19a15b93
KH
503 end = i + (rom[i] >> 16) + 1;
504 i++;
1dadff71 505 if (end > READ_BIB_ROM_SIZE)
c781c06d
KH
506 /*
507 * This block extends outside standard config
19a15b93
KH
508 * area (and the array we're reading it
509 * into). That's broken, so ignore this
c781c06d
KH
510 * device.
511 */
1dadff71 512 goto out;
19a15b93 513
c781c06d
KH
514 /*
515 * Now read in the block. If this is a directory
19a15b93 516 * block, check the entries as we read them to see if
c781c06d
KH
517 * it references another block, and push it in that case.
518 */
19a15b93 519 while (i < end) {
f8d2dc39
SR
520 if (read_rom(device, generation, i, &rom[i]) !=
521 RCODE_COMPLETE)
1dadff71 522 goto out;
19a15b93 523 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
1dadff71 524 sp < READ_BIB_STACK_SIZE)
19a15b93
KH
525 stack[sp++] = i + rom[i];
526 i++;
527 }
528 if (length < i)
529 length = i;
530 }
531
c9755e14
SR
532 old_rom = device->config_rom;
533 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
534 if (new_rom == NULL)
1dadff71 535 goto out;
c9755e14
SR
536
537 down_write(&fw_device_rwsem);
538 device->config_rom = new_rom;
19a15b93 539 device->config_rom_length = length;
c9755e14
SR
540 up_write(&fw_device_rwsem);
541
542 kfree(old_rom);
1dadff71 543 ret = 0;
c9755e14 544 device->cmc = rom[2] & 1 << 30;
1dadff71
SR
545 out:
546 kfree(rom);
19a15b93 547
1dadff71 548 return ret;
19a15b93
KH
549}
550
551static void fw_unit_release(struct device *dev)
552{
553 struct fw_unit *unit = fw_unit(dev);
554
555 kfree(unit);
556}
557
21351dbe 558static struct device_type fw_unit_type = {
21351dbe
KH
559 .uevent = fw_unit_uevent,
560 .release = fw_unit_release,
561};
562
19a15b93
KH
563static int is_fw_unit(struct device *dev)
564{
21351dbe 565 return dev->type == &fw_unit_type;
19a15b93
KH
566}
567
568static void create_units(struct fw_device *device)
569{
570 struct fw_csr_iterator ci;
571 struct fw_unit *unit;
572 int key, value, i;
573
574 i = 0;
575 fw_csr_iterator_init(&ci, &device->config_rom[5]);
576 while (fw_csr_iterator_next(&ci, &key, &value)) {
577 if (key != (CSR_UNIT | CSR_DIRECTORY))
578 continue;
579
c781c06d
KH
580 /*
581 * Get the address of the unit directory and try to
582 * match the drivers id_tables against it.
583 */
2d826cc5 584 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
19a15b93
KH
585 if (unit == NULL) {
586 fw_error("failed to allocate memory for unit\n");
587 continue;
588 }
589
590 unit->directory = ci.p + value - 1;
591 unit->device.bus = &fw_bus_type;
21351dbe 592 unit->device.type = &fw_unit_type;
19a15b93 593 unit->device.parent = &device->device;
a1f64819 594 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
19a15b93 595
6f2e53d5
KH
596 init_fw_attribute_group(&unit->device,
597 fw_unit_attributes,
598 &unit->attribute_group);
7feb9cce
KH
599 if (device_register(&unit->device) < 0)
600 goto skip_unit;
601
7feb9cce
KH
602 continue;
603
7feb9cce
KH
604 skip_unit:
605 kfree(unit);
19a15b93
KH
606 }
607}
608
609static int shutdown_unit(struct device *device, void *data)
610{
21351dbe 611 device_unregister(device);
19a15b93
KH
612
613 return 0;
614}
615
c9755e14
SR
616/*
617 * fw_device_rwsem acts as dual purpose mutex:
618 * - serializes accesses to fw_device_idr,
619 * - serializes accesses to fw_device.config_rom/.config_rom_length and
620 * fw_unit.directory, unless those accesses happen at safe occasions
621 */
622DECLARE_RWSEM(fw_device_rwsem);
623
d6053e08 624DEFINE_IDR(fw_device_idr);
a3aca3da
KH
625int fw_cdev_major;
626
96b19062 627struct fw_device *fw_device_get_by_devt(dev_t devt)
a3aca3da
KH
628{
629 struct fw_device *device;
630
c9755e14 631 down_read(&fw_device_rwsem);
a3aca3da 632 device = idr_find(&fw_device_idr, MINOR(devt));
96b19062
SR
633 if (device)
634 fw_device_get(device);
c9755e14 635 up_read(&fw_device_rwsem);
a3aca3da
KH
636
637 return device;
638}
639
3d36a0df
SR
640/*
641 * These defines control the retry behavior for reading the config
642 * rom. It shouldn't be necessary to tweak these; if the device
643 * doesn't respond to a config rom read within 10 seconds, it's not
644 * going to respond at all. As for the initial delay, a lot of
645 * devices will be able to respond within half a second after bus
646 * reset. On the other hand, it's not really worth being more
647 * aggressive than that, since it scales pretty well; if 10 devices
648 * are plugged in, they're all getting read within one second.
649 */
650
651#define MAX_RETRIES 10
652#define RETRY_DELAY (3 * HZ)
653#define INITIAL_DELAY (HZ / 2)
654#define SHUTDOWN_DELAY (2 * HZ)
655
19a15b93
KH
656static void fw_device_shutdown(struct work_struct *work)
657{
658 struct fw_device *device =
659 container_of(work, struct fw_device, work.work);
a3aca3da
KH
660 int minor = MINOR(device->device.devt);
661
e747a5c0
SR
662 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
663 && !list_empty(&device->card->link)) {
3d36a0df
SR
664 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
665 return;
666 }
667
668 if (atomic_cmpxchg(&device->state,
669 FW_DEVICE_GONE,
670 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
671 return;
672
2603bf21 673 fw_device_cdev_remove(device);
19a15b93
KH
674 device_for_each_child(&device->device, NULL, shutdown_unit);
675 device_unregister(&device->device);
96b19062 676
c9755e14 677 down_write(&fw_device_rwsem);
96b19062 678 idr_remove(&fw_device_idr, minor);
c9755e14 679 up_write(&fw_device_rwsem);
3d36a0df 680
96b19062 681 fw_device_put(device);
19a15b93
KH
682}
683
21351dbe 684static struct device_type fw_device_type = {
21351dbe
KH
685 .release = fw_device_release,
686};
687
3d36a0df
SR
688static void fw_device_update(struct work_struct *work);
689
c781c06d 690/*
3d36a0df
SR
691 * If a device was pending for deletion because its node went away but its
692 * bus info block and root directory header matches that of a newly discovered
693 * device, revive the existing fw_device.
694 * The newly allocated fw_device becomes obsolete instead.
c781c06d 695 */
3d36a0df
SR
696static int lookup_existing_device(struct device *dev, void *data)
697{
698 struct fw_device *old = fw_device(dev);
699 struct fw_device *new = data;
700 struct fw_card *card = new->card;
701 int match = 0;
702
703 down_read(&fw_device_rwsem); /* serialize config_rom access */
704 spin_lock_irq(&card->lock); /* serialize node access */
705
706 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
707 atomic_cmpxchg(&old->state,
708 FW_DEVICE_GONE,
709 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
710 struct fw_node *current_node = new->node;
711 struct fw_node *obsolete_node = old->node;
712
713 new->node = obsolete_node;
714 new->node->data = new;
715 old->node = current_node;
716 old->node->data = old;
717
718 old->max_speed = new->max_speed;
719 old->node_id = current_node->node_id;
720 smp_wmb(); /* update node_id before generation */
721 old->generation = card->generation;
722 old->config_rom_retries = 0;
723 fw_notify("rediscovered device %s\n", dev_name(dev));
19a15b93 724
3d36a0df
SR
725 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
726 schedule_delayed_work(&old->work, 0);
727
728 if (current_node == card->root_node)
729 fw_schedule_bm_work(card, 0);
730
731 match = 1;
732 }
733
734 spin_unlock_irq(&card->lock);
735 up_read(&fw_device_rwsem);
736
737 return match;
738}
19a15b93
KH
739
740static void fw_device_init(struct work_struct *work)
741{
19a15b93
KH
742 struct fw_device *device =
743 container_of(work, struct fw_device, work.work);
3d36a0df 744 struct device *revived_dev;
a3aca3da 745 int minor, err;
19a15b93 746
c781c06d
KH
747 /*
748 * All failure paths here set node->data to NULL, so that we
19a15b93 749 * don't try to do device_for_each_child() on a kfree()'d
c781c06d
KH
750 * device.
751 */
19a15b93 752
f8d2dc39 753 if (read_bus_info_block(device, device->generation) < 0) {
855c603d
SR
754 if (device->config_rom_retries < MAX_RETRIES &&
755 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
19a15b93
KH
756 device->config_rom_retries++;
757 schedule_delayed_work(&device->work, RETRY_DELAY);
758 } else {
907293d7 759 fw_notify("giving up on config rom for node id %x\n",
19a15b93 760 device->node_id);
931c4834 761 if (device->node == device->card->root_node)
0fa1986f 762 fw_schedule_bm_work(device->card, 0);
19a15b93
KH
763 fw_device_release(&device->device);
764 }
765 return;
766 }
767
3d36a0df
SR
768 revived_dev = device_find_child(device->card->device,
769 device, lookup_existing_device);
770 if (revived_dev) {
771 put_device(revived_dev);
772 fw_device_release(&device->device);
773
774 return;
775 }
776
62305823 777 device_initialize(&device->device);
96b19062
SR
778
779 fw_device_get(device);
c9755e14 780 down_write(&fw_device_rwsem);
62305823
SR
781 err = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
782 idr_get_new(&fw_device_idr, device, &minor) :
783 -ENOMEM;
c9755e14 784 up_write(&fw_device_rwsem);
96b19062 785
a3aca3da
KH
786 if (err < 0)
787 goto error;
788
19a15b93 789 device->device.bus = &fw_bus_type;
21351dbe 790 device->device.type = &fw_device_type;
19a15b93 791 device->device.parent = device->card->device;
a3aca3da 792 device->device.devt = MKDEV(fw_cdev_major, minor);
a1f64819 793 dev_set_name(&device->device, "fw%d", minor);
19a15b93 794
6f2e53d5
KH
795 init_fw_attribute_group(&device->device,
796 fw_device_attributes,
797 &device->attribute_group);
19a15b93
KH
798 if (device_add(&device->device)) {
799 fw_error("Failed to add device.\n");
a3aca3da 800 goto error_with_cdev;
19a15b93
KH
801 }
802
19a15b93
KH
803 create_units(device);
804
c781c06d
KH
805 /*
806 * Transition the device to running state. If it got pulled
19a15b93
KH
807 * out from under us while we did the intialization work, we
808 * have to shut down the device again here. Normally, though,
809 * fw_node_event will be responsible for shutting it down when
810 * necessary. We have to use the atomic cmpxchg here to avoid
811 * racing with the FW_NODE_DESTROYED case in
c781c06d
KH
812 * fw_node_event().
813 */
641f8791 814 if (atomic_cmpxchg(&device->state,
3d36a0df
SR
815 FW_DEVICE_INITIALIZING,
816 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
817 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
818 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
fa6e697b
SR
819 } else {
820 if (device->config_rom_retries)
821 fw_notify("created device %s: GUID %08x%08x, S%d00, "
822 "%d config ROM retries\n",
a1f64819 823 dev_name(&device->device),
fa6e697b
SR
824 device->config_rom[3], device->config_rom[4],
825 1 << device->max_speed,
826 device->config_rom_retries);
827 else
828 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
a1f64819 829 dev_name(&device->device),
fa6e697b
SR
830 device->config_rom[3], device->config_rom[4],
831 1 << device->max_speed);
c9755e14 832 device->config_rom_retries = 0;
fa6e697b 833 }
19a15b93 834
c781c06d
KH
835 /*
836 * Reschedule the IRM work if we just finished reading the
19a15b93
KH
837 * root node config rom. If this races with a bus reset we
838 * just end up running the IRM work a couple of extra times -
c781c06d
KH
839 * pretty harmless.
840 */
19a15b93 841 if (device->node == device->card->root_node)
0fa1986f 842 fw_schedule_bm_work(device->card, 0);
19a15b93
KH
843
844 return;
845
a3aca3da 846 error_with_cdev:
c9755e14 847 down_write(&fw_device_rwsem);
a3aca3da 848 idr_remove(&fw_device_idr, minor);
c9755e14 849 up_write(&fw_device_rwsem);
373b2edd 850 error:
96b19062
SR
851 fw_device_put(device); /* fw_device_idr's reference */
852
853 put_device(&device->device); /* our reference */
19a15b93
KH
854}
855
856static int update_unit(struct device *dev, void *data)
857{
858 struct fw_unit *unit = fw_unit(dev);
859 struct fw_driver *driver = (struct fw_driver *)dev->driver;
860
015b066f
KH
861 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
862 down(&dev->sem);
19a15b93 863 driver->update(unit);
015b066f
KH
864 up(&dev->sem);
865 }
19a15b93
KH
866
867 return 0;
868}
869
5f480477
KH
870static void fw_device_update(struct work_struct *work)
871{
872 struct fw_device *device =
873 container_of(work, struct fw_device, work.work);
874
97bd9efa 875 fw_device_cdev_update(device);
5f480477
KH
876 device_for_each_child(&device->device, NULL, update_unit);
877}
878
c9755e14
SR
879enum {
880 REREAD_BIB_ERROR,
881 REREAD_BIB_GONE,
882 REREAD_BIB_UNCHANGED,
883 REREAD_BIB_CHANGED,
884};
885
886/* Reread and compare bus info block and header of root directory */
887static int reread_bus_info_block(struct fw_device *device, int generation)
888{
889 u32 q;
890 int i;
891
892 for (i = 0; i < 6; i++) {
893 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
894 return REREAD_BIB_ERROR;
895
896 if (i == 0 && q == 0)
897 return REREAD_BIB_GONE;
898
899 if (i > device->config_rom_length || q != device->config_rom[i])
900 return REREAD_BIB_CHANGED;
901 }
902
903 return REREAD_BIB_UNCHANGED;
904}
905
906static void fw_device_refresh(struct work_struct *work)
907{
908 struct fw_device *device =
909 container_of(work, struct fw_device, work.work);
910 struct fw_card *card = device->card;
911 int node_id = device->node_id;
912
913 switch (reread_bus_info_block(device, device->generation)) {
914 case REREAD_BIB_ERROR:
915 if (device->config_rom_retries < MAX_RETRIES / 2 &&
916 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
917 device->config_rom_retries++;
918 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
919
920 return;
921 }
922 goto give_up;
923
924 case REREAD_BIB_GONE:
925 goto gone;
926
927 case REREAD_BIB_UNCHANGED:
928 if (atomic_cmpxchg(&device->state,
3d36a0df
SR
929 FW_DEVICE_INITIALIZING,
930 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
c9755e14
SR
931 goto gone;
932
933 fw_device_update(work);
934 device->config_rom_retries = 0;
935 goto out;
936
937 case REREAD_BIB_CHANGED:
938 break;
939 }
940
941 /*
942 * Something changed. We keep things simple and don't investigate
943 * further. We just destroy all previous units and create new ones.
944 */
945 device_for_each_child(&device->device, NULL, shutdown_unit);
946
947 if (read_bus_info_block(device, device->generation) < 0) {
948 if (device->config_rom_retries < MAX_RETRIES &&
949 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
950 device->config_rom_retries++;
951 schedule_delayed_work(&device->work, RETRY_DELAY);
952
953 return;
954 }
955 goto give_up;
956 }
957
958 create_units(device);
959
960 if (atomic_cmpxchg(&device->state,
3d36a0df
SR
961 FW_DEVICE_INITIALIZING,
962 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
c9755e14
SR
963 goto gone;
964
a1f64819 965 fw_notify("refreshed device %s\n", dev_name(&device->device));
c9755e14
SR
966 device->config_rom_retries = 0;
967 goto out;
968
969 give_up:
a1f64819 970 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
c9755e14 971 gone:
3d36a0df
SR
972 atomic_set(&device->state, FW_DEVICE_GONE);
973 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
974 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
c9755e14
SR
975 out:
976 if (node_id == card->root_node->node_id)
0fa1986f 977 fw_schedule_bm_work(card, 0);
c9755e14
SR
978}
979
19a15b93
KH
980void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
981{
982 struct fw_device *device;
983
19a15b93
KH
984 switch (event) {
985 case FW_NODE_CREATED:
986 case FW_NODE_LINK_ON:
987 if (!node->link_on)
988 break;
c9755e14 989 create:
19a15b93
KH
990 device = kzalloc(sizeof(*device), GFP_ATOMIC);
991 if (device == NULL)
992 break;
993
c781c06d
KH
994 /*
995 * Do minimal intialization of the device here, the
62305823
SR
996 * rest will happen in fw_device_init().
997 *
998 * Attention: A lot of things, even fw_device_get(),
999 * cannot be done before fw_device_init() finished!
1000 * You can basically just check device->state and
1001 * schedule work until then, but only while holding
1002 * card->lock.
c781c06d 1003 */
641f8791 1004 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
459f7923 1005 device->card = fw_card_get(card);
19a15b93
KH
1006 device->node = fw_node_get(node);
1007 device->node_id = node->node_id;
1008 device->generation = card->generation;
d67cfb96 1009 mutex_init(&device->client_list_mutex);
97bd9efa 1010 INIT_LIST_HEAD(&device->client_list);
19a15b93 1011
c781c06d
KH
1012 /*
1013 * Set the node data to point back to this device so
19a15b93 1014 * FW_NODE_UPDATED callbacks can update the node_id
c781c06d
KH
1015 * and generation for the device.
1016 */
19a15b93
KH
1017 node->data = device;
1018
c781c06d
KH
1019 /*
1020 * Many devices are slow to respond after bus resets,
19a15b93
KH
1021 * especially if they are bus powered and go through
1022 * power-up after getting plugged in. We schedule the
c781c06d
KH
1023 * first config rom scan half a second after bus reset.
1024 */
19a15b93
KH
1025 INIT_DELAYED_WORK(&device->work, fw_device_init);
1026 schedule_delayed_work(&device->work, INITIAL_DELAY);
1027 break;
1028
c9755e14
SR
1029 case FW_NODE_INITIATED_RESET:
1030 device = node->data;
1031 if (device == NULL)
1032 goto create;
1033
1034 device->node_id = node->node_id;
1035 smp_wmb(); /* update node_id before generation */
1036 device->generation = card->generation;
1037 if (atomic_cmpxchg(&device->state,
1038 FW_DEVICE_RUNNING,
1039 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1040 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1041 schedule_delayed_work(&device->work,
1042 node == card->local_node ? 0 : INITIAL_DELAY);
1043 }
1044 break;
1045
19a15b93
KH
1046 case FW_NODE_UPDATED:
1047 if (!node->link_on || node->data == NULL)
1048 break;
1049
1050 device = node->data;
1051 device->node_id = node->node_id;
b5d2a5e0 1052 smp_wmb(); /* update node_id before generation */
19a15b93 1053 device->generation = card->generation;
5f480477
KH
1054 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1055 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1056 schedule_delayed_work(&device->work, 0);
1057 }
19a15b93
KH
1058 break;
1059
1060 case FW_NODE_DESTROYED:
1061 case FW_NODE_LINK_OFF:
1062 if (!node->data)
1063 break;
1064
c781c06d
KH
1065 /*
1066 * Destroy the device associated with the node. There
19a15b93
KH
1067 * are two cases here: either the device is fully
1068 * initialized (FW_DEVICE_RUNNING) or we're in the
1069 * process of reading its config rom
1070 * (FW_DEVICE_INITIALIZING). If it is fully
1071 * initialized we can reuse device->work to schedule a
1072 * full fw_device_shutdown(). If not, there's work
1073 * scheduled to read it's config rom, and we just put
1074 * the device in shutdown state to have that code fail
c781c06d
KH
1075 * to create the device.
1076 */
19a15b93 1077 device = node->data;
641f8791 1078 if (atomic_xchg(&device->state,
3d36a0df 1079 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
5f480477 1080 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
e747a5c0
SR
1081 schedule_delayed_work(&device->work,
1082 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
19a15b93
KH
1083 }
1084 break;
1085 }
1086}