fwserial.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #ifndef _FIREWIRE_FWSERIAL_H
  2. #define _FIREWIRE_FWSERIAL_H
  3. #include <linux/kernel.h>
  4. #include <linux/tty.h>
  5. #include <linux/tty_driver.h>
  6. #include <linux/tty_flip.h>
  7. #include <linux/list.h>
  8. #include <linux/firewire.h>
  9. #include <linux/firewire-constants.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/rcupdate.h>
  12. #include <linux/mutex.h>
  13. #include <linux/serial.h>
  14. #include <linux/serial_reg.h>
  15. #include <linux/module.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/debugfs.h>
  18. #include "dma_fifo.h"
  19. #ifdef FWTTY_PROFILING
  20. #define DISTRIBUTION_MAX_SIZE 8192
  21. #define DISTRIBUTION_MAX_INDEX (ilog2(DISTRIBUTION_MAX_SIZE) + 1)
  22. static inline void fwtty_profile_data(unsigned stat[], unsigned val)
  23. {
  24. int n = (val) ? min(ilog2(val) + 1, DISTRIBUTION_MAX_INDEX) : 0;
  25. ++stat[n];
  26. }
  27. #else
  28. #define DISTRIBUTION_MAX_INDEX 0
  29. #define fwtty_profile_data(st, n)
  30. #endif
  31. /* Parameters for both VIRT_CABLE_PLUG & VIRT_CABLE_PLUG_RSP mgmt codes */
  32. struct virt_plug_params {
  33. __be32 status_hi;
  34. __be32 status_lo;
  35. __be32 fifo_hi;
  36. __be32 fifo_lo;
  37. __be32 fifo_len;
  38. };
  39. struct peer_work_params {
  40. union {
  41. struct virt_plug_params plug_req;
  42. };
  43. };
  44. /**
  45. * fwtty_peer: structure representing local & remote unit devices
  46. * @unit: unit child device of fw_device node
  47. * @serial: back pointer to associated fw_serial aggregate
  48. * @guid: unique 64-bit guid for this unit device
  49. * @generation: most recent bus generation
  50. * @node_id: most recent node_id
  51. * @speed: link speed of peer (0 = S100, 2 = S400, ... 5 = S3200)
  52. * @mgmt_addr: bus addr region to write mgmt packets to
  53. * @status_addr: bus addr register to write line status to
  54. * @fifo_addr: bus addr region to write serial output to
  55. * @fifo_len: max length for single write to fifo_addr
  56. * @list: link for insertion into fw_serial's peer_list
  57. * @rcu: for deferring peer reclamation
  58. * @lock: spinlock to synchonize changes to state & port fields
  59. * @work: only one work item can be queued at any one time
  60. * Note: pending work is canceled prior to removal, so this
  61. * peer is valid for at least the lifetime of the work function
  62. * @work_params: parameter block for work functions
  63. * @timer: timer for resetting peer state if remote request times out
  64. * @state: current state
  65. * @connect: work item for auto-connecting
  66. * @connect_retries: # of connections already attempted
  67. * @port: associated tty_port (usable if state == FWSC_ATTACHED)
  68. */
  69. struct fwtty_peer {
  70. struct fw_unit *unit;
  71. struct fw_serial *serial;
  72. u64 guid;
  73. int generation;
  74. int node_id;
  75. unsigned speed;
  76. int max_payload;
  77. u64 mgmt_addr;
  78. /* these are usable only if state == FWSC_ATTACHED */
  79. u64 status_addr;
  80. u64 fifo_addr;
  81. int fifo_len;
  82. struct list_head list;
  83. struct rcu_head rcu;
  84. spinlock_t lock;
  85. work_func_t workfn;
  86. struct work_struct work;
  87. struct peer_work_params work_params;
  88. struct timer_list timer;
  89. int state;
  90. struct delayed_work connect;
  91. int connect_retries;
  92. struct fwtty_port *port;
  93. };
  94. #define to_peer(ptr, field) (container_of(ptr, struct fwtty_peer, field))
  95. /* state values for fwtty_peer.state field */
  96. enum fwtty_peer_state {
  97. FWPS_GONE,
  98. FWPS_NOT_ATTACHED,
  99. FWPS_ATTACHED,
  100. FWPS_PLUG_PENDING,
  101. FWPS_PLUG_RESPONDING,
  102. FWPS_UNPLUG_PENDING,
  103. FWPS_UNPLUG_RESPONDING,
  104. FWPS_NO_MGMT_ADDR = -1,
  105. };
  106. #define CONNECT_RETRY_DELAY HZ
  107. #define MAX_CONNECT_RETRIES 10
  108. /* must be holding peer lock for these state funclets */
  109. static inline void peer_set_state(struct fwtty_peer *peer, int new)
  110. {
  111. peer->state = new;
  112. }
  113. static inline struct fwtty_port *peer_revert_state(struct fwtty_peer *peer)
  114. {
  115. struct fwtty_port *port = peer->port;
  116. peer->port = NULL;
  117. peer_set_state(peer, FWPS_NOT_ATTACHED);
  118. return port;
  119. }
  120. struct fwserial_mgmt_pkt {
  121. struct {
  122. __be16 len;
  123. __be16 code;
  124. } hdr;
  125. union {
  126. struct virt_plug_params plug_req;
  127. struct virt_plug_params plug_rsp;
  128. };
  129. } __packed;
  130. /* fwserial_mgmt_packet codes */
  131. #define FWSC_RSP_OK 0x0000
  132. #define FWSC_RSP_NACK 0x8000
  133. #define FWSC_CODE_MASK 0x0fff
  134. #define FWSC_VIRT_CABLE_PLUG 1
  135. #define FWSC_VIRT_CABLE_UNPLUG 2
  136. #define FWSC_VIRT_CABLE_PLUG_RSP 3
  137. #define FWSC_VIRT_CABLE_UNPLUG_RSP 4
  138. /* 1 min. plug timeout -- suitable for userland authorization */
  139. #define VIRT_CABLE_PLUG_TIMEOUT (60 * HZ)
  140. struct stats {
  141. unsigned xchars;
  142. unsigned dropped;
  143. unsigned tx_stall;
  144. unsigned fifo_errs;
  145. unsigned sent;
  146. unsigned lost;
  147. unsigned throttled;
  148. unsigned reads[DISTRIBUTION_MAX_INDEX + 1];
  149. unsigned writes[DISTRIBUTION_MAX_INDEX + 1];
  150. unsigned txns[DISTRIBUTION_MAX_INDEX + 1];
  151. unsigned unthrottle[DISTRIBUTION_MAX_INDEX + 1];
  152. };
  153. struct fwconsole_ops {
  154. void (*notify)(int code, void *data);
  155. void (*stats)(struct stats *stats, void *data);
  156. void (*proc_show)(struct seq_file *m, void *data);
  157. };
  158. /* codes for console ops notify */
  159. #define FWCON_NOTIFY_ATTACH 1
  160. #define FWCON_NOTIFY_DETACH 2
  161. /**
  162. * fwtty_port: structure used to track/represent underlying tty_port
  163. * @port: underlying tty_port
  164. * @device: tty device
  165. * @index: index into port_table for this particular port
  166. * note: minor = index + minor_start assigned by tty_alloc_driver()
  167. * @serial: back pointer to the containing fw_serial
  168. * @rx_handler: bus address handler for unique addr region used by remotes
  169. * to communicate with this port. Every port uses
  170. * fwtty_port_handler() for per port transactions.
  171. * @fwcon_ops: ops for attached fw_console (if any)
  172. * @con_data: private data for fw_console
  173. * @wait_tx: waitqueue for sleeping until writer/drain completes tx
  174. * @emit_breaks: delayed work responsible for generating breaks when the
  175. * break line status is active
  176. * @cps : characters per second computed from the termios settings
  177. * @break_last: timestamp in jiffies from last emit_breaks
  178. * @hangup: work responsible for HUPing when carrier is dropped/lost
  179. * @mstatus: loose virtualization of LSR/MSR
  180. * bits 15..0 correspond to TIOCM_* bits
  181. * bits 19..16 reserved for mctrl
  182. * bit 20 OOB_TX_THROTTLE
  183. * bits 23..21 reserved
  184. * bits 31..24 correspond to UART_LSR_* bits
  185. * @lock: spinlock for protecting concurrent access to fields below it
  186. * @mctrl: loose virtualization of MCR
  187. * bits 15..0 correspond to TIOCM_* bits
  188. * bit 16 OOB_RX_THROTTLE
  189. * bits 19..17 reserved
  190. * bits 31..20 reserved for mstatus
  191. * @drain: delayed work scheduled to ensure that writes are flushed.
  192. * The work can race with the writer but concurrent sending is
  193. * prevented with the IN_TX flag. Scheduled under lock to
  194. * limit scheduling when fifo has just been drained.
  195. * @tx_fifo: fifo used to store & block-up writes for dma to remote
  196. * @max_payload: max bytes transmissible per dma (based on peer's max_payload)
  197. * @status_mask: UART_LSR_* bitmask significant to rx (based on termios)
  198. * @ignore_mask: UART_LSR_* bitmask of states to ignore (also based on termios)
  199. * @break_ctl: if set, port is 'sending break' to remote
  200. * @write_only: self-explanatory
  201. * @overrun: previous rx was lost (partially or completely)
  202. * @loopback: if set, port is in loopback mode
  203. * @flags: atomic bit flags
  204. * bit 0: IN_TX - gate to allow only one cpu to send from the dma fifo
  205. * at a time.
  206. * bit 1: STOP_TX - force tx to exit while sending
  207. * @peer: rcu-pointer to associated fwtty_peer (if attached)
  208. * NULL if no peer attached
  209. * @icount: predefined statistics reported by the TIOCGICOUNT ioctl
  210. * @stats: additional statistics reported in /proc/tty/driver/firewire_serial
  211. */
  212. struct fwtty_port {
  213. struct tty_port port;
  214. struct device *device;
  215. unsigned index;
  216. struct fw_serial *serial;
  217. struct fw_address_handler rx_handler;
  218. struct fwconsole_ops *fwcon_ops;
  219. void *con_data;
  220. wait_queue_head_t wait_tx;
  221. struct delayed_work emit_breaks;
  222. unsigned cps;
  223. unsigned long break_last;
  224. struct work_struct hangup;
  225. unsigned mstatus;
  226. spinlock_t lock;
  227. unsigned mctrl;
  228. struct delayed_work drain;
  229. struct dma_fifo tx_fifo;
  230. int max_payload;
  231. unsigned status_mask;
  232. unsigned ignore_mask;
  233. unsigned break_ctl:1,
  234. write_only:1,
  235. overrun:1,
  236. loopback:1;
  237. unsigned long flags;
  238. struct fwtty_peer __rcu *peer;
  239. struct async_icount icount;
  240. struct stats stats;
  241. };
  242. #define to_port(ptr, field) (container_of(ptr, struct fwtty_port, field))
  243. /* bit #s for flags field */
  244. #define IN_TX 0
  245. #define STOP_TX 1
  246. /* bitmasks for special mctrl/mstatus bits */
  247. #define OOB_RX_THROTTLE 0x00010000
  248. #define MCTRL_RSRVD 0x000e0000
  249. #define OOB_TX_THROTTLE 0x00100000
  250. #define MSTATUS_RSRVD 0x00e00000
  251. #define MCTRL_MASK (TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 | TIOCM_OUT2 | \
  252. TIOCM_LOOP | OOB_RX_THROTTLE | MCTRL_RSRVD)
  253. /* XXX even every 1/50th secs. may be unnecessarily accurate */
  254. /* delay in jiffies between brk emits */
  255. #define FREQ_BREAKS (HZ / 50)
  256. /* Ports are allocated in blocks of num_ports for each fw_card */
  257. #define MAX_CARD_PORTS CONFIG_FWTTY_MAX_CARD_PORTS
  258. #define MAX_TOTAL_PORTS CONFIG_FWTTY_MAX_TOTAL_PORTS
  259. /* tuning parameters */
  260. #define FWTTY_PORT_TXFIFO_LEN 4096
  261. #define FWTTY_PORT_MAX_PEND_DMA 8 /* costs a cache line per pend */
  262. #define DRAIN_THRESHOLD 1024
  263. #define MAX_ASYNC_PAYLOAD 4096 /* ohci-defined limit */
  264. #define WRITER_MINIMUM 128
  265. /* TODO: how to set watermark to AR context size? see fwtty_rx() */
  266. #define HIGH_WATERMARK 32768 /* AR context is 32K */
  267. /*
  268. * Size of bus addr region above 4GB used per port as the recv addr
  269. * - must be at least as big as the MAX_ASYNC_PAYLOAD
  270. */
  271. #define FWTTY_PORT_RXFIFO_LEN MAX_ASYNC_PAYLOAD
  272. /**
  273. * fw_serial: aggregate used to associate tty ports with specific fw_card
  274. * @card: fw_card associated with this fw_serial device (1:1 association)
  275. * @kref: reference-counted multi-port management allows delayed destroy
  276. * @self: local unit device as 'peer'. Not valid until local unit device
  277. * is enumerated.
  278. * @list: link for insertion into fwserial_list
  279. * @peer_list: list of local & remote unit devices attached to this card
  280. * @ports: fixed array of tty_ports provided by this serial device
  281. */
  282. struct fw_serial {
  283. struct fw_card *card;
  284. struct kref kref;
  285. struct dentry *debugfs;
  286. struct fwtty_peer *self;
  287. struct list_head list;
  288. struct list_head peer_list;
  289. struct fwtty_port *ports[MAX_CARD_PORTS];
  290. };
  291. #define to_serial(ptr, field) (container_of(ptr, struct fw_serial, field))
  292. #define TTY_DEV_NAME "fwtty" /* ttyFW was taken */
  293. static const char tty_dev_name[] = TTY_DEV_NAME;
  294. static const char loop_dev_name[] = "fwloop";
  295. extern struct tty_driver *fwtty_driver;
  296. struct fwtty_port *fwtty_port_get(unsigned index);
  297. void fwtty_port_put(struct fwtty_port *port);
  298. static inline void fwtty_bind_console(struct fwtty_port *port,
  299. struct fwconsole_ops *fwcon_ops,
  300. void *data)
  301. {
  302. port->con_data = data;
  303. port->fwcon_ops = fwcon_ops;
  304. }
  305. /*
  306. * Returns the max send async payload size in bytes based on the unit device
  307. * link speed. Self-limiting asynchronous bandwidth (via reducing the payload)
  308. * is not necessary and does not work, because
  309. * 1) asynchronous traffic will absorb all available bandwidth (less that
  310. * being used for isochronous traffic)
  311. * 2) isochronous arbitration always wins.
  312. */
  313. static inline int link_speed_to_max_payload(unsigned speed)
  314. {
  315. /* Max async payload is 4096 - see IEEE 1394-2008 tables 6-4, 16-18 */
  316. return min(512 << speed, 4096);
  317. }
  318. #endif /* _FIREWIRE_FWSERIAL_H */