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