fw-card.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-card.c - card level functions
  4. *
  5. * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/device.h>
  24. #include "fw-transaction.h"
  25. #include "fw-topology.h"
  26. #include "fw-device.h"
  27. /* The lib/crc16.c implementation uses the standard (0x8005)
  28. * polynomial, but we need the ITU-T (or CCITT) polynomial (0x1021).
  29. * The implementation below works on an array of host-endian u32
  30. * words, assuming they'll be transmited msb first. */
  31. static u16
  32. crc16_itu_t(const u32 *buffer, size_t length)
  33. {
  34. int shift, i;
  35. u32 data;
  36. u16 sum, crc = 0;
  37. for (i = 0; i < length; i++) {
  38. data = *buffer++;
  39. for (shift = 28; shift >= 0; shift -= 4 ) {
  40. sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
  41. crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
  42. }
  43. crc &= 0xffff;
  44. }
  45. return crc;
  46. }
  47. static LIST_HEAD(card_list);
  48. static LIST_HEAD(descriptor_list);
  49. static int descriptor_count;
  50. #define bib_crc(v) ((v) << 0)
  51. #define bib_crc_length(v) ((v) << 16)
  52. #define bib_info_length(v) ((v) << 24)
  53. #define bib_link_speed(v) ((v) << 0)
  54. #define bib_generation(v) ((v) << 4)
  55. #define bib_max_rom(v) ((v) << 8)
  56. #define bib_max_receive(v) ((v) << 12)
  57. #define bib_cyc_clk_acc(v) ((v) << 16)
  58. #define bib_pmc ((1) << 27)
  59. #define bib_bmc ((1) << 28)
  60. #define bib_isc ((1) << 29)
  61. #define bib_cmc ((1) << 30)
  62. #define bib_imc ((1) << 31)
  63. static u32 *
  64. 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. /* Initialize contents of config rom buffer. On the OHCI
  70. * controller, block reads to the config rom accesses the host
  71. * memory, but quadlet read access the hardware bus info block
  72. * registers. That's just crack, but it means we should make
  73. * sure the contents of bus info block in host memory mathces
  74. * the version stored in the OHCI registers. */
  75. memset(config_rom, 0, sizeof config_rom);
  76. config_rom[0] = bib_crc_length(4) | bib_info_length(4) | bib_crc(0);
  77. config_rom[1] = 0x31333934;
  78. config_rom[2] =
  79. bib_link_speed(card->link_speed) |
  80. bib_generation(card->config_rom_generation++ % 14 + 2) |
  81. bib_max_rom(2) |
  82. bib_max_receive(card->max_receive) |
  83. bib_bmc | bib_isc | bib_cmc | bib_imc;
  84. config_rom[3] = card->guid >> 32;
  85. config_rom[4] = card->guid;
  86. /* Generate root directory. */
  87. i = 5;
  88. config_rom[i++] = 0;
  89. config_rom[i++] = 0x0c0083c0; /* node capabilities */
  90. j = i + descriptor_count;
  91. /* Generate root directory entries for descriptors. */
  92. list_for_each_entry (desc, &descriptor_list, link) {
  93. if (desc->immediate > 0)
  94. config_rom[i++] = desc->immediate;
  95. config_rom[i] = desc->key | (j - i);
  96. i++;
  97. j += desc->length;
  98. }
  99. /* Update root directory length. */
  100. config_rom[5] = (i - 5 - 1) << 16;
  101. /* End of root directory, now copy in descriptors. */
  102. list_for_each_entry (desc, &descriptor_list, link) {
  103. memcpy(&config_rom[i], desc->data, desc->length * 4);
  104. i += desc->length;
  105. }
  106. /* Calculate CRCs for all blocks in the config rom. This
  107. * assumes that CRC length and info length are identical for
  108. * the bus info block, which is always the case for this
  109. * implementation. */
  110. for (i = 0; i < j; i += length + 1) {
  111. length = (config_rom[i] >> 16) & 0xff;
  112. config_rom[i] |= crc16_itu_t(&config_rom[i + 1], length);
  113. }
  114. *config_rom_length = j;
  115. return config_rom;
  116. }
  117. static void
  118. update_config_roms (void)
  119. {
  120. struct fw_card *card;
  121. u32 *config_rom;
  122. size_t length;
  123. list_for_each_entry (card, &card_list, link) {
  124. config_rom = generate_config_rom(card, &length);
  125. card->driver->set_config_rom(card, config_rom, length);
  126. }
  127. }
  128. int
  129. fw_core_add_descriptor (struct fw_descriptor *desc)
  130. {
  131. size_t i;
  132. /* Check descriptor is valid; the length of all blocks in the
  133. * descriptor has to add up to exactly the length of the
  134. * block. */
  135. i = 0;
  136. while (i < desc->length)
  137. i += (desc->data[i] >> 16) + 1;
  138. if (i != desc->length)
  139. return -1;
  140. down_write(&fw_bus_type.subsys.rwsem);
  141. list_add_tail (&desc->link, &descriptor_list);
  142. descriptor_count++;
  143. if (desc->immediate > 0)
  144. descriptor_count++;
  145. update_config_roms();
  146. up_write(&fw_bus_type.subsys.rwsem);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(fw_core_add_descriptor);
  150. void
  151. fw_core_remove_descriptor (struct fw_descriptor *desc)
  152. {
  153. down_write(&fw_bus_type.subsys.rwsem);
  154. list_del(&desc->link);
  155. descriptor_count--;
  156. if (desc->immediate > 0)
  157. descriptor_count--;
  158. update_config_roms();
  159. up_write(&fw_bus_type.subsys.rwsem);
  160. }
  161. EXPORT_SYMBOL(fw_core_remove_descriptor);
  162. static const char gap_count_table[] = {
  163. 63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
  164. };
  165. struct bm_data {
  166. struct fw_transaction t;
  167. struct {
  168. __be32 arg;
  169. __be32 data;
  170. } lock;
  171. u32 old;
  172. int rcode;
  173. struct completion done;
  174. };
  175. static void
  176. complete_bm_lock(struct fw_card *card, int rcode,
  177. void *payload, size_t length, void *data)
  178. {
  179. struct bm_data *bmd = data;
  180. if (rcode == RCODE_COMPLETE)
  181. bmd->old = be32_to_cpu(*(__be32 *) payload);
  182. bmd->rcode = rcode;
  183. complete(&bmd->done);
  184. }
  185. static void
  186. 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;
  190. struct bm_data bmd;
  191. unsigned long flags;
  192. int root_id, new_root_id, irm_id, gap_count, generation, grace;
  193. int do_reset = 0;
  194. spin_lock_irqsave(&card->lock, flags);
  195. generation = card->generation;
  196. root = card->root_node->data;
  197. root_id = card->root_node->node_id;
  198. grace = time_after(jiffies, card->reset_jiffies + DIV_ROUND_UP(HZ, 10));
  199. if (card->bm_generation + 1 == generation ||
  200. (card->bm_generation != generation && grace)) {
  201. /* This first step is to figure out who is IRM and
  202. * then try to become bus manager. If the IRM is not
  203. * well defined (e.g. does not have an active link
  204. * layer or does not responds to our lock request, we
  205. * will have to do a little vigilante bus management.
  206. * In that case, we do a goto into the gap count logic
  207. * so that when we do the reset, we still optimize the
  208. * gap count. That could well save a reset in the
  209. * next generation. */
  210. irm_id = card->irm_node->node_id;
  211. if (!card->irm_node->link_on) {
  212. new_root_id = card->local_node->node_id;
  213. fw_notify("IRM has link off, making local node (%02x) root.\n",
  214. new_root_id);
  215. goto pick_me;
  216. }
  217. bmd.lock.arg = cpu_to_be32(0x3f);
  218. bmd.lock.data = cpu_to_be32(card->local_node->node_id);
  219. spin_unlock_irqrestore(&card->lock, flags);
  220. init_completion(&bmd.done);
  221. fw_send_request(card, &bmd.t, TCODE_LOCK_COMPARE_SWAP,
  222. irm_id, generation,
  223. SCODE_100, CSR_REGISTER_BASE + CSR_BUS_MANAGER_ID,
  224. &bmd.lock, sizeof bmd.lock,
  225. complete_bm_lock, &bmd);
  226. wait_for_completion(&bmd.done);
  227. if (bmd.rcode == RCODE_GENERATION) {
  228. /* Another bus reset happened. Just return,
  229. * the BM work has been rescheduled. */
  230. return;
  231. }
  232. if (bmd.rcode == RCODE_COMPLETE && bmd.old != 0x3f)
  233. /* Somebody else is BM, let them do the work. */
  234. return;
  235. spin_lock_irqsave(&card->lock, flags);
  236. if (bmd.rcode != RCODE_COMPLETE) {
  237. /* The lock request failed, maybe the IRM
  238. * isn't really IRM capable after all. Let's
  239. * do a bus reset and pick the local node as
  240. * root, and thus, IRM. */
  241. new_root_id = card->local_node->node_id;
  242. fw_notify("BM lock failed, making local node (%02x) root.\n",
  243. new_root_id);
  244. goto pick_me;
  245. }
  246. } else if (card->bm_generation != generation) {
  247. /* OK, we weren't BM in the last generation, and it's
  248. * less than 100ms since last bus reset. Reschedule
  249. * this task 100ms from now. */
  250. spin_unlock_irqrestore(&card->lock, flags);
  251. schedule_delayed_work(&card->work, DIV_ROUND_UP(HZ, 10));
  252. return;
  253. }
  254. /* We're bus manager for this generation, so next step is to
  255. * make sure we have an active cycle master and do gap count
  256. * optimization. */
  257. card->bm_generation = generation;
  258. if (root == NULL) {
  259. /* Either link_on is false, or we failed to read the
  260. * config rom. In either case, pick another root. */
  261. new_root_id = card->local_node->node_id;
  262. } else if (atomic_read(&root->state) != FW_DEVICE_RUNNING) {
  263. /* If we haven't probed this device yet, bail out now
  264. * and let's try again once that's done. */
  265. spin_unlock_irqrestore(&card->lock, flags);
  266. return;
  267. } else if (root->config_rom[2] & bib_cmc) {
  268. /* FIXME: I suppose we should set the cmstr bit in the
  269. * STATE_CLEAR register of this node, as described in
  270. * 1394-1995, 8.4.2.6. Also, send out a force root
  271. * packet for this node. */
  272. new_root_id = root_id;
  273. } else {
  274. /* Current root has an active link layer and we
  275. * successfully read the config rom, but it's not
  276. * cycle master capable. */
  277. new_root_id = card->local_node->node_id;
  278. }
  279. pick_me:
  280. /* Now figure out what gap count to set. */
  281. if (card->topology_type == FW_TOPOLOGY_A &&
  282. card->root_node->max_hops < ARRAY_SIZE(gap_count_table))
  283. gap_count = gap_count_table[card->root_node->max_hops];
  284. else
  285. gap_count = 63;
  286. /* Finally, figure out if we should do a reset or not. If we've
  287. * done less that 5 resets with the same physical topology and we
  288. * have either a new root or a new gap count setting, let's do it. */
  289. if (card->bm_retries++ < 5 &&
  290. (card->gap_count != gap_count || new_root_id != root_id))
  291. do_reset = 1;
  292. spin_unlock_irqrestore(&card->lock, flags);
  293. if (do_reset) {
  294. fw_notify("phy config: card %d, new root=%x, gap_count=%d\n",
  295. card->index, new_root_id, gap_count);
  296. fw_send_phy_config(card, new_root_id, generation, gap_count);
  297. fw_core_initiate_bus_reset(card, 1);
  298. }
  299. }
  300. static void
  301. release_card(struct device *device)
  302. {
  303. struct fw_card *card =
  304. container_of(device, struct fw_card, card_device);
  305. kfree(card);
  306. }
  307. static void
  308. flush_timer_callback(unsigned long data)
  309. {
  310. struct fw_card *card = (struct fw_card *)data;
  311. fw_flush_transactions(card);
  312. }
  313. void
  314. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  315. struct device *device)
  316. {
  317. static atomic_t index = ATOMIC_INIT(-1);
  318. card->index = atomic_inc_return(&index);
  319. card->driver = driver;
  320. card->device = device;
  321. card->current_tlabel = 0;
  322. card->tlabel_mask = 0;
  323. card->color = 0;
  324. INIT_LIST_HEAD(&card->transaction_list);
  325. spin_lock_init(&card->lock);
  326. setup_timer(&card->flush_timer,
  327. flush_timer_callback, (unsigned long)card);
  328. card->local_node = NULL;
  329. INIT_DELAYED_WORK(&card->work, fw_card_bm_work);
  330. card->card_device.bus = &fw_bus_type;
  331. card->card_device.release = release_card;
  332. card->card_device.parent = card->device;
  333. snprintf(card->card_device.bus_id, sizeof card->card_device.bus_id,
  334. "fwcard%d", card->index);
  335. device_initialize(&card->card_device);
  336. }
  337. EXPORT_SYMBOL(fw_card_initialize);
  338. int
  339. fw_card_add(struct fw_card *card,
  340. u32 max_receive, u32 link_speed, u64 guid)
  341. {
  342. int retval;
  343. u32 *config_rom;
  344. size_t length;
  345. card->max_receive = max_receive;
  346. card->link_speed = link_speed;
  347. card->guid = guid;
  348. /* FIXME: add #define's for phy registers. */
  349. /* Activate link_on bit and contender bit in our self ID packets.*/
  350. if (card->driver->update_phy_reg(card, 4, 0, 0x80 | 0x40) < 0)
  351. return -EIO;
  352. retval = device_add(&card->card_device);
  353. if (retval < 0) {
  354. fw_error("Failed to register card device.");
  355. return retval;
  356. }
  357. /* The subsystem grabs a reference when the card is added and
  358. * drops it when the driver calls fw_core_remove_card. */
  359. fw_card_get(card);
  360. down_write(&fw_bus_type.subsys.rwsem);
  361. config_rom = generate_config_rom (card, &length);
  362. list_add_tail(&card->link, &card_list);
  363. up_write(&fw_bus_type.subsys.rwsem);
  364. return card->driver->enable(card, config_rom, length);
  365. }
  366. EXPORT_SYMBOL(fw_card_add);
  367. /* The next few functions implements a dummy driver that use once a
  368. * card driver shuts down an fw_card. This allows the driver to
  369. * cleanly unload, as all IO to the card will be handled by the dummy
  370. * driver instead of calling into the (possibly) unloaded module. The
  371. * dummy driver just fails all IO. */
  372. static int
  373. dummy_enable(struct fw_card *card, u32 *config_rom, size_t length)
  374. {
  375. BUG();
  376. return -1;
  377. }
  378. static int
  379. dummy_update_phy_reg(struct fw_card *card, int address,
  380. int clear_bits, int set_bits)
  381. {
  382. return -ENODEV;
  383. }
  384. static int
  385. dummy_set_config_rom(struct fw_card *card,
  386. u32 *config_rom, size_t length)
  387. {
  388. /* We take the card out of card_list before setting the dummy
  389. * driver, so this should never get called. */
  390. BUG();
  391. return -1;
  392. }
  393. static void
  394. dummy_send_request(struct fw_card *card, struct fw_packet *packet)
  395. {
  396. packet->callback(packet, card, -ENODEV);
  397. }
  398. static void
  399. dummy_send_response(struct fw_card *card, struct fw_packet *packet)
  400. {
  401. packet->callback(packet, card, -ENODEV);
  402. }
  403. static int
  404. dummy_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  405. {
  406. return -ENOENT;
  407. }
  408. static int
  409. dummy_enable_phys_dma(struct fw_card *card,
  410. int node_id, int generation)
  411. {
  412. return -ENODEV;
  413. }
  414. static struct fw_card_driver dummy_driver = {
  415. .name = "dummy",
  416. .enable = dummy_enable,
  417. .update_phy_reg = dummy_update_phy_reg,
  418. .set_config_rom = dummy_set_config_rom,
  419. .send_request = dummy_send_request,
  420. .cancel_packet = dummy_cancel_packet,
  421. .send_response = dummy_send_response,
  422. .enable_phys_dma = dummy_enable_phys_dma,
  423. };
  424. void
  425. fw_core_remove_card(struct fw_card *card)
  426. {
  427. card->driver->update_phy_reg(card, 4, 0x80 | 0x40, 0);
  428. fw_core_initiate_bus_reset(card, 1);
  429. down_write(&fw_bus_type.subsys.rwsem);
  430. list_del(&card->link);
  431. up_write(&fw_bus_type.subsys.rwsem);
  432. /* Set up the dummy driver. */
  433. card->driver = &dummy_driver;
  434. fw_flush_transactions(card);
  435. fw_destroy_nodes(card);
  436. /* This also drops the subsystem reference. */
  437. device_unregister(&card->card_device);
  438. }
  439. EXPORT_SYMBOL(fw_core_remove_card);
  440. struct fw_card *
  441. fw_card_get(struct fw_card *card)
  442. {
  443. get_device(&card->card_device);
  444. return card;
  445. }
  446. EXPORT_SYMBOL(fw_card_get);
  447. /* An assumption for fw_card_put() is that the card driver allocates
  448. * the fw_card struct with kalloc and that it has been shut down
  449. * before the last ref is dropped. */
  450. void
  451. fw_card_put(struct fw_card *card)
  452. {
  453. put_device(&card->card_device);
  454. }
  455. EXPORT_SYMBOL(fw_card_put);
  456. int
  457. fw_core_initiate_bus_reset(struct fw_card *card, int short_reset)
  458. {
  459. return card->driver->update_phy_reg(card, short_reset ? 5 : 1, 0, 0x40);
  460. }
  461. EXPORT_SYMBOL(fw_core_initiate_bus_reset);