Merge branch 'for-5.5' of git://git.kernel.org/pub/scm/linux/kernel/git/dennis/percpu
[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 |
103                        (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT),
104                 .ht_supported = 1,
105                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K,
106                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE,
107                 .mcs = {
108                         .rx_mask = { 0xFF }, // MCS0 to MCS7
109                         .rx_highest = 65,
110                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
111                 },
112         },
113 };
114
115 static const struct ieee80211_iface_limit wdev_iface_limits[] = {
116         { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
117         { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
118 };
119
120 static const struct ieee80211_iface_combination wfx_iface_combinations[] = {
121         {
122                 .num_different_channels = 2,
123                 .max_interfaces = 2,
124                 .limits = wdev_iface_limits,
125                 .n_limits = ARRAY_SIZE(wdev_iface_limits),
126         }
127 };
128
129 static const struct ieee80211_ops wfx_ops = {
130         .start                  = wfx_start,
131         .stop                   = wfx_stop,
132         .add_interface          = wfx_add_interface,
133         .remove_interface       = wfx_remove_interface,
134         .config                 = wfx_config,
135         .tx                     = wfx_tx,
136         .conf_tx                = wfx_conf_tx,
137         .hw_scan                = wfx_hw_scan,
138         .sta_add                = wfx_sta_add,
139         .sta_remove             = wfx_sta_remove,
140         .sta_notify             = wfx_sta_notify,
141         .set_tim                = wfx_set_tim,
142         .set_key                = wfx_set_key,
143         .set_rts_threshold      = wfx_set_rts_threshold,
144         .bss_info_changed       = wfx_bss_info_changed,
145         .prepare_multicast      = wfx_prepare_multicast,
146         .configure_filter       = wfx_configure_filter,
147         .ampdu_action           = wfx_ampdu_action,
148         .flush                  = wfx_flush,
149         .add_chanctx            = wfx_add_chanctx,
150         .remove_chanctx         = wfx_remove_chanctx,
151         .change_chanctx         = wfx_change_chanctx,
152         .assign_vif_chanctx     = wfx_assign_vif_chanctx,
153         .unassign_vif_chanctx   = wfx_unassign_vif_chanctx,
154 };
155
156 bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor)
157 {
158         if (wdev->hw_caps.api_version_major < major)
159                 return true;
160         if (wdev->hw_caps.api_version_major > major)
161                 return false;
162         if (wdev->hw_caps.api_version_minor < minor)
163                 return true;
164         return false;
165 }
166
167 struct gpio_desc *wfx_get_gpio(struct device *dev, int override,
168                                const char *label)
169 {
170         struct gpio_desc *ret;
171         char label_buf[256];
172
173         if (override >= 0) {
174                 snprintf(label_buf, sizeof(label_buf), "wfx_%s", label);
175                 ret = ERR_PTR(devm_gpio_request_one(dev, override,
176                                                     GPIOF_OUT_INIT_LOW,
177                                                     label_buf));
178                 if (!ret)
179                         ret = gpio_to_desc(override);
180         } else if (override == -1) {
181                 ret = NULL;
182         } else {
183                 ret = devm_gpiod_get(dev, label, GPIOD_OUT_LOW);
184         }
185         if (IS_ERR(ret) || !ret) {
186                 if (!ret || PTR_ERR(ret) == -ENOENT)
187                         dev_warn(dev, "gpio %s is not defined\n", label);
188                 else
189                         dev_warn(dev,
190                                  "error while requesting gpio %s\n", label);
191                 ret = NULL;
192         } else {
193                 dev_dbg(dev,
194                         "using gpio %d for %s\n", desc_to_gpio(ret), label);
195         }
196         return ret;
197 }
198
199 /* NOTE: wfx_send_pds() destroy buf */
200 int wfx_send_pds(struct wfx_dev *wdev, unsigned char *buf, size_t len)
201 {
202         int ret;
203         int start, brace_level, i;
204
205         start = 0;
206         brace_level = 0;
207         if (buf[0] != '{') {
208                 dev_err(wdev->dev, "valid PDS start with '{'. Did you forget to compress it?\n");
209                 return -EINVAL;
210         }
211         for (i = 1; i < len - 1; i++) {
212                 if (buf[i] == '{')
213                         brace_level++;
214                 if (buf[i] == '}')
215                         brace_level--;
216                 if (buf[i] == '}' && !brace_level) {
217                         i++;
218                         if (i - start + 1 > WFX_PDS_MAX_SIZE)
219                                 return -EFBIG;
220                         buf[start] = '{';
221                         buf[i] = 0;
222                         dev_dbg(wdev->dev, "send PDS '%s}'\n", buf + start);
223                         buf[i] = '}';
224                         ret = hif_configuration(wdev, buf + start,
225                                                 i - start + 1);
226                         if (ret == HIF_STATUS_FAILURE) {
227                                 dev_err(wdev->dev, "PDS bytes %d to %d: invalid data (unsupported options?)\n", start, i);
228                                 return -EINVAL;
229                         }
230                         if (ret == -ETIMEDOUT) {
231                                 dev_err(wdev->dev, "PDS bytes %d to %d: chip didn't reply (corrupted file?)\n", start, i);
232                                 return ret;
233                         }
234                         if (ret) {
235                                 dev_err(wdev->dev, "PDS bytes %d to %d: chip returned an unknown error\n", start, i);
236                                 return -EIO;
237                         }
238                         buf[i] = ',';
239                         start = i;
240                 }
241         }
242         return 0;
243 }
244
245 static int wfx_send_pdata_pds(struct wfx_dev *wdev)
246 {
247         int ret = 0;
248         const struct firmware *pds;
249         unsigned char *tmp_buf;
250
251         ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
252         if (ret) {
253                 dev_err(wdev->dev, "can't load PDS file %s\n",
254                         wdev->pdata.file_pds);
255                 return ret;
256         }
257         tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
258         ret = wfx_send_pds(wdev, tmp_buf, pds->size);
259         kfree(tmp_buf);
260         release_firmware(pds);
261         return ret;
262 }
263
264 struct wfx_dev *wfx_init_common(struct device *dev,
265                                 const struct wfx_platform_data *pdata,
266                                 const struct hwbus_ops *hwbus_ops,
267                                 void *hwbus_priv)
268 {
269         struct ieee80211_hw *hw;
270         struct wfx_dev *wdev;
271
272         hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
273         if (!hw)
274                 return NULL;
275
276         SET_IEEE80211_DEV(hw, dev);
277
278         ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
279         ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
280         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
281         ieee80211_hw_set(hw, CONNECTION_MONITOR);
282         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
283         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
284         ieee80211_hw_set(hw, SIGNAL_DBM);
285         ieee80211_hw_set(hw, SUPPORTS_PS);
286         ieee80211_hw_set(hw, MFP_CAPABLE);
287
288         hw->vif_data_size = sizeof(struct wfx_vif);
289         hw->sta_data_size = sizeof(struct wfx_sta_priv);
290         hw->queues = 4;
291         hw->max_rates = 8;
292         hw->max_rate_tries = 15;
293         hw->extra_tx_headroom = sizeof(struct hif_sl_msg_hdr) +
294                                 sizeof(struct hif_msg)
295                                 + sizeof(struct hif_req_tx)
296                                 + 4 /* alignment */ + 8 /* TKIP IV */;
297         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
298                                      BIT(NL80211_IFTYPE_ADHOC) |
299                                      BIT(NL80211_IFTYPE_AP);
300         hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
301         hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
302         hw->wiphy->max_ap_assoc_sta = WFX_MAX_STA_IN_AP_MODE;
303         hw->wiphy->max_scan_ssids = 2;
304         hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
305         hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
306         hw->wiphy->iface_combinations = wfx_iface_combinations;
307         hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmalloc(dev, sizeof(wfx_band_2ghz), GFP_KERNEL);
308         // FIXME: also copy wfx_rates and wfx_2ghz_chantable
309         memcpy(hw->wiphy->bands[NL80211_BAND_2GHZ], &wfx_band_2ghz,
310                sizeof(wfx_band_2ghz));
311
312         wdev = hw->priv;
313         wdev->hw = hw;
314         wdev->dev = dev;
315         wdev->hwbus_ops = hwbus_ops;
316         wdev->hwbus_priv = hwbus_priv;
317         memcpy(&wdev->pdata, pdata, sizeof(*pdata));
318         of_property_read_string(dev->of_node, "config-file",
319                                 &wdev->pdata.file_pds);
320         wdev->pdata.gpio_wakeup = wfx_get_gpio(dev, gpio_wakeup, "wakeup");
321         wfx_sl_fill_pdata(dev, &wdev->pdata);
322
323         mutex_init(&wdev->conf_mutex);
324         mutex_init(&wdev->rx_stats_lock);
325         init_completion(&wdev->firmware_ready);
326         wfx_init_hif_cmd(&wdev->hif_cmd);
327         wfx_tx_queues_init(wdev);
328
329         return wdev;
330 }
331
332 void wfx_free_common(struct wfx_dev *wdev)
333 {
334         mutex_destroy(&wdev->rx_stats_lock);
335         mutex_destroy(&wdev->conf_mutex);
336         wfx_tx_queues_deinit(wdev);
337         ieee80211_free_hw(wdev->hw);
338 }
339
340 int wfx_probe(struct wfx_dev *wdev)
341 {
342         int i;
343         int err;
344         const void *macaddr;
345         struct gpio_desc *gpio_saved;
346
347         // During first part of boot, gpio_wakeup cannot yet been used. So
348         // prevent bh() to touch it.
349         gpio_saved = wdev->pdata.gpio_wakeup;
350         wdev->pdata.gpio_wakeup = NULL;
351
352         wfx_bh_register(wdev);
353
354         err = wfx_init_device(wdev);
355         if (err)
356                 goto err1;
357
358         err = wait_for_completion_interruptible_timeout(&wdev->firmware_ready,
359                                                         10 * HZ);
360         if (err <= 0) {
361                 if (err == 0) {
362                         dev_err(wdev->dev, "timeout while waiting for startup indication. IRQ configuration error?\n");
363                         err = -ETIMEDOUT;
364                 } else if (err == -ERESTARTSYS) {
365                         dev_info(wdev->dev, "probe interrupted by user\n");
366                 }
367                 goto err1;
368         }
369
370         // FIXME: fill wiphy::hw_version
371         dev_info(wdev->dev, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
372                  wdev->hw_caps.firmware_major, wdev->hw_caps.firmware_minor,
373                  wdev->hw_caps.firmware_build, wdev->hw_caps.firmware_label,
374                  wdev->hw_caps.api_version_major,
375                  wdev->hw_caps.api_version_minor,
376                  wdev->keyset, *((u32 *) &wdev->hw_caps.capabilities));
377         snprintf(wdev->hw->wiphy->fw_version,
378                  sizeof(wdev->hw->wiphy->fw_version),
379                  "%d.%d.%d",
380                  wdev->hw_caps.firmware_major,
381                  wdev->hw_caps.firmware_minor,
382                  wdev->hw_caps.firmware_build);
383
384         if (wfx_api_older_than(wdev, 1, 0)) {
385                 dev_err(wdev->dev,
386                         "unsupported firmware API version (expect 1 while firmware returns %d)\n",
387                         wdev->hw_caps.api_version_major);
388                 err = -ENOTSUPP;
389                 goto err1;
390         }
391
392         err = wfx_sl_init(wdev);
393         if (err && wdev->hw_caps.capabilities.link_mode == SEC_LINK_ENFORCED) {
394                 dev_err(wdev->dev,
395                         "chip require secure_link, but can't negociate it\n");
396                 goto err1;
397         }
398
399         if (wdev->hw_caps.regul_sel_mode_info.region_sel_mode) {
400                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |= IEEE80211_CHAN_NO_IR;
401                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |= IEEE80211_CHAN_NO_IR;
402                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |= IEEE80211_CHAN_DISABLED;
403         }
404
405         dev_dbg(wdev->dev, "sending configuration file %s\n",
406                 wdev->pdata.file_pds);
407         err = wfx_send_pdata_pds(wdev);
408         if (err < 0)
409                 goto err1;
410
411         wdev->pdata.gpio_wakeup = gpio_saved;
412         if (wdev->pdata.gpio_wakeup) {
413                 dev_dbg(wdev->dev,
414                         "enable 'quiescent' power mode with gpio %d and PDS file %s\n",
415                         desc_to_gpio(wdev->pdata.gpio_wakeup),
416                         wdev->pdata.file_pds);
417                 gpiod_set_value(wdev->pdata.gpio_wakeup, 1);
418                 control_reg_write(wdev, 0);
419                 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
420         } else {
421                 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
422         }
423
424         hif_use_multi_tx_conf(wdev, true);
425
426         for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
427                 eth_zero_addr(wdev->addresses[i].addr);
428                 macaddr = of_get_mac_address(wdev->dev->of_node);
429                 if (!IS_ERR_OR_NULL(macaddr)) {
430                         ether_addr_copy(wdev->addresses[i].addr, macaddr);
431                         wdev->addresses[i].addr[ETH_ALEN - 1] += i;
432                 } else {
433                         ether_addr_copy(wdev->addresses[i].addr,
434                                         wdev->hw_caps.mac_addr[i]);
435                 }
436                 if (!is_valid_ether_addr(wdev->addresses[i].addr)) {
437                         dev_warn(wdev->dev, "using random MAC address\n");
438                         eth_random_addr(wdev->addresses[i].addr);
439                 }
440                 dev_info(wdev->dev, "MAC address %d: %pM\n", i,
441                          wdev->addresses[i].addr);
442         }
443         wdev->hw->wiphy->n_addresses = ARRAY_SIZE(wdev->addresses);
444         wdev->hw->wiphy->addresses = wdev->addresses;
445
446         err = ieee80211_register_hw(wdev->hw);
447         if (err)
448                 goto err1;
449
450         err = wfx_debug_init(wdev);
451         if (err)
452                 goto err2;
453
454         return 0;
455
456 err2:
457         ieee80211_unregister_hw(wdev->hw);
458         ieee80211_free_hw(wdev->hw);
459 err1:
460         wfx_bh_unregister(wdev);
461         return err;
462 }
463
464 void wfx_release(struct wfx_dev *wdev)
465 {
466         ieee80211_unregister_hw(wdev->hw);
467         hif_shutdown(wdev);
468         wfx_bh_unregister(wdev);
469         wfx_sl_deinit(wdev);
470 }
471
472 static int __init wfx_core_init(void)
473 {
474         int ret = 0;
475
476         if (IS_ENABLED(CONFIG_SPI))
477                 ret = spi_register_driver(&wfx_spi_driver);
478         if (IS_ENABLED(CONFIG_MMC) && !ret)
479                 ret = sdio_register_driver(&wfx_sdio_driver);
480         return ret;
481 }
482 module_init(wfx_core_init);
483
484 static void __exit wfx_core_exit(void)
485 {
486         if (IS_ENABLED(CONFIG_MMC))
487                 sdio_unregister_driver(&wfx_sdio_driver);
488         if (IS_ENABLED(CONFIG_SPI))
489                 spi_unregister_driver(&wfx_spi_driver);
490 }
491 module_exit(wfx_core_exit);