fw-device.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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] >> 30 & 1;
  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. enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, };
  628. void fw_device_set_broadcast_channel(struct fw_device *device, int generation)
  629. {
  630. struct fw_card *card = device->card;
  631. __be32 data;
  632. int rcode;
  633. if (!card->broadcast_channel_allocated)
  634. return;
  635. if (device->bc_implemented == BC_UNKNOWN) {
  636. rcode = fw_run_transaction(card, TCODE_READ_QUADLET_REQUEST,
  637. device->node_id, generation, device->max_speed,
  638. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  639. &data, 4);
  640. switch (rcode) {
  641. case RCODE_COMPLETE:
  642. if (data & cpu_to_be32(1 << 31)) {
  643. device->bc_implemented = BC_IMPLEMENTED;
  644. break;
  645. }
  646. /* else fall through to case address error */
  647. case RCODE_ADDRESS_ERROR:
  648. device->bc_implemented = BC_UNIMPLEMENTED;
  649. }
  650. }
  651. if (device->bc_implemented == BC_IMPLEMENTED) {
  652. data = cpu_to_be32(BROADCAST_CHANNEL_INITIAL |
  653. BROADCAST_CHANNEL_VALID);
  654. fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
  655. device->node_id, generation, device->max_speed,
  656. CSR_REGISTER_BASE + CSR_BROADCAST_CHANNEL,
  657. &data, 4);
  658. }
  659. }
  660. static void fw_device_init(struct work_struct *work)
  661. {
  662. struct fw_device *device =
  663. container_of(work, struct fw_device, work.work);
  664. struct device *revived_dev;
  665. int minor, ret;
  666. /*
  667. * All failure paths here set node->data to NULL, so that we
  668. * don't try to do device_for_each_child() on a kfree()'d
  669. * device.
  670. */
  671. if (read_bus_info_block(device, device->generation) < 0) {
  672. if (device->config_rom_retries < MAX_RETRIES &&
  673. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  674. device->config_rom_retries++;
  675. schedule_delayed_work(&device->work, RETRY_DELAY);
  676. } else {
  677. fw_notify("giving up on config rom for node id %x\n",
  678. device->node_id);
  679. if (device->node == device->card->root_node)
  680. fw_schedule_bm_work(device->card, 0);
  681. fw_device_release(&device->device);
  682. }
  683. return;
  684. }
  685. revived_dev = device_find_child(device->card->device,
  686. device, lookup_existing_device);
  687. if (revived_dev) {
  688. put_device(revived_dev);
  689. fw_device_release(&device->device);
  690. return;
  691. }
  692. device_initialize(&device->device);
  693. fw_device_get(device);
  694. down_write(&fw_device_rwsem);
  695. ret = idr_pre_get(&fw_device_idr, GFP_KERNEL) ?
  696. idr_get_new(&fw_device_idr, device, &minor) :
  697. -ENOMEM;
  698. up_write(&fw_device_rwsem);
  699. if (ret < 0)
  700. goto error;
  701. device->device.bus = &fw_bus_type;
  702. device->device.type = &fw_device_type;
  703. device->device.parent = device->card->device;
  704. device->device.devt = MKDEV(fw_cdev_major, minor);
  705. dev_set_name(&device->device, "fw%d", minor);
  706. init_fw_attribute_group(&device->device,
  707. fw_device_attributes,
  708. &device->attribute_group);
  709. if (device_add(&device->device)) {
  710. fw_error("Failed to add device.\n");
  711. goto error_with_cdev;
  712. }
  713. create_units(device);
  714. /*
  715. * Transition the device to running state. If it got pulled
  716. * out from under us while we did the intialization work, we
  717. * have to shut down the device again here. Normally, though,
  718. * fw_node_event will be responsible for shutting it down when
  719. * necessary. We have to use the atomic cmpxchg here to avoid
  720. * racing with the FW_NODE_DESTROYED case in
  721. * fw_node_event().
  722. */
  723. if (atomic_cmpxchg(&device->state,
  724. FW_DEVICE_INITIALIZING,
  725. FW_DEVICE_RUNNING) == FW_DEVICE_GONE) {
  726. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  727. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  728. } else {
  729. if (device->config_rom_retries)
  730. fw_notify("created device %s: GUID %08x%08x, S%d00, "
  731. "%d config ROM retries\n",
  732. dev_name(&device->device),
  733. device->config_rom[3], device->config_rom[4],
  734. 1 << device->max_speed,
  735. device->config_rom_retries);
  736. else
  737. fw_notify("created device %s: GUID %08x%08x, S%d00\n",
  738. dev_name(&device->device),
  739. device->config_rom[3], device->config_rom[4],
  740. 1 << device->max_speed);
  741. device->config_rom_retries = 0;
  742. fw_device_set_broadcast_channel(device, device->generation);
  743. }
  744. /*
  745. * Reschedule the IRM work if we just finished reading the
  746. * root node config rom. If this races with a bus reset we
  747. * just end up running the IRM work a couple of extra times -
  748. * pretty harmless.
  749. */
  750. if (device->node == device->card->root_node)
  751. fw_schedule_bm_work(device->card, 0);
  752. return;
  753. error_with_cdev:
  754. down_write(&fw_device_rwsem);
  755. idr_remove(&fw_device_idr, minor);
  756. up_write(&fw_device_rwsem);
  757. error:
  758. fw_device_put(device); /* fw_device_idr's reference */
  759. put_device(&device->device); /* our reference */
  760. }
  761. enum {
  762. REREAD_BIB_ERROR,
  763. REREAD_BIB_GONE,
  764. REREAD_BIB_UNCHANGED,
  765. REREAD_BIB_CHANGED,
  766. };
  767. /* Reread and compare bus info block and header of root directory */
  768. static int reread_bus_info_block(struct fw_device *device, int generation)
  769. {
  770. u32 q;
  771. int i;
  772. for (i = 0; i < 6; i++) {
  773. if (read_rom(device, generation, i, &q) != RCODE_COMPLETE)
  774. return REREAD_BIB_ERROR;
  775. if (i == 0 && q == 0)
  776. return REREAD_BIB_GONE;
  777. if (q != device->config_rom[i])
  778. return REREAD_BIB_CHANGED;
  779. }
  780. return REREAD_BIB_UNCHANGED;
  781. }
  782. static void fw_device_refresh(struct work_struct *work)
  783. {
  784. struct fw_device *device =
  785. container_of(work, struct fw_device, work.work);
  786. struct fw_card *card = device->card;
  787. int node_id = device->node_id;
  788. switch (reread_bus_info_block(device, device->generation)) {
  789. case REREAD_BIB_ERROR:
  790. if (device->config_rom_retries < MAX_RETRIES / 2 &&
  791. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  792. device->config_rom_retries++;
  793. schedule_delayed_work(&device->work, RETRY_DELAY / 2);
  794. return;
  795. }
  796. goto give_up;
  797. case REREAD_BIB_GONE:
  798. goto gone;
  799. case REREAD_BIB_UNCHANGED:
  800. if (atomic_cmpxchg(&device->state,
  801. FW_DEVICE_INITIALIZING,
  802. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  803. goto gone;
  804. fw_device_update(work);
  805. device->config_rom_retries = 0;
  806. goto out;
  807. case REREAD_BIB_CHANGED:
  808. break;
  809. }
  810. /*
  811. * Something changed. We keep things simple and don't investigate
  812. * further. We just destroy all previous units and create new ones.
  813. */
  814. device_for_each_child(&device->device, NULL, shutdown_unit);
  815. if (read_bus_info_block(device, device->generation) < 0) {
  816. if (device->config_rom_retries < MAX_RETRIES &&
  817. atomic_read(&device->state) == FW_DEVICE_INITIALIZING) {
  818. device->config_rom_retries++;
  819. schedule_delayed_work(&device->work, RETRY_DELAY);
  820. return;
  821. }
  822. goto give_up;
  823. }
  824. create_units(device);
  825. if (atomic_cmpxchg(&device->state,
  826. FW_DEVICE_INITIALIZING,
  827. FW_DEVICE_RUNNING) == FW_DEVICE_GONE)
  828. goto gone;
  829. fw_notify("refreshed device %s\n", dev_name(&device->device));
  830. device->config_rom_retries = 0;
  831. goto out;
  832. give_up:
  833. fw_notify("giving up on refresh of device %s\n", dev_name(&device->device));
  834. gone:
  835. atomic_set(&device->state, FW_DEVICE_GONE);
  836. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  837. schedule_delayed_work(&device->work, SHUTDOWN_DELAY);
  838. out:
  839. if (node_id == card->root_node->node_id)
  840. fw_schedule_bm_work(card, 0);
  841. }
  842. void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
  843. {
  844. struct fw_device *device;
  845. switch (event) {
  846. case FW_NODE_CREATED:
  847. case FW_NODE_LINK_ON:
  848. if (!node->link_on)
  849. break;
  850. create:
  851. device = kzalloc(sizeof(*device), GFP_ATOMIC);
  852. if (device == NULL)
  853. break;
  854. /*
  855. * Do minimal intialization of the device here, the
  856. * rest will happen in fw_device_init().
  857. *
  858. * Attention: A lot of things, even fw_device_get(),
  859. * cannot be done before fw_device_init() finished!
  860. * You can basically just check device->state and
  861. * schedule work until then, but only while holding
  862. * card->lock.
  863. */
  864. atomic_set(&device->state, FW_DEVICE_INITIALIZING);
  865. device->card = fw_card_get(card);
  866. device->node = fw_node_get(node);
  867. device->node_id = node->node_id;
  868. device->generation = card->generation;
  869. mutex_init(&device->client_list_mutex);
  870. INIT_LIST_HEAD(&device->client_list);
  871. /*
  872. * Set the node data to point back to this device so
  873. * FW_NODE_UPDATED callbacks can update the node_id
  874. * and generation for the device.
  875. */
  876. node->data = device;
  877. /*
  878. * Many devices are slow to respond after bus resets,
  879. * especially if they are bus powered and go through
  880. * power-up after getting plugged in. We schedule the
  881. * first config rom scan half a second after bus reset.
  882. */
  883. INIT_DELAYED_WORK(&device->work, fw_device_init);
  884. schedule_delayed_work(&device->work, INITIAL_DELAY);
  885. break;
  886. case FW_NODE_INITIATED_RESET:
  887. device = node->data;
  888. if (device == NULL)
  889. goto create;
  890. device->node_id = node->node_id;
  891. smp_wmb(); /* update node_id before generation */
  892. device->generation = card->generation;
  893. if (atomic_cmpxchg(&device->state,
  894. FW_DEVICE_RUNNING,
  895. FW_DEVICE_INITIALIZING) == FW_DEVICE_RUNNING) {
  896. PREPARE_DELAYED_WORK(&device->work, fw_device_refresh);
  897. schedule_delayed_work(&device->work,
  898. node == card->local_node ? 0 : INITIAL_DELAY);
  899. }
  900. break;
  901. case FW_NODE_UPDATED:
  902. if (!node->link_on || node->data == NULL)
  903. break;
  904. device = node->data;
  905. device->node_id = node->node_id;
  906. smp_wmb(); /* update node_id before generation */
  907. device->generation = card->generation;
  908. if (atomic_read(&device->state) == FW_DEVICE_RUNNING) {
  909. PREPARE_DELAYED_WORK(&device->work, fw_device_update);
  910. schedule_delayed_work(&device->work, 0);
  911. }
  912. break;
  913. case FW_NODE_DESTROYED:
  914. case FW_NODE_LINK_OFF:
  915. if (!node->data)
  916. break;
  917. /*
  918. * Destroy the device associated with the node. There
  919. * are two cases here: either the device is fully
  920. * initialized (FW_DEVICE_RUNNING) or we're in the
  921. * process of reading its config rom
  922. * (FW_DEVICE_INITIALIZING). If it is fully
  923. * initialized we can reuse device->work to schedule a
  924. * full fw_device_shutdown(). If not, there's work
  925. * scheduled to read it's config rom, and we just put
  926. * the device in shutdown state to have that code fail
  927. * to create the device.
  928. */
  929. device = node->data;
  930. if (atomic_xchg(&device->state,
  931. FW_DEVICE_GONE) == FW_DEVICE_RUNNING) {
  932. PREPARE_DELAYED_WORK(&device->work, fw_device_shutdown);
  933. schedule_delayed_work(&device->work,
  934. list_empty(&card->link) ? 0 : SHUTDOWN_DELAY);
  935. }
  936. break;
  937. }
  938. }