Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * Authors: |
| 5 | * Alexander Aring <aar@pengutronix.de> |
| 6 | * |
| 7 | * Based on: net/mac80211/util.c |
| 8 | */ |
| 9 | |
| 10 | #include "ieee802154_i.h" |
Alexander Aring | c4227c8 | 2015-06-24 11:36:34 +0200 | [diff] [blame] | 11 | #include "driver-ops.h" |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 12 | |
Alexander Aring | 6322d50 | 2014-11-12 03:36:51 +0100 | [diff] [blame] | 13 | /* privid for wpan_phys to determine whether they belong to us or not */ |
| 14 | const void *const mac802154_wpan_phy_privid = &mac802154_wpan_phy_privid; |
| 15 | |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 16 | /** |
| 17 | * ieee802154_wake_queue - wake ieee802154 queue |
Stefan Schmidt | 982e2b7 | 2022-10-26 09:40:34 +0200 | [diff] [blame] | 18 | * @hw: main hardware object |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 19 | * |
| 20 | * Tranceivers usually have either one transmit framebuffer or one framebuffer |
| 21 | * for both transmitting and receiving. Hence, the core currently only handles |
| 22 | * one frame at a time for each phy, which means we had to stop the queue to |
| 23 | * avoid new skb to come during the transmission. The queue then needs to be |
| 24 | * woken up after the operation. |
| 25 | */ |
| 26 | static void ieee802154_wake_queue(struct ieee802154_hw *hw) |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 27 | { |
| 28 | struct ieee802154_local *local = hw_to_local(hw); |
| 29 | struct ieee802154_sub_if_data *sdata; |
| 30 | |
| 31 | rcu_read_lock(); |
Miquel Raynal | 2b13db1 | 2022-05-19 17:05:15 +0200 | [diff] [blame] | 32 | clear_bit(WPAN_PHY_FLAG_STATE_QUEUE_STOPPED, &local->phy->flags); |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 33 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 34 | if (!sdata->dev) |
| 35 | continue; |
| 36 | |
| 37 | netif_wake_queue(sdata->dev); |
| 38 | } |
| 39 | rcu_read_unlock(); |
| 40 | } |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 41 | |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 42 | /** |
| 43 | * ieee802154_stop_queue - stop ieee802154 queue |
Stefan Schmidt | 982e2b7 | 2022-10-26 09:40:34 +0200 | [diff] [blame] | 44 | * @hw: main hardware object |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 45 | * |
| 46 | * Tranceivers usually have either one transmit framebuffer or one framebuffer |
| 47 | * for both transmitting and receiving. Hence, the core currently only handles |
| 48 | * one frame at a time for each phy, which means we need to tell upper layers to |
| 49 | * stop giving us new skbs while we are busy with the transmitted one. The queue |
| 50 | * must then be stopped before transmitting. |
| 51 | */ |
| 52 | static void ieee802154_stop_queue(struct ieee802154_hw *hw) |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 53 | { |
| 54 | struct ieee802154_local *local = hw_to_local(hw); |
| 55 | struct ieee802154_sub_if_data *sdata; |
| 56 | |
| 57 | rcu_read_lock(); |
| 58 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 59 | if (!sdata->dev) |
| 60 | continue; |
| 61 | |
| 62 | netif_stop_queue(sdata->dev); |
| 63 | } |
| 64 | rcu_read_unlock(); |
| 65 | } |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 66 | |
| 67 | void ieee802154_hold_queue(struct ieee802154_local *local) |
| 68 | { |
| 69 | unsigned long flags; |
| 70 | |
| 71 | spin_lock_irqsave(&local->phy->queue_lock, flags); |
| 72 | if (!atomic_fetch_inc(&local->phy->hold_txs)) |
| 73 | ieee802154_stop_queue(&local->hw); |
| 74 | spin_unlock_irqrestore(&local->phy->queue_lock, flags); |
| 75 | } |
| 76 | |
| 77 | void ieee802154_release_queue(struct ieee802154_local *local) |
| 78 | { |
| 79 | unsigned long flags; |
| 80 | |
| 81 | spin_lock_irqsave(&local->phy->queue_lock, flags); |
Alexander Aring | 2ec2f6b | 2022-06-13 00:37:34 -0400 | [diff] [blame] | 82 | if (atomic_dec_and_test(&local->phy->hold_txs)) |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 83 | ieee802154_wake_queue(&local->hw); |
| 84 | spin_unlock_irqrestore(&local->phy->queue_lock, flags); |
| 85 | } |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 86 | |
Miquel Raynal | a40612f | 2022-05-19 17:05:12 +0200 | [diff] [blame] | 87 | void ieee802154_disable_queue(struct ieee802154_local *local) |
| 88 | { |
| 89 | struct ieee802154_sub_if_data *sdata; |
| 90 | |
| 91 | rcu_read_lock(); |
| 92 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 93 | if (!sdata->dev) |
| 94 | continue; |
| 95 | |
| 96 | netif_tx_disable(sdata->dev); |
| 97 | } |
| 98 | rcu_read_unlock(); |
| 99 | } |
| 100 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 101 | enum hrtimer_restart ieee802154_xmit_ifs_timer(struct hrtimer *timer) |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 102 | { |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 103 | struct ieee802154_local *local = |
| 104 | container_of(timer, struct ieee802154_local, ifs_timer); |
| 105 | |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 106 | ieee802154_release_queue(local); |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 107 | |
| 108 | return HRTIMER_NORESTART; |
| 109 | } |
| 110 | |
| 111 | void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb, |
| 112 | bool ifs_handling) |
| 113 | { |
Miquel Raynal | 337e2f8 | 2022-04-07 12:08:56 +0200 | [diff] [blame] | 114 | struct ieee802154_local *local = hw_to_local(hw); |
| 115 | |
| 116 | local->tx_result = IEEE802154_SUCCESS; |
| 117 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 118 | if (ifs_handling) { |
Alexander Aring | 3f3c4bb | 2015-03-04 21:19:59 +0100 | [diff] [blame] | 119 | u8 max_sifs_size; |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 120 | |
Alexander Aring | 3f3c4bb | 2015-03-04 21:19:59 +0100 | [diff] [blame] | 121 | /* If transceiver sets CRC on his own we need to use lifs |
| 122 | * threshold len above 16 otherwise 18, because it's not |
| 123 | * part of skb->len. |
| 124 | */ |
| 125 | if (hw->flags & IEEE802154_HW_TX_OMIT_CKSUM) |
| 126 | max_sifs_size = IEEE802154_MAX_SIFS_FRAME_SIZE - |
| 127 | IEEE802154_FCS_LEN; |
| 128 | else |
| 129 | max_sifs_size = IEEE802154_MAX_SIFS_FRAME_SIZE; |
| 130 | |
| 131 | if (skb->len > max_sifs_size) |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 132 | hrtimer_start(&local->ifs_timer, |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 133 | hw->phy->lifs_period * NSEC_PER_USEC, |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 134 | HRTIMER_MODE_REL); |
| 135 | else |
| 136 | hrtimer_start(&local->ifs_timer, |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 137 | hw->phy->sifs_period * NSEC_PER_USEC, |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 138 | HRTIMER_MODE_REL); |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 139 | } else { |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 140 | ieee802154_release_queue(local); |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 141 | } |
Alexander Aring | 3862eba | 2015-05-17 21:44:56 +0200 | [diff] [blame] | 142 | |
| 143 | dev_consume_skb_any(skb); |
Alexander Aring | 6c1c78d | 2022-06-13 00:37:35 -0400 | [diff] [blame] | 144 | if (atomic_dec_and_test(&hw->phy->ongoing_txs)) |
Miquel Raynal | f0feb34 | 2022-05-19 17:05:13 +0200 | [diff] [blame] | 145 | wake_up(&hw->phy->sync_txq); |
Alexander Aring | c208510 | 2014-10-26 09:37:05 +0100 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL(ieee802154_xmit_complete); |
Alexander Aring | c4227c8 | 2015-06-24 11:36:34 +0200 | [diff] [blame] | 148 | |
Miquel Raynal | 30ca44e | 2022-04-07 12:08:57 +0200 | [diff] [blame] | 149 | void ieee802154_xmit_error(struct ieee802154_hw *hw, struct sk_buff *skb, |
| 150 | int reason) |
| 151 | { |
| 152 | struct ieee802154_local *local = hw_to_local(hw); |
| 153 | |
| 154 | local->tx_result = reason; |
Miquel Raynal | 20a19d1 | 2022-05-19 17:05:10 +0200 | [diff] [blame] | 155 | ieee802154_release_queue(local); |
Miquel Raynal | 30ca44e | 2022-04-07 12:08:57 +0200 | [diff] [blame] | 156 | dev_kfree_skb_any(skb); |
Alexander Aring | 6c1c78d | 2022-06-13 00:37:35 -0400 | [diff] [blame] | 157 | if (atomic_dec_and_test(&hw->phy->ongoing_txs)) |
Miquel Raynal | f0feb34 | 2022-05-19 17:05:13 +0200 | [diff] [blame] | 158 | wake_up(&hw->phy->sync_txq); |
Miquel Raynal | 30ca44e | 2022-04-07 12:08:57 +0200 | [diff] [blame] | 159 | } |
| 160 | EXPORT_SYMBOL(ieee802154_xmit_error); |
| 161 | |
Miquel Raynal | 5a1b57c | 2022-04-07 12:08:58 +0200 | [diff] [blame] | 162 | void ieee802154_xmit_hw_error(struct ieee802154_hw *hw, struct sk_buff *skb) |
| 163 | { |
| 164 | ieee802154_xmit_error(hw, skb, IEEE802154_SYSTEM_ERROR); |
| 165 | } |
| 166 | EXPORT_SYMBOL(ieee802154_xmit_hw_error); |
| 167 | |
Alexander Aring | c4227c8 | 2015-06-24 11:36:34 +0200 | [diff] [blame] | 168 | void ieee802154_stop_device(struct ieee802154_local *local) |
| 169 | { |
| 170 | flush_workqueue(local->workqueue); |
| 171 | hrtimer_cancel(&local->ifs_timer); |
| 172 | drv_stop(local); |
| 173 | } |