Merge tag 'modules-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof...
[platform/kernel/linux-rpi.git] / drivers / net / wireless / silabs / wfx / main.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Device probe and register.
4  *
5  * Copyright (c) 2017-2020, 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/consumer.h>
17 #include <linux/mmc/sdio_func.h>
18 #include <linux/spi/spi.h>
19 #include <linux/etherdevice.h>
20 #include <linux/firmware.h>
21
22 #include "main.h"
23 #include "wfx.h"
24 #include "fwio.h"
25 #include "hwio.h"
26 #include "bus.h"
27 #include "bh.h"
28 #include "sta.h"
29 #include "key.h"
30 #include "scan.h"
31 #include "debug.h"
32 #include "data_tx.h"
33 #include "hif_tx_mib.h"
34 #include "hif_api_cmd.h"
35
36 #define WFX_PDS_TLV_TYPE 0x4450 // "PD" (Platform Data) in ascii little-endian
37 #define WFX_PDS_MAX_CHUNK_SIZE 1500
38
39 MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WF200");
40 MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>");
41 MODULE_LICENSE("GPL");
42
43 #define RATETAB_ENT(_rate, _rateid, _flags) { \
44         .bitrate  = (_rate),   \
45         .hw_value = (_rateid), \
46         .flags    = (_flags),  \
47 }
48
49 static struct ieee80211_rate wfx_rates[] = {
50         RATETAB_ENT(10,  0,  0),
51         RATETAB_ENT(20,  1,  IEEE80211_RATE_SHORT_PREAMBLE),
52         RATETAB_ENT(55,  2,  IEEE80211_RATE_SHORT_PREAMBLE),
53         RATETAB_ENT(110, 3,  IEEE80211_RATE_SHORT_PREAMBLE),
54         RATETAB_ENT(60,  6,  0),
55         RATETAB_ENT(90,  7,  0),
56         RATETAB_ENT(120, 8,  0),
57         RATETAB_ENT(180, 9,  0),
58         RATETAB_ENT(240, 10, 0),
59         RATETAB_ENT(360, 11, 0),
60         RATETAB_ENT(480, 12, 0),
61         RATETAB_ENT(540, 13, 0),
62 };
63
64 #define CHAN2G(_channel, _freq, _flags) { \
65         .band = NL80211_BAND_2GHZ, \
66         .center_freq = (_freq),    \
67         .hw_value = (_channel),    \
68         .flags = (_flags),         \
69         .max_antenna_gain = 0,     \
70         .max_power = 30,           \
71 }
72
73 static struct ieee80211_channel wfx_2ghz_chantable[] = {
74         CHAN2G(1,  2412, 0),
75         CHAN2G(2,  2417, 0),
76         CHAN2G(3,  2422, 0),
77         CHAN2G(4,  2427, 0),
78         CHAN2G(5,  2432, 0),
79         CHAN2G(6,  2437, 0),
80         CHAN2G(7,  2442, 0),
81         CHAN2G(8,  2447, 0),
82         CHAN2G(9,  2452, 0),
83         CHAN2G(10, 2457, 0),
84         CHAN2G(11, 2462, 0),
85         CHAN2G(12, 2467, 0),
86         CHAN2G(13, 2472, 0),
87         CHAN2G(14, 2484, 0),
88 };
89
90 static const struct ieee80211_supported_band wfx_band_2ghz = {
91         .channels = wfx_2ghz_chantable,
92         .n_channels = ARRAY_SIZE(wfx_2ghz_chantable),
93         .bitrates = wfx_rates,
94         .n_bitrates = ARRAY_SIZE(wfx_rates),
95         .ht_cap = {
96                 /* Receive caps */
97                 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
98                        IEEE80211_HT_CAP_MAX_AMSDU | (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT),
99                 .ht_supported = 1,
100                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K,
101                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE,
102                 .mcs = {
103                         .rx_mask = { 0xFF }, /* MCS0 to MCS7 */
104                         .rx_highest = cpu_to_le16(72),
105                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
106                 },
107         },
108 };
109
110 static const struct ieee80211_iface_limit wdev_iface_limits[] = {
111         { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
112         { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
113 };
114
115 static const struct ieee80211_iface_combination wfx_iface_combinations[] = {
116         {
117                 .num_different_channels = 2,
118                 .max_interfaces = 2,
119                 .limits = wdev_iface_limits,
120                 .n_limits = ARRAY_SIZE(wdev_iface_limits),
121         }
122 };
123
124 static const struct ieee80211_ops wfx_ops = {
125         .start                   = wfx_start,
126         .stop                    = wfx_stop,
127         .add_interface           = wfx_add_interface,
128         .remove_interface        = wfx_remove_interface,
129         .config                  = wfx_config,
130         .tx                      = wfx_tx,
131         .join_ibss               = wfx_join_ibss,
132         .leave_ibss              = wfx_leave_ibss,
133         .conf_tx                 = wfx_conf_tx,
134         .hw_scan                 = wfx_hw_scan,
135         .cancel_hw_scan          = wfx_cancel_hw_scan,
136         .start_ap                = wfx_start_ap,
137         .stop_ap                 = wfx_stop_ap,
138         .sta_add                 = wfx_sta_add,
139         .sta_remove              = wfx_sta_remove,
140         .set_tim                 = wfx_set_tim,
141         .set_key                 = wfx_set_key,
142         .set_rts_threshold       = wfx_set_rts_threshold,
143         .set_default_unicast_key = wfx_set_default_unicast_key,
144         .bss_info_changed        = wfx_bss_info_changed,
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 /* The device needs data about the antenna configuration. This information in provided by PDS
167  * (Platform Data Set, this is the wording used in WF200 documentation) files. For hardware
168  * integrators, the full process to create PDS files is described here:
169  *   https://github.com/SiliconLabs/wfx-firmware/blob/master/PDS/README.md
170  *
171  * The PDS file is an array of Time-Length-Value structs.
172  */
173  int wfx_send_pds(struct wfx_dev *wdev, u8 *buf, size_t len)
174 {
175         int ret, chunk_type, chunk_len, chunk_num = 0;
176
177         if (*buf == '{') {
178                 dev_err(wdev->dev, "PDS: malformed file (legacy format?)\n");
179                 return -EINVAL;
180         }
181         while (len > 0) {
182                 chunk_type = get_unaligned_le16(buf + 0);
183                 chunk_len = get_unaligned_le16(buf + 2);
184                 if (chunk_len > len) {
185                         dev_err(wdev->dev, "PDS:%d: corrupted file\n", chunk_num);
186                         return -EINVAL;
187                 }
188                 if (chunk_type != WFX_PDS_TLV_TYPE) {
189                         dev_info(wdev->dev, "PDS:%d: skip unknown data\n", chunk_num);
190                         goto next;
191                 }
192                 if (chunk_len > WFX_PDS_MAX_CHUNK_SIZE)
193                         dev_warn(wdev->dev, "PDS:%d: unexpectedly large chunk\n", chunk_num);
194                 if (buf[4] != '{' || buf[chunk_len - 1] != '}')
195                         dev_warn(wdev->dev, "PDS:%d: unexpected content\n", chunk_num);
196
197                 ret = wfx_hif_configuration(wdev, buf + 4, chunk_len - 4);
198                 if (ret > 0) {
199                         dev_err(wdev->dev, "PDS:%d: invalid data (unsupported options?)\n", chunk_num);
200                         return -EINVAL;
201                 }
202                 if (ret == -ETIMEDOUT) {
203                         dev_err(wdev->dev, "PDS:%d: chip didn't reply (corrupted file?)\n", chunk_num);
204                         return ret;
205                 }
206                 if (ret) {
207                         dev_err(wdev->dev, "PDS:%d: chip returned an unknown error\n", chunk_num);
208                         return -EIO;
209                 }
210 next:
211                 chunk_num++;
212                 len -= chunk_len;
213                 buf += chunk_len;
214         }
215         return 0;
216 }
217
218 static int wfx_send_pdata_pds(struct wfx_dev *wdev)
219 {
220         int ret = 0;
221         const struct firmware *pds;
222         u8 *tmp_buf;
223
224         ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
225         if (ret) {
226                 dev_err(wdev->dev, "can't load antenna parameters (PDS file %s). The device may be unstable.\n",
227                         wdev->pdata.file_pds);
228                 return ret;
229         }
230         tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
231         if (!tmp_buf) {
232                 ret = -ENOMEM;
233                 goto release_fw;
234         }
235         ret = wfx_send_pds(wdev, tmp_buf, pds->size);
236         kfree(tmp_buf);
237 release_fw:
238         release_firmware(pds);
239         return ret;
240 }
241
242 static void wfx_free_common(void *data)
243 {
244         struct wfx_dev *wdev = data;
245
246         mutex_destroy(&wdev->tx_power_loop_info_lock);
247         mutex_destroy(&wdev->rx_stats_lock);
248         mutex_destroy(&wdev->conf_mutex);
249         ieee80211_free_hw(wdev->hw);
250 }
251
252 struct wfx_dev *wfx_init_common(struct device *dev, const struct wfx_platform_data *pdata,
253                                 const struct wfx_hwbus_ops *hwbus_ops, void *hwbus_priv)
254 {
255         struct ieee80211_hw *hw;
256         struct wfx_dev *wdev;
257
258         hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
259         if (!hw)
260                 return NULL;
261
262         SET_IEEE80211_DEV(hw, dev);
263
264         ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
265         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
266         ieee80211_hw_set(hw, CONNECTION_MONITOR);
267         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
268         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
269         ieee80211_hw_set(hw, SIGNAL_DBM);
270         ieee80211_hw_set(hw, SUPPORTS_PS);
271         ieee80211_hw_set(hw, MFP_CAPABLE);
272
273         hw->vif_data_size = sizeof(struct wfx_vif);
274         hw->sta_data_size = sizeof(struct wfx_sta_priv);
275         hw->queues = 4;
276         hw->max_rates = 8;
277         hw->max_rate_tries = 8;
278         hw->extra_tx_headroom = sizeof(struct wfx_hif_msg) + sizeof(struct wfx_hif_req_tx) +
279                                 4 /* alignment */ + 8 /* TKIP IV */;
280         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
281                                      BIT(NL80211_IFTYPE_ADHOC) |
282                                      BIT(NL80211_IFTYPE_AP);
283         hw->wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
284                                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
285                                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P |
286                                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U;
287         hw->wiphy->features |= NL80211_FEATURE_AP_SCAN;
288         hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
289         hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
290         hw->wiphy->max_ap_assoc_sta = HIF_LINK_ID_MAX;
291         hw->wiphy->max_scan_ssids = 2;
292         hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
293         hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
294         hw->wiphy->iface_combinations = wfx_iface_combinations;
295         hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmalloc(dev, sizeof(wfx_band_2ghz), GFP_KERNEL);
296         if (!hw->wiphy->bands[NL80211_BAND_2GHZ])
297                 goto err;
298
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, "silabs,antenna-config-file", &wdev->pdata.file_pds);
309         wdev->pdata.gpio_wakeup = devm_gpiod_get_optional(dev, "wakeup", GPIOD_OUT_LOW);
310         if (IS_ERR(wdev->pdata.gpio_wakeup))
311                 goto err;
312
313         if (wdev->pdata.gpio_wakeup)
314                 gpiod_set_consumer_name(wdev->pdata.gpio_wakeup, "wfx wakeup");
315
316         mutex_init(&wdev->conf_mutex);
317         mutex_init(&wdev->rx_stats_lock);
318         mutex_init(&wdev->tx_power_loop_info_lock);
319         init_completion(&wdev->firmware_ready);
320         INIT_DELAYED_WORK(&wdev->cooling_timeout_work, wfx_cooling_timeout_work);
321         skb_queue_head_init(&wdev->tx_pending);
322         init_waitqueue_head(&wdev->tx_dequeue);
323         wfx_init_hif_cmd(&wdev->hif_cmd);
324
325         if (devm_add_action_or_reset(dev, wfx_free_common, wdev))
326                 return NULL;
327
328         return wdev;
329
330 err:
331         ieee80211_free_hw(hw);
332         return NULL;
333 }
334
335 int wfx_probe(struct wfx_dev *wdev)
336 {
337         int i;
338         int err;
339         struct gpio_desc *gpio_saved;
340
341         /* During first part of boot, gpio_wakeup cannot yet been used. So prevent bh() to touch
342          * it.
343          */
344         gpio_saved = wdev->pdata.gpio_wakeup;
345         wdev->pdata.gpio_wakeup = NULL;
346         wdev->poll_irq = true;
347
348         wdev->bh_wq = alloc_workqueue("wfx_bh_wq", WQ_HIGHPRI, 0);
349         if (!wdev->bh_wq)
350                 return -ENOMEM;
351
352         wfx_bh_register(wdev);
353
354         err = wfx_init_device(wdev);
355         if (err)
356                 goto bh_unregister;
357
358         wfx_bh_poll_irq(wdev);
359         err = wait_for_completion_timeout(&wdev->firmware_ready, 1 * HZ);
360         if (err <= 0) {
361                 if (err == 0) {
362                         dev_err(wdev->dev, "timeout while waiting for startup indication\n");
363                         err = -ETIMEDOUT;
364                 } else if (err == -ERESTARTSYS) {
365                         dev_info(wdev->dev, "probe interrupted by user\n");
366                 }
367                 goto bh_unregister;
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, wdev->hw_caps.api_version_minor,
375                  wdev->keyset, wdev->hw_caps.link_mode);
376         snprintf(wdev->hw->wiphy->fw_version,
377                  sizeof(wdev->hw->wiphy->fw_version),
378                  "%d.%d.%d",
379                  wdev->hw_caps.firmware_major,
380                  wdev->hw_caps.firmware_minor,
381                  wdev->hw_caps.firmware_build);
382
383         if (wfx_api_older_than(wdev, 1, 0)) {
384                 dev_err(wdev->dev, "unsupported firmware API version (expect 1 while firmware returns %d)\n",
385                         wdev->hw_caps.api_version_major);
386                 err = -EOPNOTSUPP;
387                 goto bh_unregister;
388         }
389
390         if (wdev->hw_caps.link_mode == SEC_LINK_ENFORCED) {
391                 dev_err(wdev->dev, "chip require secure_link, but can't negotiate it\n");
392                 goto bh_unregister;
393         }
394
395         if (wdev->hw_caps.region_sel_mode) {
396                 wdev->hw->wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS;
397                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |=
398                         IEEE80211_CHAN_NO_IR;
399                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |=
400                         IEEE80211_CHAN_NO_IR;
401                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |=
402                         IEEE80211_CHAN_DISABLED;
403         }
404
405         dev_dbg(wdev->dev, "sending configuration file %s\n", wdev->pdata.file_pds);
406         err = wfx_send_pdata_pds(wdev);
407         if (err < 0 && err != -ENOENT)
408                 goto bh_unregister;
409
410         wdev->poll_irq = false;
411         err = wdev->hwbus_ops->irq_subscribe(wdev->hwbus_priv);
412         if (err)
413                 goto bh_unregister;
414
415         err = wfx_hif_use_multi_tx_conf(wdev, true);
416         if (err)
417                 dev_err(wdev->dev, "misconfigured IRQ?\n");
418
419         wdev->pdata.gpio_wakeup = gpio_saved;
420         if (wdev->pdata.gpio_wakeup) {
421                 dev_dbg(wdev->dev, "enable 'quiescent' power mode with wakeup GPIO and PDS file %s\n",
422                         wdev->pdata.file_pds);
423                 gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
424                 wfx_control_reg_write(wdev, 0);
425                 wfx_hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
426         } else {
427                 wfx_hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
428         }
429
430         for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
431                 eth_zero_addr(wdev->addresses[i].addr);
432                 err = of_get_mac_address(wdev->dev->of_node, wdev->addresses[i].addr);
433                 if (!err)
434                         wdev->addresses[i].addr[ETH_ALEN - 1] += i;
435                 else
436                         ether_addr_copy(wdev->addresses[i].addr, wdev->hw_caps.mac_addr[i]);
437                 if (!is_valid_ether_addr(wdev->addresses[i].addr)) {
438                         dev_warn(wdev->dev, "using random MAC address\n");
439                         eth_random_addr(wdev->addresses[i].addr);
440                 }
441                 dev_info(wdev->dev, "MAC address %d: %pM\n", i, wdev->addresses[i].addr);
442         }
443         wdev->hw->wiphy->n_addresses = ARRAY_SIZE(wdev->addresses);
444         wdev->hw->wiphy->addresses = wdev->addresses;
445
446         if (!wfx_api_older_than(wdev, 3, 8))
447                 wdev->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
448
449         err = ieee80211_register_hw(wdev->hw);
450         if (err)
451                 goto irq_unsubscribe;
452
453         err = wfx_debug_init(wdev);
454         if (err)
455                 goto ieee80211_unregister;
456
457         return 0;
458
459 ieee80211_unregister:
460         ieee80211_unregister_hw(wdev->hw);
461 irq_unsubscribe:
462         wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
463 bh_unregister:
464         wfx_bh_unregister(wdev);
465         destroy_workqueue(wdev->bh_wq);
466         return err;
467 }
468
469 void wfx_release(struct wfx_dev *wdev)
470 {
471         ieee80211_unregister_hw(wdev->hw);
472         wfx_hif_shutdown(wdev);
473         wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
474         wfx_bh_unregister(wdev);
475         destroy_workqueue(wdev->bh_wq);
476 }
477
478 static int __init wfx_core_init(void)
479 {
480         int ret = 0;
481
482         if (IS_ENABLED(CONFIG_SPI))
483                 ret = spi_register_driver(&wfx_spi_driver);
484         if (IS_ENABLED(CONFIG_MMC) && !ret)
485                 ret = sdio_register_driver(&wfx_sdio_driver);
486         return ret;
487 }
488 module_init(wfx_core_init);
489
490 static void __exit wfx_core_exit(void)
491 {
492         if (IS_ENABLED(CONFIG_MMC))
493                 sdio_unregister_driver(&wfx_sdio_driver);
494         if (IS_ENABLED(CONFIG_SPI))
495                 spi_unregister_driver(&wfx_spi_driver);
496 }
497 module_exit(wfx_core_exit);