fw-device.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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/mutex.h>
  30. #include <linux/rwsem.h>
  31. #include <linux/semaphore.h>
  32. #include <linux/spinlock.h>
  33. #include <asm/system.h>
  34. #include <linux/ctype.h>
  35. #include "fw-transaction.h"
  36. #include "fw-topology.h"
  37. #include "fw-device.h"
  38. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 * p)
  39. {
  40. ci->p = p + 1;
  41. ci->end = ci->p + (p[0] >> 16);
  42. }
  43. EXPORT_SYMBOL(fw_csr_iterator_init);
  44. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value)
  45. {
  46. *key = *ci->p >> 24;
  47. *value = *ci->p & 0xffffff;
  48. return ci->p++ < ci->end;
  49. }
  50. EXPORT_SYMBOL(fw_csr_iterator_next);
  51. static int is_fw_unit(struct device *dev);
  52. static int match_unit_directory(u32 * directory, const struct fw_device_id *id)
  53. {
  54. struct fw_csr_iterator ci;
  55. int key, value, match;
  56. match = 0;
  57. fw_csr_iterator_init(&ci, directory);
  58. while (fw_csr_iterator_next(&ci, &key, &value)) {
  59. if (key == CSR_VENDOR && value == id->vendor)
  60. match |= FW_MATCH_VENDOR;
  61. if (key == CSR_MODEL && value == id->model)
  62. match |= FW_MATCH_MODEL;
  63. if (key == CSR_SPECIFIER_ID && value == id->specifier_id)
  64. match |= FW_MATCH_SPECIFIER_ID;
  65. if (key == CSR_VERSION && value == id->version)
  66. match |= FW_MATCH_VERSION;
  67. }
  68. return (match & id->match_flags) == id->match_flags;
  69. }
  70. static int fw_unit_match(struct device *dev, struct device_driver *drv)
  71. {
  72. struct fw_unit *unit = fw_unit(dev);
  73. struct fw_driver *driver = fw_driver(drv);
  74. int i;
  75. /* We only allow binding to fw_units. */
  76. if (!is_fw_unit(dev))
  77. return 0;
  78. for (i = 0; driver->id_table[i].match_flags != 0; i++) {
  79. if (match_unit_directory(unit->directory, &driver->id_table[i]))
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static int get_modalias(struct fw_unit *unit, char *buffer, size_t buffer_size)
  85. {
  86. struct fw_device *device = fw_device(unit->device.parent);
  87. struct fw_csr_iterator ci;
  88. int key, value;
  89. int vendor = 0;
  90. int model = 0;
  91. int specifier_id = 0;
  92. int version = 0;
  93. fw_csr_iterator_init(&ci, &device->config_rom[5]);
  94. while (fw_csr_iterator_next(&ci, &key, &value)) {
  95. switch (key) {
  96. case CSR_VENDOR:
  97. vendor = value;
  98. break;
  99. case CSR_MODEL:
  100. model = value;
  101. break;
  102. }
  103. }
  104. fw_csr_iterator_init(&ci, unit->directory);
  105. while (fw_csr_iterator_next(&ci, &key, &value)) {
  106. switch (key) {
  107. case CSR_SPECIFIER_ID:
  108. specifier_id = value;
  109. break;
  110. case CSR_VERSION:
  111. version = value;
  112. break;
  113. }
  114. }
  115. return snprintf(buffer, buffer_size,
  116. "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  117. vendor, model, specifier_id, version);
  118. }
  119. static int fw_unit_uevent(struct device *dev, struct kobj_uevent_env *env)
  120. {
  121. struct fw_unit *unit = fw_unit(dev);
  122. char modalias[64];
  123. get_modalias(unit, modalias, sizeof(modalias));
  124. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  125. return -ENOMEM;
  126. return 0;
  127. }
  128. struct bus_type fw_bus_type = {
  129. .name = "firewire",
  130. .match = fw_unit_match,
  131. };
  132. EXPORT_SYMBOL(fw_bus_type);
  133. static void fw_device_release(struct device *dev)
  134. {
  135. struct fw_device *device = fw_device(dev);
  136. struct fw_card *card = device->card;
  137. unsigned long flags;
  138. /*
  139. * Take the card lock so we don't set this to NULL while a
  140. * FW_NODE_UPDATED callback is being handled or while the
  141. * bus manager work looks at this node.
  142. */
  143. spin_lock_irqsave(&card->lock, flags);
  144. device->node->data = NULL;
  145. spin_unlock_irqrestore(&card->lock, flags);
  146. fw_node_put(device->node);
  147. kfree(device->config_rom);
  148. kfree(device);
  149. fw_card_put(card);
  150. }
  151. int fw_device_enable_phys_dma(struct fw_device *device)
  152. {
  153. int generation = device->generation;
  154. /* device->node_id, accessed below, must not be older than generation */
  155. smp_rmb();
  156. return device->card->driver->enable_phys_dma(device->card,
  157. device->node_id,
  158. generation);
  159. }
  160. EXPORT_SYMBOL(fw_device_enable_phys_dma);
  161. struct config_rom_attribute {
  162. struct device_attribute attr;
  163. u32 key;
  164. };
  165. static ssize_t show_immediate(struct device *dev,
  166. struct device_attribute *dattr, char *buf)
  167. {
  168. struct config_rom_attribute *attr =
  169. container_of(dattr, struct config_rom_attribute, attr);
  170. struct fw_csr_iterator ci;
  171. u32 *dir;
  172. int key, value, ret = -ENOENT;
  173. down_read(&fw_device_rwsem);
  174. if (is_fw_unit(dev))
  175. dir = fw_unit(dev)->directory;
  176. else
  177. dir = fw_device(dev)->config_rom + 5;
  178. fw_csr_iterator_init(&ci, dir);
  179. while (fw_csr_iterator_next(&ci, &key, &value))
  180. if (attr->key == key) {
  181. ret = snprintf(buf, buf ? PAGE_SIZE : 0,
  182. "0x%06x\n", value);
  183. break;
  184. }
  185. up_read(&fw_device_rwsem);
  186. return ret;
  187. }
  188. #define IMMEDIATE_ATTR(name, key) \
  189. { __ATTR(name, S_IRUGO, show_immediate, NULL), key }
  190. static ssize_t show_text_leaf(struct device *dev,
  191. struct device_attribute *dattr, char *buf)
  192. {
  193. struct config_rom_attribute *attr =
  194. container_of(dattr, struct config_rom_attribute, attr);
  195. struct fw_csr_iterator ci;
  196. u32 *dir, *block = NULL, *p, *end;
  197. int length, key, value, last_key = 0, ret = -ENOENT;
  198. char *b;
  199. down_read(&fw_device_rwsem);
  200. if (is_fw_unit(dev))
  201. dir = fw_unit(dev)->directory;
  202. else
  203. dir = fw_device(dev)->config_rom + 5;
  204. fw_csr_iterator_init(&ci, dir);
  205. while (fw_csr_iterator_next(&ci, &key, &value)) {
  206. if (attr->key == last_key &&
  207. key == (CSR_DESCRIPTOR | CSR_LEAF))
  208. block = ci.p - 1 + value;
  209. last_key = key;
  210. }
  211. if (block == NULL)
  212. goto out;
  213. length = min(block[0] >> 16, 256U);
  214. if (length < 3)
  215. goto out;
  216. if (block[1] != 0 || block[2] != 0)
  217. /* Unknown encoding. */
  218. goto out;
  219. if (buf == NULL) {
  220. ret = length * 4;
  221. goto out;
  222. }
  223. b = buf;
  224. end = &block[length + 1];
  225. for (p = &block[3]; p < end; p++, b += 4)
  226. * (u32 *) b = (__force u32) __cpu_to_be32(*p);
  227. /* Strip trailing whitespace and add newline. */
  228. while (b--, (isspace(*b) || *b == '\0') && b > buf);
  229. strcpy(b + 1, "\n");
  230. ret = b + 2 - buf;
  231. out:
  232. up_read(&fw_device_rwsem);
  233. return ret;
  234. }
  235. #define TEXT_LEAF_ATTR(name, key) \
  236. { __ATTR(name, S_IRUGO, show_text_leaf, NULL), key }
  237. static struct config_rom_attribute config_rom_attributes[] = {
  238. IMMEDIATE_ATTR(vendor, CSR_VENDOR),
  239. IMMEDIATE_ATTR(hardware_version, CSR_HARDWARE_VERSION),
  240. IMMEDIATE_ATTR(specifier_id, CSR_SPECIFIER_ID),
  241. IMMEDIATE_ATTR(version, CSR_VERSION),
  242. IMMEDIATE_ATTR(model, CSR_MODEL),
  243. TEXT_LEAF_ATTR(vendor_name, CSR_VENDOR),
  244. TEXT_LEAF_ATTR(model_name, CSR_MODEL),
  245. TEXT_LEAF_ATTR(hardware_version_name, CSR_HARDWARE_VERSION),
  246. };
  247. static void 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 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 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 config_rom_show(struct device *dev,
  291. 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 guid_show(struct device *dev,
  302. 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 read_rom(struct fw_device *device,
  318. 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. 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. /*
  531. * These defines control the retry behavior for reading the config
  532. * rom. It shouldn't be necessary to tweak these; if the device
  533. * doesn't respond to a config rom read within 10 seconds, it's not
  534. * going to respond at all. As for the initial delay, a lot of
  535. * devices will be able to respond within half a second after bus
  536. * reset. On the other hand, it's not really worth being more
  537. * aggressive than that, since it scales pretty well; if 10 devices
  538. * are plugged in, they're all getting read within one second.
  539. */
  540. #define MAX_RETRIES 10
  541. #define RETRY_DELAY (3 * HZ)
  542. #define INITIAL_DELAY (HZ / 2)
  543. #define SHUTDOWN_DELAY (2 * HZ)
  544. static void fw_device_shutdown(struct work_struct *work)
  545. {
  546. struct fw_device *device =
  547. container_of(work, struct fw_device, work.work);
  548. int minor = MINOR(device->device.devt);
  549. if (time_is_after_jiffies(device->card->reset_jiffies + SHUTDOWN_DELAY)
  550. && !list_empty(&device->card->link)) {
  551. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  552. return;
  553. }
  554. if (atomic_cmpxchg(&device->state,
  555. FW_DEVICE_GONE,
  556. FW_DEVICE_SHUTDOWN) != FW_DEVICE_GONE)
  557. return;
  558. fw_device_cdev_remove(device);
  559. device_for_each_child(&device->device, NULL, shutdown_unit);
  560. device_unregister(&device->device);
  561. down_write(&fw_device_rwsem);
  562. idr_remove(&fw_device_idr, minor);
  563. up_write(&fw_device_rwsem);
  564. fw_device_put(device);
  565. }
  566. static struct device_type fw_device_type = {
  567. .release = fw_device_release,
  568. };
  569. static void fw_device_update(struct work_struct *work);
  570. /*
  571. * If a device was pending for deletion because its node went away but its
  572. * bus info block and root directory header matches that of a newly discovered
  573. * device, revive the existing fw_device.
  574. * The newly allocated fw_device becomes obsolete instead.
  575. */
  576. static int lookup_existing_device(struct device *dev, void *data)
  577. {
  578. struct fw_device *old = fw_device(dev);
  579. struct fw_device *new = data;
  580. struct fw_card *card = new->card;
  581. int match = 0;
  582. down_read(&fw_device_rwsem); /* serialize config_rom access */
  583. spin_lock_irq(&card->lock); /* serialize node access */
  584. if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 &&
  585. atomic_cmpxchg(&old->state,
  586. FW_DEVICE_GONE,
  587. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  588. struct fw_node *current_node = new->node;
  589. struct fw_node *obsolete_node = old->node;
  590. new->node = obsolete_node;
  591. new->node->data = new;
  592. old->node = current_node;
  593. old->node->data = old;
  594. old->max_speed = new->max_speed;
  595. old->node_id = current_node->node_id;
  596. smp_wmb(); /* update node_id before generation */
  597. old->generation = card->generation;
  598. old->config_rom_retries = 0;
  599. fw_notify("rediscovered device %s\n", dev_name(dev));
  600. PREPARE_DELAYED_WORK(&old->work, fw_device_update);
  601. schedule_delayed_work(&old->work, 0);
  602. if (current_node == card->root_node)
  603. fw_schedule_bm_work(card, 0);
  604. match = 1;
  605. }
  606. spin_unlock_irq(&card->lock);
  607. up_read(&fw_device_rwsem);
  608. return match;
  609. }
  610. static void fw_device_init(struct work_struct *work)
  611. {
  612. struct fw_device *device =
  613. container_of(work, struct fw_device, work.work);
  614. struct device *revived_dev;
  615. int minor, err;
  616. /*
  617. * All failure paths here set node->data to NULL, so that we
  618. * don't try to do device_for_each_child() on a kfree()'d
  619. * device.
  620. */
  621. if (read_bus_info_block(device, device->generation) < 0) {
  622. if (device->config_rom_retries < MAX_RETRIES &&
  623. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  624. device->config_rom_retries++;
  625. schedule_delayed_work(&device->work, RETRY_DELAY);
  626. } else {
  627. fw_notify("giving up on config rom for node id %x\n",
  628. device->node_id);
  629. if (device->node == device->card->root_node)
  630. fw_schedule_bm_work(device->card, 0);
  631. fw_device_release(&device->device);
  632. }
  633. return;
  634. }
  635. revived_dev = device_find_child(device->card->device,
  636. device, lookup_existing_device);
  637. if (revived_dev) {
  638. put_device(revived_dev);
  639. fw_device_release(&device->device);
  640. return;
  641. }
  642. device_initialize(&device->device);
  643. fw_device_get(device);
  644. down_write(&fw_device_rwsem);
  645. err = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
  646. idr_get_new(&fw_device_idr, device, &minor) :
  647. -ENOMEM;
  648. up_write(&fw_device_rwsem);
  649. if (err < 0)
  650. goto error;
  651. device->device.bus = &fw_bus_type;
  652. device->device.type = &fw_device_type;
  653. device->device.parent = device->card->device;
  654. device->device.devt = MKDEV(fw_cdev_major, minor);
  655. dev_set_name(&device->device, "fw%d", minor);
  656. init_fw_attribute_group(&device->device,
  657. fw_device_attributes,
  658. &device->attribute_group);
  659. if (device_add(&device->device)) {
  660. fw_error("Failed to add device.\n");
  661. goto error_with_cdev;
  662. }
  663. create_units(device);
  664. /*
  665. * Transition the device to running state. If it got pulled
  666. * out from under us while we did the intialization work, we
  667. * have to shut down the device again here. Normally, though,
  668. * fw_node_event will be responsible for shutting it down when
  669. * necessary. We have to use the atomic cmpxchg here to avoid
  670. * racing with the FW_NODE_DESTROYED case in
  671. * fw_node_event().
  672. */
  673. if (atomic_cmpxchg(&device->state,
  674. FW_DEVICE_INITIALIZING,
  675. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  676. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  677. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  678. } else {
  679. if (device->config_rom_retries)
  680. fw_notify("created device %s: GUID %08x%08x, S%d00, "
  681. "%d config ROM retries\n",
  682. dev_name(&device->device),
  683. device->config_rom[3], device->config_rom[4],
  684. 1 << device->max_speed,
  685. device->config_rom_retries);
  686. else
  687. fw_notify("created device %s: GUID %08x%08x, S%d00\n",
  688. dev_name(&device->device),
  689. device->config_rom[3], device->config_rom[4],
  690. 1 << device->max_speed);
  691. device->config_rom_retries = 0;
  692. }
  693. /*
  694. * Reschedule the IRM work if we just finished reading the
  695. * root node config rom. If this races with a bus reset we
  696. * just end up running the IRM work a couple of extra times -
  697. * pretty harmless.
  698. */
  699. if (device->node == device->card->root_node)
  700. fw_schedule_bm_work(device->card, 0);
  701. return;
  702. error_with_cdev:
  703. down_write(&fw_device_rwsem);
  704. idr_remove(&fw_device_idr, minor);
  705. up_write(&fw_device_rwsem);
  706. error:
  707. fw_device_put(device); /* fw_device_idr's reference */
  708. put_device(&device->device); /* our reference */
  709. }
  710. static int update_unit(struct device *dev, void *data)
  711. {
  712. struct fw_unit *unit = fw_unit(dev);
  713. struct fw_driver *driver = (struct fw_driver *)dev->driver;
  714. if (is_fw_unit(dev) && driver != NULL && driver->update != NULL) {
  715. down(&dev->sem);
  716. driver->update(unit);
  717. up(&dev->sem);
  718. }
  719. return 0;
  720. }
  721. static void fw_device_update(struct work_struct *work)
  722. {
  723. struct fw_device *device =
  724. container_of(work, struct fw_device, work.work);
  725. fw_device_cdev_update(device);
  726. device_for_each_child(&device->device, NULL, update_unit);
  727. }
  728. enum {
  729. REREAD_BIB_ERROR,
  730. REREAD_BIB_GONE,
  731. REREAD_BIB_UNCHANGED,
  732. REREAD_BIB_CHANGED,
  733. };
  734. /* Reread and compare bus info block and header of root directory */
  735. static int reread_bus_info_block(struct fw_device *device, int generation)
  736. {
  737. u32 q;
  738. int i;
  739. for (i = 0; i < 6; i++) {
  740. if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
  741. return REREAD_BIB_ERROR;
  742. if (i == 0 && q == 0)
  743. return REREAD_BIB_GONE;
  744. if (i > device->config_rom_length || q != device->config_rom[i])
  745. return REREAD_BIB_CHANGED;
  746. }
  747. return REREAD_BIB_UNCHANGED;
  748. }
  749. static void fw_device_refresh(struct work_struct *work)
  750. {
  751. struct fw_device *device =
  752. container_of(work, struct fw_device, work.work);
  753. struct fw_card *card = device->card;
  754. int node_id = device->node_id;
  755. switch (reread_bus_info_block(device, device->generation)) {
  756. case REREAD_BIB_ERROR:
  757. if (device->config_rom_retries < MAX_RETRIES / 2 &&
  758. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  759. device->config_rom_retries++;
  760. schedule_delayed_work(&device->work, RETRY_DELAY / 2);
  761. return;
  762. }
  763. goto give_up;
  764. case REREAD_BIB_GONE:
  765. goto gone;
  766. case REREAD_BIB_UNCHANGED:
  767. if (atomic_cmpxchg(&device->state,
  768. FW_DEVICE_INITIALIZING,
  769. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  770. goto gone;
  771. fw_device_update(work);
  772. device->config_rom_retries = 0;
  773. goto out;
  774. case REREAD_BIB_CHANGED:
  775. break;
  776. }
  777. /*
  778. * Something changed. We keep things simple and don't investigate
  779. * further. We just destroy all previous units and create new ones.
  780. */
  781. device_for_each_child(&device->device, NULL, shutdown_unit);
  782. if (read_bus_info_block(device, device->generation) < 0) {
  783. if (device->config_rom_retries < MAX_RETRIES &&
  784. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  785. device->config_rom_retries++;
  786. schedule_delayed_work(&device->work, RETRY_DELAY);
  787. return;
  788. }
  789. goto give_up;
  790. }
  791. create_units(device);
  792. if (atomic_cmpxchg(&device->state,
  793. FW_DEVICE_INITIALIZING,
  794. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  795. goto gone;
  796. fw_notify("refreshed device %s\n", dev_name(&device->device));
  797. device->config_rom_retries = 0;
  798. goto out;
  799. give_up:
  800. fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
  801. gone:
  802. atomic_set(&device->state, FW_DEVICE_GONE);
  803. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  804. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  805. out:
  806. if (node_id == card->root_node->node_id)
  807. fw_schedule_bm_work(card, 0);
  808. }
  809. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  810. {
  811. struct fw_device *device;
  812. switch (event) {
  813. case FW_NODE_CREATED:
  814. case FW_NODE_LINK_ON:
  815. if (!node->link_on)
  816. break;
  817. create:
  818. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  819. if (device == NULL)
  820. break;
  821. /*
  822. * Do minimal intialization of the device here, the
  823. * rest will happen in fw_device_init().
  824. *
  825. * Attention: A lot of things, even fw_device_get(),
  826. * cannot be done before fw_device_init() finished!
  827. * You can basically just check device->state and
  828. * schedule work until then, but only while holding
  829. * card->lock.
  830. */
  831. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  832. device->card = fw_card_get(card);
  833. device->node = fw_node_get(node);
  834. device->node_id = node->node_id;
  835. device->generation = card->generation;
  836. mutex_init(&device->client_list_mutex);
  837. INIT_LIST_HEAD(&device->client_list);
  838. /*
  839. * Set the node data to point back to this device so
  840. * FW_NODE_UPDATED callbacks can update the node_id
  841. * and generation for the device.
  842. */
  843. node->data = device;
  844. /*
  845. * Many devices are slow to respond after bus resets,
  846. * especially if they are bus powered and go through
  847. * power-up after getting plugged in. We schedule the
  848. * first config rom scan half a second after bus reset.
  849. */
  850. INIT_DELAYED_WORK(&device->work, fw_device_init);
  851. schedule_delayed_work(&device->work, INITIAL_DELAY);
  852. break;
  853. case FW_NODE_INITIATED_RESET:
  854. device = node->data;
  855. if (device == NULL)
  856. goto create;
  857. device->node_id = node->node_id;
  858. smp_wmb(); /* update node_id before generation */
  859. device->generation = card->generation;
  860. if (atomic_cmpxchg(&device->state,
  861. FW_DEVICE_RUNNING,
  862. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  863. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  864. schedule_delayed_work(&device->work,
  865. node == card->local_node ? 0 : INITIAL_DELAY);
  866. }
  867. break;
  868. case FW_NODE_UPDATED:
  869. if (!node->link_on || node->data == NULL)
  870. break;
  871. device = node->data;
  872. device->node_id = node->node_id;
  873. smp_wmb(); /* update node_id before generation */
  874. device->generation = card->generation;
  875. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  876. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  877. schedule_delayed_work(&device->work, 0);
  878. }
  879. break;
  880. case FW_NODE_DESTROYED:
  881. case FW_NODE_LINK_OFF:
  882. if (!node->data)
  883. break;
  884. /*
  885. * Destroy the device associated with the node. There
  886. * are two cases here: either the device is fully
  887. * initialized (FW_DEVICE_RUNNING) or we're in the
  888. * process of reading its config rom
  889. * (FW_DEVICE_INITIALIZING). If it is fully
  890. * initialized we can reuse device->work to schedule a
  891. * full fw_device_shutdown(). If not, there's work
  892. * scheduled to read it's config rom, and we just put
  893. * the device in shutdown state to have that code fail
  894. * to create the device.
  895. */
  896. device = node->data;
  897. if (atomic_xchg(&device->state,
  898. FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
  899. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  900. schedule_delayed_work(&device->work,
  901. list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
  902. }
  903. break;
  904. }
  905. }