xref: /linux/drivers/net/wireless/microchip/wilc1000/wlan.c (revision 40ccd6aa3e2e05be93394e3cd560c718dedfcc77)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4  * All rights reserved.
5  */
6 
7 #include <linux/if_ether.h>
8 #include <linux/ip.h>
9 #include <net/dsfield.h>
10 #include "cfg80211.h"
11 #include "wlan_cfg.h"
12 
13 #define WAKE_UP_TRIAL_RETRY		10000
14 
15 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
16 {
17 	mutex_lock(&wilc->hif_cs);
18 	if (acquire == WILC_BUS_ACQUIRE_AND_WAKEUP && wilc->power_save_mode)
19 		chip_wakeup(wilc);
20 }
21 
22 static inline void release_bus(struct wilc *wilc, enum bus_release release)
23 {
24 	if (release == WILC_BUS_RELEASE_ALLOW_SLEEP && wilc->power_save_mode)
25 		chip_allow_sleep(wilc);
26 	mutex_unlock(&wilc->hif_cs);
27 }
28 
29 static void wilc_wlan_txq_remove(struct wilc *wilc, u8 q_num,
30 				 struct txq_entry_t *tqe)
31 {
32 	list_del(&tqe->list);
33 	wilc->txq_entries -= 1;
34 	wilc->txq[q_num].count--;
35 }
36 
37 static struct txq_entry_t *
38 wilc_wlan_txq_remove_from_head(struct wilc *wilc, u8 q_num)
39 {
40 	struct txq_entry_t *tqe = NULL;
41 	unsigned long flags;
42 
43 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
44 
45 	if (!list_empty(&wilc->txq[q_num].txq_head.list)) {
46 		tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
47 				       struct txq_entry_t, list);
48 		list_del(&tqe->list);
49 		wilc->txq_entries -= 1;
50 		wilc->txq[q_num].count--;
51 	}
52 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
53 	return tqe;
54 }
55 
56 static void wilc_wlan_txq_add_to_tail(struct net_device *dev, u8 q_num,
57 				      struct txq_entry_t *tqe)
58 {
59 	unsigned long flags;
60 	struct wilc_vif *vif = netdev_priv(dev);
61 	struct wilc *wilc = vif->wilc;
62 
63 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
64 
65 	list_add_tail(&tqe->list, &wilc->txq[q_num].txq_head.list);
66 	wilc->txq_entries += 1;
67 	wilc->txq[q_num].count++;
68 
69 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
70 
71 	complete(&wilc->txq_event);
72 }
73 
74 static void wilc_wlan_txq_add_to_head(struct wilc_vif *vif, u8 q_num,
75 				      struct txq_entry_t *tqe)
76 {
77 	unsigned long flags;
78 	struct wilc *wilc = vif->wilc;
79 
80 	mutex_lock(&wilc->txq_add_to_head_cs);
81 
82 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
83 
84 	list_add(&tqe->list, &wilc->txq[q_num].txq_head.list);
85 	wilc->txq_entries += 1;
86 	wilc->txq[q_num].count++;
87 
88 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
89 	mutex_unlock(&wilc->txq_add_to_head_cs);
90 	complete(&wilc->txq_event);
91 }
92 
93 #define NOT_TCP_ACK			(-1)
94 
95 static inline void add_tcp_session(struct wilc_vif *vif, u32 src_prt,
96 				   u32 dst_prt, u32 seq)
97 {
98 	struct tcp_ack_filter *f = &vif->ack_filter;
99 
100 	if (f->tcp_session < 2 * MAX_TCP_SESSION) {
101 		f->ack_session_info[f->tcp_session].seq_num = seq;
102 		f->ack_session_info[f->tcp_session].bigger_ack_num = 0;
103 		f->ack_session_info[f->tcp_session].src_port = src_prt;
104 		f->ack_session_info[f->tcp_session].dst_port = dst_prt;
105 		f->tcp_session++;
106 	}
107 }
108 
109 static inline void update_tcp_session(struct wilc_vif *vif, u32 index, u32 ack)
110 {
111 	struct tcp_ack_filter *f = &vif->ack_filter;
112 
113 	if (index < 2 * MAX_TCP_SESSION &&
114 	    ack > f->ack_session_info[index].bigger_ack_num)
115 		f->ack_session_info[index].bigger_ack_num = ack;
116 }
117 
118 static inline void add_tcp_pending_ack(struct wilc_vif *vif, u32 ack,
119 				       u32 session_index,
120 				       struct txq_entry_t *txqe)
121 {
122 	struct tcp_ack_filter *f = &vif->ack_filter;
123 	u32 i = f->pending_base + f->pending_acks_idx;
124 
125 	if (i < MAX_PENDING_ACKS) {
126 		f->pending_acks[i].ack_num = ack;
127 		f->pending_acks[i].txqe = txqe;
128 		f->pending_acks[i].session_index = session_index;
129 		txqe->ack_idx = i;
130 		f->pending_acks_idx++;
131 	}
132 }
133 
134 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
135 {
136 	void *buffer = tqe->buffer;
137 	const struct ethhdr *eth_hdr_ptr = buffer;
138 	int i;
139 	unsigned long flags;
140 	struct wilc_vif *vif = netdev_priv(dev);
141 	struct wilc *wilc = vif->wilc;
142 	struct tcp_ack_filter *f = &vif->ack_filter;
143 	const struct iphdr *ip_hdr_ptr;
144 	const struct tcphdr *tcp_hdr_ptr;
145 	u32 ihl, total_length, data_offset;
146 
147 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
148 
149 	if (eth_hdr_ptr->h_proto != htons(ETH_P_IP))
150 		goto out;
151 
152 	ip_hdr_ptr = buffer + ETH_HLEN;
153 
154 	if (ip_hdr_ptr->protocol != IPPROTO_TCP)
155 		goto out;
156 
157 	ihl = ip_hdr_ptr->ihl << 2;
158 	tcp_hdr_ptr = buffer + ETH_HLEN + ihl;
159 	total_length = ntohs(ip_hdr_ptr->tot_len);
160 
161 	data_offset = tcp_hdr_ptr->doff << 2;
162 	if (total_length == (ihl + data_offset)) {
163 		u32 seq_no, ack_no;
164 
165 		seq_no = ntohl(tcp_hdr_ptr->seq);
166 		ack_no = ntohl(tcp_hdr_ptr->ack_seq);
167 		for (i = 0; i < f->tcp_session; i++) {
168 			u32 j = f->ack_session_info[i].seq_num;
169 
170 			if (i < 2 * MAX_TCP_SESSION &&
171 			    j == seq_no) {
172 				update_tcp_session(vif, i, ack_no);
173 				break;
174 			}
175 		}
176 		if (i == f->tcp_session)
177 			add_tcp_session(vif, 0, 0, seq_no);
178 
179 		add_tcp_pending_ack(vif, ack_no, i, tqe);
180 	}
181 
182 out:
183 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
184 }
185 
186 static void wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
187 {
188 	struct wilc_vif *vif = netdev_priv(dev);
189 	struct wilc *wilc = vif->wilc;
190 	struct tcp_ack_filter *f = &vif->ack_filter;
191 	u32 i = 0;
192 	u32 dropped = 0;
193 	unsigned long flags;
194 
195 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
196 	for (i = f->pending_base;
197 	     i < (f->pending_base + f->pending_acks_idx); i++) {
198 		u32 index;
199 		u32 bigger_ack_num;
200 
201 		if (i >= MAX_PENDING_ACKS)
202 			break;
203 
204 		index = f->pending_acks[i].session_index;
205 
206 		if (index >= 2 * MAX_TCP_SESSION)
207 			break;
208 
209 		bigger_ack_num = f->ack_session_info[index].bigger_ack_num;
210 
211 		if (f->pending_acks[i].ack_num < bigger_ack_num) {
212 			struct txq_entry_t *tqe;
213 
214 			tqe = f->pending_acks[i].txqe;
215 			if (tqe) {
216 				wilc_wlan_txq_remove(wilc, tqe->q_num, tqe);
217 				tqe->status = 1;
218 				if (tqe->tx_complete_func)
219 					tqe->tx_complete_func(tqe->priv,
220 							      tqe->status);
221 				kfree(tqe);
222 				dropped++;
223 			}
224 		}
225 	}
226 	f->pending_acks_idx = 0;
227 	f->tcp_session = 0;
228 
229 	if (f->pending_base == 0)
230 		f->pending_base = MAX_TCP_SESSION;
231 	else
232 		f->pending_base = 0;
233 
234 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
235 
236 	while (dropped > 0) {
237 		wait_for_completion_timeout(&wilc->txq_event,
238 					    msecs_to_jiffies(1));
239 		dropped--;
240 	}
241 }
242 
243 void wilc_enable_tcp_ack_filter(struct wilc_vif *vif, bool value)
244 {
245 	vif->ack_filter.enabled = value;
246 }
247 
248 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
249 				     u32 buffer_size)
250 {
251 	struct txq_entry_t *tqe;
252 	struct wilc *wilc = vif->wilc;
253 
254 	netdev_dbg(vif->ndev, "Adding config packet ...\n");
255 	if (wilc->quit) {
256 		netdev_dbg(vif->ndev, "Return due to clear function\n");
257 		complete(&wilc->cfg_event);
258 		return 0;
259 	}
260 
261 	tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
262 	if (!tqe) {
263 		complete(&wilc->cfg_event);
264 		return 0;
265 	}
266 
267 	tqe->type = WILC_CFG_PKT;
268 	tqe->buffer = buffer;
269 	tqe->buffer_size = buffer_size;
270 	tqe->tx_complete_func = NULL;
271 	tqe->priv = NULL;
272 	tqe->q_num = AC_VO_Q;
273 	tqe->ack_idx = NOT_TCP_ACK;
274 	tqe->vif = vif;
275 
276 	wilc_wlan_txq_add_to_head(vif, AC_VO_Q, tqe);
277 
278 	return 1;
279 }
280 
281 static bool is_ac_q_limit(struct wilc *wl, u8 q_num)
282 {
283 	u8 factors[NQUEUES] = {1, 1, 1, 1};
284 	u16 i;
285 	unsigned long flags;
286 	struct wilc_tx_queue_status *q = &wl->tx_q_limit;
287 	u8 end_index;
288 	u8 q_limit;
289 	bool ret = false;
290 
291 	spin_lock_irqsave(&wl->txq_spinlock, flags);
292 	if (!q->initialized) {
293 		for (i = 0; i < AC_BUFFER_SIZE; i++)
294 			q->buffer[i] = i % NQUEUES;
295 
296 		for (i = 0; i < NQUEUES; i++) {
297 			q->cnt[i] = AC_BUFFER_SIZE * factors[i] / NQUEUES;
298 			q->sum += q->cnt[i];
299 		}
300 		q->end_index = AC_BUFFER_SIZE - 1;
301 		q->initialized = 1;
302 	}
303 
304 	end_index = q->end_index;
305 	q->cnt[q->buffer[end_index]] -= factors[q->buffer[end_index]];
306 	q->cnt[q_num] += factors[q_num];
307 	q->sum += (factors[q_num] - factors[q->buffer[end_index]]);
308 
309 	q->buffer[end_index] = q_num;
310 	if (end_index > 0)
311 		q->end_index--;
312 	else
313 		q->end_index = AC_BUFFER_SIZE - 1;
314 
315 	if (!q->sum)
316 		q_limit = 1;
317 	else
318 		q_limit = (q->cnt[q_num] * FLOW_CONTROL_UPPER_THRESHOLD / q->sum) + 1;
319 
320 	if (wl->txq[q_num].count <= q_limit)
321 		ret = true;
322 
323 	spin_unlock_irqrestore(&wl->txq_spinlock, flags);
324 
325 	return ret;
326 }
327 
328 static inline u8 ac_classify(struct wilc *wilc, struct sk_buff *skb)
329 {
330 	u8 q_num = AC_BE_Q;
331 	u8 dscp;
332 
333 	switch (skb->protocol) {
334 	case htons(ETH_P_IP):
335 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
336 		break;
337 	case htons(ETH_P_IPV6):
338 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
339 		break;
340 	default:
341 		return q_num;
342 	}
343 
344 	switch (dscp) {
345 	case 0x08:
346 	case 0x20:
347 	case 0x40:
348 		q_num = AC_BK_Q;
349 		break;
350 	case 0x80:
351 	case 0xA0:
352 	case 0x28:
353 		q_num = AC_VI_Q;
354 		break;
355 	case 0xC0:
356 	case 0xD0:
357 	case 0xE0:
358 	case 0x88:
359 	case 0xB8:
360 		q_num = AC_VO_Q;
361 		break;
362 	}
363 
364 	return q_num;
365 }
366 
367 static inline int ac_balance(struct wilc *wl, u8 *ratio)
368 {
369 	u8 i, max_count = 0;
370 
371 	if (!ratio)
372 		return -EINVAL;
373 
374 	for (i = 0; i < NQUEUES; i++)
375 		if (wl->txq[i].fw.count > max_count)
376 			max_count = wl->txq[i].fw.count;
377 
378 	for (i = 0; i < NQUEUES; i++)
379 		ratio[i] = max_count - wl->txq[i].fw.count;
380 
381 	return 0;
382 }
383 
384 static inline void ac_update_fw_ac_pkt_info(struct wilc *wl, u32 reg)
385 {
386 	wl->txq[AC_BK_Q].fw.count = FIELD_GET(BK_AC_COUNT_FIELD, reg);
387 	wl->txq[AC_BE_Q].fw.count = FIELD_GET(BE_AC_COUNT_FIELD, reg);
388 	wl->txq[AC_VI_Q].fw.count = FIELD_GET(VI_AC_COUNT_FIELD, reg);
389 	wl->txq[AC_VO_Q].fw.count = FIELD_GET(VO_AC_COUNT_FIELD, reg);
390 
391 	wl->txq[AC_BK_Q].fw.acm = FIELD_GET(BK_AC_ACM_STAT_FIELD, reg);
392 	wl->txq[AC_BE_Q].fw.acm = FIELD_GET(BE_AC_ACM_STAT_FIELD, reg);
393 	wl->txq[AC_VI_Q].fw.acm = FIELD_GET(VI_AC_ACM_STAT_FIELD, reg);
394 	wl->txq[AC_VO_Q].fw.acm = FIELD_GET(VO_AC_ACM_STAT_FIELD, reg);
395 }
396 
397 static inline u8 ac_change(struct wilc *wilc, u8 *ac)
398 {
399 	do {
400 		if (wilc->txq[*ac].fw.acm == 0)
401 			return 0;
402 		(*ac)++;
403 	} while (*ac < NQUEUES);
404 
405 	return 1;
406 }
407 
408 int wilc_wlan_txq_add_net_pkt(struct net_device *dev,
409 			      struct tx_complete_data *tx_data, u8 *buffer,
410 			      u32 buffer_size,
411 			      void (*tx_complete_fn)(void *, int))
412 {
413 	struct txq_entry_t *tqe;
414 	struct wilc_vif *vif = netdev_priv(dev);
415 	struct wilc *wilc;
416 	u8 q_num;
417 
418 	wilc = vif->wilc;
419 
420 	if (wilc->quit) {
421 		tx_complete_fn(tx_data, 0);
422 		return 0;
423 	}
424 
425 	if (!wilc->initialized) {
426 		tx_complete_fn(tx_data, 0);
427 		return 0;
428 	}
429 
430 	tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
431 
432 	if (!tqe) {
433 		tx_complete_fn(tx_data, 0);
434 		return 0;
435 	}
436 	tqe->type = WILC_NET_PKT;
437 	tqe->buffer = buffer;
438 	tqe->buffer_size = buffer_size;
439 	tqe->tx_complete_func = tx_complete_fn;
440 	tqe->priv = tx_data;
441 	tqe->vif = vif;
442 
443 	q_num = ac_classify(wilc, tx_data->skb);
444 	tqe->q_num = q_num;
445 	if (ac_change(wilc, &q_num)) {
446 		tx_complete_fn(tx_data, 0);
447 		kfree(tqe);
448 		return 0;
449 	}
450 
451 	if (is_ac_q_limit(wilc, q_num)) {
452 		tqe->ack_idx = NOT_TCP_ACK;
453 		if (vif->ack_filter.enabled)
454 			tcp_process(dev, tqe);
455 		wilc_wlan_txq_add_to_tail(dev, q_num, tqe);
456 	} else {
457 		tx_complete_fn(tx_data, 0);
458 		kfree(tqe);
459 	}
460 
461 	return wilc->txq_entries;
462 }
463 
464 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
465 			       u32 buffer_size,
466 			       void (*tx_complete_fn)(void *, int))
467 {
468 	struct txq_entry_t *tqe;
469 	struct wilc_vif *vif = netdev_priv(dev);
470 	struct wilc *wilc;
471 
472 	wilc = vif->wilc;
473 
474 	if (wilc->quit) {
475 		tx_complete_fn(priv, 0);
476 		return 0;
477 	}
478 
479 	if (!wilc->initialized) {
480 		tx_complete_fn(priv, 0);
481 		return 0;
482 	}
483 	tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
484 
485 	if (!tqe) {
486 		tx_complete_fn(priv, 0);
487 		return 0;
488 	}
489 	tqe->type = WILC_MGMT_PKT;
490 	tqe->buffer = buffer;
491 	tqe->buffer_size = buffer_size;
492 	tqe->tx_complete_func = tx_complete_fn;
493 	tqe->priv = priv;
494 	tqe->q_num = AC_BE_Q;
495 	tqe->ack_idx = NOT_TCP_ACK;
496 	tqe->vif = vif;
497 	wilc_wlan_txq_add_to_tail(dev, AC_VO_Q, tqe);
498 	return 1;
499 }
500 
501 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc, u8 q_num)
502 {
503 	struct txq_entry_t *tqe = NULL;
504 	unsigned long flags;
505 
506 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
507 
508 	if (!list_empty(&wilc->txq[q_num].txq_head.list))
509 		tqe = list_first_entry(&wilc->txq[q_num].txq_head.list,
510 				       struct txq_entry_t, list);
511 
512 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
513 
514 	return tqe;
515 }
516 
517 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
518 						  struct txq_entry_t *tqe,
519 						  u8 q_num)
520 {
521 	unsigned long flags;
522 
523 	spin_lock_irqsave(&wilc->txq_spinlock, flags);
524 
525 	if (!list_is_last(&tqe->list, &wilc->txq[q_num].txq_head.list))
526 		tqe = list_next_entry(tqe, list);
527 	else
528 		tqe = NULL;
529 	spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
530 
531 	return tqe;
532 }
533 
534 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
535 {
536 	if (wilc->quit)
537 		return;
538 
539 	mutex_lock(&wilc->rxq_cs);
540 	list_add_tail(&rqe->list, &wilc->rxq_head.list);
541 	mutex_unlock(&wilc->rxq_cs);
542 }
543 
544 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
545 {
546 	struct rxq_entry_t *rqe = NULL;
547 
548 	mutex_lock(&wilc->rxq_cs);
549 	if (!list_empty(&wilc->rxq_head.list)) {
550 		rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
551 				       list);
552 		list_del(&rqe->list);
553 	}
554 	mutex_unlock(&wilc->rxq_cs);
555 	return rqe;
556 }
557 
558 void chip_allow_sleep(struct wilc *wilc)
559 {
560 	u32 reg = 0;
561 	const struct wilc_hif_func *hif_func = wilc->hif_func;
562 	u32 wakeup_reg, wakeup_bit;
563 	u32 to_host_from_fw_reg, to_host_from_fw_bit;
564 	u32 from_host_to_fw_reg, from_host_to_fw_bit;
565 	u32 trials = 100;
566 	int ret;
567 
568 	if (wilc->io_type == WILC_HIF_SDIO) {
569 		wakeup_reg = WILC_SDIO_WAKEUP_REG;
570 		wakeup_bit = WILC_SDIO_WAKEUP_BIT;
571 		from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
572 		from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
573 		to_host_from_fw_reg = WILC_SDIO_FW_TO_HOST_REG;
574 		to_host_from_fw_bit = WILC_SDIO_FW_TO_HOST_BIT;
575 	} else {
576 		wakeup_reg = WILC_SPI_WAKEUP_REG;
577 		wakeup_bit = WILC_SPI_WAKEUP_BIT;
578 		from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
579 		from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
580 		to_host_from_fw_reg = WILC_SPI_FW_TO_HOST_REG;
581 		to_host_from_fw_bit = WILC_SPI_FW_TO_HOST_BIT;
582 	}
583 
584 	while (--trials) {
585 		ret = hif_func->hif_read_reg(wilc, to_host_from_fw_reg, &reg);
586 		if (ret)
587 			return;
588 		if ((reg & to_host_from_fw_bit) == 0)
589 			break;
590 	}
591 	if (!trials)
592 		pr_warn("FW not responding\n");
593 
594 	/* Clear bit 1 */
595 	ret = hif_func->hif_read_reg(wilc, wakeup_reg, &reg);
596 	if (ret)
597 		return;
598 	if (reg & wakeup_bit) {
599 		reg &= ~wakeup_bit;
600 		ret = hif_func->hif_write_reg(wilc, wakeup_reg, reg);
601 		if (ret)
602 			return;
603 	}
604 
605 	ret = hif_func->hif_read_reg(wilc, from_host_to_fw_reg, &reg);
606 	if (ret)
607 		return;
608 	if (reg & from_host_to_fw_bit) {
609 		reg &= ~from_host_to_fw_bit;
610 		ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg, reg);
611 		if (ret)
612 			return;
613 
614 	}
615 }
616 EXPORT_SYMBOL_GPL(chip_allow_sleep);
617 
618 void chip_wakeup(struct wilc *wilc)
619 {
620 	u32 ret = 0;
621 	u32 clk_status_val = 0, trials = 0;
622 	u32 wakeup_reg, wakeup_bit;
623 	u32 clk_status_reg, clk_status_bit;
624 	u32 from_host_to_fw_reg, from_host_to_fw_bit;
625 	const struct wilc_hif_func *hif_func = wilc->hif_func;
626 
627 	if (wilc->io_type == WILC_HIF_SDIO) {
628 		wakeup_reg = WILC_SDIO_WAKEUP_REG;
629 		wakeup_bit = WILC_SDIO_WAKEUP_BIT;
630 		clk_status_reg = WILC_SDIO_CLK_STATUS_REG;
631 		clk_status_bit = WILC_SDIO_CLK_STATUS_BIT;
632 		from_host_to_fw_reg = WILC_SDIO_HOST_TO_FW_REG;
633 		from_host_to_fw_bit = WILC_SDIO_HOST_TO_FW_BIT;
634 	} else {
635 		wakeup_reg = WILC_SPI_WAKEUP_REG;
636 		wakeup_bit = WILC_SPI_WAKEUP_BIT;
637 		clk_status_reg = WILC_SPI_CLK_STATUS_REG;
638 		clk_status_bit = WILC_SPI_CLK_STATUS_BIT;
639 		from_host_to_fw_reg = WILC_SPI_HOST_TO_FW_REG;
640 		from_host_to_fw_bit = WILC_SPI_HOST_TO_FW_BIT;
641 	}
642 
643 	/* indicate host wakeup */
644 	ret = hif_func->hif_write_reg(wilc, from_host_to_fw_reg,
645 				      from_host_to_fw_bit);
646 	if (ret)
647 		return;
648 
649 	/* Set wake-up bit */
650 	ret = hif_func->hif_write_reg(wilc, wakeup_reg,
651 				      wakeup_bit);
652 	if (ret)
653 		return;
654 
655 	while (trials < WAKE_UP_TRIAL_RETRY) {
656 		ret = hif_func->hif_read_reg(wilc, clk_status_reg,
657 					     &clk_status_val);
658 		if (ret) {
659 			pr_err("Bus error %d %x\n", ret, clk_status_val);
660 			return;
661 		}
662 		if (clk_status_val & clk_status_bit)
663 			break;
664 
665 		trials++;
666 	}
667 	if (trials >= WAKE_UP_TRIAL_RETRY) {
668 		pr_err("Failed to wake-up the chip\n");
669 		return;
670 	}
671 	/* Sometimes spi fail to read clock regs after reading
672 	 * writing clockless registers
673 	 */
674 	if (wilc->io_type == WILC_HIF_SPI)
675 		wilc->hif_func->hif_reset(wilc);
676 }
677 EXPORT_SYMBOL_GPL(chip_wakeup);
678 
679 void host_wakeup_notify(struct wilc *wilc)
680 {
681 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
682 	wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_2, 1);
683 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
684 }
685 EXPORT_SYMBOL_GPL(host_wakeup_notify);
686 
687 void host_sleep_notify(struct wilc *wilc)
688 {
689 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
690 	wilc->hif_func->hif_write_reg(wilc, WILC_CORTUS_INTERRUPT_1, 1);
691 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
692 }
693 EXPORT_SYMBOL_GPL(host_sleep_notify);
694 
695 int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
696 {
697 	int i, entries = 0;
698 	u8 k, ac;
699 	u32 sum;
700 	u32 reg;
701 	u8 ac_desired_ratio[NQUEUES] = {0, 0, 0, 0};
702 	u8 ac_preserve_ratio[NQUEUES] = {1, 1, 1, 1};
703 	u8 *num_pkts_to_add;
704 	u8 vmm_entries_ac[WILC_VMM_TBL_SIZE];
705 	u32 offset = 0;
706 	bool max_size_over = 0, ac_exist = 0;
707 	int vmm_sz = 0;
708 	struct txq_entry_t *tqe_q[NQUEUES];
709 	int ret = 0;
710 	int counter;
711 	int timeout;
712 	u32 *vmm_table = wilc->vmm_table;
713 	u8 ac_pkt_num_to_chip[NQUEUES] = {0, 0, 0, 0};
714 	const struct wilc_hif_func *func;
715 	u8 *txb = wilc->tx_buffer;
716 	struct wilc_vif *vif;
717 
718 	if (wilc->quit)
719 		goto out_update_cnt;
720 
721 	if (ac_balance(wilc, ac_desired_ratio))
722 		return -EINVAL;
723 
724 	mutex_lock(&wilc->txq_add_to_head_cs);
725 
726 	rcu_read_lock();
727 	wilc_for_each_vif(wilc, vif)
728 		wilc_wlan_txq_filter_dup_tcp_ack(vif->ndev);
729 	rcu_read_unlock();
730 
731 	for (ac = 0; ac < NQUEUES; ac++)
732 		tqe_q[ac] = wilc_wlan_txq_get_first(wilc, ac);
733 
734 	i = 0;
735 	sum = 0;
736 	max_size_over = 0;
737 	num_pkts_to_add = ac_desired_ratio;
738 	do {
739 		ac_exist = 0;
740 		for (ac = 0; (ac < NQUEUES) && (!max_size_over); ac++) {
741 			if (!tqe_q[ac])
742 				continue;
743 
744 			ac_exist = 1;
745 			for (k = 0; (k < num_pkts_to_add[ac]) &&
746 			     (!max_size_over) && tqe_q[ac]; k++) {
747 				if (i >= (WILC_VMM_TBL_SIZE - 1)) {
748 					max_size_over = 1;
749 					break;
750 				}
751 
752 				if (tqe_q[ac]->type == WILC_CFG_PKT)
753 					vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
754 				else if (tqe_q[ac]->type == WILC_NET_PKT)
755 					vmm_sz = ETH_ETHERNET_HDR_OFFSET;
756 				else
757 					vmm_sz = HOST_HDR_OFFSET;
758 
759 				vmm_sz += tqe_q[ac]->buffer_size;
760 				vmm_sz = ALIGN(vmm_sz, 4);
761 
762 				if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE) {
763 					max_size_over = 1;
764 					break;
765 				}
766 				vmm_table[i] = vmm_sz / 4;
767 				if (tqe_q[ac]->type == WILC_CFG_PKT)
768 					vmm_table[i] |= BIT(10);
769 
770 				cpu_to_le32s(&vmm_table[i]);
771 				vmm_entries_ac[i] = ac;
772 
773 				i++;
774 				sum += vmm_sz;
775 				tqe_q[ac] = wilc_wlan_txq_get_next(wilc,
776 								   tqe_q[ac],
777 								   ac);
778 			}
779 		}
780 		num_pkts_to_add = ac_preserve_ratio;
781 	} while (!max_size_over && ac_exist);
782 
783 	if (i == 0)
784 		goto out_unlock;
785 	vmm_table[i] = 0x0;
786 
787 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
788 	counter = 0;
789 	func = wilc->hif_func;
790 	do {
791 		ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
792 		if (ret)
793 			break;
794 
795 		if ((reg & 0x1) == 0) {
796 			ac_update_fw_ac_pkt_info(wilc, reg);
797 			break;
798 		}
799 
800 		counter++;
801 		if (counter > 200) {
802 			counter = 0;
803 			ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
804 			break;
805 		}
806 	} while (!wilc->quit);
807 
808 	if (ret)
809 		goto out_release_bus;
810 
811 	timeout = 200;
812 	do {
813 		ret = func->hif_block_tx(wilc,
814 					 WILC_VMM_TBL_RX_SHADOW_BASE,
815 					 (u8 *)vmm_table,
816 					 ((i + 1) * 4));
817 		if (ret)
818 			break;
819 
820 		ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
821 		if (ret)
822 			break;
823 
824 		do {
825 			ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, &reg);
826 			if (ret)
827 				break;
828 			if (FIELD_GET(WILC_VMM_ENTRY_AVAILABLE, reg)) {
829 				entries = FIELD_GET(WILC_VMM_ENTRY_COUNT, reg);
830 				break;
831 			}
832 		} while (--timeout);
833 		if (timeout <= 0) {
834 			ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
835 			break;
836 		}
837 
838 		if (ret)
839 			break;
840 
841 		if (entries == 0) {
842 			ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
843 			if (ret)
844 				break;
845 			reg &= ~BIT(0);
846 			ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
847 		}
848 	} while (0);
849 
850 	if (ret)
851 		goto out_release_bus;
852 
853 	if (entries == 0) {
854 		/*
855 		 * No VMM space available in firmware so retry to transmit
856 		 * the packet from tx queue.
857 		 */
858 		ret = WILC_VMM_ENTRY_FULL_RETRY;
859 		goto out_release_bus;
860 	}
861 
862 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
863 
864 	offset = 0;
865 	i = 0;
866 	do {
867 		struct txq_entry_t *tqe;
868 		u32 header, buffer_offset;
869 		char *bssid;
870 		u8 mgmt_ptk = 0;
871 
872 		if (vmm_table[i] == 0 || vmm_entries_ac[i] >= NQUEUES)
873 			break;
874 
875 		tqe = wilc_wlan_txq_remove_from_head(wilc, vmm_entries_ac[i]);
876 		if (!tqe)
877 			break;
878 
879 		ac_pkt_num_to_chip[vmm_entries_ac[i]]++;
880 		vif = tqe->vif;
881 
882 		le32_to_cpus(&vmm_table[i]);
883 		vmm_sz = FIELD_GET(WILC_VMM_BUFFER_SIZE, vmm_table[i]);
884 		vmm_sz *= 4;
885 
886 		if (tqe->type == WILC_MGMT_PKT)
887 			mgmt_ptk = 1;
888 
889 		header = (FIELD_PREP(WILC_VMM_HDR_TYPE, tqe->type) |
890 			  FIELD_PREP(WILC_VMM_HDR_MGMT_FIELD, mgmt_ptk) |
891 			  FIELD_PREP(WILC_VMM_HDR_PKT_SIZE, tqe->buffer_size) |
892 			  FIELD_PREP(WILC_VMM_HDR_BUFF_SIZE, vmm_sz));
893 
894 		cpu_to_le32s(&header);
895 		memcpy(&txb[offset], &header, 4);
896 		if (tqe->type == WILC_CFG_PKT) {
897 			buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
898 		} else if (tqe->type == WILC_NET_PKT) {
899 			int prio = tqe->q_num;
900 
901 			bssid = tqe->vif->bssid;
902 			buffer_offset = ETH_ETHERNET_HDR_OFFSET;
903 			memcpy(&txb[offset + 4], &prio, sizeof(prio));
904 			memcpy(&txb[offset + 8], bssid, 6);
905 		} else {
906 			buffer_offset = HOST_HDR_OFFSET;
907 		}
908 
909 		memcpy(&txb[offset + buffer_offset],
910 		       tqe->buffer, tqe->buffer_size);
911 		offset += vmm_sz;
912 		i++;
913 		tqe->status = 1;
914 		if (tqe->tx_complete_func)
915 			tqe->tx_complete_func(tqe->priv, tqe->status);
916 		if (tqe->ack_idx != NOT_TCP_ACK &&
917 		    tqe->ack_idx < MAX_PENDING_ACKS)
918 			vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
919 		kfree(tqe);
920 	} while (--entries);
921 	for (i = 0; i < NQUEUES; i++)
922 		wilc->txq[i].fw.count += ac_pkt_num_to_chip[i];
923 
924 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
925 
926 	ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
927 	if (ret)
928 		goto out_release_bus;
929 
930 	ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
931 
932 out_release_bus:
933 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
934 
935 out_unlock:
936 	mutex_unlock(&wilc->txq_add_to_head_cs);
937 
938 out_update_cnt:
939 	*txq_count = wilc->txq_entries;
940 	return ret;
941 }
942 
943 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
944 {
945 	int offset = 0;
946 	u32 header;
947 	u32 pkt_len, pkt_offset, tp_len;
948 	int is_cfg_packet;
949 	u8 *buff_ptr;
950 
951 	do {
952 		buff_ptr = buffer + offset;
953 		header = get_unaligned_le32(buff_ptr);
954 
955 		is_cfg_packet = FIELD_GET(WILC_PKT_HDR_CONFIG_FIELD, header);
956 		pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
957 		tp_len = FIELD_GET(WILC_PKT_HDR_TOTAL_LEN_FIELD, header);
958 		pkt_len = FIELD_GET(WILC_PKT_HDR_LEN_FIELD, header);
959 
960 		if (pkt_len == 0 || tp_len == 0)
961 			break;
962 
963 		if (pkt_offset & IS_MANAGMEMENT) {
964 			buff_ptr += HOST_HDR_OFFSET;
965 			wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len,
966 					 pkt_offset & IS_MGMT_AUTH_PKT);
967 		} else {
968 			if (!is_cfg_packet) {
969 				wilc_frmw_to_host(wilc, buff_ptr, pkt_len,
970 						  pkt_offset);
971 			} else {
972 				struct wilc_cfg_rsp rsp;
973 
974 				buff_ptr += pkt_offset;
975 
976 				wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
977 							  pkt_len,
978 							  &rsp);
979 				if (rsp.type == WILC_CFG_RSP) {
980 					if (wilc->cfg_seq_no == rsp.seq_no)
981 						complete(&wilc->cfg_event);
982 				} else if (rsp.type == WILC_CFG_RSP_STATUS) {
983 					wilc_mac_indicate(wilc);
984 				}
985 			}
986 		}
987 		offset += tp_len;
988 	} while (offset < size);
989 }
990 
991 static void wilc_wlan_handle_rxq(struct wilc *wilc)
992 {
993 	int size;
994 	u8 *buffer;
995 	struct rxq_entry_t *rqe;
996 
997 	while (!wilc->quit) {
998 		rqe = wilc_wlan_rxq_remove(wilc);
999 		if (!rqe)
1000 			break;
1001 
1002 		buffer = rqe->buffer;
1003 		size = rqe->buffer_size;
1004 		wilc_wlan_handle_rx_buff(wilc, buffer, size);
1005 
1006 		kfree(rqe);
1007 	}
1008 	if (wilc->quit)
1009 		complete(&wilc->cfg_event);
1010 }
1011 
1012 static void wilc_unknown_isr_ext(struct wilc *wilc)
1013 {
1014 	wilc->hif_func->hif_clear_int_ext(wilc, 0);
1015 }
1016 
1017 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
1018 {
1019 	u32 offset = wilc->rx_buffer_offset;
1020 	u8 *buffer = NULL;
1021 	u32 size;
1022 	u32 retries = 0;
1023 	int ret = 0;
1024 	struct rxq_entry_t *rqe;
1025 
1026 	size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, int_status) << 2;
1027 
1028 	while (!size && retries < 10) {
1029 		wilc->hif_func->hif_read_size(wilc, &size);
1030 		size = FIELD_GET(WILC_INTERRUPT_DATA_SIZE, size) << 2;
1031 		retries++;
1032 	}
1033 
1034 	if (size <= 0)
1035 		return;
1036 
1037 	if (WILC_RX_BUFF_SIZE - offset < size)
1038 		offset = 0;
1039 
1040 	buffer = &wilc->rx_buffer[offset];
1041 
1042 	wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
1043 	ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
1044 	if (ret)
1045 		return;
1046 
1047 	offset += size;
1048 	wilc->rx_buffer_offset = offset;
1049 	rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
1050 	if (!rqe)
1051 		return;
1052 
1053 	rqe->buffer = buffer;
1054 	rqe->buffer_size = size;
1055 	wilc_wlan_rxq_add(wilc, rqe);
1056 	wilc_wlan_handle_rxq(wilc);
1057 }
1058 
1059 void wilc_handle_isr(struct wilc *wilc)
1060 {
1061 	u32 int_status;
1062 
1063 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1064 	wilc->hif_func->hif_read_int(wilc, &int_status);
1065 
1066 	if (int_status & DATA_INT_EXT)
1067 		wilc_wlan_handle_isr_ext(wilc, int_status);
1068 
1069 	if (!(int_status & (ALL_INT_EXT)))
1070 		wilc_unknown_isr_ext(wilc);
1071 
1072 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1073 }
1074 EXPORT_SYMBOL_GPL(wilc_handle_isr);
1075 
1076 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
1077 				u32 buffer_size)
1078 {
1079 	u32 offset;
1080 	u32 addr, size, size2, blksz;
1081 	u8 *dma_buffer;
1082 	int ret = 0;
1083 	u32 reg = 0;
1084 
1085 	blksz = BIT(12);
1086 
1087 	dma_buffer = kmalloc(blksz, GFP_KERNEL);
1088 	if (!dma_buffer)
1089 		return -EIO;
1090 
1091 	offset = 0;
1092 	pr_debug("%s: Downloading firmware size = %d\n", __func__, buffer_size);
1093 
1094 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1095 
1096 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1097 	reg &= ~BIT(10);
1098 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1099 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1100 	if (reg & BIT(10))
1101 		pr_err("%s: Failed to reset\n", __func__);
1102 
1103 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1104 	do {
1105 		addr = get_unaligned_le32(&buffer[offset]);
1106 		size = get_unaligned_le32(&buffer[offset + 4]);
1107 		acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1108 		offset += 8;
1109 		while (((int)size) && (offset < buffer_size)) {
1110 			if (size <= blksz)
1111 				size2 = size;
1112 			else
1113 				size2 = blksz;
1114 
1115 			memcpy(dma_buffer, &buffer[offset], size2);
1116 			ret = wilc->hif_func->hif_block_tx(wilc, addr,
1117 							   dma_buffer, size2);
1118 			if (ret)
1119 				break;
1120 
1121 			addr += size2;
1122 			offset += size2;
1123 			size -= size2;
1124 		}
1125 		release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1126 
1127 		if (ret) {
1128 			pr_err("%s Bus error\n", __func__);
1129 			goto fail;
1130 		}
1131 		pr_debug("%s Offset = %d\n", __func__, offset);
1132 	} while (offset < buffer_size);
1133 
1134 fail:
1135 
1136 	kfree(dma_buffer);
1137 
1138 	return ret;
1139 }
1140 
1141 int wilc_wlan_start(struct wilc *wilc)
1142 {
1143 	u32 reg = 0;
1144 	int ret;
1145 	u32 chipid;
1146 
1147 	if (wilc->io_type == WILC_HIF_SDIO) {
1148 		reg = 0;
1149 		reg |= BIT(3);
1150 	} else if (wilc->io_type == WILC_HIF_SPI) {
1151 		reg = 1;
1152 	}
1153 	acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1154 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
1155 	if (ret)
1156 		goto release;
1157 
1158 	reg = 0;
1159 	if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
1160 		reg |= WILC_HAVE_SDIO_IRQ_GPIO;
1161 
1162 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
1163 	if (ret)
1164 		goto release;
1165 
1166 	wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
1167 
1168 	ret = wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
1169 	if (ret)
1170 		goto release;
1171 
1172 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1173 	if ((reg & BIT(10)) == BIT(10)) {
1174 		reg &= ~BIT(10);
1175 		wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1176 		wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1177 	}
1178 
1179 	reg |= BIT(10);
1180 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
1181 	wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
1182 
1183 release:
1184 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1185 	return ret;
1186 }
1187 
1188 int wilc_wlan_stop(struct wilc *wilc, struct wilc_vif *vif)
1189 {
1190 	u32 reg = 0;
1191 	int ret;
1192 
1193 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1194 
1195 	ret = wilc->hif_func->hif_read_reg(wilc, GLOBAL_MODE_CONTROL, &reg);
1196 	if (ret)
1197 		goto release;
1198 
1199 	reg &= ~WILC_GLOBAL_MODE_ENABLE_WIFI;
1200 	ret = wilc->hif_func->hif_write_reg(wilc, GLOBAL_MODE_CONTROL, reg);
1201 	if (ret)
1202 		goto release;
1203 
1204 	ret = wilc->hif_func->hif_read_reg(wilc, PWR_SEQ_MISC_CTRL, &reg);
1205 	if (ret)
1206 		goto release;
1207 
1208 	reg &= ~WILC_PWR_SEQ_ENABLE_WIFI_SLEEP;
1209 	ret = wilc->hif_func->hif_write_reg(wilc, PWR_SEQ_MISC_CTRL, reg);
1210 	if (ret)
1211 		goto release;
1212 
1213 	ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, &reg);
1214 	if (ret) {
1215 		netdev_err(vif->ndev, "Error while reading reg\n");
1216 		goto release;
1217 	}
1218 
1219 	ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
1220 					(reg | WILC_ABORT_REQ_BIT));
1221 	if (ret) {
1222 		netdev_err(vif->ndev, "Error while writing reg\n");
1223 		goto release;
1224 	}
1225 
1226 	ret = 0;
1227 release:
1228 	/* host comm is disabled - we can't issue sleep command anymore: */
1229 	release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1230 
1231 	return ret;
1232 }
1233 
1234 void wilc_wlan_cleanup(struct net_device *dev)
1235 {
1236 	struct txq_entry_t *tqe;
1237 	struct rxq_entry_t *rqe;
1238 	u8 ac;
1239 	struct wilc_vif *vif = netdev_priv(dev);
1240 	struct wilc *wilc = vif->wilc;
1241 
1242 	wilc->quit = 1;
1243 	for (ac = 0; ac < NQUEUES; ac++) {
1244 		while ((tqe = wilc_wlan_txq_remove_from_head(wilc, ac))) {
1245 			if (tqe->tx_complete_func)
1246 				tqe->tx_complete_func(tqe->priv, 0);
1247 			kfree(tqe);
1248 		}
1249 	}
1250 
1251 	while ((rqe = wilc_wlan_rxq_remove(wilc)))
1252 		kfree(rqe);
1253 
1254 	kfree(wilc->vmm_table);
1255 	wilc->vmm_table = NULL;
1256 	kfree(wilc->rx_buffer);
1257 	wilc->rx_buffer = NULL;
1258 	kfree(wilc->tx_buffer);
1259 	wilc->tx_buffer = NULL;
1260 	wilc->hif_func->hif_deinit(wilc);
1261 }
1262 
1263 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
1264 				u32 drv_handler)
1265 {
1266 	struct wilc *wilc = vif->wilc;
1267 	struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
1268 	int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
1269 
1270 	if (type == WILC_CFG_SET)
1271 		cfg->hdr.cmd_type = 'W';
1272 	else
1273 		cfg->hdr.cmd_type = 'Q';
1274 
1275 	cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
1276 	cfg->hdr.total_len = cpu_to_le16(t_len);
1277 	cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
1278 	wilc->cfg_seq_no = cfg->hdr.seq_no;
1279 
1280 	if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
1281 		return -1;
1282 
1283 	return 0;
1284 }
1285 
1286 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1287 		      u32 buffer_size, int commit, u32 drv_handler)
1288 {
1289 	u32 offset;
1290 	int ret_size;
1291 	struct wilc *wilc = vif->wilc;
1292 
1293 	mutex_lock(&wilc->cfg_cmd_lock);
1294 
1295 	if (start)
1296 		wilc->cfg_frame_offset = 0;
1297 
1298 	offset = wilc->cfg_frame_offset;
1299 	ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1300 					 wid, buffer, buffer_size);
1301 	offset += ret_size;
1302 	wilc->cfg_frame_offset = offset;
1303 
1304 	if (!commit) {
1305 		mutex_unlock(&wilc->cfg_cmd_lock);
1306 		return ret_size;
1307 	}
1308 
1309 	netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1310 
1311 	if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1312 		ret_size = 0;
1313 
1314 	if (!wait_for_completion_timeout(&wilc->cfg_event,
1315 					 WILC_CFG_PKTS_TIMEOUT)) {
1316 		netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1317 		ret_size = 0;
1318 	}
1319 
1320 	wilc->cfg_frame_offset = 0;
1321 	wilc->cfg_seq_no += 1;
1322 	mutex_unlock(&wilc->cfg_cmd_lock);
1323 
1324 	return ret_size;
1325 }
1326 
1327 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1328 		      u32 drv_handler)
1329 {
1330 	u32 offset;
1331 	int ret_size;
1332 	struct wilc *wilc = vif->wilc;
1333 
1334 	mutex_lock(&wilc->cfg_cmd_lock);
1335 
1336 	if (start)
1337 		wilc->cfg_frame_offset = 0;
1338 
1339 	offset = wilc->cfg_frame_offset;
1340 	ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1341 	offset += ret_size;
1342 	wilc->cfg_frame_offset = offset;
1343 
1344 	if (!commit) {
1345 		mutex_unlock(&wilc->cfg_cmd_lock);
1346 		return ret_size;
1347 	}
1348 
1349 	if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1350 		ret_size = 0;
1351 
1352 	if (!wait_for_completion_timeout(&wilc->cfg_event,
1353 					 WILC_CFG_PKTS_TIMEOUT)) {
1354 		netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1355 		ret_size = 0;
1356 	}
1357 	wilc->cfg_frame_offset = 0;
1358 	wilc->cfg_seq_no += 1;
1359 	mutex_unlock(&wilc->cfg_cmd_lock);
1360 
1361 	return ret_size;
1362 }
1363 
1364 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1365 			 u32 count)
1366 {
1367 	int i;
1368 	int ret = 0;
1369 	u32 drv = wilc_get_vif_idx(vif);
1370 
1371 	if (mode == WILC_GET_CFG) {
1372 		for (i = 0; i < count; i++) {
1373 			if (!wilc_wlan_cfg_get(vif, !i,
1374 					       wids[i].id,
1375 					       (i == count - 1),
1376 					       drv)) {
1377 				ret = -ETIMEDOUT;
1378 				break;
1379 			}
1380 		}
1381 		for (i = 0; i < count; i++) {
1382 			wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1383 							     wids[i].id,
1384 							     wids[i].val,
1385 							     wids[i].size);
1386 		}
1387 	} else if (mode == WILC_SET_CFG) {
1388 		for (i = 0; i < count; i++) {
1389 			if (!wilc_wlan_cfg_set(vif, !i,
1390 					       wids[i].id,
1391 					       wids[i].val,
1392 					       wids[i].size,
1393 					       (i == count - 1),
1394 					       drv)) {
1395 				ret = -ETIMEDOUT;
1396 				break;
1397 			}
1398 		}
1399 	}
1400 
1401 	return ret;
1402 }
1403 
1404 static int init_chip(struct net_device *dev)
1405 {
1406 	u32 chipid;
1407 	u32 reg;
1408 	int ret = 0;
1409 	struct wilc_vif *vif = netdev_priv(dev);
1410 	struct wilc *wilc = vif->wilc;
1411 
1412 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
1413 
1414 	chipid = wilc_get_chipid(wilc, true);
1415 
1416 	if ((chipid & 0xfff) != 0xa0) {
1417 		ret = wilc->hif_func->hif_read_reg(wilc,
1418 						   WILC_CORTUS_RESET_MUX_SEL,
1419 						   &reg);
1420 		if (ret) {
1421 			netdev_err(dev, "fail read reg 0x1118\n");
1422 			goto release;
1423 		}
1424 		reg |= BIT(0);
1425 		ret = wilc->hif_func->hif_write_reg(wilc,
1426 						    WILC_CORTUS_RESET_MUX_SEL,
1427 						    reg);
1428 		if (ret) {
1429 			netdev_err(dev, "fail write reg 0x1118\n");
1430 			goto release;
1431 		}
1432 		ret = wilc->hif_func->hif_write_reg(wilc,
1433 						    WILC_CORTUS_BOOT_REGISTER,
1434 						    WILC_CORTUS_BOOT_FROM_IRAM);
1435 		if (ret) {
1436 			netdev_err(dev, "fail write reg 0xc0000\n");
1437 			goto release;
1438 		}
1439 	}
1440 
1441 release:
1442 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
1443 
1444 	return ret;
1445 }
1446 
1447 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1448 {
1449 	u32 chipid = 0;
1450 	u32 rfrevid = 0;
1451 
1452 	if (wilc->chipid == 0 || update) {
1453 		wilc->hif_func->hif_read_reg(wilc, WILC_CHIPID, &chipid);
1454 		wilc->hif_func->hif_read_reg(wilc, WILC_RF_REVISION_ID,
1455 					     &rfrevid);
1456 		if (!is_wilc1000(chipid)) {
1457 			wilc->chipid = 0;
1458 			return wilc->chipid;
1459 		}
1460 		if (chipid == WILC_1000_BASE_ID_2A) { /* 0x1002A0 */
1461 			if (rfrevid != 0x1)
1462 				chipid = WILC_1000_BASE_ID_2A_REV1;
1463 		} else if (chipid == WILC_1000_BASE_ID_2B) { /* 0x1002B0 */
1464 			if (rfrevid == 0x4)
1465 				chipid = WILC_1000_BASE_ID_2B_REV1;
1466 			else if (rfrevid != 0x3)
1467 				chipid = WILC_1000_BASE_ID_2B_REV2;
1468 		}
1469 
1470 		wilc->chipid = chipid;
1471 	}
1472 	return wilc->chipid;
1473 }
1474 
1475 int wilc_wlan_init(struct net_device *dev)
1476 {
1477 	int ret = 0;
1478 	struct wilc_vif *vif = netdev_priv(dev);
1479 	struct wilc *wilc;
1480 
1481 	wilc = vif->wilc;
1482 
1483 	wilc->quit = 0;
1484 
1485 	if (!wilc->hif_func->hif_is_init(wilc)) {
1486 		acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1487 		ret = wilc->hif_func->hif_init(wilc, false);
1488 		release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1489 		if (ret)
1490 			goto fail;
1491 	}
1492 
1493 	if (!wilc->vmm_table)
1494 		wilc->vmm_table = kcalloc(WILC_VMM_TBL_SIZE, sizeof(u32), GFP_KERNEL);
1495 
1496 	if (!wilc->vmm_table) {
1497 		ret = -ENOBUFS;
1498 		goto fail;
1499 	}
1500 
1501 	if (!wilc->tx_buffer)
1502 		wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1503 
1504 	if (!wilc->tx_buffer) {
1505 		ret = -ENOBUFS;
1506 		goto fail;
1507 	}
1508 
1509 	if (!wilc->rx_buffer)
1510 		wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1511 
1512 	if (!wilc->rx_buffer) {
1513 		ret = -ENOBUFS;
1514 		goto fail;
1515 	}
1516 
1517 	if (init_chip(dev)) {
1518 		ret = -EIO;
1519 		goto fail;
1520 	}
1521 
1522 	return 0;
1523 
1524 fail:
1525 	kfree(wilc->vmm_table);
1526 	wilc->vmm_table = NULL;
1527 	kfree(wilc->rx_buffer);
1528 	wilc->rx_buffer = NULL;
1529 	kfree(wilc->tx_buffer);
1530 	wilc->tx_buffer = NULL;
1531 
1532 	return ret;
1533 }
1534