mac802154: cleanup address filtering flags
[firefly-linux-kernel-4.4.55.git] / include / net / mac802154.h
1 /*
2  * IEEE802.15.4-2003 specification
3  *
4  * Copyright (C) 2007-2012 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 #ifndef NET_MAC802154_H
17 #define NET_MAC802154_H
18
19 #include <net/af_ieee802154.h>
20 #include <linux/ieee802154.h>
21 #include <linux/skbuff.h>
22 #include <linux/unaligned/memmove.h>
23
24 #include <net/cfg802154.h>
25
26 /* General MAC frame format:
27  *  2 bytes: Frame Control
28  *  1 byte:  Sequence Number
29  * 20 bytes: Addressing fields
30  * 14 bytes: Auxiliary Security Header
31  */
32 #define MAC802154_FRAME_HARD_HEADER_LEN         (2 + 1 + 20 + 14)
33
34 /**
35  * enum ieee802154_hw_addr_filt_flags - hardware address filtering flags
36  *
37  * The following flags are used to indicate changed address settings from
38  * the stack to the hardware.
39  *
40  * @IEEE802154_AFILT_SADDR_CHANGED: Indicates that the short address will be
41  *      change.
42  *
43  * @IEEE802154_AFILT_IEEEADDR_CHANGED: Indicates that the extended address
44  *      will be change.
45  *
46  * @IEEE802154_AFILT_PANID_CHANGED: Indicates that the pan id will be change.
47  *
48  * @IEEE802154_AFILT_PANC_CHANGED: Indicates that the address filter will
49  *      do frame address filtering as a pan coordinator.
50  */
51 enum ieee802154_hw_addr_filt_flags {
52         IEEE802154_AFILT_SADDR_CHANGED          = BIT(1),
53         IEEE802154_AFILT_IEEEADDR_CHANGED       = BIT(2),
54         IEEE802154_AFILT_PANID_CHANGED          = BIT(3),
55         IEEE802154_AFILT_PANC_CHANGED           = BIT(4),
56 };
57
58 struct ieee802154_hw_addr_filt {
59         __le16  pan_id;         /* Each independent PAN selects a unique
60                                  * identifier. This PAN id allows communication
61                                  * between devices within a network using short
62                                  * addresses and enables transmissions between
63                                  * devices across independent networks.
64                                  */
65         __le16  short_addr;
66         __le64  ieee_addr;
67         u8      pan_coord;
68 };
69
70 struct ieee802154_hw {
71         /* filled by the driver */
72         int     extra_tx_headroom;
73         u32     flags;
74         struct  device *parent;
75
76         /* filled by mac802154 core */
77         struct  ieee802154_hw_addr_filt hw_filt;
78         void    *priv;
79         struct  wpan_phy *phy;
80 };
81
82 /* Checksum is in hardware and is omitted from a packet
83  *
84  * These following flags are used to indicate hardware capabilities to
85  * the stack. Generally, flags here should have their meaning
86  * done in a way that the simplest hardware doesn't need setting
87  * any particular flags. There are some exceptions to this rule,
88  * however, so you are advised to review these flags carefully.
89  */
90
91 /* Indicates that xmitter will add FCS on it's own. */
92 #define IEEE802154_HW_TX_OMIT_CKSUM     0x00000001
93 /* Indicates that receiver will autorespond with ACK frames. */
94 #define IEEE802154_HW_AACK              0x00000002
95 /* Indicates that transceiver will support listen before transmit. */
96 #define IEEE802154_HW_LBT               0x00000004
97 /* Indicates that transceiver will support csma (max_be, min_be, csma retries)
98  * settings. */
99 #define IEEE802154_HW_CSMA_PARAMS       0x00000008
100 /* Indicates that transceiver will support ARET frame retries setting. */
101 #define IEEE802154_HW_FRAME_RETRIES     0x00000010
102 /* Indicates that transceiver will support hardware address filter setting. */
103 #define IEEE802154_HW_AFILT             0x00000020
104 /* Indicates that transceiver will support promiscuous mode setting. */
105 #define IEEE802154_HW_PROMISCUOUS       0x00000040
106 /* Indicates that receiver omits FCS. */
107 #define IEEE802154_HW_RX_OMIT_CKSUM     0x00000080
108 /* Indicates that receiver will not filter frames with bad checksum. */
109 #define IEEE802154_HW_RX_DROP_BAD_CKSUM 0x00000100
110
111 /* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
112 #define IEEE802154_HW_OMIT_CKSUM        (IEEE802154_HW_TX_OMIT_CKSUM | \
113                                          IEEE802154_HW_RX_OMIT_CKSUM)
114
115 /* struct ieee802154_ops - callbacks from mac802154 to the driver
116  *
117  * This structure contains various callbacks that the driver may
118  * handle or, in some cases, must handle, for example to transmit
119  * a frame.
120  *
121  * start: Handler that 802.15.4 module calls for device initialization.
122  *        This function is called before the first interface is attached.
123  *
124  * stop:  Handler that 802.15.4 module calls for device cleanup.
125  *        This function is called after the last interface is removed.
126  *
127  * xmit_sync:
128  *        Handler that 802.15.4 module calls for each transmitted frame.
129  *        skb cntains the buffer starting from the IEEE 802.15.4 header.
130  *        The low-level driver should send the frame based on available
131  *        configuration. This is called by a workqueue and useful for
132  *        synchronous 802.15.4 drivers.
133  *        This function should return zero or negative errno.
134  *
135  *        WARNING:
136  *        This will be deprecated soon. We don't accept synced xmit callbacks
137  *        drivers anymore.
138  *
139  * xmit_async:
140  *        Handler that 802.15.4 module calls for each transmitted frame.
141  *        skb cntains the buffer starting from the IEEE 802.15.4 header.
142  *        The low-level driver should send the frame based on available
143  *        configuration.
144  *        This function should return zero or negative errno.
145  *
146  * ed:    Handler that 802.15.4 module calls for Energy Detection.
147  *        This function should place the value for detected energy
148  *        (usually device-dependant) in the level pointer and return
149  *        either zero or negative errno. Called with pib_lock held.
150  *
151  * set_channel:
152  *        Set radio for listening on specific channel.
153  *        Set the device for listening on specified channel.
154  *        Returns either zero, or negative errno. Called with pib_lock held.
155  *
156  * set_hw_addr_filt:
157  *        Set radio for listening on specific address.
158  *        Set the device for listening on specified address.
159  *        Returns either zero, or negative errno.
160  *
161  * set_txpower:
162  *        Set radio transmit power in mBm. Called with pib_lock held.
163  *        Returns either zero, or negative errno.
164  *
165  * set_lbt
166  *        Enables or disables listen before talk on the device. Called with
167  *        pib_lock held.
168  *        Returns either zero, or negative errno.
169  *
170  * set_cca_mode
171  *        Sets the CCA mode used by the device. Called with pib_lock held.
172  *        Returns either zero, or negative errno.
173  *
174  * set_cca_ed_level
175  *        Sets the CCA energy detection threshold in mBm. Called with pib_lock
176  *        held.
177  *        Returns either zero, or negative errno.
178  *
179  * set_csma_params
180  *        Sets the CSMA parameter set for the PHY. Called with pib_lock held.
181  *        Returns either zero, or negative errno.
182  *
183  * set_frame_retries
184  *        Sets the retransmission attempt limit. Called with pib_lock held.
185  *        Returns either zero, or negative errno.
186  *
187  * set_promiscuous_mode
188  *        Enables or disable promiscuous mode.
189  */
190 struct ieee802154_ops {
191         struct module   *owner;
192         int             (*start)(struct ieee802154_hw *hw);
193         void            (*stop)(struct ieee802154_hw *hw);
194         int             (*xmit_sync)(struct ieee802154_hw *hw,
195                                      struct sk_buff *skb);
196         int             (*xmit_async)(struct ieee802154_hw *hw,
197                                       struct sk_buff *skb);
198         int             (*ed)(struct ieee802154_hw *hw, u8 *level);
199         int             (*set_channel)(struct ieee802154_hw *hw, u8 page,
200                                        u8 channel);
201         int             (*set_hw_addr_filt)(struct ieee802154_hw *hw,
202                                             struct ieee802154_hw_addr_filt *filt,
203                                             unsigned long changed);
204         int             (*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
205         int             (*set_lbt)(struct ieee802154_hw *hw, bool on);
206         int             (*set_cca_mode)(struct ieee802154_hw *hw,
207                                         const struct wpan_phy_cca *cca);
208         int             (*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
209         int             (*set_csma_params)(struct ieee802154_hw *hw,
210                                            u8 min_be, u8 max_be, u8 retries);
211         int             (*set_frame_retries)(struct ieee802154_hw *hw,
212                                              s8 retries);
213         int             (*set_promiscuous_mode)(struct ieee802154_hw *hw,
214                                                 const bool on);
215 };
216
217 /**
218  * ieee802154_be64_to_le64 - copies and convert be64 to le64
219  * @le64_dst: le64 destination pointer
220  * @be64_src: be64 source pointer
221  */
222 static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
223 {
224         __put_unaligned_memmove64(swab64p(be64_src), le64_dst);
225 }
226
227 /**
228  * ieee802154_le64_to_be64 - copies and convert le64 to be64
229  * @be64_dst: be64 destination pointer
230  * @le64_src: le64 source pointer
231  */
232 static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
233 {
234         __put_unaligned_memmove64(swab64p(le64_src), be64_dst);
235 }
236
237 /**
238  * ieee802154_alloc_hw - Allocate a new hardware device
239  *
240  * This must be called once for each hardware device. The returned pointer
241  * must be used to refer to this device when calling other functions.
242  * mac802154 allocates a private data area for the driver pointed to by
243  * @priv in &struct ieee802154_hw, the size of this area is given as
244  * @priv_data_len.
245  *
246  * @priv_data_len: length of private data
247  * @ops: callbacks for this device
248  *
249  * Return: A pointer to the new hardware device, or %NULL on error.
250  */
251 struct ieee802154_hw *
252 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
253
254 /**
255  * ieee802154_free_hw - free hardware descriptor
256  *
257  * This function frees everything that was allocated, including the
258  * private data for the driver. You must call ieee802154_unregister_hw()
259  * before calling this function.
260  *
261  * @hw: the hardware to free
262  */
263 void ieee802154_free_hw(struct ieee802154_hw *hw);
264
265 /**
266  * ieee802154_register_hw - Register hardware device
267  *
268  * You must call this function before any other functions in
269  * mac802154. Note that before a hardware can be registered, you
270  * need to fill the contained wpan_phy's information.
271  *
272  * @hw: the device to register as returned by ieee802154_alloc_hw()
273  *
274  * Return: 0 on success. An error code otherwise.
275  */
276 int ieee802154_register_hw(struct ieee802154_hw *hw);
277
278 /**
279  * ieee802154_unregister_hw - Unregister a hardware device
280  *
281  * This function instructs mac802154 to free allocated resources
282  * and unregister netdevices from the networking subsystem.
283  *
284  * @hw: the hardware to unregister
285  */
286 void ieee802154_unregister_hw(struct ieee802154_hw *hw);
287
288 /**
289  * ieee802154_rx - receive frame
290  *
291  * Use this function to hand received frames to mac802154. The receive
292  * buffer in @skb must start with an IEEE 802.15.4 header. In case of a
293  * paged @skb is used, the driver is recommended to put the ieee802154
294  * header of the frame on the linear part of the @skb to avoid memory
295  * allocation and/or memcpy by the stack.
296  *
297  * This function may not be called in IRQ context. Calls to this function
298  * for a single hardware must be synchronized against each other.
299  *
300  * @hw: the hardware this frame came in on
301  * @skb: the buffer to receive, owned by mac802154 after this call
302  */
303 void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb);
304
305 /**
306  * ieee802154_rx_irqsafe - receive frame
307  *
308  * Like ieee802154_rx() but can be called in IRQ context
309  * (internally defers to a tasklet.)
310  *
311  * @hw: the hardware this frame came in on
312  * @skb: the buffer to receive, owned by mac802154 after this call
313  * @lqi: link quality indicator
314  */
315 void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
316                            u8 lqi);
317 /**
318  * ieee802154_wake_queue - wake ieee802154 queue
319  * @hw: pointer as obtained from ieee802154_alloc_hw().
320  *
321  * Drivers should use this function instead of netif_wake_queue.
322  */
323 void ieee802154_wake_queue(struct ieee802154_hw *hw);
324
325 /**
326  * ieee802154_stop_queue - stop ieee802154 queue
327  * @hw: pointer as obtained from ieee802154_alloc_hw().
328  *
329  * Drivers should use this function instead of netif_stop_queue.
330  */
331 void ieee802154_stop_queue(struct ieee802154_hw *hw);
332
333 /**
334  * ieee802154_xmit_complete - frame transmission complete
335  *
336  * @hw: pointer as obtained from ieee802154_alloc_hw().
337  * @skb: buffer for transmission
338  * @ifs_handling: indicate interframe space handling
339  */
340 void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
341                               bool ifs_handling);
342
343 #endif /* NET_MAC802154_H */