fw-card.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_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. config_rom[i++] = 0x03d00d1e; /* vendor id */
  91. j = i + descriptor_count;
  92. /* Generate root directory entries for descriptors. */
  93. list_for_each_entry (desc, &descriptor_list, link) {
  94. config_rom[i] = desc->key | (j - i);
  95. i++;
  96. j += desc->length;
  97. }
  98. /* Update root directory length. */
  99. config_rom[5] = (i - 5 - 1) << 16;
  100. /* End of root directory, now copy in descriptors. */
  101. list_for_each_entry (desc, &descriptor_list, link) {
  102. memcpy(&config_rom[i], desc->data, desc->length * 4);
  103. i += desc->length;
  104. }
  105. /* Calculate CRCs for all blocks in the config rom. This
  106. * assumes that CRC length and info length are identical for
  107. * the bus info block, which is always the case for this
  108. * implementation. */
  109. for (i = 0; i < j; i += length + 1) {
  110. length = (config_rom[i] >> 16) & 0xff;
  111. config_rom[i] |= crc16_itu_t(&config_rom[i + 1], length);
  112. }
  113. *config_rom_length = j;
  114. return config_rom;
  115. }
  116. static void
  117. 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
  128. fw_core_add_descriptor (struct fw_descriptor *desc)
  129. {
  130. size_t i;
  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. i = 0;
  135. while (i < desc->length)
  136. i += (desc->data[i] >> 16) + 1;
  137. if (i != desc->length)
  138. return -1;
  139. down_write(&fw_bus_type.subsys.rwsem);
  140. list_add_tail (&desc->link, &descriptor_list);
  141. descriptor_count++;
  142. update_config_roms();
  143. up_write(&fw_bus_type.subsys.rwsem);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(fw_core_add_descriptor);
  147. void
  148. fw_core_remove_descriptor (struct fw_descriptor *desc)
  149. {
  150. down_write(&fw_bus_type.subsys.rwsem);
  151. list_del(&desc->link);
  152. descriptor_count--;
  153. update_config_roms();
  154. up_write(&fw_bus_type.subsys.rwsem);
  155. }
  156. EXPORT_SYMBOL(fw_core_remove_descriptor);
  157. static const char gap_count_table[] = {
  158. 63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
  159. };
  160. static void
  161. fw_card_irm_work(struct work_struct *work)
  162. {
  163. struct fw_card *card = container_of(work, struct fw_card, work.work);
  164. struct fw_device *root;
  165. unsigned long flags;
  166. int root_id, new_irm_id, gap_count, generation, do_reset = 0;
  167. /* FIXME: This simple bus management unconditionally picks a
  168. * cycle master if the current root can't do it. We need to
  169. * not do this if there is a bus manager already. Also, some
  170. * hubs set the contender bit, which is bogus, so we should
  171. * probably do a little sanity check on the IRM (like, read
  172. * the bandwidth register) if it's not us. */
  173. spin_lock_irqsave(&card->lock, flags);
  174. generation = card->generation;
  175. root = card->root_node->data;
  176. root_id = card->root_node->node_id;
  177. if (root == NULL) {
  178. /* Either link_on is false, or we failed to read the
  179. * config rom. In either case, pick another root. */
  180. new_irm_id = card->local_node->node_id;
  181. } else if (root->state != FW_DEVICE_RUNNING) {
  182. /* If we haven't probed this device yet, bail out now
  183. * and let's try again once that's done. */
  184. new_irm_id = root_id;
  185. } else if (root->config_rom[2] & bib_cmc) {
  186. /* FIXME: I suppose we should set the cmstr bit in the
  187. * STATE_CLEAR register of this node, as described in
  188. * 1394-1995, 8.4.2.6. Also, send out a force root
  189. * packet for this node. */
  190. new_irm_id = root_id;
  191. } else {
  192. /* Current root has an active link layer and we
  193. * successfully read the config rom, but it's not
  194. * cycle master capable. */
  195. new_irm_id = card->local_node->node_id;
  196. }
  197. /* Now figure out what gap count to set. */
  198. if (card->topology_type == FW_TOPOLOGY_A &&
  199. card->root_node->max_hops < ARRAY_SIZE(gap_count_table))
  200. gap_count = gap_count_table[card->root_node->max_hops];
  201. else
  202. gap_count = 63;
  203. /* Finally, figure out if we should do a reset or not. If we've
  204. * done less that 5 resets with the same physical topology and we
  205. * have either a new root or a new gap count setting, let's do it. */
  206. if (card->irm_retries++ < 5 &&
  207. (card->gap_count != gap_count || new_irm_id != root_id))
  208. do_reset = 1;
  209. spin_unlock_irqrestore(&card->lock, flags);
  210. if (do_reset) {
  211. fw_notify("phy config: card %d, new root=%x, gap_count=%d\n",
  212. card->index, new_irm_id, gap_count);
  213. fw_send_phy_config(card, new_irm_id, generation, gap_count);
  214. fw_core_initiate_bus_reset(card, 1);
  215. }
  216. }
  217. static void
  218. release_card(struct device *device)
  219. {
  220. struct fw_card *card =
  221. container_of(device, struct fw_card, card_device);
  222. kfree(card);
  223. }
  224. static void
  225. flush_timer_callback(unsigned long data)
  226. {
  227. struct fw_card *card = (struct fw_card *)data;
  228. fw_flush_transactions(card);
  229. }
  230. void
  231. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  232. struct device *device)
  233. {
  234. static int index;
  235. card->index = index++;
  236. card->driver = driver;
  237. card->device = device;
  238. card->current_tlabel = 0;
  239. card->tlabel_mask = 0;
  240. card->color = 0;
  241. INIT_LIST_HEAD(&card->transaction_list);
  242. spin_lock_init(&card->lock);
  243. setup_timer(&card->flush_timer,
  244. flush_timer_callback, (unsigned long)card);
  245. card->local_node = NULL;
  246. INIT_DELAYED_WORK(&card->work, fw_card_irm_work);
  247. card->card_device.bus = &fw_bus_type;
  248. card->card_device.release = release_card;
  249. card->card_device.parent = card->device;
  250. snprintf(card->card_device.bus_id, sizeof card->card_device.bus_id,
  251. "fwcard%d", card->index);
  252. device_initialize(&card->card_device);
  253. }
  254. EXPORT_SYMBOL(fw_card_initialize);
  255. int
  256. fw_card_add(struct fw_card *card,
  257. u32 max_receive, u32 link_speed, u64 guid)
  258. {
  259. int retval;
  260. u32 *config_rom;
  261. size_t length;
  262. card->max_receive = max_receive;
  263. card->link_speed = link_speed;
  264. card->guid = guid;
  265. /* FIXME: add #define's for phy registers. */
  266. /* Activate link_on bit and contender bit in our self ID packets.*/
  267. if (card->driver->update_phy_reg(card, 4, 0, 0x80 | 0x40) < 0)
  268. return -EIO;
  269. retval = device_add(&card->card_device);
  270. if (retval < 0) {
  271. fw_error("Failed to register card device.");
  272. return retval;
  273. }
  274. /* The subsystem grabs a reference when the card is added and
  275. * drops it when the driver calls fw_core_remove_card. */
  276. fw_card_get(card);
  277. down_write(&fw_bus_type.subsys.rwsem);
  278. config_rom = generate_config_rom (card, &length);
  279. list_add_tail(&card->link, &card_list);
  280. up_write(&fw_bus_type.subsys.rwsem);
  281. return card->driver->enable(card, config_rom, length);
  282. }
  283. EXPORT_SYMBOL(fw_card_add);
  284. /* The next few functions implements a dummy driver that use once a
  285. * card driver shuts down an fw_card. This allows the driver to
  286. * cleanly unload, as all IO to the card will be handled by the dummy
  287. * driver instead of calling into the (possibly) unloaded module. The
  288. * dummy driver just fails all IO. */
  289. static int
  290. dummy_enable(struct fw_card *card, u32 *config_rom, size_t length)
  291. {
  292. BUG();
  293. return -1;
  294. }
  295. static int
  296. dummy_update_phy_reg(struct fw_card *card, int address,
  297. int clear_bits, int set_bits)
  298. {
  299. return -ENODEV;
  300. }
  301. static int
  302. dummy_set_config_rom(struct fw_card *card,
  303. u32 *config_rom, size_t length)
  304. {
  305. /* We take the card out of card_list before setting the dummy
  306. * driver, so this should never get called. */
  307. BUG();
  308. return -1;
  309. }
  310. static void
  311. dummy_send_request(struct fw_card *card, struct fw_packet *packet)
  312. {
  313. packet->callback(packet, card, -ENODEV);
  314. }
  315. static void
  316. dummy_send_response(struct fw_card *card, struct fw_packet *packet)
  317. {
  318. packet->callback(packet, card, -ENODEV);
  319. }
  320. static int
  321. dummy_enable_phys_dma(struct fw_card *card,
  322. int node_id, int generation)
  323. {
  324. return -ENODEV;
  325. }
  326. static struct fw_card_driver dummy_driver = {
  327. .name = "dummy",
  328. .enable = dummy_enable,
  329. .update_phy_reg = dummy_update_phy_reg,
  330. .set_config_rom = dummy_set_config_rom,
  331. .send_request = dummy_send_request,
  332. .send_response = dummy_send_response,
  333. .enable_phys_dma = dummy_enable_phys_dma,
  334. };
  335. void
  336. fw_core_remove_card(struct fw_card *card)
  337. {
  338. card->driver->update_phy_reg(card, 4, 0x80 | 0x40, 0);
  339. fw_core_initiate_bus_reset(card, 1);
  340. down_write(&fw_bus_type.subsys.rwsem);
  341. list_del(&card->link);
  342. up_write(&fw_bus_type.subsys.rwsem);
  343. /* Set up the dummy driver. */
  344. card->driver = &dummy_driver;
  345. fw_flush_transactions(card);
  346. fw_destroy_nodes(card);
  347. /* This also drops the subsystem reference. */
  348. device_unregister(&card->card_device);
  349. }
  350. EXPORT_SYMBOL(fw_core_remove_card);
  351. struct fw_card *
  352. fw_card_get(struct fw_card *card)
  353. {
  354. get_device(&card->card_device);
  355. return card;
  356. }
  357. EXPORT_SYMBOL(fw_card_get);
  358. /* An assumption for fw_card_put() is that the card driver allocates
  359. * the fw_card struct with kalloc and that it has been shut down
  360. * before the last ref is dropped. */
  361. void
  362. fw_card_put(struct fw_card *card)
  363. {
  364. put_device(&card->card_device);
  365. }
  366. EXPORT_SYMBOL(fw_card_put);
  367. int
  368. fw_core_initiate_bus_reset(struct fw_card *card, int short_reset)
  369. {
  370. return card->driver->update_phy_reg(card, short_reset ? 5 : 1, 0, 0x40);
  371. }
  372. EXPORT_SYMBOL(fw_core_initiate_bus_reset);