fw-card.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 void
  158. fw_card_irm_work(struct work_struct *work)
  159. {
  160. struct fw_card *card =
  161. container_of(work, struct fw_card, work.work);
  162. struct fw_device *root;
  163. unsigned long flags;
  164. int new_irm_id, generation;
  165. /* FIXME: This simple bus management unconditionally picks a
  166. * cycle master if the current root can't do it. We need to
  167. * not do this if there is a bus manager already. Also, some
  168. * hubs set the contender bit, which is bogus, so we should
  169. * probably do a little sanity check on the IRM (like, read
  170. * the bandwidth register) if it's not us. */
  171. spin_lock_irqsave(&card->lock, flags);
  172. generation = card->generation;
  173. root = card->root_node->data;
  174. if (root == NULL)
  175. /* Either link_on is false, or we failed to read the
  176. * config rom. In either case, pick another root. */
  177. new_irm_id = card->local_node->node_id;
  178. else if (root->state != FW_DEVICE_RUNNING)
  179. /* If we haven't probed this device yet, bail out now
  180. * and let's try again once that's done. */
  181. new_irm_id = -1;
  182. else if (root->config_rom[2] & bib_cmc)
  183. /* FIXME: I suppose we should set the cmstr bit in the
  184. * STATE_CLEAR register of this node, as described in
  185. * 1394-1995, 8.4.2.6. Also, send out a force root
  186. * packet for this node. */
  187. new_irm_id = -1;
  188. else
  189. /* Current root has an active link layer and we
  190. * successfully read the config rom, but it's not
  191. * cycle master capable. */
  192. new_irm_id = card->local_node->node_id;
  193. if (card->irm_retries++ > 5)
  194. new_irm_id = -1;
  195. spin_unlock_irqrestore(&card->lock, flags);
  196. if (new_irm_id > 0) {
  197. fw_notify("Trying to become root (card %d)\n", card->index);
  198. fw_send_force_root(card, new_irm_id, generation);
  199. fw_core_initiate_bus_reset(card, 1);
  200. }
  201. }
  202. static void
  203. release_card(struct device *device)
  204. {
  205. struct fw_card *card =
  206. container_of(device, struct fw_card, card_device);
  207. kfree(card);
  208. }
  209. static void
  210. flush_timer_callback(unsigned long data)
  211. {
  212. struct fw_card *card = (struct fw_card *)data;
  213. fw_flush_transactions(card);
  214. }
  215. void
  216. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  217. struct device *device)
  218. {
  219. static int index;
  220. card->index = index++;
  221. card->driver = driver;
  222. card->device = device;
  223. card->current_tlabel = 0;
  224. card->tlabel_mask = 0;
  225. card->color = 0;
  226. INIT_LIST_HEAD(&card->transaction_list);
  227. spin_lock_init(&card->lock);
  228. setup_timer(&card->flush_timer,
  229. flush_timer_callback, (unsigned long)card);
  230. card->local_node = NULL;
  231. INIT_DELAYED_WORK(&card->work, fw_card_irm_work);
  232. card->card_device.bus = &fw_bus_type;
  233. card->card_device.release = release_card;
  234. card->card_device.parent = card->device;
  235. snprintf(card->card_device.bus_id, sizeof card->card_device.bus_id,
  236. "fwcard%d", card->index);
  237. device_initialize(&card->card_device);
  238. }
  239. EXPORT_SYMBOL(fw_card_initialize);
  240. int
  241. fw_card_add(struct fw_card *card,
  242. u32 max_receive, u32 link_speed, u64 guid)
  243. {
  244. int retval;
  245. u32 *config_rom;
  246. size_t length;
  247. card->max_receive = max_receive;
  248. card->link_speed = link_speed;
  249. card->guid = guid;
  250. /* FIXME: add #define's for phy registers. */
  251. /* Activate link_on bit and contender bit in our self ID packets.*/
  252. if (card->driver->update_phy_reg(card, 4, 0, 0x80 | 0x40) < 0)
  253. return -EIO;
  254. retval = device_add(&card->card_device);
  255. if (retval < 0) {
  256. fw_error("Failed to register card device.");
  257. return retval;
  258. }
  259. /* The subsystem grabs a reference when the card is added and
  260. * drops it when the driver calls fw_core_remove_card. */
  261. fw_card_get(card);
  262. down_write(&fw_bus_type.subsys.rwsem);
  263. config_rom = generate_config_rom (card, &length);
  264. list_add_tail(&card->link, &card_list);
  265. up_write(&fw_bus_type.subsys.rwsem);
  266. return card->driver->enable(card, config_rom, length);
  267. }
  268. EXPORT_SYMBOL(fw_card_add);
  269. /* The next few functions implements a dummy driver that use once a
  270. * card driver shuts down an fw_card. This allows the driver to
  271. * cleanly unload, as all IO to the card will be handled by the dummy
  272. * driver instead of calling into the (possibly) unloaded module. The
  273. * dummy driver just fails all IO. */
  274. static int
  275. dummy_enable(struct fw_card *card, u32 *config_rom, size_t length)
  276. {
  277. BUG();
  278. return -1;
  279. }
  280. static int
  281. dummy_update_phy_reg(struct fw_card *card, int address,
  282. int clear_bits, int set_bits)
  283. {
  284. return -ENODEV;
  285. }
  286. static int
  287. dummy_set_config_rom(struct fw_card *card,
  288. u32 *config_rom, size_t length)
  289. {
  290. /* We take the card out of card_list before setting the dummy
  291. * driver, so this should never get called. */
  292. BUG();
  293. return -1;
  294. }
  295. static void
  296. dummy_send_request(struct fw_card *card, struct fw_packet *packet)
  297. {
  298. packet->callback(packet, card, -ENODEV);
  299. }
  300. static void
  301. dummy_send_response(struct fw_card *card, struct fw_packet *packet)
  302. {
  303. packet->callback(packet, card, -ENODEV);
  304. }
  305. static int
  306. dummy_enable_phys_dma(struct fw_card *card,
  307. int node_id, int generation)
  308. {
  309. return -ENODEV;
  310. }
  311. static struct fw_card_driver dummy_driver = {
  312. .name = "dummy",
  313. .enable = dummy_enable,
  314. .update_phy_reg = dummy_update_phy_reg,
  315. .set_config_rom = dummy_set_config_rom,
  316. .send_request = dummy_send_request,
  317. .send_response = dummy_send_response,
  318. .enable_phys_dma = dummy_enable_phys_dma
  319. };
  320. void
  321. fw_core_remove_card(struct fw_card *card)
  322. {
  323. card->driver->update_phy_reg(card, 4, 0x80 | 0x40, 0);
  324. fw_core_initiate_bus_reset(card, 1);
  325. down_write(&fw_bus_type.subsys.rwsem);
  326. list_del(&card->link);
  327. up_write(&fw_bus_type.subsys.rwsem);
  328. /* Set up the dummy driver. */
  329. card->driver = &dummy_driver;
  330. fw_flush_transactions(card);
  331. fw_destroy_nodes(card);
  332. /* This also drops the subsystem reference. */
  333. device_unregister(&card->card_device);
  334. }
  335. EXPORT_SYMBOL(fw_core_remove_card);
  336. struct fw_card *
  337. fw_card_get(struct fw_card *card)
  338. {
  339. get_device(&card->card_device);
  340. return card;
  341. }
  342. EXPORT_SYMBOL(fw_card_get);
  343. /* An assumption for fw_card_put() is that the card driver allocates
  344. * the fw_card struct with kalloc and that it has been shut down
  345. * before the last ref is dropped. */
  346. void
  347. fw_card_put(struct fw_card *card)
  348. {
  349. put_device(&card->card_device);
  350. }
  351. EXPORT_SYMBOL(fw_card_put);
  352. int
  353. fw_core_initiate_bus_reset(struct fw_card *card, int short_reset)
  354. {
  355. return card->driver->update_phy_reg(card, short_reset ? 5 : 1, 0, 0x40);
  356. }
  357. EXPORT_SYMBOL(fw_core_initiate_bus_reset);