core-card.c 15 KB

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