fw-device.c 25 KB

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