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