mac802154.h 16 KB

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