blob: de36057c383ce02f3323a3010eb602b8dc2776ea [file] [log] [blame]
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001/* ////////////////////////////////////////////////////////////////////////// */
2/* */
3/* Copyright (c) Atmel Corporation. All rights reserved. */
4/* */
5/* Module Name: wilc_wlan.c */
6/* */
7/* */
8/* //////////////////////////////////////////////////////////////////////////// */
9
10#include "wilc_wlan_if.h"
11#include "wilc_wlan.h"
Johnny Kimc5c77ba2015-05-11 14:30:56 +090012
13/********************************************
14 *
15 * Global
16 *
17 ********************************************/
Johnny Kimc5c77ba2015-05-11 14:30:56 +090018extern wilc_hif_func_t hif_sdio;
19extern wilc_hif_func_t hif_spi;
20extern wilc_cfg_func_t mac_cfg;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090021extern void WILC_WFI_mgmt_rx(u8 *buff, u32 size);
22u32 wilc_get_chipid(u8 update);
Dean Lee72ed4dc2015-06-12 14:11:44 +090023u16 Set_machw_change_vir_if(bool bValue);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090024
Johnny Kimc5c77ba2015-05-11 14:30:56 +090025
26
27typedef struct {
28 int quit;
29
30 /**
31 * input interface functions
32 **/
33 wilc_wlan_os_func_t os_func;
34 wilc_wlan_io_func_t io_func;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090035
36 /**
37 * host interface functions
38 **/
39 wilc_hif_func_t hif_func;
Greg Kroah-Hartman5e150b52015-09-03 19:32:11 -070040 struct mutex *hif_lock;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090041
42 /**
43 * configuration interface functions
44 **/
45 wilc_cfg_func_t cif_func;
46 int cfg_frame_in_use;
47 wilc_cfg_frame_t cfg_frame;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090048 u32 cfg_frame_offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090049 int cfg_seq_no;
50 void *cfg_wait;
51
52 /**
53 * RX buffer
54 **/
55 #ifdef MEMORY_STATIC
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090056 u32 rx_buffer_size;
Chaehyun Lim51e825f2015-09-15 14:06:14 +090057 u8 *rx_buffer;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090058 u32 rx_buffer_offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090059 #endif
60 /**
61 * TX buffer
62 **/
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090063 u32 tx_buffer_size;
Chaehyun Lim51e825f2015-09-15 14:06:14 +090064 u8 *tx_buffer;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +090065 u32 tx_buffer_offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090066
67 /**
68 * TX queue
69 **/
70 void *txq_lock;
71
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -070072 struct semaphore *txq_add_to_head_lock;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090073 unsigned long txq_spinlock_flags;
74
75 struct txq_entry_t *txq_head;
76 struct txq_entry_t *txq_tail;
77 int txq_entries;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090078 int txq_exit;
79
80 /**
81 * RX queue
82 **/
Johnny Kimc5c77ba2015-05-11 14:30:56 +090083 struct rxq_entry_t *rxq_head;
84 struct rxq_entry_t *rxq_tail;
85 int rxq_entries;
Johnny Kimc5c77ba2015-05-11 14:30:56 +090086 int rxq_exit;
87
88
89} wilc_wlan_dev_t;
90
91static wilc_wlan_dev_t g_wlan;
92
Chaehyun Lim9af382b2015-09-16 20:11:27 +090093static inline void chip_allow_sleep(void);
94static inline void chip_wakeup(void);
Johnny Kimc5c77ba2015-05-11 14:30:56 +090095/********************************************
96 *
97 * Debug
98 *
99 ********************************************/
100
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900101static u32 dbgflag = N_INIT | N_ERR | N_INTR | N_TXQ | N_RXQ;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900102
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900103static void wilc_debug(u32 flag, char *fmt, ...)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900104{
105 char buf[256];
106 va_list args;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900107
108 if (flag & dbgflag) {
109 va_start(args, fmt);
Hari Prasath Gujulan Elango81053222015-06-22 13:13:58 +0000110 vsprintf(buf, fmt, args);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900111 va_end(args);
112
Glen Leeef2b7842015-09-24 18:14:55 +0900113 linux_wlan_dbg(buf);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900114 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900115}
116
117static CHIP_PS_STATE_T genuChipPSstate = CHIP_WAKEDUP;
118
Chaehyun Lim9af382b2015-09-16 20:11:27 +0900119/*acquire_bus() and release_bus() are made static inline functions*/
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900120/*as a temporary workaround to fix a problem of receiving*/
121/*unknown interrupt from FW*/
Chaehyun Lim9af382b2015-09-16 20:11:27 +0900122static inline void acquire_bus(BUS_ACQUIRE_T acquire)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900123{
124
Greg Kroah-Hartman5e150b52015-09-03 19:32:11 -0700125 mutex_lock(g_wlan.hif_lock);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900126 #ifndef WILC_OPTIMIZE_SLEEP_INT
127 if (genuChipPSstate != CHIP_WAKEDUP)
128 #endif
129 {
130 if (acquire == ACQUIRE_AND_WAKEUP)
131 chip_wakeup();
132 }
133
134}
Chaehyun Lim9af382b2015-09-16 20:11:27 +0900135static inline void release_bus(BUS_RELEASE_T release)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900136{
137 #ifdef WILC_OPTIMIZE_SLEEP_INT
138 if (release == RELEASE_ALLOW_SLEEP)
139 chip_allow_sleep();
140 #endif
Greg Kroah-Hartman5e150b52015-09-03 19:32:11 -0700141 mutex_unlock(g_wlan.hif_lock);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900142}
143/********************************************
144 *
145 * Queue
146 *
147 ********************************************/
148
149static void wilc_wlan_txq_remove(struct txq_entry_t *tqe)
150{
151
152 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
153 /* unsigned long flags; */
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900154 if (tqe == p->txq_head) {
155
156 p->txq_head = tqe->next;
157 if (p->txq_head)
158 p->txq_head->prev = NULL;
159
160
161 } else if (tqe == p->txq_tail) {
162 p->txq_tail = (tqe->prev);
163 if (p->txq_tail)
164 p->txq_tail->next = NULL;
165 } else {
166 tqe->prev->next = tqe->next;
167 tqe->next->prev = tqe->prev;
168 }
169 p->txq_entries -= 1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900170
171}
172
173static struct txq_entry_t *wilc_wlan_txq_remove_from_head(void)
174{
175 struct txq_entry_t *tqe;
176 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
177 unsigned long flags;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900178
Glen Lee85e57562015-09-24 18:14:56 +0900179 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900180 if (p->txq_head) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900181 tqe = p->txq_head;
182 p->txq_head = tqe->next;
183 if (p->txq_head) {
184 p->txq_head->prev = NULL;
185 }
186 p->txq_entries -= 1;
187
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900188
189
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900190
191 } else {
192 tqe = NULL;
193 }
Glen Lee85e57562015-09-24 18:14:56 +0900194 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900195 return tqe;
196}
197
198static void wilc_wlan_txq_add_to_tail(struct txq_entry_t *tqe)
199{
200 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
201 unsigned long flags;
Glen Lee85e57562015-09-24 18:14:56 +0900202 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900203
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900204 if (p->txq_head == NULL) {
205 tqe->next = NULL;
206 tqe->prev = NULL;
207 p->txq_head = tqe;
208 p->txq_tail = tqe;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900209 } else {
210 tqe->next = NULL;
211 tqe->prev = p->txq_tail;
212 p->txq_tail->next = tqe;
213 p->txq_tail = tqe;
214 }
215 p->txq_entries += 1;
216 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900217
Glen Lee85e57562015-09-24 18:14:56 +0900218 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900219
220 /**
221 * wake up TX queue
222 **/
223 PRINT_D(TX_DBG, "Wake the txq_handling\n");
224
Glen Lee5cd63632015-09-24 18:14:58 +0900225 up(&g_linux_wlan->txq_event);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900226}
227
228static int wilc_wlan_txq_add_to_head(struct txq_entry_t *tqe)
229{
230 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
231 unsigned long flags;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900232 if (p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT))
233 return -1;
234
Glen Lee85e57562015-09-24 18:14:56 +0900235 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900236
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900237 if (p->txq_head == NULL) {
238 tqe->next = NULL;
239 tqe->prev = NULL;
240 p->txq_head = tqe;
241 p->txq_tail = tqe;
242 } else {
243 tqe->next = p->txq_head;
244 tqe->prev = NULL;
245 p->txq_head->prev = tqe;
246 p->txq_head = tqe;
247 }
248 p->txq_entries += 1;
249 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900250
Glen Lee85e57562015-09-24 18:14:56 +0900251 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -0700252 up(p->txq_add_to_head_lock);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900253
254
255 /**
256 * wake up TX queue
257 **/
Glen Lee5cd63632015-09-24 18:14:58 +0900258 up(&g_linux_wlan->txq_event);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900259 PRINT_D(TX_DBG, "Wake up the txq_handler\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900260
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900261 return 0;
262
263}
264
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900265u32 Statisitcs_totalAcks = 0, Statisitcs_DroppedAcks = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900266
267#ifdef TCP_ACK_FILTER
268struct Ack_session_info;
Shraddha Barkeeeb1c062015-08-05 01:29:22 +0530269struct Ack_session_info {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900270 u32 Ack_seq_num;
271 u32 Bigger_Ack_num;
Chaehyun Limec53adf2015-09-15 14:06:15 +0900272 u16 src_port;
273 u16 dst_port;
274 u16 status;
Shraddha Barkeeeb1c062015-08-05 01:29:22 +0530275};
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900276
277typedef struct {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900278 u32 ack_num;
279 u32 Session_index;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900280 struct txq_entry_t *txqe;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900281} Pending_Acks_info_t /*Ack_info_t*/;
282
283
284
285
286struct Ack_session_info *Free_head;
287struct Ack_session_info *Alloc_head;
288
289#define TCP_FIN_MASK (1 << 0)
290#define TCP_SYN_MASK (1 << 1)
291#define TCP_Ack_MASK (1 << 4)
292#define NOT_TCP_ACK (-1)
293
294#define MAX_TCP_SESSION 25
295#define MAX_PENDING_ACKS 256
Shraddha Barkeeeb1c062015-08-05 01:29:22 +0530296struct Ack_session_info Acks_keep_track_info[2 * MAX_TCP_SESSION];
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900297Pending_Acks_info_t Pending_Acks_info[MAX_PENDING_ACKS];
298
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900299u32 PendingAcks_arrBase;
300u32 Opened_TCP_session;
301u32 Pending_Acks;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900302
303
304
Chaehyun Limfed16f22015-09-17 16:48:43 +0900305static inline int Init_TCP_tracking(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900306{
307
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900308 return 0;
309
310}
Chaehyun Limfed16f22015-09-17 16:48:43 +0900311static inline int add_TCP_track_session(u32 src_prt, u32 dst_prt, u32 seq)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900312{
313 Acks_keep_track_info[Opened_TCP_session].Ack_seq_num = seq;
314 Acks_keep_track_info[Opened_TCP_session].Bigger_Ack_num = 0;
315 Acks_keep_track_info[Opened_TCP_session].src_port = src_prt;
316 Acks_keep_track_info[Opened_TCP_session].dst_port = dst_prt;
317 Opened_TCP_session++;
318
319 PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", Opened_TCP_session, seq);
320 return 0;
321}
322
Chaehyun Limfed16f22015-09-17 16:48:43 +0900323static inline int Update_TCP_track_session(u32 index, u32 Ack)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900324{
325
326 if (Ack > Acks_keep_track_info[index].Bigger_Ack_num) {
327 Acks_keep_track_info[index].Bigger_Ack_num = Ack;
328 }
329 return 0;
330
331}
Chaehyun Limfed16f22015-09-17 16:48:43 +0900332static inline int add_TCP_Pending_Ack(u32 Ack, u32 Session_index, struct txq_entry_t *txqe)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900333{
334 Statisitcs_totalAcks++;
335 if (Pending_Acks < MAX_PENDING_ACKS) {
336 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].ack_num = Ack;
337 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].txqe = txqe;
338 Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].Session_index = Session_index;
339 txqe->tcp_PendingAck_index = PendingAcks_arrBase + Pending_Acks;
340 Pending_Acks++;
341
342 } else {
343
344 }
345 return 0;
346}
Chaehyun Limfed16f22015-09-17 16:48:43 +0900347static inline int remove_TCP_related(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900348{
349 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
350 unsigned long flags;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900351
Glen Lee85e57562015-09-24 18:14:56 +0900352 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900353
Glen Lee85e57562015-09-24 18:14:56 +0900354 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900355 return 0;
356}
357
Chaehyun Limfed16f22015-09-17 16:48:43 +0900358static inline int tcp_process(struct txq_entry_t *tqe)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900359{
360 int ret;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900361 u8 *eth_hdr_ptr;
362 u8 *buffer = tqe->buffer;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900363 unsigned short h_proto;
364 int i;
365 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
366 unsigned long flags;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900367
Glen Lee85e57562015-09-24 18:14:56 +0900368 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900369
370 eth_hdr_ptr = &buffer[0];
371 h_proto = ntohs(*((unsigned short *)&eth_hdr_ptr[12]));
372 if (h_proto == 0x0800) { /* IP */
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900373 u8 *ip_hdr_ptr;
374 u8 protocol;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900375
376 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
377 protocol = ip_hdr_ptr[9];
378
379
380 if (protocol == 0x06) {
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900381 u8 *tcp_hdr_ptr;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900382 u32 IHL, Total_Length, Data_offset;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900383
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900384 tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
385 IHL = (ip_hdr_ptr[0] & 0xf) << 2;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900386 Total_Length = (((u32)ip_hdr_ptr[2]) << 8) + ((u32)ip_hdr_ptr[3]);
387 Data_offset = (((u32)tcp_hdr_ptr[12] & 0xf0) >> 2);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900388 if (Total_Length == (IHL + Data_offset)) { /*we want to recognize the clear Acks(packet only carry Ack infos not with data) so data size must be equal zero*/
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900389 u32 seq_no, Ack_no;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900390
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900391 seq_no = (((u32)tcp_hdr_ptr[4]) << 24) + (((u32)tcp_hdr_ptr[5]) << 16) + (((u32)tcp_hdr_ptr[6]) << 8) + ((u32)tcp_hdr_ptr[7]);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900392
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900393 Ack_no = (((u32)tcp_hdr_ptr[8]) << 24) + (((u32)tcp_hdr_ptr[9]) << 16) + (((u32)tcp_hdr_ptr[10]) << 8) + ((u32)tcp_hdr_ptr[11]);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900394
395
396 for (i = 0; i < Opened_TCP_session; i++) {
397 if (Acks_keep_track_info[i].Ack_seq_num == seq_no) {
398 Update_TCP_track_session(i, Ack_no);
399 break;
400 }
401 }
402 if (i == Opened_TCP_session) {
403 add_TCP_track_session(0, 0, seq_no);
404 }
405 add_TCP_Pending_Ack(Ack_no, i, tqe);
406
407
408 }
409
410 } else {
411 ret = 0;
412 }
413 } else {
414 ret = 0;
415 }
Glen Lee85e57562015-09-24 18:14:56 +0900416 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900417 return ret;
418}
419
420
421static int wilc_wlan_txq_filter_dup_tcp_ack(void)
422{
423
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900424 u32 i = 0;
425 u32 Dropped = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900426 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
427
Glen Lee85e57562015-09-24 18:14:56 +0900428 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, p->txq_spinlock_flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900429 for (i = PendingAcks_arrBase; i < (PendingAcks_arrBase + Pending_Acks); i++) {
430 if (Pending_Acks_info[i].ack_num < Acks_keep_track_info[Pending_Acks_info[i].Session_index].Bigger_Ack_num) {
431 struct txq_entry_t *tqe;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900432
Chandra S Gorentla17aacd42015-08-08 17:41:35 +0530433 PRINT_D(TCP_ENH, "DROP ACK: %u\n", Pending_Acks_info[i].ack_num);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900434 tqe = Pending_Acks_info[i].txqe;
435 if (tqe) {
436 wilc_wlan_txq_remove(tqe);
437 Statisitcs_DroppedAcks++;
438 tqe->status = 1; /* mark the packet send */
439 if (tqe->tx_complete_func)
440 tqe->tx_complete_func(tqe->priv, tqe->status);
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -0700441 kfree(tqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900442 Dropped++;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900443 }
444 }
445 }
446 Pending_Acks = 0;
447 Opened_TCP_session = 0;
448
Chandra S Gorentla78174ad2015-08-08 17:41:36 +0530449 if (PendingAcks_arrBase == 0)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900450 PendingAcks_arrBase = MAX_TCP_SESSION;
Chandra S Gorentla78174ad2015-08-08 17:41:36 +0530451 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900452 PendingAcks_arrBase = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900453
454
Glen Lee85e57562015-09-24 18:14:56 +0900455 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock,
456 p->txq_spinlock_flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900457
458 while (Dropped > 0) {
459 /*consume the semaphore count of the removed packet*/
Glen Lee5cd63632015-09-24 18:14:58 +0900460 p->os_func.os_wait(&g_linux_wlan->txq_event, 1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900461 Dropped--;
462 }
463
464 return 1;
465}
466#endif
467
Dean Lee72ed4dc2015-06-12 14:11:44 +0900468bool EnableTCPAckFilter = false;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900469
Dean Lee72ed4dc2015-06-12 14:11:44 +0900470void Enable_TCP_ACK_Filter(bool value)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900471{
472 EnableTCPAckFilter = value;
473}
474
Dean Lee72ed4dc2015-06-12 14:11:44 +0900475bool is_TCP_ACK_Filter_Enabled(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900476{
477 return EnableTCPAckFilter;
478}
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900479
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900480static int wilc_wlan_txq_add_cfg_pkt(u8 *buffer, u32 buffer_size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900481{
482 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
483 struct txq_entry_t *tqe;
484
485 PRINT_D(TX_DBG, "Adding config packet ...\n");
486 if (p->quit) {
487 PRINT_D(TX_DBG, "Return due to clear function\n");
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -0700488 up(p->cfg_wait);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900489 return 0;
490 }
491
Greg Kroah-Hartman47c632d2015-09-03 18:55:43 -0700492 tqe = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900493 if (tqe == NULL) {
494 PRINT_ER("Failed to allocate memory\n");
495 return 0;
496 }
497
498 tqe->type = WILC_CFG_PKT;
499 tqe->buffer = buffer;
500 tqe->buffer_size = buffer_size;
501 tqe->tx_complete_func = NULL;
502 tqe->priv = NULL;
503#ifdef TCP_ACK_FILTER
504 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
505#endif
506 /**
507 * Configuration packet always at the front
508 **/
509 PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
510
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900511 if (wilc_wlan_txq_add_to_head(tqe))
512 return 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900513 return 1;
514}
515
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900516static int wilc_wlan_txq_add_net_pkt(void *priv, u8 *buffer, u32 buffer_size, wilc_tx_complete_func_t func)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900517{
518 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
519 struct txq_entry_t *tqe;
520
521 if (p->quit)
522 return 0;
523
Greg Kroah-Hartman47c632d2015-09-03 18:55:43 -0700524 tqe = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900525
526 if (tqe == NULL)
527 return 0;
528 tqe->type = WILC_NET_PKT;
529 tqe->buffer = buffer;
530 tqe->buffer_size = buffer_size;
531 tqe->tx_complete_func = func;
532 tqe->priv = priv;
533
534 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
535#ifdef TCP_ACK_FILTER
536 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
Abdul Hussain5a66bf22015-06-16 09:44:06 +0000537 if (is_TCP_ACK_Filter_Enabled())
Glen Lee25a84832015-09-16 18:53:22 +0900538 tcp_process(tqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900539#endif
540 wilc_wlan_txq_add_to_tail(tqe);
541 /*return number of itemes in the queue*/
542 return p->txq_entries;
543}
Glen Leefcc6ef92015-09-16 18:53:21 +0900544
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900545int wilc_wlan_txq_add_mgmt_pkt(void *priv, u8 *buffer, u32 buffer_size, wilc_tx_complete_func_t func)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900546{
547
548 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
549 struct txq_entry_t *tqe;
550
551 if (p->quit)
552 return 0;
553
Greg Kroah-Hartman47c632d2015-09-03 18:55:43 -0700554 tqe = kmalloc(sizeof(struct txq_entry_t), GFP_KERNEL);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900555
556 if (tqe == NULL)
557 return 0;
558 tqe->type = WILC_MGMT_PKT;
559 tqe->buffer = buffer;
560 tqe->buffer_size = buffer_size;
561 tqe->tx_complete_func = func;
562 tqe->priv = priv;
563#ifdef TCP_ACK_FILTER
564 tqe->tcp_PendingAck_index = NOT_TCP_ACK;
565#endif
566 PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
567 wilc_wlan_txq_add_to_tail(tqe);
568 return 1;
569}
Glen Leefcc6ef92015-09-16 18:53:21 +0900570
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900571static struct txq_entry_t *wilc_wlan_txq_get_first(void)
572{
573 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
574 struct txq_entry_t *tqe;
575 unsigned long flags;
576
Glen Lee85e57562015-09-24 18:14:56 +0900577 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900578
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900579 tqe = p->txq_head;
580
Glen Lee85e57562015-09-24 18:14:56 +0900581 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900582
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900583
584 return tqe;
585}
586
587static struct txq_entry_t *wilc_wlan_txq_get_next(struct txq_entry_t *tqe)
588{
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900589 unsigned long flags;
Glen Lee85e57562015-09-24 18:14:56 +0900590 spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900591
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900592 tqe = tqe->next;
Glen Lee85e57562015-09-24 18:14:56 +0900593 spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900594
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900595
596 return tqe;
597}
598
599static int wilc_wlan_rxq_add(struct rxq_entry_t *rqe)
600{
601 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
602
603 if (p->quit)
604 return 0;
605
Glen Lee645db602015-09-24 18:14:57 +0900606 mutex_lock(&g_linux_wlan->rxq_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900607 if (p->rxq_head == NULL) {
608 PRINT_D(RX_DBG, "Add to Queue head\n");
609 rqe->next = NULL;
610 p->rxq_head = rqe;
611 p->rxq_tail = rqe;
612 } else {
613 PRINT_D(RX_DBG, "Add to Queue tail\n");
614 p->rxq_tail->next = rqe;
615 rqe->next = NULL;
616 p->rxq_tail = rqe;
617 }
618 p->rxq_entries += 1;
619 PRINT_D(RX_DBG, "Number of queue entries: %d\n", p->rxq_entries);
Glen Lee645db602015-09-24 18:14:57 +0900620 mutex_unlock(&g_linux_wlan->rxq_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900621 return p->rxq_entries;
622}
623
624static struct rxq_entry_t *wilc_wlan_rxq_remove(void)
625{
626 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
627
628 PRINT_D(RX_DBG, "Getting rxQ element\n");
629 if (p->rxq_head) {
630 struct rxq_entry_t *rqe;
631
Glen Lee645db602015-09-24 18:14:57 +0900632 mutex_lock(&g_linux_wlan->rxq_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900633 rqe = p->rxq_head;
634 p->rxq_head = p->rxq_head->next;
635 p->rxq_entries -= 1;
636 PRINT_D(RX_DBG, "RXQ entries decreased\n");
Glen Lee645db602015-09-24 18:14:57 +0900637 mutex_unlock(&g_linux_wlan->rxq_cs);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900638 return rqe;
639 }
640 PRINT_D(RX_DBG, "Nothing to get from Q\n");
641 return NULL;
642}
643
644
645/********************************************
646 *
647 * Power Save handle functions
648 *
649 ********************************************/
650
651
652
653#ifdef WILC_OPTIMIZE_SLEEP_INT
654
Chaehyun Lim9af382b2015-09-16 20:11:27 +0900655static inline void chip_allow_sleep(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900656{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900657 u32 reg = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900658
659 /* Clear bit 1 */
660 g_wlan.hif_func.hif_read_reg(0xf0, &reg);
661
662 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
663}
664
Chaehyun Lim9af382b2015-09-16 20:11:27 +0900665static inline void chip_wakeup(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900666{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900667 u32 reg, clk_status_reg, trials = 0;
668 u32 sleep_time;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900669
670 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
671 do {
672 g_wlan.hif_func.hif_read_reg(1, &reg);
673 /* Set bit 1 */
674 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
675
676 /* Clear bit 1*/
677 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
678
679 do {
680 /* Wait for the chip to stabilize*/
Greg Kroah-Hartman80e29c72015-08-14 19:42:23 -0700681 usleep_range(2 * 1000, 2 * 1000);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900682 /* Make sure chip is awake. This is an extra step that can be removed */
683 /* later to avoid the bus access overhead */
Dean Lee72ed4dc2015-06-12 14:11:44 +0900684 if ((wilc_get_chipid(true) == 0)) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900685 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
686 }
Dean Lee72ed4dc2015-06-12 14:11:44 +0900687 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900688
Dean Lee72ed4dc2015-06-12 14:11:44 +0900689 } while (wilc_get_chipid(true) == 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900690 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
691 g_wlan.hif_func.hif_read_reg(0xf0, &reg);
692 do {
693 /* Set bit 1 */
694 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
695
696 /* Check the clock status */
697 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
698
699 /* in case of clocks off, wait 2ms, and check it again. */
700 /* if still off, wait for another 2ms, for a total wait of 6ms. */
701 /* If still off, redo the wake up sequence */
702 while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
703 /* Wait for the chip to stabilize*/
Greg Kroah-Hartman80e29c72015-08-14 19:42:23 -0700704 usleep_range(2 * 1000, 2 * 1000);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900705
706 /* Make sure chip is awake. This is an extra step that can be removed */
707 /* later to avoid the bus access overhead */
708 g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
709
710 if ((clk_status_reg & 0x1) == 0) {
711 wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
712 }
713 }
714 /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
715 if ((clk_status_reg & 0x1) == 0) {
716 /* Reset bit 0 */
717 g_wlan.hif_func.hif_write_reg(0xf0, reg & (~(1 << 0)));
718 }
719 } while ((clk_status_reg & 0x1) == 0);
720 }
721
722
723 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
724 g_wlan.hif_func.hif_read_reg(0x1C0C, &reg);
725 reg &= ~(1 << 0);
726 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
727
Dean Lee72ed4dc2015-06-12 14:11:44 +0900728 if (wilc_get_chipid(false) >= 0x1002b0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900729 /* Enable PALDO back right after wakeup */
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900730 u32 val32;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900731
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900732 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
733 val32 |= (1 << 6);
734 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
735
736 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
737 val32 |= (1 << 6);
738 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
739 }
740 }
741 genuChipPSstate = CHIP_WAKEDUP;
742}
743#else
Chaehyun Lim9af382b2015-09-16 20:11:27 +0900744static inline void chip_wakeup(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900745{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900746 u32 reg, trials = 0;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900747
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900748 do {
749 if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
750 g_wlan.hif_func.hif_read_reg(1, &reg);
751 /* Make sure bit 1 is 0 before we start. */
752 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
753 /* Set bit 1 */
754 g_wlan.hif_func.hif_write_reg(1, reg | (1 << 1));
755 /* Clear bit 1*/
756 g_wlan.hif_func.hif_write_reg(1, reg & ~(1 << 1));
757 } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
758 /* Make sure bit 0 is 0 before we start. */
759 g_wlan.hif_func.hif_read_reg(0xf0, &reg);
760 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
761 /* Set bit 1 */
762 g_wlan.hif_func.hif_write_reg(0xf0, reg | (1 << 0));
763 /* Clear bit 1 */
764 g_wlan.hif_func.hif_write_reg(0xf0, reg & ~(1 << 0));
765 }
766
767 do {
768 /* Wait for the chip to stabilize*/
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900769 mdelay(3);
770
771 /* Make sure chip is awake. This is an extra step that can be removed */
772 /* later to avoid the bus access overhead */
Dean Lee72ed4dc2015-06-12 14:11:44 +0900773 if ((wilc_get_chipid(true) == 0)) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900774 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
775 }
Dean Lee72ed4dc2015-06-12 14:11:44 +0900776 } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900777
Dean Lee72ed4dc2015-06-12 14:11:44 +0900778 } while (wilc_get_chipid(true) == 0);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900779
780 if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
781 g_wlan.hif_func.hif_read_reg(0x1C0C, &reg);
782 reg &= ~(1 << 0);
783 g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
784
Dean Lee72ed4dc2015-06-12 14:11:44 +0900785 if (wilc_get_chipid(false) >= 0x1002b0) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900786 /* Enable PALDO back right after wakeup */
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900787 u32 val32;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900788
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900789 g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
790 val32 |= (1 << 6);
791 g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
792
793 g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
794 val32 |= (1 << 6);
795 g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
796 }
797 }
798 genuChipPSstate = CHIP_WAKEDUP;
799}
800#endif
Chaehyun Lim4e4467f2015-06-11 14:35:55 +0900801void chip_sleep_manually(u32 u32SleepTime)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900802{
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900803 if (genuChipPSstate != CHIP_WAKEDUP) {
804 /* chip is already sleeping. Do nothing */
805 return;
806 }
807 acquire_bus(ACQUIRE_ONLY);
808
809#ifdef WILC_OPTIMIZE_SLEEP_INT
810 chip_allow_sleep();
811#endif
812
813 /* Trigger the manual sleep interrupt */
814 g_wlan.hif_func.hif_write_reg(0x10a8, 1);
815
816 genuChipPSstate = CHIP_SLEEPING_MANUAL;
817 release_bus(RELEASE_ONLY);
818
819}
820
821
822/********************************************
823 *
824 * Tx, Rx queue handle functions
825 *
826 ********************************************/
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900827static int wilc_wlan_handle_txq(u32 *pu32TxqCount)
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900828{
829 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
830 int i, entries = 0;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900831 u32 sum;
832 u32 reg;
Chaehyun Lim51e825f2015-09-15 14:06:14 +0900833 u8 *txb = p->tx_buffer;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900834 u32 offset = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900835 int vmm_sz = 0;
836 struct txq_entry_t *tqe;
837 int ret = 0;
838 int counter;
839 int timeout;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +0900840 u32 vmm_table[WILC_VMM_TBL_SIZE];
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +0900841
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900842 p->txq_exit = 0;
843 do {
844 if (p->quit)
845 break;
846
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900847 p->os_func.os_wait(p->txq_add_to_head_lock, CFG_PKTS_TIMEOUT);
848#ifdef TCP_ACK_FILTER
849 wilc_wlan_txq_filter_dup_tcp_ack();
850#endif
851 /**
852 * build the vmm list
853 **/
854 PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
855 tqe = wilc_wlan_txq_get_first();
856 i = 0;
857 sum = 0;
858 do {
859 /* if ((tqe != NULL) && (i < (8)) && */
860 /* if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE-1)) && */
861 if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE - 1)) /* reserve last entry to 0 */) {
862
863 if (tqe->type == WILC_CFG_PKT) {
864 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
865 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900866 else if (tqe->type == WILC_NET_PKT) {
867 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
868 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900869 else {
870 vmm_sz = HOST_HDR_OFFSET;
871 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900872 vmm_sz += tqe->buffer_size;
873 PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
874 if (vmm_sz & 0x3) { /* has to be word aligned */
875 vmm_sz = (vmm_sz + 4) & ~0x3;
876 }
877 if ((sum + vmm_sz) > p->tx_buffer_size) {
878 break;
879 }
880 PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
881 vmm_table[i] = vmm_sz / 4; /* table take the word size */
882 PRINT_D(TX_DBG, "VMMTable entry size = %d\n", vmm_table[i]);
883
884 if (tqe->type == WILC_CFG_PKT) {
885 vmm_table[i] |= (1 << 10);
886 PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
887 }
888#ifdef BIG_ENDIAN
889 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
890#endif
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900891
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900892 i++;
893 sum += vmm_sz;
894 PRINT_D(TX_DBG, "sum = %d\n", sum);
895 tqe = wilc_wlan_txq_get_next(tqe);
896 } else {
897 break;
898 }
899 } while (1);
900
901 if (i == 0) { /* nothing in the queue */
902 PRINT_D(TX_DBG, "Nothing in TX-Q\n");
903 break;
904 } else {
905 PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
906 vmm_table[i] = 0x0; /* mark the last element to 0 */
907 }
908 acquire_bus(ACQUIRE_AND_WAKEUP);
909 counter = 0;
910 do {
911
912 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, &reg);
913 if (!ret) {
914 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
915 break;
916 }
917
918 if ((reg & 0x1) == 0) {
919 /**
920 * write to vmm table
921 **/
922 PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
923 break;
924 } else {
925 counter++;
926 if (counter > 200) {
927 counter = 0;
928 PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
929 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, 0);
930 break;
931 }
932 /**
933 * wait for vmm table is ready
934 **/
Chandra S Gorentla17aacd42015-08-08 17:41:35 +0530935 PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900936 release_bus(RELEASE_ALLOW_SLEEP);
Greg Kroah-Hartman2b922cb2015-09-04 09:30:51 -0700937 usleep_range(3000, 3000);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900938 acquire_bus(ACQUIRE_AND_WAKEUP);
939 }
940 } while (!p->quit);
941
942 if (!ret) {
943 goto _end_;
944 }
945
946 timeout = 200;
947 do {
948
949 /**
950 * write to vmm table
951 **/
Chaehyun Limc3ca6372015-09-20 15:51:19 +0900952 ret = p->hif_func.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE, (u8 *)vmm_table, ((i + 1) * 4));
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900953 if (!ret) {
954 wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
955 break;
956 }
957
958
959 /**
960 * interrupt firmware
961 **/
962 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x2);
963 if (!ret) {
964 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
965 break;
966 }
967
968 /**
969 * wait for confirm...
970 **/
971
972 do {
973 ret = p->hif_func.hif_read_reg(WILC_HOST_VMM_CTL, &reg);
974 if (!ret) {
975 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
976 break;
977 }
978 if ((reg >> 2) & 0x1) {
979 /**
980 * Get the entries
981 **/
982 entries = ((reg >> 3) & 0x3f);
983 /* entries = ((reg>>3)&0x2f); */
984 break;
985 } else {
986 release_bus(RELEASE_ALLOW_SLEEP);
Greg Kroah-Hartman2b922cb2015-09-04 09:30:51 -0700987 usleep_range(3000, 3000);
Johnny Kimc5c77ba2015-05-11 14:30:56 +0900988 acquire_bus(ACQUIRE_AND_WAKEUP);
989 PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
990 }
991 } while (--timeout);
992 if (timeout <= 0) {
993 ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x0);
994 break;
995 }
996
997 if (!ret) {
998 break;
999 }
1000
1001 if (entries == 0) {
Chandra S Gorentla17aacd42015-08-08 17:41:35 +05301002 PRINT_WRN(GENERIC_DBG, "[wilc txq]: no more buffer in the chip (reg: %08x), retry later [[ %d, %x ]]\n", reg, i, vmm_table[i - 1]);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001003
1004 /* undo the transaction. */
1005 ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, &reg);
1006 if (!ret) {
1007 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
1008 break;
1009 }
1010 reg &= ~(1ul << 0);
1011 ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, reg);
1012 if (!ret) {
1013 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
1014 break;
1015 }
1016 break;
1017 } else {
1018 break;
1019 }
1020 } while (1);
1021
1022 if (!ret) {
1023 goto _end_;
1024 }
1025 if (entries == 0) {
1026 ret = WILC_TX_ERR_NO_BUF;
1027 goto _end_;
1028 }
1029
1030 /* since copying data into txb takes some time, then
1031 * allow the bus lock to be released let the RX task go. */
1032 release_bus(RELEASE_ALLOW_SLEEP);
1033
1034 /**
1035 * Copy data to the TX buffer
1036 **/
1037 offset = 0;
1038 i = 0;
1039 do {
1040 tqe = wilc_wlan_txq_remove_from_head();
1041 if (tqe != NULL && (vmm_table[i] != 0)) {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001042 u32 header, buffer_offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001043
1044#ifdef BIG_ENDIAN
1045 vmm_table[i] = BYTE_SWAP(vmm_table[i]);
1046#endif
1047 vmm_sz = (vmm_table[i] & 0x3ff); /* in word unit */
1048 vmm_sz *= 4;
1049 header = (tqe->type << 31) | (tqe->buffer_size << 15) | vmm_sz;
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05301050 if (tqe->type == WILC_MGMT_PKT)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001051 header |= (1 << 30);
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05301052 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001053 header &= ~(1 << 30);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001054
1055#ifdef BIG_ENDIAN
1056 header = BYTE_SWAP(header);
1057#endif
1058 memcpy(&txb[offset], &header, 4);
1059 if (tqe->type == WILC_CFG_PKT) {
1060 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
1061 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001062 else if (tqe->type == WILC_NET_PKT) {
1063 char *pBSSID = ((struct tx_complete_data *)(tqe->priv))->pBssid;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +09001064
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001065 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
1066 /* copy the bssid at the sart of the buffer */
1067 memcpy(&txb[offset + 4], pBSSID, 6);
1068 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001069 else {
1070 buffer_offset = HOST_HDR_OFFSET;
1071 }
1072
1073 memcpy(&txb[offset + buffer_offset], tqe->buffer, tqe->buffer_size);
1074 offset += vmm_sz;
1075 i++;
1076 tqe->status = 1; /* mark the packet send */
1077 if (tqe->tx_complete_func)
1078 tqe->tx_complete_func(tqe->priv, tqe->status);
1079 #ifdef TCP_ACK_FILTER
1080 if (tqe->tcp_PendingAck_index != NOT_TCP_ACK) {
1081 Pending_Acks_info[tqe->tcp_PendingAck_index].txqe = NULL;
1082 }
1083 #endif
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001084 kfree(tqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001085 } else {
1086 break;
1087 }
1088 } while (--entries);
1089
1090 /**
1091 * lock the bus
1092 **/
1093 acquire_bus(ACQUIRE_AND_WAKEUP);
1094
1095 ret = p->hif_func.hif_clear_int_ext(ENABLE_TX_VMM);
1096 if (!ret) {
1097 wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
1098 goto _end_;
1099 }
1100
1101 /**
1102 * transfer
1103 **/
1104 ret = p->hif_func.hif_block_tx_ext(0, txb, offset);
1105 if (!ret) {
1106 wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
1107 goto _end_;
1108 }
1109
1110_end_:
1111
1112 release_bus(RELEASE_ALLOW_SLEEP);
1113 if (ret != 1)
1114 break;
1115 } while (0);
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -07001116 up(p->txq_add_to_head_lock);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001117
1118 p->txq_exit = 1;
1119 PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
1120 /* return tx[]q count */
1121 *pu32TxqCount = p->txq_entries;
1122 return ret;
1123}
1124
1125static void wilc_wlan_handle_rxq(void)
1126{
1127 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1128 int offset = 0, size, has_packet = 0;
Chaehyun Lim51e825f2015-09-15 14:06:14 +09001129 u8 *buffer;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001130 struct rxq_entry_t *rqe;
1131
1132 p->rxq_exit = 0;
1133
1134
1135
1136
1137 do {
1138 if (p->quit) {
Chandra S Gorentla17aacd42015-08-08 17:41:35 +05301139 PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function\n");
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -07001140 up(p->cfg_wait);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001141 break;
1142 }
1143 rqe = wilc_wlan_rxq_remove();
1144 if (rqe == NULL) {
1145 PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
1146 break;
1147 }
1148 buffer = rqe->buffer;
1149 size = rqe->buffer_size;
1150 PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n", size, buffer);
1151 offset = 0;
1152
1153
1154
1155 do {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001156 u32 header;
1157 u32 pkt_len, pkt_offset, tp_len;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001158 int is_cfg_packet;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +09001159
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001160 PRINT_D(RX_DBG, "In the 2nd do-while\n");
1161 memcpy(&header, &buffer[offset], 4);
1162#ifdef BIG_ENDIAN
1163 header = BYTE_SWAP(header);
1164#endif
1165 PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n", header, offset);
1166
1167
1168
1169 is_cfg_packet = (header >> 31) & 0x1;
1170 pkt_offset = (header >> 22) & 0x1ff;
1171 tp_len = (header >> 11) & 0x7ff;
1172 pkt_len = header & 0x7ff;
1173
1174 if (pkt_len == 0 || tp_len == 0) {
1175 wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
1176 break;
1177 }
1178
1179/*bug 3887: [AP] Allow Management frames to be passed to the host*/
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001180 #define IS_MANAGMEMENT 0x100
1181 #define IS_MANAGMEMENT_CALLBACK 0x080
1182 #define IS_MGMT_STATUS_SUCCES 0x040
1183
1184
1185 if (pkt_offset & IS_MANAGMEMENT) {
1186 /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
1187 pkt_offset &= ~(IS_MANAGMEMENT | IS_MANAGMEMENT_CALLBACK | IS_MGMT_STATUS_SUCCES);
1188
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001189 WILC_WFI_mgmt_rx(&buffer[offset + HOST_HDR_OFFSET], pkt_len);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001190 }
1191 /* BUG4530 fix */
1192 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001193 {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001194
1195 if (!is_cfg_packet) {
Glen Lee63611672015-09-24 18:14:52 +09001196 if (pkt_len > 0) {
1197 frmw_to_linux(&buffer[offset],
1198 pkt_len,
1199 pkt_offset);
1200 has_packet = 1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001201 }
1202 } else {
1203 wilc_cfg_rsp_t rsp;
1204
1205
1206
1207 p->cif_func.rx_indicate(&buffer[pkt_offset + offset], pkt_len, &rsp);
1208 if (rsp.type == WILC_CFG_RSP) {
1209 /**
1210 * wake up the waiting task...
1211 **/
1212 PRINT_D(RX_DBG, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p->cfg_seq_no, rsp.seq_no);
1213 if (p->cfg_seq_no == rsp.seq_no) {
Greg Kroah-Hartman8990d852015-09-03 20:07:58 -07001214 up(p->cfg_wait);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001215 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001216 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
1217 /**
1218 * Call back to indicate status...
1219 **/
Glen Lee4417d3d2015-09-24 18:14:53 +09001220 linux_wlan_mac_indicate(WILC_MAC_INDICATE_STATUS);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001221
1222 } else if (rsp.type == WILC_CFG_RSP_SCAN) {
Glen Lee4417d3d2015-09-24 18:14:53 +09001223 linux_wlan_mac_indicate(WILC_MAC_INDICATE_SCAN);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001224 }
1225 }
1226 }
1227 offset += tp_len;
1228 if (offset >= size)
1229 break;
1230 } while (1);
1231
1232
1233#ifndef MEMORY_STATIC
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001234 kfree(buffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001235#endif
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001236 kfree(rqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001237
1238 if (has_packet) {
Glen Leec0cadaa2015-09-24 18:14:54 +09001239 linux_wlan_rx_complete();
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001240 }
1241 } while (1);
1242
1243 p->rxq_exit = 1;
Chandra S Gorentla17aacd42015-08-08 17:41:35 +05301244 PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001245}
1246
1247/********************************************
1248 *
1249 * Fast DMA Isr
1250 *
1251 ********************************************/
1252static void wilc_unknown_isr_ext(void)
1253{
1254 g_wlan.hif_func.hif_clear_int_ext(0);
1255}
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001256static void wilc_pllupdate_isr_ext(u32 int_stats)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001257{
1258
1259 int trials = 10;
1260
1261 g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
1262
1263 /* Waiting for PLL */
Greg Kroah-Hartmanc2e4c0f2015-09-03 19:09:50 -07001264 mdelay(WILC_PLL_TO);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001265
1266 /* poll till read a valid data */
Dean Lee72ed4dc2015-06-12 14:11:44 +09001267 while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001268 PRINT_D(TX_DBG, "PLL update retrying\n");
Greg Kroah-Hartmanc2e4c0f2015-09-03 19:09:50 -07001269 mdelay(1);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001270 }
1271}
1272
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001273static void wilc_sleeptimer_isr_ext(u32 int_stats1)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001274{
1275 g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
1276#ifndef WILC_OPTIMIZE_SLEEP_INT
1277 genuChipPSstate = CHIP_SLEEPING_AUTO;
1278#endif
1279}
1280
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001281static void wilc_wlan_handle_isr_ext(u32 int_status)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001282{
1283 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1284#ifdef MEMORY_STATIC
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001285 u32 offset = p->rx_buffer_offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001286#endif
Chaehyun Lim51e825f2015-09-15 14:06:14 +09001287 u8 *buffer = NULL;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001288 u32 size;
1289 u32 retries = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001290 int ret = 0;
1291 struct rxq_entry_t *rqe;
1292
1293
1294 /**
1295 * Get the rx size
1296 **/
1297
1298 size = ((int_status & 0x7fff) << 2);
1299
1300 while (!size && retries < 10) {
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001301 u32 time = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001302 /*looping more secure*/
1303 /*zero size make a crashe because the dma will not happen and that will block the firmware*/
1304 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1305 p->hif_func.hif_read_size(&size);
1306 size = ((size & 0x7fff) << 2);
1307 retries++;
1308
1309 }
1310
1311 if (size > 0) {
1312#ifdef MEMORY_STATIC
1313 if (p->rx_buffer_size - offset < size)
1314 offset = 0;
1315
1316 if (p->rx_buffer)
1317 buffer = &p->rx_buffer[offset];
1318 else {
1319 wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1320 goto _end_;
1321 }
1322
1323#else
Greg Kroah-Hartmanf019b9d2015-09-03 18:50:27 -07001324 buffer = kmalloc(size, GFP_KERNEL);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001325 if (buffer == NULL) {
1326 wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
Greg Kroah-Hartman80e29c72015-08-14 19:42:23 -07001327 usleep_range(100 * 1000, 100 * 1000);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001328 goto _end_;
1329 }
1330#endif
1331
1332 /**
1333 * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
1334 **/
1335 p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
1336
1337
1338 /**
1339 * start transfer
1340 **/
1341 ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
1342
1343 if (!ret) {
1344 wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1345 goto _end_;
1346 }
1347_end_:
1348
1349
1350 if (ret) {
1351#ifdef MEMORY_STATIC
1352 offset += size;
1353 p->rx_buffer_offset = offset;
1354#endif
1355 /**
1356 * add to rx queue
1357 **/
Greg Kroah-Hartmanf019b9d2015-09-03 18:50:27 -07001358 rqe = kmalloc(sizeof(struct rxq_entry_t), GFP_KERNEL);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001359 if (rqe != NULL) {
1360 rqe->buffer = buffer;
1361 rqe->buffer_size = size;
1362 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1363 wilc_wlan_rxq_add(rqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001364 }
1365 } else {
1366#ifndef MEMORY_STATIC
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001367 kfree(buffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001368#endif
1369 }
1370 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001371 wilc_wlan_handle_rxq();
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001372}
1373
1374void wilc_handle_isr(void)
1375{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001376 u32 int_status;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001377
1378 acquire_bus(ACQUIRE_AND_WAKEUP);
1379 g_wlan.hif_func.hif_read_int(&int_status);
1380
1381 if (int_status & PLL_INT_EXT) {
1382 wilc_pllupdate_isr_ext(int_status);
1383 }
1384 if (int_status & DATA_INT_EXT) {
1385 wilc_wlan_handle_isr_ext(int_status);
1386 #ifndef WILC_OPTIMIZE_SLEEP_INT
1387 /* Chip is up and talking*/
1388 genuChipPSstate = CHIP_WAKEDUP;
1389 #endif
1390 }
1391 if (int_status & SLEEP_INT_EXT) {
1392 wilc_sleeptimer_isr_ext(int_status);
1393 }
1394
1395 if (!(int_status & (ALL_INT_EXT))) {
1396#ifdef WILC_SDIO
1397 PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
1398#endif
1399 wilc_unknown_isr_ext();
1400 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001401 release_bus(RELEASE_ALLOW_SLEEP);
1402}
1403
1404/********************************************
1405 *
1406 * Firmware download
1407 *
1408 ********************************************/
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001409static int wilc_wlan_firmware_download(const u8 *buffer, u32 buffer_size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001410{
1411 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001412 u32 offset;
1413 u32 addr, size, size2, blksz;
Chaehyun Lim51e825f2015-09-15 14:06:14 +09001414 u8 *dma_buffer;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001415 int ret = 0;
1416
Chaehyun Limc3ca6372015-09-20 15:51:19 +09001417 blksz = (1ul << 12);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001418 /* Allocate a DMA coherent buffer. */
1419
Greg Kroah-Hartmanf019b9d2015-09-03 18:50:27 -07001420 dma_buffer = kmalloc(blksz, GFP_KERNEL);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001421 if (dma_buffer == NULL) {
1422 /*EIO 5*/
1423 ret = -5;
1424 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1425 goto _fail_1;
1426 }
1427
1428 PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1429 /**
1430 * load the firmware
1431 **/
1432 offset = 0;
1433 do {
1434 memcpy(&addr, &buffer[offset], 4);
1435 memcpy(&size, &buffer[offset + 4], 4);
1436#ifdef BIG_ENDIAN
1437 addr = BYTE_SWAP(addr);
1438 size = BYTE_SWAP(size);
1439#endif
1440 acquire_bus(ACQUIRE_ONLY);
1441 offset += 8;
1442 while (((int)size) && (offset < buffer_size)) {
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05301443 if (size <= blksz)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001444 size2 = size;
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05301445 else
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001446 size2 = blksz;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001447 /* Copy firmware into a DMA coherent buffer */
1448 memcpy(dma_buffer, &buffer[offset], size2);
1449 ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
1450 if (!ret)
1451 break;
1452
1453 addr += size2;
1454 offset += size2;
1455 size -= size2;
1456 }
1457 release_bus(RELEASE_ONLY);
1458
1459 if (!ret) {
1460 /*EIO 5*/
1461 ret = -5;
1462 PRINT_ER("Can't download firmware IO error\n ");
1463 goto _fail_;
1464 }
1465 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1466 } while (offset < buffer_size);
1467
1468_fail_:
1469
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001470 kfree(dma_buffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001471
1472_fail_1:
1473
1474 return (ret < 0) ? ret : 0;
1475}
1476
1477/********************************************
1478 *
1479 * Common
1480 *
1481 ********************************************/
1482static int wilc_wlan_start(void)
1483{
1484 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001485 u32 reg = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001486 int ret;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001487 u32 chipid;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001488
1489 /**
1490 * Set the host interface
1491 **/
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001492 if (p->io_func.io_type == HIF_SDIO) {
1493 reg = 0;
1494 reg |= (1 << 3); /* bug 4456 and 4557 */
1495 } else if (p->io_func.io_type == HIF_SPI) {
1496 reg = 1;
1497 }
1498 acquire_bus(ACQUIRE_ONLY);
1499 ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CFG, reg);
1500 if (!ret) {
1501 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
1502 release_bus(RELEASE_ONLY);
1503 /* EIO 5*/
1504 ret = -5;
1505 return ret;
1506 }
1507 reg = 0;
1508#ifdef WILC_SDIO_IRQ_GPIO
1509 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1510#endif
1511
1512#ifdef WILC_DISABLE_PMU
1513#else
1514 reg |= WILC_HAVE_USE_PMU;
1515#endif
1516
1517#ifdef WILC_SLEEP_CLK_SRC_XO
1518 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1519#elif defined WILC_SLEEP_CLK_SRC_RTC
1520 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1521#endif
1522
1523#ifdef WILC_EXT_PA_INV_TX_RX
1524 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1525#endif
1526
1527 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1528
1529
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001530/*Set oscillator frequency*/
1531#ifdef XTAL_24
1532 reg |= WILC_HAVE_XTAL_24;
1533#endif
1534
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001535/*Enable/Disable GPIO configuration for FW logs*/
1536#ifdef DISABLE_WILC_UART
1537 reg |= WILC_HAVE_DISABLE_WILC_UART;
1538#endif
1539
1540 ret = p->hif_func.hif_write_reg(WILC_GP_REG_1, reg);
1541 if (!ret) {
1542 wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1543 release_bus(RELEASE_ONLY);
1544 /* EIO 5*/
1545 ret = -5;
1546 return ret;
1547 }
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001548
1549 /**
1550 * Bus related
1551 **/
1552 p->hif_func.hif_sync_ext(NUM_INT_EXT);
1553
1554 ret = p->hif_func.hif_read_reg(0x1000, &chipid);
1555 if (!ret) {
1556 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
1557 release_bus(RELEASE_ONLY);
1558 /* EIO 5*/
1559 ret = -5;
1560 return ret;
1561 }
1562
1563 /**
1564 * Go...
1565 **/
1566
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001567
1568 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1569 if ((reg & (1ul << 10)) == (1ul << 10)) {
1570 reg &= ~(1ul << 10);
1571 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1572 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1573 }
1574
1575 reg |= (1ul << 10);
1576 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1577 p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1578 release_bus(RELEASE_ONLY);
1579
1580 return (ret < 0) ? ret : 0;
1581}
1582
1583void wilc_wlan_global_reset(void)
1584{
1585
1586 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
Chaehyun Lim8dfaafd2015-08-18 23:18:11 +09001587
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001588 acquire_bus(ACQUIRE_AND_WAKEUP);
1589 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
1590 release_bus(RELEASE_ONLY);
1591}
1592static int wilc_wlan_stop(void)
1593{
1594 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001595 u32 reg = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001596 int ret;
Chaehyun Lim51e825f2015-09-15 14:06:14 +09001597 u8 timeout = 10;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001598 /**
1599 * TODO: stop the firmware, need a re-download
1600 **/
1601 acquire_bus(ACQUIRE_AND_WAKEUP);
1602
1603 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1604 if (!ret) {
1605 PRINT_ER("Error while reading reg\n");
1606 release_bus(RELEASE_ALLOW_SLEEP);
1607 return ret;
1608 }
1609
1610 reg &= ~(1 << 10);
1611
1612
1613 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1614 if (!ret) {
1615 PRINT_ER("Error while writing reg\n");
1616 release_bus(RELEASE_ALLOW_SLEEP);
1617 return ret;
1618 }
1619
1620
1621
1622 do {
1623 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1624 if (!ret) {
1625 PRINT_ER("Error while reading reg\n");
1626 release_bus(RELEASE_ALLOW_SLEEP);
1627 return ret;
1628 }
1629 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1630 /*Workaround to ensure that the chip is actually reset*/
1631 if ((reg & (1 << 10))) {
1632 PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
1633 reg &= ~(1 << 10);
1634 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
1635 timeout--;
1636 } else {
1637 PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
1638 ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
1639 if (!ret) {
1640 PRINT_ER("Error while reading reg\n");
1641 release_bus(RELEASE_ALLOW_SLEEP);
1642 return ret;
1643 }
1644 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
1645 break;
1646 }
1647
1648 } while (timeout);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001649 reg = ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 8) | (1 << 9) | (1 << 26) | (1 << 29) | (1 << 30) | (1 << 31)); /**/
1650 /**/
Hari Prasath Gujulan Elango369f1902015-06-22 07:04:39 +00001651 p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001652 reg = ~(1 << 10); /**/
1653 /**/
1654 ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg); /**/
1655/******************************************************************************/
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001656
1657 release_bus(RELEASE_ALLOW_SLEEP);
1658
1659 return ret;
1660}
1661
1662static void wilc_wlan_cleanup(void)
1663{
1664 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1665 struct txq_entry_t *tqe;
1666 struct rxq_entry_t *rqe;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001667 u32 reg = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001668 int ret;
1669
1670 p->quit = 1;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001671 do {
1672 tqe = wilc_wlan_txq_remove_from_head();
1673 if (tqe == NULL)
1674 break;
1675 if (tqe->tx_complete_func)
1676 tqe->tx_complete_func(tqe->priv, 0);
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001677 kfree(tqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001678 } while (1);
1679
1680 do {
1681 rqe = wilc_wlan_rxq_remove();
1682 if (rqe == NULL)
1683 break;
Javier Martinez Canillas6a272242015-09-22 15:24:50 +02001684#ifndef MEMORY_STATIC
1685 kfree(rqe->buffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001686#endif
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001687 kfree(rqe);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001688 } while (1);
1689
1690 /**
1691 * clean up buffer
1692 **/
1693
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001694 #ifdef MEMORY_STATIC
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001695 kfree(p->rx_buffer);
1696 p->rx_buffer = NULL;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001697 #endif
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07001698 kfree(p->tx_buffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001699
1700 acquire_bus(ACQUIRE_AND_WAKEUP);
1701
1702
1703 ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, &reg);
1704 if (!ret) {
1705 PRINT_ER("Error while reading reg\n");
1706 release_bus(RELEASE_ALLOW_SLEEP);
1707 }
1708 PRINT_ER("Writing ABORT reg\n");
1709 ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
1710 if (!ret) {
1711 PRINT_ER("Error while writing reg\n");
1712 release_bus(RELEASE_ALLOW_SLEEP);
1713 }
1714 release_bus(RELEASE_ALLOW_SLEEP);
1715 /**
1716 * io clean up
1717 **/
1718 p->hif_func.hif_deinit(NULL);
1719
1720}
1721
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001722static int wilc_wlan_cfg_commit(int type, u32 drvHandler)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001723{
1724 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1725 wilc_cfg_frame_t *cfg = &p->cfg_frame;
1726 int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1727 int seq_no = p->cfg_seq_no % 256;
Chaehyun Lim4e4467f2015-06-11 14:35:55 +09001728 int driver_handler = (u32)drvHandler;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001729
1730
1731 /**
1732 * Set up header
1733 **/
1734 if (type == WILC_CFG_SET) { /* Set */
1735 cfg->wid_header[0] = 'W';
1736 } else { /* Query */
1737 cfg->wid_header[0] = 'Q';
1738 }
1739 cfg->wid_header[1] = seq_no; /* sequence number */
Chaehyun Lim51e825f2015-09-15 14:06:14 +09001740 cfg->wid_header[2] = (u8)total_len;
1741 cfg->wid_header[3] = (u8)(total_len >> 8);
1742 cfg->wid_header[4] = (u8)driver_handler;
1743 cfg->wid_header[5] = (u8)(driver_handler >> 8);
1744 cfg->wid_header[6] = (u8)(driver_handler >> 16);
1745 cfg->wid_header[7] = (u8)(driver_handler >> 24);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001746 p->cfg_seq_no = seq_no;
1747
1748 /**
1749 * Add to TX queue
1750 **/
1751
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001752 if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
1753 return -1;
1754
1755 return 0;
1756}
1757
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001758static int wilc_wlan_cfg_set(int start, u32 wid, u8 *buffer, u32 buffer_size, int commit, u32 drvHandler)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001759{
1760 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001761 u32 offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001762 int ret_size;
1763
1764
1765 if (p->cfg_frame_in_use)
1766 return 0;
1767
1768 if (start)
1769 p->cfg_frame_offset = 0;
1770
1771 offset = p->cfg_frame_offset;
Chaehyun Limec53adf2015-09-15 14:06:15 +09001772 ret_size = p->cif_func.cfg_wid_set(p->cfg_frame.frame, offset, (u16)wid, buffer, buffer_size);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001773 offset += ret_size;
1774 p->cfg_frame_offset = offset;
1775
1776 if (commit) {
1777 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
1778 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1779 p->cfg_frame_in_use = 1;
1780
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001781 if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
Chaehyun Lim5af6b852015-09-17 16:48:48 +09001782 ret_size = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001783
1784 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1785 PRINT_D(TX_DBG, "Set Timed Out\n");
1786 ret_size = 0;
1787 }
1788 p->cfg_frame_in_use = 0;
1789 p->cfg_frame_offset = 0;
1790 p->cfg_seq_no += 1;
1791
1792 }
1793
1794 return ret_size;
1795}
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001796static int wilc_wlan_cfg_get(int start, u32 wid, int commit, u32 drvHandler)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001797{
1798 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001799 u32 offset;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001800 int ret_size;
1801
1802
1803 if (p->cfg_frame_in_use)
1804 return 0;
1805
1806 if (start)
1807 p->cfg_frame_offset = 0;
1808
1809 offset = p->cfg_frame_offset;
Chaehyun Limec53adf2015-09-15 14:06:15 +09001810 ret_size = p->cif_func.cfg_wid_get(p->cfg_frame.frame, offset, (u16)wid);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001811 offset += ret_size;
1812 p->cfg_frame_offset = offset;
1813
1814 if (commit) {
1815 p->cfg_frame_in_use = 1;
1816
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001817 if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
Chaehyun Lim5af6b852015-09-17 16:48:48 +09001818 ret_size = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001819
1820
1821 if (p->os_func.os_wait(p->cfg_wait, CFG_PKTS_TIMEOUT)) {
1822 PRINT_D(TX_DBG, "Get Timed Out\n");
1823 ret_size = 0;
1824 }
1825 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1826 p->cfg_frame_in_use = 0;
1827 p->cfg_frame_offset = 0;
1828 p->cfg_seq_no += 1;
1829 }
1830
1831 return ret_size;
1832}
1833
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001834static int wilc_wlan_cfg_get_val(u32 wid, u8 *buffer, u32 buffer_size)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001835{
1836 wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
1837 int ret;
1838
Chaehyun Limec53adf2015-09-15 14:06:15 +09001839 ret = p->cif_func.cfg_wid_get_val((u16)wid, buffer, buffer_size);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001840
1841 return ret;
1842}
1843
1844void wilc_bus_set_max_speed(void)
1845{
1846
1847 /* Increase bus speed to max possible. */
1848 g_wlan.hif_func.hif_set_max_bus_speed();
1849}
1850
1851void wilc_bus_set_default_speed(void)
1852{
1853
1854 /* Restore bus speed to default. */
1855 g_wlan.hif_func.hif_set_default_bus_speed();
1856}
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001857u32 init_chip(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001858{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001859 u32 chipid;
1860 u32 reg, ret = 0;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001861
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001862 acquire_bus(ACQUIRE_ONLY);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001863
Dean Lee72ed4dc2015-06-12 14:11:44 +09001864 chipid = wilc_get_chipid(true);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001865
1866
1867
1868 if ((chipid & 0xfff) != 0xa0) {
1869 /**
1870 * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
1871 * or SD_DAT3 [SPI platform] to ?1?
1872 **/
1873 /* Set cortus reset register to register control. */
1874 ret = g_wlan.hif_func.hif_read_reg(0x1118, &reg);
1875 if (!ret) {
1876 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
1877 return ret;
1878 }
1879 reg |= (1 << 0);
1880 ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
1881 if (!ret) {
1882 wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
1883 return ret;
1884 }
1885 /**
1886 * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
1887 * (Cortus map) or C0000 (AHB map).
1888 **/
1889 ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
1890 if (!ret) {
1891 wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
1892 return ret;
1893 }
1894 }
1895
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001896 release_bus(RELEASE_ONLY);
1897
1898 return ret;
1899
1900}
1901
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001902u32 wilc_get_chipid(u8 update)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001903{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001904 static u32 chipid;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001905 /* SDIO can't read into global variables */
1906 /* Use this variable as a temp, then copy to the global */
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001907 u32 tempchipid = 0;
1908 u32 rfrevid;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001909
1910 if (chipid == 0 || update != 0) {
1911 g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
1912 g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
1913 if (!ISWILC1000(tempchipid)) {
1914 chipid = 0;
1915 goto _fail_;
1916 }
1917 if (tempchipid == 0x1002a0) {
1918 if (rfrevid == 0x1) { /* 1002A0 */
1919 } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
1920 tempchipid = 0x1002a1;
1921 }
1922 } else if (tempchipid == 0x1002b0) {
1923 if (rfrevid == 3) { /* 1002B0 */
1924 } else if (rfrevid == 4) { /* 1002B1 */
1925 tempchipid = 0x1002b1;
1926 } else { /* if(rfrevid == 5) */ /* 1002B2 */
1927 tempchipid = 0x1002b2;
1928 }
1929 } else {
1930 }
1931
1932 chipid = tempchipid;
1933 }
1934_fail_:
1935 return chipid;
1936}
1937
1938#ifdef COMPLEMENT_BOOT
Chaehyun Lim51e825f2015-09-15 14:06:14 +09001939u8 core_11b_ready(void)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001940{
Chaehyun Limfbc2fe12015-09-15 14:06:16 +09001941 u32 reg_val;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001942
1943 acquire_bus(ACQUIRE_ONLY);
1944 g_wlan.hif_func.hif_write_reg(0x16082c, 1);
1945 g_wlan.hif_func.hif_write_reg(0x161600, 0x90);
1946 g_wlan.hif_func.hif_read_reg(0x161600, &reg_val);
1947 release_bus(RELEASE_ONLY);
1948
1949 if (reg_val == 0x90)
1950 return 0;
1951 else
1952 return 1;
1953}
1954#endif
1955
1956int wilc_wlan_init(wilc_wlan_inp_t *inp, wilc_wlan_oup_t *oup)
1957{
1958
1959 int ret = 0;
1960
1961 PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
1962
1963 memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
1964
1965 /**
1966 * store the input
1967 **/
1968 memcpy((void *)&g_wlan.os_func, (void *)&inp->os_func, sizeof(wilc_wlan_os_func_t));
1969 memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001970 g_wlan.hif_lock = inp->os_context.hif_critical_section;
1971 g_wlan.txq_lock = inp->os_context.txq_critical_section;
1972
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001973 g_wlan.txq_add_to_head_lock = inp->os_context.txq_add_to_head_critical_section;
1974
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001975 g_wlan.cfg_wait = inp->os_context.cfg_wait_event;
1976 g_wlan.tx_buffer_size = inp->os_context.tx_buffer_size;
1977#if defined (MEMORY_STATIC)
1978 g_wlan.rx_buffer_size = inp->os_context.rx_buffer_size;
1979#endif
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001980 /***
1981 * host interface init
1982 **/
Johnny Kimc5c77ba2015-05-11 14:30:56 +09001983 if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
1984 if (!hif_sdio.hif_init(inp, wilc_debug)) {
1985 /* EIO 5 */
1986 ret = -5;
1987 goto _fail_;
1988 }
1989 memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
1990 } else {
1991 if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
1992 /**
1993 * TODO:
1994 **/
1995 if (!hif_spi.hif_init(inp, wilc_debug)) {
1996 /* EIO 5 */
1997 ret = -5;
1998 goto _fail_;
1999 }
2000 memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
2001 } else {
2002 /* EIO 5 */
2003 ret = -5;
2004 goto _fail_;
2005 }
2006 }
2007
2008 /***
2009 * mac interface init
2010 **/
2011 if (!mac_cfg.cfg_init(wilc_debug)) {
2012 /* ENOBUFS 105 */
2013 ret = -105;
2014 goto _fail_;
2015 }
2016 memcpy((void *)&g_wlan.cif_func, &mac_cfg, sizeof(wilc_cfg_func_t));
2017
2018
2019 /**
2020 * alloc tx, rx buffer
2021 **/
Greg Kroah-Hartmanb1413b62015-06-02 14:11:12 +09002022 if (g_wlan.tx_buffer == NULL)
Greg Kroah-Hartmanf019b9d2015-09-03 18:50:27 -07002023 g_wlan.tx_buffer = kmalloc(g_wlan.tx_buffer_size, GFP_KERNEL);
Arnd Bergmann7a8fd842015-06-01 21:06:45 +02002024 PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002025
Greg Kroah-Hartmanb1413b62015-06-02 14:11:12 +09002026 if (g_wlan.tx_buffer == NULL) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002027 /* ENOBUFS 105 */
2028 ret = -105;
2029 PRINT_ER("Can't allocate Tx Buffer");
2030 goto _fail_;
2031 }
2032
2033/* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
2034#if defined (MEMORY_STATIC)
Greg Kroah-Hartmanb1413b62015-06-02 14:11:12 +09002035 if (g_wlan.rx_buffer == NULL)
Greg Kroah-Hartmanf019b9d2015-09-03 18:50:27 -07002036 g_wlan.rx_buffer = kmalloc(g_wlan.rx_buffer_size, GFP_KERNEL);
Arnd Bergmann7a8fd842015-06-01 21:06:45 +02002037 PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
Greg Kroah-Hartmanb1413b62015-06-02 14:11:12 +09002038 if (g_wlan.rx_buffer == NULL) {
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002039 /* ENOBUFS 105 */
2040 ret = -105;
2041 PRINT_ER("Can't allocate Rx Buffer");
2042 goto _fail_;
2043 }
2044#endif
2045
2046 /**
2047 * export functions
2048 **/
2049 oup->wlan_firmware_download = wilc_wlan_firmware_download;
2050 oup->wlan_start = wilc_wlan_start;
2051 oup->wlan_stop = wilc_wlan_stop;
2052 oup->wlan_add_to_tx_que = wilc_wlan_txq_add_net_pkt;
2053 oup->wlan_handle_tx_que = wilc_wlan_handle_txq;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002054 oup->wlan_handle_rx_isr = wilc_handle_isr;
2055 oup->wlan_cleanup = wilc_wlan_cleanup;
2056 oup->wlan_cfg_set = wilc_wlan_cfg_set;
2057 oup->wlan_cfg_get = wilc_wlan_cfg_get;
2058 oup->wlan_cfg_get_value = wilc_wlan_cfg_get_val;
2059
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002060 oup->wlan_add_mgmt_to_tx_que = wilc_wlan_txq_add_mgmt_pkt;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002061
2062 if (!init_chip()) {
2063 /* EIO 5 */
2064 ret = -5;
2065 goto _fail_;
2066 }
2067#ifdef TCP_ACK_FILTER
2068 Init_TCP_tracking();
2069#endif
2070
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002071 return 1;
2072
2073_fail_:
2074
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002075 #ifdef MEMORY_STATIC
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07002076 kfree(g_wlan.rx_buffer);
2077 g_wlan.rx_buffer = NULL;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002078 #endif
Greg Kroah-Hartmana18dd632015-09-03 19:04:19 -07002079 kfree(g_wlan.tx_buffer);
2080 g_wlan.tx_buffer = NULL;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002081
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002082 return ret;
2083
2084}
2085
Dean Lee72ed4dc2015-06-12 14:11:44 +09002086u16 Set_machw_change_vir_if(bool bValue)
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002087{
Chaehyun Limd85f5322015-06-11 14:35:54 +09002088 u16 ret;
Chaehyun Lim4e4467f2015-06-11 14:35:55 +09002089 u32 reg;
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002090
2091 /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
Greg Kroah-Hartman5e150b52015-09-03 19:32:11 -07002092 mutex_lock((&g_wlan)->hif_lock);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002093 ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, &reg);
2094 if (!ret) {
2095 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
2096 }
2097
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05302098 if (bValue)
Chaehyun Lim50b51da2015-09-16 20:11:26 +09002099 reg |= BIT(31);
Chandra S Gorentla78174ad2015-08-08 17:41:36 +05302100 else
Chaehyun Lim50b51da2015-09-16 20:11:26 +09002101 reg &= ~BIT(31);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002102
2103 ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
2104
2105 if (!ret) {
2106 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
2107 }
Greg Kroah-Hartman5e150b52015-09-03 19:32:11 -07002108 mutex_unlock((&g_wlan)->hif_lock);
Johnny Kimc5c77ba2015-05-11 14:30:56 +09002109
2110 return ret;
2111}