fw-device.c 28 KB

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