fw-transaction.h 13 KB

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