fw-card.c 14 KB

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