fw-device.c 28 KB

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