core-device.c 34 KB

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