ieee802154: add ieee802154_skb_src_pan helper
[linux-block.git] / include / net / mac802154.h
CommitLineData
0afd7ad9 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 *
0afd7ad9 15 */
16#ifndef NET_MAC802154_H
17#define NET_MAC802154_H
18
f1608920 19#include <asm/unaligned.h>
0afd7ad9 20#include <net/af_ieee802154.h>
239a84a9 21#include <linux/ieee802154.h>
94b4f6c2 22#include <linux/skbuff.h>
0afd7ad9 23
7fe9a388
AA
24#include <net/cfg802154.h>
25
6b70a43c
AA
26/**
27 * enum ieee802154_hw_addr_filt_flags - hardware address filtering flags
28 *
29 * The following flags are used to indicate changed address settings from
0afd7ad9 30 * the stack to the hardware.
6b70a43c
AA
31 *
32 * @IEEE802154_AFILT_SADDR_CHANGED: Indicates that the short address will be
33 * change.
34 *
35 * @IEEE802154_AFILT_IEEEADDR_CHANGED: Indicates that the extended address
36 * will be change.
37 *
38 * @IEEE802154_AFILT_PANID_CHANGED: Indicates that the pan id will be change.
39 *
40 * @IEEE802154_AFILT_PANC_CHANGED: Indicates that the address filter will
41 * do frame address filtering as a pan coordinator.
0afd7ad9 42 */
6b70a43c 43enum ieee802154_hw_addr_filt_flags {
70f36507
AA
44 IEEE802154_AFILT_SADDR_CHANGED = BIT(0),
45 IEEE802154_AFILT_IEEEADDR_CHANGED = BIT(1),
46 IEEE802154_AFILT_PANID_CHANGED = BIT(2),
47 IEEE802154_AFILT_PANC_CHANGED = BIT(3),
6b70a43c 48};
0afd7ad9 49
a0825b03
AA
50/**
51 * struct ieee802154_hw_addr_filt - hardware address filtering settings
52 *
53 * @pan_id: pan_id which should be set to the hardware address filter.
54 *
55 * @short_addr: short_addr which should be set to the hardware address filter.
56 *
57 * @ieee_addr: extended address which should be set to the hardware address
58 * filter.
59 *
60 * @pan_coord: boolean if hardware filtering should be operate as coordinator.
61 */
0afd7ad9 62struct ieee802154_hw_addr_filt {
a0825b03 63 __le16 pan_id;
0afd7ad9 64 __le16 short_addr;
b70ab2e8 65 __le64 ieee_addr;
623c1234 66 bool pan_coord;
0afd7ad9 67};
68
a0825b03
AA
69/**
70 * struct ieee802154_hw - ieee802154 hardware
71 *
72 * @extra_tx_headroom: headroom to reserve in each transmit skb for use by the
73 * driver (e.g. for transmit headers.)
74 *
75 * @flags: hardware flags, see &enum ieee802154_hw_flags
76 *
77 * @parent: parent device of the hardware.
78 *
79 * @priv: pointer to private area that was allocated for driver use along with
80 * this structure.
81 *
82 * @phy: This points to the &struct wpan_phy allocated for this 802.15.4 PHY.
83 */
5a504397 84struct ieee802154_hw {
0afd7ad9 85 /* filled by the driver */
86 int extra_tx_headroom;
87 u32 flags;
88 struct device *parent;
af69a345 89 void *priv;
0afd7ad9 90
91 /* filled by mac802154 core */
0afd7ad9 92 struct wpan_phy *phy;
93};
94
bcbfd207
AA
95/**
96 * enum ieee802154_hw_flags - hardware flags
0afd7ad9 97 *
bcbfd207 98 * These flags are used to indicate hardware capabilities to
0afd7ad9 99 * the stack. Generally, flags here should have their meaning
100 * done in a way that the simplest hardware doesn't need setting
101 * any particular flags. There are some exceptions to this rule,
102 * however, so you are advised to review these flags carefully.
bcbfd207
AA
103 *
104 * @IEEE802154_HW_TX_OMIT_CKSUM: Indicates that xmitter will add FCS on it's
105 * own.
106 *
107 * @IEEE802154_HW_LBT: Indicates that transceiver will support listen before
108 * transmit.
109 *
110 * @IEEE802154_HW_CSMA_PARAMS: Indicates that transceiver will support csma
111 * parameters (max_be, min_be, backoff exponents).
112 *
113 * @IEEE802154_HW_FRAME_RETRIES: Indicates that transceiver will support ARET
114 * frame retries setting.
115 *
116 * @IEEE802154_HW_AFILT: Indicates that transceiver will support hardware
117 * address filter setting.
118 *
119 * @IEEE802154_HW_PROMISCUOUS: Indicates that transceiver will support
120 * promiscuous mode setting.
121 *
122 * @IEEE802154_HW_RX_OMIT_CKSUM: Indicates that receiver omits FCS.
123 *
124 * @IEEE802154_HW_RX_DROP_BAD_CKSUM: Indicates that receiver will not filter
125 * frames with bad checksum.
0afd7ad9 126 */
bcbfd207 127enum ieee802154_hw_flags {
70f36507
AA
128 IEEE802154_HW_TX_OMIT_CKSUM = BIT(0),
129 IEEE802154_HW_LBT = BIT(1),
130 IEEE802154_HW_CSMA_PARAMS = BIT(2),
131 IEEE802154_HW_FRAME_RETRIES = BIT(3),
132 IEEE802154_HW_AFILT = BIT(4),
133 IEEE802154_HW_PROMISCUOUS = BIT(5),
134 IEEE802154_HW_RX_OMIT_CKSUM = BIT(6),
135 IEEE802154_HW_RX_DROP_BAD_CKSUM = BIT(7),
bcbfd207 136};
90386a7e
AA
137
138/* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
139#define IEEE802154_HW_OMIT_CKSUM (IEEE802154_HW_TX_OMIT_CKSUM | \
140 IEEE802154_HW_RX_OMIT_CKSUM)
640985ec 141
0afd7ad9 142/* struct ieee802154_ops - callbacks from mac802154 to the driver
143 *
144 * This structure contains various callbacks that the driver may
145 * handle or, in some cases, must handle, for example to transmit
146 * a frame.
147 *
148 * start: Handler that 802.15.4 module calls for device initialization.
149 * This function is called before the first interface is attached.
150 *
151 * stop: Handler that 802.15.4 module calls for device cleanup.
152 * This function is called after the last interface is removed.
153 *
ed0a5dce
AA
154 * xmit_sync:
155 * Handler that 802.15.4 module calls for each transmitted frame.
156 * skb cntains the buffer starting from the IEEE 802.15.4 header.
157 * The low-level driver should send the frame based on available
158 * configuration. This is called by a workqueue and useful for
159 * synchronous 802.15.4 drivers.
160 * This function should return zero or negative errno.
161 *
1e7283a2
AA
162 * WARNING:
163 * This will be deprecated soon. We don't accept synced xmit callbacks
164 * drivers anymore.
165 *
ed0a5dce
AA
166 * xmit_async:
167 * Handler that 802.15.4 module calls for each transmitted frame.
0afd7ad9 168 * skb cntains the buffer starting from the IEEE 802.15.4 header.
169 * The low-level driver should send the frame based on available
170 * configuration.
dc67c6b3 171 * This function should return zero or negative errno.
0afd7ad9 172 *
173 * ed: Handler that 802.15.4 module calls for Energy Detection.
174 * This function should place the value for detected energy
175 * (usually device-dependant) in the level pointer and return
176 * either zero or negative errno. Called with pib_lock held.
177 *
178 * set_channel:
179 * Set radio for listening on specific channel.
180 * Set the device for listening on specified channel.
181 * Returns either zero, or negative errno. Called with pib_lock held.
182 *
183 * set_hw_addr_filt:
184 * Set radio for listening on specific address.
185 * Set the device for listening on specified address.
186 * Returns either zero, or negative errno.
9b2777d6
PB
187 *
188 * set_txpower:
e2eb173a 189 * Set radio transmit power in mBm. Called with pib_lock held.
9b2777d6 190 * Returns either zero, or negative errno.
84dda3c6
PB
191 *
192 * set_lbt
193 * Enables or disables listen before talk on the device. Called with
194 * pib_lock held.
195 * Returns either zero, or negative errno.
ba08fea5
PB
196 *
197 * set_cca_mode
198 * Sets the CCA mode used by the device. Called with pib_lock held.
199 * Returns either zero, or negative errno.
6ca00197
PB
200 *
201 * set_cca_ed_level
32b23550 202 * Sets the CCA energy detection threshold in mBm. Called with pib_lock
6ca00197
PB
203 * held.
204 * Returns either zero, or negative errno.
4244db1b
PB
205 *
206 * set_csma_params
207 * Sets the CSMA parameter set for the PHY. Called with pib_lock held.
208 * Returns either zero, or negative errno.
209 *
210 * set_frame_retries
211 * Sets the retransmission attempt limit. Called with pib_lock held.
212 * Returns either zero, or negative errno.
94b79222
AA
213 *
214 * set_promiscuous_mode
215 * Enables or disable promiscuous mode.
0afd7ad9 216 */
217struct ieee802154_ops {
218 struct module *owner;
5a504397
AA
219 int (*start)(struct ieee802154_hw *hw);
220 void (*stop)(struct ieee802154_hw *hw);
ed0a5dce
AA
221 int (*xmit_sync)(struct ieee802154_hw *hw,
222 struct sk_buff *skb);
223 int (*xmit_async)(struct ieee802154_hw *hw,
224 struct sk_buff *skb);
5a504397 225 int (*ed)(struct ieee802154_hw *hw, u8 *level);
e37d2ec8
AA
226 int (*set_channel)(struct ieee802154_hw *hw, u8 page,
227 u8 channel);
5a504397
AA
228 int (*set_hw_addr_filt)(struct ieee802154_hw *hw,
229 struct ieee802154_hw_addr_filt *filt,
0afd7ad9 230 unsigned long changed);
e2eb173a 231 int (*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
5a504397 232 int (*set_lbt)(struct ieee802154_hw *hw, bool on);
7fe9a388
AA
233 int (*set_cca_mode)(struct ieee802154_hw *hw,
234 const struct wpan_phy_cca *cca);
32b23550 235 int (*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
5a504397 236 int (*set_csma_params)(struct ieee802154_hw *hw,
4244db1b 237 u8 min_be, u8 max_be, u8 retries);
5a504397 238 int (*set_frame_retries)(struct ieee802154_hw *hw,
4244db1b 239 s8 retries);
94b79222
AA
240 int (*set_promiscuous_mode)(struct ieee802154_hw *hw,
241 const bool on);
0afd7ad9 242};
243
54552d03
AA
244/**
245 * ieee802154_get_fc_from_skb - get the frame control field from an skb
246 * @skb: skb where the frame control field will be get from
247 */
248static inline __le16 ieee802154_get_fc_from_skb(const struct sk_buff *skb)
249{
07b0188a
AA
250 /* check if we can fc at skb_mac_header of sk buffer */
251 if (unlikely(!skb_mac_header_was_set(skb) ||
252 (skb_tail_pointer(skb) - skb_mac_header(skb)) < 2)) {
54552d03
AA
253 WARN_ON(1);
254 return cpu_to_le16(0);
255 }
256
f1608920 257 return get_unaligned_le16(skb_mac_header(skb));
54552d03
AA
258}
259
9cc577dd
AA
260/**
261 * ieee802154_skb_dst_pan - get the pointer to destination pan field
262 * @fc: mac header frame control field
263 * @skb: skb where the destination pan pointer will be get from
264 */
265static inline unsigned char *ieee802154_skb_dst_pan(__le16 fc,
266 const struct sk_buff *skb)
267{
268 unsigned char *dst_pan;
269
270 switch (ieee802154_daddr_mode(fc)) {
271 case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
272 dst_pan = NULL;
273 break;
274 case cpu_to_le16(IEEE802154_FCTL_DADDR_SHORT):
275 case cpu_to_le16(IEEE802154_FCTL_DADDR_EXTENDED):
276 dst_pan = skb_mac_header(skb) +
277 IEEE802154_FC_LEN +
278 IEEE802154_SEQ_LEN;
279 break;
280 default:
281 WARN_ONCE(1, "invalid addr mode detected");
282 dst_pan = NULL;
283 break;
284 }
285
286 return dst_pan;
287}
288
19580cc1
AA
289/**
290 * ieee802154_skb_src_pan - get the pointer to source pan field
291 * @fc: mac header frame control field
292 * @skb: skb where the source pan pointer will be get from
293 */
294static inline unsigned char *ieee802154_skb_src_pan(__le16 fc,
295 const struct sk_buff *skb)
296{
297 unsigned char *src_pan;
298
299 switch (ieee802154_saddr_mode(fc)) {
300 case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
301 src_pan = NULL;
302 break;
303 case cpu_to_le16(IEEE802154_FCTL_SADDR_SHORT):
304 case cpu_to_le16(IEEE802154_FCTL_SADDR_EXTENDED):
305 /* if intra-pan and source addr mode is non none,
306 * then source pan id is equal destination pan id.
307 */
308 if (ieee802154_is_intra_pan(fc)) {
309 src_pan = ieee802154_skb_dst_pan(fc, skb);
310 break;
311 }
312
313 switch (ieee802154_daddr_mode(fc)) {
314 case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
315 src_pan = skb_mac_header(skb) +
316 IEEE802154_FC_LEN +
317 IEEE802154_SEQ_LEN;
318 break;
319 case cpu_to_le16(IEEE802154_FCTL_DADDR_SHORT):
320 src_pan = skb_mac_header(skb) +
321 IEEE802154_FC_LEN +
322 IEEE802154_SEQ_LEN +
323 IEEE802154_PAN_ID_LEN +
324 IEEE802154_SHORT_ADDR_LEN;
325 break;
326 case cpu_to_le16(IEEE802154_FCTL_DADDR_EXTENDED):
327 src_pan = skb_mac_header(skb) +
328 IEEE802154_FC_LEN +
329 IEEE802154_SEQ_LEN +
330 IEEE802154_PAN_ID_LEN +
331 IEEE802154_EXTENDED_ADDR_LEN;
332 break;
333 default:
334 WARN_ONCE(1, "invalid addr mode detected");
335 src_pan = NULL;
336 break;
337 }
338 break;
339 default:
340 WARN_ONCE(1, "invalid addr mode detected");
341 src_pan = NULL;
342 break;
343 }
344
345 return src_pan;
346}
347
ab24f50f 348/**
705cbbbe
AA
349 * ieee802154_be64_to_le64 - copies and convert be64 to le64
350 * @le64_dst: le64 destination pointer
351 * @be64_src: be64 source pointer
ab24f50f 352 */
705cbbbe 353static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
ab24f50f 354{
f1608920 355 put_unaligned_le64(get_unaligned_be64(be64_src), le64_dst);
ab24f50f
AA
356}
357
239a84a9
AA
358/**
359 * ieee802154_le64_to_be64 - copies and convert le64 to be64
360 * @be64_dst: be64 destination pointer
361 * @le64_src: le64 source pointer
362 */
363static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
364{
f1608920 365 put_unaligned_be64(get_unaligned_le64(le64_src), be64_dst);
239a84a9
AA
366}
367
8911d774
AA
368/**
369 * ieee802154_le16_to_be16 - copies and convert le16 to be16
370 * @be16_dst: be16 destination pointer
371 * @le16_src: le16 source pointer
372 */
373static inline void ieee802154_le16_to_be16(void *be16_dst, const void *le16_src)
374{
f1608920 375 put_unaligned_be16(get_unaligned_le16(le16_src), be16_dst);
8911d774
AA
376}
377
118a5cf8
AA
378/**
379 * ieee802154_be16_to_le16 - copies and convert be16 to le16
380 * @le16_dst: le16 destination pointer
381 * @be16_src: be16 source pointer
382 */
383static inline void ieee802154_be16_to_le16(void *le16_dst, const void *be16_src)
384{
385 put_unaligned_le16(get_unaligned_be16(be16_src), le16_dst);
386}
387
42fb23e2
VB
388/**
389 * ieee802154_alloc_hw - Allocate a new hardware device
390 *
391 * This must be called once for each hardware device. The returned pointer
392 * must be used to refer to this device when calling other functions.
393 * mac802154 allocates a private data area for the driver pointed to by
394 * @priv in &struct ieee802154_hw, the size of this area is given as
395 * @priv_data_len.
396 *
397 * @priv_data_len: length of private data
398 * @ops: callbacks for this device
399 *
400 * Return: A pointer to the new hardware device, or %NULL on error.
401 */
5a504397 402struct ieee802154_hw *
16301861 403ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
42fb23e2
VB
404
405/**
406 * ieee802154_free_hw - free hardware descriptor
407 *
408 * This function frees everything that was allocated, including the
409 * private data for the driver. You must call ieee802154_unregister_hw()
410 * before calling this function.
411 *
412 * @hw: the hardware to free
413 */
5a504397 414void ieee802154_free_hw(struct ieee802154_hw *hw);
42fb23e2
VB
415
416/**
417 * ieee802154_register_hw - Register hardware device
418 *
419 * You must call this function before any other functions in
420 * mac802154. Note that before a hardware can be registered, you
421 * need to fill the contained wpan_phy's information.
422 *
423 * @hw: the device to register as returned by ieee802154_alloc_hw()
424 *
425 * Return: 0 on success. An error code otherwise.
426 */
5a504397 427int ieee802154_register_hw(struct ieee802154_hw *hw);
42fb23e2
VB
428
429/**
430 * ieee802154_unregister_hw - Unregister a hardware device
431 *
432 * This function instructs mac802154 to free allocated resources
433 * and unregister netdevices from the networking subsystem.
434 *
435 * @hw: the hardware to unregister
436 */
5a504397 437void ieee802154_unregister_hw(struct ieee802154_hw *hw);
1010f540 438
42fb23e2
VB
439/**
440 * ieee802154_rx_irqsafe - receive frame
441 *
442 * Like ieee802154_rx() but can be called in IRQ context
443 * (internally defers to a tasklet.)
444 *
445 * @hw: the hardware this frame came in on
446 * @skb: the buffer to receive, owned by mac802154 after this call
447 * @lqi: link quality indicator
448 */
5a504397 449void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
1cd829c8 450 u8 lqi);
42fb23e2
VB
451/**
452 * ieee802154_wake_queue - wake ieee802154 queue
453 * @hw: pointer as obtained from ieee802154_alloc_hw().
454 *
455 * Drivers should use this function instead of netif_wake_queue.
456 */
c2085103 457void ieee802154_wake_queue(struct ieee802154_hw *hw);
42fb23e2
VB
458
459/**
460 * ieee802154_stop_queue - stop ieee802154 queue
461 * @hw: pointer as obtained from ieee802154_alloc_hw().
462 *
463 * Drivers should use this function instead of netif_stop_queue.
464 */
c2085103 465void ieee802154_stop_queue(struct ieee802154_hw *hw);
42fb23e2
VB
466
467/**
468 * ieee802154_xmit_complete - frame transmission complete
469 *
470 * @hw: pointer as obtained from ieee802154_alloc_hw().
471 * @skb: buffer for transmission
472 * @ifs_handling: indicate interframe space handling
473 */
61f2dcba
AA
474void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
475 bool ifs_handling);
c2085103 476
0afd7ad9 477#endif /* NET_MAC802154_H */