xref: /linux/include/linux/atmdev.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* atmdev.h - ATM device driver declarations and various related items */
3 #ifndef LINUX_ATMDEV_H
4 #define LINUX_ATMDEV_H
5 
6 
7 #include <linux/wait.h> /* wait_queue_head_t */
8 #include <linux/time.h> /* struct timeval */
9 #include <linux/net.h>
10 #include <linux/bug.h>
11 #include <linux/skbuff.h> /* struct sk_buff */
12 #include <linux/uio.h>
13 #include <net/sock.h>
14 #include <linux/atomic.h>
15 #include <linux/refcount.h>
16 #include <uapi/linux/atmdev.h>
17 
18 #ifdef CONFIG_PROC_FS
19 #include <linux/proc_fs.h>
20 
21 extern struct proc_dir_entry *atm_proc_root;
22 #endif
23 
24 #ifdef CONFIG_COMPAT
25 #include <linux/compat.h>
26 struct compat_atm_iobuf {
27 	int length;
28 	compat_uptr_t buffer;
29 };
30 #endif
31 
32 struct k_atm_aal_stats {
33 #define __HANDLE_ITEM(i) atomic_t i
34 	__AAL_STAT_ITEMS
35 #undef __HANDLE_ITEM
36 };
37 
38 
39 struct k_atm_dev_stats {
40 	struct k_atm_aal_stats aal0;
41 	struct k_atm_aal_stats aal34;
42 	struct k_atm_aal_stats aal5;
43 };
44 
45 struct device;
46 
47 enum {
48 	ATM_VF_ADDR,		/* Address is in use. Set by anybody, cleared
49 				   by device driver. */
50 	ATM_VF_READY,		/* VC is ready to transfer data. Set by device
51 				   driver, cleared by anybody. */
52 	ATM_VF_PARTIAL,		/* resources are bound to PVC (partial PVC
53 				   setup), controlled by socket layer */
54 	ATM_VF_HASQOS,		/* QOS parameters have been set */
55 	ATM_VF_CLOSE,		/* asynchronous close - VC is being torn down */
56 };
57 
58 
59 enum {
60 	ATM_DF_REMOVED,		/* device was removed from atm_devs list */
61 };
62 
63 
64 #define ATM_PHY_SIG_LOST    0	/* no carrier/light */
65 #define ATM_PHY_SIG_UNKNOWN 1	/* carrier/light status is unknown */
66 #define ATM_PHY_SIG_FOUND   2	/* carrier/light okay */
67 
68 #define ATM_ATMOPT_CLP	1	/* set CLP bit */
69 
70 struct atm_vcc {
71 	/* struct sock has to be the first member of atm_vcc */
72 	struct sock	sk;
73 	unsigned long	flags;		/* VCC flags (ATM_VF_*) */
74 	short		vpi;		/* VPI and VCI (types must be equal */
75 					/* with sockaddr) */
76 	int 		vci;
77 	unsigned long	aal_options;	/* AAL layer options */
78 	unsigned long	atm_options;	/* ATM layer options */
79 	struct atm_dev	*dev;		/* device back pointer */
80 	struct atm_qos	qos;		/* QOS */
81 	void (*release_cb)(struct atm_vcc *vcc); /* release_sock callback */
82 	void (*push)(struct atm_vcc *vcc,struct sk_buff *skb);
83 	void (*pop)(struct atm_vcc *vcc,struct sk_buff *skb); /* optional */
84 	int (*send)(struct atm_vcc *vcc,struct sk_buff *skb);
85 	void		*dev_data;	/* per-device data */
86 	void		*proto_data;	/* per-protocol data */
87 	struct k_atm_aal_stats *stats;	/* pointer to AAL stats group */
88 	struct module *owner;		/* owner of ->push function */
89 	void		*user_back;	/* user backlink - not touched by the */
90 					/* native ATM stack, used by sch_atm */
91 };
92 
atm_sk(struct sock * sk)93 static inline struct atm_vcc *atm_sk(struct sock *sk)
94 {
95 	return (struct atm_vcc *)sk;
96 }
97 
ATM_SD(struct socket * sock)98 static inline struct atm_vcc *ATM_SD(struct socket *sock)
99 {
100 	return atm_sk(sock->sk);
101 }
102 
sk_atm(struct atm_vcc * vcc)103 static inline struct sock *sk_atm(struct atm_vcc *vcc)
104 {
105 	return (struct sock *)vcc;
106 }
107 
108 struct atm_dev {
109 	const struct atmdev_ops *ops;	/* device operations; NULL if unused */
110 	const char	*type;		/* device type name */
111 	int		number;		/* device index */
112 	void		*dev_data;	/* per-device data */
113 	void		*phy_data;	/* private PHY data */
114 	unsigned long	flags;		/* device flags (ATM_DF_*) */
115 	unsigned char	esi[ESI_LEN];	/* ESI ("MAC" addr) */
116 	struct atm_cirange ci_range;	/* VPI/VCI range */
117 	struct k_atm_dev_stats stats;	/* statistics */
118 	char		signal;		/* signal status (ATM_PHY_SIG_*) */
119 	int		link_rate;	/* link rate (default: OC3) */
120 	refcount_t	refcnt;		/* reference count */
121 #ifdef CONFIG_PROC_FS
122 	struct proc_dir_entry *proc_entry; /* proc entry */
123 	char *proc_name;		/* proc entry name */
124 #endif
125 	struct device class_dev;	/* sysfs device */
126 	struct list_head dev_list;	/* linkage */
127 };
128 
129 struct atmdev_ops { /* only send is required */
130 	void (*dev_close)(struct atm_dev *dev);
131 	int (*open)(struct atm_vcc *vcc);
132 	void (*close)(struct atm_vcc *vcc);
133 	int (*ioctl)(struct atm_dev *dev,unsigned int cmd,void __user *arg);
134 #ifdef CONFIG_COMPAT
135 	int (*compat_ioctl)(struct atm_dev *dev,unsigned int cmd,
136 			    void __user *arg);
137 #endif
138 	int (*send)(struct atm_vcc *vcc,struct sk_buff *skb);
139 	int (*proc_read)(struct atm_dev *dev,loff_t *pos,char *page);
140 	struct module *owner;
141 };
142 
143 struct atm_skb_data {
144 	struct atm_vcc	*vcc;		/* ATM VCC */
145 	unsigned long	atm_options;	/* ATM layer options */
146 	unsigned int	acct_truesize;  /* truesize accounted to vcc */
147 } __packed;
148 
149 #define VCC_HTABLE_SIZE 32
150 
151 extern struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
152 extern rwlock_t vcc_sklist_lock;
153 
154 #define ATM_SKB(skb) (((struct atm_skb_data *) (skb)->cb))
155 
156 struct atm_dev *atm_dev_register(const char *type, struct device *parent,
157 				 const struct atmdev_ops *ops,
158 				 int number, /* -1 == pick first available */
159 				 unsigned long *flags);
160 struct atm_dev *atm_dev_lookup(int number);
161 void atm_dev_deregister(struct atm_dev *dev);
162 
163 /* atm_dev_signal_change
164  *
165  * Propagate lower layer signal change in atm_dev->signal to netdevice.
166  * The event will be sent via a notifier call chain.
167  */
168 void atm_dev_signal_change(struct atm_dev *dev, char signal);
169 
170 void vcc_insert_socket(struct sock *sk);
171 
172 void atm_dev_release_vccs(struct atm_dev *dev);
173 
atm_account_tx(struct atm_vcc * vcc,struct sk_buff * skb)174 static inline void atm_account_tx(struct atm_vcc *vcc, struct sk_buff *skb)
175 {
176 	/*
177 	 * Because ATM skbs may not belong to a sock (and we don't
178 	 * necessarily want to), skb->truesize may be adjusted,
179 	 * escaping the hack in pskb_expand_head() which avoids
180 	 * doing so for some cases. So stash the value of truesize
181 	 * at the time we accounted it, and atm_pop_raw() can use
182 	 * that value later, in case it changes.
183 	 */
184 	refcount_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
185 	ATM_SKB(skb)->acct_truesize = skb->truesize;
186 	ATM_SKB(skb)->atm_options = vcc->atm_options;
187 }
188 
atm_return_tx(struct atm_vcc * vcc,struct sk_buff * skb)189 static inline void atm_return_tx(struct atm_vcc *vcc, struct sk_buff *skb)
190 {
191 	WARN_ON_ONCE(refcount_sub_and_test(ATM_SKB(skb)->acct_truesize,
192 					   &sk_atm(vcc)->sk_wmem_alloc));
193 }
194 
atm_force_charge(struct atm_vcc * vcc,int truesize)195 static inline void atm_force_charge(struct atm_vcc *vcc,int truesize)
196 {
197 	atomic_add(truesize, &sk_atm(vcc)->sk_rmem_alloc);
198 }
199 
200 
atm_return(struct atm_vcc * vcc,int truesize)201 static inline void atm_return(struct atm_vcc *vcc,int truesize)
202 {
203 	atomic_sub(truesize, &sk_atm(vcc)->sk_rmem_alloc);
204 }
205 
206 
atm_may_send(struct atm_vcc * vcc,unsigned int size)207 static inline int atm_may_send(struct atm_vcc *vcc,unsigned int size)
208 {
209 	return (size + refcount_read(&sk_atm(vcc)->sk_wmem_alloc)) <
210 	       sk_atm(vcc)->sk_sndbuf;
211 }
212 
213 
atm_dev_hold(struct atm_dev * dev)214 static inline void atm_dev_hold(struct atm_dev *dev)
215 {
216 	refcount_inc(&dev->refcnt);
217 }
218 
219 
atm_dev_put(struct atm_dev * dev)220 static inline void atm_dev_put(struct atm_dev *dev)
221 {
222 	if (refcount_dec_and_test(&dev->refcnt)) {
223 		BUG_ON(!test_bit(ATM_DF_REMOVED, &dev->flags));
224 		if (dev->ops->dev_close)
225 			dev->ops->dev_close(dev);
226 		put_device(&dev->class_dev);
227 	}
228 }
229 
230 
231 int atm_charge(struct atm_vcc *vcc,int truesize);
232 
233 void vcc_release_async(struct atm_vcc *vcc, int reply);
234 
235 struct atm_ioctl {
236 	struct module *owner;
237 	/* A module reference is kept if appropriate over this call.
238 	 * Return -ENOIOCTLCMD if you don't handle it. */
239 	int (*ioctl)(struct socket *, unsigned int cmd, unsigned long arg);
240 	struct list_head list;
241 };
242 
243 /**
244  * register_atm_ioctl - register handler for ioctl operations
245  * @ioctl: ioctl handler to register
246  *
247  * Special (non-device) handlers of ioctl's should
248  * register here. If you're a normal device, you should
249  * set .ioctl in your atmdev_ops instead.
250  */
251 void register_atm_ioctl(struct atm_ioctl *ioctl);
252 
253 /**
254  * deregister_atm_ioctl - remove the ioctl handler
255  * @ioctl: ioctl handler to deregister
256  */
257 void deregister_atm_ioctl(struct atm_ioctl *ioctl);
258 
259 
260 /* register_atmdevice_notifier - register atm_dev notify events
261  *
262  * Clients like br2684 will register notify events
263  * Currently we notify of signal found/lost
264  */
265 int register_atmdevice_notifier(struct notifier_block *nb);
266 void unregister_atmdevice_notifier(struct notifier_block *nb);
267 
268 #endif
269