core-card.c 15 KB

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