fw-transaction.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 key;
  146. const u32 *data;
  147. };
  148. int fw_core_add_descriptor (struct fw_descriptor *desc);
  149. void fw_core_remove_descriptor (struct fw_descriptor *desc);
  150. typedef void (*fw_packet_callback_t) (struct fw_packet *packet,
  151. struct fw_card *card, int status);
  152. typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
  153. void *data,
  154. size_t length,
  155. void *callback_data);
  156. typedef void (*fw_address_callback_t)(struct fw_card *card,
  157. struct fw_request *request,
  158. int tcode, int destination, int source,
  159. int generation, int speed,
  160. unsigned long long offset,
  161. void *data, size_t length,
  162. void *callback_data);
  163. typedef void (*fw_bus_reset_callback_t)(struct fw_card *handle,
  164. int node_id, int generation,
  165. u32 *self_ids,
  166. int self_id_count,
  167. void *callback_data);
  168. struct fw_packet {
  169. int speed;
  170. int generation;
  171. u32 header[4];
  172. size_t header_length;
  173. void *payload;
  174. size_t payload_length;
  175. u32 timestamp;
  176. dma_addr_t payload_bus;
  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. };
  188. struct fw_transaction {
  189. int node_id; /* The generation is implied; it is always the current. */
  190. int tlabel;
  191. int timestamp;
  192. struct list_head link;
  193. struct fw_packet packet;
  194. /* The data passed to the callback is valid only during the
  195. * callback. */
  196. fw_transaction_callback_t callback;
  197. void *callback_data;
  198. };
  199. static inline struct fw_packet *
  200. fw_packet(struct list_head *l)
  201. {
  202. return list_entry (l, struct fw_packet, link);
  203. }
  204. struct fw_address_handler {
  205. u64 offset;
  206. size_t length;
  207. fw_address_callback_t address_callback;
  208. void *callback_data;
  209. struct list_head link;
  210. };
  211. struct fw_address_region {
  212. u64 start;
  213. u64 end;
  214. };
  215. extern const struct fw_address_region fw_low_memory_region;
  216. extern const struct fw_address_region fw_high_memory_region;
  217. extern const struct fw_address_region fw_private_region;
  218. extern const struct fw_address_region fw_csr_region;
  219. extern const struct fw_address_region fw_unit_space_region;
  220. int fw_core_add_address_handler(struct fw_address_handler *handler,
  221. const struct fw_address_region *region);
  222. void fw_core_remove_address_handler(struct fw_address_handler *handler);
  223. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  224. int rcode, void *payload, size_t length);
  225. void fw_send_response(struct fw_card *card,
  226. struct fw_request *request, int rcode);
  227. extern struct bus_type fw_bus_type;
  228. struct fw_card {
  229. const struct fw_card_driver *driver;
  230. struct device *device;
  231. int node_id;
  232. int generation;
  233. /* This is the generation used for timestamping incoming requests. */
  234. int request_generation;
  235. int current_tlabel, tlabel_mask;
  236. struct list_head transaction_list;
  237. struct timer_list flush_timer;
  238. unsigned long reset_jiffies;
  239. unsigned long long guid;
  240. int max_receive;
  241. int link_speed;
  242. int config_rom_generation;
  243. /* We need to store up to 4 self ID for a maximum of 63 devices. */
  244. int self_id_count;
  245. u32 self_ids[252];
  246. spinlock_t lock; /* Take this lock when handling the lists in
  247. * this struct. */
  248. struct fw_node *local_node;
  249. struct fw_node *root_node;
  250. struct fw_node *irm_node;
  251. int color;
  252. int gap_count;
  253. int topology_type;
  254. int index;
  255. struct device card_device;
  256. struct list_head link;
  257. /* Work struct for BM duties. */
  258. struct delayed_work work;
  259. int bm_retries;
  260. int bm_generation;
  261. };
  262. struct fw_card *fw_card_get(struct fw_card *card);
  263. void fw_card_put(struct fw_card *card);
  264. /* The iso packet format allows for an immediate header/payload part
  265. * stored in 'header' immediately after the packet info plus an
  266. * indirect payload part that is pointer to by the 'payload' field.
  267. * Applications can use one or the other or both to implement simple
  268. * low-bandwidth streaming (e.g. audio) or more advanced
  269. * scatter-gather streaming (e.g. assembling video frame automatically). */
  270. struct fw_iso_packet {
  271. u16 payload_length; /* Length of indirect payload. */
  272. u32 interrupt : 1; /* Generate interrupt on this packet */
  273. u32 skip : 1; /* Set to not send packet at all. */
  274. u32 tag : 2;
  275. u32 sy : 4;
  276. u32 header_length : 8; /* Length of immediate header. */
  277. u32 header[0];
  278. };
  279. #define FW_ISO_CONTEXT_TRANSMIT 0
  280. #define FW_ISO_CONTEXT_RECEIVE 1
  281. struct fw_iso_context;
  282. typedef void (*fw_iso_callback_t) (struct fw_iso_context *context,
  283. int status, u32 cycle, void *data);
  284. /* An iso buffer is just a set of pages mapped for DMA in the
  285. * specified direction. Since the pages are to be used for DMA, they
  286. * are not mapped into the kernel virtual address space. We store the
  287. * DMA address in the page private. The helper function
  288. * fw_iso_buffer_map() will map the pages into a given vma. */
  289. struct fw_iso_buffer {
  290. enum dma_data_direction direction;
  291. struct page **pages;
  292. int page_count;
  293. };
  294. struct fw_iso_context {
  295. struct fw_card *card;
  296. int type;
  297. int channel;
  298. int speed;
  299. size_t header_size;
  300. fw_iso_callback_t callback;
  301. void *callback_data;
  302. };
  303. int
  304. fw_iso_buffer_init(struct fw_iso_buffer *buffer,
  305. struct fw_card *card,
  306. int page_count,
  307. enum dma_data_direction direction);
  308. int
  309. fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
  310. void
  311. fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  312. struct fw_iso_context *
  313. fw_iso_context_create(struct fw_card *card, int type, size_t header_size,
  314. fw_iso_callback_t callback, void *callback_data);
  315. void
  316. fw_iso_context_destroy(struct fw_iso_context *ctx);
  317. int
  318. fw_iso_context_queue(struct fw_iso_context *ctx,
  319. struct fw_iso_packet *packet,
  320. struct fw_iso_buffer *buffer,
  321. unsigned long payload);
  322. int
  323. fw_iso_context_start(struct fw_iso_context *ctx,
  324. int channel, int speed, int cycle);
  325. int
  326. fw_iso_context_stop(struct fw_iso_context *ctx);
  327. struct fw_card_driver {
  328. const char *name;
  329. /* Enable the given card with the given initial config rom.
  330. * This function is expected to activate the card, and either
  331. * enable the PHY or set the link_on bit and initiate a bus
  332. * reset. */
  333. int (*enable) (struct fw_card *card, u32 *config_rom, size_t length);
  334. int (*update_phy_reg) (struct fw_card *card, int address,
  335. int clear_bits, int set_bits);
  336. /* Update the config rom for an enabled card. This function
  337. * should change the config rom that is presented on the bus
  338. * an initiate a bus reset. */
  339. int (*set_config_rom) (struct fw_card *card,
  340. u32 *config_rom, size_t length);
  341. void (*send_request) (struct fw_card *card, struct fw_packet *packet);
  342. void (*send_response) (struct fw_card *card, struct fw_packet *packet);
  343. /* Calling cancel is valid once a packet has been submitted. */
  344. int (*cancel_packet) (struct fw_card *card, struct fw_packet *packet);
  345. /* Allow the specified node ID to do direct DMA out and in of
  346. * host memory. The card will disable this for all node when
  347. * a bus reset happens, so driver need to reenable this after
  348. * bus reset. Returns 0 on success, -ENODEV if the card
  349. * doesn't support this, -ESTALE if the generation doesn't
  350. * match. */
  351. int (*enable_phys_dma) (struct fw_card *card,
  352. int node_id, int generation);
  353. struct fw_iso_context *
  354. (*allocate_iso_context)(struct fw_card *card, int type);
  355. void (*free_iso_context)(struct fw_iso_context *ctx);
  356. int (*start_iso)(struct fw_iso_context *ctx, s32 cycle);
  357. int (*queue_iso)(struct fw_iso_context *ctx,
  358. struct fw_iso_packet *packet,
  359. struct fw_iso_buffer *buffer,
  360. unsigned long payload);
  361. int (*stop_iso)(struct fw_iso_context *ctx);
  362. };
  363. int
  364. fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
  365. void
  366. fw_send_request(struct fw_card *card, struct fw_transaction *t,
  367. int tcode, int node_id, int generation, int speed,
  368. unsigned long long offset,
  369. void *data, size_t length,
  370. fw_transaction_callback_t callback, void *callback_data);
  371. int fw_cancel_transaction(struct fw_card *card,
  372. struct fw_transaction *transaction);
  373. void fw_flush_transactions(struct fw_card *card);
  374. void fw_send_phy_config(struct fw_card *card,
  375. int node_id, int generation, int gap_count);
  376. /* Called by the topology code to inform the device code of node
  377. * activity; found, lost, or updated nodes */
  378. void
  379. fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  380. /* API used by card level drivers */
  381. /* Do we need phy speed here also? If we add more args, maybe we
  382. should go back to struct fw_card_info. */
  383. void
  384. fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
  385. struct device *device);
  386. int
  387. fw_card_add(struct fw_card *card,
  388. u32 max_receive, u32 link_speed, u64 guid);
  389. void
  390. fw_core_remove_card(struct fw_card *card);
  391. void
  392. fw_core_handle_bus_reset(struct fw_card *card,
  393. int node_id, int generation,
  394. int self_id_count, u32 *self_ids);
  395. void
  396. fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  397. void
  398. fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  399. #endif /* __fw_transaction_h */