core.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef _FIREWIRE_CORE_H
  2. #define _FIREWIRE_CORE_H
  3. #include <linux/compiler.h>
  4. #include <linux/device.h>
  5. #include <linux/fs.h>
  6. #include <linux/list.h>
  7. #include <linux/idr.h>
  8. #include <linux/mm_types.h>
  9. #include <linux/rwsem.h>
  10. #include <linux/slab.h>
  11. #include <linux/types.h>
  12. #include <linux/atomic.h>
  13. struct device;
  14. struct fw_card;
  15. struct fw_device;
  16. struct fw_iso_buffer;
  17. struct fw_iso_context;
  18. struct fw_iso_packet;
  19. struct fw_node;
  20. struct fw_packet;
  21. /* -card */
  22. extern __printf(2, 3)
  23. void fw_err(const struct fw_card *card, const char *fmt, ...);
  24. extern __printf(2, 3)
  25. void fw_notice(const struct fw_card *card, const char *fmt, ...);
  26. /* bitfields within the PHY registers */
  27. #define PHY_LINK_ACTIVE 0x80
  28. #define PHY_CONTENDER 0x40
  29. #define PHY_BUS_RESET 0x40
  30. #define PHY_EXTENDED_REGISTERS 0xe0
  31. #define PHY_BUS_SHORT_RESET 0x40
  32. #define PHY_INT_STATUS_BITS 0x3c
  33. #define PHY_ENABLE_ACCEL 0x02
  34. #define PHY_ENABLE_MULTI 0x01
  35. #define PHY_PAGE_SELECT 0xe0
  36. #define BANDWIDTH_AVAILABLE_INITIAL 4915
  37. #define BROADCAST_CHANNEL_INITIAL (1 << 31 | 31)
  38. #define BROADCAST_CHANNEL_VALID (1 << 30)
  39. #define CSR_STATE_BIT_CMSTR (1 << 8)
  40. #define CSR_STATE_BIT_ABDICATE (1 << 10)
  41. struct fw_card_driver {
  42. /*
  43. * Enable the given card with the given initial config rom.
  44. * This function is expected to activate the card, and either
  45. * enable the PHY or set the link_on bit and initiate a bus
  46. * reset.
  47. */
  48. int (*enable)(struct fw_card *card,
  49. const __be32 *config_rom, size_t length);
  50. int (*read_phy_reg)(struct fw_card *card, int address);
  51. int (*update_phy_reg)(struct fw_card *card, int address,
  52. int clear_bits, int set_bits);
  53. /*
  54. * Update the config rom for an enabled card. This function
  55. * should change the config rom that is presented on the bus
  56. * and initiate a bus reset.
  57. */
  58. int (*set_config_rom)(struct fw_card *card,
  59. const __be32 *config_rom, size_t length);
  60. void (*send_request)(struct fw_card *card, struct fw_packet *packet);
  61. void (*send_response)(struct fw_card *card, struct fw_packet *packet);
  62. /* Calling cancel is valid once a packet has been submitted. */
  63. int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
  64. /*
  65. * Allow the specified node ID to do direct DMA out and in of
  66. * host memory. The card will disable this for all node when
  67. * a bus reset happens, so driver need to reenable this after
  68. * bus reset. Returns 0 on success, -ENODEV if the card
  69. * doesn't support this, -ESTALE if the generation doesn't
  70. * match.
  71. */
  72. int (*enable_phys_dma)(struct fw_card *card,
  73. int node_id, int generation);
  74. u32 (*read_csr)(struct fw_card *card, int csr_offset);
  75. void (*write_csr)(struct fw_card *card, int csr_offset, u32 value);
  76. struct fw_iso_context *
  77. (*allocate_iso_context)(struct fw_card *card,
  78. int type, int channel, size_t header_size);
  79. void (*free_iso_context)(struct fw_iso_context *ctx);
  80. int (*start_iso)(struct fw_iso_context *ctx,
  81. s32 cycle, u32 sync, u32 tags);
  82. int (*set_iso_channels)(struct fw_iso_context *ctx, u64 *channels);
  83. int (*queue_iso)(struct fw_iso_context *ctx,
  84. struct fw_iso_packet *packet,
  85. struct fw_iso_buffer *buffer,
  86. unsigned long payload);
  87. void (*flush_queue_iso)(struct fw_iso_context *ctx);
  88. int (*flush_iso_completions)(struct fw_iso_context *ctx);
  89. int (*stop_iso)(struct fw_iso_context *ctx);
  90. };
  91. void fw_card_initialize(struct fw_card *card,
  92. const struct fw_card_driver *driver, struct device *device);
  93. int fw_card_add(struct fw_card *card,
  94. u32 max_receive, u32 link_speed, u64 guid);
  95. void fw_core_remove_card(struct fw_card *card);
  96. int fw_compute_block_crc(__be32 *block);
  97. void fw_schedule_bus_reset(struct fw_card *card, bool delayed, bool short_reset);
  98. void fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
  99. static inline struct fw_card *fw_card_get(struct fw_card *card)
  100. {
  101. kref_get(&card->kref);
  102. return card;
  103. }
  104. void fw_card_release(struct kref *kref);
  105. static inline void fw_card_put(struct fw_card *card)
  106. {
  107. kref_put(&card->kref, fw_card_release);
  108. }
  109. /* -cdev */
  110. extern const struct file_operations fw_device_ops;
  111. void fw_device_cdev_update(struct fw_device *device);
  112. void fw_device_cdev_remove(struct fw_device *device);
  113. void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p);
  114. /* -device */
  115. extern struct rw_semaphore fw_device_rwsem;
  116. extern struct idr fw_device_idr;
  117. extern int fw_cdev_major;
  118. static inline struct fw_device *fw_device_get(struct fw_device *device)
  119. {
  120. get_device(&device->device);
  121. return device;
  122. }
  123. static inline void fw_device_put(struct fw_device *device)
  124. {
  125. put_device(&device->device);
  126. }
  127. struct fw_device *fw_device_get_by_devt(dev_t devt);
  128. int fw_device_set_broadcast_channel(struct device *dev, void *gen);
  129. void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  130. /* -iso */
  131. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
  132. /* -topology */
  133. enum {
  134. FW_NODE_CREATED,
  135. FW_NODE_UPDATED,
  136. FW_NODE_DESTROYED,
  137. FW_NODE_LINK_ON,
  138. FW_NODE_LINK_OFF,
  139. FW_NODE_INITIATED_RESET,
  140. };
  141. struct fw_node {
  142. u16 node_id;
  143. u8 color;
  144. u8 port_count;
  145. u8 link_on:1;
  146. u8 initiated_reset:1;
  147. u8 b_path:1;
  148. u8 phy_speed:2; /* As in the self ID packet. */
  149. u8 max_speed:2; /* Minimum of all phy-speeds on the path from the
  150. * local node to this node. */
  151. u8 max_depth:4; /* Maximum depth to any leaf node */
  152. u8 max_hops:4; /* Max hops in this sub tree */
  153. atomic_t ref_count;
  154. /* For serializing node topology into a list. */
  155. struct list_head link;
  156. /* Upper layer specific data. */
  157. void *data;
  158. struct fw_node *ports[0];
  159. };
  160. static inline struct fw_node *fw_node_get(struct fw_node *node)
  161. {
  162. atomic_inc(&node->ref_count);
  163. return node;
  164. }
  165. static inline void fw_node_put(struct fw_node *node)
  166. {
  167. if (atomic_dec_and_test(&node->ref_count))
  168. kfree(node);
  169. }
  170. void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
  171. int generation, int self_id_count, u32 *self_ids, bool bm_abdicate);
  172. void fw_destroy_nodes(struct fw_card *card);
  173. /*
  174. * Check whether new_generation is the immediate successor of old_generation.
  175. * Take counter roll-over at 255 (as per OHCI) into account.
  176. */
  177. static inline bool is_next_generation(int new_generation, int old_generation)
  178. {
  179. return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
  180. }
  181. /* -transaction */
  182. #define TCODE_LINK_INTERNAL 0xe
  183. #define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4)
  184. #define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0)
  185. #define TCODE_IS_LINK_INTERNAL(tcode) ((tcode) == TCODE_LINK_INTERNAL)
  186. #define TCODE_IS_REQUEST(tcode) (((tcode) & 2) == 0)
  187. #define TCODE_IS_RESPONSE(tcode) (((tcode) & 2) != 0)
  188. #define TCODE_HAS_REQUEST_DATA(tcode) (((tcode) & 12) != 4)
  189. #define TCODE_HAS_RESPONSE_DATA(tcode) (((tcode) & 12) != 0)
  190. #define LOCAL_BUS 0xffc0
  191. void fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  192. void fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  193. int fw_get_response_length(struct fw_request *request);
  194. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  195. int rcode, void *payload, size_t length);
  196. #define FW_PHY_CONFIG_NO_NODE_ID -1
  197. #define FW_PHY_CONFIG_CURRENT_GAP_COUNT -1
  198. void fw_send_phy_config(struct fw_card *card,
  199. int node_id, int generation, int gap_count);
  200. static inline bool is_ping_packet(u32 *data)
  201. {
  202. return (data[0] & 0xc0ffffff) == 0 && ~data[0] == data[1];
  203. }
  204. #endif /* _FIREWIRE_CORE_H */