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