blob: 2de3dd5ebc4b5af51bd84436f3583cf6f920e9b1 [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
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>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050027#include <linux/idr.h>
Stefan Richter3d36a0d2009-01-17 22:45:54 +010028#include <linux/jiffies.h>
Stefan Richterc9755e12008-03-24 20:54:28 +010029#include <linux/string.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 Richterb5d2a5e2008-01-25 18:57:41 +010034#include <asm/system.h>
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -040035#include <linux/ctype.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050036#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}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050045EXPORT_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}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050054EXPORT_SYMBOL(fw_csr_iterator_next);
55
56static int is_fw_unit(struct device *dev);
57
Stefan Richter21ebcd12007-01-14 15:29:07 +010058static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050059{
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
Kay Sievers7eff2e72007-08-14 15:15:12 +0200138fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500139{
140 struct fw_unit *unit = fw_unit(dev);
141 char modalias[64];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500142
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400143 get_modalias(unit, modalias, sizeof(modalias));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500144
Kay Sievers7eff2e72007-08-14 15:15:12 +0200145 if (add_uevent_var(env, "MODALIAS=%s", modalias))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500146 return -ENOMEM;
147
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500148 return 0;
149}
150
151struct bus_type fw_bus_type = {
Kristian Høgsberg362c2c82007-02-06 14:49:37 -0500152 .name = "firewire",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500153 .match = fw_unit_match,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500154};
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500155EXPORT_SYMBOL(fw_bus_type);
156
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500157static void fw_device_release(struct device *dev)
158{
159 struct fw_device *device = fw_device(dev);
Stefan Richter855c6032008-02-27 22:14:27 +0100160 struct fw_card *card = device->card;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500161 unsigned long flags;
162
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400163 /*
164 * Take the card lock so we don't set this to NULL while a
Stefan Richter62305822009-01-09 20:49:37 +0100165 * FW_NODE_UPDATED callback is being handled or while the
166 * bus manager work looks at this node.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400167 */
Stefan Richterc9755e12008-03-24 20:54:28 +0100168 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500169 device->node->data = NULL;
Stefan Richterc9755e12008-03-24 20:54:28 +0100170 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171
172 fw_node_put(device->node);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500173 kfree(device->config_rom);
174 kfree(device);
Stefan Richter459f7922008-05-24 16:50:22 +0200175 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500176}
177
178int fw_device_enable_phys_dma(struct fw_device *device)
179{
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100180 int generation = device->generation;
181
182 /* device->node_id, accessed below, must not be older than generation */
183 smp_rmb();
184
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500185 return device->card->driver->enable_phys_dma(device->card,
186 device->node_id,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100187 generation);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500188}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500189EXPORT_SYMBOL(fw_device_enable_phys_dma);
190
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400191struct 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;
Stefan Richterc9755e12008-03-24 20:54:28 +0100203 int key, value, ret = -ENOENT;
204
205 down_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400206
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))
Stefan Richterc9755e12008-03-24 20:54:28 +0100214 if (attr->key == key) {
215 ret = snprintf(buf, buf ? PAGE_SIZE : 0,
216 "0x%06x\n", value);
217 break;
218 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400219
Stefan Richterc9755e12008-03-24 20:54:28 +0100220 up_read(&fw_device_rwsem);
221
222 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400223}
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;
Stefan Richterc9755e12008-03-24 20:54:28 +0100235 int length, key, value, last_key = 0, ret = -ENOENT;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400236 char *b;
237
Stefan Richterc9755e12008-03-24 20:54:28 +0100238 down_read(&fw_device_rwsem);
239
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400240 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)
Stefan Richterc9755e12008-03-24 20:54:28 +0100254 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400255
256 length = min(block[0] >> 16, 256U);
257 if (length < 3)
Stefan Richterc9755e12008-03-24 20:54:28 +0100258 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400259
260 if (block[1] != 0 || block[2] != 0)
261 /* Unknown encoding. */
Stefan Richterc9755e12008-03-24 20:54:28 +0100262 goto out;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400263
Stefan Richterc9755e12008-03-24 20:54:28 +0100264 if (buf == NULL) {
265 ret = length * 4;
266 goto out;
267 }
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400268
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");
Stefan Richterc9755e12008-03-24 20:54:28 +0100277 ret = b + 2 - buf;
278 out:
279 up_read(&fw_device_rwsem);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400280
Stefan Richterc9755e12008-03-24 20:54:28 +0100281 return ret;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400282}
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
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400299init_fw_attribute_group(struct device *dev,
300 struct device_attribute *attrs,
301 struct fw_attribute_group *group)
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400302{
303 struct device_attribute *attr;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400304 int i, j;
305
306 for (j = 0; attrs[j].attr.name != NULL; j++)
307 group->attrs[j] = &attrs[j].attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400308
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;
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400313 group->attrs[j++] = &attr->attr;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400314 }
315
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400316 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;
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400322}
323
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500324static ssize_t
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400325modalias_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500327{
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
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500337static ssize_t
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400338rom_index_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500340{
341 struct fw_device *device = fw_device(dev->parent);
342 struct fw_unit *unit = fw_unit(dev);
343
344 return snprintf(buf, PAGE_SIZE, "%d\n",
Stefan Richterd84702a2007-03-20 19:42:15 +0100345 (int)(unit->directory - device->config_rom));
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500346}
347
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400348static struct device_attribute fw_unit_attributes[] = {
349 __ATTR_RO(modalias),
350 __ATTR_RO(rom_index),
351 __ATTR_NULL,
352};
353
354static ssize_t
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400355config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400356{
357 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100358 size_t length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400359
Stefan Richterc9755e12008-03-24 20:54:28 +0100360 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);
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400364
Stefan Richterc9755e12008-03-24 20:54:28 +0100365 return length;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400366}
367
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400368static ssize_t
369guid_show(struct device *dev, struct device_attribute *attr, char *buf)
370{
371 struct fw_device *device = fw_device(dev);
Stefan Richterc9755e12008-03-24 20:54:28 +0100372 int ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400373
Stefan Richterc9755e12008-03-24 20:54:28 +0100374 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);
378
379 return ret;
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400380}
381
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400382static struct device_attribute fw_device_attributes[] = {
383 __ATTR_RO(config_rom),
Kristian Høgsbergbbd14942007-03-20 20:58:35 -0400384 __ATTR_RO(guid),
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400385 __ATTR_NULL,
Kristian Høgsberg048961e2007-03-07 12:12:46 -0500386};
387
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100388static int
389read_rom(struct fw_device *device, int generation, int index, u32 *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500390{
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200391 int rcode;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100392
393 /* device->node_id, accessed below, must not be older than generation */
394 smp_rmb();
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500395
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200396 rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100397 device->node_id, generation, device->max_speed,
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200398 (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
399 data, 4);
400 be32_to_cpus(data);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500401
Jay Fenlason1e119fa2008-07-20 14:20:53 +0200402 return rcode;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500403}
404
Stefan Richter1dadff72008-03-02 19:35:42 +0100405#define READ_BIB_ROM_SIZE 256
406#define READ_BIB_STACK_SIZE 16
407
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100408/*
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)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500416{
Stefan Richterc9755e12008-03-24 20:54:28 +0100417 u32 *rom, *stack, *old_rom, *new_rom;
Stefan Richter1dadff72008-03-02 19:35:42 +0100418 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];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500427
Stefan Richterf1397492007-06-10 21:31:36 +0200428 device->max_speed = SCODE_100;
429
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500430 /* First read the bus info block. */
431 for (i = 0; i < 5; i++) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100432 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100433 goto out;
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400434 /*
435 * As per IEEE1212 7.2, during power-up, devices can
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500436 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400440 * retry mechanism will try again later.
441 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500442 if (i == 0 && rom[i] == 0)
Stefan Richter1dadff72008-03-02 19:35:42 +0100443 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500444 }
445
Stefan Richterf1397492007-06-10 21:31:36 +0200446 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) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100467 if (read_rom(device, generation, 0, &dummy) ==
468 RCODE_COMPLETE)
Stefan Richterf1397492007-06-10 21:31:36 +0200469 break;
470 device->max_speed--;
471 }
472 }
473
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400474 /*
475 * Now parse the config rom. The config rom is a recursive
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500476 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400479 * start things off.
480 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500481 length = i;
482 sp = 0;
483 stack[sp++] = 0xc0000005;
484 while (sp > 0) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400485 /*
486 * Pop the next block reference of the stack. The
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500487 * lower 24 bits is the offset into the config rom,
488 * the upper 8 bits are the type of the reference the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400489 * block.
490 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500491 key = stack[--sp];
492 i = key & 0xffffff;
Stefan Richter1dadff72008-03-02 19:35:42 +0100493 if (i >= READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400494 /*
495 * The reference points outside the standard
496 * config rom area, something's fishy.
497 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100498 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500499
500 /* Read header quadlet for the block to get the length. */
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100501 if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100502 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500503 end = i + (rom[i] >> 16) + 1;
504 i++;
Stefan Richter1dadff72008-03-02 19:35:42 +0100505 if (end > READ_BIB_ROM_SIZE)
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400506 /*
507 * This block extends outside standard config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500508 * area (and the array we're reading it
509 * into). That's broken, so ignore this
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400510 * device.
511 */
Stefan Richter1dadff72008-03-02 19:35:42 +0100512 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500513
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400514 /*
515 * Now read in the block. If this is a directory
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500516 * block, check the entries as we read them to see if
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400517 * it references another block, and push it in that case.
518 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500519 while (i < end) {
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100520 if (read_rom(device, generation, i, &rom[i]) !=
521 RCODE_COMPLETE)
Stefan Richter1dadff72008-03-02 19:35:42 +0100522 goto out;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500523 if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
Stefan Richter1dadff72008-03-02 19:35:42 +0100524 sp < READ_BIB_STACK_SIZE)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500525 stack[sp++] = i + rom[i];
526 i++;
527 }
528 if (length < i)
529 length = i;
530 }
531
Stefan Richterc9755e12008-03-24 20:54:28 +0100532 old_rom = device->config_rom;
533 new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
534 if (new_rom == NULL)
Stefan Richter1dadff72008-03-02 19:35:42 +0100535 goto out;
Stefan Richterc9755e12008-03-24 20:54:28 +0100536
537 down_write(&fw_device_rwsem);
538 device->config_rom = new_rom;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500539 device->config_rom_length = length;
Stefan Richterc9755e12008-03-24 20:54:28 +0100540 up_write(&fw_device_rwsem);
541
542 kfree(old_rom);
Stefan Richter1dadff72008-03-02 19:35:42 +0100543 ret = 0;
Stefan Richterc9755e12008-03-24 20:54:28 +0100544 device->cmc = rom[2] & 1 << 30;
Stefan Richter1dadff72008-03-02 19:35:42 +0100545 out:
546 kfree(rom);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500547
Stefan Richter1dadff72008-03-02 19:35:42 +0100548 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500549}
550
551static void fw_unit_release(struct device *dev)
552{
553 struct fw_unit *unit = fw_unit(dev);
554
555 kfree(unit);
556}
557
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400558static struct device_type fw_unit_type = {
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400559 .uevent = fw_unit_uevent,
560 .release = fw_unit_release,
561};
562
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500563static int is_fw_unit(struct device *dev)
564{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400565 return dev->type == &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500566}
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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400580 /*
581 * Get the address of the unit directory and try to
582 * match the drivers id_tables against it.
583 */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400584 unit = kzalloc(sizeof(*unit), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500585 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;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400592 unit->device.type = &fw_unit_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500593 unit->device.parent = &device->device;
Kay Sieversa1f64812008-10-30 01:41:56 +0100594 dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500595
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400596 init_fw_attribute_group(&unit->device,
597 fw_unit_attributes,
598 &unit->attribute_group);
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400599 if (device_register(&unit->device) < 0)
600 goto skip_unit;
601
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400602 continue;
603
Kristian Høgsberg7feb9cc2007-03-21 10:55:19 -0400604 skip_unit:
605 kfree(unit);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500606 }
607}
608
609static int shutdown_unit(struct device *device, void *data)
610{
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400611 device_unregister(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500612
613 return 0;
614}
615
Stefan Richterc9755e12008-03-24 20:54:28 +0100616/*
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
Stefan Richterd6053e02008-11-24 20:40:00 +0100624DEFINE_IDR(fw_device_idr);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500625int fw_cdev_major;
626
Stefan Richter96b19062008-02-02 15:01:09 +0100627struct fw_device *fw_device_get_by_devt(dev_t devt)
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500628{
629 struct fw_device *device;
630
Stefan Richterc9755e12008-03-24 20:54:28 +0100631 down_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500632 device = idr_find(&fw_device_idr, MINOR(devt));
Stefan Richter96b19062008-02-02 15:01:09 +0100633 if (device)
634 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100635 up_read(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500636
637 return device;
638}
639
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400640/*
641 * These defines control the retry behavior for reading the config
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500642 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400648 * are plugged in, they're all getting read within one second.
649 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500650
Kristian Høgsbergc5dfd0a2007-03-27 01:43:43 -0400651#define MAX_RETRIES 10
652#define RETRY_DELAY (3 * HZ)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500653#define INITIAL_DELAY (HZ / 2)
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100654#define SHUTDOWN_DELAY (2 * HZ)
655
656static void fw_device_shutdown(struct work_struct *work)
657{
658 struct fw_device *device =
659 container_of(work, struct fw_device, work.work);
660 int minor = MINOR(device->device.devt);
661
Stefan Richtere747a5c2009-01-24 20:35:38 +0100662 if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
663 && !list_empty(&device->card->link)) {
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100664 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
673 fw_device_cdev_remove(device);
674 device_for_each_child(&device->device, NULL, shutdown_unit);
675 device_unregister(&device->device);
676
677 down_write(&fw_device_rwsem);
678 idr_remove(&fw_device_idr, minor);
679 up_write(&fw_device_rwsem);
680
681 fw_device_put(device);
682}
683
684static struct device_type fw_device_type = {
685 .release = fw_device_release,
686};
687
688static void fw_device_update(struct work_struct *work);
689
690/*
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.
695 */
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));
724
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}
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500739
740static void fw_device_init(struct work_struct *work)
741{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500742 struct fw_device *device =
743 container_of(work, struct fw_device, work.work);
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100744 struct device *revived_dev;
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500745 int minor, err;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500746
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400747 /*
748 * All failure paths here set node->data to NULL, so that we
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500749 * don't try to do device_for_each_child() on a kfree()'d
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400750 * device.
751 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500752
Stefan Richterf8d2dc32008-01-25 17:53:49 +0100753 if (read_bus_info_block(device, device->generation) < 0) {
Stefan Richter855c6032008-02-27 22:14:27 +0100754 if (device->config_rom_retries < MAX_RETRIES &&
755 atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500756 device->config_rom_retries++;
757 schedule_delayed_work(&device->work, RETRY_DELAY);
758 } else {
Stefan Richter907293d2007-01-23 21:11:43 +0100759 fw_notify("giving up on config rom for node id %x\n",
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500760 device->node_id);
Kristian Høgsberg931c4832007-01-26 00:38:45 -0500761 if (device->node == device->card->root_node)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +0100762 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500763 fw_device_release(&device->device);
764 }
765 return;
766 }
767
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100768 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
Stefan Richter62305822009-01-09 20:49:37 +0100777 device_initialize(&device->device);
Stefan Richter96b19062008-02-02 15:01:09 +0100778
779 fw_device_get(device);
Stefan Richterc9755e12008-03-24 20:54:28 +0100780 down_write(&fw_device_rwsem);
Stefan Richter62305822009-01-09 20:49:37 +0100781 err = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
782 idr_get_new(&fw_device_idr, device, &minor) :
783 -ENOMEM;
Stefan Richterc9755e12008-03-24 20:54:28 +0100784 up_write(&fw_device_rwsem);
Stefan Richter96b19062008-02-02 15:01:09 +0100785
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500786 if (err < 0)
787 goto error;
788
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500789 device->device.bus = &fw_bus_type;
Kristian Høgsberg21351db2007-03-20 20:58:33 -0400790 device->device.type = &fw_device_type;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500791 device->device.parent = device->card->device;
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500792 device->device.devt = MKDEV(fw_cdev_major, minor);
Kay Sieversa1f64812008-10-30 01:41:56 +0100793 dev_set_name(&device->device, "fw%d", minor);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500794
Kristian Høgsberg6f2e53d2007-03-27 19:35:13 -0400795 init_fw_attribute_group(&device->device,
796 fw_device_attributes,
797 &device->attribute_group);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500798 if (device_add(&device->device)) {
799 fw_error("Failed to add device.\n");
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500800 goto error_with_cdev;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500801 }
802
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803 create_units(device);
804
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400805 /*
806 * Transition the device to running state. If it got pulled
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500807 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400812 * fw_node_event().
813 */
Stefan Richter641f8792007-01-27 10:34:55 +0100814 if (atomic_cmpxchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100815 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);
Stefan Richterfa6e6972008-02-03 23:03:00 +0100819 } else {
820 if (device->config_rom_retries)
821 fw_notify("created device %s: GUID %08x%08x, S%d00, "
822 "%d config ROM retries\n",
Kay Sieversa1f64812008-10-30 01:41:56 +0100823 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +0100824 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",
Kay Sieversa1f64812008-10-30 01:41:56 +0100829 dev_name(&device->device),
Stefan Richterfa6e6972008-02-03 23:03:00 +0100830 device->config_rom[3], device->config_rom[4],
831 1 << device->max_speed);
Stefan Richterc9755e12008-03-24 20:54:28 +0100832 device->config_rom_retries = 0;
Stefan Richterfa6e6972008-02-03 23:03:00 +0100833 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500834
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400835 /*
836 * Reschedule the IRM work if we just finished reading the
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500837 * 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 -
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400839 * pretty harmless.
840 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500841 if (device->node == device->card->root_node)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +0100842 fw_schedule_bm_work(device->card, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500843
844 return;
845
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500846 error_with_cdev:
Stefan Richterc9755e12008-03-24 20:54:28 +0100847 down_write(&fw_device_rwsem);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500848 idr_remove(&fw_device_idr, minor);
Stefan Richterc9755e12008-03-24 20:54:28 +0100849 up_write(&fw_device_rwsem);
Stefan Richter373b2ed2007-03-04 14:45:18 +0100850 error:
Stefan Richter96b19062008-02-02 15:01:09 +0100851 fw_device_put(device); /* fw_device_idr's reference */
852
853 put_device(&device->device); /* our reference */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500854}
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
Kristian Høgsberg015b0662007-03-19 11:37:16 -0400861 if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
862 down(&dev->sem);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500863 driver->update(unit);
Kristian Høgsberg015b0662007-03-19 11:37:16 -0400864 up(&dev->sem);
865 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500866
867 return 0;
868}
869
Kristian Høgsberg5f480472007-03-07 12:12:39 -0500870static void fw_device_update(struct work_struct *work)
871{
872 struct fw_device *device =
873 container_of(work, struct fw_device, work.work);
874
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500875 fw_device_cdev_update(device);
Kristian Høgsberg5f480472007-03-07 12:12:39 -0500876 device_for_each_child(&device->device, NULL, update_unit);
877}
878
Stefan Richterc9755e12008-03-24 20:54:28 +0100879enum {
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,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100929 FW_DEVICE_INITIALIZING,
930 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +0100931 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,
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100961 FW_DEVICE_INITIALIZING,
962 FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
Stefan Richterc9755e12008-03-24 20:54:28 +0100963 goto gone;
964
Kay Sieversa1f64812008-10-30 01:41:56 +0100965 fw_notify("refreshed device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +0100966 device->config_rom_retries = 0;
967 goto out;
968
969 give_up:
Kay Sieversa1f64812008-10-30 01:41:56 +0100970 fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
Stefan Richterc9755e12008-03-24 20:54:28 +0100971 gone:
Stefan Richter3d36a0d2009-01-17 22:45:54 +0100972 atomic_set(&device->state, FW_DEVICE_GONE);
973 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
974 schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
Stefan Richterc9755e12008-03-24 20:54:28 +0100975 out:
976 if (node_id == card->root_node->node_id)
Jay Fenlason0fa1986f2008-11-29 17:44:57 +0100977 fw_schedule_bm_work(card, 0);
Stefan Richterc9755e12008-03-24 20:54:28 +0100978}
979
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500980void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
981{
982 struct fw_device *device;
983
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500984 switch (event) {
985 case FW_NODE_CREATED:
986 case FW_NODE_LINK_ON:
987 if (!node->link_on)
988 break;
Stefan Richterc9755e12008-03-24 20:54:28 +0100989 create:
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500990 device = kzalloc(sizeof(*device), GFP_ATOMIC);
991 if (device == NULL)
992 break;
993
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400994 /*
995 * Do minimal intialization of the device here, the
Stefan Richter62305822009-01-09 20:49:37 +0100996 * 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.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001003 */
Stefan Richter641f8792007-01-27 10:34:55 +01001004 atomic_set(&device->state, FW_DEVICE_INITIALIZING);
Stefan Richter459f7922008-05-24 16:50:22 +02001005 device->card = fw_card_get(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001006 device->node = fw_node_get(node);
1007 device->node_id = node->node_id;
1008 device->generation = card->generation;
Stefan Richterd67cfb92008-10-05 10:37:11 +02001009 mutex_init(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -05001010 INIT_LIST_HEAD(&device->client_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001011
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001012 /*
1013 * Set the node data to point back to this device so
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001014 * FW_NODE_UPDATED callbacks can update the node_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001015 * and generation for the device.
1016 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001017 node->data = device;
1018
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001019 /*
1020 * Many devices are slow to respond after bus resets,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001021 * especially if they are bus powered and go through
1022 * power-up after getting plugged in. We schedule the
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001023 * first config rom scan half a second after bus reset.
1024 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001025 INIT_DELAYED_WORK(&device->work, fw_device_init);
1026 schedule_delayed_work(&device->work, INITIAL_DELAY);
1027 break;
1028
Stefan Richterc9755e12008-03-24 20:54:28 +01001029 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
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001046 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;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +01001052 smp_wmb(); /* update node_id before generation */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001053 device->generation = card->generation;
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001054 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 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001058 break;
1059
1060 case FW_NODE_DESTROYED:
1061 case FW_NODE_LINK_OFF:
1062 if (!node->data)
1063 break;
1064
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001065 /*
1066 * Destroy the device associated with the node. There
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001067 * 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
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001075 * to create the device.
1076 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001077 device = node->data;
Stefan Richter641f8792007-01-27 10:34:55 +01001078 if (atomic_xchg(&device->state,
Stefan Richter3d36a0d2009-01-17 22:45:54 +01001079 FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
Kristian Høgsberg5f480472007-03-07 12:12:39 -05001080 PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
Stefan Richtere747a5c2009-01-24 20:35:38 +01001081 schedule_delayed_work(&device->work,
1082 list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001083 }
1084 break;
1085 }
1086}