]> Git Repo - esp-hosted.git/blame - esp_hosted_ng/host/main.c
Merge branch 'bugfix/raw_tp_reboot_crash' into 'master'
[esp-hosted.git] / esp_hosted_ng / host / main.c
CommitLineData
e8b168e8 1// SPDX-License-Identifier: GPL-2.0-only
774e9b2e
MM
2/*
3 * Espressif Systems Wireless LAN device driver
4 *
d7215282 5 * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
774e9b2e 6 *
774e9b2e 7 */
057d3956 8#include "utils.h"
774e9b2e
MM
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/gpio.h>
7406fe82 13#include <linux/igmp.h>
774e9b2e
MM
14
15#include "esp.h"
16#include "esp_if.h"
17#include "esp_bt_api.h"
18#include "esp_api.h"
19#include "esp_cmd.h"
aad2ae2e 20#include "esp_kernel_port.h"
774e9b2e
MM
21
22#include "esp_cfg80211.h"
bf3d6cb6 23#include "esp_stats.h"
774e9b2e 24
7e6de249 25#define RELEASE_VERSION "1.0.3"
774e9b2e 26#define HOST_GPIO_PIN_INVALID -1
50785e92 27#define CONFIG_ALLOW_MULTICAST_WAKEUP 1
774e9b2e 28static int resetpin = HOST_GPIO_PIN_INVALID;
2b40207b 29static u32 clockspeed = 0;
774e9b2e 30extern u8 ap_bssid[MAC_ADDR_LEN];
3fef9acf 31extern volatile u8 host_sleep;
38a59879 32u32 raw_tp_mode = 0;
e2766bb0 33int log_level = ESP_INFO;
774e9b2e
MM
34
35module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
36MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device");
37
2b40207b
JT
38module_param(clockspeed, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
39MODULE_PARM_DESC(clockspeed, "Hosts clock speed in MHz");
40
38a59879
SS
41module_param(raw_tp_mode, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
42MODULE_PARM_DESC(raw_tp_mode, "Mode choosed to test raw throughput");
43
774e9b2e
MM
44static void deinit_adapter(void);
45
46
7406fe82 47struct multicast_list mcast_list = {0};
774e9b2e
MM
48struct esp_adapter adapter;
49/*struct esp_device esp_dev;*/
50
a3829bf3 51struct esp_adapter *esp_get_adapter(void)
774e9b2e
MM
52{
53 return &adapter;
54}
55
56void esp_process_new_packet_intr(struct esp_adapter *adapter)
57{
a3829bf3 58 if (adapter)
774e9b2e
MM
59 queue_work(adapter->if_rx_workqueue, &adapter->if_rx_work);
60}
61
a3829bf3 62static int process_tx_packet(struct sk_buff *skb)
774e9b2e
MM
63{
64 struct esp_wifi_device *priv = NULL;
65 struct esp_skb_cb *cb = NULL;
66 struct esp_payload_header *payload_header = NULL;
67 struct sk_buff *new_skb = NULL;
68 int ret = 0;
69 u8 pad_len = 0, realloc_skb = 0;
70 u16 len = 0;
71 u16 total_len = 0;
a3829bf3 72 static u8 c;
774e9b2e
MM
73 u8 *pos = NULL;
74
75 c++;
76 /* Get the priv */
77 cb = (struct esp_skb_cb *) skb->cb;
78 priv = cb->priv;
79
80 if (!priv) {
81 dev_kfree_skb(skb);
6f8fee57 82 esp_info("No priv\n");
774e9b2e
MM
83 return NETDEV_TX_OK;
84 }
85
86 if (netif_queue_stopped((const struct net_device *) priv->ndev)) {
6f8fee57 87 esp_info("Netif queue stopped\n");
774e9b2e
MM
88 return NETDEV_TX_BUSY;
89 }
90
3fef9acf
MM
91 if (host_sleep) {
92 return NETDEV_TX_BUSY;
93 }
94
774e9b2e
MM
95 len = skb->len;
96
97 /* Create space for payload header */
98 pad_len = sizeof(struct esp_payload_header);
99
100 total_len = len + pad_len;
101
102 /* Align buffer length */
103 pad_len += SKB_DATA_ADDR_ALIGNMENT - (total_len % SKB_DATA_ADDR_ALIGNMENT);
104
105 if (skb_headroom(skb) < pad_len) {
106 /* Headroom is not sufficient */
107 realloc_skb = 1;
108 }
109
110 if (realloc_skb || !IS_ALIGNED((unsigned long) skb->data, SKB_DATA_ADDR_ALIGNMENT)) {
111 /* Realloc SKB */
112 if (skb_linearize(skb)) {
113 priv->stats.tx_errors++;
114 dev_kfree_skb(skb);
6f8fee57 115 esp_err("Failed to linearize SKB");
774e9b2e
MM
116 return NETDEV_TX_OK;
117 }
118
119 new_skb = esp_alloc_skb(skb->len + pad_len);
120
121 if (!new_skb) {
6f8fee57 122 esp_err("Failed to allocate SKB");
774e9b2e
MM
123 priv->stats.tx_errors++;
124 dev_kfree_skb(skb);
125 return NETDEV_TX_OK;
126 }
127
128 pos = new_skb->data;
129 pos += pad_len;
130
131 /* Populate new SKB */
132 skb_copy_from_linear_data(skb, pos, skb->len);
133 skb_put(new_skb, skb->len + pad_len);
134
135 /* Replace old SKB */
136 dev_kfree_skb_any(skb);
137 skb = new_skb;
138 } else {
139 /* Realloc is not needed, Make space for interface header */
140 skb_push(skb, pad_len);
141 }
142
143 /* Set payload header */
144 payload_header = (struct esp_payload_header *) skb->data;
145 memset(payload_header, 0, pad_len);
146
147 payload_header->if_type = priv->if_type;
148 payload_header->if_num = priv->if_num;
149 payload_header->len = cpu_to_le16(len);
150 payload_header->offset = cpu_to_le16(pad_len);
151 payload_header->packet_type = PACKET_TYPE_DATA;
152
bf3d6cb6
SR
153 if (adapter.capabilities & ESP_CHECKSUM_ENABLED)
154 payload_header->checksum = cpu_to_le16(compute_checksum(skb->data, (len + pad_len)));
774e9b2e
MM
155
156 if (!priv->stop_data) {
157 ret = esp_send_packet(priv->adapter, skb);
158
159 if (ret) {
c19cde88 160 esp_verbose("Failed to send SKB");
774e9b2e
MM
161 priv->stats.tx_errors++;
162 } else {
163 priv->stats.tx_packets++;
164 priv->stats.tx_bytes += skb->len;
165 }
166 } else {
167 dev_kfree_skb_any(skb);
168 priv->stats.tx_dropped++;
169 }
170
171 return 0;
172}
173
a3829bf3 174void esp_port_open(struct esp_wifi_device *priv)
774e9b2e
MM
175{
176 priv->port_open = 1;
177 priv->stop_data = 0;
178}
179
a3829bf3 180void esp_port_close(struct esp_wifi_device *priv)
774e9b2e
MM
181{
182 if (!priv)
183 return;
184
185 priv->port_open = 0;
186 priv->stop_data = 1;
187}
188
189void print_capabilities(u32 cap)
190{
6f8fee57 191 esp_info("Capabilities: 0x%x. Features supported are:\n", cap);
774e9b2e 192 if (cap & ESP_WLAN_SDIO_SUPPORT)
6f8fee57 193 esp_info("\t * WLAN on SDIO\n");
774e9b2e 194 else if (cap & ESP_WLAN_SPI_SUPPORT)
6f8fee57 195 esp_info("\t * WLAN on SPI\n");
774e9b2e
MM
196
197 if ((cap & ESP_BT_UART_SUPPORT) ||
bf3d6cb6
SR
198 (cap & ESP_BT_SDIO_SUPPORT) ||
199 (cap & ESP_BT_SPI_SUPPORT)) {
6f8fee57 200 esp_info("\t * BT/BLE\n");
774e9b2e 201 if (cap & ESP_BT_UART_SUPPORT)
6f8fee57 202 esp_info("\t - HCI over UART\n");
774e9b2e 203 if (cap & ESP_BT_SDIO_SUPPORT)
6f8fee57 204 esp_info("\t - HCI over SDIO\n");
774e9b2e 205 if (cap & ESP_BT_SPI_SUPPORT)
6f8fee57 206 esp_info("\t - HCI over SPI\n");
774e9b2e
MM
207
208 if ((cap & ESP_BLE_ONLY_SUPPORT) && (cap & ESP_BR_EDR_ONLY_SUPPORT))
6f8fee57 209 esp_info("\t - BT/BLE dual mode\n");
774e9b2e 210 else if (cap & ESP_BLE_ONLY_SUPPORT)
6f8fee57 211 esp_info("\t - BLE only\n");
774e9b2e 212 else if (cap & ESP_BR_EDR_ONLY_SUPPORT)
6f8fee57 213 esp_info("\t - BR EDR only\n");
774e9b2e
MM
214 }
215}
216
58c0e50b 217void init_bt(struct esp_adapter *adapter)
774e9b2e 218{
774e9b2e
MM
219
220 if ((adapter->capabilities & ESP_BT_SPI_SUPPORT) ||
221 (adapter->capabilities & ESP_BT_SDIO_SUPPORT)) {
222 msleep(200);
6f8fee57 223 esp_info("ESP Bluetooth init\n");
774e9b2e
MM
224 esp_init_bt(adapter);
225 }
226}
227
228static int check_esp_version(struct fw_version *ver)
229{
58c0e50b 230 esp_info("ESP Firmware version: %u.%u.%u\n",
774e9b2e
MM
231 ver->major1, ver->major2, ver->minor);
232 if (!ver->major1) {
6f8fee57 233 esp_err("Incompatible ESP firmware release detected, Please use correct ESP-Hosted branch/compatible release\n");
774e9b2e
MM
234 return -1;
235 }
236 return 0;
237}
238
239static void print_reset_reason(uint32_t reason)
240{
241 switch (reason)
242 {
a3829bf3
KG
243 case 1: esp_info("POWERON_RESET\n"); break; /**<1, Vbat power on reset*/
244 case 3: esp_info("SW_RESET\n"); break; /**<3, Software reset digital core*/
245 case 4: esp_info("OWDT_RESET\n"); break; /**<4, Legacy watch dog reset digital core*/
246 case 5: esp_info("DEEPSLEEP_RESET\n"); break; /**<5, Deep Sleep reset digital core*/
247 case 6: esp_info("SDIO_RESET\n"); break; /**<6, Reset by SLC module, reset digital core*/
248 case 7: esp_info("TG0WDT_SYS_RESET\n"); break; /**<7, Timer Group0 Watch dog reset digital core*/
249 case 8: esp_info("TG1WDT_SYS_RESET\n"); break; /**<8, Timer Group1 Watch dog reset digital core*/
250 case 9: esp_info("RTCWDT_SYS_RESET\n"); break; /**<9, RTC Watch dog Reset digital core*/
251 case 10: esp_info("INTRUSION_RESET\n"); break; /**<10, Instrusion tested to reset CPU*/
252 case 11: esp_info("TGWDT_CPU_RESET\n"); break; /**<11, Time Group reset CPU*/
253 case 12: esp_info("SW_CPU_RESET\n"); break; /**<12, Software reset CPU*/
254 case 13: esp_info("RTCWDT_CPU_RESET\n"); break; /**<13, RTC Watch dog Reset CPU*/
255 case 14: esp_info("EXT_CPU_RESET\n"); break; /**<14, for APP CPU, reseted by PRO CPU*/
256 case 15: esp_info("RTCWDT_BROWN_OUT_RESET\n"); break;/**<15, Reset when the vdd voltage is not stable*/
257 case 16: esp_info("RTCWDT_RTC_RESET\n"); break; /**<16, RTC Watch dog reset digital core and rtc module*/
258 default: esp_info("Unknown[%u]\n", reason); break;
774e9b2e
MM
259 }
260}
261
58c0e50b 262static int process_fw_data(struct fw_data *fw_p, int tag_len)
774e9b2e 263{
58c0e50b
KG
264 if (tag_len != sizeof(struct fw_data)) {
265 esp_err("Length not matching to firmware data size\n");
774e9b2e
MM
266 return -1;
267 }
268
e58fd496 269 esp_info("ESP chipset's last reset cause:\n");
774e9b2e 270 print_reset_reason(le32_to_cpu(fw_p->last_reset_reason));
58c0e50b 271
774e9b2e
MM
272 return check_esp_version(&fw_p->version);
273}
274
58c0e50b
KG
275int process_event_esp_bootup(struct esp_adapter *adapter, u8 *evt_buf, u8 len)
276{
277 int len_left = len, tag_len, ret = 0;
278 u8 *pos;
279
280 if (!adapter || !evt_buf)
281 return -1;
282
283 if (len_left >= 64) {
284 esp_info("ESP init event len looks unexpected: %u (>=64)\n", len_left);
285 esp_info("You probably facing timing mismatch at transport layer\n");
286 }
287
288 clear_bit(ESP_INIT_DONE, &adapter->state_flags);
289 /* Deinit module if already initialized */
48e29210 290 test_raw_tp_cleanup();
58c0e50b
KG
291 esp_deinit_module(adapter);
292
293 pos = evt_buf;
294
295 while (len_left > 0) {
296 tag_len = *(pos + 1);
297
298 esp_info("Bootup Event tag: %d\n", *pos);
299
300 switch (*pos) {
301 case ESP_BOOTUP_CAPABILITY:
302 adapter->capabilities = *(pos + 2);
303 break;
304 case ESP_BOOTUP_FIRMWARE_CHIP_ID:
305 ret = esp_validate_chipset(adapter, *(pos + 2));
306 break;
307 case ESP_BOOTUP_FW_DATA:
308 ret = process_fw_data((struct fw_data *)(pos + 2), tag_len);
309 break;
310 case ESP_BOOTUP_SPI_CLK_MHZ:
311 ret = esp_adjust_spi_clock(adapter, *(pos + 2));
312 break;
313 default:
314 esp_warn("Unsupported tag=%x in bootup event\n", *pos);
315 }
316
317 if (ret < 0) {
318 esp_err("failed to process tag=%x in bootup event\n", *pos);
319 return -1;
320 }
321 pos += (tag_len + 2);
322 len_left -= (tag_len + 2);
323 }
324
325 if (esp_add_card(adapter)) {
326 esp_err("network iterface init failed\n");
327 return -1;
328 }
329 init_bt(adapter);
38a59879
SS
330
331 if (raw_tp_mode !=0) {
332#if TEST_RAW_TP
333 process_test_capabilities(raw_tp_mode);
334 esp_init_raw_tp(adapter);
4ced2c85 335#else
38a59879
SS
336 esp_err("RAW TP mode selected but not enabled\n");
337 return -1;
338#endif
339 }
58c0e50b
KG
340 set_bit(ESP_INIT_DONE, &adapter->state_flags);
341 print_capabilities(adapter->capabilities);
342
343 return 0;
344}
345
774e9b2e
MM
346static int esp_open(struct net_device *ndev)
347{
774e9b2e
MM
348 return 0;
349}
350
351static int esp_stop(struct net_device *ndev)
352{
2127f3fa 353 return 0;
774e9b2e
MM
354}
355
a3829bf3 356static struct net_device_stats *esp_get_stats(struct net_device *ndev)
774e9b2e
MM
357{
358 struct esp_wifi_device *priv = netdev_priv(ndev);
359
360 if (!priv)
361 return NULL;
362
363 return &priv->stats;
364}
365
366static int esp_set_mac_address(struct net_device *ndev, void *data)
367{
368 struct esp_wifi_device *priv = netdev_priv(ndev);
98f8ece9
KG
369 struct sockaddr *sa = (struct sockaddr *)data;
370 int ret;
774e9b2e
MM
371
372 if (!priv || !priv->adapter)
373 return -EINVAL;
374
98f8ece9 375 esp_info("%u "MACSTR"\n", __LINE__, MAC2STR(sa->sa_data));
774e9b2e 376
98f8ece9
KG
377 ret = cmd_set_mac(priv, sa->sa_data);
378
379 if (ret == 0)
380 eth_hw_addr_set(ndev, priv->mac_address/*mac_addr->sa_data*/);
381
382 return ret;
774e9b2e
MM
383}
384
774e9b2e
MM
385static void esp_set_rx_mode(struct net_device *ndev)
386{
c19cde88 387 struct esp_adapter *adapter = esp_get_adapter();
7406fe82 388
c19cde88 389 schedule_work(&adapter->mac_flter_work);
774e9b2e
MM
390}
391
392static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
393{
394 struct esp_wifi_device *priv = NULL;
395 struct esp_skb_cb *cb = NULL;
396
397 if (!skb || !ndev)
398 return NETDEV_TX_OK;
399
400 priv = netdev_priv(ndev);
401 if (!priv) {
402 dev_kfree_skb(skb);
403 return NETDEV_TX_OK;
404 }
405
406 if (!priv->port_open) {
407 priv->stats.tx_dropped++;
c19cde88 408 esp_verbose("Port not yet open\n");
774e9b2e
MM
409 dev_kfree_skb(skb);
410 return NETDEV_TX_OK;
411 }
412
413 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
6f8fee57 414 esp_err("Bad len %d\n", skb->len);
774e9b2e
MM
415 priv->stats.tx_dropped++;
416 dev_kfree_skb(skb);
417 return NETDEV_TX_OK;
418 }
419
420 cb = (struct esp_skb_cb *) skb->cb;
421 cb->priv = priv;
422
423 return process_tx_packet(skb);
424}
425
426static const struct net_device_ops esp_netdev_ops = {
427 .ndo_open = esp_open,
428 .ndo_stop = esp_stop,
429 .ndo_start_xmit = esp_hard_start_xmit,
98f8ece9 430 .ndo_set_mac_address = esp_set_mac_address,
774e9b2e 431 .ndo_validate_addr = eth_validate_addr,
774e9b2e
MM
432 .ndo_get_stats = esp_get_stats,
433 .ndo_set_rx_mode = esp_set_rx_mode,
434};
435
436
437void esp_init_priv(struct net_device *ndev)
438{
439 ndev->netdev_ops = &esp_netdev_ops;
440 ndev->needed_headroom = roundup(sizeof(struct esp_payload_header) +
441 INTERFACE_HEADER_PADDING, 4);
442}
443
58c0e50b 444static int esp_add_network_ifaces(struct esp_adapter *adapter)
774e9b2e 445{
a3829bf3 446 struct wireless_dev *wdev = NULL;
774e9b2e
MM
447
448 if (!adapter) {
6f8fee57 449 esp_info("adapter not yet init\n");
774e9b2e
MM
450 return -EINVAL;
451 }
452
774e9b2e 453 rtnl_lock();
76085845 454 wdev = esp_cfg80211_add_iface(adapter->wiphy, "wlan%d", 1, NL80211_IFTYPE_STATION, NULL);
774e9b2e
MM
455 rtnl_unlock();
456
225e14eb
YM
457 /* Return success if network added successfully */
458 if (wdev)
459 return 0;
460
461 return -1;
774e9b2e
MM
462}
463
38a59879
SS
464int esp_init_raw_tp(struct esp_adapter *adapter)
465{
466 RET_ON_FAIL(cmd_init_raw_tp_task_timer(adapter->priv[ESP_STA_NW_IF]));
467 return 0;
468}
469
774e9b2e
MM
470int esp_add_card(struct esp_adapter *adapter)
471{
225e14eb 472 RET_ON_FAIL(esp_commands_setup(adapter));
58c0e50b
KG
473 RET_ON_FAIL(esp_add_wiphy(adapter));
474 RET_ON_FAIL(esp_add_network_ifaces(adapter));
76085845 475 clear_bit(ESP_CLEANUP_IN_PROGRESS, &adapter->state_flags);
774e9b2e 476
225e14eb 477 return 0;
774e9b2e
MM
478}
479
58c0e50b 480static int esp_remove_network_ifaces(struct esp_adapter *adapter)
774e9b2e
MM
481{
482 uint8_t iface_idx = 0;
483 struct net_device *ndev = NULL;
484 struct esp_wifi_device *priv = NULL;
485
a3829bf3 486 for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
774e9b2e
MM
487
488 priv = adapter->priv[iface_idx];
774e9b2e
MM
489 if (!priv)
490 continue;
774e9b2e
MM
491 if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags))
492 continue;
493
774e9b2e 494 ndev = priv->ndev;
2127f3fa 495#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
58c0e50b
KG
496 if (ndev)
497 ndev->needs_free_netdev = true;
2127f3fa 498 rtnl_lock();
58c0e50b
KG
499 wiphy_lock(adapter->wiphy);
500 cfg80211_unregister_wdev(&priv->wdev);
501 wiphy_unlock(adapter->wiphy);
2127f3fa
KG
502 rtnl_unlock();
503#else
504 if (ndev && ndev->reg_state == NETREG_REGISTERED) {
505 unregister_netdev(ndev);
506 free_netdev(ndev);
507 ndev = NULL;
508 }
509#endif
58c0e50b
KG
510 adapter->priv[iface_idx] = NULL;
511 }
774e9b2e 512
58c0e50b
KG
513 return 0;
514}
774e9b2e 515
58c0e50b
KG
516static int stop_network_iface(struct esp_wifi_device *priv)
517{
518 struct net_device *ndev;
774e9b2e 519
58c0e50b
KG
520 if (!priv)
521 return 0;
774e9b2e 522
58c0e50b
KG
523 if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags))
524 return 0;
525
2127f3fa 526 esp_mark_scan_done_and_disconnect(priv, false);
58c0e50b 527 esp_port_close(priv);
774e9b2e 528
58c0e50b
KG
529 /* stop and unregister network */
530 ndev = priv->ndev;
774e9b2e 531
58c0e50b
KG
532 if (ndev) {
533 netif_carrier_off(ndev);
534 netif_device_detach(ndev);
535
536 unregister_inetaddr_notifier(&(priv->nb));
774e9b2e 537 }
58c0e50b
KG
538
539 return 0;
774e9b2e
MM
540}
541
58c0e50b 542int esp_stop_network_ifaces(struct esp_adapter *adapter)
774e9b2e
MM
543{
544 uint8_t iface_idx = 0;
545
58c0e50b
KG
546 for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
547 stop_network_iface(adapter->priv[iface_idx]);
548 }
549
2127f3fa
KG
550 rtnl_lock();
551 if (adapter->wiphy)
552 cfg80211_shutdown_all_interfaces(adapter->wiphy);
553
554 rtnl_unlock();
555
58c0e50b
KG
556 return 0;
557}
558
559int esp_remove_card(struct esp_adapter *adapter)
560{
774e9b2e
MM
561 if (!adapter) {
562 return 0;
563 }
564
58c0e50b 565 esp_stop_network_ifaces(adapter);
76085845 566 esp_cfg_cleanup(adapter);
58c0e50b 567 /* BT may have been initialized after fw bootup event, deinit it */
225e14eb 568 esp_deinit_bt(adapter);
225e14eb 569 esp_commands_teardown(adapter);
58c0e50b
KG
570 esp_remove_network_ifaces(adapter);
571 esp_remove_wiphy(adapter);
774e9b2e
MM
572
573 return 0;
574}
575
a3829bf3 576struct esp_wifi_device *get_priv_from_payload_header(
774e9b2e
MM
577 struct esp_payload_header *header)
578{
579 struct esp_wifi_device *priv = NULL;
580 u8 i = 0;
581
582 if (!header)
583 return NULL;
584
585 for (i = 0; i < ESP_MAX_INTERFACE; i++) {
586 priv = adapter.priv[i];
587
588 if (!priv)
589 continue;
590
591 if (priv->if_type == header->if_type &&
592 priv->if_num == header->if_num) {
593 return priv;
594 }
595 }
596 return NULL;
597}
598
599static void process_esp_bootup_event(struct esp_adapter *adapter,
600 struct esp_internal_bootup_event *evt)
601{
602 if (!adapter || !evt) {
6f8fee57 603 esp_err("Invalid arguments\n");
774e9b2e
MM
604 return;
605 }
606
607 if (evt->header.status) {
6f8fee57 608 esp_err("Incorrect ESP bootup event\n");
774e9b2e
MM
609 return;
610 }
611
58c0e50b 612 esp_info("Received ESP bootup event\n");
774e9b2e
MM
613 process_event_esp_bootup(adapter, evt->data, evt->len);
614}
615
616static int process_internal_event(struct esp_adapter *adapter,
617 struct sk_buff *skb)
618{
619 struct event_header *header = NULL;
620
621 if (!skb || !adapter) {
58c0e50b 622 esp_err("Incorrect event data!\n");
774e9b2e
MM
623 return -1;
624 }
625
626 header = (struct event_header *) (skb->data);
627
628 switch (header->event_code) {
629
630 case ESP_INTERNAL_BOOTUP_EVENT:
631 process_esp_bootup_event(adapter,
632 (struct esp_internal_bootup_event *)(skb->data));
633 break;
634
635 default:
6f8fee57
KG
636 esp_info("%u unhandled internal event[%u]\n",
637 __LINE__, header->event_code);
774e9b2e
MM
638 break;
639 }
640
641 return 0;
642}
643
644static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
645{
646 struct esp_wifi_device *priv = NULL;
647 struct esp_payload_header *payload_header = NULL;
648 u16 len = 0, offset = 0;
bf3d6cb6 649 u16 rx_checksum = 0, checksum = 0;
774e9b2e
MM
650 struct hci_dev *hdev = adapter->hcidev;
651 u8 *type = NULL;
774e9b2e
MM
652
653 if (!skb)
654 return;
655
656 /* get the paload header */
657 payload_header = (struct esp_payload_header *) skb->data;
658
659 len = le16_to_cpu(payload_header->len);
660 offset = le16_to_cpu(payload_header->offset);
661
3fef9acf 662 if (payload_header->reserved2 == 0xFF) {
c19cde88 663 esp_hex_dump("Wake up packet: ", skb->data, len+offset);
3fef9acf 664 }
774e9b2e 665
bf3d6cb6
SR
666 if (adapter->capabilities & ESP_CHECKSUM_ENABLED) {
667 rx_checksum = le16_to_cpu(payload_header->checksum);
668 payload_header->checksum = 0;
669
670 checksum = compute_checksum(skb->data, (len + offset));
671
672 if (checksum != rx_checksum) {
673 dev_kfree_skb_any(skb);
674 return;
675 }
676 }
677
774e9b2e
MM
678 /* chop off the header from skb */
679 skb_pull(skb, offset);
680
681 if (payload_header->if_type == ESP_STA_IF || payload_header->if_type == ESP_AP_IF) {
682
683 /* retrieve priv based on payload header contents */
684 priv = get_priv_from_payload_header(payload_header);
685
686 if (!priv) {
6f8fee57 687 esp_err("Empty priv\n");
774e9b2e
MM
688 dev_kfree_skb_any(skb);
689 return;
690 }
691
692 if (payload_header->packet_type == PACKET_TYPE_EAPOL) {
76085845 693 esp_dbg("Rx PACKET_TYPE_EAPOL!!!!\n");
774e9b2e 694 esp_port_open(priv);
76085845
KG
695 skb->dev = priv->ndev;
696 skb->protocol = eth_type_trans(skb, priv->ndev);
697 netif_rx(skb);
774e9b2e
MM
698
699 } else if (payload_header->packet_type == PACKET_TYPE_DATA) {
700
701 skb->dev = priv->ndev;
702 skb->protocol = eth_type_trans(skb, priv->ndev);
703 skb->ip_summed = CHECKSUM_NONE;
704
705 priv->stats.rx_bytes += skb->len;
706 /* Forward skb to kernel */
1294f8ac 707 NETIF_RX_NI(skb);
774e9b2e
MM
708 priv->stats.rx_packets++;
709 } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) {
225e14eb 710 process_cmd_resp(priv->adapter, skb);
774e9b2e 711 } else if (payload_header->packet_type == PACKET_TYPE_EVENT) {
225e14eb 712 process_cmd_event(priv, skb);
774e9b2e
MM
713 dev_kfree_skb_any(skb);
714 }
715
716 } else if (payload_header->if_type == ESP_HCI_IF) {
717 if (hdev) {
718
719 type = skb->data;
720 hci_skb_pkt_type(skb) = *type;
721 skb_pull(skb, 1);
722
723#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
724 if (hci_recv_frame(hdev, skb)) {
725#else
726 if (hci_recv_frame(skb)) {
727#endif
728 hdev->stat.err_rx++;
729 } else {
730 esp_hci_update_rx_counter(hdev, *type, skb->len);
731 }
732 }
733 } else if (payload_header->if_type == ESP_INTERNAL_IF) {
734
735 /* Queue event skb for processing in events workqueue */
736 skb_queue_tail(&adapter->events_skb_q, skb);
737
738 if (adapter->events_wq)
739 queue_work(adapter->events_wq, &adapter->events_work);
740 else
741 dev_kfree_skb_any(skb);
742
bf3d6cb6 743 } else if (payload_header->if_type == ESP_TEST_IF) {
38a59879
SS
744#if TEST_RAW_TP
745 if (raw_tp_mode != 0) {
bf3d6cb6 746 update_test_raw_tp_rx_stats(len);
38a59879
SS
747 }
748#endif
bf3d6cb6 749 dev_kfree_skb_any(skb);
774e9b2e
MM
750 } else {
751 dev_kfree_skb_any(skb);
752 }
753}
754
76085845
KG
755char *esp_get_hardware_name(int hardware_id)
756{
757 if(hardware_id == ESP_FIRMWARE_CHIP_ESP32)
758 return "ESP32";
759 else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32S2)
760 return "ESP32S2";
761 else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C3)
762 return "ESP32C3";
763 else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32S3)
764 return "ESP32S3";
765 else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C2)
766 return "ESP32C2";
767 else if(hardware_id == ESP_FIRMWARE_CHIP_ESP32C6)
768 return "ESP32C6";
769 else
770 return "N/A";
771}
772
773bool esp_is_valid_hardware_id(int hardware_id)
774{
775 switch(hardware_id) {
776 case ESP_FIRMWARE_CHIP_ESP32:
777 case ESP_FIRMWARE_CHIP_ESP32S2:
778 case ESP_FIRMWARE_CHIP_ESP32C3:
779 case ESP_FIRMWARE_CHIP_ESP32S3:
780 case ESP_FIRMWARE_CHIP_ESP32C2:
781 case ESP_FIRMWARE_CHIP_ESP32C6:
782 return true;
783 default:
784 return false;
785 }
786}
787
bf3d6cb6
SR
788int esp_is_tx_queue_paused(struct esp_wifi_device *priv)
789{
790 if (!priv || !priv->ndev)
791 return 0;
792
793 if ((priv->ndev &&
794 !netif_queue_stopped((const struct net_device *)priv->ndev)))
795 return 1;
796 return 0;
797}
798
774e9b2e
MM
799void esp_tx_pause(struct esp_wifi_device *priv)
800{
801 if (!priv || !priv->ndev)
802 return;
803
804 if (!netif_queue_stopped((const struct net_device *)priv->ndev)) {
805 netif_stop_queue(priv->ndev);
806 }
807}
808
809void esp_tx_resume(struct esp_wifi_device *priv)
810{
811 if (!priv || !priv->ndev)
812 return;
813
814 if (netif_queue_stopped((const struct net_device *)priv->ndev)) {
815 netif_wake_queue(priv->ndev);
816 }
817}
818
a3829bf3 819struct sk_buff *esp_alloc_skb(u32 len)
774e9b2e
MM
820{
821 struct sk_buff *skb = NULL;
822
823 u8 offset;
824
825 skb = netdev_alloc_skb(NULL, len + INTERFACE_HEADER_PADDING);
826
827 if (skb) {
828 /* Align SKB data pointer */
829 offset = ((unsigned long)skb->data) & (SKB_DATA_ADDR_ALIGNMENT - 1);
830
831 if (offset)
832 skb_reserve(skb, INTERFACE_HEADER_PADDING - offset);
833 }
834
835 return skb;
836}
837
838
839static int esp_get_packets(struct esp_adapter *adapter)
840{
841 struct sk_buff *skb = NULL;
842
843 if (!adapter || !adapter->if_ops || !adapter->if_ops->read)
844 return -EINVAL;
845
846 skb = adapter->if_ops->read(adapter);
847
848 if (!skb)
849 return -EFAULT;
850
851 process_rx_packet(adapter, skb);
852
853 return 0;
854}
855
856int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb)
857{
225e14eb 858 if (!adapter || !adapter->if_ops || !adapter->if_ops->write) {
6f8fee57 859 esp_err("%u adapter: %p\n", __LINE__, adapter);
774e9b2e 860 return -EINVAL;
225e14eb 861 }
774e9b2e
MM
862
863 return adapter->if_ops->write(adapter, skb);
864}
865
866static void esp_if_rx_work(struct work_struct *work)
867{
868 /* read inbound packet and forward it to network/serial interface */
869 esp_get_packets(&adapter);
870}
871
7406fe82
MM
872static void update_mac_filter(struct work_struct *work)
873{
c19cde88
KG
874 struct esp_adapter *adapter = esp_get_adapter();
875 struct esp_wifi_device *priv = adapter->priv[0];
876 struct net_device *ndev;
877 struct netdev_hw_addr *mac_addr;
878 u32 count = 0;
879
880 if (!priv)
881 return;
882
883 ndev = priv->ndev;
884 if (!ndev)
885 return;
886
887 if (!priv->port_open) {
888 esp_verbose("Port is not open yet, skipping mac filter update\n");
889 return;
890 }
891
50785e92 892#if CONFIG_ALLOW_MULTICAST_WAKEUP
c19cde88
KG
893 netdev_for_each_mc_addr(mac_addr, ndev) {
894 if (count < MAX_MULTICAST_ADDR_COUNT) {
895 esp_verbose("%d: "MACSTR"\n", count+1, MAC2STR(mac_addr->addr));
896 memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN);
897 }
898 }
899
900 mcast_list.priv = priv;
901 mcast_list.addr_count = count;
902
903 esp_verbose("Setting Multicast list\n");
5a47b073 904 cmd_set_mcast_mac_list(mcast_list.priv, &mcast_list);
50785e92
YM
905#else
906 esp_info("Not setting FW multicast addresses\n");
907#endif
7406fe82
MM
908}
909
774e9b2e
MM
910static void esp_events_work(struct work_struct *work)
911{
912 struct sk_buff *skb = NULL;
913
914 skb = skb_dequeue(&adapter.events_skb_q);
915 if (!skb)
916 return;
917
918 process_internal_event(&adapter, skb);
919 dev_kfree_skb_any(skb);
920}
921
a3829bf3 922static struct esp_adapter *init_adapter(void)
774e9b2e
MM
923{
924 memset(&adapter, 0, sizeof(adapter));
925
926 /* Prepare interface RX work */
927 adapter.if_rx_workqueue = alloc_workqueue("ESP_IF_RX_WORK_QUEUE", 0, 0);
928
929 if (!adapter.if_rx_workqueue) {
930 deinit_adapter();
931 return NULL;
932 }
933
934 INIT_WORK(&adapter.if_rx_work, esp_if_rx_work);
935
936 skb_queue_head_init(&adapter.events_skb_q);
937
938 adapter.events_wq = alloc_workqueue("ESP_EVENTS_WORKQUEUE", WQ_HIGHPRI, 0);
939
940 if (!adapter.events_wq) {
941 deinit_adapter();
942 return NULL;
943 }
944
945 INIT_WORK(&adapter.events_work, esp_events_work);
946
7406fe82
MM
947 INIT_WORK(&adapter.mac_flter_work, update_mac_filter);
948
774e9b2e
MM
949 return &adapter;
950}
951
952static void deinit_adapter(void)
953{
954 skb_queue_purge(&adapter.events_skb_q);
955
956 if (adapter.events_wq)
957 destroy_workqueue(adapter.events_wq);
958
959 if (adapter.if_rx_workqueue)
960 destroy_workqueue(adapter.if_rx_workqueue);
961}
962
963static void esp_reset(void)
964{
965 if (resetpin != HOST_GPIO_PIN_INVALID) {
966 /* Check valid GPIO or not */
967 if (!gpio_is_valid(resetpin)) {
6f8fee57 968 esp_warn("host resetpin (%d) configured is invalid GPIO\n", resetpin);
774e9b2e
MM
969 resetpin = HOST_GPIO_PIN_INVALID;
970 } else {
971 gpio_request(resetpin, "sysfs");
972
973 /* HOST's resetpin set to OUTPUT, HIGH */
974 gpio_direction_output(resetpin, true);
975
976 /* HOST's resetpin set to LOW */
977 gpio_set_value(resetpin, 0);
978 udelay(200);
979
980 /* HOST's resetpin set to INPUT */
981 gpio_direction_input(resetpin);
982
6f8fee57 983 esp_dbg("Triggering ESP reset.\n");
774e9b2e
MM
984 }
985 }
986}
987
774e9b2e
MM
988static int __init esp_init(void)
989{
990 int ret = 0;
991 struct esp_adapter *adapter = NULL;
992
993 /* Reset ESP, Clean start ESP */
994 esp_reset();
995 msleep(200);
996
997 adapter = init_adapter();
998
999 if (!adapter)
1000 return -EFAULT;
1001
1002 /* Init transport layer */
2b40207b 1003 ret = esp_init_interface_layer(adapter, clockspeed);
774e9b2e
MM
1004
1005 if (ret != 0) {
1006 deinit_adapter();
1007 }
1008
e2766bb0 1009 ret = debugfs_init();
774e9b2e
MM
1010 return ret;
1011}
1012
1013static void __exit esp_exit(void)
1014{
1015 uint8_t iface_idx = 0;
bf3d6cb6 1016#if TEST_RAW_TP
38a59879
SS
1017 if (raw_tp_mode != 0) {
1018 test_raw_tp_cleanup();
1019 }
bf3d6cb6 1020#endif
a3829bf3 1021 for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
774e9b2e
MM
1022 cmd_deinit_interface(adapter.priv[iface_idx]);
1023 }
225e14eb 1024 clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags);
774e9b2e
MM
1025
1026 esp_deinit_interface_layer();
1027 deinit_adapter();
1028
1029 if (resetpin != HOST_GPIO_PIN_INVALID) {
1030 gpio_free(resetpin);
1031 }
e2766bb0 1032 debugfs_exit();
774e9b2e
MM
1033}
1034MODULE_LICENSE("GPL");
1035MODULE_AUTHOR("Amey Inamdar <[email protected]>");
1036MODULE_AUTHOR("Mangesh Malusare <[email protected]>");
1037MODULE_AUTHOR("Yogesh Mantri <[email protected]>");
e2766bb0 1038MODULE_AUTHOR("Kapil Gupta <[email protected]>");
774e9b2e 1039MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution");
7e6de249 1040MODULE_VERSION(RELEASE_VERSION);
774e9b2e
MM
1041module_init(esp_init);
1042module_exit(esp_exit);
This page took 0.196837 seconds and 4 git commands to generate.