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