xref: /openbmc/linux/include/net/mac802154.h (revision 6189f1b0)
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_be64_to_le64 - copies and convert be64 to le64
254  * @le64_dst: le64 destination pointer
255  * @be64_src: be64 source pointer
256  */
257 static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
258 {
259 	__put_unaligned_memmove64(swab64p(be64_src), le64_dst);
260 }
261 
262 /**
263  * ieee802154_le64_to_be64 - copies and convert le64 to be64
264  * @be64_dst: be64 destination pointer
265  * @le64_src: le64 source pointer
266  */
267 static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
268 {
269 	__put_unaligned_memmove64(swab64p(le64_src), be64_dst);
270 }
271 
272 /**
273  * ieee802154_alloc_hw - Allocate a new hardware device
274  *
275  * This must be called once for each hardware device. The returned pointer
276  * must be used to refer to this device when calling other functions.
277  * mac802154 allocates a private data area for the driver pointed to by
278  * @priv in &struct ieee802154_hw, the size of this area is given as
279  * @priv_data_len.
280  *
281  * @priv_data_len: length of private data
282  * @ops: callbacks for this device
283  *
284  * Return: A pointer to the new hardware device, or %NULL on error.
285  */
286 struct ieee802154_hw *
287 ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
288 
289 /**
290  * ieee802154_free_hw - free hardware descriptor
291  *
292  * This function frees everything that was allocated, including the
293  * private data for the driver. You must call ieee802154_unregister_hw()
294  * before calling this function.
295  *
296  * @hw: the hardware to free
297  */
298 void ieee802154_free_hw(struct ieee802154_hw *hw);
299 
300 /**
301  * ieee802154_register_hw - Register hardware device
302  *
303  * You must call this function before any other functions in
304  * mac802154. Note that before a hardware can be registered, you
305  * need to fill the contained wpan_phy's information.
306  *
307  * @hw: the device to register as returned by ieee802154_alloc_hw()
308  *
309  * Return: 0 on success. An error code otherwise.
310  */
311 int ieee802154_register_hw(struct ieee802154_hw *hw);
312 
313 /**
314  * ieee802154_unregister_hw - Unregister a hardware device
315  *
316  * This function instructs mac802154 to free allocated resources
317  * and unregister netdevices from the networking subsystem.
318  *
319  * @hw: the hardware to unregister
320  */
321 void ieee802154_unregister_hw(struct ieee802154_hw *hw);
322 
323 /**
324  * ieee802154_rx - receive frame
325  *
326  * Use this function to hand received frames to mac802154. The receive
327  * buffer in @skb must start with an IEEE 802.15.4 header. In case of a
328  * paged @skb is used, the driver is recommended to put the ieee802154
329  * header of the frame on the linear part of the @skb to avoid memory
330  * allocation and/or memcpy by the stack.
331  *
332  * This function may not be called in IRQ context. Calls to this function
333  * for a single hardware must be synchronized against each other.
334  *
335  * @hw: the hardware this frame came in on
336  * @skb: the buffer to receive, owned by mac802154 after this call
337  */
338 void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb);
339 
340 /**
341  * ieee802154_rx_irqsafe - receive frame
342  *
343  * Like ieee802154_rx() but can be called in IRQ context
344  * (internally defers to a tasklet.)
345  *
346  * @hw: the hardware this frame came in on
347  * @skb: the buffer to receive, owned by mac802154 after this call
348  * @lqi: link quality indicator
349  */
350 void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
351 			   u8 lqi);
352 /**
353  * ieee802154_wake_queue - wake ieee802154 queue
354  * @hw: pointer as obtained from ieee802154_alloc_hw().
355  *
356  * Drivers should use this function instead of netif_wake_queue.
357  */
358 void ieee802154_wake_queue(struct ieee802154_hw *hw);
359 
360 /**
361  * ieee802154_stop_queue - stop ieee802154 queue
362  * @hw: pointer as obtained from ieee802154_alloc_hw().
363  *
364  * Drivers should use this function instead of netif_stop_queue.
365  */
366 void ieee802154_stop_queue(struct ieee802154_hw *hw);
367 
368 /**
369  * ieee802154_xmit_complete - frame transmission complete
370  *
371  * @hw: pointer as obtained from ieee802154_alloc_hw().
372  * @skb: buffer for transmission
373  * @ifs_handling: indicate interframe space handling
374  */
375 void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
376 			      bool ifs_handling);
377 
378 #endif /* NET_MAC802154_H */
379