core-device.c 33 KB

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