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 #define WAKE_UP_TRIAL_RETRY 10000
15 static inline bool is_wilc1000(u32 id)
17 return (id & (~WILC_CHIP_REV_FIELD)) == WILC_1000_BASE_ID;
20 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
22 mutex_lock(&wilc->hif_cs);
23 if (acquire == WILC_BUS_ACQUIRE_AND_WAKEUP && wilc->power_save_mode)
27 static inline void release_bus(struct wilc *wilc, enum bus_release release)
29 if (release == WILC_BUS_RELEASE_ALLOW_SLEEP && wilc->power_save_mode)
30 chip_allow_sleep(wilc);
31 mutex_unlock(&wilc->hif_cs);
34 static void wilc_wlan_txq_remove(struct wilc *wilc, u8 q_num,
35 struct txq_entry_t *tqe)
38 wilc->txq_entries -= 1;
39 wilc->txq[q_num].count--;
42 static struct txq_entry_t *
43 wilc_wlan_txq_remove_from_head(struct wilc *wilc, u8 q_num)
45 struct txq_entry_t *tqe = NULL;
48 spin_lock_irqsave(&wilc->txq_spinlock, flags);
50 if (!list_empty(&wilc->txq[q_num].txq_head.list)) {
51 tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
52 struct txq_entry_t, list);
54 wilc->txq_entries -= 1;
55 wilc->txq[q_num].count--;
57 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
61 static void wilc_wlan_txq_add_to_tail(struct net_device *dev, u8 q_num,
62 struct txq_entry_t *tqe)
65 struct wilc_vif *vif = netdev_priv(dev);
66 struct wilc *wilc = vif->wilc;
68 spin_lock_irqsave(&wilc->txq_spinlock, flags);
70 list_add_tail(&tqe->list, &wilc->txq[q_num].txq_head.list);
71 wilc->txq_entries += 1;
72 wilc->txq[q_num].count++;
74 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
76 complete(&wilc->txq_event);
79 static void wilc_wlan_txq_add_to_head(struct wilc_vif *vif, u8 q_num,
80 struct txq_entry_t *tqe)
83 struct wilc *wilc = vif->wilc;
85 mutex_lock(&wilc->txq_add_to_head_cs);
87 spin_lock_irqsave(&wilc->txq_spinlock, flags);
89 list_add(&tqe->list, &wilc->txq[q_num].txq_head.list);
90 wilc->txq_entries += 1;
91 wilc->txq[q_num].count++;
93 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
94 mutex_unlock(&wilc->txq_add_to_head_cs);
95 complete(&wilc->txq_event);
98 #define NOT_TCP_ACK (-1)
100 static inline void add_tcp_session(struct wilc_vif *vif, u32 src_prt,
101 u32 dst_prt, u32 seq)
103 struct tcp_ack_filter *f = &vif->ack_filter;
105 if (f->tcp_session < 2 * MAX_TCP_SESSION) {
106 f->ack_session_info[f->tcp_session].seq_num = seq;
107 f->ack_session_info[f->tcp_session].bigger_ack_num = 0;
108 f->ack_session_info[f->tcp_session].src_port = src_prt;
109 f->ack_session_info[f->tcp_session].dst_port = dst_prt;
114 static inline void update_tcp_session(struct wilc_vif *vif, u32 index, u32 ack)
116 struct tcp_ack_filter *f = &vif->ack_filter;
118 if (index < 2 * MAX_TCP_SESSION &&
119 ack > f->ack_session_info[index].bigger_ack_num)
120 f->ack_session_info[index].bigger_ack_num = ack;
123 static inline void add_tcp_pending_ack(struct wilc_vif *vif, u32 ack,
125 struct txq_entry_t *txqe)
127 struct tcp_ack_filter *f = &vif->ack_filter;
128 u32 i = f->pending_base + f->pending_acks_idx;
130 if (i < MAX_PENDING_ACKS) {
131 f->pending_acks[i].ack_num = ack;
132 f->pending_acks[i].txqe = txqe;
133 f->pending_acks[i].session_index = session_index;
135 f->pending_acks_idx++;
139 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
141 void *buffer = tqe->buffer;
142 const struct ethhdr *eth_hdr_ptr = buffer;
145 struct wilc_vif *vif = netdev_priv(dev);
146 struct wilc *wilc = vif->wilc;
147 struct tcp_ack_filter *f = &vif->ack_filter;
148 const struct iphdr *ip_hdr_ptr;
149 const struct tcphdr *tcp_hdr_ptr;
150 u32 ihl, total_length, data_offset;
152 spin_lock_irqsave(&wilc->txq_spinlock, flags);
154 if (eth_hdr_ptr->h_proto != htons(ETH_P_IP))
157 ip_hdr_ptr = buffer + ETH_HLEN;
159 if (ip_hdr_ptr->protocol != IPPROTO_TCP)
162 ihl = ip_hdr_ptr->ihl << 2;
163 tcp_hdr_ptr = buffer + ETH_HLEN + ihl;
164 total_length = ntohs(ip_hdr_ptr->tot_len);
166 data_offset = tcp_hdr_ptr->doff << 2;
167 if (total_length == (ihl + data_offset)) {
170 seq_no = ntohl(tcp_hdr_ptr->seq);
171 ack_no = ntohl(tcp_hdr_ptr->ack_seq);
172 for (i = 0; i < f->tcp_session; i++) {
173 u32 j = f->ack_session_info[i].seq_num;
175 if (i < 2 * MAX_TCP_SESSION &&
177 update_tcp_session(vif, i, ack_no);
181 if (i == f->tcp_session)
182 add_tcp_session(vif, 0, 0, seq_no);
184 add_tcp_pending_ack(vif, ack_no, i, tqe);
188 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
191 static void wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
193 struct wilc_vif *vif = netdev_priv(dev);
194 struct wilc *wilc = vif->wilc;
195 struct tcp_ack_filter *f = &vif->ack_filter;
200 spin_lock_irqsave(&wilc->txq_spinlock, flags);
201 for (i = f->pending_base;
202 i < (f->pending_base + f->pending_acks_idx); i++) {
206 if (i >= MAX_PENDING_ACKS)
209 index = f->pending_acks[i].session_index;
211 if (index >= 2 * MAX_TCP_SESSION)
214 bigger_ack_num = f->ack_session_info[index].bigger_ack_num;
216 if (f->pending_acks[i].ack_num < bigger_ack_num) {
217 struct txq_entry_t *tqe;
219 tqe = f->pending_acks[i].txqe;
221 wilc_wlan_txq_remove(wilc, tqe->q_num, tqe);
223 if (tqe->tx_complete_func)
224 tqe->tx_complete_func(tqe->priv,
231 f->pending_acks_idx = 0;
234 if (f->pending_base == 0)
235 f->pending_base = MAX_TCP_SESSION;
239 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
241 while (dropped > 0) {
242 wait_for_completion_timeout(&wilc->txq_event,
243 msecs_to_jiffies(1));
248 void wilc_enable_tcp_ack_filter(struct wilc_vif *vif, bool value)
250 vif->ack_filter.enabled = value;
253 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
256 struct txq_entry_t *tqe;
257 struct wilc *wilc = vif->wilc;
259 netdev_dbg(vif->ndev, "Adding config packet ...\n");
261 netdev_dbg(vif->ndev, "Return due to clear function\n");
262 complete(&wilc->cfg_event);
266 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
268 complete(&wilc->cfg_event);
272 tqe->type = WILC_CFG_PKT;
273 tqe->buffer = buffer;
274 tqe->buffer_size = buffer_size;
275 tqe->tx_complete_func = NULL;
277 tqe->q_num = AC_VO_Q;
278 tqe->ack_idx = NOT_TCP_ACK;
281 wilc_wlan_txq_add_to_head(vif, AC_VO_Q, tqe);
286 static bool is_ac_q_limit(struct wilc *wl, u8 q_num)
288 u8 factors[NQUEUES] = {1, 1, 1, 1};
291 struct wilc_tx_queue_status *q = &wl->tx_q_limit;
296 spin_lock_irqsave(&wl->txq_spinlock, flags);
297 if (!q->initialized) {
298 for (i = 0; i < AC_BUFFER_SIZE; i++)
299 q->buffer[i] = i % NQUEUES;
301 for (i = 0; i < NQUEUES; i++) {
302 q->cnt[i] = AC_BUFFER_SIZE * factors[i] / NQUEUES;
305 q->end_index = AC_BUFFER_SIZE - 1;
309 end_index = q->end_index;
310 q->cnt[q->buffer[end_index]] -= factors[q->buffer[end_index]];
311 q->cnt[q_num] += factors[q_num];
312 q->sum += (factors[q_num] - factors[q->buffer[end_index]]);
314 q->buffer[end_index] = q_num;
318 q->end_index = AC_BUFFER_SIZE - 1;
323 q_limit = (q->cnt[q_num] * FLOW_CONTROL_UPPER_THRESHOLD / q->sum) + 1;
325 if (wl->txq[q_num].count <= q_limit)
328 spin_unlock_irqrestore(&wl->txq_spinlock, flags);
333 static inline u8 ac_classify(struct wilc *wilc, struct sk_buff *skb)
338 switch (skb->protocol) {
339 case htons(ETH_P_IP):
340 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
342 case htons(ETH_P_IPV6):
343 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
372 static inline int ac_balance(struct wilc *wl, u8 *ratio)
379 for (i = 0; i < NQUEUES; i++)
380 if (wl->txq[i].fw.count > max_count)
381 max_count = wl->txq[i].fw.count;
383 for (i = 0; i < NQUEUES; i++)
384 ratio[i] = max_count - wl->txq[i].fw.count;
389 static inline void ac_update_fw_ac_pkt_info(struct wilc *wl, u32 reg)
391 wl->txq[AC_BK_Q].fw.count = FIELD_GET(BK_AC_COUNT_FIELD, reg);
392 wl->txq[AC_BE_Q].fw.count = FIELD_GET(BE_AC_COUNT_FIELD, reg);
393 wl->txq[AC_VI_Q].fw.count = FIELD_GET(VI_AC_COUNT_FIELD, reg);
394 wl->txq[AC_VO_Q].fw.count = FIELD_GET(VO_AC_COUNT_FIELD, reg);
396 wl->txq[AC_BK_Q].fw.acm = FIELD_GET(BK_AC_ACM_STAT_FIELD, reg);
397 wl->txq[AC_BE_Q].fw.acm = FIELD_GET(BE_AC_ACM_STAT_FIELD, reg);
398 wl->txq[AC_VI_Q].fw.acm = FIELD_GET(VI_AC_ACM_STAT_FIELD, reg);
399 wl->txq[AC_VO_Q].fw.acm = FIELD_GET(VO_AC_ACM_STAT_FIELD, reg);
402 static inline u8 ac_change(struct wilc *wilc, u8 *ac)
405 if (wilc->txq[*ac].fw.acm == 0)
408 } while (*ac < NQUEUES);
413 int wilc_wlan_txq_add_net_pkt(struct net_device *dev,
414 struct tx_complete_data *tx_data, u8 *buffer,
416 void (*tx_complete_fn)(void *, int))
418 struct txq_entry_t *tqe;
419 struct wilc_vif *vif = netdev_priv(dev);
426 tx_complete_fn(tx_data, 0);
430 if (!wilc->initialized) {
431 tx_complete_fn(tx_data, 0);
435 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
438 tx_complete_fn(tx_data, 0);
441 tqe->type = WILC_NET_PKT;
442 tqe->buffer = buffer;
443 tqe->buffer_size = buffer_size;
444 tqe->tx_complete_func = tx_complete_fn;
448 q_num = ac_classify(wilc, tx_data->skb);
450 if (ac_change(wilc, &q_num)) {
451 tx_complete_fn(tx_data, 0);
456 if (is_ac_q_limit(wilc, q_num)) {
457 tqe->ack_idx = NOT_TCP_ACK;
458 if (vif->ack_filter.enabled)
459 tcp_process(dev, tqe);
460 wilc_wlan_txq_add_to_tail(dev, q_num, tqe);
462 tx_complete_fn(tx_data, 0);
466 return wilc->txq_entries;
469 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
471 void (*tx_complete_fn)(void *, int))
473 struct txq_entry_t *tqe;
474 struct wilc_vif *vif = netdev_priv(dev);
480 tx_complete_fn(priv, 0);
484 if (!wilc->initialized) {
485 tx_complete_fn(priv, 0);
488 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
491 tx_complete_fn(priv, 0);
494 tqe->type = WILC_MGMT_PKT;
495 tqe->buffer = buffer;
496 tqe->buffer_size = buffer_size;
497 tqe->tx_complete_func = tx_complete_fn;
499 tqe->q_num = AC_BE_Q;
500 tqe->ack_idx = NOT_TCP_ACK;
502 wilc_wlan_txq_add_to_tail(dev, AC_VO_Q, tqe);
506 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc, u8 q_num)
508 struct txq_entry_t *tqe = NULL;
511 spin_lock_irqsave(&wilc->txq_spinlock, flags);
513 if (!list_empty(&wilc->txq[q_num].txq_head.list))
514 tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
515 struct txq_entry_t, list);
517 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
522 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
523 struct txq_entry_t *tqe,
528 spin_lock_irqsave(&wilc->txq_spinlock, flags);
530 if (!list_is_last(&tqe->list, &wilc->txq[q_num].txq_head.list))
531 tqe = list_next_entry(tqe, list);
534 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
539 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
544 mutex_lock(&wilc->rxq_cs);
545 list_add_tail(&rqe->list, &wilc->rxq_head.list);
546 mutex_unlock(&wilc->rxq_cs);
549 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
551 struct rxq_entry_t *rqe = NULL;
553 mutex_lock(&wilc->rxq_cs);
554 if (!list_empty(&wilc->rxq_head.list)) {
555 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
557 list_del(&rqe->list);
559 mutex_unlock(&wilc->rxq_cs);
563 void chip_allow_sleep(struct wilc *wilc)
566 const struct wilc_hif_func *hif_func = wilc->hif_func;
567 u32 wakeup_reg, wakeup_bit;
568 u32 to_host_from_fw_reg, to_host_from_fw_bit;
569 u32 from_host_to_fw_reg, from_host_to_fw_bit;
573 if (wilc->io_type == WILC_HIF_SDIO) {
574 wakeup_reg = WILC_SDIO_WAKEUP_REG;
575 wakeup_bit = WILC_SDIO_WAKEUP_BIT;
576 from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
577 from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
578 to_host_from_fw_reg = WILC_SDIO_FW_TO_HOST_REG;
579 to_host_from_fw_bit = WILC_SDIO_FW_TO_HOST_BIT;
581 wakeup_reg = WILC_SPI_WAKEUP_REG;
582 wakeup_bit = WILC_SPI_WAKEUP_BIT;
583 from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
584 from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
585 to_host_from_fw_reg = WILC_SPI_FW_TO_HOST_REG;
586 to_host_from_fw_bit = WILC_SPI_FW_TO_HOST_BIT;
590 ret = hif_func->hif_read_reg(wilc, to_host_from_fw_reg, ®);
593 if ((reg & to_host_from_fw_bit) == 0)
597 pr_warn("FW not responding\n");
600 ret = hif_func->hif_read_reg(wilc, wakeup_reg, ®);
603 if (reg & wakeup_bit) {
605 ret = hif_func->hif_write_reg(wilc, wakeup_reg, reg);
610 ret = hif_func->hif_read_reg(wilc, from_host_to_fw_reg, ®);
613 if (reg & from_host_to_fw_bit) {
614 reg &= ~from_host_to_fw_bit;
615 ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg, reg);
621 EXPORT_SYMBOL_GPL(chip_allow_sleep);
623 void chip_wakeup(struct wilc *wilc)
626 u32 clk_status_val = 0, trials = 0;
627 u32 wakeup_reg, wakeup_bit;
628 u32 clk_status_reg, clk_status_bit;
629 u32 from_host_to_fw_reg, from_host_to_fw_bit;
630 const struct wilc_hif_func *hif_func = wilc->hif_func;
632 if (wilc->io_type == WILC_HIF_SDIO) {
633 wakeup_reg = WILC_SDIO_WAKEUP_REG;
634 wakeup_bit = WILC_SDIO_WAKEUP_BIT;
635 clk_status_reg = WILC_SDIO_CLK_STATUS_REG;
636 clk_status_bit = WILC_SDIO_CLK_STATUS_BIT;
637 from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
638 from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
640 wakeup_reg = WILC_SPI_WAKEUP_REG;
641 wakeup_bit = WILC_SPI_WAKEUP_BIT;
642 clk_status_reg = WILC_SPI_CLK_STATUS_REG;
643 clk_status_bit = WILC_SPI_CLK_STATUS_BIT;
644 from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
645 from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
648 /* indicate host wakeup */
649 ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg,
650 from_host_to_fw_bit);
654 /* Set wake-up bit */
655 ret = hif_func->hif_write_reg(wilc, wakeup_reg,
660 while (trials < WAKE_UP_TRIAL_RETRY) {
661 ret = hif_func->hif_read_reg(wilc, clk_status_reg,
664 pr_err("Bus error %d %x\n", ret, clk_status_val);
667 if (clk_status_val & clk_status_bit)
672 if (trials >= WAKE_UP_TRIAL_RETRY) {
673 pr_err("Failed to wake-up the chip\n");
676 /* Sometimes spi fail to read clock regs after reading
677 * writing clockless registers
679 if (wilc->io_type == WILC_HIF_SPI)
680 wilc->hif_func->hif_reset(wilc);
682 EXPORT_SYMBOL_GPL(chip_wakeup);
684 void host_wakeup_notify(struct wilc *wilc)
686 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
687 wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_2, 1);
688 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
690 EXPORT_SYMBOL_GPL(host_wakeup_notify);
692 void host_sleep_notify(struct wilc *wilc)
694 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
695 wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_1, 1);
696 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
698 EXPORT_SYMBOL_GPL(host_sleep_notify);
700 int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
706 u8 ac_desired_ratio[NQUEUES] = {0, 0, 0, 0};
707 u8 ac_preserve_ratio[NQUEUES] = {1, 1, 1, 1};
709 u8 vmm_entries_ac[WILC_VMM_TBL_SIZE];
711 bool max_size_over = 0, ac_exist = 0;
713 struct txq_entry_t *tqe_q[NQUEUES];
717 u32 vmm_table[WILC_VMM_TBL_SIZE];
718 u8 ac_pkt_num_to_chip[NQUEUES] = {0, 0, 0, 0};
719 const struct wilc_hif_func *func;
721 u8 *txb = wilc->tx_buffer;
722 struct wilc_vif *vif;
727 if (ac_balance(wilc, ac_desired_ratio))
730 mutex_lock(&wilc->txq_add_to_head_cs);
732 srcu_idx = srcu_read_lock(&wilc->srcu);
733 list_for_each_entry_rcu(vif, &wilc->vif_list, list)
734 wilc_wlan_txq_filter_dup_tcp_ack(vif->ndev);
735 srcu_read_unlock(&wilc->srcu, srcu_idx);
737 for (ac = 0; ac < NQUEUES; ac++)
738 tqe_q[ac] = wilc_wlan_txq_get_first(wilc, ac);
743 num_pkts_to_add = ac_desired_ratio;
746 for (ac = 0; (ac < NQUEUES) && (!max_size_over); ac++) {
751 for (k = 0; (k < num_pkts_to_add[ac]) &&
752 (!max_size_over) && tqe_q[ac]; k++) {
753 if (i >= (WILC_VMM_TBL_SIZE - 1)) {
758 if (tqe_q[ac]->type == WILC_CFG_PKT)
759 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
760 else if (tqe_q[ac]->type == WILC_NET_PKT)
761 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
763 vmm_sz = HOST_HDR_OFFSET;
765 vmm_sz += tqe_q[ac]->buffer_size;
766 vmm_sz = ALIGN(vmm_sz, 4);
768 if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE) {
772 vmm_table[i] = vmm_sz / 4;
773 if (tqe_q[ac]->type == WILC_CFG_PKT)
774 vmm_table[i] |= BIT(10);
776 cpu_to_le32s(&vmm_table[i]);
777 vmm_entries_ac[i] = ac;
781 tqe_q[ac] = wilc_wlan_txq_get_next(wilc,
786 num_pkts_to_add = ac_preserve_ratio;
787 } while (!max_size_over && ac_exist);
793 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
795 func = wilc->hif_func;
797 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
801 if ((reg & 0x1) == 0) {
802 ac_update_fw_ac_pkt_info(wilc, reg);
809 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
812 } while (!wilc->quit);
815 goto out_release_bus;
819 ret = func->hif_block_tx(wilc,
820 WILC_VMM_TBL_RX_SHADOW_BASE,
826 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
831 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
834 if (FIELD_GET(WILC_VMM_ENTRY_AVAILABLE, reg)) {
835 entries = FIELD_GET(WILC_VMM_ENTRY_COUNT, reg);
840 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
848 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
852 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
857 goto out_release_bus;
861 * No VMM space available in firmware so retry to transmit
862 * the packet from tx queue.
864 ret = WILC_VMM_ENTRY_FULL_RETRY;
865 goto out_release_bus;
868 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
873 struct txq_entry_t *tqe;
874 u32 header, buffer_offset;
878 if (vmm_table[i] == 0 || vmm_entries_ac[i] >= NQUEUES)
881 tqe = wilc_wlan_txq_remove_from_head(wilc, vmm_entries_ac[i]);
885 ac_pkt_num_to_chip[vmm_entries_ac[i]]++;
888 le32_to_cpus(&vmm_table[i]);
889 vmm_sz = FIELD_GET(WILC_VMM_BUFFER_SIZE, vmm_table[i]);
892 if (tqe->type == WILC_MGMT_PKT)
895 header = (FIELD_PREP(WILC_VMM_HDR_TYPE, tqe->type) |
896 FIELD_PREP(WILC_VMM_HDR_MGMT_FIELD, mgmt_ptk) |
897 FIELD_PREP(WILC_VMM_HDR_PKT_SIZE, tqe->buffer_size) |
898 FIELD_PREP(WILC_VMM_HDR_BUFF_SIZE, vmm_sz));
900 cpu_to_le32s(&header);
901 memcpy(&txb[offset], &header, 4);
902 if (tqe->type == WILC_CFG_PKT) {
903 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
904 } else if (tqe->type == WILC_NET_PKT) {
905 int prio = tqe->q_num;
907 bssid = tqe->vif->bssid;
908 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
909 memcpy(&txb[offset + 4], &prio, sizeof(prio));
910 memcpy(&txb[offset + 8], bssid, 6);
912 buffer_offset = HOST_HDR_OFFSET;
915 memcpy(&txb[offset + buffer_offset],
916 tqe->buffer, tqe->buffer_size);
920 if (tqe->tx_complete_func)
921 tqe->tx_complete_func(tqe->priv, tqe->status);
922 if (tqe->ack_idx != NOT_TCP_ACK &&
923 tqe->ack_idx < MAX_PENDING_ACKS)
924 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
927 for (i = 0; i < NQUEUES; i++)
928 wilc->txq[i].fw.count += ac_pkt_num_to_chip[i];
930 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
932 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
934 goto out_release_bus;
936 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
939 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
942 mutex_unlock(&wilc->txq_add_to_head_cs);
945 *txq_count = wilc->txq_entries;
949 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
953 u32 pkt_len, pkt_offset, tp_len;
958 buff_ptr = buffer + offset;
959 header = get_unaligned_le32(buff_ptr);
961 is_cfg_packet = FIELD_GET(WILC_PKT_HDR_CONFIG_FIELD, header);
962 pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
963 tp_len = FIELD_GET(WILC_PKT_HDR_TOTAL_LEN_FIELD, header);
964 pkt_len = FIELD_GET(WILC_PKT_HDR_LEN_FIELD, header);
966 if (pkt_len == 0 || tp_len == 0)
969 if (pkt_offset & IS_MANAGMEMENT) {
970 buff_ptr += HOST_HDR_OFFSET;
971 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
973 if (!is_cfg_packet) {
974 wilc_frmw_to_host(wilc, buff_ptr, pkt_len,
977 struct wilc_cfg_rsp rsp;
979 buff_ptr += pkt_offset;
981 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
984 if (rsp.type == WILC_CFG_RSP) {
985 if (wilc->cfg_seq_no == rsp.seq_no)
986 complete(&wilc->cfg_event);
987 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
988 wilc_mac_indicate(wilc);
993 } while (offset < size);
996 static void wilc_wlan_handle_rxq(struct wilc *wilc)
1000 struct rxq_entry_t *rqe;
1002 while (!wilc->quit) {
1003 rqe = wilc_wlan_rxq_remove(wilc);
1007 buffer = rqe->buffer;
1008 size = rqe->buffer_size;
1009 wilc_wlan_handle_rx_buff(wilc, buffer, size);
1014 complete(&wilc->cfg_event);
1017 static void wilc_unknown_isr_ext(struct wilc *wilc)
1019 wilc->hif_func->hif_clear_int_ext(wilc, 0);
1022 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
1024 u32 offset = wilc->rx_buffer_offset;
1029 struct rxq_entry_t *rqe;
1031 size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, int_status) << 2;
1033 while (!size && retries < 10) {
1034 wilc->hif_func->hif_read_size(wilc, &size);
1035 size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, size) << 2;
1042 if (WILC_RX_BUFF_SIZE - offset < size)
1045 buffer = &wilc->rx_buffer[offset];
1047 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
1048 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
1053 wilc->rx_buffer_offset = offset;
1054 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
1058 rqe->buffer = buffer;
1059 rqe->buffer_size = size;
1060 wilc_wlan_rxq_add(wilc, rqe);
1061 wilc_wlan_handle_rxq(wilc);
1064 void wilc_handle_isr(struct wilc *wilc)
1068 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1069 wilc->hif_func->hif_read_int(wilc, &int_status);
1071 if (int_status & DATA_INT_EXT)
1072 wilc_wlan_handle_isr_ext(wilc, int_status);
1074 if (!(int_status & (ALL_INT_EXT)))
1075 wilc_unknown_isr_ext(wilc);
1077 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1079 EXPORT_SYMBOL_GPL(wilc_handle_isr);
1081 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
1085 u32 addr, size, size2, blksz;
1092 dma_buffer = kmalloc(blksz, GFP_KERNEL);
1097 pr_debug("%s: Downloading firmware size = %d\n", __func__, buffer_size);
1099 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1101 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1103 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1104 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1106 pr_err("%s: Failed to reset\n", __func__);
1108 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1110 addr = get_unaligned_le32(&buffer[offset]);
1111 size = get_unaligned_le32(&buffer[offset + 4]);
1112 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1114 while (((int)size) && (offset < buffer_size)) {
1120 memcpy(dma_buffer, &buffer[offset], size2);
1121 ret = wilc->hif_func->hif_block_tx(wilc, addr,
1130 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1133 pr_err("%s Bus error\n", __func__);
1136 pr_debug("%s Offset = %d\n", __func__, offset);
1137 } while (offset < buffer_size);
1146 int wilc_wlan_start(struct wilc *wilc)
1152 if (wilc->io_type == WILC_HIF_SDIO) {
1155 } else if (wilc->io_type == WILC_HIF_SPI) {
1158 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1159 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1164 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
1165 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1167 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1171 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1173 ret = wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
1177 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1178 if ((reg & BIT(10)) == BIT(10)) {
1180 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1181 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1185 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1186 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1189 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1193 int wilc_wlan_stop(struct wilc *wilc, struct wilc_vif *vif)
1198 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1200 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1202 netdev_err(vif->ndev, "Error while reading reg\n");
1206 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1207 (reg | WILC_ABORT_REQ_BIT));
1209 netdev_err(vif->ndev, "Error while writing reg\n");
1213 ret = wilc->hif_func->hif_read_reg(wilc, WILC_FW_HOST_COMM, ®);
1215 netdev_err(vif->ndev, "Error while reading reg\n");
1220 ret = wilc->hif_func->hif_write_reg(wilc, WILC_FW_HOST_COMM, reg);
1222 netdev_err(vif->ndev, "Error while writing reg\n");
1228 /* host comm is disabled - we can't issue sleep command anymore: */
1229 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1234 void wilc_wlan_cleanup(struct net_device *dev)
1236 struct txq_entry_t *tqe;
1237 struct rxq_entry_t *rqe;
1239 struct wilc_vif *vif = netdev_priv(dev);
1240 struct wilc *wilc = vif->wilc;
1243 for (ac = 0; ac < NQUEUES; ac++) {
1244 while ((tqe = wilc_wlan_txq_remove_from_head(wilc, ac))) {
1245 if (tqe->tx_complete_func)
1246 tqe->tx_complete_func(tqe->priv, 0);
1251 while ((rqe = wilc_wlan_rxq_remove(wilc)))
1254 kfree(wilc->rx_buffer);
1255 wilc->rx_buffer = NULL;
1256 kfree(wilc->tx_buffer);
1257 wilc->tx_buffer = NULL;
1258 wilc->hif_func->hif_deinit(wilc);
1261 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1264 struct wilc *wilc = vif->wilc;
1265 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1266 int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
1268 if (type == WILC_CFG_SET)
1269 cfg->hdr.cmd_type = 'W';
1271 cfg->hdr.cmd_type = 'Q';
1273 cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
1274 cfg->hdr.total_len = cpu_to_le16(t_len);
1275 cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
1276 wilc->cfg_seq_no = cfg->hdr.seq_no;
1278 if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
1284 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1285 u32 buffer_size, int commit, u32 drv_handler)
1289 struct wilc *wilc = vif->wilc;
1291 mutex_lock(&wilc->cfg_cmd_lock);
1294 wilc->cfg_frame_offset = 0;
1296 offset = wilc->cfg_frame_offset;
1297 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1298 wid, buffer, buffer_size);
1300 wilc->cfg_frame_offset = offset;
1303 mutex_unlock(&wilc->cfg_cmd_lock);
1307 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1309 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1312 if (!wait_for_completion_timeout(&wilc->cfg_event,
1313 WILC_CFG_PKTS_TIMEOUT)) {
1314 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1318 wilc->cfg_frame_offset = 0;
1319 wilc->cfg_seq_no += 1;
1320 mutex_unlock(&wilc->cfg_cmd_lock);
1325 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1330 struct wilc *wilc = vif->wilc;
1332 mutex_lock(&wilc->cfg_cmd_lock);
1335 wilc->cfg_frame_offset = 0;
1337 offset = wilc->cfg_frame_offset;
1338 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1340 wilc->cfg_frame_offset = offset;
1343 mutex_unlock(&wilc->cfg_cmd_lock);
1347 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1350 if (!wait_for_completion_timeout(&wilc->cfg_event,
1351 WILC_CFG_PKTS_TIMEOUT)) {
1352 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1355 wilc->cfg_frame_offset = 0;
1356 wilc->cfg_seq_no += 1;
1357 mutex_unlock(&wilc->cfg_cmd_lock);
1362 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1367 u32 drv = wilc_get_vif_idx(vif);
1369 if (mode == WILC_GET_CFG) {
1370 for (i = 0; i < count; i++) {
1371 if (!wilc_wlan_cfg_get(vif, !i,
1379 for (i = 0; i < count; i++) {
1380 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1385 } else if (mode == WILC_SET_CFG) {
1386 for (i = 0; i < count; i++) {
1387 if (!wilc_wlan_cfg_set(vif, !i,
1402 static int init_chip(struct net_device *dev)
1407 struct wilc_vif *vif = netdev_priv(dev);
1408 struct wilc *wilc = vif->wilc;
1410 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1412 chipid = wilc_get_chipid(wilc, true);
1414 if ((chipid & 0xfff) != 0xa0) {
1415 ret = wilc->hif_func->hif_read_reg(wilc,
1416 WILC_CORTUS_RESET_MUX_SEL,
1419 netdev_err(dev, "fail read reg 0x1118\n");
1423 ret = wilc->hif_func->hif_write_reg(wilc,
1424 WILC_CORTUS_RESET_MUX_SEL,
1427 netdev_err(dev, "fail write reg 0x1118\n");
1430 ret = wilc->hif_func->hif_write_reg(wilc,
1431 WILC_CORTUS_BOOT_REGISTER,
1432 WILC_CORTUS_BOOT_FROM_IRAM);
1434 netdev_err(dev, "fail write reg 0xc0000\n");
1440 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1445 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1450 if (wilc->chipid == 0 || update) {
1451 wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
1452 wilc->hif_func->hif_read_reg(wilc, WILC_RF_REVISION_ID,
1454 if (!is_wilc1000(chipid)) {
1456 return wilc->chipid;
1458 if (chipid == WILC_1000_BASE_ID_2A) { /* 0x1002A0 */
1460 chipid = WILC_1000_BASE_ID_2A_REV1;
1461 } else if (chipid == WILC_1000_BASE_ID_2B) { /* 0x1002B0 */
1463 chipid = WILC_1000_BASE_ID_2B_REV1;
1464 else if (rfrevid != 0x3)
1465 chipid = WILC_1000_BASE_ID_2B_REV2;
1468 wilc->chipid = chipid;
1470 return wilc->chipid;
1473 int wilc_wlan_init(struct net_device *dev)
1476 struct wilc_vif *vif = netdev_priv(dev);
1483 if (wilc->hif_func->hif_init(wilc, false)) {
1488 if (!wilc->tx_buffer)
1489 wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1491 if (!wilc->tx_buffer) {
1496 if (!wilc->rx_buffer)
1497 wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1499 if (!wilc->rx_buffer) {
1504 if (init_chip(dev)) {
1513 kfree(wilc->rx_buffer);
1514 wilc->rx_buffer = NULL;
1515 kfree(wilc->tx_buffer);
1516 wilc->tx_buffer = NULL;