1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
7 #include <linux/if_ether.h>
9 #include <net/dsfield.h>
13 static inline bool is_wilc1000(u32 id)
15 return (id & (~WILC_CHIP_REV_FIELD)) == WILC_1000_BASE_ID;
18 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
20 mutex_lock(&wilc->hif_cs);
21 if (acquire == WILC_BUS_ACQUIRE_AND_WAKEUP)
25 static inline void release_bus(struct wilc *wilc, enum bus_release release)
27 if (release == WILC_BUS_RELEASE_ALLOW_SLEEP)
28 chip_allow_sleep(wilc);
29 mutex_unlock(&wilc->hif_cs);
32 static void wilc_wlan_txq_remove(struct wilc *wilc, u8 q_num,
33 struct txq_entry_t *tqe)
36 wilc->txq_entries -= 1;
37 wilc->txq[q_num].count--;
40 static struct txq_entry_t *
41 wilc_wlan_txq_remove_from_head(struct wilc *wilc, u8 q_num)
43 struct txq_entry_t *tqe = NULL;
46 spin_lock_irqsave(&wilc->txq_spinlock, flags);
48 if (!list_empty(&wilc->txq[q_num].txq_head.list)) {
49 tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
50 struct txq_entry_t, list);
52 wilc->txq_entries -= 1;
53 wilc->txq[q_num].count--;
55 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
59 static void wilc_wlan_txq_add_to_tail(struct net_device *dev, u8 q_num,
60 struct txq_entry_t *tqe)
63 struct wilc_vif *vif = netdev_priv(dev);
64 struct wilc *wilc = vif->wilc;
66 spin_lock_irqsave(&wilc->txq_spinlock, flags);
68 list_add_tail(&tqe->list, &wilc->txq[q_num].txq_head.list);
69 wilc->txq_entries += 1;
70 wilc->txq[q_num].count++;
72 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
74 complete(&wilc->txq_event);
77 static void wilc_wlan_txq_add_to_head(struct wilc_vif *vif, u8 q_num,
78 struct txq_entry_t *tqe)
81 struct wilc *wilc = vif->wilc;
83 mutex_lock(&wilc->txq_add_to_head_cs);
85 spin_lock_irqsave(&wilc->txq_spinlock, flags);
87 list_add(&tqe->list, &wilc->txq[q_num].txq_head.list);
88 wilc->txq_entries += 1;
89 wilc->txq[q_num].count++;
91 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
92 mutex_unlock(&wilc->txq_add_to_head_cs);
93 complete(&wilc->txq_event);
96 #define NOT_TCP_ACK (-1)
98 static inline void add_tcp_session(struct wilc_vif *vif, u32 src_prt,
101 struct tcp_ack_filter *f = &vif->ack_filter;
103 if (f->tcp_session < 2 * MAX_TCP_SESSION) {
104 f->ack_session_info[f->tcp_session].seq_num = seq;
105 f->ack_session_info[f->tcp_session].bigger_ack_num = 0;
106 f->ack_session_info[f->tcp_session].src_port = src_prt;
107 f->ack_session_info[f->tcp_session].dst_port = dst_prt;
112 static inline void update_tcp_session(struct wilc_vif *vif, u32 index, u32 ack)
114 struct tcp_ack_filter *f = &vif->ack_filter;
116 if (index < 2 * MAX_TCP_SESSION &&
117 ack > f->ack_session_info[index].bigger_ack_num)
118 f->ack_session_info[index].bigger_ack_num = ack;
121 static inline void add_tcp_pending_ack(struct wilc_vif *vif, u32 ack,
123 struct txq_entry_t *txqe)
125 struct tcp_ack_filter *f = &vif->ack_filter;
126 u32 i = f->pending_base + f->pending_acks_idx;
128 if (i < MAX_PENDING_ACKS) {
129 f->pending_acks[i].ack_num = ack;
130 f->pending_acks[i].txqe = txqe;
131 f->pending_acks[i].session_index = session_index;
133 f->pending_acks_idx++;
137 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
139 void *buffer = tqe->buffer;
140 const struct ethhdr *eth_hdr_ptr = buffer;
143 struct wilc_vif *vif = netdev_priv(dev);
144 struct wilc *wilc = vif->wilc;
145 struct tcp_ack_filter *f = &vif->ack_filter;
146 const struct iphdr *ip_hdr_ptr;
147 const struct tcphdr *tcp_hdr_ptr;
148 u32 ihl, total_length, data_offset;
150 spin_lock_irqsave(&wilc->txq_spinlock, flags);
152 if (eth_hdr_ptr->h_proto != htons(ETH_P_IP))
155 ip_hdr_ptr = buffer + ETH_HLEN;
157 if (ip_hdr_ptr->protocol != IPPROTO_TCP)
160 ihl = ip_hdr_ptr->ihl << 2;
161 tcp_hdr_ptr = buffer + ETH_HLEN + ihl;
162 total_length = ntohs(ip_hdr_ptr->tot_len);
164 data_offset = tcp_hdr_ptr->doff << 2;
165 if (total_length == (ihl + data_offset)) {
168 seq_no = ntohl(tcp_hdr_ptr->seq);
169 ack_no = ntohl(tcp_hdr_ptr->ack_seq);
170 for (i = 0; i < f->tcp_session; i++) {
171 u32 j = f->ack_session_info[i].seq_num;
173 if (i < 2 * MAX_TCP_SESSION &&
175 update_tcp_session(vif, i, ack_no);
179 if (i == f->tcp_session)
180 add_tcp_session(vif, 0, 0, seq_no);
182 add_tcp_pending_ack(vif, ack_no, i, tqe);
186 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
189 static void wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
191 struct wilc_vif *vif = netdev_priv(dev);
192 struct wilc *wilc = vif->wilc;
193 struct tcp_ack_filter *f = &vif->ack_filter;
198 spin_lock_irqsave(&wilc->txq_spinlock, flags);
199 for (i = f->pending_base;
200 i < (f->pending_base + f->pending_acks_idx); i++) {
204 if (i >= MAX_PENDING_ACKS)
207 index = f->pending_acks[i].session_index;
209 if (index >= 2 * MAX_TCP_SESSION)
212 bigger_ack_num = f->ack_session_info[index].bigger_ack_num;
214 if (f->pending_acks[i].ack_num < bigger_ack_num) {
215 struct txq_entry_t *tqe;
217 tqe = f->pending_acks[i].txqe;
219 wilc_wlan_txq_remove(wilc, tqe->q_num, tqe);
221 if (tqe->tx_complete_func)
222 tqe->tx_complete_func(tqe->priv,
229 f->pending_acks_idx = 0;
232 if (f->pending_base == 0)
233 f->pending_base = MAX_TCP_SESSION;
237 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
239 while (dropped > 0) {
240 wait_for_completion_timeout(&wilc->txq_event,
241 msecs_to_jiffies(1));
246 void wilc_enable_tcp_ack_filter(struct wilc_vif *vif, bool value)
248 vif->ack_filter.enabled = value;
251 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
254 struct txq_entry_t *tqe;
255 struct wilc *wilc = vif->wilc;
257 netdev_dbg(vif->ndev, "Adding config packet ...\n");
259 netdev_dbg(vif->ndev, "Return due to clear function\n");
260 complete(&wilc->cfg_event);
264 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
266 complete(&wilc->cfg_event);
270 tqe->type = WILC_CFG_PKT;
271 tqe->buffer = buffer;
272 tqe->buffer_size = buffer_size;
273 tqe->tx_complete_func = NULL;
275 tqe->q_num = AC_VO_Q;
276 tqe->ack_idx = NOT_TCP_ACK;
279 wilc_wlan_txq_add_to_head(vif, AC_VO_Q, tqe);
284 static bool is_ac_q_limit(struct wilc *wl, u8 q_num)
286 u8 factors[NQUEUES] = {1, 1, 1, 1};
289 struct wilc_tx_queue_status *q = &wl->tx_q_limit;
294 spin_lock_irqsave(&wl->txq_spinlock, flags);
295 if (!q->initialized) {
296 for (i = 0; i < AC_BUFFER_SIZE; i++)
297 q->buffer[i] = i % NQUEUES;
299 for (i = 0; i < NQUEUES; i++) {
300 q->cnt[i] = AC_BUFFER_SIZE * factors[i] / NQUEUES;
303 q->end_index = AC_BUFFER_SIZE - 1;
307 end_index = q->end_index;
308 q->cnt[q->buffer[end_index]] -= factors[q->buffer[end_index]];
309 q->cnt[q_num] += factors[q_num];
310 q->sum += (factors[q_num] - factors[q->buffer[end_index]]);
312 q->buffer[end_index] = q_num;
316 q->end_index = AC_BUFFER_SIZE - 1;
321 q_limit = (q->cnt[q_num] * FLOW_CONTROL_UPPER_THRESHOLD / q->sum) + 1;
323 if (wl->txq[q_num].count <= q_limit)
326 spin_unlock_irqrestore(&wl->txq_spinlock, flags);
331 static inline u8 ac_classify(struct wilc *wilc, struct sk_buff *skb)
336 switch (skb->protocol) {
337 case htons(ETH_P_IP):
338 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
340 case htons(ETH_P_IPV6):
341 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
370 static inline int ac_balance(struct wilc *wl, u8 *ratio)
377 for (i = 0; i < NQUEUES; i++)
378 if (wl->txq[i].fw.count > max_count)
379 max_count = wl->txq[i].fw.count;
381 for (i = 0; i < NQUEUES; i++)
382 ratio[i] = max_count - wl->txq[i].fw.count;
387 static inline void ac_update_fw_ac_pkt_info(struct wilc *wl, u32 reg)
389 wl->txq[AC_BK_Q].fw.count = FIELD_GET(BK_AC_COUNT_FIELD, reg);
390 wl->txq[AC_BE_Q].fw.count = FIELD_GET(BE_AC_COUNT_FIELD, reg);
391 wl->txq[AC_VI_Q].fw.count = FIELD_GET(VI_AC_COUNT_FIELD, reg);
392 wl->txq[AC_VO_Q].fw.count = FIELD_GET(VO_AC_COUNT_FIELD, reg);
394 wl->txq[AC_BK_Q].fw.acm = FIELD_GET(BK_AC_ACM_STAT_FIELD, reg);
395 wl->txq[AC_BE_Q].fw.acm = FIELD_GET(BE_AC_ACM_STAT_FIELD, reg);
396 wl->txq[AC_VI_Q].fw.acm = FIELD_GET(VI_AC_ACM_STAT_FIELD, reg);
397 wl->txq[AC_VO_Q].fw.acm = FIELD_GET(VO_AC_ACM_STAT_FIELD, reg);
400 static inline u8 ac_change(struct wilc *wilc, u8 *ac)
403 if (wilc->txq[*ac].fw.acm == 0)
406 } while (*ac < NQUEUES);
411 int wilc_wlan_txq_add_net_pkt(struct net_device *dev,
412 struct tx_complete_data *tx_data, u8 *buffer,
414 void (*tx_complete_fn)(void *, int))
416 struct txq_entry_t *tqe;
417 struct wilc_vif *vif = netdev_priv(dev);
424 tx_complete_fn(tx_data, 0);
428 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
431 tx_complete_fn(tx_data, 0);
434 tqe->type = WILC_NET_PKT;
435 tqe->buffer = buffer;
436 tqe->buffer_size = buffer_size;
437 tqe->tx_complete_func = tx_complete_fn;
441 q_num = ac_classify(wilc, tx_data->skb);
443 if (ac_change(wilc, &q_num)) {
444 tx_complete_fn(tx_data, 0);
449 if (is_ac_q_limit(wilc, q_num)) {
450 tqe->ack_idx = NOT_TCP_ACK;
451 if (vif->ack_filter.enabled)
452 tcp_process(dev, tqe);
453 wilc_wlan_txq_add_to_tail(dev, q_num, tqe);
455 tx_complete_fn(tx_data, 0);
459 return wilc->txq_entries;
462 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
464 void (*tx_complete_fn)(void *, int))
466 struct txq_entry_t *tqe;
467 struct wilc_vif *vif = netdev_priv(dev);
473 tx_complete_fn(priv, 0);
477 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
480 tx_complete_fn(priv, 0);
483 tqe->type = WILC_MGMT_PKT;
484 tqe->buffer = buffer;
485 tqe->buffer_size = buffer_size;
486 tqe->tx_complete_func = tx_complete_fn;
488 tqe->q_num = AC_BE_Q;
489 tqe->ack_idx = NOT_TCP_ACK;
491 wilc_wlan_txq_add_to_tail(dev, AC_VO_Q, tqe);
495 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc, u8 q_num)
497 struct txq_entry_t *tqe = NULL;
500 spin_lock_irqsave(&wilc->txq_spinlock, flags);
502 if (!list_empty(&wilc->txq[q_num].txq_head.list))
503 tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
504 struct txq_entry_t, list);
506 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
511 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
512 struct txq_entry_t *tqe,
517 spin_lock_irqsave(&wilc->txq_spinlock, flags);
519 if (!list_is_last(&tqe->list, &wilc->txq[q_num].txq_head.list))
520 tqe = list_next_entry(tqe, list);
523 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
528 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
533 mutex_lock(&wilc->rxq_cs);
534 list_add_tail(&rqe->list, &wilc->rxq_head.list);
535 mutex_unlock(&wilc->rxq_cs);
538 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
540 struct rxq_entry_t *rqe = NULL;
542 mutex_lock(&wilc->rxq_cs);
543 if (!list_empty(&wilc->rxq_head.list)) {
544 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
546 list_del(&rqe->list);
548 mutex_unlock(&wilc->rxq_cs);
552 void chip_allow_sleep(struct wilc *wilc)
555 const struct wilc_hif_func *hif_func = wilc->hif_func;
556 u32 wakeup_reg, wakeup_bit;
557 u32 to_host_from_fw_reg, to_host_from_fw_bit;
558 u32 from_host_to_fw_reg, from_host_to_fw_bit;
562 if (wilc->io_type == WILC_HIF_SDIO) {
563 wakeup_reg = WILC_SDIO_WAKEUP_REG;
564 wakeup_bit = WILC_SDIO_WAKEUP_BIT;
565 from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
566 from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
567 to_host_from_fw_reg = WILC_SDIO_FW_TO_HOST_REG;
568 to_host_from_fw_bit = WILC_SDIO_FW_TO_HOST_BIT;
570 wakeup_reg = WILC_SPI_WAKEUP_REG;
571 wakeup_bit = WILC_SPI_WAKEUP_BIT;
572 from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
573 from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
574 to_host_from_fw_reg = WILC_SPI_FW_TO_HOST_REG;
575 to_host_from_fw_bit = WILC_SPI_FW_TO_HOST_BIT;
579 ret = hif_func->hif_read_reg(wilc, to_host_from_fw_reg, ®);
582 if ((reg & to_host_from_fw_bit) == 0)
586 pr_warn("FW not responding\n");
589 ret = hif_func->hif_read_reg(wilc, wakeup_reg, ®);
592 if (reg & wakeup_bit) {
594 ret = hif_func->hif_write_reg(wilc, wakeup_reg, reg);
599 ret = hif_func->hif_read_reg(wilc, from_host_to_fw_reg, ®);
602 if (reg & from_host_to_fw_bit) {
603 reg &= ~from_host_to_fw_bit;
604 ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg, reg);
610 EXPORT_SYMBOL_GPL(chip_allow_sleep);
612 void chip_wakeup(struct wilc *wilc)
614 u32 reg, clk_status_reg;
615 const struct wilc_hif_func *h = wilc->hif_func;
617 if (wilc->io_type == WILC_HIF_SPI) {
619 h->hif_read_reg(wilc, WILC_SPI_WAKEUP_REG, ®);
620 h->hif_write_reg(wilc, WILC_SPI_WAKEUP_REG,
621 reg | WILC_SPI_WAKEUP_BIT);
622 h->hif_write_reg(wilc, WILC_SPI_WAKEUP_REG,
623 reg & ~WILC_SPI_WAKEUP_BIT);
626 usleep_range(2000, 2500);
627 wilc_get_chipid(wilc, true);
628 } while (wilc_get_chipid(wilc, true) == 0);
629 } while (wilc_get_chipid(wilc, true) == 0);
630 } else if (wilc->io_type == WILC_HIF_SDIO) {
631 h->hif_write_reg(wilc, WILC_SDIO_HOST_TO_FW_REG,
632 WILC_SDIO_HOST_TO_FW_BIT);
633 usleep_range(200, 400);
634 h->hif_read_reg(wilc, WILC_SDIO_WAKEUP_REG, ®);
636 h->hif_write_reg(wilc, WILC_SDIO_WAKEUP_REG,
637 reg | WILC_SDIO_WAKEUP_BIT);
638 h->hif_read_reg(wilc, WILC_SDIO_CLK_STATUS_REG,
641 while (!(clk_status_reg & WILC_SDIO_CLK_STATUS_BIT)) {
642 usleep_range(2000, 2500);
644 h->hif_read_reg(wilc, WILC_SDIO_CLK_STATUS_REG,
647 if (!(clk_status_reg & WILC_SDIO_CLK_STATUS_BIT)) {
648 h->hif_write_reg(wilc, WILC_SDIO_WAKEUP_REG,
649 reg & ~WILC_SDIO_WAKEUP_BIT);
651 } while (!(clk_status_reg & WILC_SDIO_CLK_STATUS_BIT));
654 if (wilc->chip_ps_state == WILC_CHIP_SLEEPING_MANUAL) {
655 if (wilc_get_chipid(wilc, false) < WILC_1000_BASE_ID_2B) {
658 h->hif_read_reg(wilc, WILC_REG_4_TO_1_RX, &val32);
660 h->hif_write_reg(wilc, WILC_REG_4_TO_1_RX, val32);
662 h->hif_read_reg(wilc, WILC_REG_4_TO_1_TX_BANK0, &val32);
664 h->hif_write_reg(wilc, WILC_REG_4_TO_1_TX_BANK0, val32);
667 wilc->chip_ps_state = WILC_CHIP_WAKEDUP;
669 EXPORT_SYMBOL_GPL(chip_wakeup);
671 void host_wakeup_notify(struct wilc *wilc)
673 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
674 wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_2, 1);
675 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
677 EXPORT_SYMBOL_GPL(host_wakeup_notify);
679 void host_sleep_notify(struct wilc *wilc)
681 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
682 wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_1, 1);
683 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
685 EXPORT_SYMBOL_GPL(host_sleep_notify);
687 int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
693 u8 ac_desired_ratio[NQUEUES] = {0, 0, 0, 0};
694 u8 ac_preserve_ratio[NQUEUES] = {1, 1, 1, 1};
696 u8 vmm_entries_ac[WILC_VMM_TBL_SIZE];
698 bool max_size_over = 0, ac_exist = 0;
700 struct txq_entry_t *tqe_q[NQUEUES];
704 u32 vmm_table[WILC_VMM_TBL_SIZE];
705 u8 ac_pkt_num_to_chip[NQUEUES] = {0, 0, 0, 0};
706 const struct wilc_hif_func *func;
708 u8 *txb = wilc->tx_buffer;
709 struct wilc_vif *vif;
714 if (ac_balance(wilc, ac_desired_ratio))
717 mutex_lock(&wilc->txq_add_to_head_cs);
719 srcu_idx = srcu_read_lock(&wilc->srcu);
720 list_for_each_entry_rcu(vif, &wilc->vif_list, list)
721 wilc_wlan_txq_filter_dup_tcp_ack(vif->ndev);
722 srcu_read_unlock(&wilc->srcu, srcu_idx);
724 for (ac = 0; ac < NQUEUES; ac++)
725 tqe_q[ac] = wilc_wlan_txq_get_first(wilc, ac);
730 num_pkts_to_add = ac_desired_ratio;
733 for (ac = 0; (ac < NQUEUES) && (!max_size_over); ac++) {
738 for (k = 0; (k < num_pkts_to_add[ac]) &&
739 (!max_size_over) && tqe_q[ac]; k++) {
740 if (i >= (WILC_VMM_TBL_SIZE - 1)) {
745 if (tqe_q[ac]->type == WILC_CFG_PKT)
746 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
747 else if (tqe_q[ac]->type == WILC_NET_PKT)
748 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
750 vmm_sz = HOST_HDR_OFFSET;
752 vmm_sz += tqe_q[ac]->buffer_size;
753 vmm_sz = ALIGN(vmm_sz, 4);
755 if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE) {
759 vmm_table[i] = vmm_sz / 4;
760 if (tqe_q[ac]->type == WILC_CFG_PKT)
761 vmm_table[i] |= BIT(10);
763 cpu_to_le32s(&vmm_table[i]);
764 vmm_entries_ac[i] = ac;
768 tqe_q[ac] = wilc_wlan_txq_get_next(wilc,
773 num_pkts_to_add = ac_preserve_ratio;
774 } while (!max_size_over && ac_exist);
780 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
782 func = wilc->hif_func;
784 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
788 if ((reg & 0x1) == 0) {
789 ac_update_fw_ac_pkt_info(wilc, reg);
796 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
799 } while (!wilc->quit);
802 goto out_release_bus;
806 ret = func->hif_block_tx(wilc,
807 WILC_VMM_TBL_RX_SHADOW_BASE,
813 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
818 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
821 if (FIELD_GET(WILC_VMM_ENTRY_AVAILABLE, reg)) {
822 entries = FIELD_GET(WILC_VMM_ENTRY_COUNT, reg);
827 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
835 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
839 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
844 goto out_release_bus;
848 * No VMM space available in firmware so retry to transmit
849 * the packet from tx queue.
851 ret = WILC_VMM_ENTRY_FULL_RETRY;
852 goto out_release_bus;
855 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
860 struct txq_entry_t *tqe;
861 u32 header, buffer_offset;
865 tqe = wilc_wlan_txq_remove_from_head(wilc, vmm_entries_ac[i]);
866 ac_pkt_num_to_chip[vmm_entries_ac[i]]++;
871 if (vmm_table[i] == 0)
874 le32_to_cpus(&vmm_table[i]);
875 vmm_sz = FIELD_GET(WILC_VMM_BUFFER_SIZE, vmm_table[i]);
878 if (tqe->type == WILC_MGMT_PKT)
881 header = (FIELD_PREP(WILC_VMM_HDR_TYPE, tqe->type) |
882 FIELD_PREP(WILC_VMM_HDR_MGMT_FIELD, mgmt_ptk) |
883 FIELD_PREP(WILC_VMM_HDR_PKT_SIZE, tqe->buffer_size) |
884 FIELD_PREP(WILC_VMM_HDR_BUFF_SIZE, vmm_sz));
886 cpu_to_le32s(&header);
887 memcpy(&txb[offset], &header, 4);
888 if (tqe->type == WILC_CFG_PKT) {
889 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
890 } else if (tqe->type == WILC_NET_PKT) {
891 int prio = tqe->q_num;
893 bssid = tqe->vif->bssid;
894 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
895 memcpy(&txb[offset + 4], &prio, sizeof(prio));
896 memcpy(&txb[offset + 8], bssid, 6);
898 buffer_offset = HOST_HDR_OFFSET;
901 memcpy(&txb[offset + buffer_offset],
902 tqe->buffer, tqe->buffer_size);
906 if (tqe->tx_complete_func)
907 tqe->tx_complete_func(tqe->priv, tqe->status);
908 if (tqe->ack_idx != NOT_TCP_ACK &&
909 tqe->ack_idx < MAX_PENDING_ACKS)
910 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
913 for (i = 0; i < NQUEUES; i++)
914 wilc->txq[i].fw.count += ac_pkt_num_to_chip[i];
916 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
918 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
920 goto out_release_bus;
922 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
925 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
928 mutex_unlock(&wilc->txq_add_to_head_cs);
931 *txq_count = wilc->txq_entries;
935 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
939 u32 pkt_len, pkt_offset, tp_len;
944 buff_ptr = buffer + offset;
945 header = get_unaligned_le32(buff_ptr);
947 is_cfg_packet = FIELD_GET(WILC_PKT_HDR_CONFIG_FIELD, header);
948 pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
949 tp_len = FIELD_GET(WILC_PKT_HDR_TOTAL_LEN_FIELD, header);
950 pkt_len = FIELD_GET(WILC_PKT_HDR_LEN_FIELD, header);
952 if (pkt_len == 0 || tp_len == 0)
955 if (pkt_offset & IS_MANAGMEMENT) {
956 buff_ptr += HOST_HDR_OFFSET;
957 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
959 if (!is_cfg_packet) {
960 wilc_frmw_to_host(wilc, buff_ptr, pkt_len,
963 struct wilc_cfg_rsp rsp;
965 buff_ptr += pkt_offset;
967 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
970 if (rsp.type == WILC_CFG_RSP) {
971 if (wilc->cfg_seq_no == rsp.seq_no)
972 complete(&wilc->cfg_event);
973 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
974 wilc_mac_indicate(wilc);
979 } while (offset < size);
982 static void wilc_wlan_handle_rxq(struct wilc *wilc)
986 struct rxq_entry_t *rqe;
988 while (!wilc->quit) {
989 rqe = wilc_wlan_rxq_remove(wilc);
993 buffer = rqe->buffer;
994 size = rqe->buffer_size;
995 wilc_wlan_handle_rx_buff(wilc, buffer, size);
1000 complete(&wilc->cfg_event);
1003 static void wilc_unknown_isr_ext(struct wilc *wilc)
1005 wilc->hif_func->hif_clear_int_ext(wilc, 0);
1008 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
1010 u32 offset = wilc->rx_buffer_offset;
1015 struct rxq_entry_t *rqe;
1017 size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, int_status) << 2;
1019 while (!size && retries < 10) {
1020 wilc->hif_func->hif_read_size(wilc, &size);
1021 size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, size) << 2;
1028 if (WILC_RX_BUFF_SIZE - offset < size)
1031 buffer = &wilc->rx_buffer[offset];
1033 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
1034 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
1039 wilc->rx_buffer_offset = offset;
1040 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
1044 rqe->buffer = buffer;
1045 rqe->buffer_size = size;
1046 wilc_wlan_rxq_add(wilc, rqe);
1047 wilc_wlan_handle_rxq(wilc);
1050 void wilc_handle_isr(struct wilc *wilc)
1054 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1055 wilc->hif_func->hif_read_int(wilc, &int_status);
1057 if (int_status & DATA_INT_EXT)
1058 wilc_wlan_handle_isr_ext(wilc, int_status);
1060 if (!(int_status & (ALL_INT_EXT)))
1061 wilc_unknown_isr_ext(wilc);
1063 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1065 EXPORT_SYMBOL_GPL(wilc_handle_isr);
1067 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
1071 u32 addr, size, size2, blksz;
1077 dma_buffer = kmalloc(blksz, GFP_KERNEL);
1083 addr = get_unaligned_le32(&buffer[offset]);
1084 size = get_unaligned_le32(&buffer[offset + 4]);
1085 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1087 while (((int)size) && (offset < buffer_size)) {
1093 memcpy(dma_buffer, &buffer[offset], size2);
1094 ret = wilc->hif_func->hif_block_tx(wilc, addr,
1103 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1107 } while (offset < buffer_size);
1116 int wilc_wlan_start(struct wilc *wilc)
1122 if (wilc->io_type == WILC_HIF_SDIO) {
1125 } else if (wilc->io_type == WILC_HIF_SPI) {
1128 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1129 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1131 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1135 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
1136 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1138 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1140 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1144 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1146 ret = wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
1148 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1152 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1153 if ((reg & BIT(10)) == BIT(10)) {
1155 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1156 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1160 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1161 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1162 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1167 int wilc_wlan_stop(struct wilc *wilc, struct wilc_vif *vif)
1172 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1174 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1176 netdev_err(vif->ndev, "Error while reading reg\n");
1177 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1181 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1182 (reg | WILC_ABORT_REQ_BIT));
1184 netdev_err(vif->ndev, "Error while writing reg\n");
1185 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1189 ret = wilc->hif_func->hif_read_reg(wilc, WILC_FW_HOST_COMM, ®);
1191 netdev_err(vif->ndev, "Error while reading reg\n");
1192 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1197 ret = wilc->hif_func->hif_write_reg(wilc, WILC_FW_HOST_COMM, reg);
1199 netdev_err(vif->ndev, "Error while writing reg\n");
1200 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1204 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1209 void wilc_wlan_cleanup(struct net_device *dev)
1211 struct txq_entry_t *tqe;
1212 struct rxq_entry_t *rqe;
1214 struct wilc_vif *vif = netdev_priv(dev);
1215 struct wilc *wilc = vif->wilc;
1218 for (ac = 0; ac < NQUEUES; ac++) {
1219 while ((tqe = wilc_wlan_txq_remove_from_head(wilc, ac))) {
1220 if (tqe->tx_complete_func)
1221 tqe->tx_complete_func(tqe->priv, 0);
1226 while ((rqe = wilc_wlan_rxq_remove(wilc)))
1229 kfree(wilc->rx_buffer);
1230 wilc->rx_buffer = NULL;
1231 kfree(wilc->tx_buffer);
1232 wilc->tx_buffer = NULL;
1233 wilc->hif_func->hif_deinit(NULL);
1236 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1239 struct wilc *wilc = vif->wilc;
1240 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1241 int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
1243 if (type == WILC_CFG_SET)
1244 cfg->hdr.cmd_type = 'W';
1246 cfg->hdr.cmd_type = 'Q';
1248 cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
1249 cfg->hdr.total_len = cpu_to_le16(t_len);
1250 cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
1251 wilc->cfg_seq_no = cfg->hdr.seq_no;
1253 if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
1259 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1260 u32 buffer_size, int commit, u32 drv_handler)
1264 struct wilc *wilc = vif->wilc;
1266 mutex_lock(&wilc->cfg_cmd_lock);
1269 wilc->cfg_frame_offset = 0;
1271 offset = wilc->cfg_frame_offset;
1272 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1273 wid, buffer, buffer_size);
1275 wilc->cfg_frame_offset = offset;
1278 mutex_unlock(&wilc->cfg_cmd_lock);
1282 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1284 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1287 if (!wait_for_completion_timeout(&wilc->cfg_event,
1288 WILC_CFG_PKTS_TIMEOUT)) {
1289 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1293 wilc->cfg_frame_offset = 0;
1294 wilc->cfg_seq_no += 1;
1295 mutex_unlock(&wilc->cfg_cmd_lock);
1300 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1305 struct wilc *wilc = vif->wilc;
1307 mutex_lock(&wilc->cfg_cmd_lock);
1310 wilc->cfg_frame_offset = 0;
1312 offset = wilc->cfg_frame_offset;
1313 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1315 wilc->cfg_frame_offset = offset;
1318 mutex_unlock(&wilc->cfg_cmd_lock);
1322 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1325 if (!wait_for_completion_timeout(&wilc->cfg_event,
1326 WILC_CFG_PKTS_TIMEOUT)) {
1327 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1330 wilc->cfg_frame_offset = 0;
1331 wilc->cfg_seq_no += 1;
1332 mutex_unlock(&wilc->cfg_cmd_lock);
1337 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1342 u32 drv = wilc_get_vif_idx(vif);
1344 if (mode == WILC_GET_CFG) {
1345 for (i = 0; i < count; i++) {
1346 if (!wilc_wlan_cfg_get(vif, !i,
1354 for (i = 0; i < count; i++) {
1355 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1360 } else if (mode == WILC_SET_CFG) {
1361 for (i = 0; i < count; i++) {
1362 if (!wilc_wlan_cfg_set(vif, !i,
1377 static int init_chip(struct net_device *dev)
1382 struct wilc_vif *vif = netdev_priv(dev);
1383 struct wilc *wilc = vif->wilc;
1385 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1387 chipid = wilc_get_chipid(wilc, true);
1389 if ((chipid & 0xfff) != 0xa0) {
1390 ret = wilc->hif_func->hif_read_reg(wilc,
1391 WILC_CORTUS_RESET_MUX_SEL,
1394 netdev_err(dev, "fail read reg 0x1118\n");
1398 ret = wilc->hif_func->hif_write_reg(wilc,
1399 WILC_CORTUS_RESET_MUX_SEL,
1402 netdev_err(dev, "fail write reg 0x1118\n");
1405 ret = wilc->hif_func->hif_write_reg(wilc,
1406 WILC_CORTUS_BOOT_REGISTER,
1407 WILC_CORTUS_BOOT_FROM_IRAM);
1409 netdev_err(dev, "fail write reg 0xc0000\n");
1415 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1420 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1426 if (chipid == 0 || update) {
1427 wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &tempchipid);
1428 wilc->hif_func->hif_read_reg(wilc, WILC_RF_REVISION_ID,
1430 if (!is_wilc1000(tempchipid)) {
1434 if (tempchipid == WILC_1000_BASE_ID_2A) { /* 0x1002A0 */
1436 tempchipid = WILC_1000_BASE_ID_2A_REV1;
1437 } else if (tempchipid == WILC_1000_BASE_ID_2B) { /* 0x1002B0 */
1439 tempchipid = WILC_1000_BASE_ID_2B_REV1;
1440 else if (rfrevid != 0x3)
1441 tempchipid = WILC_1000_BASE_ID_2B_REV2;
1444 chipid = tempchipid;
1449 int wilc_wlan_init(struct net_device *dev)
1452 struct wilc_vif *vif = netdev_priv(dev);
1459 if (wilc->hif_func->hif_init(wilc, false)) {
1464 if (!wilc->tx_buffer)
1465 wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1467 if (!wilc->tx_buffer) {
1472 if (!wilc->rx_buffer)
1473 wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1475 if (!wilc->rx_buffer) {
1480 if (init_chip(dev)) {
1489 kfree(wilc->rx_buffer);
1490 wilc->rx_buffer = NULL;
1491 kfree(wilc->tx_buffer);
1492 wilc->tx_buffer = NULL;