core-card.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
  4. */
  5. #include <linux/bug.h>
  6. #include <linux/completion.h>
  7. #include <linux/crc-itu-t.h>
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <linux/firewire.h>
  11. #include <linux/firewire-constants.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kref.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/atomic.h>
  21. #include <asm/byteorder.h>
  22. #include "core.h"
  23. #include <trace/events/firewire.h>
  24. #define define_fw_printk_level(func, kern_level) \
  25. void func(const struct fw_card *card, const char *fmt, ...) \
  26. { \
  27. struct va_format vaf; \
  28. va_list args; \
  29. \
  30. va_start(args, fmt); \
  31. vaf.fmt = fmt; \
  32. vaf.va = &args; \
  33. printk(kern_level KBUILD_MODNAME " %s: %pV", \
  34. dev_name(card->device), &vaf); \
  35. va_end(args); \
  36. }
  37. define_fw_printk_level(fw_err, KERN_ERR);
  38. define_fw_printk_level(fw_notice, KERN_NOTICE);
  39. int fw_compute_block_crc(__be32 *block)
  40. {
  41. int length;
  42. u16 crc;
  43. length = (be32_to_cpu(block[0]) >> 16) & 0xff;
  44. crc = crc_itu_t(0, (u8 *)&block[1], length * 4);
  45. *block |= cpu_to_be32(crc);
  46. return length;
  47. }
  48. static DEFINE_MUTEX(card_mutex);
  49. static LIST_HEAD(card_list);
  50. static LIST_HEAD(descriptor_list);
  51. static int descriptor_count;
  52. static __be32 tmp_config_rom[256];
  53. /* ROM header, bus info block, root dir header, capabilities = 7 quadlets */
  54. static size_t config_rom_length = 1 + 4 + 1 + 1;
  55. #define BIB_CRC(v) ((v) << 0)
  56. #define BIB_CRC_LENGTH(v) ((v) << 16)
  57. #define BIB_INFO_LENGTH(v) ((v) << 24)
  58. #define BIB_BUS_NAME 0x31333934 /* "1394" */
  59. #define BIB_LINK_SPEED(v) ((v) << 0)
  60. #define BIB_GENERATION(v) ((v) << 4)
  61. #define BIB_MAX_ROM(v) ((v) << 8)
  62. #define BIB_MAX_RECEIVE(v) ((v) << 12)
  63. #define BIB_CYC_CLK_ACC(v) ((v) << 16)
  64. #define BIB_PMC ((1) << 27)
  65. #define BIB_BMC ((1) << 28)
  66. #define BIB_ISC ((1) << 29)
  67. #define BIB_CMC ((1) << 30)
  68. #define BIB_IRMC ((1) << 31)
  69. #define NODE_CAPABILITIES 0x0c0083c0 /* per IEEE 1394 clause 8.3.2.6.5.2 */
  70. /*
  71. * IEEE-1394 specifies a default SPLIT_TIMEOUT value of 800 cycles (100 ms),
  72. * but we have to make it longer because there are many devices whose firmware
  73. * is just too slow for that.
  74. */
  75. #define DEFAULT_SPLIT_TIMEOUT (2 * 8000)
  76. static void generate_config_rom(struct fw_card *card, __be32 *config_rom)
  77. {
  78. struct fw_descriptor *desc;
  79. int i, j, k, length;
  80. /*
  81. * Initialize contents of config rom buffer. On the OHCI
  82. * controller, block reads to the config rom accesses the host
  83. * memory, but quadlet read access the hardware bus info block
  84. * registers. That's just crack, but it means we should make
  85. * sure the contents of bus info block in host memory matches
  86. * the version stored in the OHCI registers.
  87. */
  88. config_rom[0] = cpu_to_be32(
  89. BIB_CRC_LENGTH(4) | BIB_INFO_LENGTH(4) | BIB_CRC(0));
  90. config_rom[1] = cpu_to_be32(BIB_BUS_NAME);
  91. config_rom[2] = cpu_to_be32(
  92. BIB_LINK_SPEED(card->link_speed) |
  93. BIB_GENERATION(card->config_rom_generation++ % 14 + 2) |
  94. BIB_MAX_ROM(2) |
  95. BIB_MAX_RECEIVE(card->max_receive) |
  96. BIB_BMC | BIB_ISC | BIB_CMC | BIB_IRMC);
  97. config_rom[3] = cpu_to_be32(card->guid >> 32);
  98. config_rom[4] = cpu_to_be32(card->guid);
  99. /* Generate root directory. */
  100. config_rom[6] = cpu_to_be32(NODE_CAPABILITIES);
  101. i = 7;
  102. j = 7 + descriptor_count;
  103. /* Generate root directory entries for descriptors. */
  104. list_for_each_entry (desc, &descriptor_list, link) {
  105. if (desc->immediate > 0)
  106. config_rom[i++] = cpu_to_be32(desc->immediate);
  107. config_rom[i] = cpu_to_be32(desc->key | (j - i));
  108. i++;
  109. j += desc->length;
  110. }
  111. /* Update root directory length. */
  112. config_rom[5] = cpu_to_be32((i - 5 - 1) << 16);
  113. /* End of root directory, now copy in descriptors. */
  114. list_for_each_entry (desc, &descriptor_list, link) {
  115. for (k = 0; k < desc->length; k++)
  116. config_rom[i + k] = cpu_to_be32(desc->data[k]);
  117. i += desc->length;
  118. }
  119. /* Calculate CRCs for all blocks in the config rom. This
  120. * assumes that CRC length and info length are identical for
  121. * the bus info block, which is always the case for this
  122. * implementation. */
  123. for (i = 0; i < j; i += length + 1)
  124. length = fw_compute_block_crc(config_rom + i);
  125. WARN_ON(j != config_rom_length);
  126. }
  127. static void update_config_roms(void)
  128. {
  129. struct fw_card *card;
  130. list_for_each_entry (card, &card_list, link) {
  131. generate_config_rom(card, tmp_config_rom);
  132. card->driver->set_config_rom(card, tmp_config_rom,
  133. config_rom_length);
  134. }
  135. }
  136. static size_t required_space(struct fw_descriptor *desc)
  137. {
  138. /* descriptor + entry into root dir + optional immediate entry */
  139. return desc->length + 1 + (desc->immediate > 0 ? 1 : 0);
  140. }
  141. int fw_core_add_descriptor(struct fw_descriptor *desc)
  142. {
  143. size_t i;
  144. /*
  145. * Check descriptor is valid; the length of all blocks in the
  146. * descriptor has to add up to exactly the length of the
  147. * block.
  148. */
  149. i = 0;
  150. while (i < desc->length)
  151. i += (desc->data[i] >> 16) + 1;
  152. if (i != desc->length)
  153. return -EINVAL;
  154. guard(mutex)(&card_mutex);
  155. if (config_rom_length + required_space(desc) > 256)
  156. return -EBUSY;
  157. list_add_tail(&desc->link, &descriptor_list);
  158. config_rom_length += required_space(desc);
  159. descriptor_count++;
  160. if (desc->immediate > 0)
  161. descriptor_count++;
  162. update_config_roms();
  163. return 0;
  164. }
  165. EXPORT_SYMBOL(fw_core_add_descriptor);
  166. void fw_core_remove_descriptor(struct fw_descriptor *desc)
  167. {
  168. guard(mutex)(&card_mutex);
  169. list_del(&desc->link);
  170. config_rom_length -= required_space(desc);
  171. descriptor_count--;
  172. if (desc->immediate > 0)
  173. descriptor_count--;
  174. update_config_roms();
  175. }
  176. EXPORT_SYMBOL(fw_core_remove_descriptor);
  177. static int reset_bus(struct fw_card *card, bool short_reset)
  178. {
  179. int reg = short_reset ? 5 : 1;
  180. int bit = short_reset ? PHY_BUS_SHORT_RESET : PHY_BUS_RESET;
  181. trace_bus_reset_initiate(card->index, card->generation, short_reset);
  182. return card->driver->update_phy_reg(card, reg, 0, bit);
  183. }
  184. void fw_schedule_bus_reset(struct fw_card *card, bool delayed, bool short_reset)
  185. {
  186. trace_bus_reset_schedule(card->index, card->generation, short_reset);
  187. /* We don't try hard to sort out requests of long vs. short resets. */
  188. card->br_short = short_reset;
  189. /* Use an arbitrary short delay to combine multiple reset requests. */
  190. fw_card_get(card);
  191. if (!queue_delayed_work(fw_workqueue, &card->br_work, delayed ? msecs_to_jiffies(10) : 0))
  192. fw_card_put(card);
  193. }
  194. EXPORT_SYMBOL(fw_schedule_bus_reset);
  195. static void br_work(struct work_struct *work)
  196. {
  197. struct fw_card *card = from_work(card, work, br_work.work);
  198. /* Delay for 2s after last reset per IEEE 1394 clause 8.2.1. */
  199. if (card->reset_jiffies != 0 &&
  200. time_is_after_jiffies64(card->reset_jiffies + secs_to_jiffies(2))) {
  201. trace_bus_reset_postpone(card->index, card->generation, card->br_short);
  202. if (!queue_delayed_work(fw_workqueue, &card->br_work, secs_to_jiffies(2)))
  203. fw_card_put(card);
  204. return;
  205. }
  206. fw_send_phy_config(card, FW_PHY_CONFIG_NO_NODE_ID, card->generation,
  207. FW_PHY_CONFIG_CURRENT_GAP_COUNT);
  208. reset_bus(card, card->br_short);
  209. fw_card_put(card);
  210. }
  211. static void allocate_broadcast_channel(struct fw_card *card, int generation)
  212. {
  213. int channel, bandwidth = 0;
  214. if (!card->broadcast_channel_allocated) {
  215. fw_iso_resource_manage(card, generation, 1ULL << 31,
  216. &channel, &bandwidth, true);
  217. if (channel != 31) {
  218. fw_notice(card, "failed to allocate broadcast channel\n");
  219. return;
  220. }
  221. card->broadcast_channel_allocated = true;
  222. }
  223. device_for_each_child(card->device, (void *)(long)generation,
  224. fw_device_set_broadcast_channel);
  225. }
  226. void fw_schedule_bm_work(struct fw_card *card, unsigned long delay)
  227. {
  228. fw_card_get(card);
  229. if (!schedule_delayed_work(&card->bm_work, delay))
  230. fw_card_put(card);
  231. }
  232. enum bm_contention_outcome {
  233. // The bus management contention window is not expired.
  234. BM_CONTENTION_OUTCOME_WITHIN_WINDOW = 0,
  235. // The IRM node has link off.
  236. BM_CONTENTION_OUTCOME_IRM_HAS_LINK_OFF,
  237. // The IRM node complies IEEE 1394:1994 only.
  238. BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY,
  239. // Another bus reset, BM work has been rescheduled.
  240. BM_CONTENTION_OUTCOME_AT_NEW_GENERATION,
  241. // We have been unable to send the lock request to IRM node due to some local problem.
  242. BM_CONTENTION_OUTCOME_LOCAL_PROBLEM_AT_TRANSACTION,
  243. // The lock request failed, maybe the IRM isn't really IRM capable after all.
  244. BM_CONTENTION_OUTCOME_IRM_IS_NOT_CAPABLE_FOR_IRM,
  245. // Somebody else is BM.
  246. BM_CONTENTION_OUTCOME_IRM_HOLDS_ANOTHER_NODE_AS_BM,
  247. // The local node succeeds after contending for bus manager.
  248. BM_CONTENTION_OUTCOME_IRM_HOLDS_LOCAL_NODE_AS_BM,
  249. };
  250. static enum bm_contention_outcome contend_for_bm(struct fw_card *card)
  251. __must_hold(&card->lock)
  252. {
  253. int generation = card->generation;
  254. int local_id = card->local_node->node_id;
  255. __be32 data[2] = {
  256. cpu_to_be32(BUS_MANAGER_ID_NOT_REGISTERED),
  257. cpu_to_be32(local_id),
  258. };
  259. bool grace = time_is_before_jiffies64(card->reset_jiffies + msecs_to_jiffies(125));
  260. struct fw_node *irm_node;
  261. struct fw_device *irm_device;
  262. int irm_node_id, irm_device_quirks = 0;
  263. int rcode;
  264. lockdep_assert_held(&card->lock);
  265. if (!grace) {
  266. if (!is_next_generation(generation, card->bm_generation) || card->bm_abdicate)
  267. return BM_CONTENTION_OUTCOME_WITHIN_WINDOW;
  268. }
  269. irm_node = card->irm_node;
  270. if (!irm_node->link_on) {
  271. fw_notice(card, "IRM has link off, making local node (%02x) root\n", local_id);
  272. return BM_CONTENTION_OUTCOME_IRM_HAS_LINK_OFF;
  273. }
  274. // NOTE: It is likely that the quirk detection for IRM device has not done yet.
  275. irm_device = fw_node_get_device(irm_node);
  276. if (irm_device)
  277. irm_device_quirks = READ_ONCE(irm_device->quirks);
  278. if ((irm_device_quirks & FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY) &&
  279. !(irm_device_quirks & FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER)) {
  280. fw_notice(card, "IRM is not 1394a compliant, making local node (%02x) root\n",
  281. local_id);
  282. return BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY;
  283. }
  284. irm_node_id = irm_node->node_id;
  285. spin_unlock_irq(&card->lock);
  286. rcode = fw_run_transaction(card, TCODE_LOCK_COMPARE_SWAP, irm_node_id, generation,
  287. SCODE_100, CSR_REGISTER_BASE + CSR_BUS_MANAGER_ID, data,
  288. sizeof(data));
  289. spin_lock_irq(&card->lock);
  290. switch (rcode) {
  291. case RCODE_GENERATION:
  292. return BM_CONTENTION_OUTCOME_AT_NEW_GENERATION;
  293. case RCODE_SEND_ERROR:
  294. return BM_CONTENTION_OUTCOME_LOCAL_PROBLEM_AT_TRANSACTION;
  295. case RCODE_COMPLETE:
  296. {
  297. int bm_id = be32_to_cpu(data[0]);
  298. // Used by cdev layer for "struct fw_cdev_event_bus_reset".
  299. if (bm_id != BUS_MANAGER_ID_NOT_REGISTERED)
  300. card->bm_node_id = 0xffc0 & bm_id;
  301. else
  302. card->bm_node_id = local_id;
  303. if (bm_id != BUS_MANAGER_ID_NOT_REGISTERED)
  304. return BM_CONTENTION_OUTCOME_IRM_HOLDS_ANOTHER_NODE_AS_BM;
  305. else
  306. return BM_CONTENTION_OUTCOME_IRM_HOLDS_LOCAL_NODE_AS_BM;
  307. }
  308. default:
  309. if (!(irm_device_quirks & FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER)) {
  310. fw_notice(card, "BM lock failed (%s), making local node (%02x) root\n",
  311. fw_rcode_string(rcode), local_id);
  312. return BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY;
  313. } else {
  314. return BM_CONTENTION_OUTCOME_IRM_IS_NOT_CAPABLE_FOR_IRM;
  315. }
  316. }
  317. }
  318. DEFINE_FREE(node_unref, struct fw_node *, if (_T) fw_node_put(_T))
  319. DEFINE_FREE(card_unref, struct fw_card *, if (_T) fw_card_put(_T))
  320. static void bm_work(struct work_struct *work)
  321. {
  322. static const char gap_count_table[] = {
  323. 63, 5, 7, 8, 10, 13, 16, 18, 21, 24, 26, 29, 32, 35, 37, 40
  324. };
  325. struct fw_card *card __free(card_unref) = from_work(card, work, bm_work.work);
  326. struct fw_node *root_node __free(node_unref) = NULL;
  327. int root_id, new_root_id, irm_id, local_id;
  328. int expected_gap_count, generation;
  329. bool stand_for_root = false;
  330. spin_lock_irq(&card->lock);
  331. if (card->local_node == NULL) {
  332. spin_unlock_irq(&card->lock);
  333. return;
  334. }
  335. generation = card->generation;
  336. root_node = fw_node_get(card->root_node);
  337. root_id = root_node->node_id;
  338. irm_id = card->irm_node->node_id;
  339. local_id = card->local_node->node_id;
  340. if (card->bm_generation != generation) {
  341. enum bm_contention_outcome result = contend_for_bm(card);
  342. switch (result) {
  343. case BM_CONTENTION_OUTCOME_WITHIN_WINDOW:
  344. spin_unlock_irq(&card->lock);
  345. fw_schedule_bm_work(card, msecs_to_jiffies(125));
  346. return;
  347. case BM_CONTENTION_OUTCOME_IRM_HAS_LINK_OFF:
  348. stand_for_root = true;
  349. break;
  350. case BM_CONTENTION_OUTCOME_IRM_COMPLIES_1394_1995_ONLY:
  351. stand_for_root = true;
  352. break;
  353. case BM_CONTENTION_OUTCOME_AT_NEW_GENERATION:
  354. // BM work has been rescheduled.
  355. spin_unlock_irq(&card->lock);
  356. return;
  357. case BM_CONTENTION_OUTCOME_LOCAL_PROBLEM_AT_TRANSACTION:
  358. // Let's try again later and hope that the local problem has gone away by
  359. // then.
  360. spin_unlock_irq(&card->lock);
  361. fw_schedule_bm_work(card, msecs_to_jiffies(125));
  362. return;
  363. case BM_CONTENTION_OUTCOME_IRM_IS_NOT_CAPABLE_FOR_IRM:
  364. // Let's do a bus reset and pick the local node as root, and thus, IRM.
  365. stand_for_root = true;
  366. break;
  367. case BM_CONTENTION_OUTCOME_IRM_HOLDS_ANOTHER_NODE_AS_BM:
  368. if (local_id == irm_id) {
  369. // Only acts as IRM.
  370. spin_unlock_irq(&card->lock);
  371. allocate_broadcast_channel(card, generation);
  372. spin_lock_irq(&card->lock);
  373. }
  374. fallthrough;
  375. case BM_CONTENTION_OUTCOME_IRM_HOLDS_LOCAL_NODE_AS_BM:
  376. default:
  377. card->bm_generation = generation;
  378. break;
  379. }
  380. }
  381. // We're bus manager for this generation, so next step is to make sure we have an active
  382. // cycle master and do gap count optimization.
  383. if (!stand_for_root) {
  384. if (card->gap_count == GAP_COUNT_MISMATCHED) {
  385. // If self IDs have inconsistent gap counts, do a
  386. // bus reset ASAP. The config rom read might never
  387. // complete, so don't wait for it. However, still
  388. // send a PHY configuration packet prior to the
  389. // bus reset. The PHY configuration packet might
  390. // fail, but 1394-2008 8.4.5.2 explicitly permits
  391. // it in this case, so it should be safe to try.
  392. stand_for_root = true;
  393. // We must always send a bus reset if the gap count
  394. // is inconsistent, so bypass the 5-reset limit.
  395. card->bm_retries = 0;
  396. } else {
  397. // Now investigate root node.
  398. struct fw_device *root_device = fw_node_get_device(root_node);
  399. if (root_device == NULL) {
  400. // Either link_on is false, or we failed to read the
  401. // config rom. In either case, pick another root.
  402. stand_for_root = true;
  403. } else {
  404. bool root_device_is_running =
  405. atomic_read(&root_device->state) == FW_DEVICE_RUNNING;
  406. if (!root_device_is_running) {
  407. // If we haven't probed this device yet, bail out now
  408. // and let's try again once that's done.
  409. spin_unlock_irq(&card->lock);
  410. return;
  411. } else if (!root_device->cmc) {
  412. // Current root has an active link layer and we
  413. // successfully read the config rom, but it's not
  414. // cycle master capable.
  415. stand_for_root = true;
  416. }
  417. }
  418. }
  419. }
  420. if (stand_for_root) {
  421. new_root_id = local_id;
  422. } else {
  423. // We will send out a force root packet for this node as part of the gap count
  424. // optimization on behalf of the node.
  425. new_root_id = root_id;
  426. }
  427. /*
  428. * Pick a gap count from 1394a table E-1. The table doesn't cover
  429. * the typically much larger 1394b beta repeater delays though.
  430. */
  431. if (!card->beta_repeaters_present &&
  432. root_node->max_hops < ARRAY_SIZE(gap_count_table))
  433. expected_gap_count = gap_count_table[root_node->max_hops];
  434. else
  435. expected_gap_count = 63;
  436. // Finally, figure out if we should do a reset or not. If we have done less than 5 resets
  437. // with the same physical topology and we have either a new root or a new gap count
  438. // setting, let's do it.
  439. if (card->bm_retries++ < 5 && (card->gap_count != expected_gap_count || new_root_id != root_id)) {
  440. int card_gap_count = card->gap_count;
  441. spin_unlock_irq(&card->lock);
  442. fw_notice(card, "phy config: new root=%x, gap_count=%d\n",
  443. new_root_id, expected_gap_count);
  444. fw_send_phy_config(card, new_root_id, generation, expected_gap_count);
  445. /*
  446. * Where possible, use a short bus reset to minimize
  447. * disruption to isochronous transfers. But in the event
  448. * of a gap count inconsistency, use a long bus reset.
  449. *
  450. * As noted in 1394a 8.4.6.2, nodes on a mixed 1394/1394a bus
  451. * may set different gap counts after a bus reset. On a mixed
  452. * 1394/1394a bus, a short bus reset can get doubled. Some
  453. * nodes may treat the double reset as one bus reset and others
  454. * may treat it as two, causing a gap count inconsistency
  455. * again. Using a long bus reset prevents this.
  456. */
  457. reset_bus(card, card_gap_count != 0);
  458. /* Will allocate broadcast channel after the reset. */
  459. } else {
  460. struct fw_device *root_device = fw_node_get_device(root_node);
  461. spin_unlock_irq(&card->lock);
  462. if (root_device && root_device->cmc) {
  463. // Make sure that the cycle master sends cycle start packets.
  464. __be32 data = cpu_to_be32(CSR_STATE_BIT_CMSTR);
  465. int rcode = fw_run_transaction(card, TCODE_WRITE_QUADLET_REQUEST,
  466. root_id, generation, SCODE_100,
  467. CSR_REGISTER_BASE + CSR_STATE_SET,
  468. &data, sizeof(data));
  469. if (rcode == RCODE_GENERATION)
  470. return;
  471. }
  472. if (local_id == irm_id)
  473. allocate_broadcast_channel(card, generation);
  474. }
  475. }
  476. void fw_card_initialize(struct fw_card *card,
  477. const struct fw_card_driver *driver,
  478. struct device *device)
  479. {
  480. static atomic_t index = ATOMIC_INIT(-1);
  481. card->index = atomic_inc_return(&index);
  482. card->driver = driver;
  483. card->device = device;
  484. card->transactions.current_tlabel = 0;
  485. card->transactions.tlabel_mask = 0;
  486. INIT_LIST_HEAD(&card->transactions.list);
  487. spin_lock_init(&card->transactions.lock);
  488. spin_lock_init(&card->topology_map.lock);
  489. card->split_timeout.hi = DEFAULT_SPLIT_TIMEOUT / 8000;
  490. card->split_timeout.lo = (DEFAULT_SPLIT_TIMEOUT % 8000) << 19;
  491. card->split_timeout.cycles = DEFAULT_SPLIT_TIMEOUT;
  492. card->split_timeout.jiffies = isoc_cycles_to_jiffies(DEFAULT_SPLIT_TIMEOUT);
  493. spin_lock_init(&card->split_timeout.lock);
  494. card->color = 0;
  495. card->broadcast_channel = BROADCAST_CHANNEL_INITIAL;
  496. kref_init(&card->kref);
  497. init_completion(&card->done);
  498. spin_lock_init(&card->lock);
  499. card->local_node = NULL;
  500. INIT_DELAYED_WORK(&card->br_work, br_work);
  501. INIT_DELAYED_WORK(&card->bm_work, bm_work);
  502. }
  503. EXPORT_SYMBOL(fw_card_initialize);
  504. DEFINE_FREE(workqueue_destroy, struct workqueue_struct *, if (_T) destroy_workqueue(_T))
  505. int fw_card_add(struct fw_card *card, u32 max_receive, u32 link_speed, u64 guid,
  506. unsigned int supported_isoc_contexts)
  507. {
  508. struct workqueue_struct *isoc_wq __free(workqueue_destroy) = NULL;
  509. struct workqueue_struct *async_wq __free(workqueue_destroy) = NULL;
  510. int ret;
  511. // This workqueue should be:
  512. // * != WQ_BH Sleepable.
  513. // * == WQ_UNBOUND Any core can process data for isoc context. The
  514. // implementation of unit protocol could consumes the core
  515. // longer somehow.
  516. // * != WQ_MEM_RECLAIM Not used for any backend of block device.
  517. // * == WQ_FREEZABLE Isochronous communication is at regular interval in real
  518. // time, thus should be drained if possible at freeze phase.
  519. // * == WQ_HIGHPRI High priority to process semi-realtime timestamped data.
  520. // * == WQ_SYSFS Parameters are available via sysfs.
  521. // * max_active == n_it + n_ir A hardIRQ could notify events for multiple isochronous
  522. // contexts if they are scheduled to the same cycle.
  523. isoc_wq = alloc_workqueue("firewire-isoc-card%u",
  524. WQ_UNBOUND | WQ_FREEZABLE | WQ_HIGHPRI | WQ_SYSFS,
  525. supported_isoc_contexts, card->index);
  526. if (!isoc_wq)
  527. return -ENOMEM;
  528. // This workqueue should be:
  529. // * != WQ_BH Sleepable.
  530. // * == WQ_UNBOUND Any core can process data for asynchronous context.
  531. // * == WQ_MEM_RECLAIM Used for any backend of block device.
  532. // * == WQ_FREEZABLE The target device would not be available when being freezed.
  533. // * == WQ_HIGHPRI High priority to process semi-realtime timestamped data.
  534. // * == WQ_SYSFS Parameters are available via sysfs.
  535. // * max_active == 4 A hardIRQ could notify events for a pair of requests and
  536. // response AR/AT contexts.
  537. async_wq = alloc_workqueue("firewire-async-card%u",
  538. WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_HIGHPRI | WQ_SYSFS,
  539. 4, card->index);
  540. if (!async_wq)
  541. return -ENOMEM;
  542. card->isoc_wq = isoc_wq;
  543. card->async_wq = async_wq;
  544. card->max_receive = max_receive;
  545. card->link_speed = link_speed;
  546. card->guid = guid;
  547. scoped_guard(mutex, &card_mutex) {
  548. generate_config_rom(card, tmp_config_rom);
  549. ret = card->driver->enable(card, tmp_config_rom, config_rom_length);
  550. if (ret < 0) {
  551. card->isoc_wq = NULL;
  552. card->async_wq = NULL;
  553. return ret;
  554. }
  555. retain_and_null_ptr(isoc_wq);
  556. retain_and_null_ptr(async_wq);
  557. list_add_tail(&card->link, &card_list);
  558. }
  559. return 0;
  560. }
  561. EXPORT_SYMBOL(fw_card_add);
  562. /*
  563. * The next few functions implement a dummy driver that is used once a card
  564. * driver shuts down an fw_card. This allows the driver to cleanly unload,
  565. * as all IO to the card will be handled (and failed) by the dummy driver
  566. * instead of calling into the module. Only functions for iso context
  567. * shutdown still need to be provided by the card driver.
  568. *
  569. * .read/write_csr() should never be called anymore after the dummy driver
  570. * was bound since they are only used within request handler context.
  571. * .set_config_rom() is never called since the card is taken out of card_list
  572. * before switching to the dummy driver.
  573. */
  574. static int dummy_read_phy_reg(struct fw_card *card, int address)
  575. {
  576. return -ENODEV;
  577. }
  578. static int dummy_update_phy_reg(struct fw_card *card, int address,
  579. int clear_bits, int set_bits)
  580. {
  581. return -ENODEV;
  582. }
  583. static void dummy_send_request(struct fw_card *card, struct fw_packet *packet)
  584. {
  585. packet->callback(packet, card, RCODE_CANCELLED);
  586. }
  587. static void dummy_send_response(struct fw_card *card, struct fw_packet *packet)
  588. {
  589. packet->callback(packet, card, RCODE_CANCELLED);
  590. }
  591. static int dummy_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  592. {
  593. return -ENOENT;
  594. }
  595. static int dummy_enable_phys_dma(struct fw_card *card,
  596. int node_id, int generation)
  597. {
  598. return -ENODEV;
  599. }
  600. static struct fw_iso_context *dummy_allocate_iso_context(struct fw_card *card, int type,
  601. int channel, size_t header_size, size_t header_storage_size)
  602. {
  603. return ERR_PTR(-ENODEV);
  604. }
  605. static u32 dummy_read_csr(struct fw_card *card, int csr_offset)
  606. {
  607. return 0;
  608. }
  609. static void dummy_write_csr(struct fw_card *card, int csr_offset, u32 value)
  610. {
  611. }
  612. static int dummy_start_iso(struct fw_iso_context *ctx,
  613. s32 cycle, u32 sync, u32 tags)
  614. {
  615. return -ENODEV;
  616. }
  617. static int dummy_set_iso_channels(struct fw_iso_context *ctx, u64 *channels)
  618. {
  619. return -ENODEV;
  620. }
  621. static int dummy_queue_iso(struct fw_iso_context *ctx, struct fw_iso_packet *p,
  622. struct fw_iso_buffer *buffer, unsigned long payload)
  623. {
  624. return -ENODEV;
  625. }
  626. static void dummy_flush_queue_iso(struct fw_iso_context *ctx)
  627. {
  628. }
  629. static int dummy_flush_iso_completions(struct fw_iso_context *ctx)
  630. {
  631. return -ENODEV;
  632. }
  633. static const struct fw_card_driver dummy_driver_template = {
  634. .read_phy_reg = dummy_read_phy_reg,
  635. .update_phy_reg = dummy_update_phy_reg,
  636. .send_request = dummy_send_request,
  637. .send_response = dummy_send_response,
  638. .cancel_packet = dummy_cancel_packet,
  639. .enable_phys_dma = dummy_enable_phys_dma,
  640. .read_csr = dummy_read_csr,
  641. .write_csr = dummy_write_csr,
  642. .allocate_iso_context = dummy_allocate_iso_context,
  643. .start_iso = dummy_start_iso,
  644. .set_iso_channels = dummy_set_iso_channels,
  645. .queue_iso = dummy_queue_iso,
  646. .flush_queue_iso = dummy_flush_queue_iso,
  647. .flush_iso_completions = dummy_flush_iso_completions,
  648. };
  649. void fw_card_release(struct kref *kref)
  650. {
  651. struct fw_card *card = container_of(kref, struct fw_card, kref);
  652. complete(&card->done);
  653. }
  654. EXPORT_SYMBOL_GPL(fw_card_release);
  655. void fw_core_remove_card(struct fw_card *card)
  656. {
  657. struct fw_card_driver dummy_driver = dummy_driver_template;
  658. might_sleep();
  659. card->driver->update_phy_reg(card, 4,
  660. PHY_LINK_ACTIVE | PHY_CONTENDER, 0);
  661. fw_schedule_bus_reset(card, false, true);
  662. scoped_guard(mutex, &card_mutex)
  663. list_del_init(&card->link);
  664. /* Switch off most of the card driver interface. */
  665. dummy_driver.free_iso_context = card->driver->free_iso_context;
  666. dummy_driver.stop_iso = card->driver->stop_iso;
  667. dummy_driver.disable = card->driver->disable;
  668. card->driver = &dummy_driver;
  669. drain_workqueue(card->isoc_wq);
  670. drain_workqueue(card->async_wq);
  671. card->driver->disable(card);
  672. fw_cancel_pending_transactions(card);
  673. scoped_guard(spinlock_irqsave, &card->lock)
  674. fw_destroy_nodes(card);
  675. /* Wait for all users, especially device workqueue jobs, to finish. */
  676. fw_card_put(card);
  677. wait_for_completion(&card->done);
  678. destroy_workqueue(card->isoc_wq);
  679. destroy_workqueue(card->async_wq);
  680. WARN_ON(!list_empty(&card->transactions.list));
  681. }
  682. EXPORT_SYMBOL(fw_core_remove_card);
  683. /**
  684. * fw_card_read_cycle_time: read from Isochronous Cycle Timer Register of 1394 OHCI in MMIO region
  685. * for controller card.
  686. * @card: The instance of card for 1394 OHCI controller.
  687. * @cycle_time: The mutual reference to value of cycle time for the read operation.
  688. *
  689. * Read value from Isochronous Cycle Timer Register of 1394 OHCI in MMIO region for the given
  690. * controller card. This function accesses the region without any lock primitives or IRQ mask.
  691. * When returning successfully, the content of @value argument has value aligned to host endianness,
  692. * formetted by CYCLE_TIME CSR Register of IEEE 1394 std.
  693. *
  694. * Context: Any context.
  695. * Return:
  696. * * 0 - Read successfully.
  697. * * -ENODEV - The controller is unavailable due to being removed or unbound.
  698. */
  699. int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time)
  700. {
  701. if (card->driver->read_csr == dummy_read_csr)
  702. return -ENODEV;
  703. // It's possible to switch to dummy driver between the above and the below. This is the best
  704. // effort to return -ENODEV.
  705. *cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
  706. return 0;
  707. }
  708. EXPORT_SYMBOL_GPL(fw_card_read_cycle_time);