xref: /openbmc/linux/include/net/mac802154.h (revision 372892ec1151c895c7dec362f3246f089690cfc7)
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(0),
53 	IEEE802154_AFILT_IEEEADDR_CHANGED	= BIT(1),
54 	IEEE802154_AFILT_PANID_CHANGED		= BIT(2),
55 	IEEE802154_AFILT_PANC_CHANGED		= BIT(3),
56 };
57 
58 /**
59  * struct ieee802154_hw_addr_filt - hardware address filtering settings
60  *
61  * @pan_id: pan_id which should be set to the hardware address filter.
62  *
63  * @short_addr: short_addr which should be set to the hardware address filter.
64  *
65  * @ieee_addr: extended address which should be set to the hardware address
66  *	filter.
67  *
68  * @pan_coord: boolean if hardware filtering should be operate as coordinator.
69  */
70 struct ieee802154_hw_addr_filt {
71 	__le16	pan_id;
72 	__le16	short_addr;
73 	__le64	ieee_addr;
74 	bool	pan_coord;
75 };
76 
77 /**
78  * struct ieee802154_hw - ieee802154 hardware
79  *
80  * @extra_tx_headroom: headroom to reserve in each transmit skb for use by the
81  *	driver (e.g. for transmit headers.)
82  *
83  * @flags: hardware flags, see &enum ieee802154_hw_flags
84  *
85  * @parent: parent device of the hardware.
86  *
87  * @priv: pointer to private area that was allocated for driver use along with
88  *	this structure.
89  *
90  * @phy: This points to the &struct wpan_phy allocated for this 802.15.4 PHY.
91  */
92 struct ieee802154_hw {
93 	/* filled by the driver */
94 	int	extra_tx_headroom;
95 	u32	flags;
96 	struct	device *parent;
97 	void	*priv;
98 
99 	/* filled by mac802154 core */
100 	struct	wpan_phy *phy;
101 };
102 
103 /**
104  * enum ieee802154_hw_flags - hardware flags
105  *
106  * These flags are used to indicate hardware capabilities to
107  * the stack. Generally, flags here should have their meaning
108  * done in a way that the simplest hardware doesn't need setting
109  * any particular flags. There are some exceptions to this rule,
110  * however, so you are advised to review these flags carefully.
111  *
112  * @IEEE802154_HW_TX_OMIT_CKSUM: Indicates that xmitter will add FCS on it's
113  *	own.
114  *
115  * @IEEE802154_HW_LBT: Indicates that transceiver will support listen before
116  *	transmit.
117  *
118  * @IEEE802154_HW_CSMA_PARAMS: Indicates that transceiver will support csma
119  *	parameters (max_be, min_be, backoff exponents).
120  *
121  * @IEEE802154_HW_FRAME_RETRIES: Indicates that transceiver will support ARET
122  *	frame retries setting.
123  *
124  * @IEEE802154_HW_AFILT: Indicates that transceiver will support hardware
125  *	address filter setting.
126  *
127  * @IEEE802154_HW_PROMISCUOUS: Indicates that transceiver will support
128  *	promiscuous mode setting.
129  *
130  * @IEEE802154_HW_RX_OMIT_CKSUM: Indicates that receiver omits FCS.
131  *
132  * @IEEE802154_HW_RX_DROP_BAD_CKSUM: Indicates that receiver will not filter
133  *	frames with bad checksum.
134  */
135 enum ieee802154_hw_flags {
136 	IEEE802154_HW_TX_OMIT_CKSUM	= BIT(0),
137 	IEEE802154_HW_LBT		= BIT(1),
138 	IEEE802154_HW_CSMA_PARAMS	= BIT(2),
139 	IEEE802154_HW_FRAME_RETRIES	= BIT(3),
140 	IEEE802154_HW_AFILT		= BIT(4),
141 	IEEE802154_HW_PROMISCUOUS	= BIT(5),
142 	IEEE802154_HW_RX_OMIT_CKSUM	= BIT(6),
143 	IEEE802154_HW_RX_DROP_BAD_CKSUM	= BIT(7),
144 };
145 
146 /* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
147 #define IEEE802154_HW_OMIT_CKSUM	(IEEE802154_HW_TX_OMIT_CKSUM | \
148 					 IEEE802154_HW_RX_OMIT_CKSUM)
149 
150 /* struct ieee802154_ops - callbacks from mac802154 to the driver
151  *
152  * This structure contains various callbacks that the driver may
153  * handle or, in some cases, must handle, for example to transmit
154  * a frame.
155  *
156  * start: Handler that 802.15.4 module calls for device initialization.
157  *	  This function is called before the first interface is attached.
158  *
159  * stop:  Handler that 802.15.4 module calls for device cleanup.
160  *	  This function is called after the last interface is removed.
161  *
162  * xmit_sync:
163  *	  Handler that 802.15.4 module calls for each transmitted frame.
164  *	  skb cntains the buffer starting from the IEEE 802.15.4 header.
165  *	  The low-level driver should send the frame based on available
166  *	  configuration. This is called by a workqueue and useful for
167  *	  synchronous 802.15.4 drivers.
168  *	  This function should return zero or negative errno.
169  *
170  *	  WARNING:
171  *	  This will be deprecated soon. We don't accept synced xmit callbacks
172  *	  drivers anymore.
173  *
174  * xmit_async:
175  *	  Handler that 802.15.4 module calls for each transmitted frame.
176  *	  skb cntains the buffer starting from the IEEE 802.15.4 header.
177  *	  The low-level driver should send the frame based on available
178  *	  configuration.
179  *	  This function should return zero or negative errno.
180  *
181  * ed:    Handler that 802.15.4 module calls for Energy Detection.
182  *	  This function should place the value for detected energy
183  *	  (usually device-dependant) in the level pointer and return
184  *	  either zero or negative errno. Called with pib_lock held.
185  *
186  * set_channel:
187  * 	  Set radio for listening on specific channel.
188  *	  Set the device for listening on specified channel.
189  *	  Returns either zero, or negative errno. Called with pib_lock held.
190  *
191  * set_hw_addr_filt:
192  *	  Set radio for listening on specific address.
193  *	  Set the device for listening on specified address.
194  *	  Returns either zero, or negative errno.
195  *
196  * set_txpower:
197  *	  Set radio transmit power in mBm. Called with pib_lock held.
198  *	  Returns either zero, or negative errno.
199  *
200  * set_lbt
201  *	  Enables or disables listen before talk on the device. Called with
202  *	  pib_lock held.
203  *	  Returns either zero, or negative errno.
204  *
205  * set_cca_mode
206  *	  Sets the CCA mode used by the device. Called with pib_lock held.
207  *	  Returns either zero, or negative errno.
208  *
209  * set_cca_ed_level
210  *	  Sets the CCA energy detection threshold in mBm. Called with pib_lock
211  *	  held.
212  *	  Returns either zero, or negative errno.
213  *
214  * set_csma_params
215  *	  Sets the CSMA parameter set for the PHY. Called with pib_lock held.
216  *	  Returns either zero, or negative errno.
217  *
218  * set_frame_retries
219  *	  Sets the retransmission attempt limit. Called with pib_lock held.
220  *	  Returns either zero, or negative errno.
221  *
222  * set_promiscuous_mode
223  *	  Enables or disable promiscuous mode.
224  */
225 struct ieee802154_ops {
226 	struct module	*owner;
227 	int		(*start)(struct ieee802154_hw *hw);
228 	void		(*stop)(struct ieee802154_hw *hw);
229 	int		(*xmit_sync)(struct ieee802154_hw *hw,
230 				     struct sk_buff *skb);
231 	int		(*xmit_async)(struct ieee802154_hw *hw,
232 				      struct sk_buff *skb);
233 	int		(*ed)(struct ieee802154_hw *hw, u8 *level);
234 	int		(*set_channel)(struct ieee802154_hw *hw, u8 page,
235 				       u8 channel);
236 	int		(*set_hw_addr_filt)(struct ieee802154_hw *hw,
237 					    struct ieee802154_hw_addr_filt *filt,
238 					    unsigned long changed);
239 	int		(*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
240 	int		(*set_lbt)(struct ieee802154_hw *hw, bool on);
241 	int		(*set_cca_mode)(struct ieee802154_hw *hw,
242 					const struct wpan_phy_cca *cca);
243 	int		(*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
244 	int		(*set_csma_params)(struct ieee802154_hw *hw,
245 					   u8 min_be, u8 max_be, u8 retries);
246 	int		(*set_frame_retries)(struct ieee802154_hw *hw,
247 					     s8 retries);
248 	int             (*set_promiscuous_mode)(struct ieee802154_hw *hw,
249 						const bool on);
250 };
251 
252 /**
253  * ieee802154_get_fc_from_skb - get the frame control field from an skb
254  * @skb: skb where the frame control field will be get from
255  */
256 static inline __le16 ieee802154_get_fc_from_skb(const struct sk_buff *skb)
257 {
258 	/* return some invalid fc on failure */
259 	if (unlikely(skb->mac_len < 2)) {
260 		WARN_ON(1);
261 		return cpu_to_le16(0);
262 	}
263 
264 	return (__force __le16)__get_unaligned_memmove16(skb_mac_header(skb));
265 }
266 
267 /**
268  * ieee802154_be64_to_le64 - copies and convert be64 to le64
269  * @le64_dst: le64 destination pointer
270  * @be64_src: be64 source pointer
271  */
272 static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
273 {
274 	__put_unaligned_memmove64(swab64p(be64_src), le64_dst);
275 }
276 
277 /**
278  * ieee802154_le64_to_be64 - copies and convert le64 to be64
279  * @be64_dst: be64 destination pointer
280  * @le64_src: le64 source pointer
281  */
282 static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
283 {
284 	__put_unaligned_memmove64(swab64p(le64_src), be64_dst);
285 }
286 
287 /**
288  * ieee802154_alloc_hw - Allocate a new hardware device
289  *
290  * This must be called once for each hardware device. The returned pointer
291  * must be used to refer to this device when calling other functions.
292  * mac802154 allocates a private data area for the driver pointed to by
293  * @priv in &struct ieee802154_hw, the size of this area is given as
294  * @priv_data_len.
295  *
296  * @priv_data_len: length of private data
297  * @ops: callbacks for this device
298  *
299  * Return: A pointer to the new hardware device, or %NULL on error.
300  */
301 struct ieee802154_hw *
302 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
303 
304 /**
305  * ieee802154_free_hw - free hardware descriptor
306  *
307  * This function frees everything that was allocated, including the
308  * private data for the driver. You must call ieee802154_unregister_hw()
309  * before calling this function.
310  *
311  * @hw: the hardware to free
312  */
313 void ieee802154_free_hw(struct ieee802154_hw *hw);
314 
315 /**
316  * ieee802154_register_hw - Register hardware device
317  *
318  * You must call this function before any other functions in
319  * mac802154. Note that before a hardware can be registered, you
320  * need to fill the contained wpan_phy's information.
321  *
322  * @hw: the device to register as returned by ieee802154_alloc_hw()
323  *
324  * Return: 0 on success. An error code otherwise.
325  */
326 int ieee802154_register_hw(struct ieee802154_hw *hw);
327 
328 /**
329  * ieee802154_unregister_hw - Unregister a hardware device
330  *
331  * This function instructs mac802154 to free allocated resources
332  * and unregister netdevices from the networking subsystem.
333  *
334  * @hw: the hardware to unregister
335  */
336 void ieee802154_unregister_hw(struct ieee802154_hw *hw);
337 
338 /**
339  * ieee802154_rx_irqsafe - receive frame
340  *
341  * Like ieee802154_rx() but can be called in IRQ context
342  * (internally defers to a tasklet.)
343  *
344  * @hw: the hardware this frame came in on
345  * @skb: the buffer to receive, owned by mac802154 after this call
346  * @lqi: link quality indicator
347  */
348 void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
349 			   u8 lqi);
350 /**
351  * ieee802154_wake_queue - wake ieee802154 queue
352  * @hw: pointer as obtained from ieee802154_alloc_hw().
353  *
354  * Drivers should use this function instead of netif_wake_queue.
355  */
356 void ieee802154_wake_queue(struct ieee802154_hw *hw);
357 
358 /**
359  * ieee802154_stop_queue - stop ieee802154 queue
360  * @hw: pointer as obtained from ieee802154_alloc_hw().
361  *
362  * Drivers should use this function instead of netif_stop_queue.
363  */
364 void ieee802154_stop_queue(struct ieee802154_hw *hw);
365 
366 /**
367  * ieee802154_xmit_complete - frame transmission complete
368  *
369  * @hw: pointer as obtained from ieee802154_alloc_hw().
370  * @skb: buffer for transmission
371  * @ifs_handling: indicate interframe space handling
372  */
373 void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
374 			      bool ifs_handling);
375 
376 #endif /* NET_MAC802154_H */
377