1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (C) 2024 Felix Fietkau <nbd@nbd.name> 4 */ 5 #include "mt76.h" 6 7 static void mt76_scan_complete(struct mt76_dev *dev, bool abort) 8 { 9 struct mt76_phy *phy = dev->scan.phy; 10 struct cfg80211_scan_info info = { 11 .aborted = abort, 12 }; 13 14 if (!phy) 15 return; 16 17 clear_bit(MT76_SCANNING, &phy->state); 18 19 if (dev->scan.chan && phy->main_chandef.chan) 20 mt76_set_channel(phy, &phy->main_chandef, false); 21 mt76_put_vif_phy_link(phy, dev->scan.vif, dev->scan.mlink); 22 memset(&dev->scan, 0, sizeof(dev->scan)); 23 ieee80211_scan_completed(phy->hw, &info); 24 } 25 26 void mt76_abort_scan(struct mt76_dev *dev) 27 { 28 cancel_delayed_work_sync(&dev->scan_work); 29 mt76_scan_complete(dev, true); 30 } 31 32 static void 33 mt76_scan_send_probe(struct mt76_dev *dev, struct cfg80211_ssid *ssid) 34 { 35 struct cfg80211_scan_request *req = dev->scan.req; 36 struct ieee80211_vif *vif = dev->scan.vif; 37 struct mt76_vif_link *mvif = dev->scan.mlink; 38 enum nl80211_band band = dev->scan.chan->band; 39 struct mt76_phy *phy = dev->scan.phy; 40 struct ieee80211_tx_info *info; 41 struct sk_buff *skb; 42 43 skb = ieee80211_probereq_get(phy->hw, vif->addr, ssid->ssid, 44 ssid->ssid_len, req->ie_len); 45 if (!skb) 46 return; 47 48 if (is_unicast_ether_addr(req->bssid)) { 49 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 50 51 ether_addr_copy(hdr->addr1, req->bssid); 52 ether_addr_copy(hdr->addr3, req->bssid); 53 } 54 55 if (req->ie_len) 56 skb_put_data(skb, req->ie, req->ie_len); 57 58 skb->priority = 7; 59 skb_set_queue_mapping(skb, IEEE80211_AC_VO); 60 61 rcu_read_lock(); 62 63 if (!ieee80211_tx_prepare_skb(phy->hw, vif, skb, band, NULL)) { 64 ieee80211_free_txskb(phy->hw, skb); 65 goto out; 66 } 67 68 info = IEEE80211_SKB_CB(skb); 69 if (req->no_cck) 70 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 71 info->control.flags |= IEEE80211_TX_CTRL_DONT_USE_RATE_MASK; 72 73 mt76_tx(phy, NULL, mvif->wcid, skb); 74 75 out: 76 rcu_read_unlock(); 77 } 78 79 void mt76_scan_work(struct work_struct *work) 80 { 81 struct mt76_dev *dev = container_of(work, struct mt76_dev, 82 scan_work.work); 83 struct cfg80211_scan_request *req = dev->scan.req; 84 struct cfg80211_chan_def chandef = {}; 85 struct mt76_phy *phy = dev->scan.phy; 86 int duration = HZ / 9; /* ~110 ms */ 87 int i; 88 89 if (dev->scan.chan_idx >= req->n_channels) { 90 mt76_scan_complete(dev, false); 91 return; 92 } 93 94 if (dev->scan.chan && phy->num_sta) { 95 dev->scan.chan = NULL; 96 mt76_set_channel(phy, &phy->main_chandef, false); 97 goto out; 98 } 99 100 dev->scan.chan = req->channels[dev->scan.chan_idx++]; 101 cfg80211_chandef_create(&chandef, dev->scan.chan, NL80211_CHAN_HT20); 102 mt76_set_channel(phy, &chandef, true); 103 104 if (!req->n_ssids || 105 chandef.chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) 106 goto out; 107 108 duration = HZ / 16; /* ~60 ms */ 109 local_bh_disable(); 110 for (i = 0; i < req->n_ssids; i++) 111 mt76_scan_send_probe(dev, &req->ssids[i]); 112 local_bh_enable(); 113 114 out: 115 if (!duration) 116 return; 117 118 if (dev->scan.chan) 119 duration = max_t(int, duration, 120 msecs_to_jiffies(req->duration + 121 (req->duration >> 5))); 122 123 ieee80211_queue_delayed_work(dev->phy.hw, &dev->scan_work, duration); 124 } 125 126 int mt76_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 127 struct ieee80211_scan_request *req) 128 { 129 struct mt76_phy *phy = hw->priv; 130 struct mt76_dev *dev = phy->dev; 131 struct mt76_vif_link *mlink; 132 int ret = 0; 133 134 if (hw->wiphy->n_radio > 1) { 135 phy = dev->band_phys[req->req.channels[0]->band]; 136 if (!phy) 137 return -EINVAL; 138 } 139 140 mutex_lock(&dev->mutex); 141 142 if (dev->scan.req || phy->roc_vif) { 143 ret = -EBUSY; 144 goto out; 145 } 146 147 mlink = mt76_get_vif_phy_link(phy, vif); 148 if (IS_ERR(mlink)) { 149 ret = PTR_ERR(mlink); 150 goto out; 151 } 152 153 memset(&dev->scan, 0, sizeof(dev->scan)); 154 dev->scan.req = &req->req; 155 dev->scan.vif = vif; 156 dev->scan.phy = phy; 157 dev->scan.mlink = mlink; 158 ieee80211_queue_delayed_work(dev->phy.hw, &dev->scan_work, 0); 159 160 out: 161 mutex_unlock(&dev->mutex); 162 163 return ret; 164 } 165 EXPORT_SYMBOL_GPL(mt76_hw_scan); 166 167 void mt76_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 168 { 169 struct mt76_phy *phy = hw->priv; 170 171 mt76_abort_scan(phy->dev); 172 } 173 EXPORT_SYMBOL_GPL(mt76_cancel_hw_scan); 174