fw-card.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 mathces
  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. struct bm_data {
  159. struct fw_transaction t;
  160. struct {
  161. __be32 arg;
  162. __be32 data;
  163. } lock;
  164. u32 old;
  165. int rcode;
  166. struct completion done;
  167. };
  168. static void
  169. complete_bm_lock(struct fw_card *card, int rcode,
  170. void *payload, size_t length, void *data)
  171. {
  172. struct bm_data *bmd = data;
  173. if (rcode == RCODE_COMPLETE)
  174. bmd->old = be32_to_cpu(*(__be32 *) payload);
  175. bmd->rcode = rcode;
  176. complete(&bmd->done);
  177. }
  178. static void
  179. fw_card_bm_work(struct work_struct *work)
  180. {
  181. struct fw_card *card = container_of(work, struct fw_card, work.work);
  182. struct fw_device *root_device;
  183. struct fw_node *root_node, *local_node;
  184. struct bm_data bmd;
  185. unsigned long flags;
  186. int root_id, new_root_id, irm_id, gap_count, generation, grace;
  187. bool do_reset = false;
  188. spin_lock_irqsave(&card->lock, flags);
  189. local_node = card->local_node;
  190. root_node = card->root_node;
  191. if (local_node == NULL) {
  192. spin_unlock_irqrestore(&card->lock, flags);
  193. return;
  194. }
  195. fw_node_get(local_node);
  196. fw_node_get(root_node);
  197. generation = card->generation;
  198. root_device = root_node->data;
  199. if (root_device)
  200. fw_device_get(root_device);
  201. root_id = root_node->node_id;
  202. grace = time_after(jiffies, card->reset_jiffies + DIV_ROUND_UP(HZ, 10));
  203. if (card->bm_generation + 1 == generation ||
  204. (card->bm_generation != generation && grace)) {
  205. /*
  206. * This first step is to figure out who is IRM and
  207. * then try to become bus manager. If the IRM is not
  208. * well defined (e.g. does not have an active link
  209. * layer or does not responds to our lock request, we
  210. * will have to do a little vigilante bus management.
  211. * In that case, we do a goto into the gap count logic
  212. * so that when we do the reset, we still optimize the
  213. * gap count. That could well save a reset in the
  214. * next generation.
  215. */
  216. irm_id = card->irm_node->node_id;
  217. if (!card->irm_node->link_on) {
  218. new_root_id = local_node->node_id;
  219. fw_notify("IRM has link off, making local node (%02x) root.\n",
  220. new_root_id);
  221. goto pick_me;
  222. }
  223. bmd.lock.arg = cpu_to_be32(0x3f);
  224. bmd.lock.data = cpu_to_be32(local_node->node_id);
  225. spin_unlock_irqrestore(&card->lock, flags);
  226. init_completion(&bmd.done);
  227. fw_send_request(card, &bmd.t, TCODE_LOCK_COMPARE_SWAP,
  228. irm_id, generation,
  229. SCODE_100, CSR_REGISTER_BASE + CSR_BUS_MANAGER_ID,
  230. &bmd.lock, sizeof(bmd.lock),
  231. complete_bm_lock, &bmd);
  232. wait_for_completion(&bmd.done);
  233. if (bmd.rcode == RCODE_GENERATION) {
  234. /*
  235. * Another bus reset happened. Just return,
  236. * the BM work has been rescheduled.
  237. */
  238. goto out;
  239. }
  240. if (bmd.rcode == RCODE_COMPLETE && bmd.old != 0x3f)
  241. /* Somebody else is BM, let them do the work. */
  242. goto out;
  243. spin_lock_irqsave(&card->lock, flags);
  244. if (bmd.rcode != RCODE_COMPLETE) {
  245. /*
  246. * The lock request failed, maybe the IRM
  247. * isn't really IRM capable after all. Let's
  248. * do a bus reset and pick the local node as
  249. * root, and thus, IRM.
  250. */
  251. new_root_id = local_node->node_id;
  252. fw_notify("BM lock failed, making local node (%02x) root.\n",
  253. new_root_id);
  254. goto pick_me;
  255. }
  256. } else if (card->bm_generation != generation) {
  257. /*
  258. * OK, we weren't BM in the last generation, and it's
  259. * less than 100ms since last bus reset. Reschedule
  260. * this task 100ms from now.
  261. */
  262. spin_unlock_irqrestore(&card->lock, flags);
  263. schedule_delayed_work(&card->work, DIV_ROUND_UP(HZ, 10));
  264. goto out;
  265. }
  266. /*
  267. * We're bus manager for this generation, so next step is to
  268. * make sure we have an active cycle master and do gap count
  269. * optimization.
  270. */
  271. card->bm_generation = generation;
  272. if (root_device == NULL) {
  273. /*
  274. * Either link_on is false, or we failed to read the
  275. * config rom. In either case, pick another root.
  276. */
  277. new_root_id = local_node->node_id;
  278. } else if (atomic_read(&root_device->state) != FW_DEVICE_RUNNING) {
  279. /*
  280. * If we haven't probed this device yet, bail out now
  281. * and let's try again once that's done.
  282. */
  283. spin_unlock_irqrestore(&card->lock, flags);
  284. goto out;
  285. } else if (root_device->cmc) {
  286. /*
  287. * FIXME: I suppose we should set the cmstr bit in the
  288. * STATE_CLEAR register of this node, as described in
  289. * 1394-1995, 8.4.2.6. Also, send out a force root
  290. * packet for this node.
  291. */
  292. new_root_id = root_id;
  293. } else {
  294. /*
  295. * Current root has an active link layer and we
  296. * successfully read the config rom, but it's not
  297. * cycle master capable.
  298. */
  299. new_root_id = local_node->node_id;
  300. }
  301. pick_me:
  302. /*
  303. * Pick a gap count from 1394a table E-1. The table doesn't cover
  304. * the typically much larger 1394b beta repeater delays though.
  305. */
  306. if (!card->beta_repeaters_present &&
  307. root_node->max_hops < ARRAY_SIZE(gap_count_table))
  308. gap_count = gap_count_table[root_node->max_hops];
  309. else
  310. gap_count = 63;
  311. /*
  312. * Finally, figure out if we should do a reset or not. If we have
  313. * done less than 5 resets with the same physical topology and we
  314. * have either a new root or a new gap count setting, let's do it.
  315. */
  316. if (card->bm_retries++ < 5 &&
  317. (card->gap_count != gap_count || new_root_id != root_id))
  318. do_reset = true;
  319. spin_unlock_irqrestore(&card->lock, flags);
  320. if (do_reset) {
  321. fw_notify("phy config: card %d, new root=%x, gap_count=%d\n",
  322. card->index, new_root_id, gap_count);
  323. fw_send_phy_config(card, new_root_id, generation, gap_count);
  324. fw_core_initiate_bus_reset(card, 1);
  325. }
  326. out:
  327. if (root_device)
  328. fw_device_put(root_device);
  329. fw_node_put(root_node);
  330. fw_node_put(local_node);
  331. }
  332. static void
  333. flush_timer_callback(unsigned long data)
  334. {
  335. struct fw_card *card = (struct fw_card *)data;
  336. fw_flush_transactions(card);
  337. }
  338. void
  339. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  340. struct device *device)
  341. {
  342. static atomic_t index = ATOMIC_INIT(-1);
  343. card->index = atomic_inc_return(&index);
  344. card->driver = driver;
  345. card->device = device;
  346. card->current_tlabel = 0;
  347. card->tlabel_mask = 0;
  348. card->color = 0;
  349. card->broadcast_channel = BROADCAST_CHANNEL_INITIAL;
  350. kref_init(&card->kref);
  351. init_completion(&card->done);
  352. INIT_LIST_HEAD(&card->transaction_list);
  353. spin_lock_init(&card->lock);
  354. setup_timer(&card->flush_timer,
  355. flush_timer_callback, (unsigned long)card);
  356. card->local_node = NULL;
  357. INIT_DELAYED_WORK(&card->work, fw_card_bm_work);
  358. }
  359. EXPORT_SYMBOL(fw_card_initialize);
  360. int
  361. fw_card_add(struct fw_card *card,
  362. u32 max_receive, u32 link_speed, u64 guid)
  363. {
  364. u32 *config_rom;
  365. size_t length;
  366. card->max_receive = max_receive;
  367. card->link_speed = link_speed;
  368. card->guid = guid;
  369. mutex_lock(&card_mutex);
  370. config_rom = generate_config_rom(card, &length);
  371. list_add_tail(&card->link, &card_list);
  372. mutex_unlock(&card_mutex);
  373. return card->driver->enable(card, config_rom, length);
  374. }
  375. EXPORT_SYMBOL(fw_card_add);
  376. /*
  377. * The next few functions implements a dummy driver that use once a
  378. * card driver shuts down an fw_card. This allows the driver to
  379. * cleanly unload, as all IO to the card will be handled by the dummy
  380. * driver instead of calling into the (possibly) unloaded module. The
  381. * dummy driver just fails all IO.
  382. */
  383. static int
  384. dummy_enable(struct fw_card *card, u32 *config_rom, size_t length)
  385. {
  386. BUG();
  387. return -1;
  388. }
  389. static int
  390. dummy_update_phy_reg(struct fw_card *card, int address,
  391. int clear_bits, int set_bits)
  392. {
  393. return -ENODEV;
  394. }
  395. static int
  396. dummy_set_config_rom(struct fw_card *card,
  397. u32 *config_rom, size_t length)
  398. {
  399. /*
  400. * We take the card out of card_list before setting the dummy
  401. * driver, so this should never get called.
  402. */
  403. BUG();
  404. return -1;
  405. }
  406. static void
  407. dummy_send_request(struct fw_card *card, struct fw_packet *packet)
  408. {
  409. packet->callback(packet, card, -ENODEV);
  410. }
  411. static void
  412. dummy_send_response(struct fw_card *card, struct fw_packet *packet)
  413. {
  414. packet->callback(packet, card, -ENODEV);
  415. }
  416. static int
  417. dummy_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  418. {
  419. return -ENOENT;
  420. }
  421. static int
  422. dummy_enable_phys_dma(struct fw_card *card,
  423. int node_id, int generation)
  424. {
  425. return -ENODEV;
  426. }
  427. static struct fw_card_driver dummy_driver = {
  428. .enable = dummy_enable,
  429. .update_phy_reg = dummy_update_phy_reg,
  430. .set_config_rom = dummy_set_config_rom,
  431. .send_request = dummy_send_request,
  432. .cancel_packet = dummy_cancel_packet,
  433. .send_response = dummy_send_response,
  434. .enable_phys_dma = dummy_enable_phys_dma,
  435. };
  436. void
  437. fw_card_release(struct kref *kref)
  438. {
  439. struct fw_card *card = container_of(kref, struct fw_card, kref);
  440. complete(&card->done);
  441. }
  442. void
  443. fw_core_remove_card(struct fw_card *card)
  444. {
  445. card->driver->update_phy_reg(card, 4,
  446. PHY_LINK_ACTIVE | PHY_CONTENDER, 0);
  447. fw_core_initiate_bus_reset(card, 1);
  448. mutex_lock(&card_mutex);
  449. list_del(&card->link);
  450. mutex_unlock(&card_mutex);
  451. /* Set up the dummy driver. */
  452. card->driver = &dummy_driver;
  453. fw_destroy_nodes(card);
  454. /* Wait for all users, especially device workqueue jobs, to finish. */
  455. fw_card_put(card);
  456. wait_for_completion(&card->done);
  457. cancel_delayed_work_sync(&card->work);
  458. WARN_ON(!list_empty(&card->transaction_list));
  459. del_timer_sync(&card->flush_timer);
  460. }
  461. EXPORT_SYMBOL(fw_core_remove_card);
  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);