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, void *priv, u8 *buffer,
413 void (*tx_complete_fn)(void *, int))
415 struct txq_entry_t *tqe;
416 struct wilc_vif *vif = netdev_priv(dev);
423 tx_complete_fn(priv, 0);
427 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
430 tx_complete_fn(priv, 0);
433 tqe->type = WILC_NET_PKT;
434 tqe->buffer = buffer;
435 tqe->buffer_size = buffer_size;
436 tqe->tx_complete_func = tx_complete_fn;
440 q_num = ac_classify(wilc, priv);
442 if (ac_change(wilc, &q_num)) {
443 tx_complete_fn(priv, 0);
448 if (is_ac_q_limit(wilc, q_num)) {
449 tqe->ack_idx = NOT_TCP_ACK;
450 if (vif->ack_filter.enabled)
451 tcp_process(dev, tqe);
452 wilc_wlan_txq_add_to_tail(dev, q_num, tqe);
454 tx_complete_fn(priv, 0);
458 return wilc->txq_entries;
461 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
463 void (*tx_complete_fn)(void *, int))
465 struct txq_entry_t *tqe;
466 struct wilc_vif *vif = netdev_priv(dev);
472 tx_complete_fn(priv, 0);
476 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
479 tx_complete_fn(priv, 0);
482 tqe->type = WILC_MGMT_PKT;
483 tqe->buffer = buffer;
484 tqe->buffer_size = buffer_size;
485 tqe->tx_complete_func = tx_complete_fn;
487 tqe->q_num = AC_BE_Q;
488 tqe->ack_idx = NOT_TCP_ACK;
490 wilc_wlan_txq_add_to_tail(dev, AC_VO_Q, tqe);
494 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc, u8 q_num)
496 struct txq_entry_t *tqe = NULL;
499 spin_lock_irqsave(&wilc->txq_spinlock, flags);
501 if (!list_empty(&wilc->txq[q_num].txq_head.list))
502 tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
503 struct txq_entry_t, list);
505 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
510 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
511 struct txq_entry_t *tqe,
516 spin_lock_irqsave(&wilc->txq_spinlock, flags);
518 if (!list_is_last(&tqe->list, &wilc->txq[q_num].txq_head.list))
519 tqe = list_next_entry(tqe, list);
522 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
527 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
532 mutex_lock(&wilc->rxq_cs);
533 list_add_tail(&rqe->list, &wilc->rxq_head.list);
534 mutex_unlock(&wilc->rxq_cs);
537 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
539 struct rxq_entry_t *rqe = NULL;
541 mutex_lock(&wilc->rxq_cs);
542 if (!list_empty(&wilc->rxq_head.list)) {
543 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
545 list_del(&rqe->list);
547 mutex_unlock(&wilc->rxq_cs);
551 void chip_allow_sleep(struct wilc *wilc)
555 wilc->hif_func->hif_read_reg(wilc, WILC_SDIO_WAKEUP_REG, ®);
557 wilc->hif_func->hif_write_reg(wilc, WILC_SDIO_WAKEUP_REG,
558 reg & ~WILC_SDIO_WAKEUP_BIT);
559 wilc->hif_func->hif_write_reg(wilc, WILC_SDIO_HOST_TO_FW_REG, 0);
561 EXPORT_SYMBOL_GPL(chip_allow_sleep);
563 void chip_wakeup(struct wilc *wilc)
565 u32 reg, clk_status_reg;
566 const struct wilc_hif_func *h = wilc->hif_func;
568 if (wilc->io_type == WILC_HIF_SPI) {
570 h->hif_read_reg(wilc, WILC_SPI_WAKEUP_REG, ®);
571 h->hif_write_reg(wilc, WILC_SPI_WAKEUP_REG,
572 reg | WILC_SPI_WAKEUP_BIT);
573 h->hif_write_reg(wilc, WILC_SPI_WAKEUP_REG,
574 reg & ~WILC_SPI_WAKEUP_BIT);
577 usleep_range(2000, 2500);
578 wilc_get_chipid(wilc, true);
579 } while (wilc_get_chipid(wilc, true) == 0);
580 } while (wilc_get_chipid(wilc, true) == 0);
581 } else if (wilc->io_type == WILC_HIF_SDIO) {
582 h->hif_write_reg(wilc, WILC_SDIO_HOST_TO_FW_REG,
583 WILC_SDIO_HOST_TO_FW_BIT);
584 usleep_range(200, 400);
585 h->hif_read_reg(wilc, WILC_SDIO_WAKEUP_REG, ®);
587 h->hif_write_reg(wilc, WILC_SDIO_WAKEUP_REG,
588 reg | WILC_SDIO_WAKEUP_BIT);
589 h->hif_read_reg(wilc, WILC_SDIO_CLK_STATUS_REG,
592 while (!(clk_status_reg & WILC_SDIO_CLK_STATUS_BIT)) {
593 usleep_range(2000, 2500);
595 h->hif_read_reg(wilc, WILC_SDIO_CLK_STATUS_REG,
598 if (!(clk_status_reg & WILC_SDIO_CLK_STATUS_BIT)) {
599 h->hif_write_reg(wilc, WILC_SDIO_WAKEUP_REG,
600 reg & ~WILC_SDIO_WAKEUP_BIT);
602 } while (!(clk_status_reg & WILC_SDIO_CLK_STATUS_BIT));
605 if (wilc->chip_ps_state == WILC_CHIP_SLEEPING_MANUAL) {
606 if (wilc_get_chipid(wilc, false) < WILC_1000_BASE_ID_2B) {
609 h->hif_read_reg(wilc, WILC_REG_4_TO_1_RX, &val32);
611 h->hif_write_reg(wilc, WILC_REG_4_TO_1_RX, val32);
613 h->hif_read_reg(wilc, WILC_REG_4_TO_1_TX_BANK0, &val32);
615 h->hif_write_reg(wilc, WILC_REG_4_TO_1_TX_BANK0, val32);
618 wilc->chip_ps_state = WILC_CHIP_WAKEDUP;
620 EXPORT_SYMBOL_GPL(chip_wakeup);
622 void host_wakeup_notify(struct wilc *wilc)
624 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
625 wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_2, 1);
626 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
628 EXPORT_SYMBOL_GPL(host_wakeup_notify);
630 void host_sleep_notify(struct wilc *wilc)
632 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
633 wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_1, 1);
634 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
636 EXPORT_SYMBOL_GPL(host_sleep_notify);
638 int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
644 u8 ac_desired_ratio[NQUEUES] = {0, 0, 0, 0};
645 u8 ac_preserve_ratio[NQUEUES] = {1, 1, 1, 1};
647 u8 vmm_entries_ac[WILC_VMM_TBL_SIZE];
649 bool max_size_over = 0, ac_exist = 0;
651 struct txq_entry_t *tqe_q[NQUEUES];
655 u32 vmm_table[WILC_VMM_TBL_SIZE];
656 u8 ac_pkt_num_to_chip[NQUEUES] = {0, 0, 0, 0};
657 const struct wilc_hif_func *func;
659 u8 *txb = wilc->tx_buffer;
660 struct wilc_vif *vif;
665 if (ac_balance(wilc, ac_desired_ratio))
668 mutex_lock(&wilc->txq_add_to_head_cs);
670 srcu_idx = srcu_read_lock(&wilc->srcu);
671 list_for_each_entry_rcu(vif, &wilc->vif_list, list)
672 wilc_wlan_txq_filter_dup_tcp_ack(vif->ndev);
673 srcu_read_unlock(&wilc->srcu, srcu_idx);
675 for (ac = 0; ac < NQUEUES; ac++)
676 tqe_q[ac] = wilc_wlan_txq_get_first(wilc, ac);
681 num_pkts_to_add = ac_desired_ratio;
684 for (ac = 0; (ac < NQUEUES) && (!max_size_over); ac++) {
689 for (k = 0; (k < num_pkts_to_add[ac]) &&
690 (!max_size_over) && tqe_q[ac]; k++) {
691 if (i >= (WILC_VMM_TBL_SIZE - 1)) {
696 if (tqe_q[ac]->type == WILC_CFG_PKT)
697 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
698 else if (tqe_q[ac]->type == WILC_NET_PKT)
699 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
701 vmm_sz = HOST_HDR_OFFSET;
703 vmm_sz += tqe_q[ac]->buffer_size;
704 vmm_sz = ALIGN(vmm_sz, 4);
706 if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE) {
710 vmm_table[i] = vmm_sz / 4;
711 if (tqe_q[ac]->type == WILC_CFG_PKT)
712 vmm_table[i] |= BIT(10);
714 cpu_to_le32s(&vmm_table[i]);
715 vmm_entries_ac[i] = ac;
719 tqe_q[ac] = wilc_wlan_txq_get_next(wilc,
724 num_pkts_to_add = ac_preserve_ratio;
725 } while (!max_size_over && ac_exist);
731 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
733 func = wilc->hif_func;
735 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
739 if ((reg & 0x1) == 0) {
740 ac_update_fw_ac_pkt_info(wilc, reg);
747 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
750 } while (!wilc->quit);
753 goto out_release_bus;
757 ret = func->hif_block_tx(wilc,
758 WILC_VMM_TBL_RX_SHADOW_BASE,
764 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
769 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, ®);
772 if (FIELD_GET(WILC_VMM_ENTRY_AVAILABLE, reg)) {
773 entries = FIELD_GET(WILC_VMM_ENTRY_COUNT, reg);
778 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
786 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, ®);
790 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
795 goto out_release_bus;
799 * No VMM space available in firmware so retry to transmit
800 * the packet from tx queue.
802 ret = WILC_VMM_ENTRY_FULL_RETRY;
803 goto out_release_bus;
806 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
811 struct txq_entry_t *tqe;
812 u32 header, buffer_offset;
816 tqe = wilc_wlan_txq_remove_from_head(wilc, vmm_entries_ac[i]);
817 ac_pkt_num_to_chip[vmm_entries_ac[i]]++;
822 if (vmm_table[i] == 0)
825 le32_to_cpus(&vmm_table[i]);
826 vmm_sz = FIELD_GET(WILC_VMM_BUFFER_SIZE, vmm_table[i]);
829 if (tqe->type == WILC_MGMT_PKT)
832 header = (FIELD_PREP(WILC_VMM_HDR_TYPE, tqe->type) |
833 FIELD_PREP(WILC_VMM_HDR_MGMT_FIELD, mgmt_ptk) |
834 FIELD_PREP(WILC_VMM_HDR_PKT_SIZE, tqe->buffer_size) |
835 FIELD_PREP(WILC_VMM_HDR_BUFF_SIZE, vmm_sz));
837 cpu_to_le32s(&header);
838 memcpy(&txb[offset], &header, 4);
839 if (tqe->type == WILC_CFG_PKT) {
840 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
841 } else if (tqe->type == WILC_NET_PKT) {
842 int prio = tqe->q_num;
844 bssid = tqe->vif->bssid;
845 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
846 memcpy(&txb[offset + 4], &prio, sizeof(prio));
847 memcpy(&txb[offset + 8], bssid, 6);
849 buffer_offset = HOST_HDR_OFFSET;
852 memcpy(&txb[offset + buffer_offset],
853 tqe->buffer, tqe->buffer_size);
857 if (tqe->tx_complete_func)
858 tqe->tx_complete_func(tqe->priv, tqe->status);
859 if (tqe->ack_idx != NOT_TCP_ACK &&
860 tqe->ack_idx < MAX_PENDING_ACKS)
861 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
864 for (i = 0; i < NQUEUES; i++)
865 wilc->txq[i].fw.count += ac_pkt_num_to_chip[i];
867 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
869 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
871 goto out_release_bus;
873 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
876 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
879 mutex_unlock(&wilc->txq_add_to_head_cs);
882 *txq_count = wilc->txq_entries;
886 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
890 u32 pkt_len, pkt_offset, tp_len;
895 buff_ptr = buffer + offset;
896 header = get_unaligned_le32(buff_ptr);
898 is_cfg_packet = FIELD_GET(WILC_PKT_HDR_CONFIG_FIELD, header);
899 pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
900 tp_len = FIELD_GET(WILC_PKT_HDR_TOTAL_LEN_FIELD, header);
901 pkt_len = FIELD_GET(WILC_PKT_HDR_LEN_FIELD, header);
903 if (pkt_len == 0 || tp_len == 0)
906 if (pkt_offset & IS_MANAGMEMENT) {
907 buff_ptr += HOST_HDR_OFFSET;
908 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
910 if (!is_cfg_packet) {
911 wilc_frmw_to_host(wilc, buff_ptr, pkt_len,
914 struct wilc_cfg_rsp rsp;
916 buff_ptr += pkt_offset;
918 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
921 if (rsp.type == WILC_CFG_RSP) {
922 if (wilc->cfg_seq_no == rsp.seq_no)
923 complete(&wilc->cfg_event);
924 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
925 wilc_mac_indicate(wilc);
930 } while (offset < size);
933 static void wilc_wlan_handle_rxq(struct wilc *wilc)
937 struct rxq_entry_t *rqe;
939 while (!wilc->quit) {
940 rqe = wilc_wlan_rxq_remove(wilc);
944 buffer = rqe->buffer;
945 size = rqe->buffer_size;
946 wilc_wlan_handle_rx_buff(wilc, buffer, size);
951 complete(&wilc->cfg_event);
954 static void wilc_unknown_isr_ext(struct wilc *wilc)
956 wilc->hif_func->hif_clear_int_ext(wilc, 0);
959 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
961 u32 offset = wilc->rx_buffer_offset;
966 struct rxq_entry_t *rqe;
968 size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, int_status) << 2;
970 while (!size && retries < 10) {
971 wilc->hif_func->hif_read_size(wilc, &size);
972 size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, size) << 2;
979 if (WILC_RX_BUFF_SIZE - offset < size)
982 buffer = &wilc->rx_buffer[offset];
984 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
985 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
990 wilc->rx_buffer_offset = offset;
991 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
995 rqe->buffer = buffer;
996 rqe->buffer_size = size;
997 wilc_wlan_rxq_add(wilc, rqe);
998 wilc_wlan_handle_rxq(wilc);
1001 void wilc_handle_isr(struct wilc *wilc)
1005 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1006 wilc->hif_func->hif_read_int(wilc, &int_status);
1008 if (int_status & DATA_INT_EXT)
1009 wilc_wlan_handle_isr_ext(wilc, int_status);
1011 if (!(int_status & (ALL_INT_EXT)))
1012 wilc_unknown_isr_ext(wilc);
1014 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1016 EXPORT_SYMBOL_GPL(wilc_handle_isr);
1018 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
1022 u32 addr, size, size2, blksz;
1028 dma_buffer = kmalloc(blksz, GFP_KERNEL);
1034 addr = get_unaligned_le32(&buffer[offset]);
1035 size = get_unaligned_le32(&buffer[offset + 4]);
1036 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1038 while (((int)size) && (offset < buffer_size)) {
1044 memcpy(dma_buffer, &buffer[offset], size2);
1045 ret = wilc->hif_func->hif_block_tx(wilc, addr,
1054 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1058 } while (offset < buffer_size);
1067 int wilc_wlan_start(struct wilc *wilc)
1073 if (wilc->io_type == WILC_HIF_SDIO) {
1076 } else if (wilc->io_type == WILC_HIF_SPI) {
1079 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1080 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1082 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1086 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
1087 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1089 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1091 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1095 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1097 ret = wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
1099 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1103 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1104 if ((reg & BIT(10)) == BIT(10)) {
1106 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1107 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1111 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1112 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, ®);
1113 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1118 int wilc_wlan_stop(struct wilc *wilc, struct wilc_vif *vif)
1123 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1125 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, ®);
1127 netdev_err(vif->ndev, "Error while reading reg\n");
1128 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1132 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1133 (reg | WILC_ABORT_REQ_BIT));
1135 netdev_err(vif->ndev, "Error while writing reg\n");
1136 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1140 ret = wilc->hif_func->hif_read_reg(wilc, WILC_FW_HOST_COMM, ®);
1142 netdev_err(vif->ndev, "Error while reading reg\n");
1143 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1148 ret = wilc->hif_func->hif_write_reg(wilc, WILC_FW_HOST_COMM, reg);
1150 netdev_err(vif->ndev, "Error while writing reg\n");
1151 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1155 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1160 void wilc_wlan_cleanup(struct net_device *dev)
1162 struct txq_entry_t *tqe;
1163 struct rxq_entry_t *rqe;
1165 struct wilc_vif *vif = netdev_priv(dev);
1166 struct wilc *wilc = vif->wilc;
1169 for (ac = 0; ac < NQUEUES; ac++) {
1170 while ((tqe = wilc_wlan_txq_remove_from_head(wilc, ac))) {
1171 if (tqe->tx_complete_func)
1172 tqe->tx_complete_func(tqe->priv, 0);
1177 while ((rqe = wilc_wlan_rxq_remove(wilc)))
1180 kfree(wilc->rx_buffer);
1181 wilc->rx_buffer = NULL;
1182 kfree(wilc->tx_buffer);
1183 wilc->tx_buffer = NULL;
1184 wilc->hif_func->hif_deinit(NULL);
1187 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1190 struct wilc *wilc = vif->wilc;
1191 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1192 int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
1194 if (type == WILC_CFG_SET)
1195 cfg->hdr.cmd_type = 'W';
1197 cfg->hdr.cmd_type = 'Q';
1199 cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
1200 cfg->hdr.total_len = cpu_to_le16(t_len);
1201 cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
1202 wilc->cfg_seq_no = cfg->hdr.seq_no;
1204 if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
1210 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1211 u32 buffer_size, int commit, u32 drv_handler)
1215 struct wilc *wilc = vif->wilc;
1217 mutex_lock(&wilc->cfg_cmd_lock);
1220 wilc->cfg_frame_offset = 0;
1222 offset = wilc->cfg_frame_offset;
1223 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1224 wid, buffer, buffer_size);
1226 wilc->cfg_frame_offset = offset;
1229 mutex_unlock(&wilc->cfg_cmd_lock);
1233 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1235 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1238 if (!wait_for_completion_timeout(&wilc->cfg_event,
1239 WILC_CFG_PKTS_TIMEOUT)) {
1240 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1244 wilc->cfg_frame_offset = 0;
1245 wilc->cfg_seq_no += 1;
1246 mutex_unlock(&wilc->cfg_cmd_lock);
1251 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1256 struct wilc *wilc = vif->wilc;
1258 mutex_lock(&wilc->cfg_cmd_lock);
1261 wilc->cfg_frame_offset = 0;
1263 offset = wilc->cfg_frame_offset;
1264 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1266 wilc->cfg_frame_offset = offset;
1269 mutex_unlock(&wilc->cfg_cmd_lock);
1273 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1276 if (!wait_for_completion_timeout(&wilc->cfg_event,
1277 WILC_CFG_PKTS_TIMEOUT)) {
1278 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1281 wilc->cfg_frame_offset = 0;
1282 wilc->cfg_seq_no += 1;
1283 mutex_unlock(&wilc->cfg_cmd_lock);
1288 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1293 u32 drv = wilc_get_vif_idx(vif);
1295 if (mode == WILC_GET_CFG) {
1296 for (i = 0; i < count; i++) {
1297 if (!wilc_wlan_cfg_get(vif, !i,
1305 for (i = 0; i < count; i++) {
1306 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1311 } else if (mode == WILC_SET_CFG) {
1312 for (i = 0; i < count; i++) {
1313 if (!wilc_wlan_cfg_set(vif, !i,
1328 static int init_chip(struct net_device *dev)
1333 struct wilc_vif *vif = netdev_priv(dev);
1334 struct wilc *wilc = vif->wilc;
1336 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1338 chipid = wilc_get_chipid(wilc, true);
1340 if ((chipid & 0xfff) != 0xa0) {
1341 ret = wilc->hif_func->hif_read_reg(wilc,
1342 WILC_CORTUS_RESET_MUX_SEL,
1345 netdev_err(dev, "fail read reg 0x1118\n");
1349 ret = wilc->hif_func->hif_write_reg(wilc,
1350 WILC_CORTUS_RESET_MUX_SEL,
1353 netdev_err(dev, "fail write reg 0x1118\n");
1356 ret = wilc->hif_func->hif_write_reg(wilc,
1357 WILC_CORTUS_BOOT_REGISTER,
1358 WILC_CORTUS_BOOT_FROM_IRAM);
1360 netdev_err(dev, "fail write reg 0xc0000\n");
1366 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1371 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1377 if (chipid == 0 || update) {
1378 wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &tempchipid);
1379 wilc->hif_func->hif_read_reg(wilc, WILC_RF_REVISION_ID,
1381 if (!is_wilc1000(tempchipid)) {
1385 if (tempchipid == WILC_1000_BASE_ID_2A) { /* 0x1002A0 */
1387 tempchipid = WILC_1000_BASE_ID_2A_REV1;
1388 } else if (tempchipid == WILC_1000_BASE_ID_2B) { /* 0x1002B0 */
1390 tempchipid = WILC_1000_BASE_ID_2B_REV1;
1391 else if (rfrevid != 0x3)
1392 tempchipid = WILC_1000_BASE_ID_2B_REV2;
1395 chipid = tempchipid;
1400 int wilc_wlan_init(struct net_device *dev)
1403 struct wilc_vif *vif = netdev_priv(dev);
1410 if (wilc->hif_func->hif_init(wilc, false)) {
1415 if (!wilc->tx_buffer)
1416 wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1418 if (!wilc->tx_buffer) {
1423 if (!wilc->rx_buffer)
1424 wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1426 if (!wilc->rx_buffer) {
1431 if (init_chip(dev)) {
1440 kfree(wilc->rx_buffer);
1441 wilc->rx_buffer = NULL;
1442 kfree(wilc->tx_buffer);
1443 wilc->tx_buffer = NULL;