fw-transaction.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #ifndef __fw_transaction_h
  19. #define __fw_transaction_h
  20. #include <linux/completion.h>
  21. #include <linux/device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/firewire-constants.h>
  24. #include <linux/kref.h>
  25. #include <linux/list.h>
  26. #include <linux/spinlock_types.h>
  27. #include <linux/timer.h>
  28. #include <linux/types.h>
  29. #include <linux/workqueue.h>
  30. #define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4)
  31. #define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0)
  32. #define TCODE_IS_REQUEST(tcode) (((tcode) & 2) == 0)
  33. #define TCODE_IS_RESPONSE(tcode) (((tcode) & 2) != 0)
  34. #define TCODE_HAS_REQUEST_DATA(tcode) (((tcode) & 12) != 4)
  35. #define TCODE_HAS_RESPONSE_DATA(tcode) (((tcode) & 12) != 0)
  36. #define LOCAL_BUS 0xffc0
  37. #define SELFID_PORT_CHILD 0x3
  38. #define SELFID_PORT_PARENT 0x2
  39. #define SELFID_PORT_NCONN 0x1
  40. #define SELFID_PORT_NONE 0x0
  41. #define PHY_PACKET_CONFIG 0x0
  42. #define PHY_PACKET_LINK_ON 0x1
  43. #define PHY_PACKET_SELF_ID 0x2
  44. /* Bit fields _within_ the PHY registers. */
  45. #define PHY_LINK_ACTIVE 0x80
  46. #define PHY_CONTENDER 0x40
  47. #define PHY_BUS_RESET 0x40
  48. #define PHY_BUS_SHORT_RESET 0x40
  49. #define CSR_REGISTER_BASE 0xfffff0000000ULL
  50. /* register offsets relative to CSR_REGISTER_BASE */
  51. #define CSR_STATE_CLEAR 0x0
  52. #define CSR_STATE_SET 0x4
  53. #define CSR_NODE_IDS 0x8
  54. #define CSR_RESET_START 0xc
  55. #define CSR_SPLIT_TIMEOUT_HI 0x18
  56. #define CSR_SPLIT_TIMEOUT_LO 0x1c
  57. #define CSR_CYCLE_TIME 0x200
  58. #define CSR_BUS_TIME 0x204
  59. #define CSR_BUSY_TIMEOUT 0x210
  60. #define CSR_BUS_MANAGER_ID 0x21c
  61. #define CSR_BANDWIDTH_AVAILABLE 0x220
  62. #define CSR_CHANNELS_AVAILABLE 0x224
  63. #define CSR_CHANNELS_AVAILABLE_HI 0x224
  64. #define CSR_CHANNELS_AVAILABLE_LO 0x228
  65. #define CSR_BROADCAST_CHANNEL 0x234
  66. #define CSR_CONFIG_ROM 0x400
  67. #define CSR_CONFIG_ROM_END 0x800
  68. #define CSR_FCP_COMMAND 0xB00
  69. #define CSR_FCP_RESPONSE 0xD00
  70. #define CSR_FCP_END 0xF00
  71. #define CSR_TOPOLOGY_MAP 0x1000
  72. #define CSR_TOPOLOGY_MAP_END 0x1400
  73. #define CSR_SPEED_MAP 0x2000
  74. #define CSR_SPEED_MAP_END 0x3000
  75. #define BROADCAST_CHANNEL_INITIAL (1 << 31 | 31)
  76. #define BROADCAST_CHANNEL_VALID (1 << 30)
  77. #define fw_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, ## args)
  78. #define fw_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
  79. static inline void fw_memcpy_from_be32(void *_dst, void *_src, size_t size)
  80. {
  81. u32 *dst = _dst;
  82. __be32 *src = _src;
  83. int i;
  84. for (i = 0; i < size / 4; i++)
  85. dst[i] = be32_to_cpu(src[i]);
  86. }
  87. static inline void fw_memcpy_to_be32(void *_dst, void *_src, size_t size)
  88. {
  89. fw_memcpy_from_be32(_dst, _src, size);
  90. }
  91. struct fw_card;
  92. struct fw_packet;
  93. struct fw_node;
  94. struct fw_request;
  95. struct fw_descriptor {
  96. struct list_head link;
  97. size_t length;
  98. u32 immediate;
  99. u32 key;
  100. const u32 *data;
  101. };
  102. int fw_core_add_descriptor(struct fw_descriptor *desc);
  103. void fw_core_remove_descriptor(struct fw_descriptor *desc);
  104. typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
  105. struct fw_card *card, int status);
  106. typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
  107. void *data, size_t length,
  108. void *callback_data);
  109. /*
  110. * Important note: The callback must guarantee that either fw_send_response()
  111. * or kfree() is called on the @request.
  112. */
  113. typedef void (*fw_address_callback_t)(struct fw_card *card,
  114. struct fw_request *request,
  115. int tcode, int destination, int source,
  116. int generation, int speed,
  117. unsigned long long offset,
  118. void *data, size_t length,
  119. void *callback_data);
  120. typedef void (*fw_bus_reset_callback_t)(struct fw_card *handle,
  121. int node_id, int generation,
  122. u32 *self_ids,
  123. int self_id_count,
  124. void *callback_data);
  125. struct fw_packet {
  126. int speed;
  127. int generation;
  128. u32 header[4];
  129. size_t header_length;
  130. void *payload;
  131. size_t payload_length;
  132. dma_addr_t payload_bus;
  133. u32 timestamp;
  134. /*
  135. * This callback is called when the packet transmission has
  136. * completed; for successful transmission, the status code is
  137. * the ack received from the destination, otherwise it's a
  138. * negative errno: ENOMEM, ESTALE, ETIMEDOUT, ENODEV, EIO.
  139. * The callback can be called from tasklet context and thus
  140. * must never block.
  141. */
  142. fw_packet_callback_t callback;
  143. int ack;
  144. struct list_head link;
  145. void *driver_data;
  146. };
  147. struct fw_transaction {
  148. int node_id; /* The generation is implied; it is always the current. */
  149. int tlabel;
  150. int timestamp;
  151. struct list_head link;
  152. struct fw_packet packet;
  153. /*
  154. * The data passed to the callback is valid only during the
  155. * callback.
  156. */
  157. fw_transaction_callback_t callback;
  158. void *callback_data;
  159. };
  160. static inline struct fw_packet *
  161. fw_packet(struct list_head *l)
  162. {
  163. return list_entry(l, struct fw_packet, link);
  164. }
  165. struct fw_address_handler {
  166. u64 offset;
  167. size_t length;
  168. fw_address_callback_t address_callback;
  169. void *callback_data;
  170. struct list_head link;
  171. };
  172. struct fw_address_region {
  173. u64 start;
  174. u64 end;
  175. };
  176. extern const struct fw_address_region fw_high_memory_region;
  177. int fw_core_add_address_handler(struct fw_address_handler *handler,
  178. const struct fw_address_region *region);
  179. void fw_core_remove_address_handler(struct fw_address_handler *handler);
  180. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  181. int rcode, void *payload, size_t length);
  182. void fw_send_response(struct fw_card *card,
  183. struct fw_request *request, int rcode);
  184. extern struct bus_type fw_bus_type;
  185. struct fw_card {
  186. const struct fw_card_driver *driver;
  187. struct device *device;
  188. struct kref kref;
  189. struct completion done;
  190. int node_id;
  191. int generation;
  192. int current_tlabel, tlabel_mask;
  193. struct list_head transaction_list;
  194. struct timer_list flush_timer;
  195. unsigned long reset_jiffies;
  196. unsigned long long guid;
  197. unsigned max_receive;
  198. int link_speed;
  199. int config_rom_generation;
  200. spinlock_t lock; /* Take this lock when handling the lists in
  201. * this struct. */
  202. struct fw_node *local_node;
  203. struct fw_node *root_node;
  204. struct fw_node *irm_node;
  205. u8 color; /* must be u8 to match the definition in struct fw_node */
  206. int gap_count;
  207. bool beta_repeaters_present;
  208. int index;
  209. struct list_head link;
  210. /* Work struct for BM duties. */
  211. struct delayed_work work;
  212. int bm_retries;
  213. int bm_generation;
  214. u32 broadcast_channel;
  215. u32 topology_map[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4];
  216. };
  217. static inline struct fw_card *fw_card_get(struct fw_card *card)
  218. {
  219. kref_get(&card->kref);
  220. return card;
  221. }
  222. void fw_card_release(struct kref *kref);
  223. static inline void fw_card_put(struct fw_card *card)
  224. {
  225. kref_put(&card->kref, fw_card_release);
  226. }
  227. extern void fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
  228. /*
  229. * Check whether new_generation is the immediate successor of old_generation.
  230. * Take counter roll-over at 255 (as per to OHCI) into account.
  231. */
  232. static inline bool is_next_generation(int new_generation, int old_generation)
  233. {
  234. return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
  235. }
  236. /*
  237. * The iso packet format allows for an immediate header/payload part
  238. * stored in 'header' immediately after the packet info plus an
  239. * indirect payload part that is pointer to by the 'payload' field.
  240. * Applications can use one or the other or both to implement simple
  241. * low-bandwidth streaming (e.g. audio) or more advanced
  242. * scatter-gather streaming (e.g. assembling video frame automatically).
  243. */
  244. struct fw_iso_packet {
  245. u16 payload_length; /* Length of indirect payload. */
  246. u32 interrupt : 1; /* Generate interrupt on this packet */
  247. u32 skip : 1; /* Set to not send packet at all. */
  248. u32 tag : 2;
  249. u32 sy : 4;
  250. u32 header_length : 8; /* Length of immediate header. */
  251. u32 header[0];
  252. };
  253. #define FW_ISO_CONTEXT_TRANSMIT 0
  254. #define FW_ISO_CONTEXT_RECEIVE 1
  255. #define FW_ISO_CONTEXT_MATCH_TAG0 1
  256. #define FW_ISO_CONTEXT_MATCH_TAG1 2
  257. #define FW_ISO_CONTEXT_MATCH_TAG2 4
  258. #define FW_ISO_CONTEXT_MATCH_TAG3 8
  259. #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
  260. struct fw_iso_context;
  261. typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
  262. u32 cycle, size_t header_length,
  263. void *header, void *data);
  264. /*
  265. * An iso buffer is just a set of pages mapped for DMA in the
  266. * specified direction. Since the pages are to be used for DMA, they
  267. * are not mapped into the kernel virtual address space. We store the
  268. * DMA address in the page private. The helper function
  269. * fw_iso_buffer_map() will map the pages into a given vma.
  270. */
  271. struct fw_iso_buffer {
  272. enum dma_data_direction direction;
  273. struct page **pages;
  274. int page_count;
  275. };
  276. struct fw_iso_context {
  277. struct fw_card *card;
  278. int type;
  279. int channel;
  280. int speed;
  281. size_t header_size;
  282. fw_iso_callback_t callback;
  283. void *callback_data;
  284. };
  285. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  286. int page_count, enum dma_data_direction direction);
  287. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
  288. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  289. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  290. int type, int channel, int speed, size_t header_size,
  291. fw_iso_callback_t callback, void *callback_data);
  292. int fw_iso_context_queue(struct fw_iso_context *ctx,
  293. struct fw_iso_packet *packet,
  294. struct fw_iso_buffer *buffer,
  295. unsigned long payload);
  296. int fw_iso_context_start(struct fw_iso_context *ctx,
  297. int cycle, int sync, int tags);
  298. int fw_iso_context_stop(struct fw_iso_context *ctx);
  299. void fw_iso_context_destroy(struct fw_iso_context *ctx);
  300. struct fw_card_driver {
  301. /*
  302. * Enable the given card with the given initial config rom.
  303. * This function is expected to activate the card, and either
  304. * enable the PHY or set the link_on bit and initiate a bus
  305. * reset.
  306. */
  307. int (*enable)(struct fw_card *card, u32 *config_rom, size_t length);
  308. int (*update_phy_reg)(struct fw_card *card, int address,
  309. int clear_bits, int set_bits);
  310. /*
  311. * Update the config rom for an enabled card. This function
  312. * should change the config rom that is presented on the bus
  313. * an initiate a bus reset.
  314. */
  315. int (*set_config_rom)(struct fw_card *card,
  316. u32 *config_rom, size_t length);
  317. void (*send_request)(struct fw_card *card, struct fw_packet *packet);
  318. void (*send_response)(struct fw_card *card, struct fw_packet *packet);
  319. /* Calling cancel is valid once a packet has been submitted. */
  320. int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
  321. /*
  322. * Allow the specified node ID to do direct DMA out and in of
  323. * host memory. The card will disable this for all node when
  324. * a bus reset happens, so driver need to reenable this after
  325. * bus reset. Returns 0 on success, -ENODEV if the card
  326. * doesn't support this, -ESTALE if the generation doesn't
  327. * match.
  328. */
  329. int (*enable_phys_dma)(struct fw_card *card,
  330. int node_id, int generation);
  331. u64 (*get_bus_time)(struct fw_card *card);
  332. struct fw_iso_context *
  333. (*allocate_iso_context)(struct fw_card *card,
  334. int type, size_t header_size);
  335. void (*free_iso_context)(struct fw_iso_context *ctx);
  336. int (*start_iso)(struct fw_iso_context *ctx,
  337. s32 cycle, u32 sync, u32 tags);
  338. int (*queue_iso)(struct fw_iso_context *ctx,
  339. struct fw_iso_packet *packet,
  340. struct fw_iso_buffer *buffer,
  341. unsigned long payload);
  342. int (*stop_iso)(struct fw_iso_context *ctx);
  343. };
  344. int fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
  345. void fw_send_request(struct fw_card *card, struct fw_transaction *t,
  346. int tcode, int destination_id, int generation, int speed,
  347. unsigned long long offset, void *data, size_t length,
  348. fw_transaction_callback_t callback, void *callback_data);
  349. int fw_cancel_transaction(struct fw_card *card,
  350. struct fw_transaction *transaction);
  351. void fw_flush_transactions(struct fw_card *card);
  352. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  353. int generation, int speed, unsigned long long offset,
  354. void *data, size_t length);
  355. void fw_send_phy_config(struct fw_card *card,
  356. int node_id, int generation, int gap_count);
  357. /*
  358. * Called by the topology code to inform the device code of node
  359. * activity; found, lost, or updated nodes.
  360. */
  361. void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  362. /* API used by card level drivers */
  363. void fw_card_initialize(struct fw_card *card,
  364. const struct fw_card_driver *driver, struct device *device);
  365. int fw_card_add(struct fw_card *card,
  366. u32 max_receive, u32 link_speed, u64 guid);
  367. void fw_core_remove_card(struct fw_card *card);
  368. void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
  369. int generation, int self_id_count, u32 *self_ids);
  370. void fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  371. void fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  372. #endif /* __fw_transaction_h */