Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 1 | /* |
Maya Erez | 640751a | 2016-01-17 12:39:08 +0200 | [diff] [blame] | 2 | * Copyright (c) 2012-2016 Qualcomm Atheros, Inc. |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 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 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 17 | #include <linux/etherdevice.h> |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 18 | #include "wil6210.h" |
Vladimir Kondratiev | e0106ad | 2014-09-10 16:34:45 +0300 | [diff] [blame] | 19 | #include "txrx.h" |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 20 | |
| 21 | static int wil_open(struct net_device *ndev) |
| 22 | { |
| 23 | struct wil6210_priv *wil = ndev_to_wil(ndev); |
| 24 | |
Vladimir Kondratiev | 9cf10d6 | 2014-09-10 16:34:36 +0300 | [diff] [blame] | 25 | wil_dbg_misc(wil, "%s()\n", __func__); |
| 26 | |
Vladimir Kondratiev | bfc2dc7 | 2015-03-30 11:28:50 +0300 | [diff] [blame] | 27 | if (debug_fw) { |
| 28 | wil_err(wil, "%s() while in debug_fw mode\n", __func__); |
| 29 | return -EINVAL; |
| 30 | } |
| 31 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 32 | return wil_up(wil); |
| 33 | } |
| 34 | |
| 35 | static int wil_stop(struct net_device *ndev) |
| 36 | { |
| 37 | struct wil6210_priv *wil = ndev_to_wil(ndev); |
| 38 | |
Vladimir Kondratiev | 9cf10d6 | 2014-09-10 16:34:36 +0300 | [diff] [blame] | 39 | wil_dbg_misc(wil, "%s()\n", __func__); |
| 40 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 41 | return wil_down(wil); |
| 42 | } |
| 43 | |
Vladimir Kondratiev | d87bac1 | 2014-05-27 14:45:44 +0300 | [diff] [blame] | 44 | static int wil_change_mtu(struct net_device *ndev, int new_mtu) |
| 45 | { |
| 46 | struct wil6210_priv *wil = ndev_to_wil(ndev); |
| 47 | |
Vladimir Kondratiev | 9a06bec | 2014-10-28 16:51:27 +0200 | [diff] [blame] | 48 | if (new_mtu < 68 || new_mtu > mtu_max) { |
Vladimir Kondratiev | e0106ad | 2014-09-10 16:34:45 +0300 | [diff] [blame] | 49 | wil_err(wil, "invalid MTU %d\n", new_mtu); |
Vladimir Kondratiev | d87bac1 | 2014-05-27 14:45:44 +0300 | [diff] [blame] | 50 | return -EINVAL; |
Vladimir Kondratiev | e0106ad | 2014-09-10 16:34:45 +0300 | [diff] [blame] | 51 | } |
Vladimir Kondratiev | d87bac1 | 2014-05-27 14:45:44 +0300 | [diff] [blame] | 52 | |
| 53 | wil_dbg_misc(wil, "change MTU %d -> %d\n", ndev->mtu, new_mtu); |
| 54 | ndev->mtu = new_mtu; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Vladimir Kondratiev | dba4b74 | 2014-10-01 15:05:25 +0300 | [diff] [blame] | 59 | static int wil_do_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd) |
| 60 | { |
| 61 | struct wil6210_priv *wil = ndev_to_wil(ndev); |
| 62 | |
Lior David | 6777e71 | 2016-03-01 19:18:15 +0200 | [diff] [blame] | 63 | return wil_ioctl(wil, ifr->ifr_data, cmd); |
Vladimir Kondratiev | dba4b74 | 2014-10-01 15:05:25 +0300 | [diff] [blame] | 64 | } |
| 65 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 66 | static const struct net_device_ops wil_netdev_ops = { |
| 67 | .ndo_open = wil_open, |
| 68 | .ndo_stop = wil_stop, |
| 69 | .ndo_start_xmit = wil_start_xmit, |
Vladimir Kondratiev | afda8bb | 2013-01-28 18:31:07 +0200 | [diff] [blame] | 70 | .ndo_set_mac_address = eth_mac_addr, |
| 71 | .ndo_validate_addr = eth_validate_addr, |
Vladimir Kondratiev | d87bac1 | 2014-05-27 14:45:44 +0300 | [diff] [blame] | 72 | .ndo_change_mtu = wil_change_mtu, |
Vladimir Kondratiev | dba4b74 | 2014-10-01 15:05:25 +0300 | [diff] [blame] | 73 | .ndo_do_ioctl = wil_do_ioctl, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Vladimir Kondratiev | e0287c4 | 2013-05-12 14:43:36 +0300 | [diff] [blame] | 76 | static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget) |
| 77 | { |
| 78 | struct wil6210_priv *wil = container_of(napi, struct wil6210_priv, |
| 79 | napi_rx); |
| 80 | int quota = budget; |
| 81 | int done; |
| 82 | |
| 83 | wil_rx_handle(wil, "a); |
| 84 | done = budget - quota; |
| 85 | |
Vladimir Kondratiev | 7308a20 | 2015-03-08 15:42:01 +0200 | [diff] [blame] | 86 | if (done < budget) { |
Vladimir Kondratiev | e0287c4 | 2013-05-12 14:43:36 +0300 | [diff] [blame] | 87 | napi_complete(napi); |
| 88 | wil6210_unmask_irq_rx(wil); |
| 89 | wil_dbg_txrx(wil, "NAPI RX complete\n"); |
| 90 | } |
| 91 | |
| 92 | wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done); |
| 93 | |
| 94 | return done; |
| 95 | } |
| 96 | |
| 97 | static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget) |
| 98 | { |
| 99 | struct wil6210_priv *wil = container_of(napi, struct wil6210_priv, |
| 100 | napi_tx); |
| 101 | int tx_done = 0; |
| 102 | uint i; |
| 103 | |
| 104 | /* always process ALL Tx complete, regardless budget - it is fast */ |
| 105 | for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) { |
| 106 | struct vring *vring = &wil->vring_tx[i]; |
Maya Erez | 640751a | 2016-01-17 12:39:08 +0200 | [diff] [blame] | 107 | struct vring_tx_data *txdata = &wil->vring_tx_data[i]; |
Vladimir Kondratiev | e0287c4 | 2013-05-12 14:43:36 +0300 | [diff] [blame] | 108 | |
Maya Erez | 640751a | 2016-01-17 12:39:08 +0200 | [diff] [blame] | 109 | if (!vring->va || !txdata->enabled) |
Vladimir Kondratiev | e0287c4 | 2013-05-12 14:43:36 +0300 | [diff] [blame] | 110 | continue; |
| 111 | |
| 112 | tx_done += wil_tx_complete(wil, i); |
| 113 | } |
| 114 | |
Vladimir Kondratiev | 7308a20 | 2015-03-08 15:42:01 +0200 | [diff] [blame] | 115 | if (tx_done < budget) { |
Vladimir Kondratiev | e0287c4 | 2013-05-12 14:43:36 +0300 | [diff] [blame] | 116 | napi_complete(napi); |
| 117 | wil6210_unmask_irq_tx(wil); |
| 118 | wil_dbg_txrx(wil, "NAPI TX complete\n"); |
| 119 | } |
| 120 | |
| 121 | wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done); |
| 122 | |
| 123 | return min(tx_done, budget); |
| 124 | } |
| 125 | |
Vladimir Shulman | f1871cd | 2015-01-25 10:52:45 +0200 | [diff] [blame] | 126 | static void wil_dev_setup(struct net_device *dev) |
| 127 | { |
| 128 | ether_setup(dev); |
| 129 | dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT; |
| 130 | } |
| 131 | |
Vladimir Kondratiev | 3e2d8e1 | 2015-06-09 14:11:19 +0300 | [diff] [blame] | 132 | void *wil_if_alloc(struct device *dev) |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 133 | { |
| 134 | struct net_device *ndev; |
| 135 | struct wireless_dev *wdev; |
| 136 | struct wil6210_priv *wil; |
| 137 | struct ieee80211_channel *ch; |
| 138 | int rc = 0; |
| 139 | |
| 140 | wdev = wil_cfg80211_init(dev); |
| 141 | if (IS_ERR(wdev)) { |
| 142 | dev_err(dev, "wil_cfg80211_init failed\n"); |
| 143 | return wdev; |
| 144 | } |
| 145 | |
| 146 | wil = wdev_to_wil(wdev); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 147 | wil->wdev = wdev; |
Lior David | 4332cac | 2016-03-01 19:18:13 +0200 | [diff] [blame] | 148 | wil->radio_wdev = wdev; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 149 | |
Vladimir Kondratiev | 9cf10d6 | 2014-09-10 16:34:36 +0300 | [diff] [blame] | 150 | wil_dbg_misc(wil, "%s()\n", __func__); |
| 151 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 152 | rc = wil_priv_init(wil); |
| 153 | if (rc) { |
| 154 | dev_err(dev, "wil_priv_init failed\n"); |
| 155 | goto out_wdev; |
| 156 | } |
| 157 | |
| 158 | wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */ |
| 159 | /* default monitor channel */ |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 160 | ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 161 | cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT); |
| 162 | |
Vladimir Shulman | f1871cd | 2015-01-25 10:52:45 +0200 | [diff] [blame] | 163 | ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 164 | if (!ndev) { |
| 165 | dev_err(dev, "alloc_netdev_mqs failed\n"); |
| 166 | rc = -ENOMEM; |
| 167 | goto out_priv; |
| 168 | } |
| 169 | |
| 170 | ndev->netdev_ops = &wil_netdev_ops; |
Vladimir Kondratiev | b6b1b0e | 2014-09-22 15:31:41 +0300 | [diff] [blame] | 171 | wil_set_ethtoolops(ndev); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 172 | ndev->ieee80211_ptr = wdev; |
Vladimir Kondratiev | c236658 | 2014-03-17 15:34:08 +0200 | [diff] [blame] | 173 | ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM | |
Vladimir Kondratiev | 3d4bde1 | 2015-07-30 13:51:56 +0300 | [diff] [blame] | 174 | NETIF_F_SG | NETIF_F_GRO | |
Vladimir Shulman | 0553640 | 2015-07-30 13:52:04 +0300 | [diff] [blame] | 175 | NETIF_F_TSO | NETIF_F_TSO6 | |
| 176 | NETIF_F_RXHASH; |
Vladimir Kondratiev | 3d4bde1 | 2015-07-30 13:51:56 +0300 | [diff] [blame] | 177 | |
Vladimir Kondratiev | c236658 | 2014-03-17 15:34:08 +0200 | [diff] [blame] | 178 | ndev->features |= ndev->hw_features; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 179 | SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy)); |
| 180 | wdev->netdev = ndev; |
| 181 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 182 | return wil; |
| 183 | |
| 184 | out_priv: |
| 185 | wil_priv_deinit(wil); |
| 186 | |
| 187 | out_wdev: |
| 188 | wil_wdev_free(wil); |
| 189 | |
| 190 | return ERR_PTR(rc); |
| 191 | } |
| 192 | |
| 193 | void wil_if_free(struct wil6210_priv *wil) |
| 194 | { |
| 195 | struct net_device *ndev = wil_to_ndev(wil); |
Vladimir Kondratiev | 8fcfdea | 2014-08-06 10:31:59 +0300 | [diff] [blame] | 196 | |
Vladimir Kondratiev | 9cf10d6 | 2014-09-10 16:34:36 +0300 | [diff] [blame] | 197 | wil_dbg_misc(wil, "%s()\n", __func__); |
| 198 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 199 | if (!ndev) |
| 200 | return; |
| 201 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 202 | wil_priv_deinit(wil); |
Vladimir Kondratiev | 8fcfdea | 2014-08-06 10:31:59 +0300 | [diff] [blame] | 203 | |
| 204 | wil_to_ndev(wil) = NULL; |
| 205 | free_netdev(ndev); |
| 206 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 207 | wil_wdev_free(wil); |
| 208 | } |
| 209 | |
| 210 | int wil_if_add(struct wil6210_priv *wil) |
| 211 | { |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 212 | struct wireless_dev *wdev = wil_to_wdev(wil); |
| 213 | struct wiphy *wiphy = wdev->wiphy; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 214 | struct net_device *ndev = wil_to_ndev(wil); |
| 215 | int rc; |
| 216 | |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 217 | wil_dbg_misc(wil, "entered"); |
| 218 | |
Lior David | 13cd9f7 | 2016-08-22 12:42:22 +0300 | [diff] [blame] | 219 | strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version)); |
| 220 | |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 221 | rc = wiphy_register(wiphy); |
| 222 | if (rc < 0) { |
| 223 | wil_err(wil, "failed to register wiphy, err %d\n", rc); |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx, |
| 228 | WIL6210_NAPI_BUDGET); |
| 229 | netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx, |
| 230 | WIL6210_NAPI_BUDGET); |
| 231 | |
| 232 | netif_tx_stop_all_queues(ndev); |
Vladimir Kondratiev | 9cf10d6 | 2014-09-10 16:34:36 +0300 | [diff] [blame] | 233 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 234 | rc = register_netdev(ndev); |
| 235 | if (rc < 0) { |
| 236 | dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc); |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 237 | goto out_wiphy; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 240 | return 0; |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 241 | |
| 242 | out_wiphy: |
| 243 | wiphy_unregister(wdev->wiphy); |
| 244 | return rc; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void wil_if_remove(struct wil6210_priv *wil) |
| 248 | { |
| 249 | struct net_device *ndev = wil_to_ndev(wil); |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 250 | struct wireless_dev *wdev = wil_to_wdev(wil); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 251 | |
Vladimir Kondratiev | 9cf10d6 | 2014-09-10 16:34:36 +0300 | [diff] [blame] | 252 | wil_dbg_misc(wil, "%s()\n", __func__); |
| 253 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 254 | unregister_netdev(ndev); |
Lior David | 2690c4c | 2016-08-18 16:52:16 +0300 | [diff] [blame] | 255 | wiphy_unregister(wdev->wiphy); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 256 | } |