fw-transaction.h 13 KB

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