xref: /linux/drivers/net/wireless/rsi/rsi_91x_mgmt.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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 "rsi_mgmt.h"
19 #include "rsi_common.h"
20 
21 static struct bootup_params boot_params_20 = {
22 	.magic_number = cpu_to_le16(0x5aa5),
23 	.crystal_good_time = 0x0,
24 	.valid = cpu_to_le32(VALID_20),
25 	.reserved_for_valids = 0x0,
26 	.bootup_mode_info = 0x0,
27 	.digital_loop_back_params = 0x0,
28 	.rtls_timestamp_en = 0x0,
29 	.host_spi_intr_cfg = 0x0,
30 	.device_clk_info = {{
31 		.pll_config_g = {
32 			.tapll_info_g = {
33 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_20 << 8)|
34 					      (TA_PLL_M_VAL_20)),
35 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_20),
36 			},
37 			.pll960_info_g = {
38 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_20 << 8)|
39 							 (PLL960_N_VAL_20)),
40 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_20),
41 				.pll_reg_3 = 0x0,
42 			},
43 			.afepll_info_g = {
44 				.pll_reg = cpu_to_le16(0x9f0),
45 			}
46 		},
47 		.switch_clk_g = {
48 			.switch_clk_info = cpu_to_le16(BIT(3)),
49 			.bbp_lmac_clk_reg_val = cpu_to_le16(0x121),
50 			.umac_clock_reg_config = 0x0,
51 			.qspi_uart_clock_reg_config = 0x0
52 		}
53 	},
54 	{
55 		.pll_config_g = {
56 			.tapll_info_g = {
57 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_20 << 8)|
58 							 (TA_PLL_M_VAL_20)),
59 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_20),
60 			},
61 			.pll960_info_g = {
62 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_20 << 8)|
63 							 (PLL960_N_VAL_20)),
64 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_20),
65 				.pll_reg_3 = 0x0,
66 			},
67 			.afepll_info_g = {
68 				.pll_reg = cpu_to_le16(0x9f0),
69 			}
70 		},
71 		.switch_clk_g = {
72 			.switch_clk_info = 0x0,
73 			.bbp_lmac_clk_reg_val = 0x0,
74 			.umac_clock_reg_config = 0x0,
75 			.qspi_uart_clock_reg_config = 0x0
76 		}
77 	},
78 	{
79 		.pll_config_g = {
80 			.tapll_info_g = {
81 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_20 << 8)|
82 							 (TA_PLL_M_VAL_20)),
83 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_20),
84 			},
85 			.pll960_info_g = {
86 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_20 << 8)|
87 							 (PLL960_N_VAL_20)),
88 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_20),
89 				.pll_reg_3 = 0x0,
90 			},
91 			.afepll_info_g = {
92 				.pll_reg = cpu_to_le16(0x9f0),
93 			}
94 		},
95 		.switch_clk_g = {
96 			.switch_clk_info = 0x0,
97 			.bbp_lmac_clk_reg_val = 0x0,
98 			.umac_clock_reg_config = 0x0,
99 			.qspi_uart_clock_reg_config = 0x0
100 		}
101 	} },
102 	.buckboost_wakeup_cnt = 0x0,
103 	.pmu_wakeup_wait = 0x0,
104 	.shutdown_wait_time = 0x0,
105 	.pmu_slp_clkout_sel = 0x0,
106 	.wdt_prog_value = 0x0,
107 	.wdt_soc_rst_delay = 0x0,
108 	.dcdc_operation_mode = 0x0,
109 	.soc_reset_wait_cnt = 0x0
110 };
111 
112 static struct bootup_params boot_params_40 = {
113 	.magic_number = cpu_to_le16(0x5aa5),
114 	.crystal_good_time = 0x0,
115 	.valid = cpu_to_le32(VALID_40),
116 	.reserved_for_valids = 0x0,
117 	.bootup_mode_info = 0x0,
118 	.digital_loop_back_params = 0x0,
119 	.rtls_timestamp_en = 0x0,
120 	.host_spi_intr_cfg = 0x0,
121 	.device_clk_info = {{
122 		.pll_config_g = {
123 			.tapll_info_g = {
124 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_40 << 8)|
125 							 (TA_PLL_M_VAL_40)),
126 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_40),
127 			},
128 			.pll960_info_g = {
129 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_40 << 8)|
130 							 (PLL960_N_VAL_40)),
131 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_40),
132 				.pll_reg_3 = 0x0,
133 			},
134 			.afepll_info_g = {
135 				.pll_reg = cpu_to_le16(0x9f0),
136 			}
137 		},
138 		.switch_clk_g = {
139 			.switch_clk_info = cpu_to_le16(0x09),
140 			.bbp_lmac_clk_reg_val = cpu_to_le16(0x1121),
141 			.umac_clock_reg_config = cpu_to_le16(0x48),
142 			.qspi_uart_clock_reg_config = 0x0
143 		}
144 	},
145 	{
146 		.pll_config_g = {
147 			.tapll_info_g = {
148 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_40 << 8)|
149 							 (TA_PLL_M_VAL_40)),
150 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_40),
151 			},
152 			.pll960_info_g = {
153 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_40 << 8)|
154 							 (PLL960_N_VAL_40)),
155 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_40),
156 				.pll_reg_3 = 0x0,
157 			},
158 			.afepll_info_g = {
159 				.pll_reg = cpu_to_le16(0x9f0),
160 			}
161 		},
162 		.switch_clk_g = {
163 			.switch_clk_info = 0x0,
164 			.bbp_lmac_clk_reg_val = 0x0,
165 			.umac_clock_reg_config = 0x0,
166 			.qspi_uart_clock_reg_config = 0x0
167 		}
168 	},
169 	{
170 		.pll_config_g = {
171 			.tapll_info_g = {
172 				.pll_reg_1 = cpu_to_le16((TA_PLL_N_VAL_40 << 8)|
173 							 (TA_PLL_M_VAL_40)),
174 				.pll_reg_2 = cpu_to_le16(TA_PLL_P_VAL_40),
175 			},
176 			.pll960_info_g = {
177 				.pll_reg_1 = cpu_to_le16((PLL960_P_VAL_40 << 8)|
178 							 (PLL960_N_VAL_40)),
179 				.pll_reg_2 = cpu_to_le16(PLL960_M_VAL_40),
180 				.pll_reg_3 = 0x0,
181 			},
182 			.afepll_info_g = {
183 				.pll_reg = cpu_to_le16(0x9f0),
184 			}
185 		},
186 		.switch_clk_g = {
187 			.switch_clk_info = 0x0,
188 			.bbp_lmac_clk_reg_val = 0x0,
189 			.umac_clock_reg_config = 0x0,
190 			.qspi_uart_clock_reg_config = 0x0
191 		}
192 	} },
193 	.buckboost_wakeup_cnt = 0x0,
194 	.pmu_wakeup_wait = 0x0,
195 	.shutdown_wait_time = 0x0,
196 	.pmu_slp_clkout_sel = 0x0,
197 	.wdt_prog_value = 0x0,
198 	.wdt_soc_rst_delay = 0x0,
199 	.dcdc_operation_mode = 0x0,
200 	.soc_reset_wait_cnt = 0x0
201 };
202 
203 static u16 mcs[] = {13, 26, 39, 52, 78, 104, 117, 130};
204 
205 /**
206  * rsi_set_default_parameters() - This function sets default parameters.
207  * @common: Pointer to the driver private structure.
208  *
209  * Return: none
210  */
211 static void rsi_set_default_parameters(struct rsi_common *common)
212 {
213 	common->band = NL80211_BAND_2GHZ;
214 	common->channel_width = BW_20MHZ;
215 	common->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
216 	common->channel = 1;
217 	common->min_rate = 0xffff;
218 	common->fsm_state = FSM_CARD_NOT_READY;
219 	common->iface_down = true;
220 	common->endpoint = EP_2GHZ_20MHZ;
221 }
222 
223 /**
224  * rsi_set_contention_vals() - This function sets the contention values for the
225  *			       backoff procedure.
226  * @common: Pointer to the driver private structure.
227  *
228  * Return: None.
229  */
230 static void rsi_set_contention_vals(struct rsi_common *common)
231 {
232 	u8 ii = 0;
233 
234 	for (; ii < NUM_EDCA_QUEUES; ii++) {
235 		common->tx_qinfo[ii].wme_params =
236 			(((common->edca_params[ii].cw_min / 2) +
237 			  (common->edca_params[ii].aifs)) *
238 			  WMM_SHORT_SLOT_TIME + SIFS_DURATION);
239 		common->tx_qinfo[ii].weight = common->tx_qinfo[ii].wme_params;
240 		common->tx_qinfo[ii].pkt_contended = 0;
241 	}
242 }
243 
244 /**
245  * rsi_send_internal_mgmt_frame() - This function sends management frames to
246  *				    firmware.Also schedules packet to queue
247  *				    for transmission.
248  * @common: Pointer to the driver private structure.
249  * @skb: Pointer to the socket buffer structure.
250  *
251  * Return: 0 on success, -1 on failure.
252  */
253 static int rsi_send_internal_mgmt_frame(struct rsi_common *common,
254 					struct sk_buff *skb)
255 {
256 	struct skb_info *tx_params;
257 
258 	if (skb == NULL) {
259 		rsi_dbg(ERR_ZONE, "%s: Unable to allocate skb\n", __func__);
260 		return -ENOMEM;
261 	}
262 	tx_params = (struct skb_info *)&IEEE80211_SKB_CB(skb)->driver_data;
263 	tx_params->flags |= INTERNAL_MGMT_PKT;
264 	skb_queue_tail(&common->tx_queue[MGMT_SOFT_Q], skb);
265 	rsi_set_event(&common->tx_thread.event);
266 	return 0;
267 }
268 
269 /**
270  * rsi_load_radio_caps() - This function is used to send radio capabilities
271  *			   values to firmware.
272  * @common: Pointer to the driver private structure.
273  *
274  * Return: 0 on success, corresponding negative error code on failure.
275  */
276 static int rsi_load_radio_caps(struct rsi_common *common)
277 {
278 	struct rsi_radio_caps *radio_caps;
279 	struct rsi_hw *adapter = common->priv;
280 	u16 inx = 0;
281 	u8 ii;
282 	u8 radio_id = 0;
283 	u16 gc[20] = {0xf0, 0xf0, 0xf0, 0xf0,
284 		      0xf0, 0xf0, 0xf0, 0xf0,
285 		      0xf0, 0xf0, 0xf0, 0xf0,
286 		      0xf0, 0xf0, 0xf0, 0xf0,
287 		      0xf0, 0xf0, 0xf0, 0xf0};
288 	struct sk_buff *skb;
289 
290 	rsi_dbg(INFO_ZONE, "%s: Sending rate symbol req frame\n", __func__);
291 
292 	skb = dev_alloc_skb(sizeof(struct rsi_radio_caps));
293 
294 	if (!skb) {
295 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
296 			__func__);
297 		return -ENOMEM;
298 	}
299 
300 	memset(skb->data, 0, sizeof(struct rsi_radio_caps));
301 	radio_caps = (struct rsi_radio_caps *)skb->data;
302 
303 	radio_caps->desc_word[1] = cpu_to_le16(RADIO_CAPABILITIES);
304 	radio_caps->desc_word[4] = cpu_to_le16(RSI_RF_TYPE << 8);
305 
306 	if (common->channel_width == BW_40MHZ) {
307 		radio_caps->desc_word[7] |= cpu_to_le16(RSI_LMAC_CLOCK_80MHZ);
308 		radio_caps->desc_word[7] |= cpu_to_le16(RSI_ENABLE_40MHZ);
309 
310 		if (common->fsm_state == FSM_MAC_INIT_DONE) {
311 			struct ieee80211_hw *hw = adapter->hw;
312 			struct ieee80211_conf *conf = &hw->conf;
313 			if (conf_is_ht40_plus(conf)) {
314 				radio_caps->desc_word[5] =
315 					cpu_to_le16(LOWER_20_ENABLE);
316 				radio_caps->desc_word[5] |=
317 					cpu_to_le16(LOWER_20_ENABLE >> 12);
318 			} else if (conf_is_ht40_minus(conf)) {
319 				radio_caps->desc_word[5] =
320 					cpu_to_le16(UPPER_20_ENABLE);
321 				radio_caps->desc_word[5] |=
322 					cpu_to_le16(UPPER_20_ENABLE >> 12);
323 			} else {
324 				radio_caps->desc_word[5] =
325 					cpu_to_le16(BW_40MHZ << 12);
326 				radio_caps->desc_word[5] |=
327 					cpu_to_le16(FULL40M_ENABLE);
328 			}
329 		}
330 	}
331 
332 	radio_caps->sifs_tx_11n = cpu_to_le16(SIFS_TX_11N_VALUE);
333 	radio_caps->sifs_tx_11b = cpu_to_le16(SIFS_TX_11B_VALUE);
334 	radio_caps->slot_rx_11n = cpu_to_le16(SHORT_SLOT_VALUE);
335 	radio_caps->ofdm_ack_tout = cpu_to_le16(OFDM_ACK_TOUT_VALUE);
336 	radio_caps->cck_ack_tout = cpu_to_le16(CCK_ACK_TOUT_VALUE);
337 	radio_caps->preamble_type = cpu_to_le16(LONG_PREAMBLE);
338 
339 	radio_caps->desc_word[7] |= cpu_to_le16(radio_id << 8);
340 
341 	for (ii = 0; ii < MAX_HW_QUEUES; ii++) {
342 		radio_caps->qos_params[ii].cont_win_min_q = cpu_to_le16(3);
343 		radio_caps->qos_params[ii].cont_win_max_q = cpu_to_le16(0x3f);
344 		radio_caps->qos_params[ii].aifsn_val_q = cpu_to_le16(2);
345 		radio_caps->qos_params[ii].txop_q = 0;
346 	}
347 
348 	for (ii = 0; ii < MAX_HW_QUEUES - 4; ii++) {
349 		radio_caps->qos_params[ii].cont_win_min_q =
350 			cpu_to_le16(common->edca_params[ii].cw_min);
351 		radio_caps->qos_params[ii].cont_win_max_q =
352 			cpu_to_le16(common->edca_params[ii].cw_max);
353 		radio_caps->qos_params[ii].aifsn_val_q =
354 			cpu_to_le16((common->edca_params[ii].aifs) << 8);
355 		radio_caps->qos_params[ii].txop_q =
356 			cpu_to_le16(common->edca_params[ii].txop);
357 	}
358 
359 	memcpy(&common->rate_pwr[0], &gc[0], 40);
360 	for (ii = 0; ii < 20; ii++)
361 		radio_caps->gcpd_per_rate[inx++] =
362 			cpu_to_le16(common->rate_pwr[ii]  & 0x00FF);
363 
364 	radio_caps->desc_word[0] = cpu_to_le16((sizeof(struct rsi_radio_caps) -
365 						FRAME_DESC_SZ) |
366 					       (RSI_WIFI_MGMT_Q << 12));
367 
368 
369 	skb_put(skb, (sizeof(struct rsi_radio_caps)));
370 
371 	return rsi_send_internal_mgmt_frame(common, skb);
372 }
373 
374 /**
375  * rsi_mgmt_pkt_to_core() - This function is the entry point for Mgmt module.
376  * @common: Pointer to the driver private structure.
377  * @msg: Pointer to received packet.
378  * @msg_len: Length of the recieved packet.
379  * @type: Type of recieved packet.
380  *
381  * Return: 0 on success, -1 on failure.
382  */
383 static int rsi_mgmt_pkt_to_core(struct rsi_common *common,
384 				u8 *msg,
385 				s32 msg_len,
386 				u8 type)
387 {
388 	struct rsi_hw *adapter = common->priv;
389 	struct ieee80211_tx_info *info;
390 	struct skb_info *rx_params;
391 	u8 pad_bytes = msg[4];
392 	u8 pkt_recv;
393 	struct sk_buff *skb;
394 	char *buffer;
395 
396 	if (type == RX_DOT11_MGMT) {
397 		if (!adapter->sc_nvifs)
398 			return -ENOLINK;
399 
400 		msg_len -= pad_bytes;
401 		if (msg_len <= 0) {
402 			rsi_dbg(MGMT_RX_ZONE,
403 				"%s: Invalid rx msg of len = %d\n",
404 				__func__, msg_len);
405 			return -EINVAL;
406 		}
407 
408 		skb = dev_alloc_skb(msg_len);
409 		if (!skb) {
410 			rsi_dbg(ERR_ZONE, "%s: Failed to allocate skb\n",
411 				__func__);
412 			return -ENOMEM;
413 		}
414 
415 		buffer = skb_put(skb, msg_len);
416 
417 		memcpy(buffer,
418 		       (u8 *)(msg +  FRAME_DESC_SZ + pad_bytes),
419 		       msg_len);
420 
421 		pkt_recv = buffer[0];
422 
423 		info = IEEE80211_SKB_CB(skb);
424 		rx_params = (struct skb_info *)info->driver_data;
425 		rx_params->rssi = rsi_get_rssi(msg);
426 		rx_params->channel = rsi_get_channel(msg);
427 		rsi_indicate_pkt_to_os(common, skb);
428 	} else {
429 		rsi_dbg(MGMT_TX_ZONE, "%s: Internal Packet\n", __func__);
430 	}
431 
432 	return 0;
433 }
434 
435 /**
436  * rsi_hal_send_sta_notify_frame() - This function sends the station notify
437  *				     frame to firmware.
438  * @common: Pointer to the driver private structure.
439  * @opmode: Operating mode of device.
440  * @notify_event: Notification about station connection.
441  * @bssid: bssid.
442  * @qos_enable: Qos is enabled.
443  * @aid: Aid (unique for all STA).
444  *
445  * Return: status: 0 on success, corresponding negative error code on failure.
446  */
447 static int rsi_hal_send_sta_notify_frame(struct rsi_common *common,
448 					 u8 opmode,
449 					 u8 notify_event,
450 					 const unsigned char *bssid,
451 					 u8 qos_enable,
452 					 u16 aid)
453 {
454 	struct sk_buff *skb = NULL;
455 	struct rsi_peer_notify *peer_notify;
456 	u16 vap_id = 0;
457 	int status;
458 
459 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending sta notify frame\n", __func__);
460 
461 	skb = dev_alloc_skb(sizeof(struct rsi_peer_notify));
462 
463 	if (!skb) {
464 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
465 			__func__);
466 		return -ENOMEM;
467 	}
468 
469 	memset(skb->data, 0, sizeof(struct rsi_peer_notify));
470 	peer_notify = (struct rsi_peer_notify *)skb->data;
471 
472 	peer_notify->command = cpu_to_le16(opmode << 1);
473 
474 	switch (notify_event) {
475 	case STA_CONNECTED:
476 		peer_notify->command |= cpu_to_le16(RSI_ADD_PEER);
477 		break;
478 	case STA_DISCONNECTED:
479 		peer_notify->command |= cpu_to_le16(RSI_DELETE_PEER);
480 		break;
481 	default:
482 		break;
483 	}
484 
485 	peer_notify->command |= cpu_to_le16((aid & 0xfff) << 4);
486 	ether_addr_copy(peer_notify->mac_addr, bssid);
487 
488 	peer_notify->sta_flags = cpu_to_le32((qos_enable) ? 1 : 0);
489 
490 	peer_notify->desc_word[0] =
491 		cpu_to_le16((sizeof(struct rsi_peer_notify) - FRAME_DESC_SZ) |
492 			    (RSI_WIFI_MGMT_Q << 12));
493 	peer_notify->desc_word[1] = cpu_to_le16(PEER_NOTIFY);
494 	peer_notify->desc_word[7] |= cpu_to_le16(vap_id << 8);
495 
496 	skb_put(skb, sizeof(struct rsi_peer_notify));
497 
498 	status = rsi_send_internal_mgmt_frame(common, skb);
499 
500 	if (!status && qos_enable) {
501 		rsi_set_contention_vals(common);
502 		status = rsi_load_radio_caps(common);
503 	}
504 	return status;
505 }
506 
507 /**
508  * rsi_send_aggregation_params_frame() - This function sends the ampdu
509  *					 indication frame to firmware.
510  * @common: Pointer to the driver private structure.
511  * @tid: traffic identifier.
512  * @ssn: ssn.
513  * @buf_size: buffer size.
514  * @event: notification about station connection.
515  *
516  * Return: 0 on success, corresponding negative error code on failure.
517  */
518 int rsi_send_aggregation_params_frame(struct rsi_common *common,
519 				      u16 tid,
520 				      u16 ssn,
521 				      u8 buf_size,
522 				      u8 event)
523 {
524 	struct sk_buff *skb = NULL;
525 	struct rsi_mac_frame *mgmt_frame;
526 	u8 peer_id = 0;
527 
528 	skb = dev_alloc_skb(FRAME_DESC_SZ);
529 
530 	if (!skb) {
531 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
532 			__func__);
533 		return -ENOMEM;
534 	}
535 
536 	memset(skb->data, 0, FRAME_DESC_SZ);
537 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
538 
539 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending AMPDU indication frame\n", __func__);
540 
541 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
542 	mgmt_frame->desc_word[1] = cpu_to_le16(AMPDU_IND);
543 
544 	if (event == STA_TX_ADDBA_DONE) {
545 		mgmt_frame->desc_word[4] = cpu_to_le16(ssn);
546 		mgmt_frame->desc_word[5] = cpu_to_le16(buf_size);
547 		mgmt_frame->desc_word[7] =
548 		cpu_to_le16((tid | (START_AMPDU_AGGR << 4) | (peer_id << 8)));
549 	} else if (event == STA_RX_ADDBA_DONE) {
550 		mgmt_frame->desc_word[4] = cpu_to_le16(ssn);
551 		mgmt_frame->desc_word[7] = cpu_to_le16(tid |
552 						       (START_AMPDU_AGGR << 4) |
553 						       (RX_BA_INDICATION << 5) |
554 						       (peer_id << 8));
555 	} else if (event == STA_TX_DELBA) {
556 		mgmt_frame->desc_word[7] = cpu_to_le16(tid |
557 						       (STOP_AMPDU_AGGR << 4) |
558 						       (peer_id << 8));
559 	} else if (event == STA_RX_DELBA) {
560 		mgmt_frame->desc_word[7] = cpu_to_le16(tid |
561 						       (STOP_AMPDU_AGGR << 4) |
562 						       (RX_BA_INDICATION << 5) |
563 						       (peer_id << 8));
564 	}
565 
566 	skb_put(skb, FRAME_DESC_SZ);
567 
568 	return rsi_send_internal_mgmt_frame(common, skb);
569 }
570 
571 /**
572  * rsi_program_bb_rf() - This function starts base band and RF programming.
573  *			 This is called after initial configurations are done.
574  * @common: Pointer to the driver private structure.
575  *
576  * Return: 0 on success, corresponding negative error code on failure.
577  */
578 static int rsi_program_bb_rf(struct rsi_common *common)
579 {
580 	struct sk_buff *skb;
581 	struct rsi_mac_frame *mgmt_frame;
582 
583 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending program BB/RF frame\n", __func__);
584 
585 	skb = dev_alloc_skb(FRAME_DESC_SZ);
586 	if (!skb) {
587 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
588 			__func__);
589 		return -ENOMEM;
590 	}
591 
592 	memset(skb->data, 0, FRAME_DESC_SZ);
593 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
594 
595 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
596 	mgmt_frame->desc_word[1] = cpu_to_le16(BBP_PROG_IN_TA);
597 	mgmt_frame->desc_word[4] = cpu_to_le16(common->endpoint);
598 
599 	if (common->rf_reset) {
600 		mgmt_frame->desc_word[7] =  cpu_to_le16(RF_RESET_ENABLE);
601 		rsi_dbg(MGMT_TX_ZONE, "%s: ===> RF RESET REQUEST SENT <===\n",
602 			__func__);
603 		common->rf_reset = 0;
604 	}
605 	common->bb_rf_prog_count = 1;
606 	mgmt_frame->desc_word[7] |= cpu_to_le16(PUT_BBP_RESET |
607 				     BBP_REG_WRITE | (RSI_RF_TYPE << 4));
608 	skb_put(skb, FRAME_DESC_SZ);
609 
610 	return rsi_send_internal_mgmt_frame(common, skb);
611 }
612 
613 /**
614  * rsi_set_vap_capabilities() - This function send vap capability to firmware.
615  * @common: Pointer to the driver private structure.
616  * @opmode: Operating mode of device.
617  *
618  * Return: 0 on success, corresponding negative error code on failure.
619  */
620 int rsi_set_vap_capabilities(struct rsi_common *common,
621 			     enum opmode mode,
622 			     u8 vap_status)
623 {
624 	struct sk_buff *skb = NULL;
625 	struct rsi_vap_caps *vap_caps;
626 	struct rsi_hw *adapter = common->priv;
627 	struct ieee80211_hw *hw = adapter->hw;
628 	struct ieee80211_conf *conf = &hw->conf;
629 	u16 vap_id = 0;
630 
631 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending VAP capabilities frame\n", __func__);
632 
633 	skb = dev_alloc_skb(sizeof(struct rsi_vap_caps));
634 	if (!skb) {
635 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
636 			__func__);
637 		return -ENOMEM;
638 	}
639 
640 	memset(skb->data, 0, sizeof(struct rsi_vap_caps));
641 	vap_caps = (struct rsi_vap_caps *)skb->data;
642 
643 	vap_caps->desc_word[0] = cpu_to_le16((sizeof(struct rsi_vap_caps) -
644 					     FRAME_DESC_SZ) |
645 					     (RSI_WIFI_MGMT_Q << 12));
646 	vap_caps->desc_word[1] = cpu_to_le16(VAP_CAPABILITIES);
647 	vap_caps->desc_word[2] = cpu_to_le16(vap_status << 8);
648 	vap_caps->desc_word[4] = cpu_to_le16(mode |
649 					     (common->channel_width << 8));
650 	vap_caps->desc_word[7] = cpu_to_le16((vap_id << 8) |
651 					     (common->mac_id << 4) |
652 					     common->radio_id);
653 
654 	memcpy(vap_caps->mac_addr, common->mac_addr, IEEE80211_ADDR_LEN);
655 	vap_caps->keep_alive_period = cpu_to_le16(90);
656 	vap_caps->frag_threshold = cpu_to_le16(IEEE80211_MAX_FRAG_THRESHOLD);
657 
658 	vap_caps->rts_threshold = cpu_to_le16(common->rts_threshold);
659 	vap_caps->default_mgmt_rate = cpu_to_le32(RSI_RATE_6);
660 
661 	if (common->band == NL80211_BAND_5GHZ) {
662 		vap_caps->default_ctrl_rate = cpu_to_le32(RSI_RATE_6);
663 		if (conf_is_ht40(&common->priv->hw->conf)) {
664 			vap_caps->default_ctrl_rate |=
665 				cpu_to_le32(FULL40M_ENABLE << 16);
666 		}
667 	} else {
668 		vap_caps->default_ctrl_rate = cpu_to_le32(RSI_RATE_1);
669 		if (conf_is_ht40_minus(conf))
670 			vap_caps->default_ctrl_rate |=
671 				cpu_to_le32(UPPER_20_ENABLE << 16);
672 		else if (conf_is_ht40_plus(conf))
673 			vap_caps->default_ctrl_rate |=
674 				cpu_to_le32(LOWER_20_ENABLE << 16);
675 	}
676 
677 	vap_caps->default_data_rate = 0;
678 	vap_caps->beacon_interval = cpu_to_le16(200);
679 	vap_caps->dtim_period = cpu_to_le16(4);
680 
681 	skb_put(skb, sizeof(*vap_caps));
682 
683 	return rsi_send_internal_mgmt_frame(common, skb);
684 }
685 
686 /**
687  * rsi_hal_load_key() - This function is used to load keys within the firmware.
688  * @common: Pointer to the driver private structure.
689  * @data: Pointer to the key data.
690  * @key_len: Key length to be loaded.
691  * @key_type: Type of key: GROUP/PAIRWISE.
692  * @key_id: Key index.
693  * @cipher: Type of cipher used.
694  *
695  * Return: 0 on success, -1 on failure.
696  */
697 int rsi_hal_load_key(struct rsi_common *common,
698 		     u8 *data,
699 		     u16 key_len,
700 		     u8 key_type,
701 		     u8 key_id,
702 		     u32 cipher)
703 {
704 	struct sk_buff *skb = NULL;
705 	struct rsi_set_key *set_key;
706 	u16 key_descriptor = 0;
707 
708 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending load key frame\n", __func__);
709 
710 	skb = dev_alloc_skb(sizeof(struct rsi_set_key));
711 	if (!skb) {
712 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
713 			__func__);
714 		return -ENOMEM;
715 	}
716 
717 	memset(skb->data, 0, sizeof(struct rsi_set_key));
718 	set_key = (struct rsi_set_key *)skb->data;
719 
720 	if ((cipher == WLAN_CIPHER_SUITE_WEP40) ||
721 	    (cipher == WLAN_CIPHER_SUITE_WEP104)) {
722 		key_len += 1;
723 		key_descriptor |= BIT(2);
724 		if (key_len >= 13)
725 			key_descriptor |= BIT(3);
726 	} else if (cipher != KEY_TYPE_CLEAR) {
727 		key_descriptor |= BIT(4);
728 		if (key_type == RSI_PAIRWISE_KEY)
729 			key_id = 0;
730 		if (cipher == WLAN_CIPHER_SUITE_TKIP)
731 			key_descriptor |= BIT(5);
732 	}
733 	key_descriptor |= (key_type | BIT(13) | (key_id << 14));
734 
735 	set_key->desc_word[0] = cpu_to_le16((sizeof(struct rsi_set_key) -
736 					    FRAME_DESC_SZ) |
737 					    (RSI_WIFI_MGMT_Q << 12));
738 	set_key->desc_word[1] = cpu_to_le16(SET_KEY_REQ);
739 	set_key->desc_word[4] = cpu_to_le16(key_descriptor);
740 
741 	if ((cipher == WLAN_CIPHER_SUITE_WEP40) ||
742 	    (cipher == WLAN_CIPHER_SUITE_WEP104)) {
743 		memcpy(&set_key->key[key_id][1],
744 		       data,
745 		       key_len * 2);
746 	} else {
747 		memcpy(&set_key->key[0][0], data, key_len);
748 	}
749 
750 	memcpy(set_key->tx_mic_key, &data[16], 8);
751 	memcpy(set_key->rx_mic_key, &data[24], 8);
752 
753 	skb_put(skb, sizeof(struct rsi_set_key));
754 
755 	return rsi_send_internal_mgmt_frame(common, skb);
756 }
757 
758 /*
759  * rsi_load_bootup_params() - This function send bootup params to the firmware.
760  * @common: Pointer to the driver private structure.
761  *
762  * Return: 0 on success, corresponding error code on failure.
763  */
764 static int rsi_load_bootup_params(struct rsi_common *common)
765 {
766 	struct sk_buff *skb;
767 	struct rsi_boot_params *boot_params;
768 
769 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending boot params frame\n", __func__);
770 	skb = dev_alloc_skb(sizeof(struct rsi_boot_params));
771 	if (!skb) {
772 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
773 			__func__);
774 		return -ENOMEM;
775 	}
776 
777 	memset(skb->data, 0, sizeof(struct rsi_boot_params));
778 	boot_params = (struct rsi_boot_params *)skb->data;
779 
780 	rsi_dbg(MGMT_TX_ZONE, "%s:\n", __func__);
781 
782 	if (common->channel_width == BW_40MHZ) {
783 		memcpy(&boot_params->bootup_params,
784 		       &boot_params_40,
785 		       sizeof(struct bootup_params));
786 		rsi_dbg(MGMT_TX_ZONE, "%s: Packet 40MHZ <=== %d\n", __func__,
787 			UMAC_CLK_40BW);
788 		boot_params->desc_word[7] = cpu_to_le16(UMAC_CLK_40BW);
789 	} else {
790 		memcpy(&boot_params->bootup_params,
791 		       &boot_params_20,
792 		       sizeof(struct bootup_params));
793 		if (boot_params_20.valid != cpu_to_le32(VALID_20)) {
794 			boot_params->desc_word[7] = cpu_to_le16(UMAC_CLK_20BW);
795 			rsi_dbg(MGMT_TX_ZONE,
796 				"%s: Packet 20MHZ <=== %d\n", __func__,
797 				UMAC_CLK_20BW);
798 		} else {
799 			boot_params->desc_word[7] = cpu_to_le16(UMAC_CLK_40MHZ);
800 			rsi_dbg(MGMT_TX_ZONE,
801 				"%s: Packet 20MHZ <=== %d\n", __func__,
802 				UMAC_CLK_40MHZ);
803 		}
804 	}
805 
806 	/**
807 	 * Bit{0:11} indicates length of the Packet
808 	 * Bit{12:15} indicates host queue number
809 	 */
810 	boot_params->desc_word[0] = cpu_to_le16(sizeof(struct bootup_params) |
811 				    (RSI_WIFI_MGMT_Q << 12));
812 	boot_params->desc_word[1] = cpu_to_le16(BOOTUP_PARAMS_REQUEST);
813 
814 	skb_put(skb, sizeof(struct rsi_boot_params));
815 
816 	return rsi_send_internal_mgmt_frame(common, skb);
817 }
818 
819 /**
820  * rsi_send_reset_mac() - This function prepares reset MAC request and sends an
821  *			  internal management frame to indicate it to firmware.
822  * @common: Pointer to the driver private structure.
823  *
824  * Return: 0 on success, corresponding error code on failure.
825  */
826 static int rsi_send_reset_mac(struct rsi_common *common)
827 {
828 	struct sk_buff *skb;
829 	struct rsi_mac_frame *mgmt_frame;
830 
831 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending reset MAC frame\n", __func__);
832 
833 	skb = dev_alloc_skb(FRAME_DESC_SZ);
834 	if (!skb) {
835 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
836 			__func__);
837 		return -ENOMEM;
838 	}
839 
840 	memset(skb->data, 0, FRAME_DESC_SZ);
841 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
842 
843 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
844 	mgmt_frame->desc_word[1] = cpu_to_le16(RESET_MAC_REQ);
845 	mgmt_frame->desc_word[4] = cpu_to_le16(RETRY_COUNT << 8);
846 
847 	skb_put(skb, FRAME_DESC_SZ);
848 
849 	return rsi_send_internal_mgmt_frame(common, skb);
850 }
851 
852 /**
853  * rsi_band_check() - This function programs the band
854  * @common: Pointer to the driver private structure.
855  *
856  * Return: 0 on success, corresponding error code on failure.
857  */
858 int rsi_band_check(struct rsi_common *common)
859 {
860 	struct rsi_hw *adapter = common->priv;
861 	struct ieee80211_hw *hw = adapter->hw;
862 	u8 prev_bw = common->channel_width;
863 	u8 prev_ep = common->endpoint;
864 	struct ieee80211_channel *curchan = hw->conf.chandef.chan;
865 	int status = 0;
866 
867 	if (common->band != curchan->band) {
868 		common->rf_reset = 1;
869 		common->band = curchan->band;
870 	}
871 
872 	if ((hw->conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) ||
873 	    (hw->conf.chandef.width == NL80211_CHAN_WIDTH_20))
874 		common->channel_width = BW_20MHZ;
875 	else
876 		common->channel_width = BW_40MHZ;
877 
878 	if (common->band == NL80211_BAND_2GHZ) {
879 		if (common->channel_width)
880 			common->endpoint = EP_2GHZ_40MHZ;
881 		else
882 			common->endpoint = EP_2GHZ_20MHZ;
883 	} else {
884 		if (common->channel_width)
885 			common->endpoint = EP_5GHZ_40MHZ;
886 		else
887 			common->endpoint = EP_5GHZ_20MHZ;
888 	}
889 
890 	if (common->endpoint != prev_ep) {
891 		status = rsi_program_bb_rf(common);
892 		if (status)
893 			return status;
894 	}
895 
896 	if (common->channel_width != prev_bw) {
897 		status = rsi_load_bootup_params(common);
898 		if (status)
899 			return status;
900 
901 		status = rsi_load_radio_caps(common);
902 		if (status)
903 			return status;
904 	}
905 
906 	return status;
907 }
908 
909 /**
910  * rsi_set_channel() - This function programs the channel.
911  * @common: Pointer to the driver private structure.
912  * @channel: Channel value to be set.
913  *
914  * Return: 0 on success, corresponding error code on failure.
915  */
916 int rsi_set_channel(struct rsi_common *common,
917 		    struct ieee80211_channel *channel)
918 {
919 	struct sk_buff *skb = NULL;
920 	struct rsi_mac_frame *mgmt_frame;
921 
922 	rsi_dbg(MGMT_TX_ZONE,
923 		"%s: Sending scan req frame\n", __func__);
924 
925 	skb = dev_alloc_skb(FRAME_DESC_SZ);
926 	if (!skb) {
927 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
928 			__func__);
929 		return -ENOMEM;
930 	}
931 
932 	if (!channel) {
933 		dev_kfree_skb(skb);
934 		return 0;
935 	}
936 	memset(skb->data, 0, FRAME_DESC_SZ);
937 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
938 
939 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
940 	mgmt_frame->desc_word[1] = cpu_to_le16(SCAN_REQUEST);
941 	mgmt_frame->desc_word[4] = cpu_to_le16(channel->hw_value);
942 
943 	mgmt_frame->desc_word[4] |=
944 		cpu_to_le16(((char)(channel->max_antenna_gain)) << 8);
945 	mgmt_frame->desc_word[5] =
946 		cpu_to_le16((char)(channel->max_antenna_gain));
947 
948 	mgmt_frame->desc_word[7] = cpu_to_le16(PUT_BBP_RESET |
949 					       BBP_REG_WRITE |
950 					       (RSI_RF_TYPE << 4));
951 
952 	if (!(channel->flags & IEEE80211_CHAN_NO_IR) &&
953 	       !(channel->flags & IEEE80211_CHAN_RADAR)) {
954 		if (common->tx_power < channel->max_power)
955 			mgmt_frame->desc_word[6] = cpu_to_le16(common->tx_power);
956 		else
957 			mgmt_frame->desc_word[6] = cpu_to_le16(channel->max_power);
958 	}
959 	mgmt_frame->desc_word[7] = cpu_to_le16(common->priv->dfs_region);
960 
961 	if (common->channel_width == BW_40MHZ)
962 		mgmt_frame->desc_word[5] |= cpu_to_le16(0x1 << 8);
963 
964 	common->channel = channel->hw_value;
965 
966 	skb_put(skb, FRAME_DESC_SZ);
967 
968 	return rsi_send_internal_mgmt_frame(common, skb);
969 }
970 
971 /**
972  * rsi_send_radio_params_update() - This function sends the radio
973  *				parameters update to device
974  * @common: Pointer to the driver private structure.
975  * @channel: Channel value to be set.
976  *
977  * Return: 0 on success, corresponding error code on failure.
978  */
979 int rsi_send_radio_params_update(struct rsi_common *common)
980 {
981 	struct rsi_mac_frame *cmd_frame;
982 	struct sk_buff *skb = NULL;
983 
984 	rsi_dbg(MGMT_TX_ZONE,
985 		"%s: Sending Radio Params update frame\n", __func__);
986 
987 	skb = dev_alloc_skb(FRAME_DESC_SZ);
988 	if (!skb) {
989 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
990 			__func__);
991 		return -ENOMEM;
992 	}
993 
994 	memset(skb->data, 0, FRAME_DESC_SZ);
995 	cmd_frame = (struct rsi_mac_frame *)skb->data;
996 
997 	cmd_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
998 	cmd_frame->desc_word[1] = cpu_to_le16(RADIO_PARAMS_UPDATE);
999 	cmd_frame->desc_word[3] = cpu_to_le16(BIT(0));
1000 
1001 	cmd_frame->desc_word[3] |= cpu_to_le16(common->tx_power << 8);
1002 
1003 	skb_put(skb, FRAME_DESC_SZ);
1004 
1005 	return rsi_send_internal_mgmt_frame(common, skb);
1006 }
1007 
1008 /**
1009  * rsi_compare() - This function is used to compare two integers
1010  * @a: pointer to the first integer
1011  * @b: pointer to the second integer
1012  *
1013  * Return: 0 if both are equal, -1 if the first is smaller, else 1
1014  */
1015 static int rsi_compare(const void *a, const void *b)
1016 {
1017 	u16 _a = *(const u16 *)(a);
1018 	u16 _b = *(const u16 *)(b);
1019 
1020 	if (_a > _b)
1021 		return -1;
1022 
1023 	if (_a < _b)
1024 		return 1;
1025 
1026 	return 0;
1027 }
1028 
1029 /**
1030  * rsi_map_rates() - This function is used to map selected rates to hw rates.
1031  * @rate: The standard rate to be mapped.
1032  * @offset: Offset that will be returned.
1033  *
1034  * Return: 0 if it is a mcs rate, else 1
1035  */
1036 static bool rsi_map_rates(u16 rate, int *offset)
1037 {
1038 	int kk;
1039 	for (kk = 0; kk < ARRAY_SIZE(rsi_mcsrates); kk++) {
1040 		if (rate == mcs[kk]) {
1041 			*offset = kk;
1042 			return false;
1043 		}
1044 	}
1045 
1046 	for (kk = 0; kk < ARRAY_SIZE(rsi_rates); kk++) {
1047 		if (rate == rsi_rates[kk].bitrate / 5) {
1048 			*offset = kk;
1049 			break;
1050 		}
1051 	}
1052 	return true;
1053 }
1054 
1055 /**
1056  * rsi_send_auto_rate_request() - This function is to set rates for connection
1057  *				  and send autorate request to firmware.
1058  * @common: Pointer to the driver private structure.
1059  *
1060  * Return: 0 on success, corresponding error code on failure.
1061  */
1062 static int rsi_send_auto_rate_request(struct rsi_common *common)
1063 {
1064 	struct sk_buff *skb;
1065 	struct rsi_auto_rate *auto_rate;
1066 	int ii = 0, jj = 0, kk = 0;
1067 	struct ieee80211_hw *hw = common->priv->hw;
1068 	u8 band = hw->conf.chandef.chan->band;
1069 	u8 num_supported_rates = 0;
1070 	u8 rate_table_offset, rate_offset = 0;
1071 	u32 rate_bitmap = common->bitrate_mask[band];
1072 
1073 	u16 *selected_rates, min_rate;
1074 
1075 	skb = dev_alloc_skb(sizeof(struct rsi_auto_rate));
1076 	if (!skb) {
1077 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1078 			__func__);
1079 		return -ENOMEM;
1080 	}
1081 
1082 	selected_rates = kzalloc(2 * RSI_TBL_SZ, GFP_KERNEL);
1083 	if (!selected_rates) {
1084 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of mem\n",
1085 			__func__);
1086 		dev_kfree_skb(skb);
1087 		return -ENOMEM;
1088 	}
1089 
1090 	memset(skb->data, 0, sizeof(struct rsi_auto_rate));
1091 
1092 	auto_rate = (struct rsi_auto_rate *)skb->data;
1093 
1094 	auto_rate->aarf_rssi = cpu_to_le16(((u16)3 << 6) | (u16)(18 & 0x3f));
1095 	auto_rate->collision_tolerance = cpu_to_le16(3);
1096 	auto_rate->failure_limit = cpu_to_le16(3);
1097 	auto_rate->initial_boundary = cpu_to_le16(3);
1098 	auto_rate->max_threshold_limt = cpu_to_le16(27);
1099 
1100 	auto_rate->desc_word[1] = cpu_to_le16(AUTO_RATE_IND);
1101 
1102 	if (common->channel_width == BW_40MHZ)
1103 		auto_rate->desc_word[7] |= cpu_to_le16(1);
1104 
1105 	if (band == NL80211_BAND_2GHZ) {
1106 		min_rate = RSI_RATE_1;
1107 		rate_table_offset = 0;
1108 	} else {
1109 		min_rate = RSI_RATE_6;
1110 		rate_table_offset = 4;
1111 	}
1112 
1113 	for (ii = 0, jj = 0;
1114 	     ii < (ARRAY_SIZE(rsi_rates) - rate_table_offset); ii++) {
1115 		if (rate_bitmap & BIT(ii)) {
1116 			selected_rates[jj++] =
1117 			(rsi_rates[ii + rate_table_offset].bitrate / 5);
1118 			rate_offset++;
1119 		}
1120 	}
1121 	num_supported_rates = jj;
1122 
1123 	if (common->vif_info[0].is_ht) {
1124 		for (ii = 0; ii < ARRAY_SIZE(mcs); ii++)
1125 			selected_rates[jj++] = mcs[ii];
1126 		num_supported_rates += ARRAY_SIZE(mcs);
1127 		rate_offset += ARRAY_SIZE(mcs);
1128 	}
1129 
1130 	sort(selected_rates, jj, sizeof(u16), &rsi_compare, NULL);
1131 
1132 	/* mapping the rates to RSI rates */
1133 	for (ii = 0; ii < jj; ii++) {
1134 		if (rsi_map_rates(selected_rates[ii], &kk)) {
1135 			auto_rate->supported_rates[ii] =
1136 				cpu_to_le16(rsi_rates[kk].hw_value);
1137 		} else {
1138 			auto_rate->supported_rates[ii] =
1139 				cpu_to_le16(rsi_mcsrates[kk]);
1140 		}
1141 	}
1142 
1143 	/* loading HT rates in the bottom half of the auto rate table */
1144 	if (common->vif_info[0].is_ht) {
1145 		for (ii = rate_offset, kk = ARRAY_SIZE(rsi_mcsrates) - 1;
1146 		     ii < rate_offset + 2 * ARRAY_SIZE(rsi_mcsrates); ii++) {
1147 			if (common->vif_info[0].sgi ||
1148 			    conf_is_ht40(&common->priv->hw->conf))
1149 				auto_rate->supported_rates[ii++] =
1150 					cpu_to_le16(rsi_mcsrates[kk] | BIT(9));
1151 			auto_rate->supported_rates[ii] =
1152 				cpu_to_le16(rsi_mcsrates[kk--]);
1153 		}
1154 
1155 		for (; ii < (RSI_TBL_SZ - 1); ii++) {
1156 			auto_rate->supported_rates[ii] =
1157 				cpu_to_le16(rsi_mcsrates[0]);
1158 		}
1159 	}
1160 
1161 	for (; ii < RSI_TBL_SZ; ii++)
1162 		auto_rate->supported_rates[ii] = cpu_to_le16(min_rate);
1163 
1164 	auto_rate->num_supported_rates = cpu_to_le16(num_supported_rates * 2);
1165 	auto_rate->moderate_rate_inx = cpu_to_le16(num_supported_rates / 2);
1166 	auto_rate->desc_word[7] |= cpu_to_le16(0 << 8);
1167 	num_supported_rates *= 2;
1168 
1169 	auto_rate->desc_word[0] = cpu_to_le16((sizeof(*auto_rate) -
1170 					       FRAME_DESC_SZ) |
1171 					       (RSI_WIFI_MGMT_Q << 12));
1172 
1173 	skb_put(skb,
1174 		sizeof(struct rsi_auto_rate));
1175 	kfree(selected_rates);
1176 
1177 	return rsi_send_internal_mgmt_frame(common, skb);
1178 }
1179 
1180 /**
1181  * rsi_inform_bss_status() - This function informs about bss status with the
1182  *			     help of sta notify params by sending an internal
1183  *			     management frame to firmware.
1184  * @common: Pointer to the driver private structure.
1185  * @status: Bss status type.
1186  * @bssid: Bssid.
1187  * @qos_enable: Qos is enabled.
1188  * @aid: Aid (unique for all STAs).
1189  *
1190  * Return: None.
1191  */
1192 void rsi_inform_bss_status(struct rsi_common *common,
1193 			   u8 status,
1194 			   const unsigned char *bssid,
1195 			   u8 qos_enable,
1196 			   u16 aid)
1197 {
1198 	if (status) {
1199 		rsi_hal_send_sta_notify_frame(common,
1200 					      RSI_IFTYPE_STATION,
1201 					      STA_CONNECTED,
1202 					      bssid,
1203 					      qos_enable,
1204 					      aid);
1205 		if (common->min_rate == 0xffff)
1206 			rsi_send_auto_rate_request(common);
1207 	} else {
1208 		rsi_hal_send_sta_notify_frame(common,
1209 					      RSI_IFTYPE_STATION,
1210 					      STA_DISCONNECTED,
1211 					      bssid,
1212 					      qos_enable,
1213 					      aid);
1214 	}
1215 }
1216 
1217 /**
1218  * rsi_eeprom_read() - This function sends a frame to read the mac address
1219  *		       from the eeprom.
1220  * @common: Pointer to the driver private structure.
1221  *
1222  * Return: 0 on success, -1 on failure.
1223  */
1224 static int rsi_eeprom_read(struct rsi_common *common)
1225 {
1226 	struct rsi_mac_frame *mgmt_frame;
1227 	struct sk_buff *skb;
1228 
1229 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending EEPROM read req frame\n", __func__);
1230 
1231 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1232 	if (!skb) {
1233 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1234 			__func__);
1235 		return -ENOMEM;
1236 	}
1237 
1238 	memset(skb->data, 0, FRAME_DESC_SZ);
1239 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
1240 
1241 	/* FrameType */
1242 	mgmt_frame->desc_word[1] = cpu_to_le16(EEPROM_READ_TYPE);
1243 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
1244 	/* Number of bytes to read */
1245 	mgmt_frame->desc_word[3] = cpu_to_le16(ETH_ALEN +
1246 					       WLAN_MAC_MAGIC_WORD_LEN +
1247 					       WLAN_HOST_MODE_LEN +
1248 					       WLAN_FW_VERSION_LEN);
1249 	/* Address to read */
1250 	mgmt_frame->desc_word[4] = cpu_to_le16(WLAN_MAC_EEPROM_ADDR);
1251 
1252 	skb_put(skb, FRAME_DESC_SZ);
1253 
1254 	return rsi_send_internal_mgmt_frame(common, skb);
1255 }
1256 
1257 /**
1258  * This function sends a frame to block/unblock
1259  * data queues in the firmware
1260  *
1261  * @param common Pointer to the driver private structure.
1262  * @param block event - block if true, unblock if false
1263  * @return 0 on success, -1 on failure.
1264  */
1265 int rsi_send_block_unblock_frame(struct rsi_common *common, bool block_event)
1266 {
1267 	struct rsi_mac_frame *mgmt_frame;
1268 	struct sk_buff *skb;
1269 
1270 	rsi_dbg(MGMT_TX_ZONE, "%s: Sending block/unblock frame\n", __func__);
1271 
1272 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1273 	if (!skb) {
1274 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1275 			__func__);
1276 		return -ENOMEM;
1277 	}
1278 
1279 	memset(skb->data, 0, FRAME_DESC_SZ);
1280 	mgmt_frame = (struct rsi_mac_frame *)skb->data;
1281 
1282 	mgmt_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
1283 	mgmt_frame->desc_word[1] = cpu_to_le16(BLOCK_HW_QUEUE);
1284 
1285 	if (block_event) {
1286 		rsi_dbg(INFO_ZONE, "blocking the data qs\n");
1287 		mgmt_frame->desc_word[4] = cpu_to_le16(0xf);
1288 	} else {
1289 		rsi_dbg(INFO_ZONE, "unblocking the data qs\n");
1290 		mgmt_frame->desc_word[5] = cpu_to_le16(0xf);
1291 	}
1292 
1293 	skb_put(skb, FRAME_DESC_SZ);
1294 
1295 	return rsi_send_internal_mgmt_frame(common, skb);
1296 
1297 }
1298 
1299 /**
1300  * rsi_send_rx_filter_frame() - Sends a frame to filter the RX packets
1301  *
1302  * @common: Pointer to the driver private structure.
1303  * @rx_filter_word: Flags of filter packets
1304  *
1305  * @Return: 0 on success, -1 on failure.
1306  */
1307 int rsi_send_rx_filter_frame(struct rsi_common *common, u16 rx_filter_word)
1308 {
1309 	struct rsi_mac_frame *cmd_frame;
1310 	struct sk_buff *skb;
1311 
1312 	rsi_dbg(MGMT_TX_ZONE, "Sending RX filter frame\n");
1313 
1314 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1315 	if (!skb) {
1316 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1317 			__func__);
1318 		return -ENOMEM;
1319 	}
1320 
1321 	memset(skb->data, 0, FRAME_DESC_SZ);
1322 	cmd_frame = (struct rsi_mac_frame *)skb->data;
1323 
1324 	cmd_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
1325 	cmd_frame->desc_word[1] = cpu_to_le16(SET_RX_FILTER);
1326 	cmd_frame->desc_word[4] = cpu_to_le16(rx_filter_word);
1327 
1328 	skb_put(skb, FRAME_DESC_SZ);
1329 
1330 	return rsi_send_internal_mgmt_frame(common, skb);
1331 }
1332 
1333 /**
1334  * rsi_set_antenna() - This fuction send antenna configuration request
1335  *		       to device
1336  *
1337  * @common: Pointer to the driver private structure.
1338  * @antenna: bitmap for tx antenna selection
1339  *
1340  * Return: 0 on Success, negative error code on failure
1341  */
1342 int rsi_set_antenna(struct rsi_common *common, u8 antenna)
1343 {
1344 	struct rsi_mac_frame *cmd_frame;
1345 	struct sk_buff *skb;
1346 
1347 	skb = dev_alloc_skb(FRAME_DESC_SZ);
1348 	if (!skb) {
1349 		rsi_dbg(ERR_ZONE, "%s: Failed in allocation of skb\n",
1350 			__func__);
1351 		return -ENOMEM;
1352 	}
1353 
1354 	memset(skb->data, 0, FRAME_DESC_SZ);
1355 	cmd_frame = (struct rsi_mac_frame *)skb->data;
1356 
1357 	cmd_frame->desc_word[1] = cpu_to_le16(ANT_SEL_FRAME);
1358 	cmd_frame->desc_word[3] = cpu_to_le16(antenna & 0x00ff);
1359 	cmd_frame->desc_word[0] = cpu_to_le16(RSI_WIFI_MGMT_Q << 12);
1360 
1361 	skb_put(skb, FRAME_DESC_SZ);
1362 
1363 	return rsi_send_internal_mgmt_frame(common, skb);
1364 }
1365 
1366 /**
1367  * rsi_handle_ta_confirm_type() - This function handles the confirm frames.
1368  * @common: Pointer to the driver private structure.
1369  * @msg: Pointer to received packet.
1370  *
1371  * Return: 0 on success, -1 on failure.
1372  */
1373 static int rsi_handle_ta_confirm_type(struct rsi_common *common,
1374 				      u8 *msg)
1375 {
1376 	u8 sub_type = (msg[15] & 0xff);
1377 
1378 	switch (sub_type) {
1379 	case BOOTUP_PARAMS_REQUEST:
1380 		rsi_dbg(FSM_ZONE, "%s: Boot up params confirm received\n",
1381 			__func__);
1382 		if (common->fsm_state == FSM_BOOT_PARAMS_SENT) {
1383 			if (rsi_eeprom_read(common)) {
1384 				common->fsm_state = FSM_CARD_NOT_READY;
1385 				goto out;
1386 			} else {
1387 				common->fsm_state = FSM_EEPROM_READ_MAC_ADDR;
1388 			}
1389 		} else {
1390 			rsi_dbg(INFO_ZONE,
1391 				"%s: Received bootup params cfm in %d state\n",
1392 				 __func__, common->fsm_state);
1393 			return 0;
1394 		}
1395 		break;
1396 
1397 	case EEPROM_READ_TYPE:
1398 		if (common->fsm_state == FSM_EEPROM_READ_MAC_ADDR) {
1399 			if (msg[16] == MAGIC_WORD) {
1400 				u8 offset = (FRAME_DESC_SZ + WLAN_HOST_MODE_LEN
1401 					     + WLAN_MAC_MAGIC_WORD_LEN);
1402 				memcpy(common->mac_addr,
1403 				       &msg[offset],
1404 				       ETH_ALEN);
1405 				memcpy(&common->fw_ver,
1406 				       &msg[offset + ETH_ALEN],
1407 				       sizeof(struct version_info));
1408 
1409 			} else {
1410 				common->fsm_state = FSM_CARD_NOT_READY;
1411 				break;
1412 			}
1413 			if (rsi_send_reset_mac(common))
1414 				goto out;
1415 			else
1416 				common->fsm_state = FSM_RESET_MAC_SENT;
1417 		} else {
1418 			rsi_dbg(ERR_ZONE,
1419 				"%s: Received eeprom mac addr in %d state\n",
1420 				__func__, common->fsm_state);
1421 			return 0;
1422 		}
1423 		break;
1424 
1425 	case RESET_MAC_REQ:
1426 		if (common->fsm_state == FSM_RESET_MAC_SENT) {
1427 			rsi_dbg(FSM_ZONE, "%s: Reset MAC cfm received\n",
1428 				__func__);
1429 
1430 			if (rsi_load_radio_caps(common))
1431 				goto out;
1432 			else
1433 				common->fsm_state = FSM_RADIO_CAPS_SENT;
1434 		} else {
1435 			rsi_dbg(ERR_ZONE,
1436 				"%s: Received reset mac cfm in %d state\n",
1437 				 __func__, common->fsm_state);
1438 			return 0;
1439 		}
1440 		break;
1441 
1442 	case RADIO_CAPABILITIES:
1443 		if (common->fsm_state == FSM_RADIO_CAPS_SENT) {
1444 			common->rf_reset = 1;
1445 			if (rsi_program_bb_rf(common)) {
1446 				goto out;
1447 			} else {
1448 				common->fsm_state = FSM_BB_RF_PROG_SENT;
1449 				rsi_dbg(FSM_ZONE, "%s: Radio cap cfm received\n",
1450 					__func__);
1451 			}
1452 		} else {
1453 			rsi_dbg(INFO_ZONE,
1454 				"%s: Received radio caps cfm in %d state\n",
1455 				 __func__, common->fsm_state);
1456 			return 0;
1457 		}
1458 		break;
1459 
1460 	case BB_PROG_VALUES_REQUEST:
1461 	case RF_PROG_VALUES_REQUEST:
1462 	case BBP_PROG_IN_TA:
1463 		rsi_dbg(FSM_ZONE, "%s: BB/RF cfm received\n", __func__);
1464 		if (common->fsm_state == FSM_BB_RF_PROG_SENT) {
1465 			common->bb_rf_prog_count--;
1466 			if (!common->bb_rf_prog_count) {
1467 				common->fsm_state = FSM_MAC_INIT_DONE;
1468 				return rsi_mac80211_attach(common);
1469 			}
1470 		} else {
1471 			rsi_dbg(INFO_ZONE,
1472 				"%s: Received bbb_rf cfm in %d state\n",
1473 				 __func__, common->fsm_state);
1474 			return 0;
1475 		}
1476 		break;
1477 
1478 	default:
1479 		rsi_dbg(INFO_ZONE, "%s: Invalid TA confirm pkt received\n",
1480 			__func__);
1481 		break;
1482 	}
1483 	return 0;
1484 out:
1485 	rsi_dbg(ERR_ZONE, "%s: Unable to send pkt/Invalid frame received\n",
1486 		__func__);
1487 	return -EINVAL;
1488 }
1489 
1490 /**
1491  * rsi_mgmt_pkt_recv() - This function processes the management packets
1492  *			 recieved from the hardware.
1493  * @common: Pointer to the driver private structure.
1494  * @msg: Pointer to the received packet.
1495  *
1496  * Return: 0 on success, -1 on failure.
1497  */
1498 int rsi_mgmt_pkt_recv(struct rsi_common *common, u8 *msg)
1499 {
1500 	s32 msg_len = (le16_to_cpu(*(__le16 *)&msg[0]) & 0x0fff);
1501 	u16 msg_type = (msg[2]);
1502 	int ret;
1503 
1504 	rsi_dbg(FSM_ZONE, "%s: Msg Len: %d, Msg Type: %4x\n",
1505 		__func__, msg_len, msg_type);
1506 
1507 	if (msg_type == TA_CONFIRM_TYPE) {
1508 		return rsi_handle_ta_confirm_type(common, msg);
1509 	} else if (msg_type == CARD_READY_IND) {
1510 		rsi_dbg(FSM_ZONE, "%s: Card ready indication received\n",
1511 			__func__);
1512 		if (common->fsm_state == FSM_CARD_NOT_READY) {
1513 			rsi_set_default_parameters(common);
1514 
1515 			ret = rsi_load_bootup_params(common);
1516 			if (ret)
1517 				return ret;
1518 			else
1519 				common->fsm_state = FSM_BOOT_PARAMS_SENT;
1520 		} else {
1521 			return -EINVAL;
1522 		}
1523 	} else if (msg_type == TX_STATUS_IND) {
1524 		if (msg[15] == PROBEREQ_CONFIRM) {
1525 			common->mgmt_q_block = false;
1526 			rsi_dbg(FSM_ZONE, "%s: Probe confirm received\n",
1527 				__func__);
1528 		}
1529 	} else {
1530 		return rsi_mgmt_pkt_to_core(common, msg, msg_len, msg_type);
1531 	}
1532 	return 0;
1533 }
1534