Merge 5.4-rc7 into staging-next
[linux-2.6-microblaze.git] / drivers / staging / wfx / main.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Device probe and register.
4  *
5  * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  * Copyright (c) 2008, Johannes Berg <johannes@sipsolutions.net>
8  * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
9  * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
10  * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
11  * Copyright (c) 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
12  */
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_net.h>
16 #include <linux/gpio.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/mmc/sdio_func.h>
19 #include <linux/spi/spi.h>
20 #include <linux/etherdevice.h>
21 #include <linux/firmware.h>
22
23 #include "main.h"
24 #include "wfx.h"
25 #include "fwio.h"
26 #include "hwio.h"
27 #include "bus.h"
28 #include "bh.h"
29 #include "sta.h"
30 #include "key.h"
31 #include "debug.h"
32 #include "data_tx.h"
33 #include "secure_link.h"
34 #include "hif_tx_mib.h"
35 #include "hif_api_cmd.h"
36
37 #define WFX_PDS_MAX_SIZE 1500
38
39 MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WFx");
40 MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>");
41 MODULE_LICENSE("GPL");
42
43 static int gpio_wakeup = -2;
44 module_param(gpio_wakeup, int, 0644);
45 MODULE_PARM_DESC(gpio_wakeup, "gpio number for wakeup. -1 for none.");
46
47 #define RATETAB_ENT(_rate, _rateid, _flags) { \
48         .bitrate  = (_rate),   \
49         .hw_value = (_rateid), \
50         .flags    = (_flags),  \
51 }
52
53 static struct ieee80211_rate wfx_rates[] = {
54         RATETAB_ENT(10,  0,  0),
55         RATETAB_ENT(20,  1,  IEEE80211_RATE_SHORT_PREAMBLE),
56         RATETAB_ENT(55,  2,  IEEE80211_RATE_SHORT_PREAMBLE),
57         RATETAB_ENT(110, 3,  IEEE80211_RATE_SHORT_PREAMBLE),
58         RATETAB_ENT(60,  6,  0),
59         RATETAB_ENT(90,  7,  0),
60         RATETAB_ENT(120, 8,  0),
61         RATETAB_ENT(180, 9,  0),
62         RATETAB_ENT(240, 10, 0),
63         RATETAB_ENT(360, 11, 0),
64         RATETAB_ENT(480, 12, 0),
65         RATETAB_ENT(540, 13, 0),
66 };
67
68 #define CHAN2G(_channel, _freq, _flags) { \
69         .band = NL80211_BAND_2GHZ, \
70         .center_freq = (_freq),    \
71         .hw_value = (_channel),    \
72         .flags = (_flags),         \
73         .max_antenna_gain = 0,     \
74         .max_power = 30,           \
75 }
76
77 static struct ieee80211_channel wfx_2ghz_chantable[] = {
78         CHAN2G(1,  2412, 0),
79         CHAN2G(2,  2417, 0),
80         CHAN2G(3,  2422, 0),
81         CHAN2G(4,  2427, 0),
82         CHAN2G(5,  2432, 0),
83         CHAN2G(6,  2437, 0),
84         CHAN2G(7,  2442, 0),
85         CHAN2G(8,  2447, 0),
86         CHAN2G(9,  2452, 0),
87         CHAN2G(10, 2457, 0),
88         CHAN2G(11, 2462, 0),
89         CHAN2G(12, 2467, 0),
90         CHAN2G(13, 2472, 0),
91         CHAN2G(14, 2484, 0),
92 };
93
94 static const struct ieee80211_supported_band wfx_band_2ghz = {
95         .channels = wfx_2ghz_chantable,
96         .n_channels = ARRAY_SIZE(wfx_2ghz_chantable),
97         .bitrates = wfx_rates,
98         .n_bitrates = ARRAY_SIZE(wfx_rates),
99         .ht_cap = {
100                 // Receive caps
101                 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
102                        IEEE80211_HT_CAP_MAX_AMSDU | (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT),
103                 .ht_supported = 1,
104                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K,
105                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE,
106                 .mcs = {
107                         .rx_mask = { 0xFF }, // MCS0 to MCS7
108                         .rx_highest = 65,
109                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
110                 },
111         },
112 };
113
114 static const struct ieee80211_iface_limit wdev_iface_limits[] = {
115         { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
116         { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
117 };
118
119 static const struct ieee80211_iface_combination wfx_iface_combinations[] = {
120         {
121                 .num_different_channels = 2,
122                 .max_interfaces = 2,
123                 .limits = wdev_iface_limits,
124                 .n_limits = ARRAY_SIZE(wdev_iface_limits),
125         }
126 };
127
128 static const struct ieee80211_ops wfx_ops = {
129         .start                  = wfx_start,
130         .stop                   = wfx_stop,
131         .add_interface          = wfx_add_interface,
132         .remove_interface       = wfx_remove_interface,
133         .config                 = wfx_config,
134         .tx                     = wfx_tx,
135         .conf_tx                = wfx_conf_tx,
136         .hw_scan                = wfx_hw_scan,
137         .sta_add                = wfx_sta_add,
138         .sta_remove             = wfx_sta_remove,
139         .sta_notify             = wfx_sta_notify,
140         .set_tim                = wfx_set_tim,
141         .set_key                = wfx_set_key,
142         .set_rts_threshold      = wfx_set_rts_threshold,
143         .bss_info_changed       = wfx_bss_info_changed,
144         .prepare_multicast      = wfx_prepare_multicast,
145         .configure_filter       = wfx_configure_filter,
146         .ampdu_action           = wfx_ampdu_action,
147         .flush                  = wfx_flush,
148         .add_chanctx            = wfx_add_chanctx,
149         .remove_chanctx         = wfx_remove_chanctx,
150         .change_chanctx         = wfx_change_chanctx,
151         .assign_vif_chanctx     = wfx_assign_vif_chanctx,
152         .unassign_vif_chanctx   = wfx_unassign_vif_chanctx,
153 };
154
155 bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor)
156 {
157         if (wdev->hw_caps.api_version_major < major)
158                 return true;
159         if (wdev->hw_caps.api_version_major > major)
160                 return false;
161         if (wdev->hw_caps.api_version_minor < minor)
162                 return true;
163         return false;
164 }
165
166 struct gpio_desc *wfx_get_gpio(struct device *dev, int override, const char *label)
167 {
168         struct gpio_desc *ret;
169         char label_buf[256];
170
171         if (override >= 0) {
172                 snprintf(label_buf, sizeof(label_buf), "wfx_%s", label);
173                 ret = ERR_PTR(devm_gpio_request_one(dev, override, GPIOF_OUT_INIT_LOW, label_buf));
174                 if (!ret)
175                         ret = gpio_to_desc(override);
176         } else if (override == -1) {
177                 ret = NULL;
178         } else {
179                 ret = devm_gpiod_get(dev, label, GPIOD_OUT_LOW);
180         }
181         if (IS_ERR(ret) || !ret) {
182                 if (!ret || PTR_ERR(ret) == -ENOENT)
183                         dev_warn(dev, "gpio %s is not defined\n", label);
184                 else
185                         dev_warn(dev, "error while requesting gpio %s\n", label);
186                 ret = NULL;
187         } else {
188                 dev_dbg(dev, "using gpio %d for %s\n", desc_to_gpio(ret), label);
189         }
190         return ret;
191 }
192
193 /* NOTE: wfx_send_pds() destroy buf */
194 int wfx_send_pds(struct wfx_dev *wdev, unsigned char *buf, size_t len)
195 {
196         int ret;
197         int start, brace_level, i;
198
199         start = 0;
200         brace_level = 0;
201         if (buf[0] != '{') {
202                 dev_err(wdev->dev, "valid PDS start with '{'. Did you forget to compress it?\n");
203                 return -EINVAL;
204         }
205         for (i = 1; i < len - 1; i++) {
206                 if (buf[i] == '{')
207                         brace_level++;
208                 if (buf[i] == '}')
209                         brace_level--;
210                 if (buf[i] == '}' && !brace_level) {
211                         i++;
212                         if (i - start + 1 > WFX_PDS_MAX_SIZE)
213                                 return -EFBIG;
214                         buf[start] = '{';
215                         buf[i] = 0;
216                         dev_dbg(wdev->dev, "send PDS '%s}'\n", buf + start);
217                         buf[i] = '}';
218                         ret = hif_configuration(wdev, buf + start, i - start + 1);
219                         if (ret == HIF_STATUS_FAILURE) {
220                                 dev_err(wdev->dev, "PDS bytes %d to %d: invalid data (unsupported options?)\n", start, i);
221                                 return -EINVAL;
222                         }
223                         if (ret == -ETIMEDOUT) {
224                                 dev_err(wdev->dev, "PDS bytes %d to %d: chip didn't reply (corrupted file?)\n", start, i);
225                                 return ret;
226                         }
227                         if (ret) {
228                                 dev_err(wdev->dev, "PDS bytes %d to %d: chip returned an unknown error\n", start, i);
229                                 return -EIO;
230                         }
231                         buf[i] = ',';
232                         start = i;
233                 }
234         }
235         return 0;
236 }
237
238 static int wfx_send_pdata_pds(struct wfx_dev *wdev)
239 {
240         int ret = 0;
241         const struct firmware *pds;
242         unsigned char *tmp_buf;
243
244         ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
245         if (ret) {
246                 dev_err(wdev->dev, "can't load PDS file %s\n", wdev->pdata.file_pds);
247                 return ret;
248         }
249         tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
250         ret = wfx_send_pds(wdev, tmp_buf, pds->size);
251         kfree(tmp_buf);
252         release_firmware(pds);
253         return ret;
254 }
255
256 struct wfx_dev *wfx_init_common(struct device *dev,
257                                 const struct wfx_platform_data *pdata,
258                                 const struct hwbus_ops *hwbus_ops,
259                                 void *hwbus_priv)
260 {
261         struct ieee80211_hw *hw;
262         struct wfx_dev *wdev;
263
264         hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
265         if (!hw)
266                 return NULL;
267
268         SET_IEEE80211_DEV(hw, dev);
269
270         ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
271         ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
272         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
273         ieee80211_hw_set(hw, CONNECTION_MONITOR);
274         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
275         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
276         ieee80211_hw_set(hw, SIGNAL_DBM);
277         ieee80211_hw_set(hw, SUPPORTS_PS);
278         ieee80211_hw_set(hw, MFP_CAPABLE);
279
280         hw->vif_data_size = sizeof(struct wfx_vif);
281         hw->sta_data_size = sizeof(struct wfx_sta_priv);
282         hw->queues = 4;
283         hw->max_rates = 8;
284         hw->max_rate_tries = 15;
285         hw->extra_tx_headroom = sizeof(struct hif_sl_msg_hdr) + sizeof(struct hif_msg)
286                                 + sizeof(struct hif_req_tx)
287                                 + 4 /* alignment */ + 8 /* TKIP IV */;
288         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
289                                      BIT(NL80211_IFTYPE_ADHOC) |
290                                      BIT(NL80211_IFTYPE_AP);
291         hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
292         hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
293         hw->wiphy->max_ap_assoc_sta = WFX_MAX_STA_IN_AP_MODE;
294         hw->wiphy->max_scan_ssids = 2;
295         hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
296         hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
297         hw->wiphy->iface_combinations = wfx_iface_combinations;
298         hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmalloc(dev, sizeof(wfx_band_2ghz), GFP_KERNEL);
299         // FIXME: also copy wfx_rates and wfx_2ghz_chantable
300         memcpy(hw->wiphy->bands[NL80211_BAND_2GHZ], &wfx_band_2ghz, sizeof(wfx_band_2ghz));
301
302         wdev = hw->priv;
303         wdev->hw = hw;
304         wdev->dev = dev;
305         wdev->hwbus_ops = hwbus_ops;
306         wdev->hwbus_priv = hwbus_priv;
307         memcpy(&wdev->pdata, pdata, sizeof(*pdata));
308         of_property_read_string(dev->of_node, "config-file", &wdev->pdata.file_pds);
309         wdev->pdata.gpio_wakeup = wfx_get_gpio(dev, gpio_wakeup, "wakeup");
310         wfx_sl_fill_pdata(dev, &wdev->pdata);
311
312         mutex_init(&wdev->conf_mutex);
313         mutex_init(&wdev->rx_stats_lock);
314         init_completion(&wdev->firmware_ready);
315         wfx_init_hif_cmd(&wdev->hif_cmd);
316         wfx_tx_queues_init(wdev);
317
318         return wdev;
319 }
320
321 void wfx_free_common(struct wfx_dev *wdev)
322 {
323         mutex_destroy(&wdev->rx_stats_lock);
324         mutex_destroy(&wdev->conf_mutex);
325         wfx_tx_queues_deinit(wdev);
326         ieee80211_free_hw(wdev->hw);
327 }
328
329 int wfx_probe(struct wfx_dev *wdev)
330 {
331         int i;
332         int err;
333         const void *macaddr;
334         struct gpio_desc *gpio_saved;
335
336         // During first part of boot, gpio_wakeup cannot yet been used. So
337         // prevent bh() to touch it.
338         gpio_saved = wdev->pdata.gpio_wakeup;
339         wdev->pdata.gpio_wakeup = NULL;
340
341         wfx_bh_register(wdev);
342
343         err = wfx_init_device(wdev);
344         if (err)
345                 goto err1;
346
347         err = wait_for_completion_interruptible_timeout(&wdev->firmware_ready, 10 * HZ);
348         if (err <= 0) {
349                 if (err == 0) {
350                         dev_err(wdev->dev, "timeout while waiting for startup indication. IRQ configuration error?\n");
351                         err = -ETIMEDOUT;
352                 } else if (err == -ERESTARTSYS) {
353                         dev_info(wdev->dev, "probe interrupted by user\n");
354                 }
355                 goto err1;
356         }
357
358         // FIXME: fill wiphy::hw_version
359         dev_info(wdev->dev, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
360                  wdev->hw_caps.firmware_major, wdev->hw_caps.firmware_minor,
361                  wdev->hw_caps.firmware_build, wdev->hw_caps.firmware_label,
362                  wdev->hw_caps.api_version_major, wdev->hw_caps.api_version_minor,
363                  wdev->keyset, *((u32 *) &wdev->hw_caps.capabilities));
364         snprintf(wdev->hw->wiphy->fw_version, sizeof(wdev->hw->wiphy->fw_version),
365                  "%d.%d.%d",
366                  wdev->hw_caps.firmware_major,
367                  wdev->hw_caps.firmware_minor,
368                  wdev->hw_caps.firmware_build);
369
370         if (wfx_api_older_than(wdev, 1, 0)) {
371                 dev_err(wdev->dev, "unsupported firmware API version (expect 1 while firmware returns %d)\n",
372                         wdev->hw_caps.api_version_major);
373                 err = -ENOTSUPP;
374                 goto err1;
375         }
376
377         err = wfx_sl_init(wdev);
378         if (err && wdev->hw_caps.capabilities.link_mode == SEC_LINK_ENFORCED) {
379                 dev_err(wdev->dev, "chip require secure_link, but can't negociate it\n");
380                 goto err1;
381         }
382
383         if (wdev->hw_caps.regul_sel_mode_info.region_sel_mode) {
384                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |= IEEE80211_CHAN_NO_IR;
385                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |= IEEE80211_CHAN_NO_IR;
386                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |= IEEE80211_CHAN_DISABLED;
387         }
388
389         dev_dbg(wdev->dev, "sending configuration file %s\n", wdev->pdata.file_pds);
390         err = wfx_send_pdata_pds(wdev);
391         if (err < 0)
392                 goto err1;
393
394         wdev->pdata.gpio_wakeup = gpio_saved;
395         if (wdev->pdata.gpio_wakeup) {
396                 dev_dbg(wdev->dev, "enable 'quiescent' power mode with gpio %d and PDS file %s\n",
397                         desc_to_gpio(wdev->pdata.gpio_wakeup), wdev->pdata.file_pds);
398                 gpiod_set_value(wdev->pdata.gpio_wakeup, 1);
399                 control_reg_write(wdev, 0);
400                 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
401         } else {
402                 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
403         }
404
405         hif_use_multi_tx_conf(wdev, true);
406
407         for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
408                 eth_zero_addr(wdev->addresses[i].addr);
409                 macaddr = of_get_mac_address(wdev->dev->of_node);
410                 if (!IS_ERR_OR_NULL(macaddr)) {
411                         ether_addr_copy(wdev->addresses[i].addr, macaddr);
412                         wdev->addresses[i].addr[ETH_ALEN - 1] += i;
413                 } else {
414                         ether_addr_copy(wdev->addresses[i].addr, wdev->hw_caps.mac_addr[i]);
415                 }
416                 if (!is_valid_ether_addr(wdev->addresses[i].addr)) {
417                         dev_warn(wdev->dev, "using random MAC address\n");
418                         eth_random_addr(wdev->addresses[i].addr);
419                 }
420                 dev_info(wdev->dev, "MAC address %d: %pM\n", i, wdev->addresses[i].addr);
421         }
422         wdev->hw->wiphy->n_addresses = ARRAY_SIZE(wdev->addresses);
423         wdev->hw->wiphy->addresses = wdev->addresses;
424
425         err = ieee80211_register_hw(wdev->hw);
426         if (err)
427                 goto err1;
428
429         err = wfx_debug_init(wdev);
430         if (err)
431                 goto err2;
432
433         return 0;
434
435 err2:
436         ieee80211_unregister_hw(wdev->hw);
437         ieee80211_free_hw(wdev->hw);
438 err1:
439         wfx_bh_unregister(wdev);
440         return err;
441 }
442
443 void wfx_release(struct wfx_dev *wdev)
444 {
445         ieee80211_unregister_hw(wdev->hw);
446         hif_shutdown(wdev);
447         wfx_bh_unregister(wdev);
448         wfx_sl_deinit(wdev);
449 }
450
451 static int __init wfx_core_init(void)
452 {
453         int ret = 0;
454
455         if (IS_ENABLED(CONFIG_SPI))
456                 ret = spi_register_driver(&wfx_spi_driver);
457         if (IS_ENABLED(CONFIG_MMC) && !ret)
458                 ret = sdio_register_driver(&wfx_sdio_driver);
459         return ret;
460 }
461 module_init(wfx_core_init);
462
463 static void __exit wfx_core_exit(void)
464 {
465         if (IS_ENABLED(CONFIG_MMC))
466                 sdio_unregister_driver(&wfx_sdio_driver);
467         if (IS_ENABLED(CONFIG_SPI))
468                 spi_unregister_driver(&wfx_spi_driver);
469 }
470 module_exit(wfx_core_exit);