blob: 47b5b422616bf1a0ce84fe00705e87889d4f58d5 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Device probing and sysfs code.
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05003 *
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
Stefan Richter41f321c2009-01-17 22:45:54 +010021#include <linux/ctype.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050022#include <linux/delay.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010023#include <linux/device.h>
24#include <linux/errno.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050025#include <linux/idr.h>
Stefan Richter3d36a0d2009-01-17 22:45:54 +010026#include <linux/jiffies.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010027#include <linux/kobject.h>
28#include <linux/list.h>
Stefan Richterb3b29882009-02-15 23:12:34 +010029#include <linux/mod_devicetable.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020030#include <linux/mutex.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040031#include <linux/rwsem.h>
32#include <linux/semaphore.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040033#include <linux/spinlock.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010034#include <linux/string.h>
35#include <linux/workqueue.h>
36
Stefan Richterb5d2a5e2008-01-25 18:57:41 +010037#include <asm/system.h>
Stefan Richter41f321c2009-01-17 22:45:54 +010038
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050039#include "fw-device.h"
Stefan Richter41f321c2009-01-17 22:45:54 +010040#include "fw-topology.h"
41#include "fw-transaction.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050042
43void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
44{
45 ci->p = p + 1;
46 ci->end = ci->p + (p[0] >> 16);
47}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050048EXPORT_SYMBOL(fw_csr_iterator_init);
49
50int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
51{
52 *key = *ci->p >> 24;
53 *value = *ci->p & 0xffffff;
54
55 return ci->p++ < ci->end;
56}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050057EXPORT_SYMBOL(fw_csr_iterator_next);
58
59static int is_fw_unit(struct device *dev);
60
Stefan Richterb3b29882009-02-15 23:12:34 +010061static int match_unit_directory(u32 *directory,
62 const struct ieee1394_device_id *id)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050063{
64 struct fw_csr_iterator ci;
65 int key, value, match;
66
67 match = 0;
68 fw_csr_iterator_init(&ci, directory);
69 while (fw_csr_iterator_next(&ci, &key, &value)) {
Stefan Richterb3b29882009-02-15 23:12:34 +010070 if (key == CSR_VENDOR && value == id->vendor_id)
71 match |= IEEE1394_MATCH_VENDOR_ID;
72 if (key == CSR_MODEL && value == id->model_id)
73 match |= IEEE1394_MATCH_MODEL_ID;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050074 if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
Stefan Richterb3b29882009-02-15 23:12:34 +010075 match |= IEEE1394_MATCH_SPECIFIER_ID;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050076 if (key == CSR_VERSION && value == id->version)
Stefan Richterb3b29882009-02-15 23:12:34 +010077 match |= IEEE1394_MATCH_VERSION;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050078 }
79
80 return (match & id->match_flags) == id->match_flags;
81}
82
83static int fw_unit_match(struct device *dev, struct device_driver *drv)
84{
85 struct fw_unit *unit = fw_unit(dev);
86 struct fw_driver *driver = fw_driver(drv);
87 int i;
88
89 /* We only allow binding to fw_units. */
90 if (!is_fw_unit(dev))
91 return 0;
92
93 for (i = 0; driver->id_table[i].match_flags != 0; i++) {
94 if (match_unit_directory(unit->directory, &driver->id_table[i]))
95 return 1;
96 }
97
98 return 0;
99}
100
101static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
102{
103 struct fw_device *device = fw_device(unit->device.parent);
104 struct fw_csr_iterator ci;
105
106 int key, value;
107 int vendor = 0;
108 int model = 0;
109 int specifier_id = 0;
110 int version = 0;
111
112 fw_csr_iterator_init(&ci, &device->config_rom[5]);
113 while (fw_csr_iterator_next(&ci, &key, &value)) {
114 switch (key) {
115 case CSR_VENDOR:
116 vendor = value;
117 break;
118 case CSR_MODEL:
119 model = value;
120 break;
121 }
122 }
123
124 fw_csr_iterator_init(&ci, unit->directory);
125 while (fw_csr_iterator_next(&ci, &key, &value)) {
126 switch (key) {
127 case CSR_SPECIFIER_ID:
128 specifier_id = value;
129 break;
130 case CSR_VERSION:
131 version = value;
132 break;
133 }
134 }
135
136 return snprintf(buffer, buffer_size,
137 "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
138 vendor, model, specifier_id, version);
139}
140
Stefan Richter53dca512008-12-14 21:47:04 +0100141static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500142{
143 struct fw_unit *unit = fw_unit(dev);
144 char modalias[64];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500145
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400146 get_modalias(unit, modalias, sizeof(modalias));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500147
Kay Sievers7eff2e72007-08-14 15:15:12 +0200148 if (add_uevent_var(env, "MODALIAS=%s", modalias))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500149 return -ENOMEM;
150
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500151 return 0;
152}
153
154struct bus_type fw_bus_type = {
Kristian Høgsberg362c2c82007-02-06 14:49:37 -0500155 .name = "firewire",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500156 .match = fw_unit_match,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500157};
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500158EXPORT_SYMBOL(fw_bus_type);
159
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500160int fw_device_enable_phys_dma(struct fw_device *device)
161{
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100162 int generation = device->generation;
163
164 /* device->node_id, accessed below, must not be older than generation */
165 smp_rmb();
166
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500167 return device->card->driver->enable_phys_dma(device->card,
168 device->node_id,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100169 generation);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500170}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171EXPORT_SYMBOL(fw_device_enable_phys_dma);
172
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400173struct config_rom_attribute {
174 struct device_attribute attr;
175 u32 key;
176};
177
Stefan Richter53dca512008-12-14 21:47:04 +0100178static ssize_t show_immediate(struct device *dev,
179 struct device_attribute *dattr, char *buf)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400180{
181 struct config_rom_attribute *attr =
182 container_of(dattr, struct config_rom_attribute, attr);
183 struct fw_csr_iterator ci;
184 u32 *dir;
Stefan Richterc9755e12008-03-24 20:54:28 +0100185 int key, value, ret = -ENOENT;
186
187 down_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400188
189 if (is_fw_unit(dev))
190 dir = fw_unit(dev)->directory;
191 else
192 dir = fw_device(dev)->config_rom + 5;
193
194 fw_csr_iterator_init(&ci, dir);
195 while (fw_csr_iterator_next(&ci, &key, &value))
Stefan Richterc9755e12008-03-24 20:54:28 +0100196 if (attr->key == key) {
197 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
198 "0x%06x\n", value);
199 break;
200 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400201
Stefan Richterc9755e12008-03-24 20:54:28 +0100202 up_read(&fw_device_rwsem);
203
204 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400205}
206
207#define IMMEDIATE_ATTR(name, key) \
208 { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
209
Stefan Richter53dca512008-12-14 21:47:04 +0100210static ssize_t show_text_leaf(struct device *dev,
211 struct device_attribute *dattr, char *buf)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400212{
213 struct config_rom_attribute *attr =
214 container_of(dattr, struct config_rom_attribute, attr);
215 struct fw_csr_iterator ci;
216 u32 *dir, *block = NULL, *p, *end;
Stefan Richterc9755e12008-03-24 20:54:28 +0100217 int length, key, value, last_key = 0, ret = -ENOENT;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400218 char *b;
219
Stefan Richterc9755e12008-03-24 20:54:28 +0100220 down_read(&fw_device_rwsem);
221
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400222 if (is_fw_unit(dev))
223 dir = fw_unit(dev)->directory;
224 else
225 dir = fw_device(dev)->config_rom + 5;
226
227 fw_csr_iterator_init(&ci, dir);
228 while (fw_csr_iterator_next(&ci, &key, &value)) {
229 if (attr->key == last_key &&
230 key == (CSR_DESCRIPTOR | CSR_LEAF))
231 block = ci.p - 1 + value;
232 last_key = key;
233 }
234
235 if (block == NULL)
Stefan Richterc9755e12008-03-24 20:54:28 +0100236 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400237
238 length = min(block[0] >> 16, 256U);
239 if (length < 3)
Stefan Richterc9755e12008-03-24 20:54:28 +0100240 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400241
242 if (block[1] != 0 || block[2] != 0)
243 /* Unknown encoding. */
Stefan Richterc9755e12008-03-24 20:54:28 +0100244 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400245
Stefan Richterc9755e12008-03-24 20:54:28 +0100246 if (buf == NULL) {
247 ret = length * 4;
248 goto out;
249 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400250
251 b = buf;
252 end = &block[length + 1];
253 for (p = &block[3]; p < end; p++, b += 4)
254 * (u32 *) b = (__force u32) __cpu_to_be32(*p);
255
256 /* Strip trailing whitespace and add newline. */
257 while (b--, (isspace(*b) || *b == '\0') && b > buf);
258 strcpy(b + 1, "\n");
Stefan Richterc9755e12008-03-24 20:54:28 +0100259 ret = b + 2 - buf;
260 out:
261 up_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400262
Stefan Richterc9755e12008-03-24 20:54:28 +0100263 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400264}
265
266#define TEXT_LEAF_ATTR(name, key) \
267 { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
268
269static struct config_rom_attribute config_rom_attributes[] = {
270 IMMEDIATE_ATTR(vendor, CSR_VENDOR),
271 IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
272 IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
273 IMMEDIATE_ATTR(version, CSR_VERSION),
274 IMMEDIATE_ATTR(model, CSR_MODEL),
275 TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
276 TEXT_LEAF_ATTR(model_name, CSR_MODEL),
277 TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
278};
279
Stefan Richter53dca512008-12-14 21:47:04 +0100280static void init_fw_attribute_group(struct device *dev,
281 struct device_attribute *attrs,
282 struct fw_attribute_group *group)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400283{
284 struct device_attribute *attr;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400285 int i, j;
286
287 for (j = 0; attrs[j].attr.name != NULL; j++)
288 group->attrs[j] = &attrs[j].attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400289
290 for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
291 attr = &config_rom_attributes[i].attr;
292 if (attr->show(dev, attr, NULL) < 0)
293 continue;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400294 group->attrs[j++] = &attr->attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400295 }
296
Stefan Richtere5333db2009-05-22 23:16:27 +0200297 group->attrs[j] = NULL;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400298 group->groups[0] = &group->group;
299 group->groups[1] = NULL;
300 group->group.attrs = group->attrs;
301 dev->groups = group->groups;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400302}
303
Stefan Richter53dca512008-12-14 21:47:04 +0100304static ssize_t modalias_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500306{
307 struct fw_unit *unit = fw_unit(dev);
308 int length;
309
310 length = get_modalias(unit, buf, PAGE_SIZE);
311 strcpy(buf + length, "\n");
312
313 return length + 1;
314}
315
Stefan Richter53dca512008-12-14 21:47:04 +0100316static ssize_t rom_index_show(struct device *dev,
317 struct device_attribute *attr, char *buf)
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500318{
319 struct fw_device *device = fw_device(dev->parent);
320 struct fw_unit *unit = fw_unit(dev);
321
322 return snprintf(buf, PAGE_SIZE, "%d\n",
Stefan Richterd84702a2007-03-20 19:42:15 +0100323 (int)(unit->directory - device->config_rom));
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500324}
325
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400326static struct device_attribute fw_unit_attributes[] = {
327 __ATTR_RO(modalias),
328 __ATTR_RO(rom_index),
329 __ATTR_NULL,
330};
331
Stefan Richter53dca512008-12-14 21:47:04 +0100332static ssize_t config_rom_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400334{
335 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100336 size_t length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400337
Stefan Richterc9755e12008-03-24 20:54:28 +0100338 down_read(&fw_device_rwsem);
339 length = device->config_rom_length * 4;
340 memcpy(buf, device->config_rom, length);
341 up_read(&fw_device_rwsem);
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400342
Stefan Richterc9755e12008-03-24 20:54:28 +0100343 return length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400344}
345
Stefan Richter53dca512008-12-14 21:47:04 +0100346static ssize_t guid_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400348{
349 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100350 int ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400351
Stefan Richterc9755e12008-03-24 20:54:28 +0100352 down_read(&fw_device_rwsem);
353 ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
354 device->config_rom[3], device->config_rom[4]);
355 up_read(&fw_device_rwsem);
356
357 return ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400358}
359
Stefan Richter0210b662009-05-23 00:03:29 +0200360static int units_sprintf(char *buf, u32 *directory)
361{
362 struct fw_csr_iterator ci;
363 int key, value;
364 int specifier_id = 0;
365 int version = 0;
366
367 fw_csr_iterator_init(&ci, directory);
368 while (fw_csr_iterator_next(&ci, &key, &value)) {
369 switch (key) {
370 case CSR_SPECIFIER_ID:
371 specifier_id = value;
372 break;
373 case CSR_VERSION:
374 version = value;
375 break;
376 }
377 }
378
379 return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
380}
381
382static ssize_t units_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
384{
385 struct fw_device *device = fw_device(dev);
386 struct fw_csr_iterator ci;
387 int key, value, i = 0;
388
389 down_read(&fw_device_rwsem);
390 fw_csr_iterator_init(&ci, &device->config_rom[5]);
391 while (fw_csr_iterator_next(&ci, &key, &value)) {
392 if (key != (CSR_UNIT | CSR_DIRECTORY))
393 continue;
394 i += units_sprintf(&buf[i], ci.p + value - 1);
395 if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
396 break;
397 }
398 up_read(&fw_device_rwsem);
399
400 if (i)
401 buf[i - 1] = '\n';
402
403 return i;
404}
405
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400406static struct device_attribute fw_device_attributes[] = {
407 __ATTR_RO(config_rom),
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400408 __ATTR_RO(guid),
Stefan Richter0210b662009-05-23 00:03:29 +0200409 __ATTR_RO(units),
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400410 __ATTR_NULL,
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500411};
412
Stefan Richter53dca512008-12-14 21:47:04 +0100413static int read_rom(struct fw_device *device,
414 int generation, int index, u32 *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500415{
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200416 int rcode;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100417
418 /* device->node_id, accessed below, must not be older than generation */
419 smp_rmb();
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500420
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200421 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100422 device->node_id, generation, device->max_speed,
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200423 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
424 data, 4);
425 be32_to_cpus(data);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500426
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200427 return rcode;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500428}
429
Stefan Richter1dadff72008-03-02 19:35:42 +0100430#define READ_BIB_ROM_SIZE 256
431#define READ_BIB_STACK_SIZE 16
432
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100433/*
434 * Read the bus info block, perform a speed probe, and read all of the rest of
435 * the config ROM. We do all this with a cached bus generation. If the bus
436 * generation changes under us, read_bus_info_block will fail and get retried.
437 * It's better to start all over in this case because the node from which we
438 * are reading the ROM may have changed the ROM during the reset.
439 */
440static int read_bus_info_block(struct fw_device *device, int generation)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500441{
Stefan Richterc9755e12008-03-24 20:54:28 +0100442 u32 *rom, *stack, *old_rom, *new_rom;
Stefan Richter1dadff72008-03-02 19:35:42 +0100443 u32 sp, key;
444 int i, end, length, ret = -1;
445
446 rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
447 sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
448 if (rom == NULL)
449 return -ENOMEM;
450
451 stack = &rom[READ_BIB_ROM_SIZE];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500452
Stefan Richterf1397492007-06-10 21:31:36 +0200453 device->max_speed = SCODE_100;
454
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500455 /* First read the bus info block. */
456 for (i = 0; i < 5; i++) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100457 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100458 goto out;
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400459 /*
460 * As per IEEE1212 7.2, during power-up, devices can
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500461 * reply with a 0 for the first quadlet of the config
462 * rom to indicate that they are booting (for example,
463 * if the firmware is on the disk of a external
464 * harddisk). In that case we just fail, and the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400465 * retry mechanism will try again later.
466 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500467 if (i == 0 && rom[i] == 0)
Stefan Richter1dadff72008-03-02 19:35:42 +0100468 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500469 }
470
Stefan Richterf1397492007-06-10 21:31:36 +0200471 device->max_speed = device->node->max_speed;
472
473 /*
474 * Determine the speed of
475 * - devices with link speed less than PHY speed,
476 * - devices with 1394b PHY (unless only connected to 1394a PHYs),
477 * - all devices if there are 1394b repeaters.
478 * Note, we cannot use the bus info block's link_spd as starting point
479 * because some buggy firmwares set it lower than necessary and because
480 * 1394-1995 nodes do not have the field.
481 */
482 if ((rom[2] & 0x7) < device->max_speed ||
483 device->max_speed == SCODE_BETA ||
484 device->card->beta_repeaters_present) {
485 u32 dummy;
486
487 /* for S1600 and S3200 */
488 if (device->max_speed == SCODE_BETA)
489 device->max_speed = device->card->link_speed;
490
491 while (device->max_speed > SCODE_100) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100492 if (read_rom(device, generation, 0, &dummy) ==
493 RCODE_COMPLETE)
Stefan Richterf1397492007-06-10 21:31:36 +0200494 break;
495 device->max_speed--;
496 }
497 }
498
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400499 /*
500 * Now parse the config rom. The config rom is a recursive
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500501 * directory structure so we parse it using a stack of
502 * references to the blocks that make up the structure. We
503 * push a reference to the root directory on the stack to
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400504 * start things off.
505 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500506 length = i;
507 sp = 0;
508 stack[sp++] = 0xc0000005;
509 while (sp > 0) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400510 /*
511 * Pop the next block reference of the stack. The
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500512 * lower 24 bits is the offset into the config rom,
513 * the upper 8 bits are the type of the reference the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400514 * block.
515 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500516 key = stack[--sp];
517 i = key & 0xffffff;
Stefan Richter1dadff72008-03-02 19:35:42 +0100518 if (i >= READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400519 /*
520 * The reference points outside the standard
521 * config rom area, something's fishy.
522 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100523 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500524
525 /* Read header quadlet for the block to get the length. */
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100526 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100527 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500528 end = i + (rom[i] >> 16) + 1;
529 i++;
Stefan Richter1dadff72008-03-02 19:35:42 +0100530 if (end > READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400531 /*
532 * This block extends outside standard config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500533 * area (and the array we're reading it
534 * into). That's broken, so ignore this
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400535 * device.
536 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100537 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400539 /*
540 * Now read in the block. If this is a directory
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500541 * block, check the entries as we read them to see if
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400542 * it references another block, and push it in that case.
543 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500544 while (i < end) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100545 if (read_rom(device, generation, i, &rom[i]) !=
546 RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100547 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500548 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
Stefan Richter1dadff72008-03-02 19:35:42 +0100549 sp < READ_BIB_STACK_SIZE)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500550 stack[sp++] = i + rom[i];
551 i++;
552 }
553 if (length < i)
554 length = i;
555 }
556
Stefan Richterc9755e12008-03-24 20:54:28 +0100557 old_rom = device->config_rom;
558 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
559 if (new_rom == NULL)
Stefan Richter1dadff72008-03-02 19:35:42 +0100560 goto out;
Stefan Richterc9755e12008-03-24 20:54:28 +0100561
562 down_write(&fw_device_rwsem);
563 device->config_rom = new_rom;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564 device->config_rom_length = length;
Stefan Richterc9755e12008-03-24 20:54:28 +0100565 up_write(&fw_device_rwsem);
566
567 kfree(old_rom);
Stefan Richter1dadff72008-03-02 19:35:42 +0100568 ret = 0;
Stefan Richter7889b602009-03-10 21:09:28 +0100569 device->cmc = rom[2] >> 30 & 1;
Stefan Richter1dadff72008-03-02 19:35:42 +0100570 out:
571 kfree(rom);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500572
Stefan Richter1dadff72008-03-02 19:35:42 +0100573 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500574}
575
576static void fw_unit_release(struct device *dev)
577{
578 struct fw_unit *unit = fw_unit(dev);
579
580 kfree(unit);
581}
582
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400583static struct device_type fw_unit_type = {
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400584 .uevent = fw_unit_uevent,
585 .release = fw_unit_release,
586};
587
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500588static int is_fw_unit(struct device *dev)
589{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400590 return dev->type == &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500591}
592
593static void create_units(struct fw_device *device)
594{
595 struct fw_csr_iterator ci;
596 struct fw_unit *unit;
597 int key, value, i;
598
599 i = 0;
600 fw_csr_iterator_init(&ci, &device->config_rom[5]);
601 while (fw_csr_iterator_next(&ci, &key, &value)) {
602 if (key != (CSR_UNIT | CSR_DIRECTORY))
603 continue;
604
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400605 /*
606 * Get the address of the unit directory and try to
607 * match the drivers id_tables against it.
608 */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400609 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500610 if (unit == NULL) {
611 fw_error("failed to allocate memory for unit\n");
612 continue;
613 }
614
615 unit->directory = ci.p + value - 1;
616 unit->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400617 unit->device.type = &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500618 unit->device.parent = &device->device;
Kay Sieversa1f64812008-10-30 01:41:56 +0100619 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500620
Stefan Richtere5333db2009-05-22 23:16:27 +0200621 BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
622 ARRAY_SIZE(fw_unit_attributes) +
623 ARRAY_SIZE(config_rom_attributes));
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400624 init_fw_attribute_group(&unit->device,
625 fw_unit_attributes,
626 &unit->attribute_group);
Stefan Richtere5333db2009-05-22 23:16:27 +0200627
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400628 if (device_register(&unit->device) < 0)
629 goto skip_unit;
630
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400631 continue;
632
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400633 skip_unit:
634 kfree(unit);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500635 }
636}
637
638static int shutdown_unit(struct device *device, void *data)
639{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400640 device_unregister(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500641
642 return 0;
643}
644
Stefan Richterc9755e12008-03-24 20:54:28 +0100645/*
646 * fw_device_rwsem acts as dual purpose mutex:
647 * - serializes accesses to fw_device_idr,
648 * - serializes accesses to fw_device.config_rom/.config_rom_length and
649 * fw_unit.directory, unless those accesses happen at safe occasions
650 */
651DECLARE_RWSEM(fw_device_rwsem);
652
Stefan Richterd6053e02008-11-24 20:40:00 +0100653DEFINE_IDR(fw_device_idr);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500654int fw_cdev_major;
655
Stefan Richter96b19062008-02-02 15:01:09 +0100656struct fw_device *fw_device_get_by_devt(dev_t devt)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500657{
658 struct fw_device *device;
659
Stefan Richterc9755e12008-03-24 20:54:28 +0100660 down_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500661 device = idr_find(&fw_device_idr, MINOR(devt));
Stefan Richter96b19062008-02-02 15:01:09 +0100662 if (device)
663 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100664 up_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500665
666 return device;
667}
668
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400669/*
670 * These defines control the retry behavior for reading the config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500671 * rom. It shouldn't be necessary to tweak these; if the device
672 * doesn't respond to a config rom read within 10 seconds, it's not
673 * going to respond at all. As for the initial delay, a lot of
674 * devices will be able to respond within half a second after bus
675 * reset. On the other hand, it's not really worth being more
676 * aggressive than that, since it scales pretty well; if 10 devices
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400677 * are plugged in, they're all getting read within one second.
678 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500679
Kristian Høgsbergc5dfd0a2007-03-27 01:43:43 -0400680#define MAX_RETRIES 10
681#define RETRY_DELAY (3 * HZ)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500682#define INITIAL_DELAY (HZ / 2)
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100683#define SHUTDOWN_DELAY (2 * HZ)
684
685static void fw_device_shutdown(struct work_struct *work)
686{
687 struct fw_device *device =
688 container_of(work, struct fw_device, work.work);
689 int minor = MINOR(device->device.devt);
690
Stefan Richtere747a5c2009-01-24 20:35:38 +0100691 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
692 && !list_empty(&device->card->link)) {
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100693 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
694 return;
695 }
696
697 if (atomic_cmpxchg(&device->state,
698 FW_DEVICE_GONE,
699 FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
700 return;
701
702 fw_device_cdev_remove(device);
703 device_for_each_child(&device->device, NULL, shutdown_unit);
704 device_unregister(&device->device);
705
706 down_write(&fw_device_rwsem);
707 idr_remove(&fw_device_idr, minor);
708 up_write(&fw_device_rwsem);
709
710 fw_device_put(device);
711}
712
Stefan Richteraed80892009-01-17 22:45:54 +0100713static void fw_device_release(struct device *dev)
714{
715 struct fw_device *device = fw_device(dev);
716 struct fw_card *card = device->card;
717 unsigned long flags;
718
719 /*
720 * Take the card lock so we don't set this to NULL while a
721 * FW_NODE_UPDATED callback is being handled or while the
722 * bus manager work looks at this node.
723 */
724 spin_lock_irqsave(&card->lock, flags);
725 device->node->data = NULL;
726 spin_unlock_irqrestore(&card->lock, flags);
727
728 fw_node_put(device->node);
729 kfree(device->config_rom);
730 kfree(device);
731 fw_card_put(card);
732}
733
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100734static struct device_type fw_device_type = {
Stefan Richteraed80892009-01-17 22:45:54 +0100735 .release = fw_device_release,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100736};
737
Stefan Richteraed80892009-01-17 22:45:54 +0100738static int update_unit(struct device *dev, void *data)
739{
740 struct fw_unit *unit = fw_unit(dev);
741 struct fw_driver *driver = (struct fw_driver *)dev->driver;
742
743 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
744 down(&dev->sem);
745 driver->update(unit);
746 up(&dev->sem);
747 }
748
749 return 0;
750}
751
752static void fw_device_update(struct work_struct *work)
753{
754 struct fw_device *device =
755 container_of(work, struct fw_device, work.work);
756
757 fw_device_cdev_update(device);
758 device_for_each_child(&device->device, NULL, update_unit);
759}
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100760
761/*
762 * If a device was pending for deletion because its node went away but its
763 * bus info block and root directory header matches that of a newly discovered
764 * device, revive the existing fw_device.
765 * The newly allocated fw_device becomes obsolete instead.
766 */
767static int lookup_existing_device(struct device *dev, void *data)
768{
769 struct fw_device *old = fw_device(dev);
770 struct fw_device *new = data;
771 struct fw_card *card = new->card;
772 int match = 0;
773
774 down_read(&fw_device_rwsem); /* serialize config_rom access */
775 spin_lock_irq(&card->lock); /* serialize node access */
776
777 if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
778 atomic_cmpxchg(&old->state,
779 FW_DEVICE_GONE,
780 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
781 struct fw_node *current_node = new->node;
782 struct fw_node *obsolete_node = old->node;
783
784 new->node = obsolete_node;
785 new->node->data = new;
786 old->node = current_node;
787 old->node->data = old;
788
789 old->max_speed = new->max_speed;
790 old->node_id = current_node->node_id;
791 smp_wmb(); /* update node_id before generation */
792 old->generation = card->generation;
793 old->config_rom_retries = 0;
794 fw_notify("rediscovered device %s\n", dev_name(dev));
795
796 PREPARE_DELAYED_WORK(&old->work, fw_device_update);
797 schedule_delayed_work(&old->work, 0);
798
799 if (current_node == card->root_node)
800 fw_schedule_bm_work(card, 0);
801
802 match = 1;
803 }
804
805 spin_unlock_irq(&card->lock);
806 up_read(&fw_device_rwsem);
807
808 return match;
809}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500810
Stefan Richter7889b602009-03-10 21:09:28 +0100811enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
812
813void fw_device_set_broadcast_channel(struct fw_device *device, int generation)
814{
815 struct fw_card *card = device->card;
816 __be32 data;
817 int rcode;
818
819 if (!card->broadcast_channel_allocated)
820 return;
821
822 if (device->bc_implemented == BC_UNKNOWN) {
823 rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
824 device->node_id, generation, device->max_speed,
825 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
826 &data, 4);
827 switch (rcode) {
828 case RCODE_COMPLETE:
829 if (data & cpu_to_be32(1 << 31)) {
830 device->bc_implemented = BC_IMPLEMENTED;
831 break;
832 }
833 /* else fall through to case address error */
834 case RCODE_ADDRESS_ERROR:
835 device->bc_implemented = BC_UNIMPLEMENTED;
836 }
837 }
838
839 if (device->bc_implemented == BC_IMPLEMENTED) {
840 data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
841 BROADCAST_CHANNEL_VALID);
842 fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
843 device->node_id, generation, device->max_speed,
844 CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
845 &data, 4);
846 }
847}
848
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500849static void fw_device_init(struct work_struct *work)
850{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500851 struct fw_device *device =
852 container_of(work, struct fw_device, work.work);
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100853 struct device *revived_dev;
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100854 int minor, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400856 /*
857 * All failure paths here set node->data to NULL, so that we
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500858 * don't try to do device_for_each_child() on a kfree()'d
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400859 * device.
860 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500861
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100862 if (read_bus_info_block(device, device->generation) < 0) {
Stefan Richter855c6032008-02-27 22:14:27 +0100863 if (device->config_rom_retries < MAX_RETRIES &&
864 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500865 device->config_rom_retries++;
866 schedule_delayed_work(&device->work, RETRY_DELAY);
867 } else {
Stefan Richter907293d2007-01-23 21:11:43 +0100868 fw_notify("giving up on config rom for node id %x\n",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500869 device->node_id);
Kristian Høgsberg931c4832007-01-26 00:38:45 -0500870 if (device->node == device->card->root_node)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +0100871 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500872 fw_device_release(&device->device);
873 }
874 return;
875 }
876
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100877 revived_dev = device_find_child(device->card->device,
878 device, lookup_existing_device);
879 if (revived_dev) {
880 put_device(revived_dev);
881 fw_device_release(&device->device);
882
883 return;
884 }
885
Stefan Richter62305822009-01-09 20:49:37 +0100886 device_initialize(&device->device);
Stefan Richter96b19062008-02-02 15:01:09 +0100887
888 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100889 down_write(&fw_device_rwsem);
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100890 ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
Stefan Richter62305822009-01-09 20:49:37 +0100891 idr_get_new(&fw_device_idr, device, &minor) :
892 -ENOMEM;
Stefan Richterc9755e12008-03-24 20:54:28 +0100893 up_write(&fw_device_rwsem);
Stefan Richter96b19062008-02-02 15:01:09 +0100894
Stefan Richtere1eff7a2009-02-03 17:55:19 +0100895 if (ret < 0)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500896 goto error;
897
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500898 device->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400899 device->device.type = &fw_device_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500900 device->device.parent = device->card->device;
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500901 device->device.devt = MKDEV(fw_cdev_major, minor);
Kay Sieversa1f64812008-10-30 01:41:56 +0100902 dev_set_name(&device->device, "fw%d", minor);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500903
Stefan Richtere5333db2009-05-22 23:16:27 +0200904 BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
905 ARRAY_SIZE(fw_device_attributes) +
906 ARRAY_SIZE(config_rom_attributes));
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400907 init_fw_attribute_group(&device->device,
908 fw_device_attributes,
909 &device->attribute_group);
Stefan Richtere5333db2009-05-22 23:16:27 +0200910
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500911 if (device_add(&device->device)) {
912 fw_error("Failed to add device.\n");
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500913 goto error_with_cdev;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500914 }
915
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500916 create_units(device);
917
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400918 /*
919 * Transition the device to running state. If it got pulled
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500920 * out from under us while we did the intialization work, we
921 * have to shut down the device again here. Normally, though,
922 * fw_node_event will be responsible for shutting it down when
923 * necessary. We have to use the atomic cmpxchg here to avoid
924 * racing with the FW_NODE_DESTROYED case in
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400925 * fw_node_event().
926 */
Stefan Richter641f8792007-01-27 10:34:55 +0100927 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100928 FW_DEVICE_INITIALIZING,
929 FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
930 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
931 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterfa6e6972008-02-03 23:03:00 +0100932 } else {
933 if (device->config_rom_retries)
934 fw_notify("created device %s: GUID %08x%08x, S%d00, "
935 "%d config ROM retries\n",
Kay Sieversa1f64812008-10-30 01:41:56 +0100936 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +0100937 device->config_rom[3], device->config_rom[4],
938 1 << device->max_speed,
939 device->config_rom_retries);
940 else
941 fw_notify("created device %s: GUID %08x%08x, S%d00\n",
Kay Sieversa1f64812008-10-30 01:41:56 +0100942 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +0100943 device->config_rom[3], device->config_rom[4],
944 1 << device->max_speed);
Stefan Richterc9755e12008-03-24 20:54:28 +0100945 device->config_rom_retries = 0;
Stefan Richter7889b602009-03-10 21:09:28 +0100946
947 fw_device_set_broadcast_channel(device, device->generation);
Stefan Richterfa6e6972008-02-03 23:03:00 +0100948 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400950 /*
951 * Reschedule the IRM work if we just finished reading the
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500952 * root node config rom. If this races with a bus reset we
953 * just end up running the IRM work a couple of extra times -
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400954 * pretty harmless.
955 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500956 if (device->node == device->card->root_node)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +0100957 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500958
959 return;
960
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500961 error_with_cdev:
Stefan Richterc9755e12008-03-24 20:54:28 +0100962 down_write(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500963 idr_remove(&fw_device_idr, minor);
Stefan Richterc9755e12008-03-24 20:54:28 +0100964 up_write(&fw_device_rwsem);
Stefan Richter373b2ed2007-03-04 14:45:18 +0100965 error:
Stefan Richter96b19062008-02-02 15:01:09 +0100966 fw_device_put(device); /* fw_device_idr's reference */
967
968 put_device(&device->device); /* our reference */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500969}
970
Stefan Richterc9755e12008-03-24 20:54:28 +0100971enum {
972 REREAD_BIB_ERROR,
973 REREAD_BIB_GONE,
974 REREAD_BIB_UNCHANGED,
975 REREAD_BIB_CHANGED,
976};
977
978/* Reread and compare bus info block and header of root directory */
979static int reread_bus_info_block(struct fw_device *device, int generation)
980{
981 u32 q;
982 int i;
983
984 for (i = 0; i < 6; i++) {
985 if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
986 return REREAD_BIB_ERROR;
987
988 if (i == 0 && q == 0)
989 return REREAD_BIB_GONE;
990
Stefan Richterd01b0172009-01-17 22:45:54 +0100991 if (q != device->config_rom[i])
Stefan Richterc9755e12008-03-24 20:54:28 +0100992 return REREAD_BIB_CHANGED;
993 }
994
995 return REREAD_BIB_UNCHANGED;
996}
997
998static void fw_device_refresh(struct work_struct *work)
999{
1000 struct fw_device *device =
1001 container_of(work, struct fw_device, work.work);
1002 struct fw_card *card = device->card;
1003 int node_id = device->node_id;
1004
1005 switch (reread_bus_info_block(device, device->generation)) {
1006 case REREAD_BIB_ERROR:
1007 if (device->config_rom_retries < MAX_RETRIES / 2 &&
1008 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1009 device->config_rom_retries++;
1010 schedule_delayed_work(&device->work, RETRY_DELAY / 2);
1011
1012 return;
1013 }
1014 goto give_up;
1015
1016 case REREAD_BIB_GONE:
1017 goto gone;
1018
1019 case REREAD_BIB_UNCHANGED:
1020 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001021 FW_DEVICE_INITIALIZING,
1022 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +01001023 goto gone;
1024
1025 fw_device_update(work);
1026 device->config_rom_retries = 0;
1027 goto out;
1028
1029 case REREAD_BIB_CHANGED:
1030 break;
1031 }
1032
1033 /*
1034 * Something changed. We keep things simple and don't investigate
1035 * further. We just destroy all previous units and create new ones.
1036 */
1037 device_for_each_child(&device->device, NULL, shutdown_unit);
1038
1039 if (read_bus_info_block(device, device->generation) < 0) {
1040 if (device->config_rom_retries < MAX_RETRIES &&
1041 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
1042 device->config_rom_retries++;
1043 schedule_delayed_work(&device->work, RETRY_DELAY);
1044
1045 return;
1046 }
1047 goto give_up;
1048 }
1049
1050 create_units(device);
1051
Stefan Richter0210b662009-05-23 00:03:29 +02001052 /* Userspace may want to re-read attributes. */
1053 kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
1054
Stefan Richterc9755e12008-03-24 20:54:28 +01001055 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001056 FW_DEVICE_INITIALIZING,
1057 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +01001058 goto gone;
1059
Kay Sieversa1f64812008-10-30 01:41:56 +01001060 fw_notify("refreshed device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +01001061 device->config_rom_retries = 0;
1062 goto out;
1063
1064 give_up:
Kay Sieversa1f64812008-10-30 01:41:56 +01001065 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +01001066 gone:
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001067 atomic_set(&device->state, FW_DEVICE_GONE);
1068 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
1069 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +01001070 out:
1071 if (node_id == card->root_node->node_id)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +01001072 fw_schedule_bm_work(card, 0);
Stefan Richterc9755e12008-03-24 20:54:28 +01001073}
1074
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001075void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
1076{
1077 struct fw_device *device;
1078
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001079 switch (event) {
1080 case FW_NODE_CREATED:
1081 case FW_NODE_LINK_ON:
1082 if (!node->link_on)
1083 break;
Stefan Richterc9755e12008-03-24 20:54:28 +01001084 create:
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001085 device = kzalloc(sizeof(*device), GFP_ATOMIC);
1086 if (device == NULL)
1087 break;
1088
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001089 /*
1090 * Do minimal intialization of the device here, the
Stefan Richter62305822009-01-09 20:49:37 +01001091 * rest will happen in fw_device_init().
1092 *
1093 * Attention: A lot of things, even fw_device_get(),
1094 * cannot be done before fw_device_init() finished!
1095 * You can basically just check device->state and
1096 * schedule work until then, but only while holding
1097 * card->lock.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001098 */
Stefan Richter641f8792007-01-27 10:34:55 +01001099 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
Stefan Richter459f7922008-05-24 16:50:22 +02001100 device->card = fw_card_get(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001101 device->node = fw_node_get(node);
1102 device->node_id = node->node_id;
1103 device->generation = card->generation;
Stefan Richter92368892009-05-13 21:42:14 +02001104 device->is_local = node == card->local_node;
Stefan Richterd67cfb92008-10-05 10:37:11 +02001105 mutex_init(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -05001106 INIT_LIST_HEAD(&device->client_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001107
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001108 /*
1109 * Set the node data to point back to this device so
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001110 * FW_NODE_UPDATED callbacks can update the node_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001111 * and generation for the device.
1112 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001113 node->data = device;
1114
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001115 /*
1116 * Many devices are slow to respond after bus resets,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001117 * especially if they are bus powered and go through
1118 * power-up after getting plugged in. We schedule the
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001119 * first config rom scan half a second after bus reset.
1120 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001121 INIT_DELAYED_WORK(&device->work, fw_device_init);
1122 schedule_delayed_work(&device->work, INITIAL_DELAY);
1123 break;
1124
Stefan Richterc9755e12008-03-24 20:54:28 +01001125 case FW_NODE_INITIATED_RESET:
1126 device = node->data;
1127 if (device == NULL)
1128 goto create;
1129
1130 device->node_id = node->node_id;
1131 smp_wmb(); /* update node_id before generation */
1132 device->generation = card->generation;
1133 if (atomic_cmpxchg(&device->state,
1134 FW_DEVICE_RUNNING,
1135 FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
1136 PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
1137 schedule_delayed_work(&device->work,
Stefan Richter92368892009-05-13 21:42:14 +02001138 device->is_local ? 0 : INITIAL_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +01001139 }
1140 break;
1141
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001142 case FW_NODE_UPDATED:
1143 if (!node->link_on || node->data == NULL)
1144 break;
1145
1146 device = node->data;
1147 device->node_id = node->node_id;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +01001148 smp_wmb(); /* update node_id before generation */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001149 device->generation = card->generation;
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001150 if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
1151 PREPARE_DELAYED_WORK(&device->work, fw_device_update);
1152 schedule_delayed_work(&device->work, 0);
1153 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001154 break;
1155
1156 case FW_NODE_DESTROYED:
1157 case FW_NODE_LINK_OFF:
1158 if (!node->data)
1159 break;
1160
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001161 /*
1162 * Destroy the device associated with the node. There
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001163 * are two cases here: either the device is fully
1164 * initialized (FW_DEVICE_RUNNING) or we're in the
1165 * process of reading its config rom
1166 * (FW_DEVICE_INITIALIZING). If it is fully
1167 * initialized we can reuse device->work to schedule a
1168 * full fw_device_shutdown(). If not, there's work
1169 * scheduled to read it's config rom, and we just put
1170 * the device in shutdown state to have that code fail
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001171 * to create the device.
1172 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001173 device = node->data;
Stefan Richter641f8792007-01-27 10:34:55 +01001174 if (atomic_xchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001175 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001176 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
Stefan Richtere747a5c2009-01-24 20:35:38 +01001177 schedule_delayed_work(&device->work,
1178 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001179 }
1180 break;
1181 }
1182}