core.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _FIREWIRE_CORE_H
  3. #define _FIREWIRE_CORE_H
  4. #include <linux/compiler.h>
  5. #include <linux/device.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/fs.h>
  8. #include <linux/list.h>
  9. #include <linux/xarray.h>
  10. #include <linux/mm_types.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/refcount.h>
  15. struct device;
  16. struct fw_card;
  17. struct fw_device;
  18. struct fw_iso_buffer;
  19. struct fw_iso_context;
  20. struct fw_iso_packet;
  21. struct fw_node;
  22. struct fw_packet;
  23. /* -card */
  24. // This is the arbitrary value we use to indicate a mismatched gap count.
  25. #define GAP_COUNT_MISMATCHED 0
  26. #define isoc_cycles_to_jiffies(cycles) usecs_to_jiffies((u32)div_u64((u64)cycles * USEC_PER_SEC, 8000))
  27. extern __printf(2, 3)
  28. void fw_err(const struct fw_card *card, const char *fmt, ...);
  29. extern __printf(2, 3)
  30. void fw_notice(const struct fw_card *card, const char *fmt, ...);
  31. /* bitfields within the PHY registers */
  32. #define PHY_LINK_ACTIVE 0x80
  33. #define PHY_CONTENDER 0x40
  34. #define PHY_BUS_RESET 0x40
  35. #define PHY_EXTENDED_REGISTERS 0xe0
  36. #define PHY_BUS_SHORT_RESET 0x40
  37. #define PHY_INT_STATUS_BITS 0x3c
  38. #define PHY_ENABLE_ACCEL 0x02
  39. #define PHY_ENABLE_MULTI 0x01
  40. #define PHY_PAGE_SELECT 0xe0
  41. #define BANDWIDTH_AVAILABLE_INITIAL 4915
  42. #define BROADCAST_CHANNEL_INITIAL (1 << 31 | 31)
  43. #define BROADCAST_CHANNEL_VALID (1 << 30)
  44. #define CSR_STATE_BIT_CMSTR (1 << 8)
  45. #define CSR_STATE_BIT_ABDICATE (1 << 10)
  46. struct fw_card_driver {
  47. /*
  48. * Enable the given card with the given initial config rom.
  49. * This function is expected to activate the card, and either
  50. * enable the PHY or set the link_on bit and initiate a bus
  51. * reset.
  52. */
  53. int (*enable)(struct fw_card *card,
  54. const __be32 *config_rom, size_t length);
  55. // After returning the call, any function is no longer triggered to handle hardware event.
  56. void (*disable)(struct fw_card *card);
  57. int (*read_phy_reg)(struct fw_card *card, int address);
  58. int (*update_phy_reg)(struct fw_card *card, int address,
  59. int clear_bits, int set_bits);
  60. /*
  61. * Update the config rom for an enabled card. This function
  62. * should change the config rom that is presented on the bus
  63. * and initiate a bus reset.
  64. */
  65. int (*set_config_rom)(struct fw_card *card,
  66. const __be32 *config_rom, size_t length);
  67. void (*send_request)(struct fw_card *card, struct fw_packet *packet);
  68. void (*send_response)(struct fw_card *card, struct fw_packet *packet);
  69. /* Calling cancel is valid once a packet has been submitted. */
  70. int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
  71. /*
  72. * Allow the specified node ID to do direct DMA out and in of
  73. * host memory. The card will disable this for all node when
  74. * a bus reset happens, so driver need to re-enable this after
  75. * bus reset. Returns 0 on success, -ENODEV if the card
  76. * doesn't support this, -ESTALE if the generation doesn't
  77. * match.
  78. */
  79. int (*enable_phys_dma)(struct fw_card *card,
  80. int node_id, int generation);
  81. u32 (*read_csr)(struct fw_card *card, int csr_offset);
  82. void (*write_csr)(struct fw_card *card, int csr_offset, u32 value);
  83. struct fw_iso_context *
  84. (*allocate_iso_context)(struct fw_card *card, int type, int channel, size_t header_size,
  85. size_t header_storage_size);
  86. void (*free_iso_context)(struct fw_iso_context *ctx);
  87. int (*start_iso)(struct fw_iso_context *ctx,
  88. s32 cycle, u32 sync, u32 tags);
  89. int (*set_iso_channels)(struct fw_iso_context *ctx, u64 *channels);
  90. int (*queue_iso)(struct fw_iso_context *ctx,
  91. struct fw_iso_packet *packet,
  92. struct fw_iso_buffer *buffer,
  93. unsigned long payload);
  94. void (*flush_queue_iso)(struct fw_iso_context *ctx);
  95. int (*flush_iso_completions)(struct fw_iso_context *ctx);
  96. int (*stop_iso)(struct fw_iso_context *ctx);
  97. };
  98. void fw_card_initialize(struct fw_card *card,
  99. const struct fw_card_driver *driver, struct device *device);
  100. int fw_card_add(struct fw_card *card, u32 max_receive, u32 link_speed, u64 guid,
  101. unsigned int supported_isoc_contexts);
  102. void fw_core_remove_card(struct fw_card *card);
  103. int fw_compute_block_crc(__be32 *block);
  104. void fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
  105. /* -cdev */
  106. extern const struct file_operations fw_device_ops;
  107. void fw_device_cdev_update(struct fw_device *device);
  108. void fw_device_cdev_remove(struct fw_device *device);
  109. void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p);
  110. /* -device */
  111. extern struct rw_semaphore fw_device_rwsem;
  112. extern struct xarray fw_device_xa;
  113. extern int fw_cdev_major;
  114. static inline struct fw_device *fw_device_get(struct fw_device *device)
  115. {
  116. get_device(&device->device);
  117. return device;
  118. }
  119. static inline void fw_device_put(struct fw_device *device)
  120. {
  121. put_device(&device->device);
  122. }
  123. struct fw_device *fw_device_get_by_devt(dev_t devt);
  124. int fw_device_set_broadcast_channel(struct device *dev, void *gen);
  125. void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  126. /* -iso */
  127. int fw_iso_buffer_alloc(struct fw_iso_buffer *buffer, int page_count);
  128. int fw_iso_buffer_map_dma(struct fw_iso_buffer *buffer, struct fw_card *card,
  129. enum dma_data_direction direction);
  130. size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed);
  131. static inline void fw_iso_context_init_work(struct fw_iso_context *ctx, work_func_t func)
  132. {
  133. INIT_WORK(&ctx->work, func);
  134. }
  135. static inline struct fw_iso_context *fw_iso_mc_context_create(struct fw_card *card,
  136. fw_iso_mc_callback_t callback, void *callback_data)
  137. {
  138. union fw_iso_callback cb = { .mc = callback };
  139. return __fw_iso_context_create(card, FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL, 0, 0, 0, 0, cb,
  140. callback_data);
  141. }
  142. /* -topology */
  143. // The initial value of BUS_MANAGER_ID register, to express nothing registered.
  144. #define BUS_MANAGER_ID_NOT_REGISTERED 0x3f
  145. enum {
  146. FW_NODE_CREATED,
  147. FW_NODE_UPDATED,
  148. FW_NODE_DESTROYED,
  149. FW_NODE_LINK_ON,
  150. FW_NODE_LINK_OFF,
  151. FW_NODE_INITIATED_RESET,
  152. };
  153. struct fw_node {
  154. u16 node_id;
  155. u8 color;
  156. u8 port_count;
  157. u8 link_on:1;
  158. u8 initiated_reset:1;
  159. u8 b_path:1;
  160. u8 phy_speed:2; /* As in the self ID packet. */
  161. u8 max_speed:2; /* Minimum of all phy-speeds on the path from the
  162. * local node to this node. */
  163. u8 max_depth:4; /* Maximum depth to any leaf node */
  164. u8 max_hops:4; /* Max hops in this sub tree */
  165. struct kref kref;
  166. /* For serializing node topology into a list. */
  167. struct list_head link;
  168. // The device when already associated, else NULL.
  169. struct fw_device *device;
  170. struct fw_node *ports[] __counted_by(port_count);
  171. };
  172. static inline struct fw_node *fw_node_get(struct fw_node *node)
  173. {
  174. kref_get(&node->kref);
  175. return node;
  176. }
  177. static void release_node(struct kref *kref)
  178. {
  179. struct fw_node *node = container_of(kref, struct fw_node, kref);
  180. kfree(node);
  181. }
  182. static inline void fw_node_put(struct fw_node *node)
  183. {
  184. kref_put(&node->kref, release_node);
  185. }
  186. static inline struct fw_device *fw_node_get_device(struct fw_node *node)
  187. {
  188. return node->device;
  189. }
  190. static inline void fw_node_set_device(struct fw_node *node, struct fw_device *device)
  191. {
  192. node->device = device;
  193. }
  194. void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
  195. int generation, int self_id_count, u32 *self_ids, bool bm_abdicate);
  196. void fw_destroy_nodes(struct fw_card *card);
  197. /*
  198. * Check whether new_generation is the immediate successor of old_generation.
  199. * Take counter roll-over at 255 (as per OHCI) into account.
  200. */
  201. static inline bool is_next_generation(int new_generation, int old_generation)
  202. {
  203. return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
  204. }
  205. /* -transaction */
  206. #define TCODE_LINK_INTERNAL 0xe
  207. static inline bool tcode_is_read_request(unsigned int tcode)
  208. {
  209. return (tcode & ~1u) == 4u;
  210. }
  211. static inline bool tcode_is_block_packet(unsigned int tcode)
  212. {
  213. return (tcode & 1u) != 0u;
  214. }
  215. static inline bool tcode_is_link_internal(unsigned int tcode)
  216. {
  217. return (tcode == TCODE_LINK_INTERNAL);
  218. }
  219. #define LOCAL_BUS 0xffc0
  220. /* OHCI-1394's default upper bound for physical DMA: 4 GB */
  221. #define FW_MAX_PHYSICAL_RANGE (1ULL << 32)
  222. void fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  223. void fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  224. int fw_get_response_length(struct fw_request *request);
  225. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  226. int rcode, void *payload, size_t length);
  227. void fw_request_get(struct fw_request *request);
  228. void fw_request_put(struct fw_request *request);
  229. void fw_cancel_pending_transactions(struct fw_card *card);
  230. // Convert the value of IEEE 1394 CYCLE_TIME register to the format of timeStamp field in
  231. // descriptors of 1394 OHCI.
  232. static inline u32 cycle_time_to_ohci_tstamp(u32 tstamp)
  233. {
  234. return (tstamp & 0x0ffff000) >> 12;
  235. }
  236. #define FW_PHY_CONFIG_NO_NODE_ID -1
  237. #define FW_PHY_CONFIG_CURRENT_GAP_COUNT -1
  238. void fw_send_phy_config(struct fw_card *card,
  239. int node_id, int generation, int gap_count);
  240. static inline bool is_ping_packet(u32 *data)
  241. {
  242. return (data[0] & 0xc0ffffff) == 0 && ~data[0] == data[1];
  243. }
  244. static inline bool is_in_fcp_region(u64 offset, size_t length)
  245. {
  246. return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
  247. offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END);
  248. }
  249. #endif /* _FIREWIRE_CORE_H */