PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/firewire/fw-card.c

https://bitbucket.org/androidarmv6/android_kernel_semc_msm7x27
C | 547 lines | 353 code | 86 blank | 108 comment | 41 complexity | 2f4975fbc647663bba00ae13033d2683 MD5 | raw file
  1. /*
  2. * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/completion.h>
  19. #include <linux/crc-itu-t.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/errno.h>
  23. #include <linux/kref.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include "fw-transaction.h"
  27. #include "fw-topology.h"
  28. #include "fw-device.h"
  29. int fw_compute_block_crc(u32 *block)
  30. {
  31. __be32 be32_block[256];
  32. int i, length;
  33. length = (*block >> 16) & 0xff;
  34. for (i = 0; i < length; i++)
  35. be32_block[i] = cpu_to_be32(block[i + 1]);
  36. *block |= crc_itu_t(0, (u8 *) be32_block, length * 4);
  37. return length;
  38. }
  39. static DEFINE_MUTEX(card_mutex);
  40. static LIST_HEAD(card_list);
  41. static LIST_HEAD(descriptor_list);
  42. static int descriptor_count;
  43. #define BIB_CRC(v) ((v) << 0)
  44. #define BIB_CRC_LENGTH(v) ((v) << 16)
  45. #define BIB_INFO_LENGTH(v) ((v) << 24)
  46. #define BIB_LINK_SPEED(v) ((v) << 0)
  47. #define BIB_GENERATION(v) ((v) << 4)
  48. #define BIB_MAX_ROM(v) ((v) << 8)
  49. #define BIB_MAX_RECEIVE(v) ((v) << 12)
  50. #define BIB_CYC_CLK_ACC(v) ((v) << 16)
  51. #define BIB_PMC ((1) << 27)
  52. #define BIB_BMC ((1) << 28)
  53. #define BIB_ISC ((1) << 29)
  54. #define BIB_CMC ((1) << 30)
  55. #define BIB_IMC ((1) << 31)
  56. static u32 *
  57. generate_config_rom(struct fw_card *card, size_t *config_rom_length)
  58. {
  59. struct fw_descriptor *desc;
  60. static u32 config_rom[256];
  61. int i, j, length;
  62. /*
  63. * Initialize contents of config rom buffer. On the OHCI
  64. * controller, block reads to the config rom accesses the host
  65. * memory, but quadlet read access the hardware bus info block
  66. * registers. That's just crack, but it means we should make
  67. * sure the contents of bus info block in host memory matches
  68. * the version stored in the OHCI registers.
  69. */
  70. memset(config_rom, 0, sizeof(config_rom));
  71. config_rom[0] = BIB_CRC_LENGTH(4) | BIB_INFO_LENGTH(4) | BIB_CRC(0);
  72. config_rom[1] = 0x31333934;
  73. config_rom[2] =
  74. BIB_LINK_SPEED(card->link_speed) |
  75. BIB_GENERATION(card->config_rom_generation++ % 14 + 2) |
  76. BIB_MAX_ROM(2) |
  77. BIB_MAX_RECEIVE(card->max_receive) |
  78. BIB_BMC | BIB_ISC | BIB_CMC | BIB_IMC;
  79. config_rom[3] = card->guid >> 32;
  80. config_rom[4] = card->guid;
  81. /* Generate root directory. */
  82. i = 5;
  83. config_rom[i++] = 0;
  84. config_rom[i++] = 0x0c0083c0; /* node capabilities */
  85. j = i + descriptor_count;
  86. /* Generate root directory entries for descriptors. */
  87. list_for_each_entry (desc, &descriptor_list, link) {
  88. if (desc->immediate > 0)
  89. config_rom[i++] = desc->immediate;
  90. config_rom[i] = desc->key | (j - i);
  91. i++;
  92. j += desc->length;
  93. }
  94. /* Update root directory length. */
  95. config_rom[5] = (i - 5 - 1) << 16;
  96. /* End of root directory, now copy in descriptors. */
  97. list_for_each_entry (desc, &descriptor_list, link) {
  98. memcpy(&config_rom[i], desc->data, desc->length * 4);
  99. i += desc->length;
  100. }
  101. /* Calculate CRCs for all blocks in the config rom. This
  102. * assumes that CRC length and info length are identical for
  103. * the bus info block, which is always the case for this
  104. * implementation. */
  105. for (i = 0; i < j; i += length + 1)
  106. length = fw_compute_block_crc(config_rom + i);
  107. *config_rom_length = j;
  108. return config_rom;
  109. }
  110. static void
  111. update_config_roms(void)
  112. {
  113. struct fw_card *card;
  114. u32 *config_rom;
  115. size_t length;
  116. list_for_each_entry (card, &card_list, link) {
  117. config_rom = generate_config_rom(card, &length);
  118. card->driver->set_config_rom(card, config_rom, length);
  119. }
  120. }
  121. int
  122. fw_core_add_descriptor(struct fw_descriptor *desc)
  123. {
  124. size_t i;
  125. /*
  126. * Check descriptor is valid; the length of all blocks in the
  127. * descriptor has to add up to exactly the length of the
  128. * block.
  129. */
  130. i = 0;
  131. while (i < desc->length)
  132. i += (desc->data[i] >> 16) + 1;
  133. if (i != desc->length)
  134. return -EINVAL;
  135. mutex_lock(&card_mutex);
  136. list_add_tail(&desc->link, &descriptor_list);
  137. descriptor_count++;
  138. if (desc->immediate > 0)
  139. descriptor_count++;
  140. update_config_roms();
  141. mutex_unlock(&card_mutex);
  142. return 0;
  143. }
  144. void
  145. fw_core_remove_descriptor(struct fw_descriptor *desc)
  146. {
  147. mutex_lock(&card_mutex);
  148. list_del(&desc->link);
  149. descriptor_count--;
  150. if (desc->immediate > 0)
  151. descriptor_count--;
  152. update_config_roms();
  153. mutex_unlock(&card_mutex);
  154. }
  155. static const char gap_count_table[] = {
  156. 63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
  157. };
  158. void
  159. fw_schedule_bm_work(struct fw_card *card, unsigned long delay)
  160. {
  161. int scheduled;
  162. fw_card_get(card);
  163. scheduled = schedule_delayed_work(&card->work, delay);
  164. if (!scheduled)
  165. fw_card_put(card);
  166. }
  167. static void
  168. fw_card_bm_work(struct work_struct *work)
  169. {
  170. struct fw_card *card = container_of(work, struct fw_card, work.work);
  171. struct fw_device *root_device;
  172. struct fw_node *root_node, *local_node;
  173. unsigned long flags;
  174. int root_id, new_root_id, irm_id, gap_count, generation, grace, rcode;
  175. bool do_reset = false;
  176. bool root_device_is_running;
  177. bool root_device_is_cmc;
  178. __be32 lock_data[2];
  179. spin_lock_irqsave(&card->lock, flags);
  180. local_node = card->local_node;
  181. root_node = card->root_node;
  182. if (local_node == NULL) {
  183. spin_unlock_irqrestore(&card->lock, flags);
  184. goto out_put_card;
  185. }
  186. fw_node_get(local_node);
  187. fw_node_get(root_node);
  188. generation = card->generation;
  189. root_device = root_node->data;
  190. root_device_is_running = root_device &&
  191. atomic_read(&root_device->state) == FW_DEVICE_RUNNING;
  192. root_device_is_cmc = root_device && root_device->cmc;
  193. root_id = root_node->node_id;
  194. grace = time_after(jiffies, card->reset_jiffies + DIV_ROUND_UP(HZ, 10));
  195. if (is_next_generation(generation, card->bm_generation) ||
  196. (card->bm_generation != generation && grace)) {
  197. /*
  198. * This first step is to figure out who is IRM and
  199. * then try to become bus manager. If the IRM is not
  200. * well defined (e.g. does not have an active link
  201. * layer or does not responds to our lock request, we
  202. * will have to do a little vigilante bus management.
  203. * In that case, we do a goto into the gap count logic
  204. * so that when we do the reset, we still optimize the
  205. * gap count. That could well save a reset in the
  206. * next generation.
  207. */
  208. irm_id = card->irm_node->node_id;
  209. if (!card->irm_node->link_on) {
  210. new_root_id = local_node->node_id;
  211. fw_notify("IRM has link off, making local node (%02x) root.\n",
  212. new_root_id);
  213. goto pick_me;
  214. }
  215. lock_data[0] = cpu_to_be32(0x3f);
  216. lock_data[1] = cpu_to_be32(local_node->node_id);
  217. spin_unlock_irqrestore(&card->lock, flags);
  218. rcode = fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP,
  219. irm_id, generation, SCODE_100,
  220. CSR_REGISTER_BASE + CSR_BUS_MANAGER_ID,
  221. lock_data, sizeof(lock_data));
  222. if (rcode == RCODE_GENERATION)
  223. /* Another bus reset, BM work has been rescheduled. */
  224. goto out;
  225. if (rcode == RCODE_COMPLETE &&
  226. lock_data[0] != cpu_to_be32(0x3f))
  227. /* Somebody else is BM, let them do the work. */
  228. goto out;
  229. spin_lock_irqsave(&card->lock, flags);
  230. if (rcode != RCODE_COMPLETE) {
  231. /*
  232. * The lock request failed, maybe the IRM
  233. * isn't really IRM capable after all. Let's
  234. * do a bus reset and pick the local node as
  235. * root, and thus, IRM.
  236. */
  237. new_root_id = local_node->node_id;
  238. fw_notify("BM lock failed, making local node (%02x) root.\n",
  239. new_root_id);
  240. goto pick_me;
  241. }
  242. } else if (card->bm_generation != generation) {
  243. /*
  244. * OK, we weren't BM in the last generation, and it's
  245. * less than 100ms since last bus reset. Reschedule
  246. * this task 100ms from now.
  247. */
  248. spin_unlock_irqrestore(&card->lock, flags);
  249. fw_schedule_bm_work(card, DIV_ROUND_UP(HZ, 10));
  250. goto out;
  251. }
  252. /*
  253. * We're bus manager for this generation, so next step is to
  254. * make sure we have an active cycle master and do gap count
  255. * optimization.
  256. */
  257. card->bm_generation = generation;
  258. if (root_device == NULL) {
  259. /*
  260. * Either link_on is false, or we failed to read the
  261. * config rom. In either case, pick another root.
  262. */
  263. new_root_id = local_node->node_id;
  264. } else if (!root_device_is_running) {
  265. /*
  266. * If we haven't probed this device yet, bail out now
  267. * and let's try again once that's done.
  268. */
  269. spin_unlock_irqrestore(&card->lock, flags);
  270. goto out;
  271. } else if (root_device_is_cmc) {
  272. /*
  273. * FIXME: I suppose we should set the cmstr bit in the
  274. * STATE_CLEAR register of this node, as described in
  275. * 1394-1995, 8.4.2.6. Also, send out a force root
  276. * packet for this node.
  277. */
  278. new_root_id = root_id;
  279. } else {
  280. /*
  281. * Current root has an active link layer and we
  282. * successfully read the config rom, but it's not
  283. * cycle master capable.
  284. */
  285. new_root_id = local_node->node_id;
  286. }
  287. pick_me:
  288. /*
  289. * Pick a gap count from 1394a table E-1. The table doesn't cover
  290. * the typically much larger 1394b beta repeater delays though.
  291. */
  292. if (!card->beta_repeaters_present &&
  293. root_node->max_hops < ARRAY_SIZE(gap_count_table))
  294. gap_count = gap_count_table[root_node->max_hops];
  295. else
  296. gap_count = 63;
  297. /*
  298. * Finally, figure out if we should do a reset or not. If we have
  299. * done less than 5 resets with the same physical topology and we
  300. * have either a new root or a new gap count setting, let's do it.
  301. */
  302. if (card->bm_retries++ < 5 &&
  303. (card->gap_count != gap_count || new_root_id != root_id))
  304. do_reset = true;
  305. spin_unlock_irqrestore(&card->lock, flags);
  306. if (do_reset) {
  307. fw_notify("phy config: card %d, new root=%x, gap_count=%d\n",
  308. card->index, new_root_id, gap_count);
  309. fw_send_phy_config(card, new_root_id, generation, gap_count);
  310. fw_core_initiate_bus_reset(card, 1);
  311. }
  312. out:
  313. fw_node_put(root_node);
  314. fw_node_put(local_node);
  315. out_put_card:
  316. fw_card_put(card);
  317. }
  318. static void
  319. flush_timer_callback(unsigned long data)
  320. {
  321. struct fw_card *card = (struct fw_card *)data;
  322. fw_flush_transactions(card);
  323. }
  324. void
  325. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  326. struct device *device)
  327. {
  328. static atomic_t index = ATOMIC_INIT(-1);
  329. card->index = atomic_inc_return(&index);
  330. card->driver = driver;
  331. card->device = device;
  332. card->current_tlabel = 0;
  333. card->tlabel_mask = 0;
  334. card->color = 0;
  335. card->broadcast_channel = BROADCAST_CHANNEL_INITIAL;
  336. kref_init(&card->kref);
  337. init_completion(&card->done);
  338. INIT_LIST_HEAD(&card->transaction_list);
  339. spin_lock_init(&card->lock);
  340. setup_timer(&card->flush_timer,
  341. flush_timer_callback, (unsigned long)card);
  342. card->local_node = NULL;
  343. INIT_DELAYED_WORK(&card->work, fw_card_bm_work);
  344. }
  345. EXPORT_SYMBOL(fw_card_initialize);
  346. int
  347. fw_card_add(struct fw_card *card,
  348. u32 max_receive, u32 link_speed, u64 guid)
  349. {
  350. u32 *config_rom;
  351. size_t length;
  352. int err;
  353. card->max_receive = max_receive;
  354. card->link_speed = link_speed;
  355. card->guid = guid;
  356. mutex_lock(&card_mutex);
  357. config_rom = generate_config_rom(card, &length);
  358. list_add_tail(&card->link, &card_list);
  359. mutex_unlock(&card_mutex);
  360. err = card->driver->enable(card, config_rom, length);
  361. if (err < 0) {
  362. mutex_lock(&card_mutex);
  363. list_del(&card->link);
  364. mutex_unlock(&card_mutex);
  365. }
  366. return err;
  367. }
  368. EXPORT_SYMBOL(fw_card_add);
  369. /*
  370. * The next few functions implements a dummy driver that use once a
  371. * card driver shuts down an fw_card. This allows the driver to
  372. * cleanly unload, as all IO to the card will be handled by the dummy
  373. * driver instead of calling into the (possibly) unloaded module. The
  374. * dummy driver just fails all IO.
  375. */
  376. static int
  377. dummy_enable(struct fw_card *card, u32 *config_rom, size_t length)
  378. {
  379. BUG();
  380. return -1;
  381. }
  382. static int
  383. dummy_update_phy_reg(struct fw_card *card, int address,
  384. int clear_bits, int set_bits)
  385. {
  386. return -ENODEV;
  387. }
  388. static int
  389. dummy_set_config_rom(struct fw_card *card,
  390. u32 *config_rom, size_t length)
  391. {
  392. /*
  393. * We take the card out of card_list before setting the dummy
  394. * driver, so this should never get called.
  395. */
  396. BUG();
  397. return -1;
  398. }
  399. static void
  400. dummy_send_request(struct fw_card *card, struct fw_packet *packet)
  401. {
  402. packet->callback(packet, card, -ENODEV);
  403. }
  404. static void
  405. dummy_send_response(struct fw_card *card, struct fw_packet *packet)
  406. {
  407. packet->callback(packet, card, -ENODEV);
  408. }
  409. static int
  410. dummy_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  411. {
  412. return -ENOENT;
  413. }
  414. static int
  415. dummy_enable_phys_dma(struct fw_card *card,
  416. int node_id, int generation)
  417. {
  418. return -ENODEV;
  419. }
  420. static struct fw_card_driver dummy_driver = {
  421. .enable = dummy_enable,
  422. .update_phy_reg = dummy_update_phy_reg,
  423. .set_config_rom = dummy_set_config_rom,
  424. .send_request = dummy_send_request,
  425. .cancel_packet = dummy_cancel_packet,
  426. .send_response = dummy_send_response,
  427. .enable_phys_dma = dummy_enable_phys_dma,
  428. };
  429. void
  430. fw_card_release(struct kref *kref)
  431. {
  432. struct fw_card *card = container_of(kref, struct fw_card, kref);
  433. complete(&card->done);
  434. }
  435. void
  436. fw_core_remove_card(struct fw_card *card)
  437. {
  438. card->driver->update_phy_reg(card, 4,
  439. PHY_LINK_ACTIVE | PHY_CONTENDER, 0);
  440. fw_core_initiate_bus_reset(card, 1);
  441. mutex_lock(&card_mutex);
  442. list_del_init(&card->link);
  443. mutex_unlock(&card_mutex);
  444. /* Set up the dummy driver. */
  445. card->driver = &dummy_driver;
  446. fw_destroy_nodes(card);
  447. /* Wait for all users, especially device workqueue jobs, to finish. */
  448. fw_card_put(card);
  449. wait_for_completion(&card->done);
  450. WARN_ON(!list_empty(&card->transaction_list));
  451. del_timer_sync(&card->flush_timer);
  452. }
  453. EXPORT_SYMBOL(fw_core_remove_card);
  454. int
  455. fw_core_initiate_bus_reset(struct fw_card *card, int short_reset)
  456. {
  457. int reg = short_reset ? 5 : 1;
  458. int bit = short_reset ? PHY_BUS_SHORT_RESET : PHY_BUS_RESET;
  459. return card->driver->update_phy_reg(card, reg, 0, bit);
  460. }
  461. EXPORT_SYMBOL(fw_core_initiate_bus_reset);