xbus-core.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Written by Oron Peled <oron@actcom.co.il>
  3. * Copyright (C) 2004-2006, Xorcom
  4. *
  5. * All rights reserved.
  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
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #ifndef XBUS_CORE_H
  23. #define XBUS_CORE_H
  24. #include <linux/wait.h>
  25. #include <linux/interrupt.h> /* for tasklets */
  26. #include <linux/kref.h>
  27. #include "xpd.h"
  28. #include "xframe_queue.h"
  29. #include "xbus-pcm.h"
  30. #define MAX_BUSES 32
  31. #define XFRAME_DATASIZE 512
  32. /* forward declarations */
  33. struct xbus_workqueue;
  34. #ifdef XPP_DEBUGFS
  35. struct debugfs_data;
  36. #endif
  37. #ifdef __KERNEL__
  38. struct xbus_ops {
  39. int (*xframe_send_pcm)(xbus_t *xbus, xframe_t *xframe);
  40. int (*xframe_send_cmd)(xbus_t *xbus, xframe_t *xframe);
  41. xframe_t *(*alloc_xframe)(xbus_t *xbus, gfp_t gfp_flags);
  42. void (*free_xframe)(xbus_t *xbus, xframe_t *xframe);
  43. };
  44. /*
  45. * XBUS statistics counters
  46. */
  47. enum {
  48. XBUS_N_UNITS,
  49. XBUS_N_TX_XFRAME_PCM,
  50. XBUS_N_RX_XFRAME_PCM,
  51. XBUS_N_TX_PACK_PCM,
  52. XBUS_N_RX_PACK_PCM,
  53. XBUS_N_TX_BYTES,
  54. XBUS_N_RX_BYTES,
  55. XBUS_N_TX_PCM_FRAG,
  56. XBUS_N_RX_CMD,
  57. XBUS_N_TX_CMD,
  58. };
  59. #define XBUS_COUNTER(xbus, counter) ((xbus)->counters[XBUS_N_ ## counter])
  60. #define C_(x) [ XBUS_N_ ## x ] = { #x }
  61. /* yucky, make an instance so we can size it... */
  62. static struct xbus_counters {
  63. char *name;
  64. } xbus_counters[] = {
  65. C_(UNITS),
  66. C_(TX_XFRAME_PCM),
  67. C_(RX_XFRAME_PCM),
  68. C_(TX_PACK_PCM),
  69. C_(RX_PACK_PCM),
  70. C_(TX_BYTES),
  71. C_(RX_BYTES),
  72. C_(TX_PCM_FRAG),
  73. C_(RX_CMD),
  74. C_(TX_CMD),
  75. };
  76. #undef C_
  77. #define XBUS_COUNTER_MAX ARRAY_SIZE(xbus_counters)
  78. enum xbus_state {
  79. XBUS_STATE_START,
  80. XBUS_STATE_IDLE,
  81. XBUS_STATE_SENT_REQUEST,
  82. XBUS_STATE_RECVD_DESC,
  83. XBUS_STATE_READY,
  84. XBUS_STATE_DEACTIVATING,
  85. XBUS_STATE_DEACTIVATED,
  86. XBUS_STATE_FAIL,
  87. };
  88. const char *xbus_statename(enum xbus_state st);
  89. struct xbus_transport {
  90. struct xbus_ops *ops;
  91. void *priv;
  92. struct device *transport_device;
  93. ushort max_send_size;
  94. enum xbus_state xbus_state;
  95. unsigned long transport_flags;
  96. spinlock_t state_lock;
  97. atomic_t transport_refcount;
  98. wait_queue_head_t transport_unused;
  99. spinlock_t lock;
  100. };
  101. #define MAX_SEND_SIZE(xbus) ((xbus)->transport.max_send_size)
  102. #define XBUS_STATE(xbus) ((xbus)->transport.xbus_state)
  103. #define XBUS_IS(xbus, st) (XBUS_STATE(xbus) == XBUS_STATE_ ## st)
  104. #define TRANSPORT_EXIST(xbus) ((xbus)->transport.ops != NULL)
  105. #define XBUS_FLAG_CONNECTED 0
  106. #define XBUS_FLAGS(xbus, flg) test_bit(XBUS_FLAG_ ## flg, &((xbus)->transport.transport_flags))
  107. struct xbus_ops *transportops_get(xbus_t *xbus);
  108. void transportops_put(xbus_t *xbus);
  109. /*
  110. * Encapsulate all poll related data of a single xbus.
  111. */
  112. struct xbus_workqueue {
  113. struct workqueue_struct *wq;
  114. struct work_struct xpds_init_work;
  115. bool xpds_init_done;
  116. struct list_head card_list;
  117. int num_units;
  118. int num_units_initialized;
  119. wait_queue_head_t wait_for_xpd_initialization;
  120. #ifdef CONFIG_PROC_FS
  121. #ifdef OLD_PROC
  122. struct proc_dir_entry *proc_xbus_waitfor_xpds;
  123. #endif
  124. #endif
  125. spinlock_t worker_lock;
  126. struct semaphore running_initialization;
  127. };
  128. /*
  129. * Allocate/Free an xframe from pools of empty xframes.
  130. * Calls to {get,put}_xframe are wrapped in
  131. * the macros bellow, so we take/return it
  132. * to the correct pool.
  133. */
  134. xframe_t *get_xframe(struct xframe_queue *q);
  135. void put_xframe(struct xframe_queue *q, xframe_t *xframe);
  136. #define ALLOC_SEND_XFRAME(xbus) get_xframe(&(xbus)->send_pool)
  137. #define ALLOC_RECV_XFRAME(xbus) get_xframe(&(xbus)->receive_pool)
  138. #define FREE_SEND_XFRAME(xbus, xframe) put_xframe(&(xbus)->send_pool, (xframe))
  139. #define FREE_RECV_XFRAME(xbus, xframe) put_xframe(&(xbus)->receive_pool, (xframe))
  140. xbus_t *xbus_num(uint num);
  141. xbus_t *get_xbus(const char *msg, uint num);
  142. void put_xbus(const char *msg, xbus_t *xbus);
  143. int refcount_xbus(xbus_t *xbus);
  144. /*
  145. * An xbus is a transport layer for Xorcom Protocol commands
  146. */
  147. struct xbus {
  148. char busname[XBUS_NAMELEN]; /* set by xbus_new() */
  149. /* low-level bus drivers set these 2 fields */
  150. char connector[XBUS_DESCLEN];
  151. char label[LABEL_SIZE];
  152. byte revision; /* Protocol revision */
  153. struct xbus_transport transport;
  154. int num;
  155. struct xpd *xpds[MAX_XPDS];
  156. int command_tick_counter;
  157. int usec_nosend; /* Firmware flow control */
  158. struct xframe_queue command_queue;
  159. wait_queue_head_t command_queue_empty;
  160. struct xframe_queue send_pool; /* empty xframes for send */
  161. struct xframe_queue receive_pool; /* empty xframes for receive */
  162. /* tasklet processing */
  163. struct xframe_queue receive_queue;
  164. struct tasklet_struct receive_tasklet;
  165. int cpu_rcv_intr[NR_CPUS];
  166. int cpu_rcv_tasklet[NR_CPUS];
  167. bool self_ticking;
  168. enum sync_mode sync_mode;
  169. /* Managed by low-level drivers: */
  170. enum sync_mode sync_mode_default;
  171. struct timer_list command_timer;
  172. unsigned int xbus_frag_count;
  173. struct xframe_queue pcm_tospan;
  174. struct xpp_ticker ticker; /* for tick rate */
  175. struct xpp_drift drift; /* for tick offset */
  176. atomic_t pcm_rx_counter;
  177. unsigned int global_counter;
  178. /* Device-Model */
  179. struct device astribank;
  180. #define dev_to_xbus(dev) container_of(dev, struct xbus, astribank)
  181. struct kref kref;
  182. #define kref_to_xbus(k) container_of(k, struct xbus, kref)
  183. spinlock_t lock;
  184. /* PCM metrics */
  185. struct timeval last_tx_sync;
  186. struct timeval last_rx_sync;
  187. unsigned long max_tx_sync;
  188. unsigned long min_tx_sync;
  189. unsigned long max_rx_sync;
  190. unsigned long min_rx_sync;
  191. unsigned long max_rx_process; /* packet processing time (usec) */
  192. #ifdef SAMPLE_TICKS
  193. #define SAMPLE_SIZE 1000
  194. int sample_ticks[SAMPLE_SIZE];
  195. bool sample_running;
  196. int sample_pos;
  197. #endif
  198. struct xbus_workqueue worker;
  199. /*
  200. * Sync adjustment
  201. */
  202. int sync_adjustment;
  203. int sync_adjustment_offset;
  204. long pll_updated_at;
  205. atomic_t num_xpds;
  206. #ifdef XPP_DEBUGFS
  207. struct dentry *debugfs_dir;
  208. struct dentry *debugfs_file;
  209. struct debugfs_data *debugfs_data;
  210. #endif
  211. #ifdef CONFIG_PROC_FS
  212. struct proc_dir_entry *proc_xbus_dir;
  213. struct proc_dir_entry *proc_xbus_summary;
  214. #ifdef PROTOCOL_DEBUG
  215. struct proc_dir_entry *proc_xbus_command;
  216. #endif
  217. #endif
  218. /* statistics */
  219. int counters[XBUS_COUNTER_MAX];
  220. };
  221. #endif
  222. #define XFRAME_MAGIC 123456L
  223. struct xframe {
  224. unsigned long xframe_magic;
  225. struct list_head frame_list;
  226. atomic_t frame_len;
  227. xbus_t *xbus;
  228. struct timeval tv_created;
  229. struct timeval tv_queued;
  230. struct timeval tv_submitted;
  231. struct timeval tv_received;
  232. /* filled by transport layer */
  233. size_t frame_maxlen;
  234. byte *packets; /* max XFRAME_DATASIZE */
  235. byte *first_free;
  236. int usec_towait; /* prevent overflowing AB */
  237. void *priv;
  238. };
  239. void xframe_init(xbus_t *xbus, xframe_t *xframe, void *buf, size_t maxsize, void *priv);
  240. #define XFRAME_LEN(frame) atomic_read(&(frame)->frame_len)
  241. int xbus_core_init(void); /* Initializer */
  242. void xbus_core_shutdown(void); /* Terminator */
  243. #ifdef XPP_DEBUGFS
  244. /* Debugfs handling */
  245. int xbus_log(xbus_t *xbus, xpd_t *xpd, int direction, const void *buf, unsigned long len);
  246. #endif
  247. /* Frame handling */
  248. void dump_xframe(const char msg[], const xbus_t *xbus, const xframe_t *xframe, int debug);
  249. int send_cmd_frame(xbus_t *xbus, xframe_t *xframe);
  250. /*
  251. * Return pointer to next packet slot in the frame
  252. * or NULL if the frame is full.
  253. */
  254. xpacket_t *xframe_next_packet(xframe_t *xframe, int len);
  255. /* XBUS handling */
  256. /*
  257. * Map: unit+subunit <--> index in xbus->xpds[]
  258. */
  259. #define XPD_IDX(unit,subunit) ((unit) * MAX_SUBUNIT + (subunit))
  260. #define XBUS_UNIT(idx) ((idx) / MAX_SUBUNIT)
  261. #define XBUS_SUBUNIT(idx) ((idx) % MAX_SUBUNIT)
  262. xpd_t *xpd_of(const xbus_t *xbus, int xpd_num);
  263. xpd_t *xpd_byaddr(const xbus_t *xbus, uint unit, uint subunit);
  264. int xbus_check_unique(xbus_t *xbus);
  265. bool xbus_setstate(xbus_t *xbus, enum xbus_state newstate);
  266. bool xbus_setflags(xbus_t *xbus, int flagbit, bool on);
  267. xbus_t *xbus_new(struct xbus_ops *ops, ushort max_send_size, struct device *transport_device, void *priv);
  268. void xbus_free(xbus_t *xbus);
  269. int xbus_connect(xbus_t *xbus);
  270. int xbus_activate(xbus_t *xbus);
  271. void xbus_deactivate(xbus_t *xbus);
  272. void xbus_disconnect(xbus_t *xbus);
  273. void xbus_receive_xframe(xbus_t *xbus, xframe_t *xframe);
  274. int xbus_process_worker(xbus_t *xbus);
  275. int waitfor_xpds(xbus_t *xbus, char *buf);
  276. int xbus_xpd_bind(xbus_t *xbus, xpd_t *xpd, int unit, int subunit);
  277. int xbus_xpd_unbind(xbus_t *xbus, xpd_t *xpd);
  278. /* sysfs */
  279. int xpd_device_register(xbus_t *xbus, xpd_t *xpd);
  280. void xpd_device_unregister(xpd_t *xpd);
  281. int xpp_driver_init(void);
  282. void xpp_driver_exit(void);
  283. int xbus_sysfs_transport_create(xbus_t *xbus);
  284. void xbus_sysfs_transport_remove(xbus_t *xbus);
  285. int xbus_sysfs_create(xbus_t *xbus);
  286. void xbus_sysfs_remove(xbus_t *xbus);
  287. #ifdef OLD_HOTPLUG_SUPPORT_269
  288. /* Copy from new kernels lib/kobject_uevent.c */
  289. enum kobject_action {
  290. KOBJ_ADD,
  291. KOBJ_REMOVE,
  292. KOBJ_CHANGE,
  293. KOBJ_MOUNT,
  294. KOBJ_UMOUNT,
  295. KOBJ_OFFLINE,
  296. KOBJ_ONLINE,
  297. };
  298. #endif
  299. void astribank_uevent_send(xbus_t *xbus, enum kobject_action act);
  300. #endif /* XBUS_CORE_H */