1 #include "wilc_wlan_if.h"
3 #include "wilc_wfi_netdevice.h"
4 #include "wilc_wlan_cfg.h"
6 #ifdef WILC_OPTIMIZE_SLEEP_INT
7 static inline void chip_allow_sleep(struct wilc *wilc);
9 static inline void chip_wakeup(struct wilc *wilc);
10 static u32 dbgflag = N_INIT | N_ERR | N_INTR | N_TXQ | N_RXQ;
12 /* FIXME: replace with dev_debug() */
13 static void wilc_debug(u32 flag, char *fmt, ...)
20 vsprintf(buf, fmt, args);
27 static CHIP_PS_STATE_T chip_ps_state = CHIP_WAKEDUP;
29 static inline void acquire_bus(struct wilc *wilc, BUS_ACQUIRE_T acquire)
31 mutex_lock(&wilc->hif_cs);
32 #ifndef WILC_OPTIMIZE_SLEEP_INT
33 if (chip_ps_state != CHIP_WAKEDUP)
36 if (acquire == ACQUIRE_AND_WAKEUP)
41 static inline void release_bus(struct wilc *wilc, BUS_RELEASE_T release)
43 #ifdef WILC_OPTIMIZE_SLEEP_INT
44 if (release == RELEASE_ALLOW_SLEEP)
45 chip_allow_sleep(wilc);
47 mutex_unlock(&wilc->hif_cs);
51 static void wilc_wlan_txq_remove(struct txq_entry_t *tqe)
54 if (tqe == wilc->txq_head) {
55 wilc->txq_head = tqe->next;
57 wilc->txq_head->prev = NULL;
58 } else if (tqe == wilc->txq_tail) {
59 wilc->txq_tail = (tqe->prev);
61 wilc->txq_tail->next = NULL;
63 tqe->prev->next = tqe->next;
64 tqe->next->prev = tqe->prev;
66 wilc->txq_entries -= 1;
70 static struct txq_entry_t *
71 wilc_wlan_txq_remove_from_head(struct net_device *dev)
73 struct txq_entry_t *tqe;
78 vif = netdev_priv(dev);
81 spin_lock_irqsave(&wilc->txq_spinlock, flags);
84 wilc->txq_head = tqe->next;
86 wilc->txq_head->prev = NULL;
88 wilc->txq_entries -= 1;
92 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
96 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
97 struct txq_entry_t *tqe)
100 struct wilc_vif *vif;
103 vif = netdev_priv(dev);
106 spin_lock_irqsave(&wilc->txq_spinlock, flags);
108 if (!wilc->txq_head) {
111 wilc->txq_head = tqe;
112 wilc->txq_tail = tqe;
115 tqe->prev = wilc->txq_tail;
116 wilc->txq_tail->next = tqe;
117 wilc->txq_tail = tqe;
119 wilc->txq_entries += 1;
120 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", wilc->txq_entries);
122 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
124 PRINT_D(TX_DBG, "Wake the txq_handling\n");
126 up(&wilc->txq_event);
129 static int wilc_wlan_txq_add_to_head(struct wilc *wilc, struct txq_entry_t *tqe)
132 if (wilc_lock_timeout(wilc, &wilc->txq_add_to_head_cs,
136 spin_lock_irqsave(&wilc->txq_spinlock, flags);
138 if (!wilc->txq_head) {
141 wilc->txq_head = tqe;
142 wilc->txq_tail = tqe;
144 tqe->next = wilc->txq_head;
146 wilc->txq_head->prev = tqe;
147 wilc->txq_head = tqe;
149 wilc->txq_entries += 1;
150 PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", wilc->txq_entries);
152 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
153 up(&wilc->txq_add_to_head_cs);
154 up(&wilc->txq_event);
155 PRINT_D(TX_DBG, "Wake up the txq_handler\n");
160 #ifdef TCP_ACK_FILTER
161 struct ack_session_info;
162 struct ack_session_info {
170 struct pending_acks_info {
173 struct txq_entry_t *txqe;
177 #define NOT_TCP_ACK (-1)
179 #define MAX_TCP_SESSION 25
180 #define MAX_PENDING_ACKS 256
181 static struct ack_session_info ack_session_info[2 * MAX_TCP_SESSION];
182 static struct pending_acks_info pending_acks_info[MAX_PENDING_ACKS];
184 static u32 pending_base;
185 static u32 tcp_session;
186 static u32 pending_acks;
188 static inline int init_tcp_tracking(void)
193 static inline int add_tcp_session(u32 src_prt, u32 dst_prt, u32 seq)
195 ack_session_info[tcp_session].seq_num = seq;
196 ack_session_info[tcp_session].bigger_ack_num = 0;
197 ack_session_info[tcp_session].src_port = src_prt;
198 ack_session_info[tcp_session].dst_port = dst_prt;
201 PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", tcp_session, seq);
205 static inline int update_tcp_session(u32 index, u32 ack)
207 if (ack > ack_session_info[index].bigger_ack_num)
208 ack_session_info[index].bigger_ack_num = ack;
212 static inline int add_tcp_pending_ack(u32 ack, u32 session_index,
213 struct txq_entry_t *txqe)
215 if (pending_acks < MAX_PENDING_ACKS) {
216 pending_acks_info[pending_base + pending_acks].ack_num = ack;
217 pending_acks_info[pending_base + pending_acks].txqe = txqe;
218 pending_acks_info[pending_base + pending_acks].session_index = session_index;
219 txqe->tcp_pending_ack_idx = pending_base + pending_acks;
224 static inline int remove_TCP_related(struct wilc *wilc)
228 spin_lock_irqsave(&wilc->txq_spinlock, flags);
230 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
234 static inline int tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
238 u8 *buffer = tqe->buffer;
239 unsigned short h_proto;
242 struct wilc_vif *vif;
245 vif = netdev_priv(dev);
249 eth_hdr_ptr = &buffer[0];
250 h_proto = ntohs(*((unsigned short *)ð_hdr_ptr[12]));
251 if (h_proto == 0x0800) {
255 ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
256 protocol = ip_hdr_ptr[9];
258 if (protocol == 0x06) {
260 u32 IHL, total_length, data_offset;
262 tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
263 IHL = (ip_hdr_ptr[0] & 0xf) << 2;
264 total_length = ((u32)ip_hdr_ptr[2] << 8) +
266 data_offset = ((u32)tcp_hdr_ptr[12] & 0xf0) >> 2;
267 if (total_length == (IHL + data_offset)) {
270 seq_no = ((u32)tcp_hdr_ptr[4] << 24) +
271 ((u32)tcp_hdr_ptr[5] << 16) +
272 ((u32)tcp_hdr_ptr[6] << 8) +
275 ack_no = ((u32)tcp_hdr_ptr[8] << 24) +
276 ((u32)tcp_hdr_ptr[9] << 16) +
277 ((u32)tcp_hdr_ptr[10] << 8) +
278 (u32)tcp_hdr_ptr[11];
280 for (i = 0; i < tcp_session; i++) {
281 if (ack_session_info[i].seq_num == seq_no) {
282 update_tcp_session(i, ack_no);
286 if (i == tcp_session)
287 add_tcp_session(0, 0, seq_no);
289 add_tcp_pending_ack(ack_no, i, tqe);
298 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
302 static int wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
304 struct wilc_vif *vif;
309 vif = netdev_priv(dev);
312 spin_lock_irqsave(&wilc->txq_spinlock, wilc->txq_spinlock_flags);
313 for (i = pending_base; i < (pending_base + pending_acks); i++) {
314 if (pending_acks_info[i].ack_num < ack_session_info[pending_acks_info[i].session_index].bigger_ack_num) {
315 struct txq_entry_t *tqe;
317 PRINT_D(TCP_ENH, "DROP ACK: %u\n",
318 pending_acks_info[i].ack_num);
319 tqe = pending_acks_info[i].txqe;
321 wilc_wlan_txq_remove(tqe);
323 if (tqe->tx_complete_func)
324 tqe->tx_complete_func(tqe->priv,
334 if (pending_base == 0)
335 pending_base = MAX_TCP_SESSION;
339 spin_unlock_irqrestore(&wilc->txq_spinlock, wilc->txq_spinlock_flags);
341 while (dropped > 0) {
342 wilc_lock_timeout(wilc, &wilc->txq_event, 1);
350 static bool enabled = false;
352 void wilc_enable_tcp_ack_filter(bool value)
357 #ifdef TCP_ACK_FILTER
358 static bool is_tcp_ack_filter_enabled(void)
364 static int wilc_wlan_txq_add_cfg_pkt(struct wilc *wilc, u8 *buffer, u32 buffer_size)
366 struct txq_entry_t *tqe;
368 PRINT_D(TX_DBG, "Adding config packet ...\n");
370 PRINT_D(TX_DBG, "Return due to clear function\n");
371 up(&wilc->cfg_event);
375 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
377 PRINT_ER("Failed to allocate memory\n");
381 tqe->type = WILC_CFG_PKT;
382 tqe->buffer = buffer;
383 tqe->buffer_size = buffer_size;
384 tqe->tx_complete_func = NULL;
386 #ifdef TCP_ACK_FILTER
387 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
389 PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
391 if (wilc_wlan_txq_add_to_head(wilc, tqe))
396 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
397 u32 buffer_size, wilc_tx_complete_func_t func)
399 struct txq_entry_t *tqe;
400 struct wilc_vif *vif = netdev_priv(dev);
408 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
412 tqe->type = WILC_NET_PKT;
413 tqe->buffer = buffer;
414 tqe->buffer_size = buffer_size;
415 tqe->tx_complete_func = func;
418 PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
419 #ifdef TCP_ACK_FILTER
420 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
421 if (is_tcp_ack_filter_enabled())
422 tcp_process(dev, tqe);
424 wilc_wlan_txq_add_to_tail(dev, tqe);
425 return wilc->txq_entries;
428 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
429 u32 buffer_size, wilc_tx_complete_func_t func)
431 struct txq_entry_t *tqe;
432 struct wilc_vif *vif = netdev_priv(dev);
440 tqe = kmalloc(sizeof(*tqe), GFP_KERNEL);
444 tqe->type = WILC_MGMT_PKT;
445 tqe->buffer = buffer;
446 tqe->buffer_size = buffer_size;
447 tqe->tx_complete_func = func;
449 #ifdef TCP_ACK_FILTER
450 tqe->tcp_pending_ack_idx = NOT_TCP_ACK;
452 PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
453 wilc_wlan_txq_add_to_tail(dev, tqe);
457 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
459 struct txq_entry_t *tqe;
462 spin_lock_irqsave(&wilc->txq_spinlock, flags);
464 tqe = wilc->txq_head;
466 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
471 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
472 struct txq_entry_t *tqe)
476 spin_lock_irqsave(&wilc->txq_spinlock, flags);
479 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
484 static int wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
490 mutex_lock(&wilc->rxq_cs);
491 if (!wilc->rxq_head) {
492 PRINT_D(RX_DBG, "Add to Queue head\n");
494 wilc->rxq_head = rqe;
495 wilc->rxq_tail = rqe;
497 PRINT_D(RX_DBG, "Add to Queue tail\n");
498 wilc->rxq_tail->next = rqe;
500 wilc->rxq_tail = rqe;
502 wilc->rxq_entries += 1;
503 PRINT_D(RX_DBG, "Number of queue entries: %d\n", wilc->rxq_entries);
504 mutex_unlock(&wilc->rxq_cs);
505 return wilc->rxq_entries;
508 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
511 PRINT_D(RX_DBG, "Getting rxQ element\n");
512 if (wilc->rxq_head) {
513 struct rxq_entry_t *rqe;
515 mutex_lock(&wilc->rxq_cs);
516 rqe = wilc->rxq_head;
517 wilc->rxq_head = wilc->rxq_head->next;
518 wilc->rxq_entries -= 1;
519 PRINT_D(RX_DBG, "RXQ entries decreased\n");
520 mutex_unlock(&wilc->rxq_cs);
523 PRINT_D(RX_DBG, "Nothing to get from Q\n");
527 #ifdef WILC_OPTIMIZE_SLEEP_INT
529 static inline void chip_allow_sleep(struct wilc *wilc)
533 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
535 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
538 static inline void chip_wakeup(struct wilc *wilc)
540 u32 reg, clk_status_reg, trials = 0;
543 if ((wilc->io_type & 0x1) == HIF_SPI) {
545 wilc->hif_func->hif_read_reg(wilc, 1, ®);
546 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
547 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
550 usleep_range(2 * 1000, 2 * 1000);
551 if ((wilc_get_chipid(wilc, true) == 0))
552 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
554 } while ((wilc_get_chipid(wilc, true) == 0) && ((++trials % 3) == 0));
556 } while (wilc_get_chipid(wilc, true) == 0);
557 } else if ((wilc->io_type & 0x1) == HIF_SDIO) {
558 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
560 wilc->hif_func->hif_write_reg(wilc, 0xf0,
562 wilc->hif_func->hif_read_reg(wilc, 0xf1,
565 while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
566 usleep_range(2 * 1000, 2 * 1000);
568 wilc->hif_func->hif_read_reg(wilc, 0xf1,
571 if ((clk_status_reg & 0x1) == 0)
572 wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
574 if ((clk_status_reg & 0x1) == 0) {
575 wilc->hif_func->hif_write_reg(wilc, 0xf0,
578 } while ((clk_status_reg & 0x1) == 0);
581 if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
582 wilc->hif_func->hif_read_reg(wilc, 0x1C0C, ®);
584 wilc->hif_func->hif_write_reg(wilc, 0x1C0C, reg);
586 if (wilc_get_chipid(wilc, false) >= 0x1002b0) {
589 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
591 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
593 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
595 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
598 chip_ps_state = CHIP_WAKEDUP;
601 static inline void chip_wakeup(struct wilc *wilc)
606 if ((wilc->io_type & 0x1) == HIF_SPI) {
607 wilc->hif_func->hif_read_reg(wilc, 1, ®);
608 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
609 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
610 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
611 } else if ((wilc->io_type & 0x1) == HIF_SDIO) {
612 wilc->hif_func->hif_read_reg(wilc, 0xf0, ®);
613 wilc->hif_func->hif_write_reg(wilc, 0xf0,
615 wilc->hif_func->hif_write_reg(wilc, 0xf0,
617 wilc->hif_func->hif_write_reg(wilc, 0xf0,
624 if ((wilc_get_chipid(wilc, true) == 0))
625 wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
627 } while ((wilc_get_chipid(wilc, true) == 0) && ((++trials % 3) == 0));
629 } while (wilc_get_chipid(wilc, true) == 0);
631 if (chip_ps_state == CHIP_SLEEPING_MANUAL) {
632 wilc->hif_func->hif_read_reg(wilc, 0x1C0C, ®);
634 wilc->hif_func->hif_write_reg(wilc, 0x1C0C, reg);
636 if (wilc_get_chipid(wilc, false) >= 0x1002b0) {
639 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
641 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
643 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
645 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
648 chip_ps_state = CHIP_WAKEDUP;
651 void wilc_chip_sleep_manually(struct wilc *wilc)
653 if (chip_ps_state != CHIP_WAKEDUP)
655 acquire_bus(wilc, ACQUIRE_ONLY);
657 #ifdef WILC_OPTIMIZE_SLEEP_INT
658 chip_allow_sleep(wilc);
660 wilc->hif_func->hif_write_reg(wilc, 0x10a8, 1);
662 chip_ps_state = CHIP_SLEEPING_MANUAL;
663 release_bus(wilc, RELEASE_ONLY);
666 int wilc_wlan_handle_txq(struct net_device *dev, u32 *txq_count)
674 struct txq_entry_t *tqe;
678 u32 vmm_table[WILC_VMM_TBL_SIZE];
679 struct wilc_vif *vif;
682 vif = netdev_priv(dev);
685 txb = wilc->tx_buffer;
691 wilc_lock_timeout(wilc, &wilc->txq_add_to_head_cs,
693 #ifdef TCP_ACK_FILTER
694 wilc_wlan_txq_filter_dup_tcp_ack(dev);
696 PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
697 tqe = wilc_wlan_txq_get_first(wilc);
701 if (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
702 if (tqe->type == WILC_CFG_PKT)
703 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
705 else if (tqe->type == WILC_NET_PKT)
706 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
709 vmm_sz = HOST_HDR_OFFSET;
711 vmm_sz += tqe->buffer_size;
712 PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
714 vmm_sz = (vmm_sz + 4) & ~0x3;
716 if ((sum + vmm_sz) > LINUX_TX_SIZE)
719 PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
720 vmm_table[i] = vmm_sz / 4;
721 PRINT_D(TX_DBG, "VMMTable entry size = %d\n",
724 if (tqe->type == WILC_CFG_PKT) {
725 vmm_table[i] |= BIT(10);
726 PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
728 vmm_table[i] = cpu_to_le32(vmm_table[i]);
732 PRINT_D(TX_DBG, "sum = %d\n", sum);
733 tqe = wilc_wlan_txq_get_next(wilc, tqe);
740 PRINT_D(TX_DBG, "Nothing in TX-Q\n");
743 PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
746 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
749 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_TX_CTRL,
752 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
756 if ((reg & 0x1) == 0) {
757 PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
763 PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
764 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
767 PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
768 release_bus(wilc, RELEASE_ALLOW_SLEEP);
769 usleep_range(3000, 3000);
770 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
772 } while (!wilc->quit);
779 ret = wilc->hif_func->hif_block_tx(wilc, WILC_VMM_TBL_RX_SHADOW_BASE, (u8 *)vmm_table, ((i + 1) * 4));
781 wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
785 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_VMM_CTL,
788 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
793 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
795 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
798 if ((reg >> 2) & 0x1) {
799 entries = ((reg >> 3) & 0x3f);
802 release_bus(wilc, RELEASE_ALLOW_SLEEP);
803 usleep_range(3000, 3000);
804 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
805 PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
809 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
817 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]);
819 ret = wilc->hif_func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
821 wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
825 ret = wilc->hif_func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
827 wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
840 ret = WILC_TX_ERR_NO_BUF;
844 release_bus(wilc, RELEASE_ALLOW_SLEEP);
849 tqe = wilc_wlan_txq_remove_from_head(dev);
850 if (tqe && (vmm_table[i] != 0)) {
851 u32 header, buffer_offset;
853 vmm_table[i] = cpu_to_le32(vmm_table[i]);
854 vmm_sz = (vmm_table[i] & 0x3ff);
856 header = (tqe->type << 31) |
857 (tqe->buffer_size << 15) |
859 if (tqe->type == WILC_MGMT_PKT)
864 header = cpu_to_le32(header);
865 memcpy(&txb[offset], &header, 4);
866 if (tqe->type == WILC_CFG_PKT) {
867 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
868 } else if (tqe->type == WILC_NET_PKT) {
869 char *bssid = ((struct tx_complete_data *)(tqe->priv))->pBssid;
871 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
872 memcpy(&txb[offset + 4], bssid, 6);
874 buffer_offset = HOST_HDR_OFFSET;
877 memcpy(&txb[offset + buffer_offset],
878 tqe->buffer, tqe->buffer_size);
882 if (tqe->tx_complete_func)
883 tqe->tx_complete_func(tqe->priv,
885 #ifdef TCP_ACK_FILTER
886 if (tqe->tcp_pending_ack_idx != NOT_TCP_ACK)
887 pending_acks_info[tqe->tcp_pending_ack_idx].txqe = NULL;
895 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
897 ret = wilc->hif_func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
899 wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
903 ret = wilc->hif_func->hif_block_tx_ext(wilc, 0, txb, offset);
905 wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
911 release_bus(wilc, RELEASE_ALLOW_SLEEP);
915 up(&wilc->txq_add_to_head_cs);
918 PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
919 *txq_count = wilc->txq_entries;
923 static void wilc_wlan_handle_rxq(struct wilc *wilc)
925 int offset = 0, size, has_packet = 0;
927 struct rxq_entry_t *rqe;
933 PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function\n");
934 up(&wilc->cfg_event);
937 rqe = wilc_wlan_rxq_remove(wilc);
939 PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
942 buffer = rqe->buffer;
943 size = rqe->buffer_size;
944 PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n",
950 u32 pkt_len, pkt_offset, tp_len;
953 PRINT_D(RX_DBG, "In the 2nd do-while\n");
954 memcpy(&header, &buffer[offset], 4);
955 header = cpu_to_le32(header);
956 PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n",
959 is_cfg_packet = (header >> 31) & 0x1;
960 pkt_offset = (header >> 22) & 0x1ff;
961 tp_len = (header >> 11) & 0x7ff;
962 pkt_len = header & 0x7ff;
964 if (pkt_len == 0 || tp_len == 0) {
965 wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
969 #define IS_MANAGMEMENT 0x100
970 #define IS_MANAGMEMENT_CALLBACK 0x080
971 #define IS_MGMT_STATUS_SUCCES 0x040
973 if (pkt_offset & IS_MANAGMEMENT) {
974 pkt_offset &= ~(IS_MANAGMEMENT |
975 IS_MANAGMEMENT_CALLBACK |
976 IS_MGMT_STATUS_SUCCES);
978 WILC_WFI_mgmt_rx(wilc, &buffer[offset + HOST_HDR_OFFSET], pkt_len);
980 if (!is_cfg_packet) {
982 wilc_frmw_to_linux(wilc,
989 struct wilc_cfg_rsp rsp;
991 wilc_wlan_cfg_indicate_rx(wilc, &buffer[pkt_offset + offset], pkt_len, &rsp);
992 if (rsp.type == WILC_CFG_RSP) {
993 PRINT_D(RX_DBG, "wilc->cfg_seq_no = %d - rsp.seq_no = %d\n", wilc->cfg_seq_no, rsp.seq_no);
994 if (wilc->cfg_seq_no == rsp.seq_no)
995 up(&wilc->cfg_event);
996 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
997 wilc_mac_indicate(wilc, WILC_MAC_INDICATE_STATUS);
999 } else if (rsp.type == WILC_CFG_RSP_SCAN) {
1000 wilc_mac_indicate(wilc, WILC_MAC_INDICATE_SCAN);
1011 wilc_rx_complete(wilc);
1016 PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
1019 static void wilc_unknown_isr_ext(struct wilc *wilc)
1021 wilc->hif_func->hif_clear_int_ext(wilc, 0);
1024 static void wilc_pllupdate_isr_ext(struct wilc *wilc, u32 int_stats)
1028 wilc->hif_func->hif_clear_int_ext(wilc, PLL_INT_CLR);
1030 if (wilc->io_type == HIF_SDIO)
1031 mdelay(WILC_PLL_TO_SDIO);
1033 mdelay(WILC_PLL_TO_SPI);
1035 while (!(ISWILC1000(wilc_get_chipid(wilc, true)) && --trials)) {
1036 PRINT_D(TX_DBG, "PLL update retrying\n");
1041 static void wilc_sleeptimer_isr_ext(struct wilc *wilc, u32 int_stats1)
1043 wilc->hif_func->hif_clear_int_ext(wilc, SLEEP_INT_CLR);
1044 #ifndef WILC_OPTIMIZE_SLEEP_INT
1045 chip_ps_state = CHIP_SLEEPING_AUTO;
1049 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
1051 u32 offset = wilc->rx_buffer_offset;
1056 struct rxq_entry_t *rqe;
1058 size = ((int_status & 0x7fff) << 2);
1060 while (!size && retries < 10) {
1063 wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
1064 wilc->hif_func->hif_read_size(wilc, &size);
1065 size = ((size & 0x7fff) << 2);
1070 if (LINUX_RX_SIZE - offset < size)
1073 if (wilc->rx_buffer) {
1074 buffer = &wilc->rx_buffer[offset];
1076 wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
1080 wilc->hif_func->hif_clear_int_ext(wilc,
1081 DATA_INT_CLR | ENABLE_RX_VMM);
1082 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
1085 wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
1091 wilc->rx_buffer_offset = offset;
1092 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
1094 rqe->buffer = buffer;
1095 rqe->buffer_size = size;
1096 PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
1097 wilc_wlan_rxq_add(wilc, rqe);
1101 wilc_wlan_handle_rxq(wilc);
1104 void wilc_handle_isr(struct wilc *wilc)
1108 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1109 wilc->hif_func->hif_read_int(wilc, &int_status);
1111 if (int_status & PLL_INT_EXT)
1112 wilc_pllupdate_isr_ext(wilc, int_status);
1114 if (int_status & DATA_INT_EXT) {
1115 wilc_wlan_handle_isr_ext(wilc, int_status);
1116 #ifndef WILC_OPTIMIZE_SLEEP_INT
1117 chip_ps_state = CHIP_WAKEDUP;
1120 if (int_status & SLEEP_INT_EXT)
1121 wilc_sleeptimer_isr_ext(wilc, int_status);
1123 if (!(int_status & (ALL_INT_EXT))) {
1124 wilc_unknown_isr_ext(wilc);
1126 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1128 EXPORT_SYMBOL_GPL(wilc_handle_isr);
1130 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer, u32 buffer_size)
1133 u32 addr, size, size2, blksz;
1139 dma_buffer = kmalloc(blksz, GFP_KERNEL);
1142 PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
1146 PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
1150 memcpy(&addr, &buffer[offset], 4);
1151 memcpy(&size, &buffer[offset + 4], 4);
1152 addr = cpu_to_le32(addr);
1153 size = cpu_to_le32(size);
1154 acquire_bus(wilc, ACQUIRE_ONLY);
1156 while (((int)size) && (offset < buffer_size)) {
1162 memcpy(dma_buffer, &buffer[offset], size2);
1163 ret = wilc->hif_func->hif_block_tx(wilc, addr, dma_buffer,
1172 release_bus(wilc, RELEASE_ONLY);
1176 PRINT_ER("Can't download firmware IO error\n ");
1179 PRINT_D(INIT_DBG, "Offset = %d\n", offset);
1180 } while (offset < buffer_size);
1188 return (ret < 0) ? ret : 0;
1191 int wilc_wlan_start(struct wilc *wilc)
1197 if (wilc->io_type == HIF_SDIO) {
1200 } else if (wilc->io_type == HIF_SPI) {
1203 acquire_bus(wilc, ACQUIRE_ONLY);
1204 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1206 wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
1207 release_bus(wilc, RELEASE_ONLY);
1212 if (wilc->io_type == HIF_SDIO && wilc->dev_irq_num)
1213 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1215 #ifdef WILC_DISABLE_PMU
1217 reg |= WILC_HAVE_USE_PMU;
1220 #ifdef WILC_SLEEP_CLK_SRC_XO
1221 reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
1222 #elif defined WILC_SLEEP_CLK_SRC_RTC
1223 reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
1226 #ifdef WILC_EXT_PA_INV_TX_RX
1227 reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
1230 reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
1232 reg |= WILC_HAVE_XTAL_24;
1234 #ifdef DISABLE_WILC_UART
1235 reg |= WILC_HAVE_DISABLE_WILC_UART;
1238 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1240 wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
1241 release_bus(wilc, RELEASE_ONLY);
1246 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1248 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
1250 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
1251 release_bus(wilc, RELEASE_ONLY);
1256 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1257 if ((reg & BIT(10)) == BIT(10)) {
1259 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1260 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1264 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1265 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1266 release_bus(wilc, RELEASE_ONLY);
1268 return (ret < 0) ? ret : 0;
1271 void wilc_wlan_global_reset(struct wilc *wilc)
1273 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1274 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, 0x0);
1275 release_bus(wilc, RELEASE_ONLY);
1277 int wilc_wlan_stop(struct wilc *wilc)
1282 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1284 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1286 PRINT_ER("Error while reading reg\n");
1287 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1292 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1294 PRINT_ER("Error while writing reg\n");
1295 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1300 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1302 PRINT_ER("Error while reading reg\n");
1303 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1306 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n",
1309 if ((reg & BIT(10))) {
1310 PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n",
1313 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0,
1317 PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n",
1319 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0,
1322 PRINT_ER("Error while reading reg\n");
1323 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1326 PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n",
1332 reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
1333 BIT(29) | BIT(30) | BIT(31));
1335 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1336 reg = (u32)~BIT(10);
1338 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1340 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1345 void wilc_wlan_cleanup(struct net_device *dev)
1347 struct txq_entry_t *tqe;
1348 struct rxq_entry_t *rqe;
1351 struct wilc_vif *vif;
1354 vif = netdev_priv(dev);
1359 tqe = wilc_wlan_txq_remove_from_head(dev);
1362 if (tqe->tx_complete_func)
1363 tqe->tx_complete_func(tqe->priv, 0);
1368 rqe = wilc_wlan_rxq_remove(wilc);
1374 kfree(wilc->rx_buffer);
1375 wilc->rx_buffer = NULL;
1376 kfree(wilc->tx_buffer);
1377 wilc->tx_buffer = NULL;
1379 acquire_bus(wilc, ACQUIRE_AND_WAKEUP);
1381 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1383 PRINT_ER("Error while reading reg\n");
1384 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1386 PRINT_ER("Writing ABORT reg\n");
1387 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1390 PRINT_ER("Error while writing reg\n");
1391 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1393 release_bus(wilc, RELEASE_ALLOW_SLEEP);
1394 wilc->hif_func->hif_deinit(NULL);
1397 static int wilc_wlan_cfg_commit(struct wilc *wilc, int type, u32 drv_handler)
1399 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1400 int total_len = wilc->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
1401 int seq_no = wilc->cfg_seq_no % 256;
1402 int driver_handler = (u32)drv_handler;
1404 if (type == WILC_CFG_SET)
1405 cfg->wid_header[0] = 'W';
1407 cfg->wid_header[0] = 'Q';
1408 cfg->wid_header[1] = seq_no;
1409 cfg->wid_header[2] = (u8)total_len;
1410 cfg->wid_header[3] = (u8)(total_len >> 8);
1411 cfg->wid_header[4] = (u8)driver_handler;
1412 cfg->wid_header[5] = (u8)(driver_handler >> 8);
1413 cfg->wid_header[6] = (u8)(driver_handler >> 16);
1414 cfg->wid_header[7] = (u8)(driver_handler >> 24);
1415 wilc->cfg_seq_no = seq_no;
1417 if (!wilc_wlan_txq_add_cfg_pkt(wilc, &cfg->wid_header[0], total_len))
1423 int wilc_wlan_cfg_set(struct wilc *wilc, int start, u32 wid, u8 *buffer,
1424 u32 buffer_size, int commit, u32 drv_handler)
1429 if (wilc->cfg_frame_in_use)
1433 wilc->cfg_frame_offset = 0;
1435 offset = wilc->cfg_frame_offset;
1436 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1437 (u16)wid, buffer, buffer_size);
1439 wilc->cfg_frame_offset = offset;
1442 PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n",
1444 PRINT_D(RX_DBG, "Processing cfg_set()\n");
1445 wilc->cfg_frame_in_use = 1;
1447 if (wilc_wlan_cfg_commit(wilc, WILC_CFG_SET, drv_handler))
1450 if (wilc_lock_timeout(wilc, &wilc->cfg_event,
1451 CFG_PKTS_TIMEOUT)) {
1452 PRINT_D(TX_DBG, "Set Timed Out\n");
1455 wilc->cfg_frame_in_use = 0;
1456 wilc->cfg_frame_offset = 0;
1457 wilc->cfg_seq_no += 1;
1463 int wilc_wlan_cfg_get(struct wilc *wilc, int start, u32 wid, int commit,
1469 if (wilc->cfg_frame_in_use)
1473 wilc->cfg_frame_offset = 0;
1475 offset = wilc->cfg_frame_offset;
1476 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset,
1479 wilc->cfg_frame_offset = offset;
1482 wilc->cfg_frame_in_use = 1;
1484 if (wilc_wlan_cfg_commit(wilc, WILC_CFG_QUERY, drv_handler))
1487 if (wilc_lock_timeout(wilc, &wilc->cfg_event,
1488 CFG_PKTS_TIMEOUT)) {
1489 PRINT_D(TX_DBG, "Get Timed Out\n");
1492 PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
1493 wilc->cfg_frame_in_use = 0;
1494 wilc->cfg_frame_offset = 0;
1495 wilc->cfg_seq_no += 1;
1501 int wilc_wlan_cfg_get_val(u32 wid, u8 *buffer, u32 buffer_size)
1505 ret = wilc_wlan_cfg_get_wid_value((u16)wid, buffer, buffer_size);
1510 static u32 init_chip(struct net_device *dev)
1514 struct wilc_vif *vif;
1517 vif = netdev_priv(dev);
1520 acquire_bus(wilc, ACQUIRE_ONLY);
1522 chipid = wilc_get_chipid(wilc, true);
1524 if ((chipid & 0xfff) != 0xa0) {
1525 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, ®);
1527 wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
1531 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1533 wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
1536 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1538 wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
1543 release_bus(wilc, RELEASE_ONLY);
1548 u32 wilc_get_chipid(struct wilc *wilc, u8 update)
1554 if (chipid == 0 || update != 0) {
1555 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1556 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1557 if (!ISWILC1000(tempchipid)) {
1561 if (tempchipid == 0x1002a0) {
1562 if (rfrevid == 0x1) {
1564 tempchipid = 0x1002a1;
1566 } else if (tempchipid == 0x1002b0) {
1568 } else if (rfrevid == 4) {
1569 tempchipid = 0x1002b1;
1571 tempchipid = 0x1002b2;
1575 chipid = tempchipid;
1581 int wilc_wlan_init(struct net_device *dev)
1584 struct wilc_vif *vif = netdev_priv(dev);
1589 PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
1591 if (!wilc->hif_func->hif_init(wilc)) {
1596 if (!wilc_wlan_cfg_init(wilc_debug)) {
1601 if (!wilc->tx_buffer)
1602 wilc->tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
1603 PRINT_D(TX_DBG, "wilc->tx_buffer = %p\n", wilc->tx_buffer);
1605 if (!wilc->tx_buffer) {
1607 PRINT_ER("Can't allocate Tx Buffer");
1611 if (!wilc->rx_buffer)
1612 wilc->rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
1613 PRINT_D(TX_DBG, "wilc->rx_buffer =%p\n", wilc->rx_buffer);
1614 if (!wilc->rx_buffer) {
1616 PRINT_ER("Can't allocate Rx Buffer");
1620 if (!init_chip(dev)) {
1624 #ifdef TCP_ACK_FILTER
1625 init_tcp_tracking();
1632 kfree(wilc->rx_buffer);
1633 wilc->rx_buffer = NULL;
1634 kfree(wilc->tx_buffer);
1635 wilc->tx_buffer = NULL;
1640 u16 wilc_set_machw_change_vir_if(struct net_device *dev, bool value)
1644 struct wilc_vif *vif;
1647 vif = netdev_priv(dev);
1650 mutex_lock(&wilc->hif_cs);
1651 ret = wilc->hif_func->hif_read_reg(wilc, WILC_CHANGING_VIR_IF,
1654 PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
1661 ret = wilc->hif_func->hif_write_reg(wilc, WILC_CHANGING_VIR_IF,
1665 PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
1667 mutex_unlock(&wilc->hif_cs);