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