1 /* 2 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <linux/etherdevice.h> 18 #include "wil6210.h" 19 #include "txrx.h" 20 21 static int wil_open(struct net_device *ndev) 22 { 23 struct wil6210_priv *wil = ndev_to_wil(ndev); 24 int rc; 25 26 wil_dbg_misc(wil, "open\n"); 27 28 if (debug_fw || 29 test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) { 30 wil_err(wil, "while in debug_fw or wmi_only mode\n"); 31 return -EINVAL; 32 } 33 34 rc = wil_pm_runtime_get(wil); 35 if (rc < 0) 36 return rc; 37 38 rc = wil_up(wil); 39 if (rc) 40 wil_pm_runtime_put(wil); 41 42 return rc; 43 } 44 45 static int wil_stop(struct net_device *ndev) 46 { 47 struct wil6210_priv *wil = ndev_to_wil(ndev); 48 int rc; 49 50 wil_dbg_misc(wil, "stop\n"); 51 52 rc = wil_down(wil); 53 if (!rc) 54 wil_pm_runtime_put(wil); 55 56 return rc; 57 } 58 59 static const struct net_device_ops wil_netdev_ops = { 60 .ndo_open = wil_open, 61 .ndo_stop = wil_stop, 62 .ndo_start_xmit = wil_start_xmit, 63 .ndo_set_mac_address = eth_mac_addr, 64 .ndo_validate_addr = eth_validate_addr, 65 }; 66 67 static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget) 68 { 69 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv, 70 napi_rx); 71 int quota = budget; 72 int done; 73 74 wil_rx_handle(wil, "a); 75 done = budget - quota; 76 77 if (done < budget) { 78 napi_complete_done(napi, done); 79 wil6210_unmask_irq_rx(wil); 80 wil_dbg_txrx(wil, "NAPI RX complete\n"); 81 } 82 83 wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done); 84 85 return done; 86 } 87 88 static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget) 89 { 90 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv, 91 napi_tx); 92 int tx_done = 0; 93 uint i; 94 95 /* always process ALL Tx complete, regardless budget - it is fast */ 96 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) { 97 struct vring *vring = &wil->vring_tx[i]; 98 struct vring_tx_data *txdata = &wil->vring_tx_data[i]; 99 100 if (!vring->va || !txdata->enabled) 101 continue; 102 103 tx_done += wil_tx_complete(wil, i); 104 } 105 106 if (tx_done < budget) { 107 napi_complete(napi); 108 wil6210_unmask_irq_tx(wil); 109 wil_dbg_txrx(wil, "NAPI TX complete\n"); 110 } 111 112 wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done); 113 114 return min(tx_done, budget); 115 } 116 117 static void wil_dev_setup(struct net_device *dev) 118 { 119 ether_setup(dev); 120 dev->max_mtu = mtu_max; 121 dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT; 122 } 123 124 void *wil_if_alloc(struct device *dev) 125 { 126 struct net_device *ndev; 127 struct wireless_dev *wdev; 128 struct wil6210_priv *wil; 129 struct ieee80211_channel *ch; 130 int rc = 0; 131 132 wdev = wil_cfg80211_init(dev); 133 if (IS_ERR(wdev)) { 134 dev_err(dev, "wil_cfg80211_init failed\n"); 135 return wdev; 136 } 137 138 wil = wdev_to_wil(wdev); 139 wil->wdev = wdev; 140 wil->radio_wdev = wdev; 141 142 wil_dbg_misc(wil, "if_alloc\n"); 143 144 rc = wil_priv_init(wil); 145 if (rc) { 146 dev_err(dev, "wil_priv_init failed\n"); 147 goto out_wdev; 148 } 149 150 wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */ 151 /* default monitor channel */ 152 ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels; 153 cfg80211_chandef_create(&wil->monitor_chandef, ch, NL80211_CHAN_NO_HT); 154 155 ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup); 156 if (!ndev) { 157 dev_err(dev, "alloc_netdev_mqs failed\n"); 158 rc = -ENOMEM; 159 goto out_priv; 160 } 161 162 ndev->netdev_ops = &wil_netdev_ops; 163 wil_set_ethtoolops(ndev); 164 ndev->ieee80211_ptr = wdev; 165 ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM | 166 NETIF_F_SG | NETIF_F_GRO | 167 NETIF_F_TSO | NETIF_F_TSO6 | 168 NETIF_F_RXHASH; 169 170 ndev->features |= ndev->hw_features; 171 SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy)); 172 wdev->netdev = ndev; 173 174 return wil; 175 176 out_priv: 177 wil_priv_deinit(wil); 178 179 out_wdev: 180 wil_wdev_free(wil); 181 182 return ERR_PTR(rc); 183 } 184 185 void wil_if_free(struct wil6210_priv *wil) 186 { 187 struct net_device *ndev = wil_to_ndev(wil); 188 189 wil_dbg_misc(wil, "if_free\n"); 190 191 if (!ndev) 192 return; 193 194 wil_priv_deinit(wil); 195 196 wil_to_ndev(wil) = NULL; 197 free_netdev(ndev); 198 199 wil_wdev_free(wil); 200 } 201 202 int wil_if_add(struct wil6210_priv *wil) 203 { 204 struct wireless_dev *wdev = wil_to_wdev(wil); 205 struct wiphy *wiphy = wdev->wiphy; 206 struct net_device *ndev = wil_to_ndev(wil); 207 int rc; 208 209 wil_dbg_misc(wil, "entered"); 210 211 strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version)); 212 213 rc = wiphy_register(wiphy); 214 if (rc < 0) { 215 wil_err(wil, "failed to register wiphy, err %d\n", rc); 216 return rc; 217 } 218 219 netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx, 220 WIL6210_NAPI_BUDGET); 221 netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx, 222 WIL6210_NAPI_BUDGET); 223 224 wil_update_net_queues_bh(wil, NULL, true); 225 226 rc = register_netdev(ndev); 227 if (rc < 0) { 228 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc); 229 goto out_wiphy; 230 } 231 232 return 0; 233 234 out_wiphy: 235 wiphy_unregister(wdev->wiphy); 236 return rc; 237 } 238 239 void wil_if_remove(struct wil6210_priv *wil) 240 { 241 struct net_device *ndev = wil_to_ndev(wil); 242 struct wireless_dev *wdev = wil_to_wdev(wil); 243 244 wil_dbg_misc(wil, "if_remove\n"); 245 246 unregister_netdev(ndev); 247 wiphy_unregister(wdev->wiphy); 248 } 249