Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _FIREWIRE_FWSERIAL_H
0003 #define _FIREWIRE_FWSERIAL_H
0004 
0005 #include <linux/kernel.h>
0006 #include <linux/tty.h>
0007 #include <linux/tty_driver.h>
0008 #include <linux/tty_flip.h>
0009 #include <linux/list.h>
0010 #include <linux/firewire.h>
0011 #include <linux/firewire-constants.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/rcupdate.h>
0014 #include <linux/mutex.h>
0015 #include <linux/serial.h>
0016 #include <linux/serial_reg.h>
0017 #include <linux/module.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/debugfs.h>
0020 
0021 #include "dma_fifo.h"
0022 
0023 #ifdef FWTTY_PROFILING
0024 #define DISTRIBUTION_MAX_SIZE     8192
0025 #define DISTRIBUTION_MAX_INDEX    (ilog2(DISTRIBUTION_MAX_SIZE) + 1)
0026 static inline void fwtty_profile_data(unsigned int stat[], unsigned int val)
0027 {
0028     int n = (val) ? min(ilog2(val) + 1, DISTRIBUTION_MAX_INDEX) : 0;
0029     ++stat[n];
0030 }
0031 #else
0032 #define DISTRIBUTION_MAX_INDEX    0
0033 #define fwtty_profile_data(st, n)
0034 #endif
0035 
0036 /* Parameters for both VIRT_CABLE_PLUG & VIRT_CABLE_PLUG_RSP mgmt codes */
0037 struct virt_plug_params {
0038     __be32  status_hi;
0039     __be32  status_lo;
0040     __be32  fifo_hi;
0041     __be32  fifo_lo;
0042     __be32  fifo_len;
0043 };
0044 
0045 struct peer_work_params {
0046     union {
0047         struct virt_plug_params plug_req;
0048     };
0049 };
0050 
0051 /**
0052  * fwtty_peer: structure representing local & remote unit devices
0053  * @unit: unit child device of fw_device node
0054  * @serial: back pointer to associated fw_serial aggregate
0055  * @guid: unique 64-bit guid for this unit device
0056  * @generation: most recent bus generation
0057  * @node_id: most recent node_id
0058  * @speed: link speed of peer (0 = S100, 2 = S400, ... 5 = S3200)
0059  * @mgmt_addr: bus addr region to write mgmt packets to
0060  * @status_addr: bus addr register to write line status to
0061  * @fifo_addr: bus addr region to write serial output to
0062  * @fifo_len:  max length for single write to fifo_addr
0063  * @list: link for insertion into fw_serial's peer_list
0064  * @rcu: for deferring peer reclamation
0065  * @lock: spinlock to synchonize changes to state & port fields
0066  * @work: only one work item can be queued at any one time
0067  *        Note: pending work is canceled prior to removal, so this
0068  *        peer is valid for at least the lifetime of the work function
0069  * @work_params: parameter block for work functions
0070  * @timer: timer for resetting peer state if remote request times out
0071  * @state: current state
0072  * @connect: work item for auto-connecting
0073  * @connect_retries: # of connections already attempted
0074  * @port: associated tty_port (usable if state == FWSC_ATTACHED)
0075  */
0076 struct fwtty_peer {
0077     struct fw_unit      *unit;
0078     struct fw_serial    *serial;
0079     u64         guid;
0080     int         generation;
0081     int         node_id;
0082     unsigned int        speed;
0083     int         max_payload;
0084     u64         mgmt_addr;
0085 
0086     /* these are usable only if state == FWSC_ATTACHED */
0087     u64         status_addr;
0088     u64         fifo_addr;
0089     int         fifo_len;
0090 
0091     struct list_head    list;
0092     struct rcu_head     rcu;
0093 
0094     spinlock_t      lock;
0095     work_func_t     workfn;
0096     struct work_struct  work;
0097     struct peer_work_params work_params;
0098     struct timer_list   timer;
0099     int         state;
0100     struct delayed_work connect;
0101     int         connect_retries;
0102 
0103     struct fwtty_port   *port;
0104 };
0105 
0106 #define to_peer(ptr, field) (container_of(ptr, struct fwtty_peer, field))
0107 
0108 /* state values for fwtty_peer.state field */
0109 enum fwtty_peer_state {
0110     FWPS_GONE,
0111     FWPS_NOT_ATTACHED,
0112     FWPS_ATTACHED,
0113     FWPS_PLUG_PENDING,
0114     FWPS_PLUG_RESPONDING,
0115     FWPS_UNPLUG_PENDING,
0116     FWPS_UNPLUG_RESPONDING,
0117 
0118     FWPS_NO_MGMT_ADDR = -1,
0119 };
0120 
0121 #define CONNECT_RETRY_DELAY HZ
0122 #define MAX_CONNECT_RETRIES 10
0123 
0124 /* must be holding peer lock for these state funclets */
0125 static inline void peer_set_state(struct fwtty_peer *peer, int new)
0126 {
0127     peer->state = new;
0128 }
0129 
0130 static inline struct fwtty_port *peer_revert_state(struct fwtty_peer *peer)
0131 {
0132     struct fwtty_port *port = peer->port;
0133 
0134     peer->port = NULL;
0135     peer_set_state(peer, FWPS_NOT_ATTACHED);
0136     return port;
0137 }
0138 
0139 struct fwserial_mgmt_pkt {
0140     struct {
0141         __be16      len;
0142         __be16      code;
0143     } hdr;
0144     union {
0145         struct virt_plug_params plug_req;
0146         struct virt_plug_params plug_rsp;
0147     };
0148 } __packed;
0149 
0150 /* fwserial_mgmt_packet codes */
0151 #define FWSC_RSP_OK         0x0000
0152 #define FWSC_RSP_NACK           0x8000
0153 #define FWSC_CODE_MASK          0x0fff
0154 
0155 #define FWSC_VIRT_CABLE_PLUG        1
0156 #define FWSC_VIRT_CABLE_UNPLUG      2
0157 #define FWSC_VIRT_CABLE_PLUG_RSP    3
0158 #define FWSC_VIRT_CABLE_UNPLUG_RSP  4
0159 
0160 /* 1 min. plug timeout -- suitable for userland authorization */
0161 #define VIRT_CABLE_PLUG_TIMEOUT     (60 * HZ)
0162 
0163 struct stats {
0164     unsigned int    xchars;
0165     unsigned int    dropped;
0166     unsigned int    tx_stall;
0167     unsigned int    fifo_errs;
0168     unsigned int    sent;
0169     unsigned int    lost;
0170     unsigned int    throttled;
0171     unsigned int    reads[DISTRIBUTION_MAX_INDEX + 1];
0172     unsigned int    writes[DISTRIBUTION_MAX_INDEX + 1];
0173     unsigned int    txns[DISTRIBUTION_MAX_INDEX + 1];
0174     unsigned int    unthrottle[DISTRIBUTION_MAX_INDEX + 1];
0175 };
0176 
0177 struct fwconsole_ops {
0178     void (*notify)(int code, void *data);
0179     void (*stats)(struct stats *stats, void *data);
0180     void (*proc_show)(struct seq_file *m, void *data);
0181 };
0182 
0183 /* codes for console ops notify */
0184 #define FWCON_NOTIFY_ATTACH     1
0185 #define FWCON_NOTIFY_DETACH     2
0186 
0187 /**
0188  * fwtty_port: structure used to track/represent underlying tty_port
0189  * @port: underlying tty_port
0190  * @device: tty device
0191  * @index: index into port_table for this particular port
0192  *    note: minor = index + minor_start assigned by tty_alloc_driver()
0193  * @serial: back pointer to the containing fw_serial
0194  * @rx_handler: bus address handler for unique addr region used by remotes
0195  *              to communicate with this port. Every port uses
0196  *      fwtty_port_handler() for per port transactions.
0197  * @fwcon_ops: ops for attached fw_console (if any)
0198  * @con_data: private data for fw_console
0199  * @wait_tx: waitqueue for sleeping until writer/drain completes tx
0200  * @emit_breaks: delayed work responsible for generating breaks when the
0201  *               break line status is active
0202  * @cps : characters per second computed from the termios settings
0203  * @break_last: timestamp in jiffies from last emit_breaks
0204  * @hangup: work responsible for HUPing when carrier is dropped/lost
0205  * @mstatus: loose virtualization of LSR/MSR
0206  *         bits 15..0  correspond to TIOCM_* bits
0207  *         bits 19..16 reserved for mctrl
0208  *         bit 20      OOB_TX_THROTTLE
0209  *     bits 23..21 reserved
0210  *         bits 31..24 correspond to UART_LSR_* bits
0211  * @lock: spinlock for protecting concurrent access to fields below it
0212  * @mctrl: loose virtualization of MCR
0213  *         bits 15..0  correspond to TIOCM_* bits
0214  *         bit 16      OOB_RX_THROTTLE
0215  *         bits 19..17 reserved
0216  *     bits 31..20 reserved for mstatus
0217  * @drain: delayed work scheduled to ensure that writes are flushed.
0218  *         The work can race with the writer but concurrent sending is
0219  *         prevented with the IN_TX flag. Scheduled under lock to
0220  *         limit scheduling when fifo has just been drained.
0221  * @tx_fifo: fifo used to store & block-up writes for dma to remote
0222  * @max_payload: max bytes transmissible per dma (based on peer's max_payload)
0223  * @status_mask: UART_LSR_* bitmask significant to rx (based on termios)
0224  * @ignore_mask: UART_LSR_* bitmask of states to ignore (also based on termios)
0225  * @break_ctl: if set, port is 'sending break' to remote
0226  * @write_only: self-explanatory
0227  * @overrun: previous rx was lost (partially or completely)
0228  * @loopback: if set, port is in loopback mode
0229  * @flags: atomic bit flags
0230  *         bit 0: IN_TX - gate to allow only one cpu to send from the dma fifo
0231  *                        at a time.
0232  *         bit 1: STOP_TX - force tx to exit while sending
0233  * @peer: rcu-pointer to associated fwtty_peer (if attached)
0234  *        NULL if no peer attached
0235  * @icount: predefined statistics reported by the TIOCGICOUNT ioctl
0236  * @stats: additional statistics reported in /proc/tty/driver/firewire_serial
0237  */
0238 struct fwtty_port {
0239     struct tty_port        port;
0240     struct device          *device;
0241     unsigned int           index;
0242     struct fw_serial       *serial;
0243     struct fw_address_handler  rx_handler;
0244 
0245     struct fwconsole_ops       *fwcon_ops;
0246     void               *con_data;
0247 
0248     wait_queue_head_t      wait_tx;
0249     struct delayed_work    emit_breaks;
0250     unsigned int           cps;
0251     unsigned long          break_last;
0252 
0253     struct work_struct     hangup;
0254 
0255     unsigned int           mstatus;
0256 
0257     spinlock_t         lock;
0258     unsigned int           mctrl;
0259     struct delayed_work    drain;
0260     struct dma_fifo        tx_fifo;
0261     int            max_payload;
0262     unsigned int           status_mask;
0263     unsigned int           ignore_mask;
0264     unsigned int           break_ctl:1,
0265                    write_only:1,
0266                    overrun:1,
0267                    loopback:1;
0268     unsigned long          flags;
0269 
0270     struct fwtty_peer __rcu    *peer;
0271 
0272     struct async_icount    icount;
0273     struct stats           stats;
0274 };
0275 
0276 #define to_port(ptr, field) (container_of(ptr, struct fwtty_port, field))
0277 
0278 /* bit #s for flags field */
0279 #define IN_TX                      0
0280 #define STOP_TX                    1
0281 
0282 /* bitmasks for special mctrl/mstatus bits */
0283 #define OOB_RX_THROTTLE   0x00010000
0284 #define MCTRL_RSRVD       0x000e0000
0285 #define OOB_TX_THROTTLE   0x00100000
0286 #define MSTATUS_RSRVD     0x00e00000
0287 
0288 #define MCTRL_MASK        (TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 | TIOCM_OUT2 | \
0289                TIOCM_LOOP | OOB_RX_THROTTLE | MCTRL_RSRVD)
0290 
0291 /* XXX even every 1/50th secs. may be unnecessarily accurate */
0292 /* delay in jiffies between brk emits */
0293 #define FREQ_BREAKS        (HZ / 50)
0294 
0295 /* Ports are allocated in blocks of num_ports for each fw_card */
0296 #define MAX_CARD_PORTS           CONFIG_FWTTY_MAX_CARD_PORTS
0297 #define MAX_TOTAL_PORTS          CONFIG_FWTTY_MAX_TOTAL_PORTS
0298 
0299 /* tuning parameters */
0300 #define FWTTY_PORT_TXFIFO_LEN   4096
0301 #define FWTTY_PORT_MAX_PEND_DMA    8    /* costs a cache line per pend */
0302 #define DRAIN_THRESHOLD         1024
0303 #define MAX_ASYNC_PAYLOAD       4096    /* ohci-defined limit          */
0304 #define WRITER_MINIMUM           128
0305 /* TODO: how to set watermark to AR context size? see fwtty_rx() */
0306 #define HIGH_WATERMARK         32768    /* AR context is 32K           */
0307 
0308 /*
0309  * Size of bus addr region above 4GB used per port as the recv addr
0310  * - must be at least as big as the MAX_ASYNC_PAYLOAD
0311  */
0312 #define FWTTY_PORT_RXFIFO_LEN   MAX_ASYNC_PAYLOAD
0313 
0314 /**
0315  * fw_serial: aggregate used to associate tty ports with specific fw_card
0316  * @card: fw_card associated with this fw_serial device (1:1 association)
0317  * @kref: reference-counted multi-port management allows delayed destroy
0318  * @self: local unit device as 'peer'. Not valid until local unit device
0319  *         is enumerated.
0320  * @list: link for insertion into fwserial_list
0321  * @peer_list: list of local & remote unit devices attached to this card
0322  * @ports: fixed array of tty_ports provided by this serial device
0323  */
0324 struct fw_serial {
0325     struct fw_card    *card;
0326     struct kref   kref;
0327 
0328     struct dentry     *debugfs;
0329     struct fwtty_peer *self;
0330 
0331     struct list_head  list;
0332     struct list_head  peer_list;
0333 
0334     struct fwtty_port *ports[MAX_CARD_PORTS];
0335 };
0336 
0337 #define to_serial(ptr, field)   (container_of(ptr, struct fw_serial, field))
0338 
0339 #define TTY_DEV_NAME            "fwtty" /* ttyFW was taken           */
0340 static const char tty_dev_name[] =  TTY_DEV_NAME;
0341 static const char loop_dev_name[] = "fwloop";
0342 
0343 extern struct tty_driver *fwtty_driver;
0344 
0345 /*
0346  * Returns the max send async payload size in bytes based on the unit device
0347  * link speed. Self-limiting asynchronous bandwidth (via reducing the payload)
0348  * is not necessary and does not work, because
0349  *   1) asynchronous traffic will absorb all available bandwidth (less that
0350  *  being used for isochronous traffic)
0351  *   2) isochronous arbitration always wins.
0352  */
0353 static inline int link_speed_to_max_payload(unsigned int speed)
0354 {
0355     /* Max async payload is 4096 - see IEEE 1394-2008 tables 6-4, 16-18 */
0356     return min(512 << speed, 4096);
0357 }
0358 
0359 #endif /* _FIREWIRE_FWSERIAL_H */