fw-transaction.h 13 KB

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