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