fw-transaction.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-transaction.h - Header for IEEE1394 transaction logic
  4. *
  5. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #ifndef __fw_transaction_h
  22. #define __fw_transaction_h
  23. #include <linux/device.h>
  24. #include <linux/timer.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/list.h>
  27. #include <linux/fs.h>
  28. #include <linux/dma-mapping.h>
  29. #define TCODE_WRITE_QUADLET_REQUEST 0
  30. #define TCODE_WRITE_BLOCK_REQUEST 1
  31. #define TCODE_WRITE_RESPONSE 2
  32. #define TCODE_READ_QUADLET_REQUEST 4
  33. #define TCODE_READ_BLOCK_REQUEST 5
  34. #define TCODE_READ_QUADLET_RESPONSE 6
  35. #define TCODE_READ_BLOCK_RESPONSE 7
  36. #define TCODE_CYCLE_START 8
  37. #define TCODE_LOCK_REQUEST 9
  38. #define TCODE_STREAM_DATA 10
  39. #define TCODE_LOCK_RESPONSE 11
  40. #define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4)
  41. #define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0)
  42. #define TCODE_IS_REQUEST(tcode) (((tcode) & 2) == 0)
  43. #define TCODE_IS_RESPONSE(tcode) (((tcode) & 2) != 0)
  44. #define TCODE_HAS_REQUEST_DATA(tcode) (((tcode) & 12) != 4)
  45. #define TCODE_HAS_RESPONSE_DATA(tcode) (((tcode) & 12) != 0)
  46. /* Juju specific tcodes */
  47. #define TCODE_DEALLOCATE 0x10
  48. #define TCODE_LOCK_MASK_SWAP 0x11
  49. #define TCODE_LOCK_COMPARE_SWAP 0x12
  50. #define TCODE_LOCK_FETCH_ADD 0x13
  51. #define TCODE_LOCK_LITTLE_ADD 0x14
  52. #define TCODE_LOCK_BOUNDED_ADD 0x15
  53. #define TCODE_LOCK_WRAP_ADD 0x16
  54. #define TCODE_LOCK_VENDOR_SPECIFIC 0x17
  55. #define SCODE_100 0x0
  56. #define SCODE_200 0x1
  57. #define SCODE_400 0x2
  58. #define SCODE_BETA 0x3
  59. #define EXTCODE_MASK_SWAP 0x1
  60. #define EXTCODE_COMPARE_SWAP 0x2
  61. #define EXTCODE_FETCH_ADD 0x3
  62. #define EXTCODE_LITTLE_ADD 0x4
  63. #define EXTCODE_BOUNDED_ADD 0x5
  64. #define EXTCODE_WRAP_ADD 0x6
  65. #define ACK_COMPLETE 0x1
  66. #define ACK_PENDING 0x2
  67. #define ACK_BUSY_X 0x4
  68. #define ACK_BUSY_A 0x5
  69. #define ACK_BUSY_B 0x6
  70. #define ACK_DATA_ERROR 0xd
  71. #define ACK_TYPE_ERROR 0xe
  72. #define RCODE_COMPLETE 0x0
  73. #define RCODE_CONFLICT_ERROR 0x4
  74. #define RCODE_DATA_ERROR 0x5
  75. #define RCODE_TYPE_ERROR 0x6
  76. #define RCODE_ADDRESS_ERROR 0x7
  77. /* Juju specific rcodes */
  78. #define RCODE_SEND_ERROR 0x10
  79. #define RCODE_CANCELLED 0x11
  80. #define RCODE_BUSY 0x12
  81. #define RCODE_GENERATION 0x13
  82. #define RCODE_NO_ACK 0x14
  83. #define RETRY_1 0x00
  84. #define RETRY_X 0x01
  85. #define RETRY_A 0x02
  86. #define RETRY_B 0x03
  87. #define LOCAL_BUS 0xffc0
  88. #define SELFID_PORT_CHILD 0x3
  89. #define SELFID_PORT_PARENT 0x2
  90. #define SELFID_PORT_NCONN 0x1
  91. #define SELFID_PORT_NONE 0x0
  92. #define PHY_PACKET_CONFIG 0x0
  93. #define PHY_PACKET_LINK_ON 0x1
  94. #define PHY_PACKET_SELF_ID 0x2
  95. #define CSR_REGISTER_BASE 0xfffff0000000ULL
  96. /* register offsets relative to CSR_REGISTER_BASE */
  97. #define CSR_STATE_CLEAR 0x0
  98. #define CSR_STATE_SET 0x4
  99. #define CSR_NODE_IDS 0x8
  100. #define CSR_RESET_START 0xc
  101. #define CSR_SPLIT_TIMEOUT_HI 0x18
  102. #define CSR_SPLIT_TIMEOUT_LO 0x1c
  103. #define CSR_CYCLE_TIME 0x200
  104. #define CSR_BUS_TIME 0x204
  105. #define CSR_BUSY_TIMEOUT 0x210
  106. #define CSR_BUS_MANAGER_ID 0x21c
  107. #define CSR_BANDWIDTH_AVAILABLE 0x220
  108. #define CSR_CHANNELS_AVAILABLE 0x224
  109. #define CSR_CHANNELS_AVAILABLE_HI 0x224
  110. #define CSR_CHANNELS_AVAILABLE_LO 0x228
  111. #define CSR_BROADCAST_CHANNEL 0x234
  112. #define CSR_CONFIG_ROM 0x400
  113. #define CSR_CONFIG_ROM_END 0x800
  114. #define CSR_FCP_COMMAND 0xB00
  115. #define CSR_FCP_RESPONSE 0xD00
  116. #define CSR_FCP_END 0xF00
  117. #define CSR_TOPOLOGY_MAP 0x1000
  118. #define CSR_TOPOLOGY_MAP_END 0x1400
  119. #define CSR_SPEED_MAP 0x2000
  120. #define CSR_SPEED_MAP_END 0x3000
  121. #define fw_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, ## args)
  122. #define fw_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
  123. #define fw_debug(s, args...) printk(KERN_DEBUG KBUILD_MODNAME ": " s, ## args)
  124. static inline void
  125. fw_memcpy_from_be32(void *_dst, void *_src, size_t size)
  126. {
  127. u32 *dst = _dst;
  128. u32 *src = _src;
  129. int i;
  130. for (i = 0; i < size / 4; i++)
  131. dst[i] = cpu_to_be32(src[i]);
  132. }
  133. static inline void
  134. fw_memcpy_to_be32(void *_dst, void *_src, size_t size)
  135. {
  136. fw_memcpy_from_be32(_dst, _src, size);
  137. }
  138. struct fw_card;
  139. struct fw_packet;
  140. struct fw_node;
  141. struct fw_request;
  142. struct fw_descriptor {
  143. struct list_head link;
  144. size_t length;
  145. u32 immediate;
  146. u32 key;
  147. const u32 *data;
  148. };
  149. int fw_core_add_descriptor (struct fw_descriptor *desc);
  150. void fw_core_remove_descriptor (struct fw_descriptor *desc);
  151. typedef void (*fw_packet_callback_t) (struct fw_packet *packet,
  152. struct fw_card *card, int status);
  153. typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
  154. void *data,
  155. size_t length,
  156. void *callback_data);
  157. typedef void (*fw_address_callback_t)(struct fw_card *card,
  158. struct fw_request *request,
  159. int tcode, int destination, int source,
  160. int generation, int speed,
  161. unsigned long long offset,
  162. void *data, size_t length,
  163. void *callback_data);
  164. typedef void (*fw_bus_reset_callback_t)(struct fw_card *handle,
  165. int node_id, int generation,
  166. u32 *self_ids,
  167. int self_id_count,
  168. void *callback_data);
  169. struct fw_packet {
  170. int speed;
  171. int generation;
  172. u32 header[4];
  173. size_t header_length;
  174. void *payload;
  175. size_t payload_length;
  176. u32 timestamp;
  177. /* This callback is called when the packet transmission has
  178. * completed; for successful transmission, the status code is
  179. * the ack received from the destination, otherwise it's a
  180. * negative errno: ENOMEM, ESTALE, ETIMEDOUT, ENODEV, EIO.
  181. * The callback can be called from tasklet context and thus
  182. * must never block.
  183. */
  184. fw_packet_callback_t callback;
  185. int ack;
  186. struct list_head link;
  187. void *driver_data;
  188. };
  189. struct fw_transaction {
  190. int node_id; /* The generation is implied; it is always the current. */
  191. int tlabel;
  192. int timestamp;
  193. struct list_head link;
  194. struct fw_packet packet;
  195. /* The data passed to the callback is valid only during the
  196. * callback. */
  197. fw_transaction_callback_t callback;
  198. void *callback_data;
  199. };
  200. static inline struct fw_packet *
  201. fw_packet(struct list_head *l)
  202. {
  203. return list_entry (l, struct fw_packet, link);
  204. }
  205. struct fw_address_handler {
  206. u64 offset;
  207. size_t length;
  208. fw_address_callback_t address_callback;
  209. void *callback_data;
  210. struct list_head link;
  211. };
  212. struct fw_address_region {
  213. u64 start;
  214. u64 end;
  215. };
  216. extern const struct fw_address_region fw_low_memory_region;
  217. extern const struct fw_address_region fw_high_memory_region;
  218. extern const struct fw_address_region fw_private_region;
  219. extern const struct fw_address_region fw_csr_region;
  220. extern const struct fw_address_region fw_unit_space_region;
  221. int fw_core_add_address_handler(struct fw_address_handler *handler,
  222. const struct fw_address_region *region);
  223. void fw_core_remove_address_handler(struct fw_address_handler *handler);
  224. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  225. int rcode, void *payload, size_t length);
  226. void fw_send_response(struct fw_card *card,
  227. struct fw_request *request, int rcode);
  228. extern struct bus_type fw_bus_type;
  229. struct fw_card {
  230. const struct fw_card_driver *driver;
  231. struct device *device;
  232. struct kref kref;
  233. int node_id;
  234. int generation;
  235. /* This is the generation used for timestamping incoming requests. */
  236. int request_generation;
  237. int current_tlabel, tlabel_mask;
  238. struct list_head transaction_list;
  239. struct timer_list flush_timer;
  240. unsigned long reset_jiffies;
  241. unsigned long long guid;
  242. int max_receive;
  243. int link_speed;
  244. int config_rom_generation;
  245. /* We need to store up to 4 self ID for a maximum of 63
  246. * devices plus 3 words for the topology map header. */
  247. int self_id_count;
  248. u32 topology_map[252 + 3];
  249. spinlock_t lock; /* Take this lock when handling the lists in
  250. * this struct. */
  251. struct fw_node *local_node;
  252. struct fw_node *root_node;
  253. struct fw_node *irm_node;
  254. int color;
  255. int gap_count;
  256. int topology_type;
  257. int index;
  258. struct list_head link;
  259. /* Work struct for BM duties. */
  260. struct delayed_work work;
  261. int bm_retries;
  262. int bm_generation;
  263. };
  264. struct fw_card *fw_card_get(struct fw_card *card);
  265. void fw_card_put(struct fw_card *card);
  266. /* The iso packet format allows for an immediate header/payload part
  267. * stored in 'header' immediately after the packet info plus an
  268. * indirect payload part that is pointer to by the 'payload' field.
  269. * Applications can use one or the other or both to implement simple
  270. * low-bandwidth streaming (e.g. audio) or more advanced
  271. * scatter-gather streaming (e.g. assembling video frame automatically). */
  272. struct fw_iso_packet {
  273. u16 payload_length; /* Length of indirect payload. */
  274. u32 interrupt : 1; /* Generate interrupt on this packet */
  275. u32 skip : 1; /* Set to not send packet at all. */
  276. u32 tag : 2;
  277. u32 sy : 4;
  278. u32 header_length : 8; /* Length of immediate header. */
  279. u32 header[0];
  280. };
  281. #define FW_ISO_CONTEXT_TRANSMIT 0
  282. #define FW_ISO_CONTEXT_RECEIVE 1
  283. #define FW_ISO_CONTEXT_MATCH_TAG0 1
  284. #define FW_ISO_CONTEXT_MATCH_TAG1 2
  285. #define FW_ISO_CONTEXT_MATCH_TAG2 4
  286. #define FW_ISO_CONTEXT_MATCH_TAG3 8
  287. #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
  288. struct fw_iso_context;
  289. typedef void (*fw_iso_callback_t) (struct fw_iso_context *context,
  290. u32 cycle,
  291. size_t header_length,
  292. void *header,
  293. void *data);
  294. /* An iso buffer is just a set of pages mapped for DMA in the
  295. * specified direction. Since the pages are to be used for DMA, they
  296. * are not mapped into the kernel virtual address space. We store the
  297. * DMA address in the page private. The helper function
  298. * fw_iso_buffer_map() will map the pages into a given vma. */
  299. struct fw_iso_buffer {
  300. enum dma_data_direction direction;
  301. struct page **pages;
  302. int page_count;
  303. };
  304. struct fw_iso_context {
  305. struct fw_card *card;
  306. int type;
  307. int channel;
  308. int speed;
  309. size_t header_size;
  310. fw_iso_callback_t callback;
  311. void *callback_data;
  312. };
  313. int
  314. fw_iso_buffer_init(struct fw_iso_buffer *buffer,
  315. struct fw_card *card,
  316. int page_count,
  317. enum dma_data_direction direction);
  318. int
  319. fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
  320. void
  321. fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  322. struct fw_iso_context *
  323. fw_iso_context_create(struct fw_card *card, int type,
  324. int channel, int speed, size_t header_size,
  325. fw_iso_callback_t callback, void *callback_data);
  326. void
  327. fw_iso_context_destroy(struct fw_iso_context *ctx);
  328. int
  329. fw_iso_context_queue(struct fw_iso_context *ctx,
  330. struct fw_iso_packet *packet,
  331. struct fw_iso_buffer *buffer,
  332. unsigned long payload);
  333. int
  334. fw_iso_context_start(struct fw_iso_context *ctx,
  335. int cycle, int sync, int tags);
  336. int
  337. fw_iso_context_stop(struct fw_iso_context *ctx);
  338. struct fw_card_driver {
  339. const char *name;
  340. /* Enable the given card with the given initial config rom.
  341. * This function is expected to activate the card, and either
  342. * enable the PHY or set the link_on bit and initiate a bus
  343. * reset. */
  344. int (*enable) (struct fw_card *card, u32 *config_rom, size_t length);
  345. int (*update_phy_reg) (struct fw_card *card, int address,
  346. int clear_bits, int set_bits);
  347. /* Update the config rom for an enabled card. This function
  348. * should change the config rom that is presented on the bus
  349. * an initiate a bus reset. */
  350. int (*set_config_rom) (struct fw_card *card,
  351. u32 *config_rom, size_t length);
  352. void (*send_request) (struct fw_card *card, struct fw_packet *packet);
  353. void (*send_response) (struct fw_card *card, struct fw_packet *packet);
  354. /* Calling cancel is valid once a packet has been submitted. */
  355. int (*cancel_packet) (struct fw_card *card, struct fw_packet *packet);
  356. /* Allow the specified node ID to do direct DMA out and in of
  357. * host memory. The card will disable this for all node when
  358. * a bus reset happens, so driver need to reenable this after
  359. * bus reset. Returns 0 on success, -ENODEV if the card
  360. * doesn't support this, -ESTALE if the generation doesn't
  361. * match. */
  362. int (*enable_phys_dma) (struct fw_card *card,
  363. int node_id, int generation);
  364. u64 (*get_bus_time) (struct fw_card *card);
  365. struct fw_iso_context *
  366. (*allocate_iso_context)(struct fw_card *card,
  367. int type, size_t header_size);
  368. void (*free_iso_context)(struct fw_iso_context *ctx);
  369. int (*start_iso)(struct fw_iso_context *ctx,
  370. s32 cycle, u32 sync, u32 tags);
  371. int (*queue_iso)(struct fw_iso_context *ctx,
  372. struct fw_iso_packet *packet,
  373. struct fw_iso_buffer *buffer,
  374. unsigned long payload);
  375. int (*stop_iso)(struct fw_iso_context *ctx);
  376. };
  377. int
  378. fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
  379. void
  380. fw_send_request(struct fw_card *card, struct fw_transaction *t,
  381. int tcode, int node_id, int generation, int speed,
  382. unsigned long long offset,
  383. void *data, size_t length,
  384. fw_transaction_callback_t callback, void *callback_data);
  385. int fw_cancel_transaction(struct fw_card *card,
  386. struct fw_transaction *transaction);
  387. void fw_flush_transactions(struct fw_card *card);
  388. void fw_send_phy_config(struct fw_card *card,
  389. int node_id, int generation, int gap_count);
  390. /* Called by the topology code to inform the device code of node
  391. * activity; found, lost, or updated nodes */
  392. void
  393. fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  394. /* API used by card level drivers */
  395. /* Do we need phy speed here also? If we add more args, maybe we
  396. should go back to struct fw_card_info. */
  397. void
  398. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  399. struct device *device);
  400. int
  401. fw_card_add(struct fw_card *card,
  402. u32 max_receive, u32 link_speed, u64 guid);
  403. void
  404. fw_core_remove_card(struct fw_card *card);
  405. void
  406. fw_core_handle_bus_reset(struct fw_card *card,
  407. int node_id, int generation,
  408. int self_id_count, u32 *self_ids);
  409. void
  410. fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  411. void
  412. fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  413. #endif /* __fw_transaction_h */