]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/wcn36xx/smd.c
wcn36xx: handle new hal response format
[karo-tx-linux.git] / drivers / net / wireless / ath / wcn36xx / smd.c
1 /*
2  * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/etherdevice.h>
20 #include <linux/firmware.h>
21 #include <linux/bitops.h>
22 #include "smd.h"
23
24 struct wcn36xx_cfg_val {
25         u32 cfg_id;
26         u32 value;
27 };
28
29 #define WCN36XX_CFG_VAL(id, val) \
30 { \
31         .cfg_id = WCN36XX_HAL_CFG_ ## id, \
32         .value = val \
33 }
34
35 static struct wcn36xx_cfg_val wcn36xx_cfg_vals[] = {
36         WCN36XX_CFG_VAL(CURRENT_TX_ANTENNA, 1),
37         WCN36XX_CFG_VAL(CURRENT_RX_ANTENNA, 1),
38         WCN36XX_CFG_VAL(LOW_GAIN_OVERRIDE, 0),
39         WCN36XX_CFG_VAL(POWER_STATE_PER_CHAIN, 785),
40         WCN36XX_CFG_VAL(CAL_PERIOD, 5),
41         WCN36XX_CFG_VAL(CAL_CONTROL, 1),
42         WCN36XX_CFG_VAL(PROXIMITY, 0),
43         WCN36XX_CFG_VAL(NETWORK_DENSITY, 3),
44         WCN36XX_CFG_VAL(MAX_MEDIUM_TIME, 6000),
45         WCN36XX_CFG_VAL(MAX_MPDUS_IN_AMPDU, 64),
46         WCN36XX_CFG_VAL(RTS_THRESHOLD, 2347),
47         WCN36XX_CFG_VAL(SHORT_RETRY_LIMIT, 6),
48         WCN36XX_CFG_VAL(LONG_RETRY_LIMIT, 6),
49         WCN36XX_CFG_VAL(FRAGMENTATION_THRESHOLD, 8000),
50         WCN36XX_CFG_VAL(DYNAMIC_THRESHOLD_ZERO, 5),
51         WCN36XX_CFG_VAL(DYNAMIC_THRESHOLD_ONE, 10),
52         WCN36XX_CFG_VAL(DYNAMIC_THRESHOLD_TWO, 15),
53         WCN36XX_CFG_VAL(FIXED_RATE, 0),
54         WCN36XX_CFG_VAL(RETRYRATE_POLICY, 4),
55         WCN36XX_CFG_VAL(RETRYRATE_SECONDARY, 0),
56         WCN36XX_CFG_VAL(RETRYRATE_TERTIARY, 0),
57         WCN36XX_CFG_VAL(FORCE_POLICY_PROTECTION, 5),
58         WCN36XX_CFG_VAL(FIXED_RATE_MULTICAST_24GHZ, 1),
59         WCN36XX_CFG_VAL(FIXED_RATE_MULTICAST_5GHZ, 5),
60         WCN36XX_CFG_VAL(DEFAULT_RATE_INDEX_5GHZ, 5),
61         WCN36XX_CFG_VAL(MAX_BA_SESSIONS, 40),
62         WCN36XX_CFG_VAL(PS_DATA_INACTIVITY_TIMEOUT, 200),
63         WCN36XX_CFG_VAL(PS_ENABLE_BCN_FILTER, 1),
64         WCN36XX_CFG_VAL(PS_ENABLE_RSSI_MONITOR, 1),
65         WCN36XX_CFG_VAL(NUM_BEACON_PER_RSSI_AVERAGE, 20),
66         WCN36XX_CFG_VAL(STATS_PERIOD, 10),
67         WCN36XX_CFG_VAL(CFP_MAX_DURATION, 30000),
68         WCN36XX_CFG_VAL(FRAME_TRANS_ENABLED, 0),
69         WCN36XX_CFG_VAL(BA_THRESHOLD_HIGH, 128),
70         WCN36XX_CFG_VAL(MAX_BA_BUFFERS, 2560),
71         WCN36XX_CFG_VAL(DYNAMIC_PS_POLL_VALUE, 0),
72         WCN36XX_CFG_VAL(TX_PWR_CTRL_ENABLE, 1),
73         WCN36XX_CFG_VAL(ENABLE_CLOSE_LOOP, 1),
74         WCN36XX_CFG_VAL(ENABLE_LPWR_IMG_TRANSITION, 0),
75         WCN36XX_CFG_VAL(MAX_ASSOC_LIMIT, 10),
76         WCN36XX_CFG_VAL(ENABLE_MCC_ADAPTIVE_SCHEDULER, 0),
77 };
78
79 static int put_cfg_tlv_u32(struct wcn36xx *wcn, size_t *len, u32 id, u32 value)
80 {
81         struct wcn36xx_hal_cfg *entry;
82         u32 *val;
83
84         if (*len + sizeof(*entry) + sizeof(u32) >= WCN36XX_HAL_BUF_SIZE) {
85                 wcn36xx_err("Not enough room for TLV entry\n");
86                 return -ENOMEM;
87         }
88
89         entry = (struct wcn36xx_hal_cfg *) (wcn->hal_buf + *len);
90         entry->id = id;
91         entry->len = sizeof(u32);
92         entry->pad_bytes = 0;
93         entry->reserve = 0;
94
95         val = (u32 *) (entry + 1);
96         *val = value;
97
98         *len += sizeof(*entry) + sizeof(u32);
99
100         return 0;
101 }
102
103 static void wcn36xx_smd_set_bss_nw_type(struct wcn36xx *wcn,
104                 struct ieee80211_sta *sta,
105                 struct wcn36xx_hal_config_bss_params *bss_params)
106 {
107         if (IEEE80211_BAND_5GHZ == WCN36XX_BAND(wcn))
108                 bss_params->nw_type = WCN36XX_HAL_11A_NW_TYPE;
109         else if (sta && sta->ht_cap.ht_supported)
110                 bss_params->nw_type = WCN36XX_HAL_11N_NW_TYPE;
111         else if (sta && (sta->supp_rates[IEEE80211_BAND_2GHZ] & 0x7f))
112                 bss_params->nw_type = WCN36XX_HAL_11G_NW_TYPE;
113         else
114                 bss_params->nw_type = WCN36XX_HAL_11B_NW_TYPE;
115 }
116
117 static inline u8 is_cap_supported(unsigned long caps, unsigned long flag)
118 {
119         return caps & flag ? 1 : 0;
120 }
121 static void wcn36xx_smd_set_bss_ht_params(struct ieee80211_vif *vif,
122                 struct ieee80211_sta *sta,
123                 struct wcn36xx_hal_config_bss_params *bss_params)
124 {
125         if (sta && sta->ht_cap.ht_supported) {
126                 unsigned long caps = sta->ht_cap.cap;
127                 bss_params->ht = sta->ht_cap.ht_supported;
128                 bss_params->tx_channel_width_set = is_cap_supported(caps,
129                         IEEE80211_HT_CAP_SUP_WIDTH_20_40);
130                 bss_params->lsig_tx_op_protection_full_support =
131                         is_cap_supported(caps,
132                                          IEEE80211_HT_CAP_LSIG_TXOP_PROT);
133
134                 bss_params->ht_oper_mode = vif->bss_conf.ht_operation_mode;
135                 bss_params->lln_non_gf_coexist =
136                         !!(vif->bss_conf.ht_operation_mode &
137                            IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
138                 /* IEEE80211_HT_STBC_PARAM_DUAL_CTS_PROT */
139                 bss_params->dual_cts_protection = 0;
140                 /* IEEE80211_HT_OP_MODE_PROTECTION_20MHZ */
141                 bss_params->ht20_coexist = 0;
142         }
143 }
144
145 static void wcn36xx_smd_set_sta_ht_params(struct ieee80211_sta *sta,
146                 struct wcn36xx_hal_config_sta_params *sta_params)
147 {
148         if (sta->ht_cap.ht_supported) {
149                 unsigned long caps = sta->ht_cap.cap;
150                 sta_params->ht_capable = sta->ht_cap.ht_supported;
151                 sta_params->tx_channel_width_set = is_cap_supported(caps,
152                         IEEE80211_HT_CAP_SUP_WIDTH_20_40);
153                 sta_params->lsig_txop_protection = is_cap_supported(caps,
154                         IEEE80211_HT_CAP_LSIG_TXOP_PROT);
155
156                 sta_params->max_ampdu_size = sta->ht_cap.ampdu_factor;
157                 sta_params->max_ampdu_density = sta->ht_cap.ampdu_density;
158                 sta_params->max_amsdu_size = is_cap_supported(caps,
159                         IEEE80211_HT_CAP_MAX_AMSDU);
160                 sta_params->sgi_20Mhz = is_cap_supported(caps,
161                         IEEE80211_HT_CAP_SGI_20);
162                 sta_params->sgi_40mhz = is_cap_supported(caps,
163                         IEEE80211_HT_CAP_SGI_40);
164                 sta_params->green_field_capable = is_cap_supported(caps,
165                         IEEE80211_HT_CAP_GRN_FLD);
166                 sta_params->delayed_ba_support = is_cap_supported(caps,
167                         IEEE80211_HT_CAP_DELAY_BA);
168                 sta_params->dsss_cck_mode_40mhz = is_cap_supported(caps,
169                         IEEE80211_HT_CAP_DSSSCCK40);
170         }
171 }
172
173 static void wcn36xx_smd_set_sta_default_ht_params(
174                 struct wcn36xx_hal_config_sta_params *sta_params)
175 {
176         sta_params->ht_capable = 1;
177         sta_params->tx_channel_width_set = 1;
178         sta_params->lsig_txop_protection = 1;
179         sta_params->max_ampdu_size = 3;
180         sta_params->max_ampdu_density = 5;
181         sta_params->max_amsdu_size = 0;
182         sta_params->sgi_20Mhz = 1;
183         sta_params->sgi_40mhz = 1;
184         sta_params->green_field_capable = 1;
185         sta_params->delayed_ba_support = 0;
186         sta_params->dsss_cck_mode_40mhz = 1;
187 }
188
189 static void wcn36xx_smd_set_sta_params(struct wcn36xx *wcn,
190                 struct ieee80211_vif *vif,
191                 struct ieee80211_sta *sta,
192                 struct wcn36xx_hal_config_sta_params *sta_params)
193 {
194         struct wcn36xx_vif *priv_vif = (struct wcn36xx_vif *)vif->drv_priv;
195         struct wcn36xx_sta *priv_sta = NULL;
196         if (vif->type == NL80211_IFTYPE_ADHOC ||
197             vif->type == NL80211_IFTYPE_AP ||
198             vif->type == NL80211_IFTYPE_MESH_POINT) {
199                 sta_params->type = 1;
200                 sta_params->sta_index = 0xFF;
201         } else {
202                 sta_params->type = 0;
203                 sta_params->sta_index = 1;
204         }
205
206         sta_params->listen_interval = WCN36XX_LISTEN_INTERVAL(wcn);
207
208         /*
209          * In STA mode ieee80211_sta contains bssid and ieee80211_vif
210          * contains our mac address. In  AP mode we are bssid so vif
211          * contains bssid and ieee80211_sta contains mac.
212          */
213         if (NL80211_IFTYPE_STATION == vif->type)
214                 memcpy(&sta_params->mac, vif->addr, ETH_ALEN);
215         else
216                 memcpy(&sta_params->bssid, vif->addr, ETH_ALEN);
217
218         sta_params->encrypt_type = priv_vif->encrypt_type;
219         sta_params->short_preamble_supported = true;
220
221         sta_params->rifs_mode = 0;
222         sta_params->rmf = 0;
223         sta_params->action = 0;
224         sta_params->uapsd = 0;
225         sta_params->mimo_ps = WCN36XX_HAL_HT_MIMO_PS_STATIC;
226         sta_params->max_ampdu_duration = 0;
227         sta_params->bssid_index = priv_vif->bss_index;
228         sta_params->p2p = 0;
229
230         if (sta) {
231                 priv_sta = (struct wcn36xx_sta *)sta->drv_priv;
232                 if (NL80211_IFTYPE_STATION == vif->type)
233                         memcpy(&sta_params->bssid, sta->addr, ETH_ALEN);
234                 else
235                         memcpy(&sta_params->mac, sta->addr, ETH_ALEN);
236                 sta_params->wmm_enabled = sta->wme;
237                 sta_params->max_sp_len = sta->max_sp;
238                 sta_params->aid = priv_sta->aid;
239                 wcn36xx_smd_set_sta_ht_params(sta, sta_params);
240                 memcpy(&sta_params->supported_rates, &priv_sta->supported_rates,
241                         sizeof(priv_sta->supported_rates));
242         } else {
243                 wcn36xx_set_default_rates(&sta_params->supported_rates);
244                 wcn36xx_smd_set_sta_default_ht_params(sta_params);
245         }
246 }
247
248 static int wcn36xx_smd_send_and_wait(struct wcn36xx *wcn, size_t len)
249 {
250         int ret = 0;
251         unsigned long start;
252         wcn36xx_dbg_dump(WCN36XX_DBG_SMD_DUMP, "HAL >>> ", wcn->hal_buf, len);
253
254         init_completion(&wcn->hal_rsp_compl);
255         start = jiffies;
256         ret = wcn->ctrl_ops->tx(wcn->hal_buf, len);
257         if (ret) {
258                 wcn36xx_err("HAL TX failed\n");
259                 goto out;
260         }
261         if (wait_for_completion_timeout(&wcn->hal_rsp_compl,
262                 msecs_to_jiffies(HAL_MSG_TIMEOUT)) <= 0) {
263                 wcn36xx_err("Timeout! No SMD response in %dms\n",
264                             HAL_MSG_TIMEOUT);
265                 ret = -ETIME;
266                 goto out;
267         }
268         wcn36xx_dbg(WCN36XX_DBG_SMD, "SMD command completed in %dms",
269                     jiffies_to_msecs(jiffies - start));
270 out:
271         return ret;
272 }
273
274 #define INIT_HAL_MSG(msg_body, type) \
275         do {                                                            \
276                 memset(&msg_body, 0, sizeof(msg_body));                 \
277                 msg_body.header.msg_type = type;                        \
278                 msg_body.header.msg_version = WCN36XX_HAL_MSG_VERSION0; \
279                 msg_body.header.len = sizeof(msg_body);                 \
280         } while (0)                                                     \
281
282 #define PREPARE_HAL_BUF(send_buf, msg_body) \
283         do {                                                    \
284                 memset(send_buf, 0, msg_body.header.len);       \
285                 memcpy(send_buf, &msg_body, sizeof(msg_body));  \
286         } while (0)                                             \
287
288 static int wcn36xx_smd_rsp_status_check(void *buf, size_t len)
289 {
290         struct wcn36xx_fw_msg_status_rsp *rsp;
291
292         if (len < sizeof(struct wcn36xx_hal_msg_header) +
293             sizeof(struct wcn36xx_fw_msg_status_rsp))
294                 return -EIO;
295
296         rsp = (struct wcn36xx_fw_msg_status_rsp *)
297                 (buf + sizeof(struct wcn36xx_hal_msg_header));
298
299         if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->status)
300                 return rsp->status;
301
302         return 0;
303 }
304
305 static int wcn36xx_smd_rsp_status_check_v2(struct wcn36xx *wcn, void *buf,
306                                              size_t len)
307 {
308         struct wcn36xx_fw_msg_status_rsp_v2 *rsp;
309
310         if (wcn->chip_version != WCN36XX_CHIP_3620 ||
311             len < sizeof(struct wcn36xx_hal_msg_header) + sizeof(*rsp))
312                 return wcn36xx_smd_rsp_status_check(buf, len);
313
314         rsp = buf + sizeof(struct wcn36xx_hal_msg_header);
315
316         if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->status)
317                 return rsp->status;
318
319         return 0;
320 }
321
322 int wcn36xx_smd_load_nv(struct wcn36xx *wcn)
323 {
324         struct nv_data *nv_d;
325         struct wcn36xx_hal_nv_img_download_req_msg msg_body;
326         int fw_bytes_left;
327         int ret;
328         u16 fm_offset = 0;
329
330         if (!wcn->nv) {
331                 ret = request_firmware(&wcn->nv, WLAN_NV_FILE, wcn->dev);
332                 if (ret) {
333                         wcn36xx_err("Failed to load nv file %s: %d\n",
334                                       WLAN_NV_FILE, ret);
335                         goto out;
336                 }
337         }
338
339         nv_d = (struct nv_data *)wcn->nv->data;
340         INIT_HAL_MSG(msg_body, WCN36XX_HAL_DOWNLOAD_NV_REQ);
341
342         msg_body.header.len += WCN36XX_NV_FRAGMENT_SIZE;
343
344         msg_body.frag_number = 0;
345         /* hal_buf must be protected with  mutex */
346         mutex_lock(&wcn->hal_mutex);
347
348         do {
349                 fw_bytes_left = wcn->nv->size - fm_offset - 4;
350                 if (fw_bytes_left > WCN36XX_NV_FRAGMENT_SIZE) {
351                         msg_body.last_fragment = 0;
352                         msg_body.nv_img_buffer_size = WCN36XX_NV_FRAGMENT_SIZE;
353                 } else {
354                         msg_body.last_fragment = 1;
355                         msg_body.nv_img_buffer_size = fw_bytes_left;
356
357                         /* Do not forget update general message len */
358                         msg_body.header.len = sizeof(msg_body) + fw_bytes_left;
359
360                 }
361
362                 /* Add load NV request message header */
363                 memcpy(wcn->hal_buf, &msg_body, sizeof(msg_body));
364
365                 /* Add NV body itself */
366                 memcpy(wcn->hal_buf + sizeof(msg_body),
367                        &nv_d->table + fm_offset,
368                        msg_body.nv_img_buffer_size);
369
370                 ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
371                 if (ret)
372                         goto out_unlock;
373                 ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf,
374                                                    wcn->hal_rsp_len);
375                 if (ret) {
376                         wcn36xx_err("hal_load_nv response failed err=%d\n",
377                                     ret);
378                         goto out_unlock;
379                 }
380                 msg_body.frag_number++;
381                 fm_offset += WCN36XX_NV_FRAGMENT_SIZE;
382
383         } while (msg_body.last_fragment != 1);
384
385 out_unlock:
386         mutex_unlock(&wcn->hal_mutex);
387 out:    return ret;
388 }
389
390 static int wcn36xx_smd_start_rsp(struct wcn36xx *wcn, void *buf, size_t len)
391 {
392         struct wcn36xx_hal_mac_start_rsp_msg *rsp;
393
394         if (len < sizeof(*rsp))
395                 return -EIO;
396
397         rsp = (struct wcn36xx_hal_mac_start_rsp_msg *)buf;
398
399         if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->start_rsp_params.status)
400                 return -EIO;
401
402         memcpy(wcn->crm_version, rsp->start_rsp_params.crm_version,
403                WCN36XX_HAL_VERSION_LENGTH);
404         memcpy(wcn->wlan_version, rsp->start_rsp_params.wlan_version,
405                WCN36XX_HAL_VERSION_LENGTH);
406
407         /* null terminate the strings, just in case */
408         wcn->crm_version[WCN36XX_HAL_VERSION_LENGTH] = '\0';
409         wcn->wlan_version[WCN36XX_HAL_VERSION_LENGTH] = '\0';
410
411         wcn->fw_revision = rsp->start_rsp_params.version.revision;
412         wcn->fw_version = rsp->start_rsp_params.version.version;
413         wcn->fw_minor = rsp->start_rsp_params.version.minor;
414         wcn->fw_major = rsp->start_rsp_params.version.major;
415
416         wcn36xx_info("firmware WLAN version '%s' and CRM version '%s'\n",
417                      wcn->wlan_version, wcn->crm_version);
418
419         wcn36xx_info("firmware API %u.%u.%u.%u, %u stations, %u bssids\n",
420                      wcn->fw_major, wcn->fw_minor,
421                      wcn->fw_version, wcn->fw_revision,
422                      rsp->start_rsp_params.stations,
423                      rsp->start_rsp_params.bssids);
424
425         return 0;
426 }
427
428 int wcn36xx_smd_start(struct wcn36xx *wcn)
429 {
430         struct wcn36xx_hal_mac_start_req_msg msg_body, *body;
431         int ret = 0;
432         int i;
433         size_t len;
434
435         mutex_lock(&wcn->hal_mutex);
436         INIT_HAL_MSG(msg_body, WCN36XX_HAL_START_REQ);
437
438         msg_body.params.type = DRIVER_TYPE_PRODUCTION;
439         msg_body.params.len = 0;
440
441         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
442
443         body = (struct wcn36xx_hal_mac_start_req_msg *)wcn->hal_buf;
444         len = body->header.len;
445
446         for (i = 0; i < ARRAY_SIZE(wcn36xx_cfg_vals); i++) {
447                 ret = put_cfg_tlv_u32(wcn, &len, wcn36xx_cfg_vals[i].cfg_id,
448                                       wcn36xx_cfg_vals[i].value);
449                 if (ret)
450                         goto out;
451         }
452         body->header.len = len;
453         body->params.len = len - sizeof(*body);
454
455         wcn36xx_dbg(WCN36XX_DBG_HAL, "hal start type %d\n",
456                     msg_body.params.type);
457
458         ret = wcn36xx_smd_send_and_wait(wcn, body->header.len);
459         if (ret) {
460                 wcn36xx_err("Sending hal_start failed\n");
461                 goto out;
462         }
463
464         ret = wcn36xx_smd_start_rsp(wcn, wcn->hal_buf, wcn->hal_rsp_len);
465         if (ret) {
466                 wcn36xx_err("hal_start response failed err=%d\n", ret);
467                 goto out;
468         }
469
470 out:
471         mutex_unlock(&wcn->hal_mutex);
472         return ret;
473 }
474
475 int wcn36xx_smd_stop(struct wcn36xx *wcn)
476 {
477         struct wcn36xx_hal_mac_stop_req_msg msg_body;
478         int ret = 0;
479
480         mutex_lock(&wcn->hal_mutex);
481         INIT_HAL_MSG(msg_body, WCN36XX_HAL_STOP_REQ);
482
483         msg_body.stop_req_params.reason = HAL_STOP_TYPE_RF_KILL;
484
485         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
486
487         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
488         if (ret) {
489                 wcn36xx_err("Sending hal_stop failed\n");
490                 goto out;
491         }
492         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
493         if (ret) {
494                 wcn36xx_err("hal_stop response failed err=%d\n", ret);
495                 goto out;
496         }
497 out:
498         mutex_unlock(&wcn->hal_mutex);
499         return ret;
500 }
501
502 int wcn36xx_smd_init_scan(struct wcn36xx *wcn, enum wcn36xx_hal_sys_mode mode)
503 {
504         struct wcn36xx_hal_init_scan_req_msg msg_body;
505         int ret = 0;
506
507         mutex_lock(&wcn->hal_mutex);
508         INIT_HAL_MSG(msg_body, WCN36XX_HAL_INIT_SCAN_REQ);
509
510         msg_body.mode = mode;
511
512         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
513
514         wcn36xx_dbg(WCN36XX_DBG_HAL, "hal init scan mode %d\n", msg_body.mode);
515
516         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
517         if (ret) {
518                 wcn36xx_err("Sending hal_init_scan failed\n");
519                 goto out;
520         }
521         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
522         if (ret) {
523                 wcn36xx_err("hal_init_scan response failed err=%d\n", ret);
524                 goto out;
525         }
526 out:
527         mutex_unlock(&wcn->hal_mutex);
528         return ret;
529 }
530
531 int wcn36xx_smd_start_scan(struct wcn36xx *wcn)
532 {
533         struct wcn36xx_hal_start_scan_req_msg msg_body;
534         int ret = 0;
535
536         mutex_lock(&wcn->hal_mutex);
537         INIT_HAL_MSG(msg_body, WCN36XX_HAL_START_SCAN_REQ);
538
539         msg_body.scan_channel = WCN36XX_HW_CHANNEL(wcn);
540
541         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
542
543         wcn36xx_dbg(WCN36XX_DBG_HAL, "hal start scan channel %d\n",
544                     msg_body.scan_channel);
545
546         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
547         if (ret) {
548                 wcn36xx_err("Sending hal_start_scan failed\n");
549                 goto out;
550         }
551         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
552         if (ret) {
553                 wcn36xx_err("hal_start_scan response failed err=%d\n", ret);
554                 goto out;
555         }
556 out:
557         mutex_unlock(&wcn->hal_mutex);
558         return ret;
559 }
560
561 int wcn36xx_smd_end_scan(struct wcn36xx *wcn)
562 {
563         struct wcn36xx_hal_end_scan_req_msg msg_body;
564         int ret = 0;
565
566         mutex_lock(&wcn->hal_mutex);
567         INIT_HAL_MSG(msg_body, WCN36XX_HAL_END_SCAN_REQ);
568
569         msg_body.scan_channel = WCN36XX_HW_CHANNEL(wcn);
570
571         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
572
573         wcn36xx_dbg(WCN36XX_DBG_HAL, "hal end scan channel %d\n",
574                     msg_body.scan_channel);
575
576         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
577         if (ret) {
578                 wcn36xx_err("Sending hal_end_scan failed\n");
579                 goto out;
580         }
581         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
582         if (ret) {
583                 wcn36xx_err("hal_end_scan response failed err=%d\n", ret);
584                 goto out;
585         }
586 out:
587         mutex_unlock(&wcn->hal_mutex);
588         return ret;
589 }
590
591 int wcn36xx_smd_finish_scan(struct wcn36xx *wcn,
592                             enum wcn36xx_hal_sys_mode mode)
593 {
594         struct wcn36xx_hal_finish_scan_req_msg msg_body;
595         int ret = 0;
596
597         mutex_lock(&wcn->hal_mutex);
598         INIT_HAL_MSG(msg_body, WCN36XX_HAL_FINISH_SCAN_REQ);
599
600         msg_body.mode = mode;
601
602         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
603
604         wcn36xx_dbg(WCN36XX_DBG_HAL, "hal finish scan mode %d\n",
605                     msg_body.mode);
606
607         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
608         if (ret) {
609                 wcn36xx_err("Sending hal_finish_scan failed\n");
610                 goto out;
611         }
612         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
613         if (ret) {
614                 wcn36xx_err("hal_finish_scan response failed err=%d\n", ret);
615                 goto out;
616         }
617 out:
618         mutex_unlock(&wcn->hal_mutex);
619         return ret;
620 }
621
622 static int wcn36xx_smd_switch_channel_rsp(void *buf, size_t len)
623 {
624         struct wcn36xx_hal_switch_channel_rsp_msg *rsp;
625         int ret = 0;
626
627         ret = wcn36xx_smd_rsp_status_check(buf, len);
628         if (ret)
629                 return ret;
630         rsp = (struct wcn36xx_hal_switch_channel_rsp_msg *)buf;
631         wcn36xx_dbg(WCN36XX_DBG_HAL, "channel switched to: %d, status: %d\n",
632                     rsp->channel_number, rsp->status);
633         return ret;
634 }
635
636 int wcn36xx_smd_switch_channel(struct wcn36xx *wcn,
637                                struct ieee80211_vif *vif, int ch)
638 {
639         struct wcn36xx_hal_switch_channel_req_msg msg_body;
640         int ret = 0;
641
642         mutex_lock(&wcn->hal_mutex);
643         INIT_HAL_MSG(msg_body, WCN36XX_HAL_CH_SWITCH_REQ);
644
645         msg_body.channel_number = (u8)ch;
646         msg_body.tx_mgmt_power = 0xbf;
647         msg_body.max_tx_power = 0xbf;
648         memcpy(msg_body.self_sta_mac_addr, vif->addr, ETH_ALEN);
649
650         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
651
652         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
653         if (ret) {
654                 wcn36xx_err("Sending hal_switch_channel failed\n");
655                 goto out;
656         }
657         ret = wcn36xx_smd_switch_channel_rsp(wcn->hal_buf, wcn->hal_rsp_len);
658         if (ret) {
659                 wcn36xx_err("hal_switch_channel response failed err=%d\n", ret);
660                 goto out;
661         }
662 out:
663         mutex_unlock(&wcn->hal_mutex);
664         return ret;
665 }
666
667 static int wcn36xx_smd_update_scan_params_rsp(void *buf, size_t len)
668 {
669         struct wcn36xx_hal_update_scan_params_resp *rsp;
670
671         rsp = (struct wcn36xx_hal_update_scan_params_resp *)buf;
672
673         /* Remove the PNO version bit */
674         rsp->status &= (~(WCN36XX_FW_MSG_PNO_VERSION_MASK));
675
676         if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->status) {
677                 wcn36xx_warn("error response from update scan\n");
678                 return rsp->status;
679         }
680
681         return 0;
682 }
683
684 int wcn36xx_smd_update_scan_params(struct wcn36xx *wcn)
685 {
686         struct wcn36xx_hal_update_scan_params_req msg_body;
687         int ret = 0;
688
689         mutex_lock(&wcn->hal_mutex);
690         INIT_HAL_MSG(msg_body, WCN36XX_HAL_UPDATE_SCAN_PARAM_REQ);
691
692         msg_body.dot11d_enabled = 0;
693         msg_body.dot11d_resolved = 0;
694         msg_body.channel_count = 26;
695         msg_body.active_min_ch_time = 60;
696         msg_body.active_max_ch_time = 120;
697         msg_body.passive_min_ch_time = 60;
698         msg_body.passive_max_ch_time = 110;
699         msg_body.state = 0;
700
701         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
702
703         wcn36xx_dbg(WCN36XX_DBG_HAL,
704                     "hal update scan params channel_count %d\n",
705                     msg_body.channel_count);
706
707         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
708         if (ret) {
709                 wcn36xx_err("Sending hal_update_scan_params failed\n");
710                 goto out;
711         }
712         ret = wcn36xx_smd_update_scan_params_rsp(wcn->hal_buf,
713                                                  wcn->hal_rsp_len);
714         if (ret) {
715                 wcn36xx_err("hal_update_scan_params response failed err=%d\n",
716                             ret);
717                 goto out;
718         }
719 out:
720         mutex_unlock(&wcn->hal_mutex);
721         return ret;
722 }
723
724 static int wcn36xx_smd_add_sta_self_rsp(struct wcn36xx *wcn,
725                                         struct ieee80211_vif *vif,
726                                         void *buf,
727                                         size_t len)
728 {
729         struct wcn36xx_hal_add_sta_self_rsp_msg *rsp;
730         struct wcn36xx_vif *priv_vif = (struct wcn36xx_vif *)vif->drv_priv;
731
732         if (len < sizeof(*rsp))
733                 return -EINVAL;
734
735         rsp = (struct wcn36xx_hal_add_sta_self_rsp_msg *)buf;
736
737         if (rsp->status != WCN36XX_FW_MSG_RESULT_SUCCESS) {
738                 wcn36xx_warn("hal add sta self failure: %d\n",
739                              rsp->status);
740                 return rsp->status;
741         }
742
743         wcn36xx_dbg(WCN36XX_DBG_HAL,
744                     "hal add sta self status %d self_sta_index %d dpu_index %d\n",
745                     rsp->status, rsp->self_sta_index, rsp->dpu_index);
746
747         priv_vif->self_sta_index = rsp->self_sta_index;
748         priv_vif->self_dpu_desc_index = rsp->dpu_index;
749
750         return 0;
751 }
752
753 int wcn36xx_smd_add_sta_self(struct wcn36xx *wcn, struct ieee80211_vif *vif)
754 {
755         struct wcn36xx_hal_add_sta_self_req msg_body;
756         int ret = 0;
757
758         mutex_lock(&wcn->hal_mutex);
759         INIT_HAL_MSG(msg_body, WCN36XX_HAL_ADD_STA_SELF_REQ);
760
761         memcpy(&msg_body.self_addr, vif->addr, ETH_ALEN);
762
763         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
764
765         wcn36xx_dbg(WCN36XX_DBG_HAL,
766                     "hal add sta self self_addr %pM status %d\n",
767                     msg_body.self_addr, msg_body.status);
768
769         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
770         if (ret) {
771                 wcn36xx_err("Sending hal_add_sta_self failed\n");
772                 goto out;
773         }
774         ret = wcn36xx_smd_add_sta_self_rsp(wcn,
775                                            vif,
776                                            wcn->hal_buf,
777                                            wcn->hal_rsp_len);
778         if (ret) {
779                 wcn36xx_err("hal_add_sta_self response failed err=%d\n", ret);
780                 goto out;
781         }
782 out:
783         mutex_unlock(&wcn->hal_mutex);
784         return ret;
785 }
786
787 int wcn36xx_smd_delete_sta_self(struct wcn36xx *wcn, u8 *addr)
788 {
789         struct wcn36xx_hal_del_sta_self_req_msg msg_body;
790         int ret = 0;
791
792         mutex_lock(&wcn->hal_mutex);
793         INIT_HAL_MSG(msg_body, WCN36XX_HAL_DEL_STA_SELF_REQ);
794
795         memcpy(&msg_body.self_addr, addr, ETH_ALEN);
796
797         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
798
799         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
800         if (ret) {
801                 wcn36xx_err("Sending hal_delete_sta_self failed\n");
802                 goto out;
803         }
804         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
805         if (ret) {
806                 wcn36xx_err("hal_delete_sta_self response failed err=%d\n",
807                             ret);
808                 goto out;
809         }
810 out:
811         mutex_unlock(&wcn->hal_mutex);
812         return ret;
813 }
814
815 int wcn36xx_smd_delete_sta(struct wcn36xx *wcn, u8 sta_index)
816 {
817         struct wcn36xx_hal_delete_sta_req_msg msg_body;
818         int ret = 0;
819
820         mutex_lock(&wcn->hal_mutex);
821         INIT_HAL_MSG(msg_body, WCN36XX_HAL_DELETE_STA_REQ);
822
823         msg_body.sta_index = sta_index;
824
825         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
826
827         wcn36xx_dbg(WCN36XX_DBG_HAL,
828                     "hal delete sta sta_index %d\n",
829                     msg_body.sta_index);
830
831         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
832         if (ret) {
833                 wcn36xx_err("Sending hal_delete_sta failed\n");
834                 goto out;
835         }
836         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
837         if (ret) {
838                 wcn36xx_err("hal_delete_sta response failed err=%d\n", ret);
839                 goto out;
840         }
841 out:
842         mutex_unlock(&wcn->hal_mutex);
843         return ret;
844 }
845
846 static int wcn36xx_smd_join_rsp(void *buf, size_t len)
847 {
848         struct wcn36xx_hal_join_rsp_msg *rsp;
849
850         if (wcn36xx_smd_rsp_status_check(buf, len))
851                 return -EIO;
852
853         rsp = (struct wcn36xx_hal_join_rsp_msg *)buf;
854
855         wcn36xx_dbg(WCN36XX_DBG_HAL,
856                     "hal rsp join status %d tx_mgmt_power %d\n",
857                     rsp->status, rsp->tx_mgmt_power);
858
859         return 0;
860 }
861
862 int wcn36xx_smd_join(struct wcn36xx *wcn, const u8 *bssid, u8 *vif, u8 ch)
863 {
864         struct wcn36xx_hal_join_req_msg msg_body;
865         int ret = 0;
866
867         mutex_lock(&wcn->hal_mutex);
868         INIT_HAL_MSG(msg_body, WCN36XX_HAL_JOIN_REQ);
869
870         memcpy(&msg_body.bssid, bssid, ETH_ALEN);
871         memcpy(&msg_body.self_sta_mac_addr, vif, ETH_ALEN);
872         msg_body.channel = ch;
873
874         if (conf_is_ht40_minus(&wcn->hw->conf))
875                 msg_body.secondary_channel_offset =
876                         PHY_DOUBLE_CHANNEL_HIGH_PRIMARY;
877         else if (conf_is_ht40_plus(&wcn->hw->conf))
878                 msg_body.secondary_channel_offset =
879                         PHY_DOUBLE_CHANNEL_LOW_PRIMARY;
880         else
881                 msg_body.secondary_channel_offset =
882                         PHY_SINGLE_CHANNEL_CENTERED;
883
884         msg_body.link_state = WCN36XX_HAL_LINK_PREASSOC_STATE;
885
886         msg_body.max_tx_power = 0xbf;
887         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
888
889         wcn36xx_dbg(WCN36XX_DBG_HAL,
890                     "hal join req bssid %pM self_sta_mac_addr %pM channel %d link_state %d\n",
891                     msg_body.bssid, msg_body.self_sta_mac_addr,
892                     msg_body.channel, msg_body.link_state);
893
894         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
895         if (ret) {
896                 wcn36xx_err("Sending hal_join failed\n");
897                 goto out;
898         }
899         ret = wcn36xx_smd_join_rsp(wcn->hal_buf, wcn->hal_rsp_len);
900         if (ret) {
901                 wcn36xx_err("hal_join response failed err=%d\n", ret);
902                 goto out;
903         }
904 out:
905         mutex_unlock(&wcn->hal_mutex);
906         return ret;
907 }
908
909 int wcn36xx_smd_set_link_st(struct wcn36xx *wcn, const u8 *bssid,
910                             const u8 *sta_mac,
911                             enum wcn36xx_hal_link_state state)
912 {
913         struct wcn36xx_hal_set_link_state_req_msg msg_body;
914         int ret = 0;
915
916         mutex_lock(&wcn->hal_mutex);
917         INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_LINK_ST_REQ);
918
919         memcpy(&msg_body.bssid, bssid, ETH_ALEN);
920         memcpy(&msg_body.self_mac_addr, sta_mac, ETH_ALEN);
921         msg_body.state = state;
922
923         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
924
925         wcn36xx_dbg(WCN36XX_DBG_HAL,
926                     "hal set link state bssid %pM self_mac_addr %pM state %d\n",
927                     msg_body.bssid, msg_body.self_mac_addr, msg_body.state);
928
929         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
930         if (ret) {
931                 wcn36xx_err("Sending hal_set_link_st failed\n");
932                 goto out;
933         }
934         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
935         if (ret) {
936                 wcn36xx_err("hal_set_link_st response failed err=%d\n", ret);
937                 goto out;
938         }
939 out:
940         mutex_unlock(&wcn->hal_mutex);
941         return ret;
942 }
943
944 static void wcn36xx_smd_convert_sta_to_v1(struct wcn36xx *wcn,
945                         const struct wcn36xx_hal_config_sta_params *orig,
946                         struct wcn36xx_hal_config_sta_params_v1 *v1)
947 {
948         /* convert orig to v1 format */
949         memcpy(&v1->bssid, orig->bssid, ETH_ALEN);
950         memcpy(&v1->mac, orig->mac, ETH_ALEN);
951         v1->aid = orig->aid;
952         v1->type = orig->type;
953         v1->listen_interval = orig->listen_interval;
954         v1->ht_capable = orig->ht_capable;
955
956         v1->max_ampdu_size = orig->max_ampdu_size;
957         v1->max_ampdu_density = orig->max_ampdu_density;
958         v1->sgi_40mhz = orig->sgi_40mhz;
959         v1->sgi_20Mhz = orig->sgi_20Mhz;
960
961         memcpy(&v1->supported_rates, &orig->supported_rates,
962                sizeof(orig->supported_rates));
963         v1->sta_index = orig->sta_index;
964 }
965
966 static int wcn36xx_smd_config_sta_rsp(struct wcn36xx *wcn,
967                                       struct ieee80211_sta *sta,
968                                       void *buf,
969                                       size_t len)
970 {
971         struct wcn36xx_hal_config_sta_rsp_msg *rsp;
972         struct config_sta_rsp_params *params;
973         struct wcn36xx_sta *sta_priv = (struct wcn36xx_sta *)sta->drv_priv;
974
975         if (len < sizeof(*rsp))
976                 return -EINVAL;
977
978         rsp = (struct wcn36xx_hal_config_sta_rsp_msg *)buf;
979         params = &rsp->params;
980
981         if (params->status != WCN36XX_FW_MSG_RESULT_SUCCESS) {
982                 wcn36xx_warn("hal config sta response failure: %d\n",
983                              params->status);
984                 return -EIO;
985         }
986
987         sta_priv->sta_index = params->sta_index;
988         sta_priv->dpu_desc_index = params->dpu_index;
989         sta_priv->ucast_dpu_sign = params->uc_ucast_sig;
990
991         wcn36xx_dbg(WCN36XX_DBG_HAL,
992                     "hal config sta rsp status %d sta_index %d bssid_index %d uc_ucast_sig %d p2p %d\n",
993                     params->status, params->sta_index, params->bssid_index,
994                     params->uc_ucast_sig, params->p2p);
995
996         return 0;
997 }
998
999 static int wcn36xx_smd_config_sta_v1(struct wcn36xx *wcn,
1000                      const struct wcn36xx_hal_config_sta_req_msg *orig)
1001 {
1002         struct wcn36xx_hal_config_sta_req_msg_v1 msg_body;
1003         struct wcn36xx_hal_config_sta_params_v1 *sta = &msg_body.sta_params;
1004
1005         INIT_HAL_MSG(msg_body, WCN36XX_HAL_CONFIG_STA_REQ);
1006
1007         wcn36xx_smd_convert_sta_to_v1(wcn, &orig->sta_params,
1008                                       &msg_body.sta_params);
1009
1010         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1011
1012         wcn36xx_dbg(WCN36XX_DBG_HAL,
1013                     "hal config sta v1 action %d sta_index %d bssid_index %d bssid %pM type %d mac %pM aid %d\n",
1014                     sta->action, sta->sta_index, sta->bssid_index,
1015                     sta->bssid, sta->type, sta->mac, sta->aid);
1016
1017         return wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1018 }
1019
1020 int wcn36xx_smd_config_sta(struct wcn36xx *wcn, struct ieee80211_vif *vif,
1021                            struct ieee80211_sta *sta)
1022 {
1023         struct wcn36xx_hal_config_sta_req_msg msg;
1024         struct wcn36xx_hal_config_sta_params *sta_params;
1025         int ret = 0;
1026
1027         mutex_lock(&wcn->hal_mutex);
1028         INIT_HAL_MSG(msg, WCN36XX_HAL_CONFIG_STA_REQ);
1029
1030         sta_params = &msg.sta_params;
1031
1032         wcn36xx_smd_set_sta_params(wcn, vif, sta, sta_params);
1033
1034         if (!wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
1035                 ret = wcn36xx_smd_config_sta_v1(wcn, &msg);
1036         } else {
1037                 PREPARE_HAL_BUF(wcn->hal_buf, msg);
1038
1039                 wcn36xx_dbg(WCN36XX_DBG_HAL,
1040                             "hal config sta action %d sta_index %d bssid_index %d bssid %pM type %d mac %pM aid %d\n",
1041                             sta_params->action, sta_params->sta_index,
1042                             sta_params->bssid_index, sta_params->bssid,
1043                             sta_params->type, sta_params->mac, sta_params->aid);
1044
1045                 ret = wcn36xx_smd_send_and_wait(wcn, msg.header.len);
1046         }
1047         if (ret) {
1048                 wcn36xx_err("Sending hal_config_sta failed\n");
1049                 goto out;
1050         }
1051         ret = wcn36xx_smd_config_sta_rsp(wcn,
1052                                          sta,
1053                                          wcn->hal_buf,
1054                                          wcn->hal_rsp_len);
1055         if (ret) {
1056                 wcn36xx_err("hal_config_sta response failed err=%d\n", ret);
1057                 goto out;
1058         }
1059 out:
1060         mutex_unlock(&wcn->hal_mutex);
1061         return ret;
1062 }
1063
1064 static int wcn36xx_smd_config_bss_v1(struct wcn36xx *wcn,
1065                         const struct wcn36xx_hal_config_bss_req_msg *orig)
1066 {
1067         struct wcn36xx_hal_config_bss_req_msg_v1 msg_body;
1068         struct wcn36xx_hal_config_bss_params_v1 *bss = &msg_body.bss_params;
1069         struct wcn36xx_hal_config_sta_params_v1 *sta = &bss->sta;
1070
1071         INIT_HAL_MSG(msg_body, WCN36XX_HAL_CONFIG_BSS_REQ);
1072
1073         /* convert orig to v1 */
1074         memcpy(&msg_body.bss_params.bssid,
1075                &orig->bss_params.bssid, ETH_ALEN);
1076         memcpy(&msg_body.bss_params.self_mac_addr,
1077                &orig->bss_params.self_mac_addr, ETH_ALEN);
1078
1079         msg_body.bss_params.bss_type = orig->bss_params.bss_type;
1080         msg_body.bss_params.oper_mode = orig->bss_params.oper_mode;
1081         msg_body.bss_params.nw_type = orig->bss_params.nw_type;
1082
1083         msg_body.bss_params.short_slot_time_supported =
1084                 orig->bss_params.short_slot_time_supported;
1085         msg_body.bss_params.lla_coexist = orig->bss_params.lla_coexist;
1086         msg_body.bss_params.llb_coexist = orig->bss_params.llb_coexist;
1087         msg_body.bss_params.llg_coexist = orig->bss_params.llg_coexist;
1088         msg_body.bss_params.ht20_coexist = orig->bss_params.ht20_coexist;
1089         msg_body.bss_params.lln_non_gf_coexist =
1090                 orig->bss_params.lln_non_gf_coexist;
1091
1092         msg_body.bss_params.lsig_tx_op_protection_full_support =
1093                 orig->bss_params.lsig_tx_op_protection_full_support;
1094         msg_body.bss_params.rifs_mode = orig->bss_params.rifs_mode;
1095         msg_body.bss_params.beacon_interval = orig->bss_params.beacon_interval;
1096         msg_body.bss_params.dtim_period = orig->bss_params.dtim_period;
1097         msg_body.bss_params.tx_channel_width_set =
1098                 orig->bss_params.tx_channel_width_set;
1099         msg_body.bss_params.oper_channel = orig->bss_params.oper_channel;
1100         msg_body.bss_params.ext_channel = orig->bss_params.ext_channel;
1101
1102         msg_body.bss_params.reserved = orig->bss_params.reserved;
1103
1104         memcpy(&msg_body.bss_params.ssid,
1105                &orig->bss_params.ssid,
1106                sizeof(orig->bss_params.ssid));
1107
1108         msg_body.bss_params.action = orig->bss_params.action;
1109         msg_body.bss_params.rateset = orig->bss_params.rateset;
1110         msg_body.bss_params.ht = orig->bss_params.ht;
1111         msg_body.bss_params.obss_prot_enabled =
1112                 orig->bss_params.obss_prot_enabled;
1113         msg_body.bss_params.rmf = orig->bss_params.rmf;
1114         msg_body.bss_params.ht_oper_mode = orig->bss_params.ht_oper_mode;
1115         msg_body.bss_params.dual_cts_protection =
1116                 orig->bss_params.dual_cts_protection;
1117
1118         msg_body.bss_params.max_probe_resp_retry_limit =
1119                 orig->bss_params.max_probe_resp_retry_limit;
1120         msg_body.bss_params.hidden_ssid = orig->bss_params.hidden_ssid;
1121         msg_body.bss_params.proxy_probe_resp =
1122                 orig->bss_params.proxy_probe_resp;
1123         msg_body.bss_params.edca_params_valid =
1124                 orig->bss_params.edca_params_valid;
1125
1126         memcpy(&msg_body.bss_params.acbe,
1127                &orig->bss_params.acbe,
1128                sizeof(orig->bss_params.acbe));
1129         memcpy(&msg_body.bss_params.acbk,
1130                &orig->bss_params.acbk,
1131                sizeof(orig->bss_params.acbk));
1132         memcpy(&msg_body.bss_params.acvi,
1133                &orig->bss_params.acvi,
1134                sizeof(orig->bss_params.acvi));
1135         memcpy(&msg_body.bss_params.acvo,
1136                &orig->bss_params.acvo,
1137                sizeof(orig->bss_params.acvo));
1138
1139         msg_body.bss_params.ext_set_sta_key_param_valid =
1140                 orig->bss_params.ext_set_sta_key_param_valid;
1141
1142         memcpy(&msg_body.bss_params.ext_set_sta_key_param,
1143                &orig->bss_params.ext_set_sta_key_param,
1144                sizeof(orig->bss_params.acvo));
1145
1146         msg_body.bss_params.wcn36xx_hal_persona =
1147                 orig->bss_params.wcn36xx_hal_persona;
1148         msg_body.bss_params.spectrum_mgt_enable =
1149                 orig->bss_params.spectrum_mgt_enable;
1150         msg_body.bss_params.tx_mgmt_power = orig->bss_params.tx_mgmt_power;
1151         msg_body.bss_params.max_tx_power = orig->bss_params.max_tx_power;
1152
1153         wcn36xx_smd_convert_sta_to_v1(wcn, &orig->bss_params.sta,
1154                                       &msg_body.bss_params.sta);
1155
1156         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1157
1158         wcn36xx_dbg(WCN36XX_DBG_HAL,
1159                     "hal config bss v1 bssid %pM self_mac_addr %pM bss_type %d oper_mode %d nw_type %d\n",
1160                     bss->bssid, bss->self_mac_addr, bss->bss_type,
1161                     bss->oper_mode, bss->nw_type);
1162
1163         wcn36xx_dbg(WCN36XX_DBG_HAL,
1164                     "- sta bssid %pM action %d sta_index %d bssid_index %d aid %d type %d mac %pM\n",
1165                     sta->bssid, sta->action, sta->sta_index,
1166                     sta->bssid_index, sta->aid, sta->type, sta->mac);
1167
1168         return wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1169 }
1170
1171
1172 static int wcn36xx_smd_config_bss_rsp(struct wcn36xx *wcn,
1173                                       struct ieee80211_vif *vif,
1174                                       void *buf,
1175                                       size_t len)
1176 {
1177         struct wcn36xx_hal_config_bss_rsp_msg *rsp;
1178         struct wcn36xx_hal_config_bss_rsp_params *params;
1179         struct wcn36xx_vif *priv_vif = (struct wcn36xx_vif *)vif->drv_priv;
1180
1181         if (len < sizeof(*rsp))
1182                 return -EINVAL;
1183
1184         rsp = (struct wcn36xx_hal_config_bss_rsp_msg *)buf;
1185         params = &rsp->bss_rsp_params;
1186
1187         if (params->status != WCN36XX_FW_MSG_RESULT_SUCCESS) {
1188                 wcn36xx_warn("hal config bss response failure: %d\n",
1189                              params->status);
1190                 return -EIO;
1191         }
1192
1193         wcn36xx_dbg(WCN36XX_DBG_HAL,
1194                     "hal config bss rsp status %d bss_idx %d dpu_desc_index %d"
1195                     " sta_idx %d self_idx %d bcast_idx %d mac %pM"
1196                     " power %d ucast_dpu_signature %d\n",
1197                     params->status, params->bss_index, params->dpu_desc_index,
1198                     params->bss_sta_index, params->bss_self_sta_index,
1199                     params->bss_bcast_sta_idx, params->mac,
1200                     params->tx_mgmt_power, params->ucast_dpu_signature);
1201
1202         priv_vif->bss_index = params->bss_index;
1203
1204         if (priv_vif->sta) {
1205                 priv_vif->sta->bss_sta_index =  params->bss_sta_index;
1206                 priv_vif->sta->bss_dpu_desc_index = params->dpu_desc_index;
1207         }
1208
1209         priv_vif->self_ucast_dpu_sign = params->ucast_dpu_signature;
1210
1211         return 0;
1212 }
1213
1214 int wcn36xx_smd_config_bss(struct wcn36xx *wcn, struct ieee80211_vif *vif,
1215                            struct ieee80211_sta *sta, const u8 *bssid,
1216                            bool update)
1217 {
1218         struct wcn36xx_hal_config_bss_req_msg msg;
1219         struct wcn36xx_hal_config_bss_params *bss;
1220         struct wcn36xx_hal_config_sta_params *sta_params;
1221         struct wcn36xx_vif *vif_priv = (struct wcn36xx_vif *)vif->drv_priv;
1222         int ret = 0;
1223
1224         mutex_lock(&wcn->hal_mutex);
1225         INIT_HAL_MSG(msg, WCN36XX_HAL_CONFIG_BSS_REQ);
1226
1227         bss = &msg.bss_params;
1228         sta_params = &bss->sta;
1229
1230         WARN_ON(is_zero_ether_addr(bssid));
1231
1232         memcpy(&bss->bssid, bssid, ETH_ALEN);
1233
1234         memcpy(bss->self_mac_addr, vif->addr, ETH_ALEN);
1235
1236         if (vif->type == NL80211_IFTYPE_STATION) {
1237                 bss->bss_type = WCN36XX_HAL_INFRASTRUCTURE_MODE;
1238
1239                 /* STA */
1240                 bss->oper_mode = 1;
1241                 bss->wcn36xx_hal_persona = WCN36XX_HAL_STA_MODE;
1242         } else if (vif->type == NL80211_IFTYPE_AP ||
1243                    vif->type == NL80211_IFTYPE_MESH_POINT) {
1244                 bss->bss_type = WCN36XX_HAL_INFRA_AP_MODE;
1245
1246                 /* AP */
1247                 bss->oper_mode = 0;
1248                 bss->wcn36xx_hal_persona = WCN36XX_HAL_STA_SAP_MODE;
1249         } else if (vif->type == NL80211_IFTYPE_ADHOC) {
1250                 bss->bss_type = WCN36XX_HAL_IBSS_MODE;
1251
1252                 /* STA */
1253                 bss->oper_mode = 1;
1254         } else {
1255                 wcn36xx_warn("Unknown type for bss config: %d\n", vif->type);
1256         }
1257
1258         if (vif->type == NL80211_IFTYPE_STATION)
1259                 wcn36xx_smd_set_bss_nw_type(wcn, sta, bss);
1260         else
1261                 bss->nw_type = WCN36XX_HAL_11N_NW_TYPE;
1262
1263         bss->short_slot_time_supported = vif->bss_conf.use_short_slot;
1264         bss->lla_coexist = 0;
1265         bss->llb_coexist = 0;
1266         bss->llg_coexist = 0;
1267         bss->rifs_mode = 0;
1268         bss->beacon_interval = vif->bss_conf.beacon_int;
1269         bss->dtim_period = vif_priv->dtim_period;
1270
1271         wcn36xx_smd_set_bss_ht_params(vif, sta, bss);
1272
1273         bss->oper_channel = WCN36XX_HW_CHANNEL(wcn);
1274
1275         if (conf_is_ht40_minus(&wcn->hw->conf))
1276                 bss->ext_channel = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1277         else if (conf_is_ht40_plus(&wcn->hw->conf))
1278                 bss->ext_channel = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1279         else
1280                 bss->ext_channel = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1281
1282         bss->reserved = 0;
1283         wcn36xx_smd_set_sta_params(wcn, vif, sta, sta_params);
1284
1285         /* wcn->ssid is only valid in AP and IBSS mode */
1286         bss->ssid.length = vif_priv->ssid.length;
1287         memcpy(bss->ssid.ssid, vif_priv->ssid.ssid, vif_priv->ssid.length);
1288
1289         bss->obss_prot_enabled = 0;
1290         bss->rmf = 0;
1291         bss->max_probe_resp_retry_limit = 0;
1292         bss->hidden_ssid = vif->bss_conf.hidden_ssid;
1293         bss->proxy_probe_resp = 0;
1294         bss->edca_params_valid = 0;
1295
1296         /* FIXME: set acbe, acbk, acvi and acvo */
1297
1298         bss->ext_set_sta_key_param_valid = 0;
1299
1300         /* FIXME: set ext_set_sta_key_param */
1301
1302         bss->spectrum_mgt_enable = 0;
1303         bss->tx_mgmt_power = 0;
1304         bss->max_tx_power = WCN36XX_MAX_POWER(wcn);
1305
1306         bss->action = update;
1307
1308         wcn36xx_dbg(WCN36XX_DBG_HAL,
1309                     "hal config bss bssid %pM self_mac_addr %pM bss_type %d oper_mode %d nw_type %d\n",
1310                     bss->bssid, bss->self_mac_addr, bss->bss_type,
1311                     bss->oper_mode, bss->nw_type);
1312
1313         wcn36xx_dbg(WCN36XX_DBG_HAL,
1314                     "- sta bssid %pM action %d sta_index %d bssid_index %d aid %d type %d mac %pM\n",
1315                     sta_params->bssid, sta_params->action,
1316                     sta_params->sta_index, sta_params->bssid_index,
1317                     sta_params->aid, sta_params->type,
1318                     sta_params->mac);
1319
1320         if (!wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
1321                 ret = wcn36xx_smd_config_bss_v1(wcn, &msg);
1322         } else {
1323                 PREPARE_HAL_BUF(wcn->hal_buf, msg);
1324
1325                 ret = wcn36xx_smd_send_and_wait(wcn, msg.header.len);
1326         }
1327         if (ret) {
1328                 wcn36xx_err("Sending hal_config_bss failed\n");
1329                 goto out;
1330         }
1331         ret = wcn36xx_smd_config_bss_rsp(wcn,
1332                                          vif,
1333                                          wcn->hal_buf,
1334                                          wcn->hal_rsp_len);
1335         if (ret) {
1336                 wcn36xx_err("hal_config_bss response failed err=%d\n", ret);
1337                 goto out;
1338         }
1339 out:
1340         mutex_unlock(&wcn->hal_mutex);
1341         return ret;
1342 }
1343
1344 int wcn36xx_smd_delete_bss(struct wcn36xx *wcn, struct ieee80211_vif *vif)
1345 {
1346         struct wcn36xx_hal_delete_bss_req_msg msg_body;
1347         struct wcn36xx_vif *priv_vif = (struct wcn36xx_vif *)vif->drv_priv;
1348         int ret = 0;
1349
1350         mutex_lock(&wcn->hal_mutex);
1351         INIT_HAL_MSG(msg_body, WCN36XX_HAL_DELETE_BSS_REQ);
1352
1353         msg_body.bss_index = priv_vif->bss_index;
1354
1355         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1356
1357         wcn36xx_dbg(WCN36XX_DBG_HAL, "hal delete bss %d\n", msg_body.bss_index);
1358
1359         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1360         if (ret) {
1361                 wcn36xx_err("Sending hal_delete_bss failed\n");
1362                 goto out;
1363         }
1364         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1365         if (ret) {
1366                 wcn36xx_err("hal_delete_bss response failed err=%d\n", ret);
1367                 goto out;
1368         }
1369 out:
1370         mutex_unlock(&wcn->hal_mutex);
1371         return ret;
1372 }
1373
1374 int wcn36xx_smd_send_beacon(struct wcn36xx *wcn, struct ieee80211_vif *vif,
1375                             struct sk_buff *skb_beacon, u16 tim_off,
1376                             u16 p2p_off)
1377 {
1378         struct wcn36xx_hal_send_beacon_req_msg msg_body;
1379         int ret = 0;
1380
1381         mutex_lock(&wcn->hal_mutex);
1382         INIT_HAL_MSG(msg_body, WCN36XX_HAL_SEND_BEACON_REQ);
1383
1384         /* TODO need to find out why this is needed? */
1385         msg_body.beacon_length = skb_beacon->len + 6;
1386
1387         if (BEACON_TEMPLATE_SIZE > msg_body.beacon_length) {
1388                 memcpy(&msg_body.beacon, &skb_beacon->len, sizeof(u32));
1389                 memcpy(&(msg_body.beacon[4]), skb_beacon->data,
1390                        skb_beacon->len);
1391         } else {
1392                 wcn36xx_err("Beacon is to big: beacon size=%d\n",
1393                               msg_body.beacon_length);
1394                 ret = -ENOMEM;
1395                 goto out;
1396         }
1397         memcpy(msg_body.bssid, vif->addr, ETH_ALEN);
1398
1399         /* TODO need to find out why this is needed? */
1400         if (vif->type == NL80211_IFTYPE_MESH_POINT)
1401                 /* mesh beacon don't need this, so push further down */
1402                 msg_body.tim_ie_offset = 256;
1403         else
1404                 msg_body.tim_ie_offset = tim_off+4;
1405         msg_body.p2p_ie_offset = p2p_off;
1406         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1407
1408         wcn36xx_dbg(WCN36XX_DBG_HAL,
1409                     "hal send beacon beacon_length %d\n",
1410                     msg_body.beacon_length);
1411
1412         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1413         if (ret) {
1414                 wcn36xx_err("Sending hal_send_beacon failed\n");
1415                 goto out;
1416         }
1417         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1418         if (ret) {
1419                 wcn36xx_err("hal_send_beacon response failed err=%d\n", ret);
1420                 goto out;
1421         }
1422 out:
1423         mutex_unlock(&wcn->hal_mutex);
1424         return ret;
1425 }
1426
1427 int wcn36xx_smd_update_proberesp_tmpl(struct wcn36xx *wcn,
1428                                       struct ieee80211_vif *vif,
1429                                       struct sk_buff *skb)
1430 {
1431         struct wcn36xx_hal_send_probe_resp_req_msg msg;
1432         int ret = 0;
1433
1434         mutex_lock(&wcn->hal_mutex);
1435         INIT_HAL_MSG(msg, WCN36XX_HAL_UPDATE_PROBE_RSP_TEMPLATE_REQ);
1436
1437         if (skb->len > BEACON_TEMPLATE_SIZE) {
1438                 wcn36xx_warn("probe response template is too big: %d\n",
1439                              skb->len);
1440                 ret = -E2BIG;
1441                 goto out;
1442         }
1443
1444         msg.probe_resp_template_len = skb->len;
1445         memcpy(&msg.probe_resp_template, skb->data, skb->len);
1446
1447         memcpy(msg.bssid, vif->addr, ETH_ALEN);
1448
1449         PREPARE_HAL_BUF(wcn->hal_buf, msg);
1450
1451         wcn36xx_dbg(WCN36XX_DBG_HAL,
1452                     "hal update probe rsp len %d bssid %pM\n",
1453                     msg.probe_resp_template_len, msg.bssid);
1454
1455         ret = wcn36xx_smd_send_and_wait(wcn, msg.header.len);
1456         if (ret) {
1457                 wcn36xx_err("Sending hal_update_proberesp_tmpl failed\n");
1458                 goto out;
1459         }
1460         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1461         if (ret) {
1462                 wcn36xx_err("hal_update_proberesp_tmpl response failed err=%d\n",
1463                             ret);
1464                 goto out;
1465         }
1466 out:
1467         mutex_unlock(&wcn->hal_mutex);
1468         return ret;
1469 }
1470
1471 int wcn36xx_smd_set_stakey(struct wcn36xx *wcn,
1472                            enum ani_ed_type enc_type,
1473                            u8 keyidx,
1474                            u8 keylen,
1475                            u8 *key,
1476                            u8 sta_index)
1477 {
1478         struct wcn36xx_hal_set_sta_key_req_msg msg_body;
1479         int ret = 0;
1480
1481         mutex_lock(&wcn->hal_mutex);
1482         INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_STAKEY_REQ);
1483
1484         msg_body.set_sta_key_params.sta_index = sta_index;
1485         msg_body.set_sta_key_params.enc_type = enc_type;
1486
1487         msg_body.set_sta_key_params.key[0].id = keyidx;
1488         msg_body.set_sta_key_params.key[0].unicast = 1;
1489         msg_body.set_sta_key_params.key[0].direction = WCN36XX_HAL_TX_RX;
1490         msg_body.set_sta_key_params.key[0].pae_role = 0;
1491         msg_body.set_sta_key_params.key[0].length = keylen;
1492         memcpy(msg_body.set_sta_key_params.key[0].key, key, keylen);
1493         msg_body.set_sta_key_params.single_tid_rc = 1;
1494
1495         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1496
1497         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1498         if (ret) {
1499                 wcn36xx_err("Sending hal_set_stakey failed\n");
1500                 goto out;
1501         }
1502         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1503         if (ret) {
1504                 wcn36xx_err("hal_set_stakey response failed err=%d\n", ret);
1505                 goto out;
1506         }
1507 out:
1508         mutex_unlock(&wcn->hal_mutex);
1509         return ret;
1510 }
1511
1512 int wcn36xx_smd_set_bsskey(struct wcn36xx *wcn,
1513                            enum ani_ed_type enc_type,
1514                            u8 keyidx,
1515                            u8 keylen,
1516                            u8 *key)
1517 {
1518         struct wcn36xx_hal_set_bss_key_req_msg msg_body;
1519         int ret = 0;
1520
1521         mutex_lock(&wcn->hal_mutex);
1522         INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_BSSKEY_REQ);
1523         msg_body.bss_idx = 0;
1524         msg_body.enc_type = enc_type;
1525         msg_body.num_keys = 1;
1526         msg_body.keys[0].id = keyidx;
1527         msg_body.keys[0].unicast = 0;
1528         msg_body.keys[0].direction = WCN36XX_HAL_RX_ONLY;
1529         msg_body.keys[0].pae_role = 0;
1530         msg_body.keys[0].length = keylen;
1531         memcpy(msg_body.keys[0].key, key, keylen);
1532
1533         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1534
1535         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1536         if (ret) {
1537                 wcn36xx_err("Sending hal_set_bsskey failed\n");
1538                 goto out;
1539         }
1540         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1541         if (ret) {
1542                 wcn36xx_err("hal_set_bsskey response failed err=%d\n", ret);
1543                 goto out;
1544         }
1545 out:
1546         mutex_unlock(&wcn->hal_mutex);
1547         return ret;
1548 }
1549
1550 int wcn36xx_smd_remove_stakey(struct wcn36xx *wcn,
1551                               enum ani_ed_type enc_type,
1552                               u8 keyidx,
1553                               u8 sta_index)
1554 {
1555         struct wcn36xx_hal_remove_sta_key_req_msg msg_body;
1556         int ret = 0;
1557
1558         mutex_lock(&wcn->hal_mutex);
1559         INIT_HAL_MSG(msg_body, WCN36XX_HAL_RMV_STAKEY_REQ);
1560
1561         msg_body.sta_idx = sta_index;
1562         msg_body.enc_type = enc_type;
1563         msg_body.key_id = keyidx;
1564
1565         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1566
1567         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1568         if (ret) {
1569                 wcn36xx_err("Sending hal_remove_stakey failed\n");
1570                 goto out;
1571         }
1572         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1573         if (ret) {
1574                 wcn36xx_err("hal_remove_stakey response failed err=%d\n", ret);
1575                 goto out;
1576         }
1577 out:
1578         mutex_unlock(&wcn->hal_mutex);
1579         return ret;
1580 }
1581
1582 int wcn36xx_smd_remove_bsskey(struct wcn36xx *wcn,
1583                               enum ani_ed_type enc_type,
1584                               u8 keyidx)
1585 {
1586         struct wcn36xx_hal_remove_bss_key_req_msg msg_body;
1587         int ret = 0;
1588
1589         mutex_lock(&wcn->hal_mutex);
1590         INIT_HAL_MSG(msg_body, WCN36XX_HAL_RMV_BSSKEY_REQ);
1591         msg_body.bss_idx = 0;
1592         msg_body.enc_type = enc_type;
1593         msg_body.key_id = keyidx;
1594
1595         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1596
1597         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1598         if (ret) {
1599                 wcn36xx_err("Sending hal_remove_bsskey failed\n");
1600                 goto out;
1601         }
1602         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1603         if (ret) {
1604                 wcn36xx_err("hal_remove_bsskey response failed err=%d\n", ret);
1605                 goto out;
1606         }
1607 out:
1608         mutex_unlock(&wcn->hal_mutex);
1609         return ret;
1610 }
1611
1612 int wcn36xx_smd_enter_bmps(struct wcn36xx *wcn, struct ieee80211_vif *vif)
1613 {
1614         struct wcn36xx_hal_enter_bmps_req_msg msg_body;
1615         struct wcn36xx_vif *vif_priv = (struct wcn36xx_vif *)vif->drv_priv;
1616         int ret = 0;
1617
1618         mutex_lock(&wcn->hal_mutex);
1619         INIT_HAL_MSG(msg_body, WCN36XX_HAL_ENTER_BMPS_REQ);
1620
1621         msg_body.bss_index = vif_priv->bss_index;
1622         msg_body.tbtt = vif->bss_conf.sync_tsf;
1623         msg_body.dtim_period = vif_priv->dtim_period;
1624
1625         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1626
1627         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1628         if (ret) {
1629                 wcn36xx_err("Sending hal_enter_bmps failed\n");
1630                 goto out;
1631         }
1632         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1633         if (ret) {
1634                 wcn36xx_err("hal_enter_bmps response failed err=%d\n", ret);
1635                 goto out;
1636         }
1637 out:
1638         mutex_unlock(&wcn->hal_mutex);
1639         return ret;
1640 }
1641
1642 int wcn36xx_smd_exit_bmps(struct wcn36xx *wcn, struct ieee80211_vif *vif)
1643 {
1644         struct wcn36xx_hal_enter_bmps_req_msg msg_body;
1645         struct wcn36xx_vif *vif_priv = (struct wcn36xx_vif *)vif->drv_priv;
1646         int ret = 0;
1647
1648         mutex_lock(&wcn->hal_mutex);
1649         INIT_HAL_MSG(msg_body, WCN36XX_HAL_EXIT_BMPS_REQ);
1650
1651         msg_body.bss_index = vif_priv->bss_index;
1652
1653         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1654
1655         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1656         if (ret) {
1657                 wcn36xx_err("Sending hal_exit_bmps failed\n");
1658                 goto out;
1659         }
1660         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1661         if (ret) {
1662                 wcn36xx_err("hal_exit_bmps response failed err=%d\n", ret);
1663                 goto out;
1664         }
1665 out:
1666         mutex_unlock(&wcn->hal_mutex);
1667         return ret;
1668 }
1669 int wcn36xx_smd_set_power_params(struct wcn36xx *wcn, bool ignore_dtim)
1670 {
1671         struct wcn36xx_hal_set_power_params_req_msg msg_body;
1672         int ret = 0;
1673
1674         mutex_lock(&wcn->hal_mutex);
1675         INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_POWER_PARAMS_REQ);
1676
1677         /*
1678          * When host is down ignore every second dtim
1679          */
1680         if (ignore_dtim) {
1681                 msg_body.ignore_dtim = 1;
1682                 msg_body.dtim_period = 2;
1683         }
1684         msg_body.listen_interval = WCN36XX_LISTEN_INTERVAL(wcn);
1685
1686         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1687
1688         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1689         if (ret) {
1690                 wcn36xx_err("Sending hal_set_power_params failed\n");
1691                 goto out;
1692         }
1693
1694 out:
1695         mutex_unlock(&wcn->hal_mutex);
1696         return ret;
1697 }
1698 /* Notice: This function should be called after associated, or else it
1699  * will be invalid
1700  */
1701 int wcn36xx_smd_keep_alive_req(struct wcn36xx *wcn,
1702                                struct ieee80211_vif *vif,
1703                                int packet_type)
1704 {
1705         struct wcn36xx_hal_keep_alive_req_msg msg_body;
1706         struct wcn36xx_vif *vif_priv = (struct wcn36xx_vif *)vif->drv_priv;
1707         int ret = 0;
1708
1709         mutex_lock(&wcn->hal_mutex);
1710         INIT_HAL_MSG(msg_body, WCN36XX_HAL_KEEP_ALIVE_REQ);
1711
1712         if (packet_type == WCN36XX_HAL_KEEP_ALIVE_NULL_PKT) {
1713                 msg_body.bss_index = vif_priv->bss_index;
1714                 msg_body.packet_type = WCN36XX_HAL_KEEP_ALIVE_NULL_PKT;
1715                 msg_body.time_period = WCN36XX_KEEP_ALIVE_TIME_PERIOD;
1716         } else if (packet_type == WCN36XX_HAL_KEEP_ALIVE_UNSOLICIT_ARP_RSP) {
1717                 /* TODO: it also support ARP response type */
1718         } else {
1719                 wcn36xx_warn("unknown keep alive packet type %d\n", packet_type);
1720                 ret = -EINVAL;
1721                 goto out;
1722         }
1723
1724         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1725
1726         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1727         if (ret) {
1728                 wcn36xx_err("Sending hal_keep_alive failed\n");
1729                 goto out;
1730         }
1731         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1732         if (ret) {
1733                 wcn36xx_err("hal_keep_alive response failed err=%d\n", ret);
1734                 goto out;
1735         }
1736 out:
1737         mutex_unlock(&wcn->hal_mutex);
1738         return ret;
1739 }
1740
1741 int wcn36xx_smd_dump_cmd_req(struct wcn36xx *wcn, u32 arg1, u32 arg2,
1742                              u32 arg3, u32 arg4, u32 arg5)
1743 {
1744         struct wcn36xx_hal_dump_cmd_req_msg msg_body;
1745         int ret = 0;
1746
1747         mutex_lock(&wcn->hal_mutex);
1748         INIT_HAL_MSG(msg_body, WCN36XX_HAL_DUMP_COMMAND_REQ);
1749
1750         msg_body.arg1 = arg1;
1751         msg_body.arg2 = arg2;
1752         msg_body.arg3 = arg3;
1753         msg_body.arg4 = arg4;
1754         msg_body.arg5 = arg5;
1755
1756         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1757
1758         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1759         if (ret) {
1760                 wcn36xx_err("Sending hal_dump_cmd failed\n");
1761                 goto out;
1762         }
1763         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1764         if (ret) {
1765                 wcn36xx_err("hal_dump_cmd response failed err=%d\n", ret);
1766                 goto out;
1767         }
1768 out:
1769         mutex_unlock(&wcn->hal_mutex);
1770         return ret;
1771 }
1772
1773 void set_feat_caps(u32 *bitmap, enum place_holder_in_cap_bitmap cap)
1774 {
1775         int arr_idx, bit_idx;
1776
1777         if (cap < 0 || cap > 127) {
1778                 wcn36xx_warn("error cap idx %d\n", cap);
1779                 return;
1780         }
1781
1782         arr_idx = cap / 32;
1783         bit_idx = cap % 32;
1784         bitmap[arr_idx] |= (1 << bit_idx);
1785 }
1786
1787 int get_feat_caps(u32 *bitmap, enum place_holder_in_cap_bitmap cap)
1788 {
1789         int arr_idx, bit_idx;
1790         int ret = 0;
1791
1792         if (cap < 0 || cap > 127) {
1793                 wcn36xx_warn("error cap idx %d\n", cap);
1794                 return -EINVAL;
1795         }
1796
1797         arr_idx = cap / 32;
1798         bit_idx = cap % 32;
1799         ret = (bitmap[arr_idx] & (1 << bit_idx)) ? 1 : 0;
1800         return ret;
1801 }
1802
1803 void clear_feat_caps(u32 *bitmap, enum place_holder_in_cap_bitmap cap)
1804 {
1805         int arr_idx, bit_idx;
1806
1807         if (cap < 0 || cap > 127) {
1808                 wcn36xx_warn("error cap idx %d\n", cap);
1809                 return;
1810         }
1811
1812         arr_idx = cap / 32;
1813         bit_idx = cap % 32;
1814         bitmap[arr_idx] &= ~(1 << bit_idx);
1815 }
1816
1817 int wcn36xx_smd_feature_caps_exchange(struct wcn36xx *wcn)
1818 {
1819         struct wcn36xx_hal_feat_caps_msg msg_body, *rsp;
1820         int ret = 0, i;
1821
1822         mutex_lock(&wcn->hal_mutex);
1823         INIT_HAL_MSG(msg_body, WCN36XX_HAL_FEATURE_CAPS_EXCHANGE_REQ);
1824
1825         set_feat_caps(msg_body.feat_caps, STA_POWERSAVE);
1826
1827         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1828
1829         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1830         if (ret) {
1831                 wcn36xx_err("Sending hal_feature_caps_exchange failed\n");
1832                 goto out;
1833         }
1834         if (wcn->hal_rsp_len != sizeof(*rsp)) {
1835                 wcn36xx_err("Invalid hal_feature_caps_exchange response");
1836                 goto out;
1837         }
1838
1839         rsp = (struct wcn36xx_hal_feat_caps_msg *) wcn->hal_buf;
1840
1841         for (i = 0; i < WCN36XX_HAL_CAPS_SIZE; i++)
1842                 wcn->fw_feat_caps[i] = rsp->feat_caps[i];
1843 out:
1844         mutex_unlock(&wcn->hal_mutex);
1845         return ret;
1846 }
1847
1848 int wcn36xx_smd_add_ba_session(struct wcn36xx *wcn,
1849                 struct ieee80211_sta *sta,
1850                 u16 tid,
1851                 u16 *ssn,
1852                 u8 direction,
1853                 u8 sta_index)
1854 {
1855         struct wcn36xx_hal_add_ba_session_req_msg msg_body;
1856         int ret = 0;
1857
1858         mutex_lock(&wcn->hal_mutex);
1859         INIT_HAL_MSG(msg_body, WCN36XX_HAL_ADD_BA_SESSION_REQ);
1860
1861         msg_body.sta_index = sta_index;
1862         memcpy(&msg_body.mac_addr, sta->addr, ETH_ALEN);
1863         msg_body.dialog_token = 0x10;
1864         msg_body.tid = tid;
1865
1866         /* Immediate BA because Delayed BA is not supported */
1867         msg_body.policy = 1;
1868         msg_body.buffer_size = WCN36XX_AGGR_BUFFER_SIZE;
1869         msg_body.timeout = 0;
1870         if (ssn)
1871                 msg_body.ssn = *ssn;
1872         msg_body.direction = direction;
1873
1874         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1875
1876         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1877         if (ret) {
1878                 wcn36xx_err("Sending hal_add_ba_session failed\n");
1879                 goto out;
1880         }
1881         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1882         if (ret) {
1883                 wcn36xx_err("hal_add_ba_session response failed err=%d\n", ret);
1884                 goto out;
1885         }
1886 out:
1887         mutex_unlock(&wcn->hal_mutex);
1888         return ret;
1889 }
1890
1891 int wcn36xx_smd_add_ba(struct wcn36xx *wcn)
1892 {
1893         struct wcn36xx_hal_add_ba_req_msg msg_body;
1894         int ret = 0;
1895
1896         mutex_lock(&wcn->hal_mutex);
1897         INIT_HAL_MSG(msg_body, WCN36XX_HAL_ADD_BA_REQ);
1898
1899         msg_body.session_id = 0;
1900         msg_body.win_size = WCN36XX_AGGR_BUFFER_SIZE;
1901
1902         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1903
1904         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1905         if (ret) {
1906                 wcn36xx_err("Sending hal_add_ba failed\n");
1907                 goto out;
1908         }
1909         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1910         if (ret) {
1911                 wcn36xx_err("hal_add_ba response failed err=%d\n", ret);
1912                 goto out;
1913         }
1914 out:
1915         mutex_unlock(&wcn->hal_mutex);
1916         return ret;
1917 }
1918
1919 int wcn36xx_smd_del_ba(struct wcn36xx *wcn, u16 tid, u8 sta_index)
1920 {
1921         struct wcn36xx_hal_del_ba_req_msg msg_body;
1922         int ret = 0;
1923
1924         mutex_lock(&wcn->hal_mutex);
1925         INIT_HAL_MSG(msg_body, WCN36XX_HAL_DEL_BA_REQ);
1926
1927         msg_body.sta_index = sta_index;
1928         msg_body.tid = tid;
1929         msg_body.direction = 0;
1930         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1931
1932         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1933         if (ret) {
1934                 wcn36xx_err("Sending hal_del_ba failed\n");
1935                 goto out;
1936         }
1937         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1938         if (ret) {
1939                 wcn36xx_err("hal_del_ba response failed err=%d\n", ret);
1940                 goto out;
1941         }
1942 out:
1943         mutex_unlock(&wcn->hal_mutex);
1944         return ret;
1945 }
1946
1947 int wcn36xx_smd_trigger_ba(struct wcn36xx *wcn, u8 sta_index)
1948 {
1949         struct wcn36xx_hal_trigger_ba_req_msg msg_body;
1950         struct wcn36xx_hal_trigger_ba_req_candidate *candidate;
1951         int ret = 0;
1952
1953         mutex_lock(&wcn->hal_mutex);
1954         INIT_HAL_MSG(msg_body, WCN36XX_HAL_TRIGGER_BA_REQ);
1955
1956         msg_body.session_id = 0;
1957         msg_body.candidate_cnt = 1;
1958         msg_body.header.len += sizeof(*candidate);
1959         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1960
1961         candidate = (struct wcn36xx_hal_trigger_ba_req_candidate *)
1962                 (wcn->hal_buf + sizeof(msg_body));
1963         candidate->sta_index = sta_index;
1964         candidate->tid_bitmap = 1;
1965
1966         ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1967         if (ret) {
1968                 wcn36xx_err("Sending hal_trigger_ba failed\n");
1969                 goto out;
1970         }
1971         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1972         if (ret) {
1973                 wcn36xx_err("hal_trigger_ba response failed err=%d\n", ret);
1974                 goto out;
1975         }
1976 out:
1977         mutex_unlock(&wcn->hal_mutex);
1978         return ret;
1979 }
1980
1981 static int wcn36xx_smd_tx_compl_ind(struct wcn36xx *wcn, void *buf, size_t len)
1982 {
1983         struct wcn36xx_hal_tx_compl_ind_msg *rsp = buf;
1984
1985         if (len != sizeof(*rsp)) {
1986                 wcn36xx_warn("Bad TX complete indication\n");
1987                 return -EIO;
1988         }
1989
1990         wcn36xx_dxe_tx_ack_ind(wcn, rsp->status);
1991
1992         return 0;
1993 }
1994
1995 static int wcn36xx_smd_missed_beacon_ind(struct wcn36xx *wcn,
1996                                          void *buf,
1997                                          size_t len)
1998 {
1999         struct wcn36xx_hal_missed_beacon_ind_msg *rsp = buf;
2000         struct ieee80211_vif *vif = NULL;
2001         struct wcn36xx_vif *tmp;
2002
2003         /* Old FW does not have bss index */
2004         if (wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
2005                 list_for_each_entry(tmp, &wcn->vif_list, list) {
2006                         wcn36xx_dbg(WCN36XX_DBG_HAL, "beacon missed bss_index %d\n",
2007                                     tmp->bss_index);
2008                         vif = container_of((void *)tmp,
2009                                                  struct ieee80211_vif,
2010                                                  drv_priv);
2011                         ieee80211_connection_loss(vif);
2012                 }
2013                 return 0;
2014         }
2015
2016         if (len != sizeof(*rsp)) {
2017                 wcn36xx_warn("Corrupted missed beacon indication\n");
2018                 return -EIO;
2019         }
2020
2021         list_for_each_entry(tmp, &wcn->vif_list, list) {
2022                 if (tmp->bss_index == rsp->bss_index) {
2023                         wcn36xx_dbg(WCN36XX_DBG_HAL, "beacon missed bss_index %d\n",
2024                                     rsp->bss_index);
2025                         vif = container_of((void *)tmp,
2026                                                  struct ieee80211_vif,
2027                                                  drv_priv);
2028                         ieee80211_connection_loss(vif);
2029                         return 0;
2030                 }
2031         }
2032
2033         wcn36xx_warn("BSS index %d not found\n", rsp->bss_index);
2034         return -ENOENT;
2035 }
2036
2037 static int wcn36xx_smd_delete_sta_context_ind(struct wcn36xx *wcn,
2038                                               void *buf,
2039                                               size_t len)
2040 {
2041         struct wcn36xx_hal_delete_sta_context_ind_msg *rsp = buf;
2042         struct wcn36xx_vif *tmp;
2043         struct ieee80211_sta *sta = NULL;
2044
2045         if (len != sizeof(*rsp)) {
2046                 wcn36xx_warn("Corrupted delete sta indication\n");
2047                 return -EIO;
2048         }
2049
2050         list_for_each_entry(tmp, &wcn->vif_list, list) {
2051                 if (sta && (tmp->sta->sta_index == rsp->sta_id)) {
2052                         sta = container_of((void *)tmp->sta,
2053                                                  struct ieee80211_sta,
2054                                                  drv_priv);
2055                         wcn36xx_dbg(WCN36XX_DBG_HAL,
2056                                     "delete station indication %pM index %d\n",
2057                                     rsp->addr2,
2058                                     rsp->sta_id);
2059                         ieee80211_report_low_ack(sta, 0);
2060                         return 0;
2061                 }
2062         }
2063
2064         wcn36xx_warn("STA with addr %pM and index %d not found\n",
2065                      rsp->addr2,
2066                      rsp->sta_id);
2067         return -ENOENT;
2068 }
2069
2070 int wcn36xx_smd_update_cfg(struct wcn36xx *wcn, u32 cfg_id, u32 value)
2071 {
2072         struct wcn36xx_hal_update_cfg_req_msg msg_body, *body;
2073         size_t len;
2074         int ret = 0;
2075
2076         mutex_lock(&wcn->hal_mutex);
2077         INIT_HAL_MSG(msg_body, WCN36XX_HAL_UPDATE_CFG_REQ);
2078
2079         PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
2080
2081         body = (struct wcn36xx_hal_update_cfg_req_msg *) wcn->hal_buf;
2082         len = msg_body.header.len;
2083
2084         put_cfg_tlv_u32(wcn, &len, cfg_id, value);
2085         body->header.len = len;
2086         body->len = len - sizeof(*body);
2087
2088         ret = wcn36xx_smd_send_and_wait(wcn, body->header.len);
2089         if (ret) {
2090                 wcn36xx_err("Sending hal_update_cfg failed\n");
2091                 goto out;
2092         }
2093         ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
2094         if (ret) {
2095                 wcn36xx_err("hal_update_cfg response failed err=%d\n", ret);
2096                 goto out;
2097         }
2098 out:
2099         mutex_unlock(&wcn->hal_mutex);
2100         return ret;
2101 }
2102 static void wcn36xx_smd_rsp_process(struct wcn36xx *wcn, void *buf, size_t len)
2103 {
2104         struct wcn36xx_hal_msg_header *msg_header = buf;
2105         struct wcn36xx_hal_ind_msg *msg_ind;
2106         wcn36xx_dbg_dump(WCN36XX_DBG_SMD_DUMP, "SMD <<< ", buf, len);
2107
2108         switch (msg_header->msg_type) {
2109         case WCN36XX_HAL_START_RSP:
2110         case WCN36XX_HAL_CONFIG_STA_RSP:
2111         case WCN36XX_HAL_CONFIG_BSS_RSP:
2112         case WCN36XX_HAL_ADD_STA_SELF_RSP:
2113         case WCN36XX_HAL_STOP_RSP:
2114         case WCN36XX_HAL_DEL_STA_SELF_RSP:
2115         case WCN36XX_HAL_DELETE_STA_RSP:
2116         case WCN36XX_HAL_INIT_SCAN_RSP:
2117         case WCN36XX_HAL_START_SCAN_RSP:
2118         case WCN36XX_HAL_END_SCAN_RSP:
2119         case WCN36XX_HAL_FINISH_SCAN_RSP:
2120         case WCN36XX_HAL_DOWNLOAD_NV_RSP:
2121         case WCN36XX_HAL_DELETE_BSS_RSP:
2122         case WCN36XX_HAL_SEND_BEACON_RSP:
2123         case WCN36XX_HAL_SET_LINK_ST_RSP:
2124         case WCN36XX_HAL_UPDATE_PROBE_RSP_TEMPLATE_RSP:
2125         case WCN36XX_HAL_SET_BSSKEY_RSP:
2126         case WCN36XX_HAL_SET_STAKEY_RSP:
2127         case WCN36XX_HAL_RMV_STAKEY_RSP:
2128         case WCN36XX_HAL_RMV_BSSKEY_RSP:
2129         case WCN36XX_HAL_ENTER_BMPS_RSP:
2130         case WCN36XX_HAL_SET_POWER_PARAMS_RSP:
2131         case WCN36XX_HAL_EXIT_BMPS_RSP:
2132         case WCN36XX_HAL_KEEP_ALIVE_RSP:
2133         case WCN36XX_HAL_DUMP_COMMAND_RSP:
2134         case WCN36XX_HAL_ADD_BA_SESSION_RSP:
2135         case WCN36XX_HAL_ADD_BA_RSP:
2136         case WCN36XX_HAL_DEL_BA_RSP:
2137         case WCN36XX_HAL_TRIGGER_BA_RSP:
2138         case WCN36XX_HAL_UPDATE_CFG_RSP:
2139         case WCN36XX_HAL_JOIN_RSP:
2140         case WCN36XX_HAL_UPDATE_SCAN_PARAM_RSP:
2141         case WCN36XX_HAL_CH_SWITCH_RSP:
2142         case WCN36XX_HAL_FEATURE_CAPS_EXCHANGE_RSP:
2143                 memcpy(wcn->hal_buf, buf, len);
2144                 wcn->hal_rsp_len = len;
2145                 complete(&wcn->hal_rsp_compl);
2146                 break;
2147
2148         case WCN36XX_HAL_COEX_IND:
2149         case WCN36XX_HAL_AVOID_FREQ_RANGE_IND:
2150         case WCN36XX_HAL_OTA_TX_COMPL_IND:
2151         case WCN36XX_HAL_MISSED_BEACON_IND:
2152         case WCN36XX_HAL_DELETE_STA_CONTEXT_IND:
2153                 msg_ind = kmalloc(sizeof(*msg_ind), GFP_KERNEL);
2154                 if (!msg_ind)
2155                         goto nomem;
2156                 msg_ind->msg_len = len;
2157                 msg_ind->msg = kmemdup(buf, len, GFP_KERNEL);
2158                 if (!msg_ind->msg) {
2159                         kfree(msg_ind);
2160 nomem:
2161                         /*
2162                          * FIXME: Do something smarter then just
2163                          * printing an error.
2164                          */
2165                         wcn36xx_err("Run out of memory while handling SMD_EVENT (%d)\n",
2166                                     msg_header->msg_type);
2167                         break;
2168                 }
2169                 mutex_lock(&wcn->hal_ind_mutex);
2170                 list_add_tail(&msg_ind->list, &wcn->hal_ind_queue);
2171                 queue_work(wcn->hal_ind_wq, &wcn->hal_ind_work);
2172                 mutex_unlock(&wcn->hal_ind_mutex);
2173                 wcn36xx_dbg(WCN36XX_DBG_HAL, "indication arrived\n");
2174                 break;
2175         default:
2176                 wcn36xx_err("SMD_EVENT (%d) not supported\n",
2177                               msg_header->msg_type);
2178         }
2179 }
2180 static void wcn36xx_ind_smd_work(struct work_struct *work)
2181 {
2182         struct wcn36xx *wcn =
2183                 container_of(work, struct wcn36xx, hal_ind_work);
2184         struct wcn36xx_hal_msg_header *msg_header;
2185         struct wcn36xx_hal_ind_msg *hal_ind_msg;
2186
2187         mutex_lock(&wcn->hal_ind_mutex);
2188
2189         hal_ind_msg = list_first_entry(&wcn->hal_ind_queue,
2190                                        struct wcn36xx_hal_ind_msg,
2191                                        list);
2192
2193         msg_header = (struct wcn36xx_hal_msg_header *)hal_ind_msg->msg;
2194
2195         switch (msg_header->msg_type) {
2196         case WCN36XX_HAL_COEX_IND:
2197         case WCN36XX_HAL_AVOID_FREQ_RANGE_IND:
2198                 break;
2199         case WCN36XX_HAL_OTA_TX_COMPL_IND:
2200                 wcn36xx_smd_tx_compl_ind(wcn,
2201                                          hal_ind_msg->msg,
2202                                          hal_ind_msg->msg_len);
2203                 break;
2204         case WCN36XX_HAL_MISSED_BEACON_IND:
2205                 wcn36xx_smd_missed_beacon_ind(wcn,
2206                                               hal_ind_msg->msg,
2207                                               hal_ind_msg->msg_len);
2208                 break;
2209         case WCN36XX_HAL_DELETE_STA_CONTEXT_IND:
2210                 wcn36xx_smd_delete_sta_context_ind(wcn,
2211                                                    hal_ind_msg->msg,
2212                                                    hal_ind_msg->msg_len);
2213                 break;
2214         default:
2215                 wcn36xx_err("SMD_EVENT (%d) not supported\n",
2216                               msg_header->msg_type);
2217         }
2218         list_del(wcn->hal_ind_queue.next);
2219         kfree(hal_ind_msg->msg);
2220         kfree(hal_ind_msg);
2221         mutex_unlock(&wcn->hal_ind_mutex);
2222 }
2223 int wcn36xx_smd_open(struct wcn36xx *wcn)
2224 {
2225         int ret = 0;
2226         wcn->hal_ind_wq = create_freezable_workqueue("wcn36xx_smd_ind");
2227         if (!wcn->hal_ind_wq) {
2228                 wcn36xx_err("failed to allocate wq\n");
2229                 ret = -ENOMEM;
2230                 goto out;
2231         }
2232         INIT_WORK(&wcn->hal_ind_work, wcn36xx_ind_smd_work);
2233         INIT_LIST_HEAD(&wcn->hal_ind_queue);
2234         mutex_init(&wcn->hal_ind_mutex);
2235
2236         ret = wcn->ctrl_ops->open(wcn, wcn36xx_smd_rsp_process);
2237         if (ret) {
2238                 wcn36xx_err("failed to open control channel\n");
2239                 goto free_wq;
2240         }
2241
2242         return ret;
2243
2244 free_wq:
2245         destroy_workqueue(wcn->hal_ind_wq);
2246 out:
2247         return ret;
2248 }
2249
2250 void wcn36xx_smd_close(struct wcn36xx *wcn)
2251 {
2252         wcn->ctrl_ops->close();
2253         destroy_workqueue(wcn->hal_ind_wq);
2254         mutex_destroy(&wcn->hal_ind_mutex);
2255 }