PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-2.6.22.19-HACHICHA_AJIT/drivers/firewire/fw-card.c

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