firewire.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_FIREWIRE_H
  3. #define _LINUX_FIREWIRE_H
  4. #include <linux/completion.h>
  5. #include <linux/device.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/kernel.h>
  8. #include <linux/kref.h>
  9. #include <linux/list.h>
  10. #include <linux/mutex.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/timer.h>
  14. #include <linux/types.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/atomic.h>
  17. #include <asm/byteorder.h>
  18. #define CSR_REGISTER_BASE 0xfffff0000000ULL
  19. /* register offsets are relative to CSR_REGISTER_BASE */
  20. #define CSR_STATE_CLEAR 0x0
  21. #define CSR_STATE_SET 0x4
  22. #define CSR_NODE_IDS 0x8
  23. #define CSR_RESET_START 0xc
  24. #define CSR_SPLIT_TIMEOUT_HI 0x18
  25. #define CSR_SPLIT_TIMEOUT_LO 0x1c
  26. #define CSR_CYCLE_TIME 0x200
  27. #define CSR_BUS_TIME 0x204
  28. #define CSR_BUSY_TIMEOUT 0x210
  29. #define CSR_PRIORITY_BUDGET 0x218
  30. #define CSR_BUS_MANAGER_ID 0x21c
  31. #define CSR_BANDWIDTH_AVAILABLE 0x220
  32. #define CSR_CHANNELS_AVAILABLE 0x224
  33. #define CSR_CHANNELS_AVAILABLE_HI 0x224
  34. #define CSR_CHANNELS_AVAILABLE_LO 0x228
  35. #define CSR_MAINT_UTILITY 0x230
  36. #define CSR_BROADCAST_CHANNEL 0x234
  37. #define CSR_CONFIG_ROM 0x400
  38. #define CSR_CONFIG_ROM_END 0x800
  39. #define CSR_OMPR 0x900
  40. #define CSR_OPCR(i) (0x904 + (i) * 4)
  41. #define CSR_IMPR 0x980
  42. #define CSR_IPCR(i) (0x984 + (i) * 4)
  43. #define CSR_FCP_COMMAND 0xB00
  44. #define CSR_FCP_RESPONSE 0xD00
  45. #define CSR_FCP_END 0xF00
  46. #define CSR_TOPOLOGY_MAP 0x1000
  47. #define CSR_TOPOLOGY_MAP_END 0x1400
  48. #define CSR_SPEED_MAP 0x2000
  49. #define CSR_SPEED_MAP_END 0x3000
  50. #define CSR_OFFSET 0x40
  51. #define CSR_LEAF 0x80
  52. #define CSR_DIRECTORY 0xc0
  53. #define CSR_DESCRIPTOR 0x01
  54. #define CSR_VENDOR 0x03
  55. #define CSR_HARDWARE_VERSION 0x04
  56. #define CSR_UNIT 0x11
  57. #define CSR_SPECIFIER_ID 0x12
  58. #define CSR_VERSION 0x13
  59. #define CSR_DEPENDENT_INFO 0x14
  60. #define CSR_MODEL 0x17
  61. #define CSR_DIRECTORY_ID 0x20
  62. struct fw_csr_iterator {
  63. const u32 *p;
  64. const u32 *end;
  65. };
  66. void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p);
  67. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value);
  68. int fw_csr_string(const u32 *directory, int key, char *buf, size_t size);
  69. extern struct bus_type fw_bus_type;
  70. struct fw_card_driver;
  71. struct fw_node;
  72. struct fw_card {
  73. const struct fw_card_driver *driver;
  74. struct device *device;
  75. struct kref kref;
  76. struct completion done;
  77. int node_id;
  78. int generation;
  79. int current_tlabel;
  80. u64 tlabel_mask;
  81. struct list_head transaction_list;
  82. u64 reset_jiffies;
  83. u32 split_timeout_hi;
  84. u32 split_timeout_lo;
  85. unsigned int split_timeout_cycles;
  86. unsigned int split_timeout_jiffies;
  87. unsigned long long guid;
  88. unsigned max_receive;
  89. int link_speed;
  90. int config_rom_generation;
  91. spinlock_t lock; /* Take this lock when handling the lists in
  92. * this struct. */
  93. struct fw_node *local_node;
  94. struct fw_node *root_node;
  95. struct fw_node *irm_node;
  96. u8 color; /* must be u8 to match the definition in struct fw_node */
  97. int gap_count;
  98. bool beta_repeaters_present;
  99. int index;
  100. struct list_head link;
  101. struct list_head phy_receiver_list;
  102. struct delayed_work br_work; /* bus reset job */
  103. bool br_short;
  104. struct delayed_work bm_work; /* bus manager job */
  105. int bm_retries;
  106. int bm_generation;
  107. int bm_node_id;
  108. bool bm_abdicate;
  109. bool priority_budget_implemented; /* controller feature */
  110. bool broadcast_channel_auto_allocated; /* controller feature */
  111. bool broadcast_channel_allocated;
  112. u32 broadcast_channel;
  113. __be32 topology_map[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4];
  114. __be32 maint_utility_register;
  115. };
  116. static inline struct fw_card *fw_card_get(struct fw_card *card)
  117. {
  118. kref_get(&card->kref);
  119. return card;
  120. }
  121. void fw_card_release(struct kref *kref);
  122. static inline void fw_card_put(struct fw_card *card)
  123. {
  124. kref_put(&card->kref, fw_card_release);
  125. }
  126. struct fw_attribute_group {
  127. struct attribute_group *groups[2];
  128. struct attribute_group group;
  129. struct attribute *attrs[13];
  130. };
  131. enum fw_device_state {
  132. FW_DEVICE_INITIALIZING,
  133. FW_DEVICE_RUNNING,
  134. FW_DEVICE_GONE,
  135. FW_DEVICE_SHUTDOWN,
  136. };
  137. /*
  138. * Note, fw_device.generation always has to be read before fw_device.node_id.
  139. * Use SMP memory barriers to ensure this. Otherwise requests will be sent
  140. * to an outdated node_id if the generation was updated in the meantime due
  141. * to a bus reset.
  142. *
  143. * Likewise, fw-core will take care to update .node_id before .generation so
  144. * that whenever fw_device.generation is current WRT the actual bus generation,
  145. * fw_device.node_id is guaranteed to be current too.
  146. *
  147. * The same applies to fw_device.card->node_id vs. fw_device.generation.
  148. *
  149. * fw_device.config_rom and fw_device.config_rom_length may be accessed during
  150. * the lifetime of any fw_unit belonging to the fw_device, before device_del()
  151. * was called on the last fw_unit. Alternatively, they may be accessed while
  152. * holding fw_device_rwsem.
  153. */
  154. struct fw_device {
  155. atomic_t state;
  156. struct fw_node *node;
  157. int node_id;
  158. int generation;
  159. unsigned max_speed;
  160. struct fw_card *card;
  161. struct device device;
  162. struct mutex client_list_mutex;
  163. struct list_head client_list;
  164. const u32 *config_rom;
  165. size_t config_rom_length;
  166. int config_rom_retries;
  167. unsigned is_local:1;
  168. unsigned max_rec:4;
  169. unsigned cmc:1;
  170. unsigned irmc:1;
  171. unsigned bc_implemented:2;
  172. work_func_t workfn;
  173. struct delayed_work work;
  174. struct fw_attribute_group attribute_group;
  175. };
  176. static inline struct fw_device *fw_device(struct device *dev)
  177. {
  178. return container_of(dev, struct fw_device, device);
  179. }
  180. static inline int fw_device_is_shutdown(struct fw_device *device)
  181. {
  182. return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN;
  183. }
  184. int fw_device_enable_phys_dma(struct fw_device *device);
  185. /*
  186. * fw_unit.directory must not be accessed after device_del(&fw_unit.device).
  187. */
  188. struct fw_unit {
  189. struct device device;
  190. const u32 *directory;
  191. struct fw_attribute_group attribute_group;
  192. };
  193. static inline struct fw_unit *fw_unit(struct device *dev)
  194. {
  195. return container_of(dev, struct fw_unit, device);
  196. }
  197. static inline struct fw_unit *fw_unit_get(struct fw_unit *unit)
  198. {
  199. get_device(&unit->device);
  200. return unit;
  201. }
  202. static inline void fw_unit_put(struct fw_unit *unit)
  203. {
  204. put_device(&unit->device);
  205. }
  206. static inline struct fw_device *fw_parent_device(struct fw_unit *unit)
  207. {
  208. return fw_device(unit->device.parent);
  209. }
  210. struct ieee1394_device_id;
  211. struct fw_driver {
  212. struct device_driver driver;
  213. int (*probe)(struct fw_unit *unit, const struct ieee1394_device_id *id);
  214. /* Called when the parent device sits through a bus reset. */
  215. void (*update)(struct fw_unit *unit);
  216. void (*remove)(struct fw_unit *unit);
  217. const struct ieee1394_device_id *id_table;
  218. };
  219. struct fw_packet;
  220. struct fw_request;
  221. typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
  222. struct fw_card *card, int status);
  223. typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
  224. void *data, size_t length,
  225. void *callback_data);
  226. /*
  227. * This callback handles an inbound request subaction. It is called in
  228. * RCU read-side context, therefore must not sleep.
  229. *
  230. * The callback should not initiate outbound request subactions directly.
  231. * Otherwise there is a danger of recursion of inbound and outbound
  232. * transactions from and to the local node.
  233. *
  234. * The callback is responsible that either fw_send_response() or kfree()
  235. * is called on the @request, except for FCP registers for which the core
  236. * takes care of that.
  237. */
  238. typedef void (*fw_address_callback_t)(struct fw_card *card,
  239. struct fw_request *request,
  240. int tcode, int destination, int source,
  241. int generation,
  242. unsigned long long offset,
  243. void *data, size_t length,
  244. void *callback_data);
  245. struct fw_packet {
  246. int speed;
  247. int generation;
  248. u32 header[4];
  249. size_t header_length;
  250. void *payload;
  251. size_t payload_length;
  252. dma_addr_t payload_bus;
  253. bool payload_mapped;
  254. u32 timestamp;
  255. /*
  256. * This callback is called when the packet transmission has completed.
  257. * For successful transmission, the status code is the ack received
  258. * from the destination. Otherwise it is one of the juju-specific
  259. * rcodes: RCODE_SEND_ERROR, _CANCELLED, _BUSY, _GENERATION, _NO_ACK.
  260. * The callback can be called from tasklet context and thus
  261. * must never block.
  262. */
  263. fw_packet_callback_t callback;
  264. int ack;
  265. struct list_head link;
  266. void *driver_data;
  267. };
  268. struct fw_transaction {
  269. int node_id; /* The generation is implied; it is always the current. */
  270. int tlabel;
  271. struct list_head link;
  272. struct fw_card *card;
  273. bool is_split_transaction;
  274. struct timer_list split_timeout_timer;
  275. struct fw_packet packet;
  276. /*
  277. * The data passed to the callback is valid only during the
  278. * callback.
  279. */
  280. fw_transaction_callback_t callback;
  281. void *callback_data;
  282. };
  283. struct fw_address_handler {
  284. u64 offset;
  285. u64 length;
  286. fw_address_callback_t address_callback;
  287. void *callback_data;
  288. struct list_head link;
  289. };
  290. struct fw_address_region {
  291. u64 start;
  292. u64 end;
  293. };
  294. extern const struct fw_address_region fw_high_memory_region;
  295. int fw_core_add_address_handler(struct fw_address_handler *handler,
  296. const struct fw_address_region *region);
  297. void fw_core_remove_address_handler(struct fw_address_handler *handler);
  298. void fw_send_response(struct fw_card *card,
  299. struct fw_request *request, int rcode);
  300. int fw_get_request_speed(struct fw_request *request);
  301. void fw_send_request(struct fw_card *card, struct fw_transaction *t,
  302. int tcode, int destination_id, int generation, int speed,
  303. unsigned long long offset, void *payload, size_t length,
  304. fw_transaction_callback_t callback, void *callback_data);
  305. int fw_cancel_transaction(struct fw_card *card,
  306. struct fw_transaction *transaction);
  307. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  308. int generation, int speed, unsigned long long offset,
  309. void *payload, size_t length);
  310. const char *fw_rcode_string(int rcode);
  311. static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
  312. {
  313. return tag << 14 | channel << 8 | sy;
  314. }
  315. void fw_schedule_bus_reset(struct fw_card *card, bool delayed,
  316. bool short_reset);
  317. struct fw_descriptor {
  318. struct list_head link;
  319. size_t length;
  320. u32 immediate;
  321. u32 key;
  322. const u32 *data;
  323. };
  324. int fw_core_add_descriptor(struct fw_descriptor *desc);
  325. void fw_core_remove_descriptor(struct fw_descriptor *desc);
  326. /*
  327. * The iso packet format allows for an immediate header/payload part
  328. * stored in 'header' immediately after the packet info plus an
  329. * indirect payload part that is pointer to by the 'payload' field.
  330. * Applications can use one or the other or both to implement simple
  331. * low-bandwidth streaming (e.g. audio) or more advanced
  332. * scatter-gather streaming (e.g. assembling video frame automatically).
  333. */
  334. struct fw_iso_packet {
  335. u16 payload_length; /* Length of indirect payload */
  336. u32 interrupt:1; /* Generate interrupt on this packet */
  337. u32 skip:1; /* tx: Set to not send packet at all */
  338. /* rx: Sync bit, wait for matching sy */
  339. u32 tag:2; /* tx: Tag in packet header */
  340. u32 sy:4; /* tx: Sy in packet header */
  341. u32 header_length:8; /* Length of immediate header */
  342. u32 header[0]; /* tx: Top of 1394 isoch. data_block */
  343. };
  344. #define FW_ISO_CONTEXT_TRANSMIT 0
  345. #define FW_ISO_CONTEXT_RECEIVE 1
  346. #define FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL 2
  347. #define FW_ISO_CONTEXT_MATCH_TAG0 1
  348. #define FW_ISO_CONTEXT_MATCH_TAG1 2
  349. #define FW_ISO_CONTEXT_MATCH_TAG2 4
  350. #define FW_ISO_CONTEXT_MATCH_TAG3 8
  351. #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
  352. /*
  353. * An iso buffer is just a set of pages mapped for DMA in the
  354. * specified direction. Since the pages are to be used for DMA, they
  355. * are not mapped into the kernel virtual address space. We store the
  356. * DMA address in the page private. The helper function
  357. * fw_iso_buffer_map() will map the pages into a given vma.
  358. */
  359. struct fw_iso_buffer {
  360. enum dma_data_direction direction;
  361. struct page **pages;
  362. int page_count;
  363. int page_count_mapped;
  364. };
  365. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  366. int page_count, enum dma_data_direction direction);
  367. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  368. size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed);
  369. struct fw_iso_context;
  370. typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
  371. u32 cycle, size_t header_length,
  372. void *header, void *data);
  373. typedef void (*fw_iso_mc_callback_t)(struct fw_iso_context *context,
  374. dma_addr_t completed, void *data);
  375. struct fw_iso_context {
  376. struct fw_card *card;
  377. int type;
  378. int channel;
  379. int speed;
  380. bool drop_overflow_headers;
  381. size_t header_size;
  382. union {
  383. fw_iso_callback_t sc;
  384. fw_iso_mc_callback_t mc;
  385. } callback;
  386. void *callback_data;
  387. };
  388. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  389. int type, int channel, int speed, size_t header_size,
  390. fw_iso_callback_t callback, void *callback_data);
  391. int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels);
  392. int fw_iso_context_queue(struct fw_iso_context *ctx,
  393. struct fw_iso_packet *packet,
  394. struct fw_iso_buffer *buffer,
  395. unsigned long payload);
  396. void fw_iso_context_queue_flush(struct fw_iso_context *ctx);
  397. int fw_iso_context_flush_completions(struct fw_iso_context *ctx);
  398. int fw_iso_context_start(struct fw_iso_context *ctx,
  399. int cycle, int sync, int tags);
  400. int fw_iso_context_stop(struct fw_iso_context *ctx);
  401. void fw_iso_context_destroy(struct fw_iso_context *ctx);
  402. void fw_iso_resource_manage(struct fw_card *card, int generation,
  403. u64 channels_mask, int *channel, int *bandwidth,
  404. bool allocate);
  405. extern struct workqueue_struct *fw_workqueue;
  406. #endif /* _LINUX_FIREWIRE_H */