fw-transaction.h 13 KB

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