fw-device.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
  496. init_fw_attribute_group(&unit->device,
  497. fw_unit_attributes,
  498. &unit->attribute_group);
  499. if (device_register(&unit->device) < 0)
  500. goto skip_unit;
  501. continue;
  502. skip_unit:
  503. kfree(unit);
  504. }
  505. }
  506. static int shutdown_unit(struct device *device, void *data)
  507. {
  508. device_unregister(device);
  509. return 0;
  510. }
  511. /*
  512. * fw_device_rwsem acts as dual purpose mutex:
  513. * - serializes accesses to fw_device_idr,
  514. * - serializes accesses to fw_device.config_rom/.config_rom_length and
  515. * fw_unit.directory, unless those accesses happen at safe occasions
  516. */
  517. DECLARE_RWSEM(fw_device_rwsem);
  518. static DEFINE_IDR(fw_device_idr);
  519. int fw_cdev_major;
  520. struct fw_device *fw_device_get_by_devt(dev_t devt)
  521. {
  522. struct fw_device *device;
  523. down_read(&fw_device_rwsem);
  524. device = idr_find(&fw_device_idr, MINOR(devt));
  525. if (device)
  526. fw_device_get(device);
  527. up_read(&fw_device_rwsem);
  528. return device;
  529. }
  530. static void fw_device_shutdown(struct work_struct *work)
  531. {
  532. struct fw_device *device =
  533. container_of(work, struct fw_device, work.work);
  534. int minor = MINOR(device->device.devt);
  535. fw_device_cdev_remove(device);
  536. device_for_each_child(&device->device, NULL, shutdown_unit);
  537. device_unregister(&device->device);
  538. down_write(&fw_device_rwsem);
  539. idr_remove(&fw_device_idr, minor);
  540. up_write(&fw_device_rwsem);
  541. fw_device_put(device);
  542. }
  543. static struct device_type fw_device_type = {
  544. .release = fw_device_release,
  545. };
  546. /*
  547. * These defines control the retry behavior for reading the config
  548. * rom. It shouldn't be necessary to tweak these; if the device
  549. * doesn't respond to a config rom read within 10 seconds, it's not
  550. * going to respond at all. As for the initial delay, a lot of
  551. * devices will be able to respond within half a second after bus
  552. * reset. On the other hand, it's not really worth being more
  553. * aggressive than that, since it scales pretty well; if 10 devices
  554. * are plugged in, they're all getting read within one second.
  555. */
  556. #define MAX_RETRIES 10
  557. #define RETRY_DELAY (3 * HZ)
  558. #define INITIAL_DELAY (HZ / 2)
  559. static void fw_device_init(struct work_struct *work)
  560. {
  561. struct fw_device *device =
  562. container_of(work, struct fw_device, work.work);
  563. int minor, err;
  564. /*
  565. * All failure paths here set node->data to NULL, so that we
  566. * don't try to do device_for_each_child() on a kfree()'d
  567. * device.
  568. */
  569. if (read_bus_info_block(device, device->generation) < 0) {
  570. if (device->config_rom_retries < MAX_RETRIES &&
  571. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  572. device->config_rom_retries++;
  573. schedule_delayed_work(&device->work, RETRY_DELAY);
  574. } else {
  575. fw_notify("giving up on config rom for node id %x\n",
  576. device->node_id);
  577. if (device->node == device->card->root_node)
  578. schedule_delayed_work(&device->card->work, 0);
  579. fw_device_release(&device->device);
  580. }
  581. return;
  582. }
  583. err = -ENOMEM;
  584. fw_device_get(device);
  585. down_write(&fw_device_rwsem);
  586. if (idr_pre_get(&fw_device_idr, GFP_KERNEL))
  587. err = idr_get_new(&fw_device_idr, device, &minor);
  588. up_write(&fw_device_rwsem);
  589. if (err < 0)
  590. goto error;
  591. device->device.bus = &fw_bus_type;
  592. device->device.type = &fw_device_type;
  593. device->device.parent = device->card->device;
  594. device->device.devt = MKDEV(fw_cdev_major, minor);
  595. dev_set_name(&device->device, "fw%d", minor);
  596. init_fw_attribute_group(&device->device,
  597. fw_device_attributes,
  598. &device->attribute_group);
  599. if (device_add(&device->device)) {
  600. fw_error("Failed to add device.\n");
  601. goto error_with_cdev;
  602. }
  603. create_units(device);
  604. /*
  605. * Transition the device to running state. If it got pulled
  606. * out from under us while we did the intialization work, we
  607. * have to shut down the device again here. Normally, though,
  608. * fw_node_event will be responsible for shutting it down when
  609. * necessary. We have to use the atomic cmpxchg here to avoid
  610. * racing with the FW_NODE_DESTROYED case in
  611. * fw_node_event().
  612. */
  613. if (atomic_cmpxchg(&device->state,
  614. FW_DEVICE_INITIALIZING,
  615. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN) {
  616. fw_device_shutdown(work);
  617. } else {
  618. if (device->config_rom_retries)
  619. fw_notify("created device %s: GUID %08x%08x, S%d00, "
  620. "%d config ROM retries\n",
  621. dev_name(&device->device),
  622. device->config_rom[3], device->config_rom[4],
  623. 1 << device->max_speed,
  624. device->config_rom_retries);
  625. else
  626. fw_notify("created device %s: GUID %08x%08x, S%d00\n",
  627. dev_name(&device->device),
  628. device->config_rom[3], device->config_rom[4],
  629. 1 << device->max_speed);
  630. device->config_rom_retries = 0;
  631. }
  632. /*
  633. * Reschedule the IRM work if we just finished reading the
  634. * root node config rom. If this races with a bus reset we
  635. * just end up running the IRM work a couple of extra times -
  636. * pretty harmless.
  637. */
  638. if (device->node == device->card->root_node)
  639. schedule_delayed_work(&device->card->work, 0);
  640. return;
  641. error_with_cdev:
  642. down_write(&fw_device_rwsem);
  643. idr_remove(&fw_device_idr, minor);
  644. up_write(&fw_device_rwsem);
  645. error:
  646. fw_device_put(device); /* fw_device_idr's reference */
  647. put_device(&device->device); /* our reference */
  648. }
  649. static int update_unit(struct device *dev, void *data)
  650. {
  651. struct fw_unit *unit = fw_unit(dev);
  652. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  653. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  654. down(&dev->sem);
  655. driver->update(unit);
  656. up(&dev->sem);
  657. }
  658. return 0;
  659. }
  660. static void fw_device_update(struct work_struct *work)
  661. {
  662. struct fw_device *device =
  663. container_of(work, struct fw_device, work.work);
  664. fw_device_cdev_update(device);
  665. device_for_each_child(&device->device, NULL, update_unit);
  666. }
  667. enum {
  668. REREAD_BIB_ERROR,
  669. REREAD_BIB_GONE,
  670. REREAD_BIB_UNCHANGED,
  671. REREAD_BIB_CHANGED,
  672. };
  673. /* Reread and compare bus info block and header of root directory */
  674. static int reread_bus_info_block(struct fw_device *device, int generation)
  675. {
  676. u32 q;
  677. int i;
  678. for (i = 0; i < 6; i++) {
  679. if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
  680. return REREAD_BIB_ERROR;
  681. if (i == 0 && q == 0)
  682. return REREAD_BIB_GONE;
  683. if (i > device->config_rom_length || q != device->config_rom[i])
  684. return REREAD_BIB_CHANGED;
  685. }
  686. return REREAD_BIB_UNCHANGED;
  687. }
  688. static void fw_device_refresh(struct work_struct *work)
  689. {
  690. struct fw_device *device =
  691. container_of(work, struct fw_device, work.work);
  692. struct fw_card *card = device->card;
  693. int node_id = device->node_id;
  694. switch (reread_bus_info_block(device, device->generation)) {
  695. case REREAD_BIB_ERROR:
  696. if (device->config_rom_retries < MAX_RETRIES / 2 &&
  697. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  698. device->config_rom_retries++;
  699. schedule_delayed_work(&device->work, RETRY_DELAY / 2);
  700. return;
  701. }
  702. goto give_up;
  703. case REREAD_BIB_GONE:
  704. goto gone;
  705. case REREAD_BIB_UNCHANGED:
  706. if (atomic_cmpxchg(&device->state,
  707. FW_DEVICE_INITIALIZING,
  708. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  709. goto gone;
  710. fw_device_update(work);
  711. device->config_rom_retries = 0;
  712. goto out;
  713. case REREAD_BIB_CHANGED:
  714. break;
  715. }
  716. /*
  717. * Something changed. We keep things simple and don't investigate
  718. * further. We just destroy all previous units and create new ones.
  719. */
  720. device_for_each_child(&device->device, NULL, shutdown_unit);
  721. if (read_bus_info_block(device, device->generation) < 0) {
  722. if (device->config_rom_retries < MAX_RETRIES &&
  723. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  724. device->config_rom_retries++;
  725. schedule_delayed_work(&device->work, RETRY_DELAY);
  726. return;
  727. }
  728. goto give_up;
  729. }
  730. create_units(device);
  731. if (atomic_cmpxchg(&device->state,
  732. FW_DEVICE_INITIALIZING,
  733. FW_DEVICE_RUNNING) == FW_DEVICE_SHUTDOWN)
  734. goto gone;
  735. fw_notify("refreshed device %s\n", dev_name(&device->device));
  736. device->config_rom_retries = 0;
  737. goto out;
  738. give_up:
  739. fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
  740. gone:
  741. atomic_set(&device->state, FW_DEVICE_SHUTDOWN);
  742. fw_device_shutdown(work);
  743. out:
  744. if (node_id == card->root_node->node_id)
  745. schedule_delayed_work(&card->work, 0);
  746. }
  747. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  748. {
  749. struct fw_device *device;
  750. switch (event) {
  751. case FW_NODE_CREATED:
  752. case FW_NODE_LINK_ON:
  753. if (!node->link_on)
  754. break;
  755. create:
  756. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  757. if (device == NULL)
  758. break;
  759. /*
  760. * Do minimal intialization of the device here, the
  761. * rest will happen in fw_device_init(). We need the
  762. * card and node so we can read the config rom and we
  763. * need to do device_initialize() now so
  764. * device_for_each_child() in FW_NODE_UPDATED is
  765. * doesn't freak out.
  766. */
  767. device_initialize(&device->device);
  768. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  769. device->card = fw_card_get(card);
  770. device->node = fw_node_get(node);
  771. device->node_id = node->node_id;
  772. device->generation = card->generation;
  773. INIT_LIST_HEAD(&device->client_list);
  774. /*
  775. * Set the node data to point back to this device so
  776. * FW_NODE_UPDATED callbacks can update the node_id
  777. * and generation for the device.
  778. */
  779. node->data = device;
  780. /*
  781. * Many devices are slow to respond after bus resets,
  782. * especially if they are bus powered and go through
  783. * power-up after getting plugged in. We schedule the
  784. * first config rom scan half a second after bus reset.
  785. */
  786. INIT_DELAYED_WORK(&device->work, fw_device_init);
  787. schedule_delayed_work(&device->work, INITIAL_DELAY);
  788. break;
  789. case FW_NODE_INITIATED_RESET:
  790. device = node->data;
  791. if (device == NULL)
  792. goto create;
  793. device->node_id = node->node_id;
  794. smp_wmb(); /* update node_id before generation */
  795. device->generation = card->generation;
  796. if (atomic_cmpxchg(&device->state,
  797. FW_DEVICE_RUNNING,
  798. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  799. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  800. schedule_delayed_work(&device->work,
  801. node == card->local_node ? 0 : INITIAL_DELAY);
  802. }
  803. break;
  804. case FW_NODE_UPDATED:
  805. if (!node->link_on || node->data == NULL)
  806. break;
  807. device = node->data;
  808. device->node_id = node->node_id;
  809. smp_wmb(); /* update node_id before generation */
  810. device->generation = card->generation;
  811. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  812. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  813. schedule_delayed_work(&device->work, 0);
  814. }
  815. break;
  816. case FW_NODE_DESTROYED:
  817. case FW_NODE_LINK_OFF:
  818. if (!node->data)
  819. break;
  820. /*
  821. * Destroy the device associated with the node. There
  822. * are two cases here: either the device is fully
  823. * initialized (FW_DEVICE_RUNNING) or we're in the
  824. * process of reading its config rom
  825. * (FW_DEVICE_INITIALIZING). If it is fully
  826. * initialized we can reuse device->work to schedule a
  827. * full fw_device_shutdown(). If not, there's work
  828. * scheduled to read it's config rom, and we just put
  829. * the device in shutdown state to have that code fail
  830. * to create the device.
  831. */
  832. device = node->data;
  833. if (atomic_xchg(&device->state,
  834. FW_DEVICE_SHUTDOWN) == FW_DEVICE_RUNNING) {
  835. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  836. schedule_delayed_work(&device->work, 0);
  837. }
  838. break;
  839. }
  840. }