fw-device.c 30 KB

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