fw-device.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 or while the
  139. * bus manager work looks at this node.
  140. */
  141. spin_lock_irqsave(&card->lock, flags);
  142. device->node->data = NULL;
  143. spin_unlock_irqrestore(&card->lock, flags);
  144. fw_node_put(device->node);
  145. kfree(device->config_rom);
  146. kfree(device);
  147. fw_card_put(card);
  148. }
  149. int fw_device_enable_phys_dma(struct fw_device *device)
  150. {
  151. int generation = device->generation;
  152. /* device->node_id, accessed below, must not be older than generation */
  153. smp_rmb();
  154. return device->card->driver->enable_phys_dma(device->card,
  155. device->node_id,
  156. generation);
  157. }
  158. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  159. struct config_rom_attribute {
  160. struct device_attribute attr;
  161. u32 key;
  162. };
  163. static ssize_t
  164. show_immediate(struct device *dev, struct device_attribute *dattr, char *buf)
  165. {
  166. struct config_rom_attribute *attr =
  167. container_of(dattr, struct config_rom_attribute, attr);
  168. struct fw_csr_iterator ci;
  169. u32 *dir;
  170. int key, value, ret = -ENOENT;
  171. down_read(&fw_device_rwsem);
  172. if (is_fw_unit(dev))
  173. dir = fw_unit(dev)->directory;
  174. else
  175. dir = fw_device(dev)->config_rom + 5;
  176. fw_csr_iterator_init(&ci, dir);
  177. while (fw_csr_iterator_next(&ci, &key, &value))
  178. if (attr->key == key) {
  179. ret = snprintf(buf, buf ? PAGE_SIZE : 0,
  180. "0x%06x\n", value);
  181. break;
  182. }
  183. up_read(&fw_device_rwsem);
  184. return ret;
  185. }
  186. #define IMMEDIATE_ATTR(name, key) \
  187. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  188. static ssize_t
  189. show_text_leaf(struct device *dev, struct device_attribute *dattr, char *buf)
  190. {
  191. struct config_rom_attribute *attr =
  192. container_of(dattr, struct config_rom_attribute, attr);
  193. struct fw_csr_iterator ci;
  194. u32 *dir, *block = NULL, *p, *end;
  195. int length, key, value, last_key = 0, ret = -ENOENT;
  196. char *b;
  197. down_read(&fw_device_rwsem);
  198. if (is_fw_unit(dev))
  199. dir = fw_unit(dev)->directory;
  200. else
  201. dir = fw_device(dev)->config_rom + 5;
  202. fw_csr_iterator_init(&ci, dir);
  203. while (fw_csr_iterator_next(&ci, &key, &value)) {
  204. if (attr->key == last_key &&
  205. key == (CSR_DESCRIPTOR | CSR_LEAF))
  206. block = ci.p - 1 + value;
  207. last_key = key;
  208. }
  209. if (block == NULL)
  210. goto out;
  211. length = min(block[0] >> 16, 256U);
  212. if (length < 3)
  213. goto out;
  214. if (block[1] != 0 || block[2] != 0)
  215. /* Unknown encoding. */
  216. goto out;
  217. if (buf == NULL) {
  218. ret = length * 4;
  219. goto out;
  220. }
  221. b = buf;
  222. end = &block[length + 1];
  223. for (p = &block[3]; p < end; p++, b += 4)
  224. * (u32 *) b = (__force u32) __cpu_to_be32(*p);
  225. /* Strip trailing whitespace and add newline. */
  226. while (b--, (isspace(*b) || *b == '\0') && b > buf);
  227. strcpy(b + 1, "\n");
  228. ret = b + 2 - buf;
  229. out:
  230. up_read(&fw_device_rwsem);
  231. return ret;
  232. }
  233. #define TEXT_LEAF_ATTR(name, key) \
  234. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  235. static struct config_rom_attribute config_rom_attributes[] = {
  236. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  237. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  238. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  239. IMMEDIATE_ATTR(version, CSR_VERSION),
  240. IMMEDIATE_ATTR(model, CSR_MODEL),
  241. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  242. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  243. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  244. };
  245. static void
  246. init_fw_attribute_group(struct device *dev,
  247. struct device_attribute *attrs,
  248. struct fw_attribute_group *group)
  249. {
  250. struct device_attribute *attr;
  251. int i, j;
  252. for (j = 0; attrs[j].attr.name != NULL; j++)
  253. group->attrs[j] = &attrs[j].attr;
  254. for (i = 0; i < ARRAY_SIZE(config_rom_attributes); i++) {
  255. attr = &config_rom_attributes[i].attr;
  256. if (attr->show(dev, attr, NULL) < 0)
  257. continue;
  258. group->attrs[j++] = &attr->attr;
  259. }
  260. BUG_ON(j >= ARRAY_SIZE(group->attrs));
  261. group->attrs[j++] = NULL;
  262. group->groups[0] = &group->group;
  263. group->groups[1] = NULL;
  264. group->group.attrs = group->attrs;
  265. dev->groups = group->groups;
  266. }
  267. static ssize_t
  268. modalias_show(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. struct fw_unit *unit = fw_unit(dev);
  272. int length;
  273. length = get_modalias(unit, buf, PAGE_SIZE);
  274. strcpy(buf + length, "\n");
  275. return length + 1;
  276. }
  277. static ssize_t
  278. rom_index_show(struct device *dev,
  279. struct device_attribute *attr, char *buf)
  280. {
  281. struct fw_device *device = fw_device(dev->parent);
  282. struct fw_unit *unit = fw_unit(dev);
  283. return snprintf(buf, PAGE_SIZE, "%d\n",
  284. (int)(unit->directory - device->config_rom));
  285. }
  286. static struct device_attribute fw_unit_attributes[] = {
  287. __ATTR_RO(modalias),
  288. __ATTR_RO(rom_index),
  289. __ATTR_NULL,
  290. };
  291. static ssize_t
  292. config_rom_show(struct device *dev, struct device_attribute *attr, char *buf)
  293. {
  294. struct fw_device *device = fw_device(dev);
  295. size_t length;
  296. down_read(&fw_device_rwsem);
  297. length = device->config_rom_length * 4;
  298. memcpy(buf, device->config_rom, length);
  299. up_read(&fw_device_rwsem);
  300. return length;
  301. }
  302. static ssize_t
  303. guid_show(struct device *dev, struct device_attribute *attr, char *buf)
  304. {
  305. struct fw_device *device = fw_device(dev);
  306. int ret;
  307. down_read(&fw_device_rwsem);
  308. ret = snprintf(buf, PAGE_SIZE, "0x%08x%08x\n",
  309. device->config_rom[3], device->config_rom[4]);
  310. up_read(&fw_device_rwsem);
  311. return ret;
  312. }
  313. static struct device_attribute fw_device_attributes[] = {
  314. __ATTR_RO(config_rom),
  315. __ATTR_RO(guid),
  316. __ATTR_NULL,
  317. };
  318. static int
  319. read_rom(struct fw_device *device, int generation, int index, u32 *data)
  320. {
  321. int rcode;
  322. /* device->node_id, accessed below, must not be older than generation */
  323. smp_rmb();
  324. rcode = fw_run_transaction(device->card, TCODE_READ_QUADLET_REQUEST,
  325. device->node_id, generation, device->max_speed,
  326. (CSR_REGISTER_BASE | CSR_CONFIG_ROM) + index * 4,
  327. data, 4);
  328. be32_to_cpus(data);
  329. return rcode;
  330. }
  331. #define READ_BIB_ROM_SIZE 256
  332. #define READ_BIB_STACK_SIZE 16
  333. /*
  334. * Read the bus info block, perform a speed probe, and read all of the rest of
  335. * the config ROM. We do all this with a cached bus generation. If the bus
  336. * generation changes under us, read_bus_info_block will fail and get retried.
  337. * It's better to start all over in this case because the node from which we
  338. * are reading the ROM may have changed the ROM during the reset.
  339. */
  340. static int read_bus_info_block(struct fw_device *device, int generation)
  341. {
  342. u32 *rom, *stack, *old_rom, *new_rom;
  343. u32 sp, key;
  344. int i, end, length, ret = -1;
  345. rom = kmalloc(sizeof(*rom) * READ_BIB_ROM_SIZE +
  346. sizeof(*stack) * READ_BIB_STACK_SIZE, GFP_KERNEL);
  347. if (rom == NULL)
  348. return -ENOMEM;
  349. stack = &rom[READ_BIB_ROM_SIZE];
  350. device->max_speed = SCODE_100;
  351. /* First read the bus info block. */
  352. for (i = 0; i < 5; i++) {
  353. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  354. goto out;
  355. /*
  356. * As per IEEE1212 7.2, during power-up, devices can
  357. * reply with a 0 for the first quadlet of the config
  358. * rom to indicate that they are booting (for example,
  359. * if the firmware is on the disk of a external
  360. * harddisk). In that case we just fail, and the
  361. * retry mechanism will try again later.
  362. */
  363. if (i == 0 && rom[i] == 0)
  364. goto out;
  365. }
  366. device->max_speed = device->node->max_speed;
  367. /*
  368. * Determine the speed of
  369. * - devices with link speed less than PHY speed,
  370. * - devices with 1394b PHY (unless only connected to 1394a PHYs),
  371. * - all devices if there are 1394b repeaters.
  372. * Note, we cannot use the bus info block's link_spd as starting point
  373. * because some buggy firmwares set it lower than necessary and because
  374. * 1394-1995 nodes do not have the field.
  375. */
  376. if ((rom[2] & 0x7) < device->max_speed ||
  377. device->max_speed == SCODE_BETA ||
  378. device->card->beta_repeaters_present) {
  379. u32 dummy;
  380. /* for S1600 and S3200 */
  381. if (device->max_speed == SCODE_BETA)
  382. device->max_speed = device->card->link_speed;
  383. while (device->max_speed > SCODE_100) {
  384. if (read_rom(device, generation, 0, &dummy) ==
  385. RCODE_COMPLETE)
  386. break;
  387. device->max_speed--;
  388. }
  389. }
  390. /*
  391. * Now parse the config rom. The config rom is a recursive
  392. * directory structure so we parse it using a stack of
  393. * references to the blocks that make up the structure. We
  394. * push a reference to the root directory on the stack to
  395. * start things off.
  396. */
  397. length = i;
  398. sp = 0;
  399. stack[sp++] = 0xc0000005;
  400. while (sp > 0) {
  401. /*
  402. * Pop the next block reference of the stack. The
  403. * lower 24 bits is the offset into the config rom,
  404. * the upper 8 bits are the type of the reference the
  405. * block.
  406. */
  407. key = stack[--sp];
  408. i = key & 0xffffff;
  409. if (i >= READ_BIB_ROM_SIZE)
  410. /*
  411. * The reference points outside the standard
  412. * config rom area, something's fishy.
  413. */
  414. goto out;
  415. /* Read header quadlet for the block to get the length. */
  416. if (read_rom(device, generation, i, &rom[i]) != RCODE_COMPLETE)
  417. goto out;
  418. end = i + (rom[i] >> 16) + 1;
  419. i++;
  420. if (end > READ_BIB_ROM_SIZE)
  421. /*
  422. * This block extends outside standard config
  423. * area (and the array we're reading it
  424. * into). That's broken, so ignore this
  425. * device.
  426. */
  427. goto out;
  428. /*
  429. * Now read in the block. If this is a directory
  430. * block, check the entries as we read them to see if
  431. * it references another block, and push it in that case.
  432. */
  433. while (i < end) {
  434. if (read_rom(device, generation, i, &rom[i]) !=
  435. RCODE_COMPLETE)
  436. goto out;
  437. if ((key >> 30) == 3 && (rom[i] >> 30) > 1 &&
  438. sp < READ_BIB_STACK_SIZE)
  439. stack[sp++] = i + rom[i];
  440. i++;
  441. }
  442. if (length < i)
  443. length = i;
  444. }
  445. old_rom = device->config_rom;
  446. new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
  447. if (new_rom == NULL)
  448. goto out;
  449. down_write(&fw_device_rwsem);
  450. device->config_rom = new_rom;
  451. device->config_rom_length = length;
  452. up_write(&fw_device_rwsem);
  453. kfree(old_rom);
  454. ret = 0;
  455. device->cmc = rom[2] & 1 << 30;
  456. out:
  457. kfree(rom);
  458. return ret;
  459. }
  460. static void fw_unit_release(struct device *dev)
  461. {
  462. struct fw_unit *unit = fw_unit(dev);
  463. kfree(unit);
  464. }
  465. static struct device_type fw_unit_type = {
  466. .uevent = fw_unit_uevent,
  467. .release = fw_unit_release,
  468. };
  469. static int is_fw_unit(struct device *dev)
  470. {
  471. return dev->type == &fw_unit_type;
  472. }
  473. static void create_units(struct fw_device *device)
  474. {
  475. struct fw_csr_iterator ci;
  476. struct fw_unit *unit;
  477. int key, value, i;
  478. i = 0;
  479. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  480. while (fw_csr_iterator_next(&ci, &key, &value)) {
  481. if (key != (CSR_UNIT | CSR_DIRECTORY))
  482. continue;
  483. /*
  484. * Get the address of the unit directory and try to
  485. * match the drivers id_tables against it.
  486. */
  487. unit = kzalloc(sizeof(*unit), GFP_KERNEL);
  488. if (unit == NULL) {
  489. fw_error("failed to allocate memory for unit\n");
  490. continue;
  491. }
  492. unit->directory = ci.p + value - 1;
  493. unit->device.bus = &fw_bus_type;
  494. unit->device.type = &fw_unit_type;
  495. unit->device.parent = &device->device;
  496. dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), 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. 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. fw_schedule_bm_work(device->card, 0);
  580. fw_device_release(&device->device);
  581. }
  582. return;
  583. }
  584. device_initialize(&device->device);
  585. fw_device_get(device);
  586. down_write(&fw_device_rwsem);
  587. err = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
  588. idr_get_new(&fw_device_idr, device, &minor) :
  589. -ENOMEM;
  590. up_write(&fw_device_rwsem);
  591. if (err < 0)
  592. goto error;
  593. device->device.bus = &fw_bus_type;
  594. device->device.type = &fw_device_type;
  595. device->device.parent = device->card->device;
  596. device->device.devt = MKDEV(fw_cdev_major, minor);
  597. dev_set_name(&device->device, "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. dev_name(&device->device),
  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. dev_name(&device->device),
  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. fw_schedule_bm_work(device->card, 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", dev_name(&device->device));
  738. device->config_rom_retries = 0;
  739. goto out;
  740. give_up:
  741. fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
  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. fw_schedule_bm_work(card, 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().
  764. *
  765. * Attention: A lot of things, even fw_device_get(),
  766. * cannot be done before fw_device_init() finished!
  767. * You can basically just check device->state and
  768. * schedule work until then, but only while holding
  769. * card->lock.
  770. */
  771. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  772. device->card = fw_card_get(card);
  773. device->node = fw_node_get(node);
  774. device->node_id = node->node_id;
  775. device->generation = card->generation;
  776. INIT_LIST_HEAD(&device->client_list);
  777. /*
  778. * Set the node data to point back to this device so
  779. * FW_NODE_UPDATED callbacks can update the node_id
  780. * and generation for the device.
  781. */
  782. node->data = device;
  783. /*
  784. * Many devices are slow to respond after bus resets,
  785. * especially if they are bus powered and go through
  786. * power-up after getting plugged in. We schedule the
  787. * first config rom scan half a second after bus reset.
  788. */
  789. INIT_DELAYED_WORK(&device->work, fw_device_init);
  790. schedule_delayed_work(&device->work, INITIAL_DELAY);
  791. break;
  792. case FW_NODE_INITIATED_RESET:
  793. device = node->data;
  794. if (device == NULL)
  795. goto create;
  796. device->node_id = node->node_id;
  797. smp_wmb(); /* update node_id before generation */
  798. device->generation = card->generation;
  799. if (atomic_cmpxchg(&device->state,
  800. FW_DEVICE_RUNNING,
  801. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  802. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  803. schedule_delayed_work(&device->work,
  804. node == card->local_node ? 0 : INITIAL_DELAY);
  805. }
  806. break;
  807. case FW_NODE_UPDATED:
  808. if (!node->link_on || node->data == NULL)
  809. break;
  810. device = node->data;
  811. device->node_id = node->node_id;
  812. smp_wmb(); /* update node_id before generation */
  813. device->generation = card->generation;
  814. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  815. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  816. schedule_delayed_work(&device->work, 0);
  817. }
  818. break;
  819. case FW_NODE_DESTROYED:
  820. case FW_NODE_LINK_OFF:
  821. if (!node->data)
  822. break;
  823. /*
  824. * Destroy the device associated with the node. There
  825. * are two cases here: either the device is fully
  826. * initialized (FW_DEVICE_RUNNING) or we're in the
  827. * process of reading its config rom
  828. * (FW_DEVICE_INITIALIZING). If it is fully
  829. * initialized we can reuse device->work to schedule a
  830. * full fw_device_shutdown(). If not, there's work
  831. * scheduled to read it's config rom, and we just put
  832. * the device in shutdown state to have that code fail
  833. * to create the device.
  834. */
  835. device = node->data;
  836. if (atomic_xchg(&device->state,
  837. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  838. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  839. schedule_delayed_work(&device->work, 0);
  840. }
  841. break;
  842. }
  843. }