fw-card.c 15 KB

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