fw-transaction.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 BANDWIDTH_AVAILABLE_INITIAL 4915
  76. #define BROADCAST_CHANNEL_INITIAL (1 << 31 | 31)
  77. #define BROADCAST_CHANNEL_VALID (1 << 30)
  78. #define fw_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, ## args)
  79. #define fw_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
  80. static inline void fw_memcpy_from_be32(void *_dst, void *_src, size_t size)
  81. {
  82. u32 *dst = _dst;
  83. __be32 *src = _src;
  84. int i;
  85. for (i = 0; i < size / 4; i++)
  86. dst[i] = be32_to_cpu(src[i]);
  87. }
  88. static inline void fw_memcpy_to_be32(void *_dst, void *_src, size_t size)
  89. {
  90. fw_memcpy_from_be32(_dst, _src, size);
  91. }
  92. struct fw_card;
  93. struct fw_packet;
  94. struct fw_node;
  95. struct fw_request;
  96. struct fw_descriptor {
  97. struct list_head link;
  98. size_t length;
  99. u32 immediate;
  100. u32 key;
  101. const u32 *data;
  102. };
  103. int fw_core_add_descriptor(struct fw_descriptor *desc);
  104. void fw_core_remove_descriptor(struct fw_descriptor *desc);
  105. typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
  106. struct fw_card *card, int status);
  107. typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
  108. void *data, size_t length,
  109. void *callback_data);
  110. /*
  111. * Important note: The callback must guarantee that either fw_send_response()
  112. * or kfree() is called on the @request.
  113. */
  114. typedef void (*fw_address_callback_t)(struct fw_card *card,
  115. struct fw_request *request,
  116. int tcode, int destination, int source,
  117. int generation, int speed,
  118. unsigned long long offset,
  119. void *data, size_t length,
  120. void *callback_data);
  121. struct fw_packet {
  122. int speed;
  123. int generation;
  124. u32 header[4];
  125. size_t header_length;
  126. void *payload;
  127. size_t payload_length;
  128. dma_addr_t payload_bus;
  129. u32 timestamp;
  130. /*
  131. * This callback is called when the packet transmission has
  132. * completed; for successful transmission, the status code is
  133. * the ack received from the destination, otherwise it's a
  134. * negative errno: ENOMEM, ESTALE, ETIMEDOUT, ENODEV, EIO.
  135. * The callback can be called from tasklet context and thus
  136. * must never block.
  137. */
  138. fw_packet_callback_t callback;
  139. int ack;
  140. struct list_head link;
  141. void *driver_data;
  142. };
  143. struct fw_transaction {
  144. int node_id; /* The generation is implied; it is always the current. */
  145. int tlabel;
  146. int timestamp;
  147. struct list_head link;
  148. struct fw_packet packet;
  149. /*
  150. * The data passed to the callback is valid only during the
  151. * callback.
  152. */
  153. fw_transaction_callback_t callback;
  154. void *callback_data;
  155. };
  156. struct fw_address_handler {
  157. u64 offset;
  158. size_t length;
  159. fw_address_callback_t address_callback;
  160. void *callback_data;
  161. struct list_head link;
  162. };
  163. struct fw_address_region {
  164. u64 start;
  165. u64 end;
  166. };
  167. extern const struct fw_address_region fw_high_memory_region;
  168. int fw_core_add_address_handler(struct fw_address_handler *handler,
  169. const struct fw_address_region *region);
  170. void fw_core_remove_address_handler(struct fw_address_handler *handler);
  171. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  172. int rcode, void *payload, size_t length);
  173. void fw_send_response(struct fw_card *card,
  174. struct fw_request *request, int rcode);
  175. extern struct bus_type fw_bus_type;
  176. struct fw_card {
  177. const struct fw_card_driver *driver;
  178. struct device *device;
  179. struct kref kref;
  180. struct completion done;
  181. int node_id;
  182. int generation;
  183. int current_tlabel, tlabel_mask;
  184. struct list_head transaction_list;
  185. struct timer_list flush_timer;
  186. unsigned long reset_jiffies;
  187. unsigned long long guid;
  188. unsigned max_receive;
  189. int link_speed;
  190. int config_rom_generation;
  191. spinlock_t lock; /* Take this lock when handling the lists in
  192. * this struct. */
  193. struct fw_node *local_node;
  194. struct fw_node *root_node;
  195. struct fw_node *irm_node;
  196. u8 color; /* must be u8 to match the definition in struct fw_node */
  197. int gap_count;
  198. bool beta_repeaters_present;
  199. /*
  200. * Set if the local device is the IRM and the broadcast channel
  201. * was allocated.
  202. */
  203. bool is_irm;
  204. int index;
  205. struct list_head link;
  206. /* Work struct for BM duties. */
  207. struct delayed_work work;
  208. int bm_retries;
  209. int bm_generation;
  210. u32 broadcast_channel;
  211. u32 topology_map[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4];
  212. };
  213. static inline struct fw_card *fw_card_get(struct fw_card *card)
  214. {
  215. kref_get(&card->kref);
  216. return card;
  217. }
  218. void fw_card_release(struct kref *kref);
  219. static inline void fw_card_put(struct fw_card *card)
  220. {
  221. kref_put(&card->kref, fw_card_release);
  222. }
  223. extern void fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
  224. /*
  225. * Check whether new_generation is the immediate successor of old_generation.
  226. * Take counter roll-over at 255 (as per to OHCI) into account.
  227. */
  228. static inline bool is_next_generation(int new_generation, int old_generation)
  229. {
  230. return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
  231. }
  232. /*
  233. * The iso packet format allows for an immediate header/payload part
  234. * stored in 'header' immediately after the packet info plus an
  235. * indirect payload part that is pointer to by the 'payload' field.
  236. * Applications can use one or the other or both to implement simple
  237. * low-bandwidth streaming (e.g. audio) or more advanced
  238. * scatter-gather streaming (e.g. assembling video frame automatically).
  239. */
  240. struct fw_iso_packet {
  241. u16 payload_length; /* Length of indirect payload. */
  242. u32 interrupt : 1; /* Generate interrupt on this packet */
  243. u32 skip : 1; /* Set to not send packet at all. */
  244. u32 tag : 2;
  245. u32 sy : 4;
  246. u32 header_length : 8; /* Length of immediate header. */
  247. u32 header[0];
  248. };
  249. #define FW_ISO_CONTEXT_TRANSMIT 0
  250. #define FW_ISO_CONTEXT_RECEIVE 1
  251. #define FW_ISO_CONTEXT_MATCH_TAG0 1
  252. #define FW_ISO_CONTEXT_MATCH_TAG1 2
  253. #define FW_ISO_CONTEXT_MATCH_TAG2 4
  254. #define FW_ISO_CONTEXT_MATCH_TAG3 8
  255. #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
  256. struct fw_iso_context;
  257. typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
  258. u32 cycle, size_t header_length,
  259. void *header, void *data);
  260. /*
  261. * An iso buffer is just a set of pages mapped for DMA in the
  262. * specified direction. Since the pages are to be used for DMA, they
  263. * are not mapped into the kernel virtual address space. We store the
  264. * DMA address in the page private. The helper function
  265. * fw_iso_buffer_map() will map the pages into a given vma.
  266. */
  267. struct fw_iso_buffer {
  268. enum dma_data_direction direction;
  269. struct page **pages;
  270. int page_count;
  271. };
  272. struct fw_iso_context {
  273. struct fw_card *card;
  274. int type;
  275. int channel;
  276. int speed;
  277. size_t header_size;
  278. fw_iso_callback_t callback;
  279. void *callback_data;
  280. };
  281. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  282. int page_count, enum dma_data_direction direction);
  283. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
  284. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  285. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  286. int type, int channel, int speed, size_t header_size,
  287. fw_iso_callback_t callback, void *callback_data);
  288. int fw_iso_context_queue(struct fw_iso_context *ctx,
  289. struct fw_iso_packet *packet,
  290. struct fw_iso_buffer *buffer,
  291. unsigned long payload);
  292. int fw_iso_context_start(struct fw_iso_context *ctx,
  293. int cycle, int sync, int tags);
  294. int fw_iso_context_stop(struct fw_iso_context *ctx);
  295. void fw_iso_context_destroy(struct fw_iso_context *ctx);
  296. void fw_iso_resource_manage(struct fw_card *card, int generation,
  297. u64 channels_mask, int *channel, int *bandwidth, bool allocate);
  298. struct fw_card_driver {
  299. /*
  300. * Enable the given card with the given initial config rom.
  301. * This function is expected to activate the card, and either
  302. * enable the PHY or set the link_on bit and initiate a bus
  303. * reset.
  304. */
  305. int (*enable)(struct fw_card *card, u32 *config_rom, size_t length);
  306. int (*update_phy_reg)(struct fw_card *card, int address,
  307. int clear_bits, int set_bits);
  308. /*
  309. * Update the config rom for an enabled card. This function
  310. * should change the config rom that is presented on the bus
  311. * an initiate a bus reset.
  312. */
  313. int (*set_config_rom)(struct fw_card *card,
  314. u32 *config_rom, size_t length);
  315. void (*send_request)(struct fw_card *card, struct fw_packet *packet);
  316. void (*send_response)(struct fw_card *card, struct fw_packet *packet);
  317. /* Calling cancel is valid once a packet has been submitted. */
  318. int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
  319. /*
  320. * Allow the specified node ID to do direct DMA out and in of
  321. * host memory. The card will disable this for all node when
  322. * a bus reset happens, so driver need to reenable this after
  323. * bus reset. Returns 0 on success, -ENODEV if the card
  324. * doesn't support this, -ESTALE if the generation doesn't
  325. * match.
  326. */
  327. int (*enable_phys_dma)(struct fw_card *card,
  328. int node_id, int generation);
  329. u64 (*get_bus_time)(struct fw_card *card);
  330. struct fw_iso_context *
  331. (*allocate_iso_context)(struct fw_card *card,
  332. int type, int channel, size_t header_size);
  333. void (*free_iso_context)(struct fw_iso_context *ctx);
  334. int (*start_iso)(struct fw_iso_context *ctx,
  335. s32 cycle, u32 sync, u32 tags);
  336. int (*queue_iso)(struct fw_iso_context *ctx,
  337. struct fw_iso_packet *packet,
  338. struct fw_iso_buffer *buffer,
  339. unsigned long payload);
  340. int (*stop_iso)(struct fw_iso_context *ctx);
  341. };
  342. int fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
  343. void fw_send_request(struct fw_card *card, struct fw_transaction *t,
  344. int tcode, int destination_id, int generation, int speed,
  345. unsigned long long offset, void *payload, size_t length,
  346. fw_transaction_callback_t callback, void *callback_data);
  347. void fw_send_stream_packet(struct fw_card *card, struct fw_packet *p,
  348. int generation, int speed, int channel, int sy, int tag,
  349. void *payload, size_t length, fw_packet_callback_t callback);
  350. int fw_cancel_transaction(struct fw_card *card,
  351. struct fw_transaction *transaction);
  352. void fw_flush_transactions(struct fw_card *card);
  353. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  354. int generation, int speed, unsigned long long offset,
  355. void *payload, size_t length);
  356. void fw_send_phy_config(struct fw_card *card,
  357. int node_id, int generation, int gap_count);
  358. /*
  359. * Called by the topology code to inform the device code of node
  360. * activity; found, lost, or updated nodes.
  361. */
  362. void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  363. /* API used by card level drivers */
  364. void fw_card_initialize(struct fw_card *card,
  365. const struct fw_card_driver *driver, struct device *device);
  366. int fw_card_add(struct fw_card *card,
  367. u32 max_receive, u32 link_speed, u64 guid);
  368. void fw_core_remove_card(struct fw_card *card);
  369. void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
  370. int generation, int self_id_count, u32 *self_ids);
  371. void fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  372. void fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  373. extern int fw_irm_set_broadcast_channel_register(struct device *dev,
  374. void *data);
  375. #endif /* __fw_transaction_h */