1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
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
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/etherdevice.h>
18 #include <linux/timer.h>
19 #include "rsi_mgmt.h"
20 #include "rsi_common.h"
21 #include "rsi_ps.h"
22 #include "rsi_hal.h"
23 
24 static struct bootup_params boot_params_20 = {
25 	.magic_number = cpu_to_le16(0x5aa5),
26 	.crystal_good_time = 0x0,
27 	.valid = cpu_to_le32(VALID_20),
28 	.reserved_for_valids = 0x0,
29 	.bootup_mode_info = 0x0,
30 	.digital_loop_back_params = 0x0,
31 	.rtls_timestamp_en = 0x0,
32 	.host_spi_intr_cfg = 0x0,
33 	.device_clk_info = {{
34 		.pll_config_g = {
35 			.tapll_info_g = {
36 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_20 << 8)|
37 					      (TA_PLL_M_VAL_20)),
38 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_20),
39 			},
40 			.pll960_info_g = {
41 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_20 << 8)|
42 							 (PLL960_N_VAL_20)),
43 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_20),
44 				.pll_reg_3 = 0x0,
45 			},
46 			.afepll_info_g = {
47 				.pll_reg = cpu_to_le16(0x9f0),
48 			}
49 		},
50 		.switch_clk_g = {
51 			.switch_clk_info = cpu_to_le16(0xb),
52 			.bbp_lmac_clk_reg_val = cpu_to_le16(0x111),
53 			.umac_clock_reg_config = cpu_to_le16(0x48),
54 			.qspi_uart_clock_reg_config = cpu_to_le16(0x1211)
55 		}
56 	},
57 	{
58 		.pll_config_g = {
59 			.tapll_info_g = {
60 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_20 << 8)|
61 							 (TA_PLL_M_VAL_20)),
62 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_20),
63 			},
64 			.pll960_info_g = {
65 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_20 << 8)|
66 							 (PLL960_N_VAL_20)),
67 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_20),
68 				.pll_reg_3 = 0x0,
69 			},
70 			.afepll_info_g = {
71 				.pll_reg = cpu_to_le16(0x9f0),
72 			}
73 		},
74 		.switch_clk_g = {
75 			.switch_clk_info = 0x0,
76 			.bbp_lmac_clk_reg_val = 0x0,
77 			.umac_clock_reg_config = 0x0,
78 			.qspi_uart_clock_reg_config = 0x0
79 		}
80 	},
81 	{
82 		.pll_config_g = {
83 			.tapll_info_g = {
84 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_20 << 8)|
85 							 (TA_PLL_M_VAL_20)),
86 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_20),
87 			},
88 			.pll960_info_g = {
89 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_20 << 8)|
90 							 (PLL960_N_VAL_20)),
91 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_20),
92 				.pll_reg_3 = 0x0,
93 			},
94 			.afepll_info_g = {
95 				.pll_reg = cpu_to_le16(0x9f0),
96 			}
97 		},
98 		.switch_clk_g = {
99 			.switch_clk_info = 0x0,
100 			.bbp_lmac_clk_reg_val = 0x0,
101 			.umac_clock_reg_config = 0x0,
102 			.qspi_uart_clock_reg_config = 0x0
103 		}
104 	} },
105 	.buckboost_wakeup_cnt = 0x0,
106 	.pmu_wakeup_wait = 0x0,
107 	.shutdown_wait_time = 0x0,
108 	.pmu_slp_clkout_sel = 0x0,
109 	.wdt_prog_value = 0x0,
110 	.wdt_soc_rst_delay = 0x0,
111 	.dcdc_operation_mode = 0x0,
112 	.soc_reset_wait_cnt = 0x0,
113 	.waiting_time_at_fresh_sleep = 0x0,
114 	.max_threshold_to_avoid_sleep = 0x0,
115 	.beacon_resedue_alg_en = 0,
116 };
117 
118 static struct bootup_params boot_params_40 = {
119 	.magic_number = cpu_to_le16(0x5aa5),
120 	.crystal_good_time = 0x0,
121 	.valid = cpu_to_le32(VALID_40),
122 	.reserved_for_valids = 0x0,
123 	.bootup_mode_info = 0x0,
124 	.digital_loop_back_params = 0x0,
125 	.rtls_timestamp_en = 0x0,
126 	.host_spi_intr_cfg = 0x0,
127 	.device_clk_info = {{
128 		.pll_config_g = {
129 			.tapll_info_g = {
130 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_40 << 8)|
131 							 (TA_PLL_M_VAL_40)),
132 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_40),
133 			},
134 			.pll960_info_g = {
135 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_40 << 8)|
136 							 (PLL960_N_VAL_40)),
137 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_40),
138 				.pll_reg_3 = 0x0,
139 			},
140 			.afepll_info_g = {
141 				.pll_reg = cpu_to_le16(0x9f0),
142 			}
143 		},
144 		.switch_clk_g = {
145 			.switch_clk_info = cpu_to_le16(0x09),
146 			.bbp_lmac_clk_reg_val = cpu_to_le16(0x1121),
147 			.umac_clock_reg_config = cpu_to_le16(0x48),
148 			.qspi_uart_clock_reg_config = cpu_to_le16(0x1211)
149 		}
150 	},
151 	{
152 		.pll_config_g = {
153 			.tapll_info_g = {
154 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_40 << 8)|
155 							 (TA_PLL_M_VAL_40)),
156 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_40),
157 			},
158 			.pll960_info_g = {
159 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_40 << 8)|
160 							 (PLL960_N_VAL_40)),
161 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_40),
162 				.pll_reg_3 = 0x0,
163 			},
164 			.afepll_info_g = {
165 				.pll_reg = cpu_to_le16(0x9f0),
166 			}
167 		},
168 		.switch_clk_g = {
169 			.switch_clk_info = 0x0,
170 			.bbp_lmac_clk_reg_val = 0x0,
171 			.umac_clock_reg_config = 0x0,
172 			.qspi_uart_clock_reg_config = 0x0
173 		}
174 	},
175 	{
176 		.pll_config_g = {
177 			.tapll_info_g = {
178 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_40 << 8)|
179 							 (TA_PLL_M_VAL_40)),
180 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_40),
181 			},
182 			.pll960_info_g = {
183 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_40 << 8)|
184 							 (PLL960_N_VAL_40)),
185 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_40),
186 				.pll_reg_3 = 0x0,
187 			},
188 			.afepll_info_g = {
189 				.pll_reg = cpu_to_le16(0x9f0),
190 			}
191 		},
192 		.switch_clk_g = {
193 			.switch_clk_info = 0x0,
194 			.bbp_lmac_clk_reg_val = 0x0,
195 			.umac_clock_reg_config = 0x0,
196 			.qspi_uart_clock_reg_config = 0x0
197 		}
198 	} },
199 	.buckboost_wakeup_cnt = 0x0,
200 	.pmu_wakeup_wait = 0x0,
201 	.shutdown_wait_time = 0x0,
202 	.pmu_slp_clkout_sel = 0x0,
203 	.wdt_prog_value = 0x0,
204 	.wdt_soc_rst_delay = 0x0,
205 	.dcdc_operation_mode = 0x0,
206 	.soc_reset_wait_cnt = 0x0,
207 	.waiting_time_at_fresh_sleep = 0x0,
208 	.max_threshold_to_avoid_sleep = 0x0,
209 	.beacon_resedue_alg_en = 0,
210 };
211 
212 static u16 mcs[] = {13, 26, 39, 52, 78, 104, 117, 130};
213 
214 /**
215  * rsi_set_default_parameters() - This function sets default parameters.
216  * @common: Pointer to the driver private structure.
217  *
218  * Return: none
219  */
220 static void rsi_set_default_parameters(struct rsi_common *common)
221 {
222 	common->band = NL80211_BAND_2GHZ;
223 	common->channel_width = BW_20MHZ;
224 	common->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
225 	common->channel = 1;
226 	common->min_rate = 0xffff;
227 	common->fsm_state = FSM_CARD_NOT_READY;
228 	common->iface_down = true;
229 	common->endpoint = EP_2GHZ_20MHZ;
230 	common->driver_mode = 1; /* End to end mode */
231 	common->lp_ps_handshake_mode = 0; /* Default no handShake mode*/
232 	common->ulp_ps_handshake_mode = 2; /* Default PKT handShake mode*/
233 	common->rf_power_val = 0; /* Default 1.9V */
234 	common->wlan_rf_power_mode = 0;
235 	common->obm_ant_sel_val = 2;
236 	common->beacon_interval = RSI_BEACON_INTERVAL;
237 	common->dtim_cnt = RSI_DTIM_COUNT;
238 }
239 
240 void init_bgscan_params(struct rsi_common *common)
241 {
242 	memset((u8 *)&common->bgscan, 0, sizeof(struct rsi_bgscan_params));
243 	common->bgscan.bgscan_threshold = RSI_DEF_BGSCAN_THRLD;
244 	common->bgscan.roam_threshold = RSI_DEF_ROAM_THRLD;
245 	common->bgscan.bgscan_periodicity = RSI_BGSCAN_PERIODICITY;
246 	common->bgscan.num_bgscan_channels = 0;
247 	common->bgscan.two_probe = 1;
248 	common->bgscan.active_scan_duration = RSI_ACTIVE_SCAN_TIME;
249 	common->bgscan.passive_scan_duration = RSI_PASSIVE_SCAN_TIME;
250 }
251 
252 /**
253  * rsi_set_contention_vals() - This function sets the contention values for the
254  *			       backoff procedure.
255  * @common: Pointer to the driver private structure.
256  *
257  * Return: None.
258  */
259 static void rsi_set_contention_vals(struct rsi_common *common)
260 {
261 	u8 ii = 0;
262 
263 	for (; ii < NUM_EDCA_QUEUES; ii++) {
264 		common->tx_qinfo[ii].wme_params =
265 			(((common->edca_params[ii].cw_min / 2) +
266 			  (common->edca_params[ii].aifs)) *
267 			  WMM_SHORT_SLOT_TIME + SIFS_DURATION);
268 		common->tx_qinfo[ii].weight = common->tx_qinfo[ii].wme_params;
269 		common->tx_qinfo[ii].pkt_contended = 0;
270 	}
271 }
272 
273 /**
274  * rsi_send_internal_mgmt_frame() - This function sends management frames to
275  *				    firmware.Also schedules packet to queue
276  *				    for transmission.
277  * @common: Pointer to the driver private structure.
278  * @skb: Pointer to the socket buffer structure.
279  *
280  * Return: 0 on success, -1 on failure.
281  */
282 static int rsi_send_internal_mgmt_frame(struct rsi_common *common,
283 					struct sk_buff *skb)
284 {
285 	struct skb_info *tx_params;
286 	struct rsi_cmd_desc *desc;
287 
288 	if (skb == NULL) {
289 		rsi_dbg(ERR_ZONE, "%s: Unable to allocate skb\n", __func__);
290 		return -ENOMEM;
291 	}
292 	desc = (struct rsi_cmd_desc *)skb->data;
293 	desc->desc_dword0.len_qno |= cpu_to_le16(DESC_IMMEDIATE_WAKEUP);
294 	skb->priority = MGMT_SOFT_Q;
295 	tx_params = (struct skb_info *)&IEEE80211_SKB_CB(skb)->driver_data;
296 	tx_params->flags |= INTERNAL_MGMT_PKT;
297 	skb_queue_tail(&common->tx_queue[MGMT_SOFT_Q], skb);
298 	rsi_set_event(&common->tx_thread.event);
299 	return 0;
300 }
301 
302 /**
303  * rsi_load_radio_caps() - This function is used to send radio capabilities
304  *			   values to firmware.
305  * @common: Pointer to the driver private structure.
306  *
307  * Return: 0 on success, corresponding negative error code on failure.
308  */
309 static int rsi_load_radio_caps(struct rsi_common *common)
310 {
311 	struct rsi_radio_caps *radio_caps;
312 	struct rsi_hw *adapter = common->priv;
313 	u16 inx = 0;
314 	u8 ii;
315 	u8 radio_id = 0;
316 	u16 gc[20] = {0xf0, 0xf0, 0xf0, 0xf0,
317 		      0xf0, 0xf0, 0xf0, 0xf0,
318 		      0xf0, 0xf0, 0xf0, 0xf0,
319 		      0xf0, 0xf0, 0xf0, 0xf0,
320 		      0xf0, 0xf0, 0xf0, 0xf0};
321 	struct sk_buff *skb;
322 	u16 frame_len = sizeof(struct rsi_radio_caps);
323 
324 	rsi_dbg(INFO_ZONE, "%s: Sending rate symbol req frame\n", __func__);
325 
326 	skb = dev_alloc_skb(frame_len);
327 
328 	if (!skb) {
329 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
330 			__func__);
331 		return -ENOMEM;
332 	}
333 
334 	memset(skb->data, 0, frame_len);
335 	radio_caps = (struct rsi_radio_caps *)skb->data;
336 
337 	radio_caps->desc_dword0.frame_type = RADIO_CAPABILITIES;
338 	radio_caps->channel_num = common->channel;
339 	radio_caps->rf_model = RSI_RF_TYPE;
340 
341 	radio_caps->radio_cfg_info = RSI_LMAC_CLOCK_80MHZ;
342 	if (common->channel_width == BW_40MHZ) {
343 		radio_caps->radio_cfg_info |= RSI_ENABLE_40MHZ;
344 
345 		if (common->fsm_state == FSM_MAC_INIT_DONE) {
346 			struct ieee80211_hw *hw = adapter->hw;
347 			struct ieee80211_conf *conf = &hw->conf;
348 
349 			if (conf_is_ht40_plus(conf)) {
350 				radio_caps->ppe_ack_rate =
351 					cpu_to_le16(LOWER_20_ENABLE |
352 						    (LOWER_20_ENABLE >> 12));
353 			} else if (conf_is_ht40_minus(conf)) {
354 				radio_caps->ppe_ack_rate =
355 					cpu_to_le16(UPPER_20_ENABLE |
356 						    (UPPER_20_ENABLE >> 12));
357 			} else {
358 				radio_caps->ppe_ack_rate =
359 					cpu_to_le16((BW_40MHZ << 12) |
360 						    FULL40M_ENABLE);
361 			}
362 		}
363 	}
364 	radio_caps->radio_info |= radio_id;
365 
366 	radio_caps->sifs_tx_11n = cpu_to_le16(SIFS_TX_11N_VALUE);
367 	radio_caps->sifs_tx_11b = cpu_to_le16(SIFS_TX_11B_VALUE);
368 	radio_caps->slot_rx_11n = cpu_to_le16(SHORT_SLOT_VALUE);
369 	radio_caps->ofdm_ack_tout = cpu_to_le16(OFDM_ACK_TOUT_VALUE);
370 	radio_caps->cck_ack_tout = cpu_to_le16(CCK_ACK_TOUT_VALUE);
371 	radio_caps->preamble_type = cpu_to_le16(LONG_PREAMBLE);
372 
373 	for (ii = 0; ii < MAX_HW_QUEUES; ii++) {
374 		radio_caps->qos_params[ii].cont_win_min_q = cpu_to_le16(3);
375 		radio_caps->qos_params[ii].cont_win_max_q = cpu_to_le16(0x3f);
376 		radio_caps->qos_params[ii].aifsn_val_q = cpu_to_le16(2);
377 		radio_caps->qos_params[ii].txop_q = 0;
378 	}
379 
380 	for (ii = 0; ii < NUM_EDCA_QUEUES; ii++) {
381 		radio_caps->qos_params[ii].cont_win_min_q =
382 			cpu_to_le16(common->edca_params[ii].cw_min);
383 		radio_caps->qos_params[ii].cont_win_max_q =
384 			cpu_to_le16(common->edca_params[ii].cw_max);
385 		radio_caps->qos_params[ii].aifsn_val_q =
386 			cpu_to_le16((common->edca_params[ii].aifs) << 8);
387 		radio_caps->qos_params[ii].txop_q =
388 			cpu_to_le16(common->edca_params[ii].txop);
389 	}
390 
391 	radio_caps->qos_params[BROADCAST_HW_Q].txop_q = cpu_to_le16(0xffff);
392 	radio_caps->qos_params[MGMT_HW_Q].txop_q = 0;
393 	radio_caps->qos_params[BEACON_HW_Q].txop_q = cpu_to_le16(0xffff);
394 
395 	memcpy(&common->rate_pwr[0], &gc[0], 40);
396 	for (ii = 0; ii < 20; ii++)
397 		radio_caps->gcpd_per_rate[inx++] =
398 			cpu_to_le16(common->rate_pwr[ii]  & 0x00FF);
399 
400 	rsi_set_len_qno(&radio_caps->desc_dword0.len_qno,
401 			(frame_len - FRAME_DESC_SZ), RSI_WIFI_MGMT_Q);
402 
403 	skb_put(skb, frame_len);
404 
405 	return rsi_send_internal_mgmt_frame(common, skb);
406 }
407 
408 /**
409  * rsi_mgmt_pkt_to_core() - This function is the entry point for Mgmt module.
410  * @common: Pointer to the driver private structure.
411  * @msg: Pointer to received packet.
412  * @msg_len: Length of the received packet.
413  * @type: Type of received packet.
414  *
415  * Return: 0 on success, -1 on failure.
416  */
417 static int rsi_mgmt_pkt_to_core(struct rsi_common *common,
418 				u8 *msg,
419 				s32 msg_len)
420 {
421 	struct rsi_hw *adapter = common->priv;
422 	struct ieee80211_tx_info *info;
423 	struct skb_info *rx_params;
424 	u8 pad_bytes = msg[4];
425 	struct sk_buff *skb;
426 
427 	if (!adapter->sc_nvifs)
428 		return -ENOLINK;
429 
430 	msg_len -= pad_bytes;
431 	if (msg_len <= 0) {
432 		rsi_dbg(MGMT_RX_ZONE,
433 			"%s: Invalid rx msg of len = %d\n",
434 			__func__, msg_len);
435 		return -EINVAL;
436 	}
437 
438 	skb = dev_alloc_skb(msg_len);
439 	if (!skb)
440 		return -ENOMEM;
441 
442 	skb_put_data(skb,
443 		     (u8 *)(msg + FRAME_DESC_SZ + pad_bytes),
444 		     msg_len);
445 
446 	info = IEEE80211_SKB_CB(skb);
447 	rx_params = (struct skb_info *)info->driver_data;
448 	rx_params->rssi = rsi_get_rssi(msg);
449 	rx_params->channel = rsi_get_channel(msg);
450 	rsi_indicate_pkt_to_os(common, skb);
451 
452 	return 0;
453 }
454 
455 /**
456  * rsi_hal_send_sta_notify_frame() - This function sends the station notify
457  *				     frame to firmware.
458  * @common: Pointer to the driver private structure.
459  * @opmode: Operating mode of device.
460  * @notify_event: Notification about station connection.
461  * @bssid: bssid.
462  * @qos_enable: Qos is enabled.
463  * @aid: Aid (unique for all STA).
464  *
465  * Return: status: 0 on success, corresponding negative error code on failure.
466  */
467 int rsi_hal_send_sta_notify_frame(struct rsi_common *common, enum opmode opmode,
468 				  u8 notify_event, const unsigned char *bssid,
469 				  u8 qos_enable, u16 aid, u16 sta_id,
470 				  struct ieee80211_vif *vif)
471 {
472 	struct sk_buff *skb = NULL;
473 	struct rsi_peer_notify *peer_notify;
474 	u16 vap_id = ((struct vif_priv *)vif->drv_priv)->vap_id;
475 	int status;
476 	u16 frame_len = sizeof(struct rsi_peer_notify);
477 
478 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending sta notify frame\n", __func__);
479 
480 	skb = dev_alloc_skb(frame_len);
481 
482 	if (!skb) {
483 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
484 			__func__);
485 		return -ENOMEM;
486 	}
487 
488 	memset(skb->data, 0, frame_len);
489 	peer_notify = (struct rsi_peer_notify *)skb->data;
490 
491 	if (opmode == RSI_OPMODE_STA)
492 		peer_notify->command = cpu_to_le16(PEER_TYPE_AP << 1);
493 	else if (opmode == RSI_OPMODE_AP)
494 		peer_notify->command = cpu_to_le16(PEER_TYPE_STA << 1);
495 
496 	switch (notify_event) {
497 	case STA_CONNECTED:
498 		peer_notify->command |= cpu_to_le16(RSI_ADD_PEER);
499 		break;
500 	case STA_DISCONNECTED:
501 		peer_notify->command |= cpu_to_le16(RSI_DELETE_PEER);
502 		break;
503 	default:
504 		break;
505 	}
506 
507 	peer_notify->command |= cpu_to_le16((aid & 0xfff) << 4);
508 	ether_addr_copy(peer_notify->mac_addr, bssid);
509 	peer_notify->mpdu_density = cpu_to_le16(RSI_MPDU_DENSITY);
510 	peer_notify->sta_flags = cpu_to_le32((qos_enable) ? 1 : 0);
511 
512 	rsi_set_len_qno(&peer_notify->desc.desc_dword0.len_qno,
513 			(frame_len - FRAME_DESC_SZ),
514 			RSI_WIFI_MGMT_Q);
515 	peer_notify->desc.desc_dword0.frame_type = PEER_NOTIFY;
516 	peer_notify->desc.desc_dword3.qid_tid = sta_id;
517 	peer_notify->desc.desc_dword3.sta_id = vap_id;
518 
519 	skb_put(skb, frame_len);
520 
521 	status = rsi_send_internal_mgmt_frame(common, skb);
522 
523 	if ((vif->type == NL80211_IFTYPE_STATION) &&
524 	    (!status && qos_enable)) {
525 		rsi_set_contention_vals(common);
526 		status = rsi_load_radio_caps(common);
527 	}
528 	return status;
529 }
530 
531 /**
532  * rsi_send_aggregation_params_frame() - This function sends the ampdu
533  *					 indication frame to firmware.
534  * @common: Pointer to the driver private structure.
535  * @tid: traffic identifier.
536  * @ssn: ssn.
537  * @buf_size: buffer size.
538  * @event: notification about station connection.
539  *
540  * Return: 0 on success, corresponding negative error code on failure.
541  */
542 int rsi_send_aggregation_params_frame(struct rsi_common *common,
543 				      u16 tid,
544 				      u16 ssn,
545 				      u8 buf_size,
546 				      u8 event,
547 				      u8 sta_id)
548 {
549 	struct sk_buff *skb = NULL;
550 	struct rsi_aggr_params *aggr_params;
551 	u16 frame_len = sizeof(struct rsi_aggr_params);
552 
553 	skb = dev_alloc_skb(frame_len);
554 
555 	if (!skb) {
556 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
557 			__func__);
558 		return -ENOMEM;
559 	}
560 
561 	memset(skb->data, 0, frame_len);
562 	aggr_params = (struct rsi_aggr_params *)skb->data;
563 
564 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending AMPDU indication frame\n", __func__);
565 
566 	rsi_set_len_qno(&aggr_params->desc_dword0.len_qno, 0, RSI_WIFI_MGMT_Q);
567 	aggr_params->desc_dword0.frame_type = AMPDU_IND;
568 
569 	aggr_params->aggr_params = tid & RSI_AGGR_PARAMS_TID_MASK;
570 	aggr_params->peer_id = sta_id;
571 	if (event == STA_TX_ADDBA_DONE) {
572 		aggr_params->seq_start = cpu_to_le16(ssn);
573 		aggr_params->baw_size = cpu_to_le16(buf_size);
574 		aggr_params->aggr_params |= RSI_AGGR_PARAMS_START;
575 	} else if (event == STA_RX_ADDBA_DONE) {
576 		aggr_params->seq_start = cpu_to_le16(ssn);
577 		aggr_params->aggr_params |= (RSI_AGGR_PARAMS_START |
578 					     RSI_AGGR_PARAMS_RX_AGGR);
579 	} else if (event == STA_RX_DELBA) {
580 		aggr_params->aggr_params |= RSI_AGGR_PARAMS_RX_AGGR;
581 	}
582 
583 	skb_put(skb, frame_len);
584 
585 	return rsi_send_internal_mgmt_frame(common, skb);
586 }
587 
588 /**
589  * rsi_program_bb_rf() - This function starts base band and RF programming.
590  *			 This is called after initial configurations are done.
591  * @common: Pointer to the driver private structure.
592  *
593  * Return: 0 on success, corresponding negative error code on failure.
594  */
595 static int rsi_program_bb_rf(struct rsi_common *common)
596 {
597 	struct sk_buff *skb;
598 	struct rsi_bb_rf_prog *bb_rf_prog;
599 	u16 frame_len = sizeof(struct rsi_bb_rf_prog);
600 
601 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending program BB/RF frame\n", __func__);
602 
603 	skb = dev_alloc_skb(frame_len);
604 	if (!skb) {
605 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
606 			__func__);
607 		return -ENOMEM;
608 	}
609 
610 	memset(skb->data, 0, frame_len);
611 	bb_rf_prog = (struct rsi_bb_rf_prog *)skb->data;
612 
613 	rsi_set_len_qno(&bb_rf_prog->desc_dword0.len_qno, 0, RSI_WIFI_MGMT_Q);
614 	bb_rf_prog->desc_dword0.frame_type = BBP_PROG_IN_TA;
615 	bb_rf_prog->endpoint = common->endpoint;
616 	bb_rf_prog->rf_power_mode = common->wlan_rf_power_mode;
617 
618 	if (common->rf_reset) {
619 		bb_rf_prog->flags =  cpu_to_le16(RF_RESET_ENABLE);
620 		rsi_dbg(MGMT_TX_ZONE, "%s: ===> RF RESET REQUEST SENT <===\n",
621 			__func__);
622 		common->rf_reset = 0;
623 	}
624 	common->bb_rf_prog_count = 1;
625 	bb_rf_prog->flags |= cpu_to_le16(PUT_BBP_RESET | BBP_REG_WRITE |
626 					 (RSI_RF_TYPE << 4));
627 	skb_put(skb, frame_len);
628 
629 	return rsi_send_internal_mgmt_frame(common, skb);
630 }
631 
632 /**
633  * rsi_set_vap_capabilities() - This function send vap capability to firmware.
634  * @common: Pointer to the driver private structure.
635  * @opmode: Operating mode of device.
636  *
637  * Return: 0 on success, corresponding negative error code on failure.
638  */
639 int rsi_set_vap_capabilities(struct rsi_common *common,
640 			     enum opmode mode,
641 			     u8 *mac_addr,
642 			     u8 vap_id,
643 			     u8 vap_status)
644 {
645 	struct sk_buff *skb = NULL;
646 	struct rsi_vap_caps *vap_caps;
647 	struct rsi_hw *adapter = common->priv;
648 	struct ieee80211_hw *hw = adapter->hw;
649 	struct ieee80211_conf *conf = &hw->conf;
650 	u16 frame_len = sizeof(struct rsi_vap_caps);
651 
652 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending VAP capabilities frame\n", __func__);
653 
654 	skb = dev_alloc_skb(frame_len);
655 	if (!skb) {
656 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
657 			__func__);
658 		return -ENOMEM;
659 	}
660 
661 	memset(skb->data, 0, frame_len);
662 	vap_caps = (struct rsi_vap_caps *)skb->data;
663 
664 	rsi_set_len_qno(&vap_caps->desc_dword0.len_qno,
665 			(frame_len - FRAME_DESC_SZ), RSI_WIFI_MGMT_Q);
666 	vap_caps->desc_dword0.frame_type = VAP_CAPABILITIES;
667 	vap_caps->status = vap_status;
668 	vap_caps->vif_type = mode;
669 	vap_caps->channel_bw = common->channel_width;
670 	vap_caps->vap_id = vap_id;
671 	vap_caps->radioid_macid = ((common->mac_id & 0xf) << 4) |
672 				   (common->radio_id & 0xf);
673 
674 	memcpy(vap_caps->mac_addr, mac_addr, IEEE80211_ADDR_LEN);
675 	vap_caps->keep_alive_period = cpu_to_le16(90);
676 	vap_caps->frag_threshold = cpu_to_le16(IEEE80211_MAX_FRAG_THRESHOLD);
677 
678 	vap_caps->rts_threshold = cpu_to_le16(common->rts_threshold);
679 
680 	if (common->band == NL80211_BAND_5GHZ) {
681 		vap_caps->default_ctrl_rate = cpu_to_le16(RSI_RATE_6);
682 		vap_caps->default_mgmt_rate = cpu_to_le32(RSI_RATE_6);
683 	} else {
684 		vap_caps->default_ctrl_rate = cpu_to_le16(RSI_RATE_1);
685 		vap_caps->default_mgmt_rate = cpu_to_le32(RSI_RATE_1);
686 	}
687 	if (conf_is_ht40(conf)) {
688 		if (conf_is_ht40_minus(conf))
689 			vap_caps->ctrl_rate_flags =
690 				cpu_to_le16(UPPER_20_ENABLE);
691 		else if (conf_is_ht40_plus(conf))
692 			vap_caps->ctrl_rate_flags =
693 				cpu_to_le16(LOWER_20_ENABLE);
694 		else
695 			vap_caps->ctrl_rate_flags =
696 				cpu_to_le16(FULL40M_ENABLE);
697 	}
698 
699 	vap_caps->default_data_rate = 0;
700 	vap_caps->beacon_interval = cpu_to_le16(common->beacon_interval);
701 	vap_caps->dtim_period = cpu_to_le16(common->dtim_cnt);
702 
703 	skb_put(skb, frame_len);
704 
705 	return rsi_send_internal_mgmt_frame(common, skb);
706 }
707 
708 /**
709  * rsi_hal_load_key() - This function is used to load keys within the firmware.
710  * @common: Pointer to the driver private structure.
711  * @data: Pointer to the key data.
712  * @key_len: Key length to be loaded.
713  * @key_type: Type of key: GROUP/PAIRWISE.
714  * @key_id: Key index.
715  * @cipher: Type of cipher used.
716  *
717  * Return: 0 on success, -1 on failure.
718  */
719 int rsi_hal_load_key(struct rsi_common *common,
720 		     u8 *data,
721 		     u16 key_len,
722 		     u8 key_type,
723 		     u8 key_id,
724 		     u32 cipher,
725 		     s16 sta_id,
726 		     struct ieee80211_vif *vif)
727 {
728 	struct sk_buff *skb = NULL;
729 	struct rsi_set_key *set_key;
730 	u16 key_descriptor = 0;
731 	u16 frame_len = sizeof(struct rsi_set_key);
732 
733 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending load key frame\n", __func__);
734 
735 	skb = dev_alloc_skb(frame_len);
736 	if (!skb) {
737 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
738 			__func__);
739 		return -ENOMEM;
740 	}
741 
742 	memset(skb->data, 0, frame_len);
743 	set_key = (struct rsi_set_key *)skb->data;
744 
745 	if (key_type == RSI_GROUP_KEY) {
746 		key_descriptor = RSI_KEY_TYPE_BROADCAST;
747 		if (vif->type == NL80211_IFTYPE_AP)
748 			key_descriptor |= RSI_KEY_MODE_AP;
749 	}
750 	if ((cipher == WLAN_CIPHER_SUITE_WEP40) ||
751 	    (cipher == WLAN_CIPHER_SUITE_WEP104)) {
752 		key_id = 0;
753 		key_descriptor |= RSI_WEP_KEY;
754 		if (key_len >= 13)
755 			key_descriptor |= RSI_WEP_KEY_104;
756 	} else if (cipher != KEY_TYPE_CLEAR) {
757 		key_descriptor |= RSI_CIPHER_WPA;
758 		if (cipher == WLAN_CIPHER_SUITE_TKIP)
759 			key_descriptor |= RSI_CIPHER_TKIP;
760 	}
761 	key_descriptor |= RSI_PROTECT_DATA_FRAMES;
762 	key_descriptor |= (key_id << RSI_KEY_ID_OFFSET);
763 
764 	rsi_set_len_qno(&set_key->desc_dword0.len_qno,
765 			(frame_len - FRAME_DESC_SZ), RSI_WIFI_MGMT_Q);
766 	set_key->desc_dword0.frame_type = SET_KEY_REQ;
767 	set_key->key_desc = cpu_to_le16(key_descriptor);
768 	set_key->sta_id = sta_id;
769 
770 	if (data) {
771 		if ((cipher == WLAN_CIPHER_SUITE_WEP40) ||
772 		    (cipher == WLAN_CIPHER_SUITE_WEP104)) {
773 			memcpy(&set_key->key[key_id][1], data, key_len * 2);
774 		} else {
775 			memcpy(&set_key->key[0][0], data, key_len);
776 		}
777 		memcpy(set_key->tx_mic_key, &data[16], 8);
778 		memcpy(set_key->rx_mic_key, &data[24], 8);
779 	} else {
780 		memset(&set_key[FRAME_DESC_SZ], 0, frame_len - FRAME_DESC_SZ);
781 	}
782 
783 	skb_put(skb, frame_len);
784 
785 	return rsi_send_internal_mgmt_frame(common, skb);
786 }
787 
788 /*
789  * This function sends the common device configuration parameters to device.
790  * This frame includes the useful information to make device works on
791  * specific operating mode.
792  */
793 static int rsi_send_common_dev_params(struct rsi_common *common)
794 {
795 	struct sk_buff *skb;
796 	u16 frame_len;
797 	struct rsi_config_vals *dev_cfgs;
798 
799 	frame_len = sizeof(struct rsi_config_vals);
800 
801 	rsi_dbg(MGMT_TX_ZONE, "Sending common device config params\n");
802 	skb = dev_alloc_skb(frame_len);
803 	if (!skb) {
804 		rsi_dbg(ERR_ZONE, "%s: Unable to allocate skb\n", __func__);
805 		return -ENOMEM;
806 	}
807 
808 	memset(skb->data, 0, frame_len);
809 
810 	dev_cfgs = (struct rsi_config_vals *)skb->data;
811 	memset(dev_cfgs, 0, (sizeof(struct rsi_config_vals)));
812 
813 	rsi_set_len_qno(&dev_cfgs->len_qno, (frame_len - FRAME_DESC_SZ),
814 			RSI_COEX_Q);
815 	dev_cfgs->pkt_type = COMMON_DEV_CONFIG;
816 
817 	dev_cfgs->lp_ps_handshake = common->lp_ps_handshake_mode;
818 	dev_cfgs->ulp_ps_handshake = common->ulp_ps_handshake_mode;
819 
820 	dev_cfgs->unused_ulp_gpio = RSI_UNUSED_ULP_GPIO_BITMAP;
821 	dev_cfgs->unused_soc_gpio_bitmap =
822 				cpu_to_le32(RSI_UNUSED_SOC_GPIO_BITMAP);
823 
824 	dev_cfgs->opermode = common->oper_mode;
825 	dev_cfgs->wlan_rf_pwr_mode = common->wlan_rf_power_mode;
826 	dev_cfgs->driver_mode = common->driver_mode;
827 	dev_cfgs->region_code = NL80211_DFS_FCC;
828 	dev_cfgs->antenna_sel_val = common->obm_ant_sel_val;
829 
830 	skb_put(skb, frame_len);
831 
832 	return rsi_send_internal_mgmt_frame(common, skb);
833 }
834 
835 /*
836  * rsi_load_bootup_params() - This function send bootup params to the firmware.
837  * @common: Pointer to the driver private structure.
838  *
839  * Return: 0 on success, corresponding error code on failure.
840  */
841 static int rsi_load_bootup_params(struct rsi_common *common)
842 {
843 	struct sk_buff *skb;
844 	struct rsi_boot_params *boot_params;
845 
846 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending boot params frame\n", __func__);
847 	skb = dev_alloc_skb(sizeof(struct rsi_boot_params));
848 	if (!skb) {
849 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
850 			__func__);
851 		return -ENOMEM;
852 	}
853 
854 	memset(skb->data, 0, sizeof(struct rsi_boot_params));
855 	boot_params = (struct rsi_boot_params *)skb->data;
856 
857 	rsi_dbg(MGMT_TX_ZONE, "%s:\n", __func__);
858 
859 	if (common->channel_width == BW_40MHZ) {
860 		memcpy(&boot_params->bootup_params,
861 		       &boot_params_40,
862 		       sizeof(struct bootup_params));
863 		rsi_dbg(MGMT_TX_ZONE, "%s: Packet 40MHZ <=== %d\n", __func__,
864 			UMAC_CLK_40BW);
865 		boot_params->desc_word[7] = cpu_to_le16(UMAC_CLK_40BW);
866 	} else {
867 		memcpy(&boot_params->bootup_params,
868 		       &boot_params_20,
869 		       sizeof(struct bootup_params));
870 		if (boot_params_20.valid != cpu_to_le32(VALID_20)) {
871 			boot_params->desc_word[7] = cpu_to_le16(UMAC_CLK_20BW);
872 			rsi_dbg(MGMT_TX_ZONE,
873 				"%s: Packet 20MHZ <=== %d\n", __func__,
874 				UMAC_CLK_20BW);
875 		} else {
876 			boot_params->desc_word[7] = cpu_to_le16(UMAC_CLK_40MHZ);
877 			rsi_dbg(MGMT_TX_ZONE,
878 				"%s: Packet 20MHZ <=== %d\n", __func__,
879 				UMAC_CLK_40MHZ);
880 		}
881 	}
882 
883 	/**
884 	 * Bit{0:11} indicates length of the Packet
885 	 * Bit{12:15} indicates host queue number
886 	 */
887 	boot_params->desc_word[0] = cpu_to_le16(sizeof(struct bootup_params) |
888 				    (RSI_WIFI_MGMT_Q << 12));
889 	boot_params->desc_word[1] = cpu_to_le16(BOOTUP_PARAMS_REQUEST);
890 
891 	skb_put(skb, sizeof(struct rsi_boot_params));
892 
893 	return rsi_send_internal_mgmt_frame(common, skb);
894 }
895 
896 /**
897  * rsi_send_reset_mac() - This function prepares reset MAC request and sends an
898  *			  internal management frame to indicate it to firmware.
899  * @common: Pointer to the driver private structure.
900  *
901  * Return: 0 on success, corresponding error code on failure.
902  */
903 static int rsi_send_reset_mac(struct rsi_common *common)
904 {
905 	struct sk_buff *skb;
906 	struct rsi_mac_frame *mgmt_frame;
907 
908 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending reset MAC frame\n", __func__);
909 
910 	skb = dev_alloc_skb(FRAME_DESC_SZ);
911 	if (!skb) {
912 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
913 			__func__);
914 		return -ENOMEM;
915 	}
916 
917 	memset(skb->data, 0, FRAME_DESC_SZ);
918 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
919 
920 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
921 	mgmt_frame->desc_word[1] = cpu_to_le16(RESET_MAC_REQ);
922 	mgmt_frame->desc_word[4] = cpu_to_le16(RETRY_COUNT << 8);
923 
924 	skb_put(skb, FRAME_DESC_SZ);
925 
926 	return rsi_send_internal_mgmt_frame(common, skb);
927 }
928 
929 /**
930  * rsi_band_check() - This function programs the band
931  * @common: Pointer to the driver private structure.
932  *
933  * Return: 0 on success, corresponding error code on failure.
934  */
935 int rsi_band_check(struct rsi_common *common,
936 		   struct ieee80211_channel *curchan)
937 {
938 	struct rsi_hw *adapter = common->priv;
939 	struct ieee80211_hw *hw = adapter->hw;
940 	u8 prev_bw = common->channel_width;
941 	u8 prev_ep = common->endpoint;
942 	int status = 0;
943 
944 	if (common->band != curchan->band) {
945 		common->rf_reset = 1;
946 		common->band = curchan->band;
947 	}
948 
949 	if ((hw->conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) ||
950 	    (hw->conf.chandef.width == NL80211_CHAN_WIDTH_20))
951 		common->channel_width = BW_20MHZ;
952 	else
953 		common->channel_width = BW_40MHZ;
954 
955 	if (common->band == NL80211_BAND_2GHZ) {
956 		if (common->channel_width)
957 			common->endpoint = EP_2GHZ_40MHZ;
958 		else
959 			common->endpoint = EP_2GHZ_20MHZ;
960 	} else {
961 		if (common->channel_width)
962 			common->endpoint = EP_5GHZ_40MHZ;
963 		else
964 			common->endpoint = EP_5GHZ_20MHZ;
965 	}
966 
967 	if (common->endpoint != prev_ep) {
968 		status = rsi_program_bb_rf(common);
969 		if (status)
970 			return status;
971 	}
972 
973 	if (common->channel_width != prev_bw) {
974 		status = rsi_load_bootup_params(common);
975 		if (status)
976 			return status;
977 
978 		status = rsi_load_radio_caps(common);
979 		if (status)
980 			return status;
981 	}
982 
983 	return status;
984 }
985 
986 /**
987  * rsi_set_channel() - This function programs the channel.
988  * @common: Pointer to the driver private structure.
989  * @channel: Channel value to be set.
990  *
991  * Return: 0 on success, corresponding error code on failure.
992  */
993 int rsi_set_channel(struct rsi_common *common,
994 		    struct ieee80211_channel *channel)
995 {
996 	struct sk_buff *skb = NULL;
997 	struct rsi_chan_config *chan_cfg;
998 	u16 frame_len = sizeof(struct rsi_chan_config);
999 
1000 	rsi_dbg(MGMT_TX_ZONE,
1001 		"%s: Sending scan req frame\n", __func__);
1002 
1003 	skb = dev_alloc_skb(frame_len);
1004 	if (!skb) {
1005 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1006 			__func__);
1007 		return -ENOMEM;
1008 	}
1009 
1010 	if (!channel) {
1011 		dev_kfree_skb(skb);
1012 		return 0;
1013 	}
1014 	memset(skb->data, 0, frame_len);
1015 	chan_cfg = (struct rsi_chan_config *)skb->data;
1016 
1017 	rsi_set_len_qno(&chan_cfg->desc_dword0.len_qno, 0, RSI_WIFI_MGMT_Q);
1018 	chan_cfg->desc_dword0.frame_type = SCAN_REQUEST;
1019 	chan_cfg->channel_number = channel->hw_value;
1020 	chan_cfg->antenna_gain_offset_2g = channel->max_antenna_gain;
1021 	chan_cfg->antenna_gain_offset_5g = channel->max_antenna_gain;
1022 	chan_cfg->region_rftype = (RSI_RF_TYPE & 0xf) << 4;
1023 
1024 	if ((channel->flags & IEEE80211_CHAN_NO_IR) ||
1025 	    (channel->flags & IEEE80211_CHAN_RADAR)) {
1026 		chan_cfg->antenna_gain_offset_2g |= RSI_CHAN_RADAR;
1027 	} else {
1028 		if (common->tx_power < channel->max_power)
1029 			chan_cfg->tx_power = cpu_to_le16(common->tx_power);
1030 		else
1031 			chan_cfg->tx_power = cpu_to_le16(channel->max_power);
1032 	}
1033 	chan_cfg->region_rftype |= (common->priv->dfs_region & 0xf);
1034 
1035 	if (common->channel_width == BW_40MHZ)
1036 		chan_cfg->channel_width = 0x1;
1037 
1038 	common->channel = channel->hw_value;
1039 
1040 	skb_put(skb, frame_len);
1041 
1042 	return rsi_send_internal_mgmt_frame(common, skb);
1043 }
1044 
1045 /**
1046  * rsi_send_radio_params_update() - This function sends the radio
1047  *				parameters update to device
1048  * @common: Pointer to the driver private structure.
1049  * @channel: Channel value to be set.
1050  *
1051  * Return: 0 on success, corresponding error code on failure.
1052  */
1053 int rsi_send_radio_params_update(struct rsi_common *common)
1054 {
1055 	struct rsi_mac_frame *cmd_frame;
1056 	struct sk_buff *skb = NULL;
1057 
1058 	rsi_dbg(MGMT_TX_ZONE,
1059 		"%s: Sending Radio Params update frame\n", __func__);
1060 
1061 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1062 	if (!skb) {
1063 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1064 			__func__);
1065 		return -ENOMEM;
1066 	}
1067 
1068 	memset(skb->data, 0, FRAME_DESC_SZ);
1069 	cmd_frame = (struct rsi_mac_frame *)skb->data;
1070 
1071 	cmd_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
1072 	cmd_frame->desc_word[1] = cpu_to_le16(RADIO_PARAMS_UPDATE);
1073 	cmd_frame->desc_word[3] = cpu_to_le16(BIT(0));
1074 
1075 	cmd_frame->desc_word[3] |= cpu_to_le16(common->tx_power << 8);
1076 
1077 	skb_put(skb, FRAME_DESC_SZ);
1078 
1079 	return rsi_send_internal_mgmt_frame(common, skb);
1080 }
1081 
1082 /* This function programs the threshold. */
1083 int rsi_send_vap_dynamic_update(struct rsi_common *common)
1084 {
1085 	struct sk_buff *skb;
1086 	struct rsi_dynamic_s *dynamic_frame;
1087 
1088 	rsi_dbg(MGMT_TX_ZONE,
1089 		"%s: Sending vap update indication frame\n", __func__);
1090 
1091 	skb = dev_alloc_skb(sizeof(struct rsi_dynamic_s));
1092 	if (!skb)
1093 		return -ENOMEM;
1094 
1095 	memset(skb->data, 0, sizeof(struct rsi_dynamic_s));
1096 	dynamic_frame = (struct rsi_dynamic_s *)skb->data;
1097 	rsi_set_len_qno(&dynamic_frame->desc_dword0.len_qno,
1098 			sizeof(dynamic_frame->frame_body), RSI_WIFI_MGMT_Q);
1099 
1100 	dynamic_frame->desc_dword0.frame_type = VAP_DYNAMIC_UPDATE;
1101 	dynamic_frame->desc_dword2.pkt_info =
1102 					cpu_to_le32(common->rts_threshold);
1103 
1104 	if (common->wow_flags & RSI_WOW_ENABLED) {
1105 		/* Beacon miss threshold */
1106 		dynamic_frame->desc_dword3.token =
1107 					cpu_to_le16(RSI_BCN_MISS_THRESHOLD);
1108 		dynamic_frame->frame_body.keep_alive_period =
1109 					cpu_to_le16(RSI_WOW_KEEPALIVE);
1110 	} else {
1111 		dynamic_frame->frame_body.keep_alive_period =
1112 					cpu_to_le16(RSI_DEF_KEEPALIVE);
1113 	}
1114 
1115 	dynamic_frame->desc_dword3.sta_id = 0; /* vap id */
1116 
1117 	skb_put(skb, sizeof(struct rsi_dynamic_s));
1118 
1119 	return rsi_send_internal_mgmt_frame(common, skb);
1120 }
1121 
1122 /**
1123  * rsi_compare() - This function is used to compare two integers
1124  * @a: pointer to the first integer
1125  * @b: pointer to the second integer
1126  *
1127  * Return: 0 if both are equal, -1 if the first is smaller, else 1
1128  */
1129 static int rsi_compare(const void *a, const void *b)
1130 {
1131 	u16 _a = *(const u16 *)(a);
1132 	u16 _b = *(const u16 *)(b);
1133 
1134 	if (_a > _b)
1135 		return -1;
1136 
1137 	if (_a < _b)
1138 		return 1;
1139 
1140 	return 0;
1141 }
1142 
1143 /**
1144  * rsi_map_rates() - This function is used to map selected rates to hw rates.
1145  * @rate: The standard rate to be mapped.
1146  * @offset: Offset that will be returned.
1147  *
1148  * Return: 0 if it is a mcs rate, else 1
1149  */
1150 static bool rsi_map_rates(u16 rate, int *offset)
1151 {
1152 	int kk;
1153 	for (kk = 0; kk < ARRAY_SIZE(rsi_mcsrates); kk++) {
1154 		if (rate == mcs[kk]) {
1155 			*offset = kk;
1156 			return false;
1157 		}
1158 	}
1159 
1160 	for (kk = 0; kk < ARRAY_SIZE(rsi_rates); kk++) {
1161 		if (rate == rsi_rates[kk].bitrate / 5) {
1162 			*offset = kk;
1163 			break;
1164 		}
1165 	}
1166 	return true;
1167 }
1168 
1169 /**
1170  * rsi_send_auto_rate_request() - This function is to set rates for connection
1171  *				  and send autorate request to firmware.
1172  * @common: Pointer to the driver private structure.
1173  *
1174  * Return: 0 on success, corresponding error code on failure.
1175  */
1176 static int rsi_send_auto_rate_request(struct rsi_common *common,
1177 				      struct ieee80211_sta *sta,
1178 				      u16 sta_id,
1179 				      struct ieee80211_vif *vif)
1180 {
1181 	struct sk_buff *skb;
1182 	struct rsi_auto_rate *auto_rate;
1183 	int ii = 0, jj = 0, kk = 0;
1184 	struct ieee80211_hw *hw = common->priv->hw;
1185 	u8 band = hw->conf.chandef.chan->band;
1186 	u8 num_supported_rates = 0;
1187 	u8 rate_table_offset, rate_offset = 0;
1188 	u32 rate_bitmap;
1189 	u16 *selected_rates, min_rate;
1190 	bool is_ht = false, is_sgi = false;
1191 	u16 frame_len = sizeof(struct rsi_auto_rate);
1192 
1193 	rsi_dbg(MGMT_TX_ZONE,
1194 		"%s: Sending auto rate request frame\n", __func__);
1195 
1196 	skb = dev_alloc_skb(frame_len);
1197 	if (!skb) {
1198 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1199 			__func__);
1200 		return -ENOMEM;
1201 	}
1202 
1203 	memset(skb->data, 0, frame_len);
1204 	selected_rates = kzalloc(2 * RSI_TBL_SZ, GFP_KERNEL);
1205 	if (!selected_rates) {
1206 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of mem\n",
1207 			__func__);
1208 		dev_kfree_skb(skb);
1209 		return -ENOMEM;
1210 	}
1211 
1212 	auto_rate = (struct rsi_auto_rate *)skb->data;
1213 
1214 	auto_rate->aarf_rssi = cpu_to_le16(((u16)3 << 6) | (u16)(18 & 0x3f));
1215 	auto_rate->collision_tolerance = cpu_to_le16(3);
1216 	auto_rate->failure_limit = cpu_to_le16(3);
1217 	auto_rate->initial_boundary = cpu_to_le16(3);
1218 	auto_rate->max_threshold_limt = cpu_to_le16(27);
1219 
1220 	auto_rate->desc.desc_dword0.frame_type = AUTO_RATE_IND;
1221 
1222 	if (common->channel_width == BW_40MHZ)
1223 		auto_rate->desc.desc_dword3.qid_tid = BW_40MHZ;
1224 	auto_rate->desc.desc_dword3.sta_id = sta_id;
1225 
1226 	if (vif->type == NL80211_IFTYPE_STATION) {
1227 		rate_bitmap = common->bitrate_mask[band];
1228 		is_ht = common->vif_info[0].is_ht;
1229 		is_sgi = common->vif_info[0].sgi;
1230 	} else {
1231 		rate_bitmap = sta->supp_rates[band];
1232 		is_ht = sta->ht_cap.ht_supported;
1233 		if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ||
1234 		    (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
1235 			is_sgi = true;
1236 	}
1237 
1238 	if (band == NL80211_BAND_2GHZ) {
1239 		if ((rate_bitmap == 0) && (is_ht))
1240 			min_rate = RSI_RATE_MCS0;
1241 		else
1242 			min_rate = RSI_RATE_1;
1243 		rate_table_offset = 0;
1244 	} else {
1245 		if ((rate_bitmap == 0) && (is_ht))
1246 			min_rate = RSI_RATE_MCS0;
1247 		else
1248 			min_rate = RSI_RATE_6;
1249 		rate_table_offset = 4;
1250 	}
1251 
1252 	for (ii = 0, jj = 0;
1253 	     ii < (ARRAY_SIZE(rsi_rates) - rate_table_offset); ii++) {
1254 		if (rate_bitmap & BIT(ii)) {
1255 			selected_rates[jj++] =
1256 			(rsi_rates[ii + rate_table_offset].bitrate / 5);
1257 			rate_offset++;
1258 		}
1259 	}
1260 	num_supported_rates = jj;
1261 
1262 	if (is_ht) {
1263 		for (ii = 0; ii < ARRAY_SIZE(mcs); ii++)
1264 			selected_rates[jj++] = mcs[ii];
1265 		num_supported_rates += ARRAY_SIZE(mcs);
1266 		rate_offset += ARRAY_SIZE(mcs);
1267 	}
1268 
1269 	sort(selected_rates, jj, sizeof(u16), &rsi_compare, NULL);
1270 
1271 	/* mapping the rates to RSI rates */
1272 	for (ii = 0; ii < jj; ii++) {
1273 		if (rsi_map_rates(selected_rates[ii], &kk)) {
1274 			auto_rate->supported_rates[ii] =
1275 				cpu_to_le16(rsi_rates[kk].hw_value);
1276 		} else {
1277 			auto_rate->supported_rates[ii] =
1278 				cpu_to_le16(rsi_mcsrates[kk]);
1279 		}
1280 	}
1281 
1282 	/* loading HT rates in the bottom half of the auto rate table */
1283 	if (is_ht) {
1284 		for (ii = rate_offset, kk = ARRAY_SIZE(rsi_mcsrates) - 1;
1285 		     ii < rate_offset + 2 * ARRAY_SIZE(rsi_mcsrates); ii++) {
1286 			if (is_sgi || conf_is_ht40(&common->priv->hw->conf))
1287 				auto_rate->supported_rates[ii++] =
1288 					cpu_to_le16(rsi_mcsrates[kk] | BIT(9));
1289 			else
1290 				auto_rate->supported_rates[ii++] =
1291 					cpu_to_le16(rsi_mcsrates[kk]);
1292 			auto_rate->supported_rates[ii] =
1293 				cpu_to_le16(rsi_mcsrates[kk--]);
1294 		}
1295 
1296 		for (; ii < (RSI_TBL_SZ - 1); ii++) {
1297 			auto_rate->supported_rates[ii] =
1298 				cpu_to_le16(rsi_mcsrates[0]);
1299 		}
1300 	}
1301 
1302 	for (; ii < RSI_TBL_SZ; ii++)
1303 		auto_rate->supported_rates[ii] = cpu_to_le16(min_rate);
1304 
1305 	auto_rate->num_supported_rates = cpu_to_le16(num_supported_rates * 2);
1306 	auto_rate->moderate_rate_inx = cpu_to_le16(num_supported_rates / 2);
1307 	num_supported_rates *= 2;
1308 
1309 	rsi_set_len_qno(&auto_rate->desc.desc_dword0.len_qno,
1310 			(frame_len - FRAME_DESC_SZ), RSI_WIFI_MGMT_Q);
1311 
1312 	skb_put(skb, frame_len);
1313 	kfree(selected_rates);
1314 
1315 	return rsi_send_internal_mgmt_frame(common, skb);
1316 }
1317 
1318 /**
1319  * rsi_inform_bss_status() - This function informs about bss status with the
1320  *			     help of sta notify params by sending an internal
1321  *			     management frame to firmware.
1322  * @common: Pointer to the driver private structure.
1323  * @status: Bss status type.
1324  * @bssid: Bssid.
1325  * @qos_enable: Qos is enabled.
1326  * @aid: Aid (unique for all STAs).
1327  *
1328  * Return: None.
1329  */
1330 void rsi_inform_bss_status(struct rsi_common *common,
1331 			   enum opmode opmode,
1332 			   u8 status,
1333 			   const u8 *addr,
1334 			   u8 qos_enable,
1335 			   u16 aid,
1336 			   struct ieee80211_sta *sta,
1337 			   u16 sta_id,
1338 			   u16 assoc_cap,
1339 			   struct ieee80211_vif *vif)
1340 {
1341 	if (status) {
1342 		if (opmode == RSI_OPMODE_STA)
1343 			common->hw_data_qs_blocked = true;
1344 		rsi_hal_send_sta_notify_frame(common,
1345 					      opmode,
1346 					      STA_CONNECTED,
1347 					      addr,
1348 					      qos_enable,
1349 					      aid, sta_id,
1350 					      vif);
1351 		if (common->min_rate == 0xffff)
1352 			rsi_send_auto_rate_request(common, sta, sta_id, vif);
1353 		if (opmode == RSI_OPMODE_STA &&
1354 		    !(assoc_cap & WLAN_CAPABILITY_PRIVACY) &&
1355 		    !rsi_send_block_unblock_frame(common, false))
1356 			common->hw_data_qs_blocked = false;
1357 	} else {
1358 		if (opmode == RSI_OPMODE_STA)
1359 			common->hw_data_qs_blocked = true;
1360 
1361 		if (!(common->wow_flags & RSI_WOW_ENABLED))
1362 			rsi_hal_send_sta_notify_frame(common, opmode,
1363 						      STA_DISCONNECTED, addr,
1364 						      qos_enable, aid, sta_id,
1365 						      vif);
1366 		if (opmode == RSI_OPMODE_STA)
1367 			rsi_send_block_unblock_frame(common, true);
1368 	}
1369 }
1370 
1371 /**
1372  * rsi_eeprom_read() - This function sends a frame to read the mac address
1373  *		       from the eeprom.
1374  * @common: Pointer to the driver private structure.
1375  *
1376  * Return: 0 on success, -1 on failure.
1377  */
1378 static int rsi_eeprom_read(struct rsi_common *common)
1379 {
1380 	struct rsi_eeprom_read_frame *mgmt_frame;
1381 	struct rsi_hw *adapter = common->priv;
1382 	struct sk_buff *skb;
1383 
1384 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending EEPROM read req frame\n", __func__);
1385 
1386 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1387 	if (!skb) {
1388 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1389 			__func__);
1390 		return -ENOMEM;
1391 	}
1392 
1393 	memset(skb->data, 0, FRAME_DESC_SZ);
1394 	mgmt_frame = (struct rsi_eeprom_read_frame *)skb->data;
1395 
1396 	/* FrameType */
1397 	rsi_set_len_qno(&mgmt_frame->len_qno, 0, RSI_WIFI_MGMT_Q);
1398 	mgmt_frame->pkt_type = EEPROM_READ;
1399 
1400 	/* Number of bytes to read */
1401 	mgmt_frame->pkt_info =
1402 		cpu_to_le32((adapter->eeprom.length << RSI_EEPROM_LEN_OFFSET) &
1403 			    RSI_EEPROM_LEN_MASK);
1404 	mgmt_frame->pkt_info |= cpu_to_le32((3 << RSI_EEPROM_HDR_SIZE_OFFSET) &
1405 					    RSI_EEPROM_HDR_SIZE_MASK);
1406 
1407 	/* Address to read */
1408 	mgmt_frame->eeprom_offset = cpu_to_le32(adapter->eeprom.offset);
1409 
1410 	skb_put(skb, FRAME_DESC_SZ);
1411 
1412 	return rsi_send_internal_mgmt_frame(common, skb);
1413 }
1414 
1415 /**
1416  * This function sends a frame to block/unblock
1417  * data queues in the firmware
1418  *
1419  * @param common Pointer to the driver private structure.
1420  * @param block event - block if true, unblock if false
1421  * @return 0 on success, -1 on failure.
1422  */
1423 int rsi_send_block_unblock_frame(struct rsi_common *common, bool block_event)
1424 {
1425 	struct rsi_block_unblock_data *mgmt_frame;
1426 	struct sk_buff *skb;
1427 
1428 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending block/unblock frame\n", __func__);
1429 
1430 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1431 	if (!skb) {
1432 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1433 			__func__);
1434 		return -ENOMEM;
1435 	}
1436 
1437 	memset(skb->data, 0, FRAME_DESC_SZ);
1438 	mgmt_frame = (struct rsi_block_unblock_data *)skb->data;
1439 
1440 	rsi_set_len_qno(&mgmt_frame->desc_dword0.len_qno, 0, RSI_WIFI_MGMT_Q);
1441 	mgmt_frame->desc_dword0.frame_type = BLOCK_HW_QUEUE;
1442 	mgmt_frame->host_quiet_info = QUIET_INFO_VALID;
1443 
1444 	if (block_event) {
1445 		rsi_dbg(INFO_ZONE, "blocking the data qs\n");
1446 		mgmt_frame->block_q_bitmap = cpu_to_le16(0xf);
1447 		mgmt_frame->block_q_bitmap |= cpu_to_le16(0xf << 4);
1448 	} else {
1449 		rsi_dbg(INFO_ZONE, "unblocking the data qs\n");
1450 		mgmt_frame->unblock_q_bitmap = cpu_to_le16(0xf);
1451 		mgmt_frame->unblock_q_bitmap |= cpu_to_le16(0xf << 4);
1452 	}
1453 
1454 	skb_put(skb, FRAME_DESC_SZ);
1455 
1456 	return rsi_send_internal_mgmt_frame(common, skb);
1457 }
1458 
1459 /**
1460  * rsi_send_rx_filter_frame() - Sends a frame to filter the RX packets
1461  *
1462  * @common: Pointer to the driver private structure.
1463  * @rx_filter_word: Flags of filter packets
1464  *
1465  * @Return: 0 on success, -1 on failure.
1466  */
1467 int rsi_send_rx_filter_frame(struct rsi_common *common, u16 rx_filter_word)
1468 {
1469 	struct rsi_mac_frame *cmd_frame;
1470 	struct sk_buff *skb;
1471 
1472 	rsi_dbg(MGMT_TX_ZONE, "Sending RX filter frame\n");
1473 
1474 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1475 	if (!skb) {
1476 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1477 			__func__);
1478 		return -ENOMEM;
1479 	}
1480 
1481 	memset(skb->data, 0, FRAME_DESC_SZ);
1482 	cmd_frame = (struct rsi_mac_frame *)skb->data;
1483 
1484 	cmd_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
1485 	cmd_frame->desc_word[1] = cpu_to_le16(SET_RX_FILTER);
1486 	cmd_frame->desc_word[4] = cpu_to_le16(rx_filter_word);
1487 
1488 	skb_put(skb, FRAME_DESC_SZ);
1489 
1490 	return rsi_send_internal_mgmt_frame(common, skb);
1491 }
1492 
1493 int rsi_send_ps_request(struct rsi_hw *adapter, bool enable,
1494 			struct ieee80211_vif *vif)
1495 {
1496 	struct rsi_common *common = adapter->priv;
1497 	struct ieee80211_bss_conf *bss = &vif->bss_conf;
1498 	struct rsi_request_ps *ps;
1499 	struct rsi_ps_info *ps_info;
1500 	struct sk_buff *skb;
1501 	int frame_len = sizeof(*ps);
1502 
1503 	skb = dev_alloc_skb(frame_len);
1504 	if (!skb)
1505 		return -ENOMEM;
1506 	memset(skb->data, 0, frame_len);
1507 
1508 	ps = (struct rsi_request_ps *)skb->data;
1509 	ps_info = &adapter->ps_info;
1510 
1511 	rsi_set_len_qno(&ps->desc.desc_dword0.len_qno,
1512 			(frame_len - FRAME_DESC_SZ), RSI_WIFI_MGMT_Q);
1513 	ps->desc.desc_dword0.frame_type = WAKEUP_SLEEP_REQUEST;
1514 	if (enable) {
1515 		ps->ps_sleep.enable = RSI_PS_ENABLE;
1516 		ps->desc.desc_dword3.token = cpu_to_le16(RSI_SLEEP_REQUEST);
1517 	} else {
1518 		ps->ps_sleep.enable = RSI_PS_DISABLE;
1519 		ps->desc.desc_dword0.len_qno |= cpu_to_le16(RSI_PS_DISABLE_IND);
1520 		ps->desc.desc_dword3.token = cpu_to_le16(RSI_WAKEUP_REQUEST);
1521 	}
1522 
1523 	ps->ps_uapsd_acs = common->uapsd_bitmap;
1524 
1525 	ps->ps_sleep.sleep_type = ps_info->sleep_type;
1526 	ps->ps_sleep.num_bcns_per_lis_int =
1527 		cpu_to_le16(ps_info->num_bcns_per_lis_int);
1528 	ps->ps_sleep.sleep_duration =
1529 		cpu_to_le32(ps_info->deep_sleep_wakeup_period);
1530 
1531 	if (bss->assoc)
1532 		ps->ps_sleep.connected_sleep = RSI_CONNECTED_SLEEP;
1533 	else
1534 		ps->ps_sleep.connected_sleep = RSI_DEEP_SLEEP;
1535 
1536 	ps->ps_listen_interval = cpu_to_le32(ps_info->listen_interval);
1537 	ps->ps_dtim_interval_duration =
1538 		cpu_to_le32(ps_info->dtim_interval_duration);
1539 
1540 	if (ps_info->listen_interval > ps_info->dtim_interval_duration)
1541 		ps->ps_listen_interval = cpu_to_le32(RSI_PS_DISABLE);
1542 
1543 	ps->ps_num_dtim_intervals = cpu_to_le16(ps_info->num_dtims_per_sleep);
1544 	skb_put(skb, frame_len);
1545 
1546 	return rsi_send_internal_mgmt_frame(common, skb);
1547 }
1548 
1549 /**
1550  * rsi_set_antenna() - This function send antenna configuration request
1551  *		       to device
1552  *
1553  * @common: Pointer to the driver private structure.
1554  * @antenna: bitmap for tx antenna selection
1555  *
1556  * Return: 0 on Success, negative error code on failure
1557  */
1558 int rsi_set_antenna(struct rsi_common *common, u8 antenna)
1559 {
1560 	struct rsi_ant_sel_frame *ant_sel_frame;
1561 	struct sk_buff *skb;
1562 
1563 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1564 	if (!skb) {
1565 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1566 			__func__);
1567 		return -ENOMEM;
1568 	}
1569 
1570 	memset(skb->data, 0, FRAME_DESC_SZ);
1571 
1572 	ant_sel_frame = (struct rsi_ant_sel_frame *)skb->data;
1573 	ant_sel_frame->desc_dword0.frame_type = ANT_SEL_FRAME;
1574 	ant_sel_frame->sub_frame_type = ANTENNA_SEL_TYPE;
1575 	ant_sel_frame->ant_value = cpu_to_le16(antenna & ANTENNA_MASK_VALUE);
1576 	rsi_set_len_qno(&ant_sel_frame->desc_dword0.len_qno,
1577 			0, RSI_WIFI_MGMT_Q);
1578 	skb_put(skb, FRAME_DESC_SZ);
1579 
1580 	return rsi_send_internal_mgmt_frame(common, skb);
1581 }
1582 
1583 static int rsi_send_beacon(struct rsi_common *common)
1584 {
1585 	struct sk_buff *skb = NULL;
1586 	u8 dword_align_bytes = 0;
1587 
1588 	skb = dev_alloc_skb(MAX_MGMT_PKT_SIZE);
1589 	if (!skb)
1590 		return -ENOMEM;
1591 
1592 	memset(skb->data, 0, MAX_MGMT_PKT_SIZE);
1593 
1594 	dword_align_bytes = ((unsigned long)skb->data & 0x3f);
1595 	if (dword_align_bytes)
1596 		skb_pull(skb, (64 - dword_align_bytes));
1597 	if (rsi_prepare_beacon(common, skb)) {
1598 		rsi_dbg(ERR_ZONE, "Failed to prepare beacon\n");
1599 		return -EINVAL;
1600 	}
1601 	skb_queue_tail(&common->tx_queue[MGMT_BEACON_Q], skb);
1602 	rsi_set_event(&common->tx_thread.event);
1603 	rsi_dbg(DATA_TX_ZONE, "%s: Added to beacon queue\n", __func__);
1604 
1605 	return 0;
1606 }
1607 
1608 #ifdef CONFIG_PM
1609 int rsi_send_wowlan_request(struct rsi_common *common, u16 flags,
1610 			    u16 sleep_status)
1611 {
1612 	struct rsi_wowlan_req *cmd_frame;
1613 	struct sk_buff *skb;
1614 	u8 length;
1615 
1616 	rsi_dbg(ERR_ZONE, "%s: Sending wowlan request frame\n", __func__);
1617 
1618 	length = sizeof(*cmd_frame);
1619 	skb = dev_alloc_skb(length);
1620 	if (!skb)
1621 		return -ENOMEM;
1622 	memset(skb->data, 0, length);
1623 	cmd_frame = (struct rsi_wowlan_req *)skb->data;
1624 
1625 	rsi_set_len_qno(&cmd_frame->desc.desc_dword0.len_qno,
1626 			(length - FRAME_DESC_SZ),
1627 			RSI_WIFI_MGMT_Q);
1628 	cmd_frame->desc.desc_dword0.frame_type = WOWLAN_CONFIG_PARAMS;
1629 	cmd_frame->host_sleep_status = sleep_status;
1630 	if (common->secinfo.security_enable &&
1631 	    common->secinfo.gtk_cipher)
1632 		flags |= RSI_WOW_GTK_REKEY;
1633 	if (sleep_status)
1634 		cmd_frame->wow_flags = flags;
1635 	rsi_dbg(INFO_ZONE, "Host_Sleep_Status : %d Flags : %d\n",
1636 		cmd_frame->host_sleep_status, cmd_frame->wow_flags);
1637 
1638 	skb_put(skb, length);
1639 
1640 	return rsi_send_internal_mgmt_frame(common, skb);
1641 }
1642 #endif
1643 
1644 int rsi_send_bgscan_params(struct rsi_common *common, int enable)
1645 {
1646 	struct rsi_bgscan_params *params = &common->bgscan;
1647 	struct cfg80211_scan_request *scan_req = common->hwscan;
1648 	struct rsi_bgscan_config *bgscan;
1649 	struct sk_buff *skb;
1650 	u16 frame_len = sizeof(*bgscan);
1651 	u8 i;
1652 
1653 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending bgscan params frame\n", __func__);
1654 
1655 	skb = dev_alloc_skb(frame_len);
1656 	if (!skb)
1657 		return -ENOMEM;
1658 	memset(skb->data, 0, frame_len);
1659 
1660 	bgscan = (struct rsi_bgscan_config *)skb->data;
1661 	rsi_set_len_qno(&bgscan->desc_dword0.len_qno,
1662 			(frame_len - FRAME_DESC_SZ), RSI_WIFI_MGMT_Q);
1663 	bgscan->desc_dword0.frame_type = BG_SCAN_PARAMS;
1664 	bgscan->bgscan_threshold = cpu_to_le16(params->bgscan_threshold);
1665 	bgscan->roam_threshold = cpu_to_le16(params->roam_threshold);
1666 	if (enable)
1667 		bgscan->bgscan_periodicity =
1668 			cpu_to_le16(params->bgscan_periodicity);
1669 	bgscan->active_scan_duration =
1670 			cpu_to_le16(params->active_scan_duration);
1671 	bgscan->passive_scan_duration =
1672 			cpu_to_le16(params->passive_scan_duration);
1673 	bgscan->two_probe = params->two_probe;
1674 
1675 	bgscan->num_bgscan_channels = scan_req->n_channels;
1676 	for (i = 0; i < bgscan->num_bgscan_channels; i++)
1677 		bgscan->channels2scan[i] =
1678 			cpu_to_le16(scan_req->channels[i]->hw_value);
1679 
1680 	skb_put(skb, frame_len);
1681 
1682 	return rsi_send_internal_mgmt_frame(common, skb);
1683 }
1684 
1685 /* This function sends the probe request to be used by firmware in
1686  * background scan
1687  */
1688 int rsi_send_bgscan_probe_req(struct rsi_common *common,
1689 			      struct ieee80211_vif *vif)
1690 {
1691 	struct cfg80211_scan_request *scan_req = common->hwscan;
1692 	struct rsi_bgscan_probe *bgscan;
1693 	struct sk_buff *skb;
1694 	struct sk_buff *probereq_skb;
1695 	u16 frame_len = sizeof(*bgscan);
1696 	size_t ssid_len = 0;
1697 	u8 *ssid = NULL;
1698 
1699 	rsi_dbg(MGMT_TX_ZONE,
1700 		"%s: Sending bgscan probe req frame\n", __func__);
1701 
1702 	if (common->priv->sc_nvifs <= 0)
1703 		return -ENODEV;
1704 
1705 	if (scan_req->n_ssids) {
1706 		ssid = scan_req->ssids[0].ssid;
1707 		ssid_len = scan_req->ssids[0].ssid_len;
1708 	}
1709 
1710 	skb = dev_alloc_skb(frame_len + MAX_BGSCAN_PROBE_REQ_LEN);
1711 	if (!skb)
1712 		return -ENOMEM;
1713 	memset(skb->data, 0, frame_len + MAX_BGSCAN_PROBE_REQ_LEN);
1714 
1715 	bgscan = (struct rsi_bgscan_probe *)skb->data;
1716 	bgscan->desc_dword0.frame_type = BG_SCAN_PROBE_REQ;
1717 	bgscan->flags = cpu_to_le16(HOST_BG_SCAN_TRIG);
1718 	if (common->band == NL80211_BAND_5GHZ) {
1719 		bgscan->mgmt_rate = cpu_to_le16(RSI_RATE_6);
1720 		bgscan->def_chan = cpu_to_le16(40);
1721 	} else {
1722 		bgscan->mgmt_rate = cpu_to_le16(RSI_RATE_1);
1723 		bgscan->def_chan = cpu_to_le16(11);
1724 	}
1725 	bgscan->channel_scan_time = cpu_to_le16(RSI_CHANNEL_SCAN_TIME);
1726 
1727 	probereq_skb = ieee80211_probereq_get(common->priv->hw, vif->addr, ssid,
1728 					      ssid_len, scan_req->ie_len);
1729 	if (!probereq_skb) {
1730 		dev_kfree_skb(skb);
1731 		return -ENOMEM;
1732 	}
1733 
1734 	memcpy(&skb->data[frame_len], probereq_skb->data, probereq_skb->len);
1735 
1736 	bgscan->probe_req_length = cpu_to_le16(probereq_skb->len);
1737 
1738 	rsi_set_len_qno(&bgscan->desc_dword0.len_qno,
1739 			(frame_len - FRAME_DESC_SZ + probereq_skb->len),
1740 			RSI_WIFI_MGMT_Q);
1741 
1742 	skb_put(skb, frame_len + probereq_skb->len);
1743 
1744 	dev_kfree_skb(probereq_skb);
1745 
1746 	return rsi_send_internal_mgmt_frame(common, skb);
1747 }
1748 
1749 /**
1750  * rsi_handle_ta_confirm_type() - This function handles the confirm frames.
1751  * @common: Pointer to the driver private structure.
1752  * @msg: Pointer to received packet.
1753  *
1754  * Return: 0 on success, -1 on failure.
1755  */
1756 static int rsi_handle_ta_confirm_type(struct rsi_common *common,
1757 				      u8 *msg)
1758 {
1759 	struct rsi_hw *adapter = common->priv;
1760 	u8 sub_type = (msg[15] & 0xff);
1761 	u16 msg_len = ((u16 *)msg)[0] & 0xfff;
1762 	u8 offset;
1763 
1764 	switch (sub_type) {
1765 	case BOOTUP_PARAMS_REQUEST:
1766 		rsi_dbg(FSM_ZONE, "%s: Boot up params confirm received\n",
1767 			__func__);
1768 		if (common->fsm_state == FSM_BOOT_PARAMS_SENT) {
1769 			adapter->eeprom.length = (IEEE80211_ADDR_LEN +
1770 						  WLAN_MAC_MAGIC_WORD_LEN +
1771 						  WLAN_HOST_MODE_LEN);
1772 			adapter->eeprom.offset = WLAN_MAC_EEPROM_ADDR;
1773 			if (rsi_eeprom_read(common)) {
1774 				common->fsm_state = FSM_CARD_NOT_READY;
1775 				goto out;
1776 			}
1777 			common->fsm_state = FSM_EEPROM_READ_MAC_ADDR;
1778 		} else {
1779 			rsi_dbg(INFO_ZONE,
1780 				"%s: Received bootup params cfm in %d state\n",
1781 				 __func__, common->fsm_state);
1782 			return 0;
1783 		}
1784 		break;
1785 
1786 	case EEPROM_READ:
1787 		rsi_dbg(FSM_ZONE, "EEPROM READ confirm received\n");
1788 		if (msg_len <= 0) {
1789 			rsi_dbg(FSM_ZONE,
1790 				"%s: [EEPROM_READ] Invalid len %d\n",
1791 				__func__, msg_len);
1792 			goto out;
1793 		}
1794 		if (msg[16] != MAGIC_WORD) {
1795 			rsi_dbg(FSM_ZONE,
1796 				"%s: [EEPROM_READ] Invalid token\n", __func__);
1797 			common->fsm_state = FSM_CARD_NOT_READY;
1798 			goto out;
1799 		}
1800 		if (common->fsm_state == FSM_EEPROM_READ_MAC_ADDR) {
1801 			offset = (FRAME_DESC_SZ + WLAN_HOST_MODE_LEN +
1802 				  WLAN_MAC_MAGIC_WORD_LEN);
1803 			memcpy(common->mac_addr, &msg[offset], ETH_ALEN);
1804 			adapter->eeprom.length =
1805 				((WLAN_MAC_MAGIC_WORD_LEN + 3) & (~3));
1806 			adapter->eeprom.offset = WLAN_EEPROM_RFTYPE_ADDR;
1807 			if (rsi_eeprom_read(common)) {
1808 				rsi_dbg(ERR_ZONE,
1809 					"%s: Failed reading RF band\n",
1810 					__func__);
1811 				common->fsm_state = FSM_CARD_NOT_READY;
1812 				goto out;
1813 			}
1814 			common->fsm_state = FSM_EEPROM_READ_RF_TYPE;
1815 		} else if (common->fsm_state == FSM_EEPROM_READ_RF_TYPE) {
1816 			if ((msg[17] & 0x3) == 0x3) {
1817 				rsi_dbg(INIT_ZONE, "Dual band supported\n");
1818 				common->band = NL80211_BAND_5GHZ;
1819 				common->num_supp_bands = 2;
1820 			} else if ((msg[17] & 0x3) == 0x1) {
1821 				rsi_dbg(INIT_ZONE,
1822 					"Only 2.4Ghz band supported\n");
1823 				common->band = NL80211_BAND_2GHZ;
1824 				common->num_supp_bands = 1;
1825 			}
1826 			if (rsi_send_reset_mac(common))
1827 				goto out;
1828 			common->fsm_state = FSM_RESET_MAC_SENT;
1829 		} else {
1830 			rsi_dbg(ERR_ZONE, "%s: Invalid EEPROM read type\n",
1831 				__func__);
1832 			return 0;
1833 		}
1834 		break;
1835 
1836 	case RESET_MAC_REQ:
1837 		if (common->fsm_state == FSM_RESET_MAC_SENT) {
1838 			rsi_dbg(FSM_ZONE, "%s: Reset MAC cfm received\n",
1839 				__func__);
1840 
1841 			if (rsi_load_radio_caps(common))
1842 				goto out;
1843 			else
1844 				common->fsm_state = FSM_RADIO_CAPS_SENT;
1845 		} else {
1846 			rsi_dbg(ERR_ZONE,
1847 				"%s: Received reset mac cfm in %d state\n",
1848 				 __func__, common->fsm_state);
1849 			return 0;
1850 		}
1851 		break;
1852 
1853 	case RADIO_CAPABILITIES:
1854 		if (common->fsm_state == FSM_RADIO_CAPS_SENT) {
1855 			common->rf_reset = 1;
1856 			if (rsi_program_bb_rf(common)) {
1857 				goto out;
1858 			} else {
1859 				common->fsm_state = FSM_BB_RF_PROG_SENT;
1860 				rsi_dbg(FSM_ZONE, "%s: Radio cap cfm received\n",
1861 					__func__);
1862 			}
1863 		} else {
1864 			rsi_dbg(INFO_ZONE,
1865 				"%s: Received radio caps cfm in %d state\n",
1866 				 __func__, common->fsm_state);
1867 			return 0;
1868 		}
1869 		break;
1870 
1871 	case BB_PROG_VALUES_REQUEST:
1872 	case RF_PROG_VALUES_REQUEST:
1873 	case BBP_PROG_IN_TA:
1874 		rsi_dbg(FSM_ZONE, "%s: BB/RF cfm received\n", __func__);
1875 		if (common->fsm_state == FSM_BB_RF_PROG_SENT) {
1876 			common->bb_rf_prog_count--;
1877 			if (!common->bb_rf_prog_count) {
1878 				common->fsm_state = FSM_MAC_INIT_DONE;
1879 				if (common->reinit_hw) {
1880 					complete(&common->wlan_init_completion);
1881 				} else {
1882 					return rsi_mac80211_attach(common);
1883 				}
1884 			}
1885 		} else {
1886 			rsi_dbg(INFO_ZONE,
1887 				"%s: Received bbb_rf cfm in %d state\n",
1888 				 __func__, common->fsm_state);
1889 			return 0;
1890 		}
1891 		break;
1892 
1893 	case SCAN_REQUEST:
1894 		rsi_dbg(INFO_ZONE, "Set channel confirm\n");
1895 		break;
1896 
1897 	case WAKEUP_SLEEP_REQUEST:
1898 		rsi_dbg(INFO_ZONE, "Wakeup/Sleep confirmation.\n");
1899 		return rsi_handle_ps_confirm(adapter, msg);
1900 
1901 	case BG_SCAN_PROBE_REQ:
1902 		rsi_dbg(INFO_ZONE, "BG scan complete event\n");
1903 		if (common->bgscan_en) {
1904 			struct cfg80211_scan_info info;
1905 
1906 			if (!rsi_send_bgscan_params(common, RSI_STOP_BGSCAN))
1907 				common->bgscan_en = 0;
1908 			info.aborted = false;
1909 			ieee80211_scan_completed(adapter->hw, &info);
1910 		}
1911 		rsi_dbg(INFO_ZONE, "Background scan completed\n");
1912 		break;
1913 
1914 	default:
1915 		rsi_dbg(INFO_ZONE, "%s: Invalid TA confirm pkt received\n",
1916 			__func__);
1917 		break;
1918 	}
1919 	return 0;
1920 out:
1921 	rsi_dbg(ERR_ZONE, "%s: Unable to send pkt/Invalid frame received\n",
1922 		__func__);
1923 	return -EINVAL;
1924 }
1925 
1926 int rsi_handle_card_ready(struct rsi_common *common, u8 *msg)
1927 {
1928 	switch (common->fsm_state) {
1929 	case FSM_CARD_NOT_READY:
1930 		rsi_dbg(INIT_ZONE, "Card ready indication from Common HAL\n");
1931 		rsi_set_default_parameters(common);
1932 		if (rsi_send_common_dev_params(common) < 0)
1933 			return -EINVAL;
1934 		common->fsm_state = FSM_COMMON_DEV_PARAMS_SENT;
1935 		break;
1936 	case FSM_COMMON_DEV_PARAMS_SENT:
1937 		rsi_dbg(INIT_ZONE, "Card ready indication from WLAN HAL\n");
1938 
1939 		/* Get usb buffer status register address */
1940 		common->priv->usb_buffer_status_reg = *(u32 *)&msg[8];
1941 		rsi_dbg(INFO_ZONE, "USB buffer status register = %x\n",
1942 			common->priv->usb_buffer_status_reg);
1943 
1944 		if (rsi_load_bootup_params(common)) {
1945 			common->fsm_state = FSM_CARD_NOT_READY;
1946 			return -EINVAL;
1947 		}
1948 		common->fsm_state = FSM_BOOT_PARAMS_SENT;
1949 		break;
1950 	default:
1951 		rsi_dbg(ERR_ZONE,
1952 			"%s: card ready indication in invalid state %d.\n",
1953 			__func__, common->fsm_state);
1954 		return -EINVAL;
1955 	}
1956 
1957 	return 0;
1958 }
1959 
1960 /**
1961  * rsi_mgmt_pkt_recv() - This function processes the management packets
1962  *			 received from the hardware.
1963  * @common: Pointer to the driver private structure.
1964  * @msg: Pointer to the received packet.
1965  *
1966  * Return: 0 on success, -1 on failure.
1967  */
1968 int rsi_mgmt_pkt_recv(struct rsi_common *common, u8 *msg)
1969 {
1970 	s32 msg_len = (le16_to_cpu(*(__le16 *)&msg[0]) & 0x0fff);
1971 	u16 msg_type = (msg[2]);
1972 
1973 	rsi_dbg(FSM_ZONE, "%s: Msg Len: %d, Msg Type: %4x\n",
1974 		__func__, msg_len, msg_type);
1975 
1976 	switch (msg_type) {
1977 	case TA_CONFIRM_TYPE:
1978 		return rsi_handle_ta_confirm_type(common, msg);
1979 	case CARD_READY_IND:
1980 		common->hibernate_resume = false;
1981 		rsi_dbg(FSM_ZONE, "%s: Card ready indication received\n",
1982 			__func__);
1983 		return rsi_handle_card_ready(common, msg);
1984 	case TX_STATUS_IND:
1985 		switch (msg[RSI_TX_STATUS_TYPE]) {
1986 		case PROBEREQ_CONFIRM:
1987 			common->mgmt_q_block = false;
1988 			rsi_dbg(FSM_ZONE, "%s: Probe confirm received\n",
1989 				__func__);
1990 			break;
1991 		case EAPOL4_CONFIRM:
1992 			if (msg[RSI_TX_STATUS]) {
1993 				common->eapol4_confirm = true;
1994 				if (!rsi_send_block_unblock_frame(common,
1995 								  false))
1996 					common->hw_data_qs_blocked = false;
1997 			}
1998 		}
1999 		break;
2000 	case BEACON_EVENT_IND:
2001 		rsi_dbg(INFO_ZONE, "Beacon event\n");
2002 		if (common->fsm_state != FSM_MAC_INIT_DONE)
2003 			return -1;
2004 		if (common->iface_down)
2005 			return -1;
2006 		if (!common->beacon_enabled)
2007 			return -1;
2008 		rsi_send_beacon(common);
2009 		break;
2010 	case WOWLAN_WAKEUP_REASON:
2011 		rsi_dbg(ERR_ZONE, "\n\nWakeup Type: %x\n", msg[15]);
2012 		switch (msg[15]) {
2013 		case RSI_UNICAST_MAGIC_PKT:
2014 			rsi_dbg(ERR_ZONE,
2015 				"*** Wakeup for Unicast magic packet ***\n");
2016 			break;
2017 		case RSI_BROADCAST_MAGICPKT:
2018 			rsi_dbg(ERR_ZONE,
2019 				"*** Wakeup for Broadcast magic packet ***\n");
2020 			break;
2021 		case RSI_EAPOL_PKT:
2022 			rsi_dbg(ERR_ZONE,
2023 				"*** Wakeup for GTK renewal ***\n");
2024 			break;
2025 		case RSI_DISCONNECT_PKT:
2026 			rsi_dbg(ERR_ZONE,
2027 				"*** Wakeup for Disconnect ***\n");
2028 			break;
2029 		case RSI_HW_BMISS_PKT:
2030 			rsi_dbg(ERR_ZONE,
2031 				"*** Wakeup for HW Beacon miss ***\n");
2032 			break;
2033 		default:
2034 			rsi_dbg(ERR_ZONE,
2035 				"##### Un-intentional Wakeup #####\n");
2036 			break;
2037 	}
2038 	break;
2039 	case RX_DOT11_MGMT:
2040 		return rsi_mgmt_pkt_to_core(common, msg, msg_len);
2041 	default:
2042 		rsi_dbg(INFO_ZONE, "Received packet type: 0x%x\n", msg_type);
2043 	}
2044 	return 0;
2045 }
2046