core-device.c 33 KB

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