xref: /openbmc/linux/drivers/net/wireless/microchip/wilc1000/wlan.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
15625f965SAjay Singh // SPDX-License-Identifier: GPL-2.0
25625f965SAjay Singh /*
35625f965SAjay Singh  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
45625f965SAjay Singh  * All rights reserved.
55625f965SAjay Singh  */
65625f965SAjay Singh 
75625f965SAjay Singh #include <linux/if_ether.h>
85625f965SAjay Singh #include <linux/ip.h>
9339754ffSAjay Singh #include <net/dsfield.h>
105625f965SAjay Singh #include "cfg80211.h"
115625f965SAjay Singh #include "wlan_cfg.h"
125625f965SAjay Singh 
135bb9de8bSAjay Singh #define WAKE_UP_TRIAL_RETRY		10000
145bb9de8bSAjay Singh 
is_wilc1000(u32 id)155625f965SAjay Singh static inline bool is_wilc1000(u32 id)
165625f965SAjay Singh {
175625f965SAjay Singh 	return (id & (~WILC_CHIP_REV_FIELD)) == WILC_1000_BASE_ID;
185625f965SAjay Singh }
195625f965SAjay Singh 
acquire_bus(struct wilc * wilc,enum bus_acquire acquire)205625f965SAjay Singh static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
215625f965SAjay Singh {
225625f965SAjay Singh 	mutex_lock(&wilc->hif_cs);
23b530d5f3SDavid Mosberger-Tang 	if (acquire == WILC_BUS_ACQUIRE_AND_WAKEUP && wilc->power_save_mode)
245625f965SAjay Singh 		chip_wakeup(wilc);
255625f965SAjay Singh }
265625f965SAjay Singh 
release_bus(struct wilc * wilc,enum bus_release release)275625f965SAjay Singh static inline void release_bus(struct wilc *wilc, enum bus_release release)
285625f965SAjay Singh {
29b530d5f3SDavid Mosberger-Tang 	if (release == WILC_BUS_RELEASE_ALLOW_SLEEP && wilc->power_save_mode)
305625f965SAjay Singh 		chip_allow_sleep(wilc);
315625f965SAjay Singh 	mutex_unlock(&wilc->hif_cs);
325625f965SAjay Singh }
335625f965SAjay Singh 
wilc_wlan_txq_remove(struct wilc * wilc,u8 q_num,struct txq_entry_t * tqe)34339754ffSAjay Singh static void wilc_wlan_txq_remove(struct wilc *wilc, u8 q_num,
35339754ffSAjay Singh 				 struct txq_entry_t *tqe)
365625f965SAjay Singh {
375625f965SAjay Singh 	list_del(&tqe->list);
385625f965SAjay Singh 	wilc->txq_entries -= 1;
39339754ffSAjay Singh 	wilc->txq[q_num].count--;
405625f965SAjay Singh }
415625f965SAjay Singh 
425625f965SAjay Singh static struct txq_entry_t *
wilc_wlan_txq_remove_from_head(struct wilc * wilc,u8 q_num)43339754ffSAjay Singh wilc_wlan_txq_remove_from_head(struct wilc *wilc, u8 q_num)
445625f965SAjay Singh {
455625f965SAjay Singh 	struct txq_entry_t *tqe = NULL;
465625f965SAjay Singh 	unsigned long flags;
475625f965SAjay Singh 
485625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
495625f965SAjay Singh 
50339754ffSAjay Singh 	if (!list_empty(&wilc->txq[q_num].txq_head.list)) {
51339754ffSAjay Singh 		tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
52339754ffSAjay Singh 				       struct txq_entry_t, list);
535625f965SAjay Singh 		list_del(&tqe->list);
545625f965SAjay Singh 		wilc->txq_entries -= 1;
55339754ffSAjay Singh 		wilc->txq[q_num].count--;
565625f965SAjay Singh 	}
575625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
585625f965SAjay Singh 	return tqe;
595625f965SAjay Singh }
605625f965SAjay Singh 
wilc_wlan_txq_add_to_tail(struct net_device * dev,u8 q_num,struct txq_entry_t * tqe)61339754ffSAjay Singh static void wilc_wlan_txq_add_to_tail(struct net_device *dev, u8 q_num,
625625f965SAjay Singh 				      struct txq_entry_t *tqe)
635625f965SAjay Singh {
645625f965SAjay Singh 	unsigned long flags;
655625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
665625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
675625f965SAjay Singh 
685625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
695625f965SAjay Singh 
70339754ffSAjay Singh 	list_add_tail(&tqe->list, &wilc->txq[q_num].txq_head.list);
715625f965SAjay Singh 	wilc->txq_entries += 1;
72339754ffSAjay Singh 	wilc->txq[q_num].count++;
735625f965SAjay Singh 
745625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
755625f965SAjay Singh 
765625f965SAjay Singh 	complete(&wilc->txq_event);
775625f965SAjay Singh }
785625f965SAjay Singh 
wilc_wlan_txq_add_to_head(struct wilc_vif * vif,u8 q_num,struct txq_entry_t * tqe)79339754ffSAjay Singh static void wilc_wlan_txq_add_to_head(struct wilc_vif *vif, u8 q_num,
805625f965SAjay Singh 				      struct txq_entry_t *tqe)
815625f965SAjay Singh {
825625f965SAjay Singh 	unsigned long flags;
835625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
845625f965SAjay Singh 
855625f965SAjay Singh 	mutex_lock(&wilc->txq_add_to_head_cs);
865625f965SAjay Singh 
875625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
885625f965SAjay Singh 
89339754ffSAjay Singh 	list_add(&tqe->list, &wilc->txq[q_num].txq_head.list);
905625f965SAjay Singh 	wilc->txq_entries += 1;
91339754ffSAjay Singh 	wilc->txq[q_num].count++;
925625f965SAjay Singh 
935625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
945625f965SAjay Singh 	mutex_unlock(&wilc->txq_add_to_head_cs);
955625f965SAjay Singh 	complete(&wilc->txq_event);
965625f965SAjay Singh }
975625f965SAjay Singh 
985625f965SAjay Singh #define NOT_TCP_ACK			(-1)
995625f965SAjay Singh 
add_tcp_session(struct wilc_vif * vif,u32 src_prt,u32 dst_prt,u32 seq)1005625f965SAjay Singh static inline void add_tcp_session(struct wilc_vif *vif, u32 src_prt,
1015625f965SAjay Singh 				   u32 dst_prt, u32 seq)
1025625f965SAjay Singh {
1035625f965SAjay Singh 	struct tcp_ack_filter *f = &vif->ack_filter;
1045625f965SAjay Singh 
1055625f965SAjay Singh 	if (f->tcp_session < 2 * MAX_TCP_SESSION) {
1065625f965SAjay Singh 		f->ack_session_info[f->tcp_session].seq_num = seq;
1075625f965SAjay Singh 		f->ack_session_info[f->tcp_session].bigger_ack_num = 0;
1085625f965SAjay Singh 		f->ack_session_info[f->tcp_session].src_port = src_prt;
1095625f965SAjay Singh 		f->ack_session_info[f->tcp_session].dst_port = dst_prt;
1105625f965SAjay Singh 		f->tcp_session++;
1115625f965SAjay Singh 	}
1125625f965SAjay Singh }
1135625f965SAjay Singh 
update_tcp_session(struct wilc_vif * vif,u32 index,u32 ack)1145625f965SAjay Singh static inline void update_tcp_session(struct wilc_vif *vif, u32 index, u32 ack)
1155625f965SAjay Singh {
1165625f965SAjay Singh 	struct tcp_ack_filter *f = &vif->ack_filter;
1175625f965SAjay Singh 
1185625f965SAjay Singh 	if (index < 2 * MAX_TCP_SESSION &&
1195625f965SAjay Singh 	    ack > f->ack_session_info[index].bigger_ack_num)
1205625f965SAjay Singh 		f->ack_session_info[index].bigger_ack_num = ack;
1215625f965SAjay Singh }
1225625f965SAjay Singh 
add_tcp_pending_ack(struct wilc_vif * vif,u32 ack,u32 session_index,struct txq_entry_t * txqe)1235625f965SAjay Singh static inline void add_tcp_pending_ack(struct wilc_vif *vif, u32 ack,
1245625f965SAjay Singh 				       u32 session_index,
1255625f965SAjay Singh 				       struct txq_entry_t *txqe)
1265625f965SAjay Singh {
1275625f965SAjay Singh 	struct tcp_ack_filter *f = &vif->ack_filter;
1285625f965SAjay Singh 	u32 i = f->pending_base + f->pending_acks_idx;
1295625f965SAjay Singh 
1305625f965SAjay Singh 	if (i < MAX_PENDING_ACKS) {
1315625f965SAjay Singh 		f->pending_acks[i].ack_num = ack;
1325625f965SAjay Singh 		f->pending_acks[i].txqe = txqe;
1335625f965SAjay Singh 		f->pending_acks[i].session_index = session_index;
1345625f965SAjay Singh 		txqe->ack_idx = i;
1355625f965SAjay Singh 		f->pending_acks_idx++;
1365625f965SAjay Singh 	}
1375625f965SAjay Singh }
1385625f965SAjay Singh 
tcp_process(struct net_device * dev,struct txq_entry_t * tqe)1395625f965SAjay Singh static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
1405625f965SAjay Singh {
1415625f965SAjay Singh 	void *buffer = tqe->buffer;
1425625f965SAjay Singh 	const struct ethhdr *eth_hdr_ptr = buffer;
1435625f965SAjay Singh 	int i;
1445625f965SAjay Singh 	unsigned long flags;
1455625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
1465625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
1475625f965SAjay Singh 	struct tcp_ack_filter *f = &vif->ack_filter;
1485625f965SAjay Singh 	const struct iphdr *ip_hdr_ptr;
1495625f965SAjay Singh 	const struct tcphdr *tcp_hdr_ptr;
1505625f965SAjay Singh 	u32 ihl, total_length, data_offset;
1515625f965SAjay Singh 
1525625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
1535625f965SAjay Singh 
1545625f965SAjay Singh 	if (eth_hdr_ptr->h_proto != htons(ETH_P_IP))
1555625f965SAjay Singh 		goto out;
1565625f965SAjay Singh 
1575625f965SAjay Singh 	ip_hdr_ptr = buffer + ETH_HLEN;
1585625f965SAjay Singh 
1595625f965SAjay Singh 	if (ip_hdr_ptr->protocol != IPPROTO_TCP)
1605625f965SAjay Singh 		goto out;
1615625f965SAjay Singh 
1625625f965SAjay Singh 	ihl = ip_hdr_ptr->ihl << 2;
1635625f965SAjay Singh 	tcp_hdr_ptr = buffer + ETH_HLEN + ihl;
1645625f965SAjay Singh 	total_length = ntohs(ip_hdr_ptr->tot_len);
1655625f965SAjay Singh 
1665625f965SAjay Singh 	data_offset = tcp_hdr_ptr->doff << 2;
1675625f965SAjay Singh 	if (total_length == (ihl + data_offset)) {
1685625f965SAjay Singh 		u32 seq_no, ack_no;
1695625f965SAjay Singh 
1705625f965SAjay Singh 		seq_no = ntohl(tcp_hdr_ptr->seq);
1715625f965SAjay Singh 		ack_no = ntohl(tcp_hdr_ptr->ack_seq);
1725625f965SAjay Singh 		for (i = 0; i < f->tcp_session; i++) {
1735625f965SAjay Singh 			u32 j = f->ack_session_info[i].seq_num;
1745625f965SAjay Singh 
1755625f965SAjay Singh 			if (i < 2 * MAX_TCP_SESSION &&
1765625f965SAjay Singh 			    j == seq_no) {
1775625f965SAjay Singh 				update_tcp_session(vif, i, ack_no);
1785625f965SAjay Singh 				break;
1795625f965SAjay Singh 			}
1805625f965SAjay Singh 		}
1815625f965SAjay Singh 		if (i == f->tcp_session)
1825625f965SAjay Singh 			add_tcp_session(vif, 0, 0, seq_no);
1835625f965SAjay Singh 
1845625f965SAjay Singh 		add_tcp_pending_ack(vif, ack_no, i, tqe);
1855625f965SAjay Singh 	}
1865625f965SAjay Singh 
1875625f965SAjay Singh out:
1885625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
1895625f965SAjay Singh }
1905625f965SAjay Singh 
wilc_wlan_txq_filter_dup_tcp_ack(struct net_device * dev)1915625f965SAjay Singh static void wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
1925625f965SAjay Singh {
1935625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
1945625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
1955625f965SAjay Singh 	struct tcp_ack_filter *f = &vif->ack_filter;
1965625f965SAjay Singh 	u32 i = 0;
1975625f965SAjay Singh 	u32 dropped = 0;
1985625f965SAjay Singh 	unsigned long flags;
1995625f965SAjay Singh 
2005625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
2015625f965SAjay Singh 	for (i = f->pending_base;
2025625f965SAjay Singh 	     i < (f->pending_base + f->pending_acks_idx); i++) {
2035625f965SAjay Singh 		u32 index;
2045625f965SAjay Singh 		u32 bigger_ack_num;
2055625f965SAjay Singh 
2065625f965SAjay Singh 		if (i >= MAX_PENDING_ACKS)
2075625f965SAjay Singh 			break;
2085625f965SAjay Singh 
2095625f965SAjay Singh 		index = f->pending_acks[i].session_index;
2105625f965SAjay Singh 
2115625f965SAjay Singh 		if (index >= 2 * MAX_TCP_SESSION)
2125625f965SAjay Singh 			break;
2135625f965SAjay Singh 
2145625f965SAjay Singh 		bigger_ack_num = f->ack_session_info[index].bigger_ack_num;
2155625f965SAjay Singh 
2165625f965SAjay Singh 		if (f->pending_acks[i].ack_num < bigger_ack_num) {
2175625f965SAjay Singh 			struct txq_entry_t *tqe;
2185625f965SAjay Singh 
2195625f965SAjay Singh 			tqe = f->pending_acks[i].txqe;
2205625f965SAjay Singh 			if (tqe) {
221339754ffSAjay Singh 				wilc_wlan_txq_remove(wilc, tqe->q_num, tqe);
2225625f965SAjay Singh 				tqe->status = 1;
2235625f965SAjay Singh 				if (tqe->tx_complete_func)
2245625f965SAjay Singh 					tqe->tx_complete_func(tqe->priv,
2255625f965SAjay Singh 							      tqe->status);
2265625f965SAjay Singh 				kfree(tqe);
2275625f965SAjay Singh 				dropped++;
2285625f965SAjay Singh 			}
2295625f965SAjay Singh 		}
2305625f965SAjay Singh 	}
2315625f965SAjay Singh 	f->pending_acks_idx = 0;
2325625f965SAjay Singh 	f->tcp_session = 0;
2335625f965SAjay Singh 
2345625f965SAjay Singh 	if (f->pending_base == 0)
2355625f965SAjay Singh 		f->pending_base = MAX_TCP_SESSION;
2365625f965SAjay Singh 	else
2375625f965SAjay Singh 		f->pending_base = 0;
2385625f965SAjay Singh 
2395625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
2405625f965SAjay Singh 
2415625f965SAjay Singh 	while (dropped > 0) {
2425625f965SAjay Singh 		wait_for_completion_timeout(&wilc->txq_event,
2435625f965SAjay Singh 					    msecs_to_jiffies(1));
2445625f965SAjay Singh 		dropped--;
2455625f965SAjay Singh 	}
2465625f965SAjay Singh }
2475625f965SAjay Singh 
wilc_enable_tcp_ack_filter(struct wilc_vif * vif,bool value)2485625f965SAjay Singh void wilc_enable_tcp_ack_filter(struct wilc_vif *vif, bool value)
2495625f965SAjay Singh {
2505625f965SAjay Singh 	vif->ack_filter.enabled = value;
2515625f965SAjay Singh }
2525625f965SAjay Singh 
wilc_wlan_txq_add_cfg_pkt(struct wilc_vif * vif,u8 * buffer,u32 buffer_size)2535625f965SAjay Singh static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
2545625f965SAjay Singh 				     u32 buffer_size)
2555625f965SAjay Singh {
2565625f965SAjay Singh 	struct txq_entry_t *tqe;
2575625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
2585625f965SAjay Singh 
2595625f965SAjay Singh 	netdev_dbg(vif->ndev, "Adding config packet ...\n");
2605625f965SAjay Singh 	if (wilc->quit) {
2615625f965SAjay Singh 		netdev_dbg(vif->ndev, "Return due to clear function\n");
2625625f965SAjay Singh 		complete(&wilc->cfg_event);
2635625f965SAjay Singh 		return 0;
2645625f965SAjay Singh 	}
2655625f965SAjay Singh 
2665625f965SAjay Singh 	tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
2679c172f30SAjay Singh 	if (!tqe) {
2689c172f30SAjay Singh 		complete(&wilc->cfg_event);
2695625f965SAjay Singh 		return 0;
2709c172f30SAjay Singh 	}
2715625f965SAjay Singh 
2725625f965SAjay Singh 	tqe->type = WILC_CFG_PKT;
2735625f965SAjay Singh 	tqe->buffer = buffer;
2745625f965SAjay Singh 	tqe->buffer_size = buffer_size;
2755625f965SAjay Singh 	tqe->tx_complete_func = NULL;
2765625f965SAjay Singh 	tqe->priv = NULL;
277339754ffSAjay Singh 	tqe->q_num = AC_VO_Q;
2785625f965SAjay Singh 	tqe->ack_idx = NOT_TCP_ACK;
2795625f965SAjay Singh 	tqe->vif = vif;
2805625f965SAjay Singh 
281339754ffSAjay Singh 	wilc_wlan_txq_add_to_head(vif, AC_VO_Q, tqe);
282339754ffSAjay Singh 
283339754ffSAjay Singh 	return 1;
284339754ffSAjay Singh }
285339754ffSAjay Singh 
is_ac_q_limit(struct wilc * wl,u8 q_num)286339754ffSAjay Singh static bool is_ac_q_limit(struct wilc *wl, u8 q_num)
287339754ffSAjay Singh {
288339754ffSAjay Singh 	u8 factors[NQUEUES] = {1, 1, 1, 1};
289339754ffSAjay Singh 	u16 i;
290339754ffSAjay Singh 	unsigned long flags;
291339754ffSAjay Singh 	struct wilc_tx_queue_status *q = &wl->tx_q_limit;
292339754ffSAjay Singh 	u8 end_index;
293339754ffSAjay Singh 	u8 q_limit;
294339754ffSAjay Singh 	bool ret = false;
295339754ffSAjay Singh 
296339754ffSAjay Singh 	spin_lock_irqsave(&wl->txq_spinlock, flags);
297339754ffSAjay Singh 	if (!q->initialized) {
298339754ffSAjay Singh 		for (i = 0; i < AC_BUFFER_SIZE; i++)
299339754ffSAjay Singh 			q->buffer[i] = i % NQUEUES;
300339754ffSAjay Singh 
301339754ffSAjay Singh 		for (i = 0; i < NQUEUES; i++) {
302339754ffSAjay Singh 			q->cnt[i] = AC_BUFFER_SIZE * factors[i] / NQUEUES;
303339754ffSAjay Singh 			q->sum += q->cnt[i];
304339754ffSAjay Singh 		}
305339754ffSAjay Singh 		q->end_index = AC_BUFFER_SIZE - 1;
306339754ffSAjay Singh 		q->initialized = 1;
307339754ffSAjay Singh 	}
308339754ffSAjay Singh 
309339754ffSAjay Singh 	end_index = q->end_index;
310339754ffSAjay Singh 	q->cnt[q->buffer[end_index]] -= factors[q->buffer[end_index]];
311339754ffSAjay Singh 	q->cnt[q_num] += factors[q_num];
312339754ffSAjay Singh 	q->sum += (factors[q_num] - factors[q->buffer[end_index]]);
313339754ffSAjay Singh 
314339754ffSAjay Singh 	q->buffer[end_index] = q_num;
315339754ffSAjay Singh 	if (end_index > 0)
316339754ffSAjay Singh 		q->end_index--;
317339754ffSAjay Singh 	else
318339754ffSAjay Singh 		q->end_index = AC_BUFFER_SIZE - 1;
319339754ffSAjay Singh 
320339754ffSAjay Singh 	if (!q->sum)
321339754ffSAjay Singh 		q_limit = 1;
322339754ffSAjay Singh 	else
323339754ffSAjay Singh 		q_limit = (q->cnt[q_num] * FLOW_CONTROL_UPPER_THRESHOLD / q->sum) + 1;
324339754ffSAjay Singh 
325339754ffSAjay Singh 	if (wl->txq[q_num].count <= q_limit)
326339754ffSAjay Singh 		ret = true;
327339754ffSAjay Singh 
328339754ffSAjay Singh 	spin_unlock_irqrestore(&wl->txq_spinlock, flags);
329339754ffSAjay Singh 
330339754ffSAjay Singh 	return ret;
331339754ffSAjay Singh }
332339754ffSAjay Singh 
ac_classify(struct wilc * wilc,struct sk_buff * skb)333339754ffSAjay Singh static inline u8 ac_classify(struct wilc *wilc, struct sk_buff *skb)
334339754ffSAjay Singh {
335339754ffSAjay Singh 	u8 q_num = AC_BE_Q;
336339754ffSAjay Singh 	u8 dscp;
337339754ffSAjay Singh 
338339754ffSAjay Singh 	switch (skb->protocol) {
339339754ffSAjay Singh 	case htons(ETH_P_IP):
340339754ffSAjay Singh 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
341339754ffSAjay Singh 		break;
342339754ffSAjay Singh 	case htons(ETH_P_IPV6):
343339754ffSAjay Singh 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
344339754ffSAjay Singh 		break;
345339754ffSAjay Singh 	default:
346339754ffSAjay Singh 		return q_num;
347339754ffSAjay Singh 	}
348339754ffSAjay Singh 
349339754ffSAjay Singh 	switch (dscp) {
350339754ffSAjay Singh 	case 0x08:
351339754ffSAjay Singh 	case 0x20:
352339754ffSAjay Singh 	case 0x40:
353339754ffSAjay Singh 		q_num = AC_BK_Q;
354339754ffSAjay Singh 		break;
355339754ffSAjay Singh 	case 0x80:
356339754ffSAjay Singh 	case 0xA0:
357339754ffSAjay Singh 	case 0x28:
358339754ffSAjay Singh 		q_num = AC_VI_Q;
359339754ffSAjay Singh 		break;
360339754ffSAjay Singh 	case 0xC0:
361339754ffSAjay Singh 	case 0xD0:
362339754ffSAjay Singh 	case 0xE0:
363339754ffSAjay Singh 	case 0x88:
364339754ffSAjay Singh 	case 0xB8:
365339754ffSAjay Singh 		q_num = AC_VO_Q;
366339754ffSAjay Singh 		break;
367339754ffSAjay Singh 	}
368339754ffSAjay Singh 
369339754ffSAjay Singh 	return q_num;
370339754ffSAjay Singh }
371339754ffSAjay Singh 
ac_balance(struct wilc * wl,u8 * ratio)372339754ffSAjay Singh static inline int ac_balance(struct wilc *wl, u8 *ratio)
373339754ffSAjay Singh {
374339754ffSAjay Singh 	u8 i, max_count = 0;
375339754ffSAjay Singh 
376339754ffSAjay Singh 	if (!ratio)
377339754ffSAjay Singh 		return -EINVAL;
378339754ffSAjay Singh 
379339754ffSAjay Singh 	for (i = 0; i < NQUEUES; i++)
380339754ffSAjay Singh 		if (wl->txq[i].fw.count > max_count)
381339754ffSAjay Singh 			max_count = wl->txq[i].fw.count;
382339754ffSAjay Singh 
383339754ffSAjay Singh 	for (i = 0; i < NQUEUES; i++)
384339754ffSAjay Singh 		ratio[i] = max_count - wl->txq[i].fw.count;
385339754ffSAjay Singh 
386339754ffSAjay Singh 	return 0;
387339754ffSAjay Singh }
388339754ffSAjay Singh 
ac_update_fw_ac_pkt_info(struct wilc * wl,u32 reg)389339754ffSAjay Singh static inline void ac_update_fw_ac_pkt_info(struct wilc *wl, u32 reg)
390339754ffSAjay Singh {
391339754ffSAjay Singh 	wl->txq[AC_BK_Q].fw.count = FIELD_GET(BK_AC_COUNT_FIELD, reg);
392339754ffSAjay Singh 	wl->txq[AC_BE_Q].fw.count = FIELD_GET(BE_AC_COUNT_FIELD, reg);
393339754ffSAjay Singh 	wl->txq[AC_VI_Q].fw.count = FIELD_GET(VI_AC_COUNT_FIELD, reg);
394339754ffSAjay Singh 	wl->txq[AC_VO_Q].fw.count = FIELD_GET(VO_AC_COUNT_FIELD, reg);
395339754ffSAjay Singh 
396339754ffSAjay Singh 	wl->txq[AC_BK_Q].fw.acm = FIELD_GET(BK_AC_ACM_STAT_FIELD, reg);
397339754ffSAjay Singh 	wl->txq[AC_BE_Q].fw.acm = FIELD_GET(BE_AC_ACM_STAT_FIELD, reg);
398339754ffSAjay Singh 	wl->txq[AC_VI_Q].fw.acm = FIELD_GET(VI_AC_ACM_STAT_FIELD, reg);
399339754ffSAjay Singh 	wl->txq[AC_VO_Q].fw.acm = FIELD_GET(VO_AC_ACM_STAT_FIELD, reg);
400339754ffSAjay Singh }
401339754ffSAjay Singh 
ac_change(struct wilc * wilc,u8 * ac)402339754ffSAjay Singh static inline u8 ac_change(struct wilc *wilc, u8 *ac)
403339754ffSAjay Singh {
404339754ffSAjay Singh 	do {
405339754ffSAjay Singh 		if (wilc->txq[*ac].fw.acm == 0)
406339754ffSAjay Singh 			return 0;
407339754ffSAjay Singh 		(*ac)++;
408339754ffSAjay Singh 	} while (*ac < NQUEUES);
4095625f965SAjay Singh 
4105625f965SAjay Singh 	return 1;
4115625f965SAjay Singh }
4125625f965SAjay Singh 
wilc_wlan_txq_add_net_pkt(struct net_device * dev,struct tx_complete_data * tx_data,u8 * buffer,u32 buffer_size,void (* tx_complete_fn)(void *,int))4136fe91b69SVsevolod Kozlov int wilc_wlan_txq_add_net_pkt(struct net_device *dev,
4146fe91b69SVsevolod Kozlov 			      struct tx_complete_data *tx_data, u8 *buffer,
4155625f965SAjay Singh 			      u32 buffer_size,
4165625f965SAjay Singh 			      void (*tx_complete_fn)(void *, int))
4175625f965SAjay Singh {
4185625f965SAjay Singh 	struct txq_entry_t *tqe;
4195625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
4205625f965SAjay Singh 	struct wilc *wilc;
421339754ffSAjay Singh 	u8 q_num;
4225625f965SAjay Singh 
4235625f965SAjay Singh 	wilc = vif->wilc;
4245625f965SAjay Singh 
425dd460e2aSAjay Singh 	if (wilc->quit) {
4266fe91b69SVsevolod Kozlov 		tx_complete_fn(tx_data, 0);
4275625f965SAjay Singh 		return 0;
428dd460e2aSAjay Singh 	}
4295625f965SAjay Singh 
430cd50248dSAjay Singh 	if (!wilc->initialized) {
431cd50248dSAjay Singh 		tx_complete_fn(tx_data, 0);
432cd50248dSAjay Singh 		return 0;
433cd50248dSAjay Singh 	}
434cd50248dSAjay Singh 
4355625f965SAjay Singh 	tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
4365625f965SAjay Singh 
437dd460e2aSAjay Singh 	if (!tqe) {
4386fe91b69SVsevolod Kozlov 		tx_complete_fn(tx_data, 0);
4395625f965SAjay Singh 		return 0;
440dd460e2aSAjay Singh 	}
4415625f965SAjay Singh 	tqe->type = WILC_NET_PKT;
4425625f965SAjay Singh 	tqe->buffer = buffer;
4435625f965SAjay Singh 	tqe->buffer_size = buffer_size;
4445625f965SAjay Singh 	tqe->tx_complete_func = tx_complete_fn;
4456fe91b69SVsevolod Kozlov 	tqe->priv = tx_data;
4465625f965SAjay Singh 	tqe->vif = vif;
4475625f965SAjay Singh 
4486fe91b69SVsevolod Kozlov 	q_num = ac_classify(wilc, tx_data->skb);
449339754ffSAjay Singh 	tqe->q_num = q_num;
450339754ffSAjay Singh 	if (ac_change(wilc, &q_num)) {
4516fe91b69SVsevolod Kozlov 		tx_complete_fn(tx_data, 0);
452339754ffSAjay Singh 		kfree(tqe);
453339754ffSAjay Singh 		return 0;
454339754ffSAjay Singh 	}
455339754ffSAjay Singh 
456339754ffSAjay Singh 	if (is_ac_q_limit(wilc, q_num)) {
4575625f965SAjay Singh 		tqe->ack_idx = NOT_TCP_ACK;
4585625f965SAjay Singh 		if (vif->ack_filter.enabled)
4595625f965SAjay Singh 			tcp_process(dev, tqe);
460339754ffSAjay Singh 		wilc_wlan_txq_add_to_tail(dev, q_num, tqe);
461339754ffSAjay Singh 	} else {
4626fe91b69SVsevolod Kozlov 		tx_complete_fn(tx_data, 0);
463339754ffSAjay Singh 		kfree(tqe);
464339754ffSAjay Singh 	}
465339754ffSAjay Singh 
4665625f965SAjay Singh 	return wilc->txq_entries;
4675625f965SAjay Singh }
4685625f965SAjay Singh 
wilc_wlan_txq_add_mgmt_pkt(struct net_device * dev,void * priv,u8 * buffer,u32 buffer_size,void (* tx_complete_fn)(void *,int))4695625f965SAjay Singh int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
4705625f965SAjay Singh 			       u32 buffer_size,
4715625f965SAjay Singh 			       void (*tx_complete_fn)(void *, int))
4725625f965SAjay Singh {
4735625f965SAjay Singh 	struct txq_entry_t *tqe;
4745625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
4755625f965SAjay Singh 	struct wilc *wilc;
4765625f965SAjay Singh 
4775625f965SAjay Singh 	wilc = vif->wilc;
4785625f965SAjay Singh 
47966aea5d1SAjay Singh 	if (wilc->quit) {
48066aea5d1SAjay Singh 		tx_complete_fn(priv, 0);
4815625f965SAjay Singh 		return 0;
48266aea5d1SAjay Singh 	}
4835625f965SAjay Singh 
484cd50248dSAjay Singh 	if (!wilc->initialized) {
485cd50248dSAjay Singh 		tx_complete_fn(priv, 0);
486cd50248dSAjay Singh 		return 0;
487cd50248dSAjay Singh 	}
4885625f965SAjay Singh 	tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
4895625f965SAjay Singh 
49066aea5d1SAjay Singh 	if (!tqe) {
49166aea5d1SAjay Singh 		tx_complete_fn(priv, 0);
4925625f965SAjay Singh 		return 0;
49366aea5d1SAjay Singh 	}
4945625f965SAjay Singh 	tqe->type = WILC_MGMT_PKT;
4955625f965SAjay Singh 	tqe->buffer = buffer;
4965625f965SAjay Singh 	tqe->buffer_size = buffer_size;
4975625f965SAjay Singh 	tqe->tx_complete_func = tx_complete_fn;
4985625f965SAjay Singh 	tqe->priv = priv;
499339754ffSAjay Singh 	tqe->q_num = AC_BE_Q;
5005625f965SAjay Singh 	tqe->ack_idx = NOT_TCP_ACK;
5015625f965SAjay Singh 	tqe->vif = vif;
502339754ffSAjay Singh 	wilc_wlan_txq_add_to_tail(dev, AC_VO_Q, tqe);
5035625f965SAjay Singh 	return 1;
5045625f965SAjay Singh }
5055625f965SAjay Singh 
wilc_wlan_txq_get_first(struct wilc * wilc,u8 q_num)506339754ffSAjay Singh static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc, u8 q_num)
5075625f965SAjay Singh {
5085625f965SAjay Singh 	struct txq_entry_t *tqe = NULL;
5095625f965SAjay Singh 	unsigned long flags;
5105625f965SAjay Singh 
5115625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
5125625f965SAjay Singh 
513339754ffSAjay Singh 	if (!list_empty(&wilc->txq[q_num].txq_head.list))
514339754ffSAjay Singh 		tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
515339754ffSAjay Singh 				       struct txq_entry_t, list);
5165625f965SAjay Singh 
5175625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
5185625f965SAjay Singh 
5195625f965SAjay Singh 	return tqe;
5205625f965SAjay Singh }
5215625f965SAjay Singh 
wilc_wlan_txq_get_next(struct wilc * wilc,struct txq_entry_t * tqe,u8 q_num)5225625f965SAjay Singh static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
523339754ffSAjay Singh 						  struct txq_entry_t *tqe,
524339754ffSAjay Singh 						  u8 q_num)
5255625f965SAjay Singh {
5265625f965SAjay Singh 	unsigned long flags;
5275625f965SAjay Singh 
5285625f965SAjay Singh 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
5295625f965SAjay Singh 
530339754ffSAjay Singh 	if (!list_is_last(&tqe->list, &wilc->txq[q_num].txq_head.list))
5315625f965SAjay Singh 		tqe = list_next_entry(tqe, list);
5325625f965SAjay Singh 	else
5335625f965SAjay Singh 		tqe = NULL;
5345625f965SAjay Singh 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
5355625f965SAjay Singh 
5365625f965SAjay Singh 	return tqe;
5375625f965SAjay Singh }
5385625f965SAjay Singh 
wilc_wlan_rxq_add(struct wilc * wilc,struct rxq_entry_t * rqe)5395625f965SAjay Singh static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
5405625f965SAjay Singh {
5415625f965SAjay Singh 	if (wilc->quit)
5425625f965SAjay Singh 		return;
5435625f965SAjay Singh 
5445625f965SAjay Singh 	mutex_lock(&wilc->rxq_cs);
5455625f965SAjay Singh 	list_add_tail(&rqe->list, &wilc->rxq_head.list);
5465625f965SAjay Singh 	mutex_unlock(&wilc->rxq_cs);
5475625f965SAjay Singh }
5485625f965SAjay Singh 
wilc_wlan_rxq_remove(struct wilc * wilc)5495625f965SAjay Singh static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
5505625f965SAjay Singh {
5515625f965SAjay Singh 	struct rxq_entry_t *rqe = NULL;
5525625f965SAjay Singh 
5535625f965SAjay Singh 	mutex_lock(&wilc->rxq_cs);
5545625f965SAjay Singh 	if (!list_empty(&wilc->rxq_head.list)) {
5555625f965SAjay Singh 		rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
5565625f965SAjay Singh 				       list);
5575625f965SAjay Singh 		list_del(&rqe->list);
5585625f965SAjay Singh 	}
5595625f965SAjay Singh 	mutex_unlock(&wilc->rxq_cs);
5605625f965SAjay Singh 	return rqe;
5615625f965SAjay Singh }
5625625f965SAjay Singh 
chip_allow_sleep(struct wilc * wilc)5635625f965SAjay Singh void chip_allow_sleep(struct wilc *wilc)
5645625f965SAjay Singh {
5655625f965SAjay Singh 	u32 reg = 0;
566f135a157SDavid Mosberger-Tang 	const struct wilc_hif_func *hif_func = wilc->hif_func;
567f135a157SDavid Mosberger-Tang 	u32 wakeup_reg, wakeup_bit;
568f135a157SDavid Mosberger-Tang 	u32 to_host_from_fw_reg, to_host_from_fw_bit;
569f135a157SDavid Mosberger-Tang 	u32 from_host_to_fw_reg, from_host_to_fw_bit;
570f135a157SDavid Mosberger-Tang 	u32 trials = 100;
571f135a157SDavid Mosberger-Tang 	int ret;
5725625f965SAjay Singh 
573f135a157SDavid Mosberger-Tang 	if (wilc->io_type == WILC_HIF_SDIO) {
574f135a157SDavid Mosberger-Tang 		wakeup_reg = WILC_SDIO_WAKEUP_REG;
575f135a157SDavid Mosberger-Tang 		wakeup_bit = WILC_SDIO_WAKEUP_BIT;
576f135a157SDavid Mosberger-Tang 		from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
577f135a157SDavid Mosberger-Tang 		from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
578f135a157SDavid Mosberger-Tang 		to_host_from_fw_reg = WILC_SDIO_FW_TO_HOST_REG;
579f135a157SDavid Mosberger-Tang 		to_host_from_fw_bit = WILC_SDIO_FW_TO_HOST_BIT;
580f135a157SDavid Mosberger-Tang 	} else {
581f135a157SDavid Mosberger-Tang 		wakeup_reg = WILC_SPI_WAKEUP_REG;
582f135a157SDavid Mosberger-Tang 		wakeup_bit = WILC_SPI_WAKEUP_BIT;
583f135a157SDavid Mosberger-Tang 		from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
584f135a157SDavid Mosberger-Tang 		from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
585f135a157SDavid Mosberger-Tang 		to_host_from_fw_reg = WILC_SPI_FW_TO_HOST_REG;
586f135a157SDavid Mosberger-Tang 		to_host_from_fw_bit = WILC_SPI_FW_TO_HOST_BIT;
587f135a157SDavid Mosberger-Tang 	}
5885625f965SAjay Singh 
5892f51061eSDan Carpenter 	while (--trials) {
590f135a157SDavid Mosberger-Tang 		ret = hif_func->hif_read_reg(wilc, to_host_from_fw_reg, &reg);
591f135a157SDavid Mosberger-Tang 		if (ret)
592f135a157SDavid Mosberger-Tang 			return;
593f135a157SDavid Mosberger-Tang 		if ((reg & to_host_from_fw_bit) == 0)
594f135a157SDavid Mosberger-Tang 			break;
595f135a157SDavid Mosberger-Tang 	}
596f135a157SDavid Mosberger-Tang 	if (!trials)
597f135a157SDavid Mosberger-Tang 		pr_warn("FW not responding\n");
598f135a157SDavid Mosberger-Tang 
599f135a157SDavid Mosberger-Tang 	/* Clear bit 1 */
600f135a157SDavid Mosberger-Tang 	ret = hif_func->hif_read_reg(wilc, wakeup_reg, &reg);
601f135a157SDavid Mosberger-Tang 	if (ret)
602f135a157SDavid Mosberger-Tang 		return;
603f135a157SDavid Mosberger-Tang 	if (reg & wakeup_bit) {
604f135a157SDavid Mosberger-Tang 		reg &= ~wakeup_bit;
605f135a157SDavid Mosberger-Tang 		ret = hif_func->hif_write_reg(wilc, wakeup_reg, reg);
606f135a157SDavid Mosberger-Tang 		if (ret)
607f135a157SDavid Mosberger-Tang 			return;
608f135a157SDavid Mosberger-Tang 	}
609f135a157SDavid Mosberger-Tang 
610f135a157SDavid Mosberger-Tang 	ret = hif_func->hif_read_reg(wilc, from_host_to_fw_reg, &reg);
611f135a157SDavid Mosberger-Tang 	if (ret)
612f135a157SDavid Mosberger-Tang 		return;
613f135a157SDavid Mosberger-Tang 	if (reg & from_host_to_fw_bit) {
614f135a157SDavid Mosberger-Tang 		reg &= ~from_host_to_fw_bit;
615f135a157SDavid Mosberger-Tang 		ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg, reg);
616f135a157SDavid Mosberger-Tang 		if (ret)
617f135a157SDavid Mosberger-Tang 			return;
618f135a157SDavid Mosberger-Tang 
619f135a157SDavid Mosberger-Tang 	}
6205625f965SAjay Singh }
6215625f965SAjay Singh EXPORT_SYMBOL_GPL(chip_allow_sleep);
6225625f965SAjay Singh 
chip_wakeup(struct wilc * wilc)6235625f965SAjay Singh void chip_wakeup(struct wilc *wilc)
6245625f965SAjay Singh {
6255bb9de8bSAjay Singh 	u32 ret = 0;
6265bb9de8bSAjay Singh 	u32 clk_status_val = 0, trials = 0;
6275bb9de8bSAjay Singh 	u32 wakeup_reg, wakeup_bit;
6285bb9de8bSAjay Singh 	u32 clk_status_reg, clk_status_bit;
6295bb9de8bSAjay Singh 	u32 from_host_to_fw_reg, from_host_to_fw_bit;
6305bb9de8bSAjay Singh 	const struct wilc_hif_func *hif_func = wilc->hif_func;
6315625f965SAjay Singh 
6325bb9de8bSAjay Singh 	if (wilc->io_type == WILC_HIF_SDIO) {
6335bb9de8bSAjay Singh 		wakeup_reg = WILC_SDIO_WAKEUP_REG;
6345bb9de8bSAjay Singh 		wakeup_bit = WILC_SDIO_WAKEUP_BIT;
6355bb9de8bSAjay Singh 		clk_status_reg = WILC_SDIO_CLK_STATUS_REG;
6365bb9de8bSAjay Singh 		clk_status_bit = WILC_SDIO_CLK_STATUS_BIT;
6375bb9de8bSAjay Singh 		from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
6385bb9de8bSAjay Singh 		from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
6395bb9de8bSAjay Singh 	} else {
6405bb9de8bSAjay Singh 		wakeup_reg = WILC_SPI_WAKEUP_REG;
6415bb9de8bSAjay Singh 		wakeup_bit = WILC_SPI_WAKEUP_BIT;
6425bb9de8bSAjay Singh 		clk_status_reg = WILC_SPI_CLK_STATUS_REG;
6435bb9de8bSAjay Singh 		clk_status_bit = WILC_SPI_CLK_STATUS_BIT;
6445bb9de8bSAjay Singh 		from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
6455bb9de8bSAjay Singh 		from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
6465625f965SAjay Singh 	}
6475625f965SAjay Singh 
6485bb9de8bSAjay Singh 	/* indicate host wakeup */
6495bb9de8bSAjay Singh 	ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg,
6505bb9de8bSAjay Singh 				      from_host_to_fw_bit);
6515bb9de8bSAjay Singh 	if (ret)
6525bb9de8bSAjay Singh 		return;
6535625f965SAjay Singh 
6545bb9de8bSAjay Singh 	/* Set wake-up bit */
6555bb9de8bSAjay Singh 	ret = hif_func->hif_write_reg(wilc, wakeup_reg,
6565bb9de8bSAjay Singh 				      wakeup_bit);
6575bb9de8bSAjay Singh 	if (ret)
6585bb9de8bSAjay Singh 		return;
6595625f965SAjay Singh 
6605bb9de8bSAjay Singh 	while (trials < WAKE_UP_TRIAL_RETRY) {
6615bb9de8bSAjay Singh 		ret = hif_func->hif_read_reg(wilc, clk_status_reg,
6625bb9de8bSAjay Singh 					     &clk_status_val);
6635bb9de8bSAjay Singh 		if (ret) {
6645bb9de8bSAjay Singh 			pr_err("Bus error %d %x\n", ret, clk_status_val);
6655bb9de8bSAjay Singh 			return;
6665625f965SAjay Singh 		}
6675bb9de8bSAjay Singh 		if (clk_status_val & clk_status_bit)
6685bb9de8bSAjay Singh 			break;
6695bb9de8bSAjay Singh 
6705bb9de8bSAjay Singh 		trials++;
6715625f965SAjay Singh 	}
6725bb9de8bSAjay Singh 	if (trials >= WAKE_UP_TRIAL_RETRY) {
6735bb9de8bSAjay Singh 		pr_err("Failed to wake-up the chip\n");
6745bb9de8bSAjay Singh 		return;
6755bb9de8bSAjay Singh 	}
676c2dcb476SAjay Singh 	/* Sometimes spi fail to read clock regs after reading
677c2dcb476SAjay Singh 	 * writing clockless registers
678c2dcb476SAjay Singh 	 */
679c2dcb476SAjay Singh 	if (wilc->io_type == WILC_HIF_SPI)
680c2dcb476SAjay Singh 		wilc->hif_func->hif_reset(wilc);
6815625f965SAjay Singh }
6825625f965SAjay Singh EXPORT_SYMBOL_GPL(chip_wakeup);
6835625f965SAjay Singh 
host_wakeup_notify(struct wilc * wilc)6845625f965SAjay Singh void host_wakeup_notify(struct wilc *wilc)
6855625f965SAjay Singh {
6865625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
6875625f965SAjay Singh 	wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_2, 1);
6885625f965SAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
6895625f965SAjay Singh }
6905625f965SAjay Singh EXPORT_SYMBOL_GPL(host_wakeup_notify);
6915625f965SAjay Singh 
host_sleep_notify(struct wilc * wilc)6925625f965SAjay Singh void host_sleep_notify(struct wilc *wilc)
6935625f965SAjay Singh {
6945625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
6955625f965SAjay Singh 	wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_1, 1);
6965625f965SAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
6975625f965SAjay Singh }
6985625f965SAjay Singh EXPORT_SYMBOL_GPL(host_sleep_notify);
6995625f965SAjay Singh 
wilc_wlan_handle_txq(struct wilc * wilc,u32 * txq_count)7005625f965SAjay Singh int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
7015625f965SAjay Singh {
7025625f965SAjay Singh 	int i, entries = 0;
703339754ffSAjay Singh 	u8 k, ac;
7045625f965SAjay Singh 	u32 sum;
7055625f965SAjay Singh 	u32 reg;
706339754ffSAjay Singh 	u8 ac_desired_ratio[NQUEUES] = {0, 0, 0, 0};
707339754ffSAjay Singh 	u8 ac_preserve_ratio[NQUEUES] = {1, 1, 1, 1};
708339754ffSAjay Singh 	u8 *num_pkts_to_add;
709339754ffSAjay Singh 	u8 vmm_entries_ac[WILC_VMM_TBL_SIZE];
7105625f965SAjay Singh 	u32 offset = 0;
711339754ffSAjay Singh 	bool max_size_over = 0, ac_exist = 0;
7125625f965SAjay Singh 	int vmm_sz = 0;
713339754ffSAjay Singh 	struct txq_entry_t *tqe_q[NQUEUES];
7145625f965SAjay Singh 	int ret = 0;
7155625f965SAjay Singh 	int counter;
7165625f965SAjay Singh 	int timeout;
71740b717bfSAjay.Kathat@microchip.com 	u32 *vmm_table = wilc->vmm_table;
718339754ffSAjay Singh 	u8 ac_pkt_num_to_chip[NQUEUES] = {0, 0, 0, 0};
7195625f965SAjay Singh 	const struct wilc_hif_func *func;
720339754ffSAjay Singh 	int srcu_idx;
7215625f965SAjay Singh 	u8 *txb = wilc->tx_buffer;
7225625f965SAjay Singh 	struct wilc_vif *vif;
7235625f965SAjay Singh 
7245625f965SAjay Singh 	if (wilc->quit)
7255625f965SAjay Singh 		goto out_update_cnt;
7265625f965SAjay Singh 
727339754ffSAjay Singh 	if (ac_balance(wilc, ac_desired_ratio))
728339754ffSAjay Singh 		return -EINVAL;
729339754ffSAjay Singh 
7305625f965SAjay Singh 	mutex_lock(&wilc->txq_add_to_head_cs);
731339754ffSAjay Singh 
732339754ffSAjay Singh 	srcu_idx = srcu_read_lock(&wilc->srcu);
733339754ffSAjay Singh 	list_for_each_entry_rcu(vif, &wilc->vif_list, list)
734339754ffSAjay Singh 		wilc_wlan_txq_filter_dup_tcp_ack(vif->ndev);
735339754ffSAjay Singh 	srcu_read_unlock(&wilc->srcu, srcu_idx);
736339754ffSAjay Singh 
737339754ffSAjay Singh 	for (ac = 0; ac < NQUEUES; ac++)
738339754ffSAjay Singh 		tqe_q[ac] = wilc_wlan_txq_get_first(wilc, ac);
739339754ffSAjay Singh 
7405625f965SAjay Singh 	i = 0;
7415625f965SAjay Singh 	sum = 0;
742339754ffSAjay Singh 	max_size_over = 0;
743339754ffSAjay Singh 	num_pkts_to_add = ac_desired_ratio;
744339754ffSAjay Singh 	do {
745339754ffSAjay Singh 		ac_exist = 0;
746339754ffSAjay Singh 		for (ac = 0; (ac < NQUEUES) && (!max_size_over); ac++) {
747339754ffSAjay Singh 			if (!tqe_q[ac])
748339754ffSAjay Singh 				continue;
749339754ffSAjay Singh 
750339754ffSAjay Singh 			ac_exist = 1;
751339754ffSAjay Singh 			for (k = 0; (k < num_pkts_to_add[ac]) &&
752339754ffSAjay Singh 			     (!max_size_over) && tqe_q[ac]; k++) {
753339754ffSAjay Singh 				if (i >= (WILC_VMM_TBL_SIZE - 1)) {
754339754ffSAjay Singh 					max_size_over = 1;
755339754ffSAjay Singh 					break;
756339754ffSAjay Singh 				}
757339754ffSAjay Singh 
758339754ffSAjay Singh 				if (tqe_q[ac]->type == WILC_CFG_PKT)
7595625f965SAjay Singh 					vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
760339754ffSAjay Singh 				else if (tqe_q[ac]->type == WILC_NET_PKT)
7615625f965SAjay Singh 					vmm_sz = ETH_ETHERNET_HDR_OFFSET;
7625625f965SAjay Singh 				else
7635625f965SAjay Singh 					vmm_sz = HOST_HDR_OFFSET;
7645625f965SAjay Singh 
765339754ffSAjay Singh 				vmm_sz += tqe_q[ac]->buffer_size;
7665625f965SAjay Singh 				vmm_sz = ALIGN(vmm_sz, 4);
7675625f965SAjay Singh 
768339754ffSAjay Singh 				if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE) {
769339754ffSAjay Singh 					max_size_over = 1;
7705625f965SAjay Singh 					break;
771339754ffSAjay Singh 				}
7725625f965SAjay Singh 				vmm_table[i] = vmm_sz / 4;
773339754ffSAjay Singh 				if (tqe_q[ac]->type == WILC_CFG_PKT)
7745625f965SAjay Singh 					vmm_table[i] |= BIT(10);
775339754ffSAjay Singh 
7765625f965SAjay Singh 				cpu_to_le32s(&vmm_table[i]);
777339754ffSAjay Singh 				vmm_entries_ac[i] = ac;
7785625f965SAjay Singh 
7795625f965SAjay Singh 				i++;
7805625f965SAjay Singh 				sum += vmm_sz;
781339754ffSAjay Singh 				tqe_q[ac] = wilc_wlan_txq_get_next(wilc,
782339754ffSAjay Singh 								   tqe_q[ac],
783339754ffSAjay Singh 								   ac);
7845625f965SAjay Singh 			}
785339754ffSAjay Singh 		}
786339754ffSAjay Singh 		num_pkts_to_add = ac_preserve_ratio;
787339754ffSAjay Singh 	} while (!max_size_over && ac_exist);
7885625f965SAjay Singh 
7895625f965SAjay Singh 	if (i == 0)
7905625f965SAjay Singh 		goto out_unlock;
7915625f965SAjay Singh 	vmm_table[i] = 0x0;
7925625f965SAjay Singh 
7935625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
7945625f965SAjay Singh 	counter = 0;
7955625f965SAjay Singh 	func = wilc->hif_func;
7965625f965SAjay Singh 	do {
7975625f965SAjay Singh 		ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
7985625f965SAjay Singh 		if (ret)
7995625f965SAjay Singh 			break;
8005625f965SAjay Singh 
801339754ffSAjay Singh 		if ((reg & 0x1) == 0) {
802339754ffSAjay Singh 			ac_update_fw_ac_pkt_info(wilc, reg);
8035625f965SAjay Singh 			break;
804339754ffSAjay Singh 		}
8055625f965SAjay Singh 
8065625f965SAjay Singh 		counter++;
8075625f965SAjay Singh 		if (counter > 200) {
8085625f965SAjay Singh 			counter = 0;
8095625f965SAjay Singh 			ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
8105625f965SAjay Singh 			break;
8115625f965SAjay Singh 		}
8125625f965SAjay Singh 	} while (!wilc->quit);
8135625f965SAjay Singh 
8145625f965SAjay Singh 	if (ret)
8155625f965SAjay Singh 		goto out_release_bus;
8165625f965SAjay Singh 
8175625f965SAjay Singh 	timeout = 200;
8185625f965SAjay Singh 	do {
8195625f965SAjay Singh 		ret = func->hif_block_tx(wilc,
8205625f965SAjay Singh 					 WILC_VMM_TBL_RX_SHADOW_BASE,
8215625f965SAjay Singh 					 (u8 *)vmm_table,
8225625f965SAjay Singh 					 ((i + 1) * 4));
8235625f965SAjay Singh 		if (ret)
8245625f965SAjay Singh 			break;
8255625f965SAjay Singh 
8265625f965SAjay Singh 		ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
8275625f965SAjay Singh 		if (ret)
8285625f965SAjay Singh 			break;
8295625f965SAjay Singh 
8305625f965SAjay Singh 		do {
8315625f965SAjay Singh 			ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, &reg);
8325625f965SAjay Singh 			if (ret)
8335625f965SAjay Singh 				break;
8345625f965SAjay Singh 			if (FIELD_GET(WILC_VMM_ENTRY_AVAILABLE, reg)) {
8355625f965SAjay Singh 				entries = FIELD_GET(WILC_VMM_ENTRY_COUNT, reg);
8365625f965SAjay Singh 				break;
8375625f965SAjay Singh 			}
8385625f965SAjay Singh 		} while (--timeout);
8395625f965SAjay Singh 		if (timeout <= 0) {
8405625f965SAjay Singh 			ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
8415625f965SAjay Singh 			break;
8425625f965SAjay Singh 		}
8435625f965SAjay Singh 
8445625f965SAjay Singh 		if (ret)
8455625f965SAjay Singh 			break;
8465625f965SAjay Singh 
8475625f965SAjay Singh 		if (entries == 0) {
8485625f965SAjay Singh 			ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
8495625f965SAjay Singh 			if (ret)
8505625f965SAjay Singh 				break;
8515625f965SAjay Singh 			reg &= ~BIT(0);
8525625f965SAjay Singh 			ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
8535625f965SAjay Singh 		}
8545625f965SAjay Singh 	} while (0);
8555625f965SAjay Singh 
8565625f965SAjay Singh 	if (ret)
8575625f965SAjay Singh 		goto out_release_bus;
8585625f965SAjay Singh 
8595625f965SAjay Singh 	if (entries == 0) {
8605625f965SAjay Singh 		/*
8615625f965SAjay Singh 		 * No VMM space available in firmware so retry to transmit
8625625f965SAjay Singh 		 * the packet from tx queue.
8635625f965SAjay Singh 		 */
8645625f965SAjay Singh 		ret = WILC_VMM_ENTRY_FULL_RETRY;
8655625f965SAjay Singh 		goto out_release_bus;
8665625f965SAjay Singh 	}
8675625f965SAjay Singh 
8685625f965SAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
8695625f965SAjay Singh 
8705625f965SAjay Singh 	offset = 0;
8715625f965SAjay Singh 	i = 0;
8725625f965SAjay Singh 	do {
873339754ffSAjay Singh 		struct txq_entry_t *tqe;
8745625f965SAjay Singh 		u32 header, buffer_offset;
8755625f965SAjay Singh 		char *bssid;
8765625f965SAjay Singh 		u8 mgmt_ptk = 0;
8775625f965SAjay Singh 
87862296b3eSAjay Singh 		if (vmm_table[i] == 0 || vmm_entries_ac[i] >= NQUEUES)
87962296b3eSAjay Singh 			break;
88062296b3eSAjay Singh 
881339754ffSAjay Singh 		tqe = wilc_wlan_txq_remove_from_head(wilc, vmm_entries_ac[i]);
8825625f965SAjay Singh 		if (!tqe)
8835625f965SAjay Singh 			break;
8845625f965SAjay Singh 
88562296b3eSAjay Singh 		ac_pkt_num_to_chip[vmm_entries_ac[i]]++;
8865625f965SAjay Singh 		vif = tqe->vif;
8875625f965SAjay Singh 
8885625f965SAjay Singh 		le32_to_cpus(&vmm_table[i]);
8895625f965SAjay Singh 		vmm_sz = FIELD_GET(WILC_VMM_BUFFER_SIZE, vmm_table[i]);
8905625f965SAjay Singh 		vmm_sz *= 4;
8915625f965SAjay Singh 
8925625f965SAjay Singh 		if (tqe->type == WILC_MGMT_PKT)
8935625f965SAjay Singh 			mgmt_ptk = 1;
8945625f965SAjay Singh 
8955625f965SAjay Singh 		header = (FIELD_PREP(WILC_VMM_HDR_TYPE, tqe->type) |
8965625f965SAjay Singh 			  FIELD_PREP(WILC_VMM_HDR_MGMT_FIELD, mgmt_ptk) |
8975625f965SAjay Singh 			  FIELD_PREP(WILC_VMM_HDR_PKT_SIZE, tqe->buffer_size) |
8985625f965SAjay Singh 			  FIELD_PREP(WILC_VMM_HDR_BUFF_SIZE, vmm_sz));
8995625f965SAjay Singh 
9005625f965SAjay Singh 		cpu_to_le32s(&header);
9015625f965SAjay Singh 		memcpy(&txb[offset], &header, 4);
9025625f965SAjay Singh 		if (tqe->type == WILC_CFG_PKT) {
9035625f965SAjay Singh 			buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
9045625f965SAjay Singh 		} else if (tqe->type == WILC_NET_PKT) {
905339754ffSAjay Singh 			int prio = tqe->q_num;
906339754ffSAjay Singh 
9075625f965SAjay Singh 			bssid = tqe->vif->bssid;
9085625f965SAjay Singh 			buffer_offset = ETH_ETHERNET_HDR_OFFSET;
909339754ffSAjay Singh 			memcpy(&txb[offset + 4], &prio, sizeof(prio));
9105625f965SAjay Singh 			memcpy(&txb[offset + 8], bssid, 6);
9115625f965SAjay Singh 		} else {
9125625f965SAjay Singh 			buffer_offset = HOST_HDR_OFFSET;
9135625f965SAjay Singh 		}
9145625f965SAjay Singh 
9155625f965SAjay Singh 		memcpy(&txb[offset + buffer_offset],
9165625f965SAjay Singh 		       tqe->buffer, tqe->buffer_size);
9175625f965SAjay Singh 		offset += vmm_sz;
9185625f965SAjay Singh 		i++;
9195625f965SAjay Singh 		tqe->status = 1;
9205625f965SAjay Singh 		if (tqe->tx_complete_func)
9215625f965SAjay Singh 			tqe->tx_complete_func(tqe->priv, tqe->status);
9225625f965SAjay Singh 		if (tqe->ack_idx != NOT_TCP_ACK &&
9235625f965SAjay Singh 		    tqe->ack_idx < MAX_PENDING_ACKS)
9245625f965SAjay Singh 			vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
9255625f965SAjay Singh 		kfree(tqe);
9265625f965SAjay Singh 	} while (--entries);
927339754ffSAjay Singh 	for (i = 0; i < NQUEUES; i++)
928339754ffSAjay Singh 		wilc->txq[i].fw.count += ac_pkt_num_to_chip[i];
9295625f965SAjay Singh 
9305625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
9315625f965SAjay Singh 
9325625f965SAjay Singh 	ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
9335625f965SAjay Singh 	if (ret)
9345625f965SAjay Singh 		goto out_release_bus;
9355625f965SAjay Singh 
9365625f965SAjay Singh 	ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
9375625f965SAjay Singh 
9385625f965SAjay Singh out_release_bus:
9395625f965SAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
9405625f965SAjay Singh 
9415625f965SAjay Singh out_unlock:
9425625f965SAjay Singh 	mutex_unlock(&wilc->txq_add_to_head_cs);
9435625f965SAjay Singh 
9445625f965SAjay Singh out_update_cnt:
9455625f965SAjay Singh 	*txq_count = wilc->txq_entries;
9465625f965SAjay Singh 	return ret;
9475625f965SAjay Singh }
9485625f965SAjay Singh 
wilc_wlan_handle_rx_buff(struct wilc * wilc,u8 * buffer,int size)9495625f965SAjay Singh static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
9505625f965SAjay Singh {
9515625f965SAjay Singh 	int offset = 0;
9525625f965SAjay Singh 	u32 header;
9535625f965SAjay Singh 	u32 pkt_len, pkt_offset, tp_len;
9545625f965SAjay Singh 	int is_cfg_packet;
9555625f965SAjay Singh 	u8 *buff_ptr;
9565625f965SAjay Singh 
9575625f965SAjay Singh 	do {
9585625f965SAjay Singh 		buff_ptr = buffer + offset;
9595625f965SAjay Singh 		header = get_unaligned_le32(buff_ptr);
9605625f965SAjay Singh 
9615625f965SAjay Singh 		is_cfg_packet = FIELD_GET(WILC_PKT_HDR_CONFIG_FIELD, header);
9625625f965SAjay Singh 		pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
9635625f965SAjay Singh 		tp_len = FIELD_GET(WILC_PKT_HDR_TOTAL_LEN_FIELD, header);
9645625f965SAjay Singh 		pkt_len = FIELD_GET(WILC_PKT_HDR_LEN_FIELD, header);
9655625f965SAjay Singh 
9665625f965SAjay Singh 		if (pkt_len == 0 || tp_len == 0)
9675625f965SAjay Singh 			break;
9685625f965SAjay Singh 
9695625f965SAjay Singh 		if (pkt_offset & IS_MANAGMEMENT) {
9705625f965SAjay Singh 			buff_ptr += HOST_HDR_OFFSET;
971c5b331d4SAjay Singh 			wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len,
972c5b331d4SAjay Singh 					 pkt_offset & IS_MGMT_AUTH_PKT);
9735625f965SAjay Singh 		} else {
9745625f965SAjay Singh 			if (!is_cfg_packet) {
9755625f965SAjay Singh 				wilc_frmw_to_host(wilc, buff_ptr, pkt_len,
9765625f965SAjay Singh 						  pkt_offset);
9775625f965SAjay Singh 			} else {
9785625f965SAjay Singh 				struct wilc_cfg_rsp rsp;
9795625f965SAjay Singh 
9805625f965SAjay Singh 				buff_ptr += pkt_offset;
9815625f965SAjay Singh 
9825625f965SAjay Singh 				wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
9835625f965SAjay Singh 							  pkt_len,
9845625f965SAjay Singh 							  &rsp);
9855625f965SAjay Singh 				if (rsp.type == WILC_CFG_RSP) {
9865625f965SAjay Singh 					if (wilc->cfg_seq_no == rsp.seq_no)
9875625f965SAjay Singh 						complete(&wilc->cfg_event);
9885625f965SAjay Singh 				} else if (rsp.type == WILC_CFG_RSP_STATUS) {
9895625f965SAjay Singh 					wilc_mac_indicate(wilc);
9905625f965SAjay Singh 				}
9915625f965SAjay Singh 			}
9925625f965SAjay Singh 		}
9935625f965SAjay Singh 		offset += tp_len;
9945625f965SAjay Singh 	} while (offset < size);
9955625f965SAjay Singh }
9965625f965SAjay Singh 
wilc_wlan_handle_rxq(struct wilc * wilc)9975625f965SAjay Singh static void wilc_wlan_handle_rxq(struct wilc *wilc)
9985625f965SAjay Singh {
9995625f965SAjay Singh 	int size;
10005625f965SAjay Singh 	u8 *buffer;
10015625f965SAjay Singh 	struct rxq_entry_t *rqe;
10025625f965SAjay Singh 
10035625f965SAjay Singh 	while (!wilc->quit) {
10045625f965SAjay Singh 		rqe = wilc_wlan_rxq_remove(wilc);
10055625f965SAjay Singh 		if (!rqe)
10065625f965SAjay Singh 			break;
10075625f965SAjay Singh 
10085625f965SAjay Singh 		buffer = rqe->buffer;
10095625f965SAjay Singh 		size = rqe->buffer_size;
10105625f965SAjay Singh 		wilc_wlan_handle_rx_buff(wilc, buffer, size);
10115625f965SAjay Singh 
10125625f965SAjay Singh 		kfree(rqe);
10135625f965SAjay Singh 	}
10145625f965SAjay Singh 	if (wilc->quit)
10155625f965SAjay Singh 		complete(&wilc->cfg_event);
10165625f965SAjay Singh }
10175625f965SAjay Singh 
wilc_unknown_isr_ext(struct wilc * wilc)10185625f965SAjay Singh static void wilc_unknown_isr_ext(struct wilc *wilc)
10195625f965SAjay Singh {
10205625f965SAjay Singh 	wilc->hif_func->hif_clear_int_ext(wilc, 0);
10215625f965SAjay Singh }
10225625f965SAjay Singh 
wilc_wlan_handle_isr_ext(struct wilc * wilc,u32 int_status)10235625f965SAjay Singh static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
10245625f965SAjay Singh {
10255625f965SAjay Singh 	u32 offset = wilc->rx_buffer_offset;
10265625f965SAjay Singh 	u8 *buffer = NULL;
10275625f965SAjay Singh 	u32 size;
10285625f965SAjay Singh 	u32 retries = 0;
10295625f965SAjay Singh 	int ret = 0;
10305625f965SAjay Singh 	struct rxq_entry_t *rqe;
10315625f965SAjay Singh 
10325625f965SAjay Singh 	size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, int_status) << 2;
10335625f965SAjay Singh 
10345625f965SAjay Singh 	while (!size && retries < 10) {
10355625f965SAjay Singh 		wilc->hif_func->hif_read_size(wilc, &size);
10365625f965SAjay Singh 		size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, size) << 2;
10375625f965SAjay Singh 		retries++;
10385625f965SAjay Singh 	}
10395625f965SAjay Singh 
10405625f965SAjay Singh 	if (size <= 0)
10415625f965SAjay Singh 		return;
10425625f965SAjay Singh 
10435625f965SAjay Singh 	if (WILC_RX_BUFF_SIZE - offset < size)
10445625f965SAjay Singh 		offset = 0;
10455625f965SAjay Singh 
10465625f965SAjay Singh 	buffer = &wilc->rx_buffer[offset];
10475625f965SAjay Singh 
10485625f965SAjay Singh 	wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
10495625f965SAjay Singh 	ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
10505625f965SAjay Singh 	if (ret)
10515625f965SAjay Singh 		return;
10525625f965SAjay Singh 
10535625f965SAjay Singh 	offset += size;
10545625f965SAjay Singh 	wilc->rx_buffer_offset = offset;
10555625f965SAjay Singh 	rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
10565625f965SAjay Singh 	if (!rqe)
10575625f965SAjay Singh 		return;
10585625f965SAjay Singh 
10595625f965SAjay Singh 	rqe->buffer = buffer;
10605625f965SAjay Singh 	rqe->buffer_size = size;
10615625f965SAjay Singh 	wilc_wlan_rxq_add(wilc, rqe);
10625625f965SAjay Singh 	wilc_wlan_handle_rxq(wilc);
10635625f965SAjay Singh }
10645625f965SAjay Singh 
wilc_handle_isr(struct wilc * wilc)10655625f965SAjay Singh void wilc_handle_isr(struct wilc *wilc)
10665625f965SAjay Singh {
10675625f965SAjay Singh 	u32 int_status;
10685625f965SAjay Singh 
10695625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
10705625f965SAjay Singh 	wilc->hif_func->hif_read_int(wilc, &int_status);
10715625f965SAjay Singh 
10725625f965SAjay Singh 	if (int_status & DATA_INT_EXT)
10735625f965SAjay Singh 		wilc_wlan_handle_isr_ext(wilc, int_status);
10745625f965SAjay Singh 
10755625f965SAjay Singh 	if (!(int_status & (ALL_INT_EXT)))
10765625f965SAjay Singh 		wilc_unknown_isr_ext(wilc);
10775625f965SAjay Singh 
10785625f965SAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
10795625f965SAjay Singh }
10805625f965SAjay Singh EXPORT_SYMBOL_GPL(wilc_handle_isr);
10815625f965SAjay Singh 
wilc_wlan_firmware_download(struct wilc * wilc,const u8 * buffer,u32 buffer_size)10825625f965SAjay Singh int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
10835625f965SAjay Singh 				u32 buffer_size)
10845625f965SAjay Singh {
10855625f965SAjay Singh 	u32 offset;
10865625f965SAjay Singh 	u32 addr, size, size2, blksz;
10875625f965SAjay Singh 	u8 *dma_buffer;
10885625f965SAjay Singh 	int ret = 0;
108929f7393eSAjay Singh 	u32 reg = 0;
10905625f965SAjay Singh 
10915625f965SAjay Singh 	blksz = BIT(12);
10925625f965SAjay Singh 
10935625f965SAjay Singh 	dma_buffer = kmalloc(blksz, GFP_KERNEL);
10945625f965SAjay Singh 	if (!dma_buffer)
10955625f965SAjay Singh 		return -EIO;
10965625f965SAjay Singh 
10975625f965SAjay Singh 	offset = 0;
109829f7393eSAjay Singh 	pr_debug("%s: Downloading firmware size = %d\n", __func__, buffer_size);
109929f7393eSAjay Singh 
110029f7393eSAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
110129f7393eSAjay Singh 
110229f7393eSAjay Singh 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
110329f7393eSAjay Singh 	reg &= ~BIT(10);
110429f7393eSAjay Singh 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
110529f7393eSAjay Singh 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
110629f7393eSAjay Singh 	if (reg & BIT(10))
110729f7393eSAjay Singh 		pr_err("%s: Failed to reset\n", __func__);
110829f7393eSAjay Singh 
110929f7393eSAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
11105625f965SAjay Singh 	do {
11115625f965SAjay Singh 		addr = get_unaligned_le32(&buffer[offset]);
11125625f965SAjay Singh 		size = get_unaligned_le32(&buffer[offset + 4]);
111329f7393eSAjay Singh 		acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
11145625f965SAjay Singh 		offset += 8;
11155625f965SAjay Singh 		while (((int)size) && (offset < buffer_size)) {
11165625f965SAjay Singh 			if (size <= blksz)
11175625f965SAjay Singh 				size2 = size;
11185625f965SAjay Singh 			else
11195625f965SAjay Singh 				size2 = blksz;
11205625f965SAjay Singh 
11215625f965SAjay Singh 			memcpy(dma_buffer, &buffer[offset], size2);
11225625f965SAjay Singh 			ret = wilc->hif_func->hif_block_tx(wilc, addr,
11235625f965SAjay Singh 							   dma_buffer, size2);
11245625f965SAjay Singh 			if (ret)
11255625f965SAjay Singh 				break;
11265625f965SAjay Singh 
11275625f965SAjay Singh 			addr += size2;
11285625f965SAjay Singh 			offset += size2;
11295625f965SAjay Singh 			size -= size2;
11305625f965SAjay Singh 		}
113129f7393eSAjay Singh 		release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
11325625f965SAjay Singh 
113329f7393eSAjay Singh 		if (ret) {
113429f7393eSAjay Singh 			pr_err("%s Bus error\n", __func__);
11355625f965SAjay Singh 			goto fail;
113629f7393eSAjay Singh 		}
113729f7393eSAjay Singh 		pr_debug("%s Offset = %d\n", __func__, offset);
11385625f965SAjay Singh 	} while (offset < buffer_size);
11395625f965SAjay Singh 
11405625f965SAjay Singh fail:
11415625f965SAjay Singh 
11425625f965SAjay Singh 	kfree(dma_buffer);
11435625f965SAjay Singh 
11445625f965SAjay Singh 	return ret;
11455625f965SAjay Singh }
11465625f965SAjay Singh 
wilc_wlan_start(struct wilc * wilc)11475625f965SAjay Singh int wilc_wlan_start(struct wilc *wilc)
11485625f965SAjay Singh {
11495625f965SAjay Singh 	u32 reg = 0;
11505625f965SAjay Singh 	int ret;
11515625f965SAjay Singh 	u32 chipid;
11525625f965SAjay Singh 
11535625f965SAjay Singh 	if (wilc->io_type == WILC_HIF_SDIO) {
11545625f965SAjay Singh 		reg = 0;
11555625f965SAjay Singh 		reg |= BIT(3);
11565625f965SAjay Singh 	} else if (wilc->io_type == WILC_HIF_SPI) {
11575625f965SAjay Singh 		reg = 1;
11585625f965SAjay Singh 	}
11595625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
11605625f965SAjay Singh 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
11618f863428Swengjianfeng 	if (ret)
11628f863428Swengjianfeng 		goto release;
11638f863428Swengjianfeng 
11645625f965SAjay Singh 	reg = 0;
11655625f965SAjay Singh 	if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
11665625f965SAjay Singh 		reg |= WILC_HAVE_SDIO_IRQ_GPIO;
11675625f965SAjay Singh 
11685625f965SAjay Singh 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
11698f863428Swengjianfeng 	if (ret)
11708f863428Swengjianfeng 		goto release;
11715625f965SAjay Singh 
11725625f965SAjay Singh 	wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
11735625f965SAjay Singh 
11745625f965SAjay Singh 	ret = wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
11758f863428Swengjianfeng 	if (ret)
11768f863428Swengjianfeng 		goto release;
11775625f965SAjay Singh 
11785625f965SAjay Singh 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
11795625f965SAjay Singh 	if ((reg & BIT(10)) == BIT(10)) {
11805625f965SAjay Singh 		reg &= ~BIT(10);
11815625f965SAjay Singh 		wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
11825625f965SAjay Singh 		wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
11835625f965SAjay Singh 	}
11845625f965SAjay Singh 
11855625f965SAjay Singh 	reg |= BIT(10);
11865625f965SAjay Singh 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
11875625f965SAjay Singh 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
11885625f965SAjay Singh 
11898f863428Swengjianfeng release:
11908f863428Swengjianfeng 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
11915625f965SAjay Singh 	return ret;
11925625f965SAjay Singh }
11935625f965SAjay Singh 
wilc_wlan_stop(struct wilc * wilc,struct wilc_vif * vif)11945625f965SAjay Singh int wilc_wlan_stop(struct wilc *wilc, struct wilc_vif *vif)
11955625f965SAjay Singh {
11965625f965SAjay Singh 	u32 reg = 0;
11975625f965SAjay Singh 	int ret;
11985625f965SAjay Singh 
11995625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
12005625f965SAjay Singh 
12015625f965SAjay Singh 	ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, &reg);
12025625f965SAjay Singh 	if (ret) {
12035625f965SAjay Singh 		netdev_err(vif->ndev, "Error while reading reg\n");
12048f863428Swengjianfeng 		goto release;
12055625f965SAjay Singh 	}
12065625f965SAjay Singh 
12075625f965SAjay Singh 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
12085625f965SAjay Singh 					(reg | WILC_ABORT_REQ_BIT));
12095625f965SAjay Singh 	if (ret) {
12105625f965SAjay Singh 		netdev_err(vif->ndev, "Error while writing reg\n");
12118f863428Swengjianfeng 		goto release;
12125625f965SAjay Singh 	}
12135625f965SAjay Singh 
12145625f965SAjay Singh 	ret = wilc->hif_func->hif_read_reg(wilc, WILC_FW_HOST_COMM, &reg);
12155625f965SAjay Singh 	if (ret) {
12165625f965SAjay Singh 		netdev_err(vif->ndev, "Error while reading reg\n");
12178f863428Swengjianfeng 		goto release;
12185625f965SAjay Singh 	}
12195625f965SAjay Singh 	reg = BIT(0);
12205625f965SAjay Singh 
12215625f965SAjay Singh 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_FW_HOST_COMM, reg);
12225625f965SAjay Singh 	if (ret) {
12235625f965SAjay Singh 		netdev_err(vif->ndev, "Error while writing reg\n");
12248f863428Swengjianfeng 		goto release;
12255625f965SAjay Singh 	}
12265625f965SAjay Singh 
12278f863428Swengjianfeng 	ret = 0;
12288f863428Swengjianfeng release:
122973bbef64SDavid Mosberger-Tang 	/* host comm is disabled - we can't issue sleep command anymore: */
123073bbef64SDavid Mosberger-Tang 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
12315625f965SAjay Singh 
12328f863428Swengjianfeng 	return ret;
12335625f965SAjay Singh }
12345625f965SAjay Singh 
wilc_wlan_cleanup(struct net_device * dev)12355625f965SAjay Singh void wilc_wlan_cleanup(struct net_device *dev)
12365625f965SAjay Singh {
12375625f965SAjay Singh 	struct txq_entry_t *tqe;
12385625f965SAjay Singh 	struct rxq_entry_t *rqe;
1239339754ffSAjay Singh 	u8 ac;
12405625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
12415625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
12425625f965SAjay Singh 
12435625f965SAjay Singh 	wilc->quit = 1;
1244339754ffSAjay Singh 	for (ac = 0; ac < NQUEUES; ac++) {
1245339754ffSAjay Singh 		while ((tqe = wilc_wlan_txq_remove_from_head(wilc, ac))) {
12465625f965SAjay Singh 			if (tqe->tx_complete_func)
12475625f965SAjay Singh 				tqe->tx_complete_func(tqe->priv, 0);
12485625f965SAjay Singh 			kfree(tqe);
12495625f965SAjay Singh 		}
1250339754ffSAjay Singh 	}
12515625f965SAjay Singh 
12525625f965SAjay Singh 	while ((rqe = wilc_wlan_rxq_remove(wilc)))
12535625f965SAjay Singh 		kfree(rqe);
12545625f965SAjay Singh 
125540b717bfSAjay.Kathat@microchip.com 	kfree(wilc->vmm_table);
125640b717bfSAjay.Kathat@microchip.com 	wilc->vmm_table = NULL;
12575625f965SAjay Singh 	kfree(wilc->rx_buffer);
12585625f965SAjay Singh 	wilc->rx_buffer = NULL;
12595625f965SAjay Singh 	kfree(wilc->tx_buffer);
12605625f965SAjay Singh 	wilc->tx_buffer = NULL;
1261ec031ac4SDavid Mosberger-Tang 	wilc->hif_func->hif_deinit(wilc);
12625625f965SAjay Singh }
12635625f965SAjay Singh 
wilc_wlan_cfg_commit(struct wilc_vif * vif,int type,u32 drv_handler)12645625f965SAjay Singh static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
12655625f965SAjay Singh 				u32 drv_handler)
12665625f965SAjay Singh {
12675625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
12685625f965SAjay Singh 	struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
12695625f965SAjay Singh 	int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
12705625f965SAjay Singh 
12715625f965SAjay Singh 	if (type == WILC_CFG_SET)
12725625f965SAjay Singh 		cfg->hdr.cmd_type = 'W';
12735625f965SAjay Singh 	else
12745625f965SAjay Singh 		cfg->hdr.cmd_type = 'Q';
12755625f965SAjay Singh 
12765625f965SAjay Singh 	cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
12775625f965SAjay Singh 	cfg->hdr.total_len = cpu_to_le16(t_len);
12785625f965SAjay Singh 	cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
12795625f965SAjay Singh 	wilc->cfg_seq_no = cfg->hdr.seq_no;
12805625f965SAjay Singh 
12815625f965SAjay Singh 	if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
12825625f965SAjay Singh 		return -1;
12835625f965SAjay Singh 
12845625f965SAjay Singh 	return 0;
12855625f965SAjay Singh }
12865625f965SAjay Singh 
wilc_wlan_cfg_set(struct wilc_vif * vif,int start,u16 wid,u8 * buffer,u32 buffer_size,int commit,u32 drv_handler)12875625f965SAjay Singh int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
12885625f965SAjay Singh 		      u32 buffer_size, int commit, u32 drv_handler)
12895625f965SAjay Singh {
12905625f965SAjay Singh 	u32 offset;
12915625f965SAjay Singh 	int ret_size;
12925625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
12935625f965SAjay Singh 
12945625f965SAjay Singh 	mutex_lock(&wilc->cfg_cmd_lock);
12955625f965SAjay Singh 
12965625f965SAjay Singh 	if (start)
12975625f965SAjay Singh 		wilc->cfg_frame_offset = 0;
12985625f965SAjay Singh 
12995625f965SAjay Singh 	offset = wilc->cfg_frame_offset;
13005625f965SAjay Singh 	ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
13015625f965SAjay Singh 					 wid, buffer, buffer_size);
13025625f965SAjay Singh 	offset += ret_size;
13035625f965SAjay Singh 	wilc->cfg_frame_offset = offset;
13045625f965SAjay Singh 
13055625f965SAjay Singh 	if (!commit) {
13065625f965SAjay Singh 		mutex_unlock(&wilc->cfg_cmd_lock);
13075625f965SAjay Singh 		return ret_size;
13085625f965SAjay Singh 	}
13095625f965SAjay Singh 
13105625f965SAjay Singh 	netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
13115625f965SAjay Singh 
13125625f965SAjay Singh 	if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
13135625f965SAjay Singh 		ret_size = 0;
13145625f965SAjay Singh 
13155625f965SAjay Singh 	if (!wait_for_completion_timeout(&wilc->cfg_event,
13165625f965SAjay Singh 					 WILC_CFG_PKTS_TIMEOUT)) {
13175625f965SAjay Singh 		netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
13185625f965SAjay Singh 		ret_size = 0;
13195625f965SAjay Singh 	}
13205625f965SAjay Singh 
13215625f965SAjay Singh 	wilc->cfg_frame_offset = 0;
13225625f965SAjay Singh 	wilc->cfg_seq_no += 1;
13235625f965SAjay Singh 	mutex_unlock(&wilc->cfg_cmd_lock);
13245625f965SAjay Singh 
13255625f965SAjay Singh 	return ret_size;
13265625f965SAjay Singh }
13275625f965SAjay Singh 
wilc_wlan_cfg_get(struct wilc_vif * vif,int start,u16 wid,int commit,u32 drv_handler)13285625f965SAjay Singh int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
13295625f965SAjay Singh 		      u32 drv_handler)
13305625f965SAjay Singh {
13315625f965SAjay Singh 	u32 offset;
13325625f965SAjay Singh 	int ret_size;
13335625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
13345625f965SAjay Singh 
13355625f965SAjay Singh 	mutex_lock(&wilc->cfg_cmd_lock);
13365625f965SAjay Singh 
13375625f965SAjay Singh 	if (start)
13385625f965SAjay Singh 		wilc->cfg_frame_offset = 0;
13395625f965SAjay Singh 
13405625f965SAjay Singh 	offset = wilc->cfg_frame_offset;
13415625f965SAjay Singh 	ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
13425625f965SAjay Singh 	offset += ret_size;
13435625f965SAjay Singh 	wilc->cfg_frame_offset = offset;
13445625f965SAjay Singh 
13455625f965SAjay Singh 	if (!commit) {
13465625f965SAjay Singh 		mutex_unlock(&wilc->cfg_cmd_lock);
13475625f965SAjay Singh 		return ret_size;
13485625f965SAjay Singh 	}
13495625f965SAjay Singh 
13505625f965SAjay Singh 	if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
13515625f965SAjay Singh 		ret_size = 0;
13525625f965SAjay Singh 
13535625f965SAjay Singh 	if (!wait_for_completion_timeout(&wilc->cfg_event,
13545625f965SAjay Singh 					 WILC_CFG_PKTS_TIMEOUT)) {
13555625f965SAjay Singh 		netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
13565625f965SAjay Singh 		ret_size = 0;
13575625f965SAjay Singh 	}
13585625f965SAjay Singh 	wilc->cfg_frame_offset = 0;
13595625f965SAjay Singh 	wilc->cfg_seq_no += 1;
13605625f965SAjay Singh 	mutex_unlock(&wilc->cfg_cmd_lock);
13615625f965SAjay Singh 
13625625f965SAjay Singh 	return ret_size;
13635625f965SAjay Singh }
13645625f965SAjay Singh 
wilc_send_config_pkt(struct wilc_vif * vif,u8 mode,struct wid * wids,u32 count)13655625f965SAjay Singh int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
13665625f965SAjay Singh 			 u32 count)
13675625f965SAjay Singh {
13685625f965SAjay Singh 	int i;
13695625f965SAjay Singh 	int ret = 0;
13705625f965SAjay Singh 	u32 drv = wilc_get_vif_idx(vif);
13715625f965SAjay Singh 
13725625f965SAjay Singh 	if (mode == WILC_GET_CFG) {
13735625f965SAjay Singh 		for (i = 0; i < count; i++) {
13745625f965SAjay Singh 			if (!wilc_wlan_cfg_get(vif, !i,
13755625f965SAjay Singh 					       wids[i].id,
13765625f965SAjay Singh 					       (i == count - 1),
13775625f965SAjay Singh 					       drv)) {
13785625f965SAjay Singh 				ret = -ETIMEDOUT;
13795625f965SAjay Singh 				break;
13805625f965SAjay Singh 			}
13815625f965SAjay Singh 		}
13825625f965SAjay Singh 		for (i = 0; i < count; i++) {
13835625f965SAjay Singh 			wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
13845625f965SAjay Singh 							     wids[i].id,
13855625f965SAjay Singh 							     wids[i].val,
13865625f965SAjay Singh 							     wids[i].size);
13875625f965SAjay Singh 		}
13885625f965SAjay Singh 	} else if (mode == WILC_SET_CFG) {
13895625f965SAjay Singh 		for (i = 0; i < count; i++) {
13905625f965SAjay Singh 			if (!wilc_wlan_cfg_set(vif, !i,
13915625f965SAjay Singh 					       wids[i].id,
13925625f965SAjay Singh 					       wids[i].val,
13935625f965SAjay Singh 					       wids[i].size,
13945625f965SAjay Singh 					       (i == count - 1),
13955625f965SAjay Singh 					       drv)) {
13965625f965SAjay Singh 				ret = -ETIMEDOUT;
13975625f965SAjay Singh 				break;
13985625f965SAjay Singh 			}
13995625f965SAjay Singh 		}
14005625f965SAjay Singh 	}
14015625f965SAjay Singh 
14025625f965SAjay Singh 	return ret;
14035625f965SAjay Singh }
14045625f965SAjay Singh 
init_chip(struct net_device * dev)14055625f965SAjay Singh static int init_chip(struct net_device *dev)
14065625f965SAjay Singh {
14075625f965SAjay Singh 	u32 chipid;
14085625f965SAjay Singh 	u32 reg;
14095625f965SAjay Singh 	int ret = 0;
14105625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
14115625f965SAjay Singh 	struct wilc *wilc = vif->wilc;
14125625f965SAjay Singh 
14135625f965SAjay Singh 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
14145625f965SAjay Singh 
14155625f965SAjay Singh 	chipid = wilc_get_chipid(wilc, true);
14165625f965SAjay Singh 
14175625f965SAjay Singh 	if ((chipid & 0xfff) != 0xa0) {
14185625f965SAjay Singh 		ret = wilc->hif_func->hif_read_reg(wilc,
14195625f965SAjay Singh 						   WILC_CORTUS_RESET_MUX_SEL,
14205625f965SAjay Singh 						   &reg);
14215625f965SAjay Singh 		if (ret) {
14225625f965SAjay Singh 			netdev_err(dev, "fail read reg 0x1118\n");
14235625f965SAjay Singh 			goto release;
14245625f965SAjay Singh 		}
14255625f965SAjay Singh 		reg |= BIT(0);
14265625f965SAjay Singh 		ret = wilc->hif_func->hif_write_reg(wilc,
14275625f965SAjay Singh 						    WILC_CORTUS_RESET_MUX_SEL,
14285625f965SAjay Singh 						    reg);
14295625f965SAjay Singh 		if (ret) {
14305625f965SAjay Singh 			netdev_err(dev, "fail write reg 0x1118\n");
14315625f965SAjay Singh 			goto release;
14325625f965SAjay Singh 		}
14335625f965SAjay Singh 		ret = wilc->hif_func->hif_write_reg(wilc,
14345625f965SAjay Singh 						    WILC_CORTUS_BOOT_REGISTER,
14355625f965SAjay Singh 						    WILC_CORTUS_BOOT_FROM_IRAM);
14365625f965SAjay Singh 		if (ret) {
14375625f965SAjay Singh 			netdev_err(dev, "fail write reg 0xc0000\n");
14385625f965SAjay Singh 			goto release;
14395625f965SAjay Singh 		}
14405625f965SAjay Singh 	}
14415625f965SAjay Singh 
14425625f965SAjay Singh release:
14435625f965SAjay Singh 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
14445625f965SAjay Singh 
14455625f965SAjay Singh 	return ret;
14465625f965SAjay Singh }
14475625f965SAjay Singh 
wilc_get_chipid(struct wilc * wilc,bool update)14485625f965SAjay Singh u32 wilc_get_chipid(struct wilc *wilc, bool update)
14495625f965SAjay Singh {
14504d2cd7b0SDavid Mosberger-Tang 	u32 chipid = 0;
14515625f965SAjay Singh 	u32 rfrevid = 0;
14525625f965SAjay Singh 
14534d2cd7b0SDavid Mosberger-Tang 	if (wilc->chipid == 0 || update) {
14544d2cd7b0SDavid Mosberger-Tang 		wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
14555625f965SAjay Singh 		wilc->hif_func->hif_read_reg(wilc, WILC_RF_REVISION_ID,
14565625f965SAjay Singh 					     &rfrevid);
14574d2cd7b0SDavid Mosberger-Tang 		if (!is_wilc1000(chipid)) {
14584d2cd7b0SDavid Mosberger-Tang 			wilc->chipid = 0;
14594d2cd7b0SDavid Mosberger-Tang 			return wilc->chipid;
14605625f965SAjay Singh 		}
14614d2cd7b0SDavid Mosberger-Tang 		if (chipid == WILC_1000_BASE_ID_2A) { /* 0x1002A0 */
14625625f965SAjay Singh 			if (rfrevid != 0x1)
14634d2cd7b0SDavid Mosberger-Tang 				chipid = WILC_1000_BASE_ID_2A_REV1;
14644d2cd7b0SDavid Mosberger-Tang 		} else if (chipid == WILC_1000_BASE_ID_2B) { /* 0x1002B0 */
14655625f965SAjay Singh 			if (rfrevid == 0x4)
14664d2cd7b0SDavid Mosberger-Tang 				chipid = WILC_1000_BASE_ID_2B_REV1;
14675625f965SAjay Singh 			else if (rfrevid != 0x3)
14684d2cd7b0SDavid Mosberger-Tang 				chipid = WILC_1000_BASE_ID_2B_REV2;
14695625f965SAjay Singh 		}
14705625f965SAjay Singh 
14714d2cd7b0SDavid Mosberger-Tang 		wilc->chipid = chipid;
14725625f965SAjay Singh 	}
14734d2cd7b0SDavid Mosberger-Tang 	return wilc->chipid;
14745625f965SAjay Singh }
14755625f965SAjay Singh 
wilc_wlan_init(struct net_device * dev)14765625f965SAjay Singh int wilc_wlan_init(struct net_device *dev)
14775625f965SAjay Singh {
14785625f965SAjay Singh 	int ret = 0;
14795625f965SAjay Singh 	struct wilc_vif *vif = netdev_priv(dev);
14805625f965SAjay Singh 	struct wilc *wilc;
14815625f965SAjay Singh 
14825625f965SAjay Singh 	wilc = vif->wilc;
14835625f965SAjay Singh 
14845625f965SAjay Singh 	wilc->quit = 0;
14855625f965SAjay Singh 
148639d0f1b0SAjay Singh 	if (!wilc->hif_func->hif_is_init(wilc)) {
148739d0f1b0SAjay Singh 		acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
148839d0f1b0SAjay Singh 		ret = wilc->hif_func->hif_init(wilc, false);
148939d0f1b0SAjay Singh 		release_bus(wilc, WILC_BUS_RELEASE_ONLY);
149039d0f1b0SAjay Singh 		if (ret)
14915625f965SAjay Singh 			goto fail;
14925625f965SAjay Singh 	}
14935625f965SAjay Singh 
149440b717bfSAjay.Kathat@microchip.com 	if (!wilc->vmm_table)
1495*3ce1c2c3SAjay Singh 		wilc->vmm_table = kcalloc(WILC_VMM_TBL_SIZE, sizeof(u32), GFP_KERNEL);
149640b717bfSAjay.Kathat@microchip.com 
149740b717bfSAjay.Kathat@microchip.com 	if (!wilc->vmm_table) {
149840b717bfSAjay.Kathat@microchip.com 		ret = -ENOBUFS;
149940b717bfSAjay.Kathat@microchip.com 		goto fail;
150040b717bfSAjay.Kathat@microchip.com 	}
150140b717bfSAjay.Kathat@microchip.com 
15025625f965SAjay Singh 	if (!wilc->tx_buffer)
15035625f965SAjay Singh 		wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
15045625f965SAjay Singh 
15055625f965SAjay Singh 	if (!wilc->tx_buffer) {
15065625f965SAjay Singh 		ret = -ENOBUFS;
15075625f965SAjay Singh 		goto fail;
15085625f965SAjay Singh 	}
15095625f965SAjay Singh 
15105625f965SAjay Singh 	if (!wilc->rx_buffer)
15115625f965SAjay Singh 		wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
15125625f965SAjay Singh 
15135625f965SAjay Singh 	if (!wilc->rx_buffer) {
15145625f965SAjay Singh 		ret = -ENOBUFS;
15155625f965SAjay Singh 		goto fail;
15165625f965SAjay Singh 	}
15175625f965SAjay Singh 
15185625f965SAjay Singh 	if (init_chip(dev)) {
15195625f965SAjay Singh 		ret = -EIO;
15205625f965SAjay Singh 		goto fail;
15215625f965SAjay Singh 	}
15225625f965SAjay Singh 
15235625f965SAjay Singh 	return 0;
15245625f965SAjay Singh 
15255625f965SAjay Singh fail:
152640b717bfSAjay.Kathat@microchip.com 	kfree(wilc->vmm_table);
152740b717bfSAjay.Kathat@microchip.com 	wilc->vmm_table = NULL;
15285625f965SAjay Singh 	kfree(wilc->rx_buffer);
15295625f965SAjay Singh 	wilc->rx_buffer = NULL;
15305625f965SAjay Singh 	kfree(wilc->tx_buffer);
15315625f965SAjay Singh 	wilc->tx_buffer = NULL;
15325625f965SAjay Singh 
15335625f965SAjay Singh 	return ret;
15345625f965SAjay Singh }
1535