fw-transaction.h 13 KB

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