fw-transaction.h 12 KB

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