treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / wilc1000 / wlan.c
blob601e4d1345d249570a42d3d1e724f5705dae3154
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
4 * All rights reserved.
5 */
7 #include <linux/if_ether.h>
8 #include <linux/ip.h>
9 #include "cfg80211.h"
10 #include "wlan_cfg.h"
12 static inline bool is_wilc1000(u32 id)
14 return (id & 0xfffff000) == 0x100000;
17 static inline void acquire_bus(struct wilc *wilc, enum bus_acquire acquire)
19 mutex_lock(&wilc->hif_cs);
20 if (acquire == WILC_BUS_ACQUIRE_AND_WAKEUP)
21 chip_wakeup(wilc);
24 static inline void release_bus(struct wilc *wilc, enum bus_release release)
26 if (release == WILC_BUS_RELEASE_ALLOW_SLEEP)
27 chip_allow_sleep(wilc);
28 mutex_unlock(&wilc->hif_cs);
31 static void wilc_wlan_txq_remove(struct wilc *wilc, struct txq_entry_t *tqe)
33 list_del(&tqe->list);
34 wilc->txq_entries -= 1;
37 static struct txq_entry_t *
38 wilc_wlan_txq_remove_from_head(struct net_device *dev)
40 struct txq_entry_t *tqe = NULL;
41 unsigned long flags;
42 struct wilc_vif *vif = netdev_priv(dev);
43 struct wilc *wilc = vif->wilc;
45 spin_lock_irqsave(&wilc->txq_spinlock, flags);
47 if (!list_empty(&wilc->txq_head.list)) {
48 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
49 list);
50 list_del(&tqe->list);
51 wilc->txq_entries -= 1;
53 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
54 return tqe;
57 static void wilc_wlan_txq_add_to_tail(struct net_device *dev,
58 struct txq_entry_t *tqe)
60 unsigned long flags;
61 struct wilc_vif *vif = netdev_priv(dev);
62 struct wilc *wilc = vif->wilc;
64 spin_lock_irqsave(&wilc->txq_spinlock, flags);
66 list_add_tail(&tqe->list, &wilc->txq_head.list);
67 wilc->txq_entries += 1;
69 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
71 complete(&wilc->txq_event);
74 static void wilc_wlan_txq_add_to_head(struct wilc_vif *vif,
75 struct txq_entry_t *tqe)
77 unsigned long flags;
78 struct wilc *wilc = vif->wilc;
80 mutex_lock(&wilc->txq_add_to_head_cs);
82 spin_lock_irqsave(&wilc->txq_spinlock, flags);
84 list_add(&tqe->list, &wilc->txq_head.list);
85 wilc->txq_entries += 1;
87 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
88 mutex_unlock(&wilc->txq_add_to_head_cs);
89 complete(&wilc->txq_event);
92 #define NOT_TCP_ACK (-1)
94 static inline void add_tcp_session(struct wilc_vif *vif, u32 src_prt,
95 u32 dst_prt, u32 seq)
97 struct tcp_ack_filter *f = &vif->ack_filter;
99 if (f->tcp_session < 2 * MAX_TCP_SESSION) {
100 f->ack_session_info[f->tcp_session].seq_num = seq;
101 f->ack_session_info[f->tcp_session].bigger_ack_num = 0;
102 f->ack_session_info[f->tcp_session].src_port = src_prt;
103 f->ack_session_info[f->tcp_session].dst_port = dst_prt;
104 f->tcp_session++;
108 static inline void update_tcp_session(struct wilc_vif *vif, u32 index, u32 ack)
110 struct tcp_ack_filter *f = &vif->ack_filter;
112 if (index < 2 * MAX_TCP_SESSION &&
113 ack > f->ack_session_info[index].bigger_ack_num)
114 f->ack_session_info[index].bigger_ack_num = ack;
117 static inline void add_tcp_pending_ack(struct wilc_vif *vif, u32 ack,
118 u32 session_index,
119 struct txq_entry_t *txqe)
121 struct tcp_ack_filter *f = &vif->ack_filter;
122 u32 i = f->pending_base + f->pending_acks_idx;
124 if (i < MAX_PENDING_ACKS) {
125 f->pending_acks[i].ack_num = ack;
126 f->pending_acks[i].txqe = txqe;
127 f->pending_acks[i].session_index = session_index;
128 txqe->ack_idx = i;
129 f->pending_acks_idx++;
133 static inline void tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
135 void *buffer = tqe->buffer;
136 const struct ethhdr *eth_hdr_ptr = buffer;
137 int i;
138 unsigned long flags;
139 struct wilc_vif *vif = netdev_priv(dev);
140 struct wilc *wilc = vif->wilc;
141 struct tcp_ack_filter *f = &vif->ack_filter;
142 const struct iphdr *ip_hdr_ptr;
143 const struct tcphdr *tcp_hdr_ptr;
144 u32 ihl, total_length, data_offset;
146 spin_lock_irqsave(&wilc->txq_spinlock, flags);
148 if (eth_hdr_ptr->h_proto != htons(ETH_P_IP))
149 goto out;
151 ip_hdr_ptr = buffer + ETH_HLEN;
153 if (ip_hdr_ptr->protocol != IPPROTO_TCP)
154 goto out;
156 ihl = ip_hdr_ptr->ihl << 2;
157 tcp_hdr_ptr = buffer + ETH_HLEN + ihl;
158 total_length = ntohs(ip_hdr_ptr->tot_len);
160 data_offset = tcp_hdr_ptr->doff << 2;
161 if (total_length == (ihl + data_offset)) {
162 u32 seq_no, ack_no;
164 seq_no = ntohl(tcp_hdr_ptr->seq);
165 ack_no = ntohl(tcp_hdr_ptr->ack_seq);
166 for (i = 0; i < f->tcp_session; i++) {
167 u32 j = f->ack_session_info[i].seq_num;
169 if (i < 2 * MAX_TCP_SESSION &&
170 j == seq_no) {
171 update_tcp_session(vif, i, ack_no);
172 break;
175 if (i == f->tcp_session)
176 add_tcp_session(vif, 0, 0, seq_no);
178 add_tcp_pending_ack(vif, ack_no, i, tqe);
181 out:
182 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
185 static void wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
187 struct wilc_vif *vif = netdev_priv(dev);
188 struct wilc *wilc = vif->wilc;
189 struct tcp_ack_filter *f = &vif->ack_filter;
190 u32 i = 0;
191 u32 dropped = 0;
192 unsigned long flags;
194 spin_lock_irqsave(&wilc->txq_spinlock, flags);
195 for (i = f->pending_base;
196 i < (f->pending_base + f->pending_acks_idx); i++) {
197 u32 index;
198 u32 bigger_ack_num;
200 if (i >= MAX_PENDING_ACKS)
201 break;
203 index = f->pending_acks[i].session_index;
205 if (index >= 2 * MAX_TCP_SESSION)
206 break;
208 bigger_ack_num = f->ack_session_info[index].bigger_ack_num;
210 if (f->pending_acks[i].ack_num < bigger_ack_num) {
211 struct txq_entry_t *tqe;
213 tqe = f->pending_acks[i].txqe;
214 if (tqe) {
215 wilc_wlan_txq_remove(wilc, tqe);
216 tqe->status = 1;
217 if (tqe->tx_complete_func)
218 tqe->tx_complete_func(tqe->priv,
219 tqe->status);
220 kfree(tqe);
221 dropped++;
225 f->pending_acks_idx = 0;
226 f->tcp_session = 0;
228 if (f->pending_base == 0)
229 f->pending_base = MAX_TCP_SESSION;
230 else
231 f->pending_base = 0;
233 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
235 while (dropped > 0) {
236 wait_for_completion_timeout(&wilc->txq_event,
237 msecs_to_jiffies(1));
238 dropped--;
242 void wilc_enable_tcp_ack_filter(struct wilc_vif *vif, bool value)
244 vif->ack_filter.enabled = value;
247 static int wilc_wlan_txq_add_cfg_pkt(struct wilc_vif *vif, u8 *buffer,
248 u32 buffer_size)
250 struct txq_entry_t *tqe;
251 struct wilc *wilc = vif->wilc;
253 netdev_dbg(vif->ndev, "Adding config packet ...\n");
254 if (wilc->quit) {
255 netdev_dbg(vif->ndev, "Return due to clear function\n");
256 complete(&wilc->cfg_event);
257 return 0;
260 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
261 if (!tqe)
262 return 0;
264 tqe->type = WILC_CFG_PKT;
265 tqe->buffer = buffer;
266 tqe->buffer_size = buffer_size;
267 tqe->tx_complete_func = NULL;
268 tqe->priv = NULL;
269 tqe->ack_idx = NOT_TCP_ACK;
270 tqe->vif = vif;
272 wilc_wlan_txq_add_to_head(vif, tqe);
274 return 1;
277 int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
278 u32 buffer_size,
279 void (*tx_complete_fn)(void *, int))
281 struct txq_entry_t *tqe;
282 struct wilc_vif *vif = netdev_priv(dev);
283 struct wilc *wilc;
285 wilc = vif->wilc;
287 if (wilc->quit)
288 return 0;
290 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
292 if (!tqe)
293 return 0;
294 tqe->type = WILC_NET_PKT;
295 tqe->buffer = buffer;
296 tqe->buffer_size = buffer_size;
297 tqe->tx_complete_func = tx_complete_fn;
298 tqe->priv = priv;
299 tqe->vif = vif;
301 tqe->ack_idx = NOT_TCP_ACK;
302 if (vif->ack_filter.enabled)
303 tcp_process(dev, tqe);
304 wilc_wlan_txq_add_to_tail(dev, tqe);
305 return wilc->txq_entries;
308 int wilc_wlan_txq_add_mgmt_pkt(struct net_device *dev, void *priv, u8 *buffer,
309 u32 buffer_size,
310 void (*tx_complete_fn)(void *, int))
312 struct txq_entry_t *tqe;
313 struct wilc_vif *vif = netdev_priv(dev);
314 struct wilc *wilc;
316 wilc = vif->wilc;
318 if (wilc->quit)
319 return 0;
321 tqe = kmalloc(sizeof(*tqe), GFP_ATOMIC);
323 if (!tqe)
324 return 0;
325 tqe->type = WILC_MGMT_PKT;
326 tqe->buffer = buffer;
327 tqe->buffer_size = buffer_size;
328 tqe->tx_complete_func = tx_complete_fn;
329 tqe->priv = priv;
330 tqe->ack_idx = NOT_TCP_ACK;
331 tqe->vif = vif;
332 wilc_wlan_txq_add_to_tail(dev, tqe);
333 return 1;
336 static struct txq_entry_t *wilc_wlan_txq_get_first(struct wilc *wilc)
338 struct txq_entry_t *tqe = NULL;
339 unsigned long flags;
341 spin_lock_irqsave(&wilc->txq_spinlock, flags);
343 if (!list_empty(&wilc->txq_head.list))
344 tqe = list_first_entry(&wilc->txq_head.list, struct txq_entry_t,
345 list);
347 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
349 return tqe;
352 static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
353 struct txq_entry_t *tqe)
355 unsigned long flags;
357 spin_lock_irqsave(&wilc->txq_spinlock, flags);
359 if (!list_is_last(&tqe->list, &wilc->txq_head.list))
360 tqe = list_next_entry(tqe, list);
361 else
362 tqe = NULL;
363 spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
365 return tqe;
368 static void wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
370 if (wilc->quit)
371 return;
373 mutex_lock(&wilc->rxq_cs);
374 list_add_tail(&rqe->list, &wilc->rxq_head.list);
375 mutex_unlock(&wilc->rxq_cs);
378 static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
380 struct rxq_entry_t *rqe = NULL;
382 mutex_lock(&wilc->rxq_cs);
383 if (!list_empty(&wilc->rxq_head.list)) {
384 rqe = list_first_entry(&wilc->rxq_head.list, struct rxq_entry_t,
385 list);
386 list_del(&rqe->list);
388 mutex_unlock(&wilc->rxq_cs);
389 return rqe;
392 void chip_allow_sleep(struct wilc *wilc)
394 u32 reg = 0;
396 wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
398 wilc->hif_func->hif_write_reg(wilc, 0xf0, reg & ~BIT(0));
399 wilc->hif_func->hif_write_reg(wilc, 0xfa, 0);
401 EXPORT_SYMBOL_GPL(chip_allow_sleep);
403 void chip_wakeup(struct wilc *wilc)
405 u32 reg, clk_status_reg;
407 if ((wilc->io_type & 0x1) == WILC_HIF_SPI) {
408 do {
409 wilc->hif_func->hif_read_reg(wilc, 1, &reg);
410 wilc->hif_func->hif_write_reg(wilc, 1, reg | BIT(1));
411 wilc->hif_func->hif_write_reg(wilc, 1, reg & ~BIT(1));
413 do {
414 usleep_range(2000, 2500);
415 wilc_get_chipid(wilc, true);
416 } while (wilc_get_chipid(wilc, true) == 0);
417 } while (wilc_get_chipid(wilc, true) == 0);
418 } else if ((wilc->io_type & 0x1) == WILC_HIF_SDIO) {
419 wilc->hif_func->hif_write_reg(wilc, 0xfa, 1);
420 usleep_range(200, 400);
421 wilc->hif_func->hif_read_reg(wilc, 0xf0, &reg);
422 do {
423 wilc->hif_func->hif_write_reg(wilc, 0xf0,
424 reg | BIT(0));
425 wilc->hif_func->hif_read_reg(wilc, 0xf1,
426 &clk_status_reg);
428 while ((clk_status_reg & 0x1) == 0) {
429 usleep_range(2000, 2500);
431 wilc->hif_func->hif_read_reg(wilc, 0xf1,
432 &clk_status_reg);
434 if ((clk_status_reg & 0x1) == 0) {
435 wilc->hif_func->hif_write_reg(wilc, 0xf0,
436 reg & (~BIT(0)));
438 } while ((clk_status_reg & 0x1) == 0);
441 if (wilc->chip_ps_state == WILC_CHIP_SLEEPING_MANUAL) {
442 if (wilc_get_chipid(wilc, false) < 0x1002b0) {
443 u32 val32;
445 wilc->hif_func->hif_read_reg(wilc, 0x1e1c, &val32);
446 val32 |= BIT(6);
447 wilc->hif_func->hif_write_reg(wilc, 0x1e1c, val32);
449 wilc->hif_func->hif_read_reg(wilc, 0x1e9c, &val32);
450 val32 |= BIT(6);
451 wilc->hif_func->hif_write_reg(wilc, 0x1e9c, val32);
454 wilc->chip_ps_state = WILC_CHIP_WAKEDUP;
456 EXPORT_SYMBOL_GPL(chip_wakeup);
458 void host_wakeup_notify(struct wilc *wilc)
460 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
461 wilc->hif_func->hif_write_reg(wilc, 0x10b0, 1);
462 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
464 EXPORT_SYMBOL_GPL(host_wakeup_notify);
466 void host_sleep_notify(struct wilc *wilc)
468 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
469 wilc->hif_func->hif_write_reg(wilc, 0x10ac, 1);
470 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
472 EXPORT_SYMBOL_GPL(host_sleep_notify);
474 int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
476 int i, entries = 0;
477 u32 sum;
478 u32 reg;
479 u32 offset = 0;
480 int vmm_sz = 0;
481 struct txq_entry_t *tqe;
482 int ret = 0;
483 int counter;
484 int timeout;
485 u32 vmm_table[WILC_VMM_TBL_SIZE];
486 const struct wilc_hif_func *func;
487 u8 *txb = wilc->tx_buffer;
488 struct net_device *dev;
489 struct wilc_vif *vif;
491 if (wilc->quit)
492 goto out_update_cnt;
494 mutex_lock(&wilc->txq_add_to_head_cs);
495 tqe = wilc_wlan_txq_get_first(wilc);
496 if (!tqe)
497 goto out_unlock;
498 dev = tqe->vif->ndev;
499 wilc_wlan_txq_filter_dup_tcp_ack(dev);
500 i = 0;
501 sum = 0;
502 while (tqe && (i < (WILC_VMM_TBL_SIZE - 1))) {
503 if (tqe->type == WILC_CFG_PKT)
504 vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
505 else if (tqe->type == WILC_NET_PKT)
506 vmm_sz = ETH_ETHERNET_HDR_OFFSET;
507 else
508 vmm_sz = HOST_HDR_OFFSET;
510 vmm_sz += tqe->buffer_size;
512 if (vmm_sz & 0x3)
513 vmm_sz = (vmm_sz + 4) & ~0x3;
515 if ((sum + vmm_sz) > WILC_TX_BUFF_SIZE)
516 break;
518 vmm_table[i] = vmm_sz / 4;
519 if (tqe->type == WILC_CFG_PKT)
520 vmm_table[i] |= BIT(10);
521 cpu_to_le32s(&vmm_table[i]);
523 i++;
524 sum += vmm_sz;
525 tqe = wilc_wlan_txq_get_next(wilc, tqe);
528 if (i == 0)
529 goto out_unlock;
530 vmm_table[i] = 0x0;
532 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
533 counter = 0;
534 func = wilc->hif_func;
535 do {
536 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
537 if (ret)
538 break;
540 if ((reg & 0x1) == 0)
541 break;
543 counter++;
544 if (counter > 200) {
545 counter = 0;
546 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, 0);
547 break;
549 } while (!wilc->quit);
551 if (ret)
552 goto out_release_bus;
554 timeout = 200;
555 do {
556 ret = func->hif_block_tx(wilc,
557 WILC_VMM_TBL_RX_SHADOW_BASE,
558 (u8 *)vmm_table,
559 ((i + 1) * 4));
560 if (ret)
561 break;
563 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x2);
564 if (ret)
565 break;
567 do {
568 ret = func->hif_read_reg(wilc, WILC_HOST_VMM_CTL, &reg);
569 if (ret)
570 break;
571 if ((reg >> 2) & 0x1) {
572 entries = ((reg >> 3) & 0x3f);
573 break;
575 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
576 } while (--timeout);
577 if (timeout <= 0) {
578 ret = func->hif_write_reg(wilc, WILC_HOST_VMM_CTL, 0x0);
579 break;
582 if (ret)
583 break;
585 if (entries == 0) {
586 ret = func->hif_read_reg(wilc, WILC_HOST_TX_CTRL, &reg);
587 if (ret)
588 break;
589 reg &= ~BIT(0);
590 ret = func->hif_write_reg(wilc, WILC_HOST_TX_CTRL, reg);
592 } while (0);
594 if (ret)
595 goto out_release_bus;
597 if (entries == 0) {
599 * No VMM space available in firmware so retry to transmit
600 * the packet from tx queue.
602 ret = WILC_VMM_ENTRY_FULL_RETRY;
603 goto out_release_bus;
606 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
608 offset = 0;
609 i = 0;
610 do {
611 u32 header, buffer_offset;
612 char *bssid;
614 tqe = wilc_wlan_txq_remove_from_head(dev);
615 if (!tqe)
616 break;
618 vif = tqe->vif;
619 if (vmm_table[i] == 0)
620 break;
622 le32_to_cpus(&vmm_table[i]);
623 vmm_sz = (vmm_table[i] & 0x3ff);
624 vmm_sz *= 4;
625 header = (tqe->type << 31) |
626 (tqe->buffer_size << 15) |
627 vmm_sz;
628 if (tqe->type == WILC_MGMT_PKT)
629 header |= BIT(30);
630 else
631 header &= ~BIT(30);
633 cpu_to_le32s(&header);
634 memcpy(&txb[offset], &header, 4);
635 if (tqe->type == WILC_CFG_PKT) {
636 buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
637 } else if (tqe->type == WILC_NET_PKT) {
638 bssid = tqe->vif->bssid;
639 buffer_offset = ETH_ETHERNET_HDR_OFFSET;
640 memcpy(&txb[offset + 8], bssid, 6);
641 } else {
642 buffer_offset = HOST_HDR_OFFSET;
645 memcpy(&txb[offset + buffer_offset],
646 tqe->buffer, tqe->buffer_size);
647 offset += vmm_sz;
648 i++;
649 tqe->status = 1;
650 if (tqe->tx_complete_func)
651 tqe->tx_complete_func(tqe->priv, tqe->status);
652 if (tqe->ack_idx != NOT_TCP_ACK &&
653 tqe->ack_idx < MAX_PENDING_ACKS)
654 vif->ack_filter.pending_acks[tqe->ack_idx].txqe = NULL;
655 kfree(tqe);
656 } while (--entries);
658 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
660 ret = func->hif_clear_int_ext(wilc, ENABLE_TX_VMM);
661 if (ret)
662 goto out_release_bus;
664 ret = func->hif_block_tx_ext(wilc, 0, txb, offset);
666 out_release_bus:
667 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
669 out_unlock:
670 mutex_unlock(&wilc->txq_add_to_head_cs);
672 out_update_cnt:
673 *txq_count = wilc->txq_entries;
674 return ret;
677 static void wilc_wlan_handle_rx_buff(struct wilc *wilc, u8 *buffer, int size)
679 int offset = 0;
680 u32 header;
681 u32 pkt_len, pkt_offset, tp_len;
682 int is_cfg_packet;
683 u8 *buff_ptr;
685 do {
686 buff_ptr = buffer + offset;
687 header = get_unaligned_le32(buff_ptr);
689 is_cfg_packet = (header >> 31) & 0x1;
690 pkt_offset = (header >> 22) & 0x1ff;
691 tp_len = (header >> 11) & 0x7ff;
692 pkt_len = header & 0x7ff;
694 if (pkt_len == 0 || tp_len == 0)
695 break;
697 if (pkt_offset & IS_MANAGMEMENT) {
698 buff_ptr += HOST_HDR_OFFSET;
699 wilc_wfi_mgmt_rx(wilc, buff_ptr, pkt_len);
700 } else {
701 if (!is_cfg_packet) {
702 if (pkt_len > 0) {
703 wilc_frmw_to_host(wilc, buff_ptr,
704 pkt_len, pkt_offset);
706 } else {
707 struct wilc_cfg_rsp rsp;
709 buff_ptr += pkt_offset;
711 wilc_wlan_cfg_indicate_rx(wilc, buff_ptr,
712 pkt_len,
713 &rsp);
714 if (rsp.type == WILC_CFG_RSP) {
715 if (wilc->cfg_seq_no == rsp.seq_no)
716 complete(&wilc->cfg_event);
717 } else if (rsp.type == WILC_CFG_RSP_STATUS) {
718 wilc_mac_indicate(wilc);
722 offset += tp_len;
723 } while (offset < size);
726 static void wilc_wlan_handle_rxq(struct wilc *wilc)
728 int size;
729 u8 *buffer;
730 struct rxq_entry_t *rqe;
732 while (!wilc->quit) {
733 rqe = wilc_wlan_rxq_remove(wilc);
734 if (!rqe)
735 break;
737 buffer = rqe->buffer;
738 size = rqe->buffer_size;
739 wilc_wlan_handle_rx_buff(wilc, buffer, size);
741 kfree(rqe);
743 if (wilc->quit)
744 complete(&wilc->cfg_event);
747 static void wilc_unknown_isr_ext(struct wilc *wilc)
749 wilc->hif_func->hif_clear_int_ext(wilc, 0);
752 static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
754 u32 offset = wilc->rx_buffer_offset;
755 u8 *buffer = NULL;
756 u32 size;
757 u32 retries = 0;
758 int ret = 0;
759 struct rxq_entry_t *rqe;
761 size = (int_status & 0x7fff) << 2;
763 while (!size && retries < 10) {
764 wilc->hif_func->hif_read_size(wilc, &size);
765 size = (size & 0x7fff) << 2;
766 retries++;
769 if (size <= 0)
770 return;
772 if (WILC_RX_BUFF_SIZE - offset < size)
773 offset = 0;
775 buffer = &wilc->rx_buffer[offset];
777 wilc->hif_func->hif_clear_int_ext(wilc, DATA_INT_CLR | ENABLE_RX_VMM);
778 ret = wilc->hif_func->hif_block_rx_ext(wilc, 0, buffer, size);
779 if (ret)
780 return;
782 offset += size;
783 wilc->rx_buffer_offset = offset;
784 rqe = kmalloc(sizeof(*rqe), GFP_KERNEL);
785 if (!rqe)
786 return;
788 rqe->buffer = buffer;
789 rqe->buffer_size = size;
790 wilc_wlan_rxq_add(wilc, rqe);
791 wilc_wlan_handle_rxq(wilc);
794 void wilc_handle_isr(struct wilc *wilc)
796 u32 int_status;
798 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
799 wilc->hif_func->hif_read_int(wilc, &int_status);
801 if (int_status & DATA_INT_EXT)
802 wilc_wlan_handle_isr_ext(wilc, int_status);
804 if (!(int_status & (ALL_INT_EXT)))
805 wilc_unknown_isr_ext(wilc);
807 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
809 EXPORT_SYMBOL_GPL(wilc_handle_isr);
811 int wilc_wlan_firmware_download(struct wilc *wilc, const u8 *buffer,
812 u32 buffer_size)
814 u32 offset;
815 u32 addr, size, size2, blksz;
816 u8 *dma_buffer;
817 int ret = 0;
819 blksz = BIT(12);
821 dma_buffer = kmalloc(blksz, GFP_KERNEL);
822 if (!dma_buffer)
823 return -EIO;
825 offset = 0;
826 do {
827 addr = get_unaligned_le32(&buffer[offset]);
828 size = get_unaligned_le32(&buffer[offset + 4]);
829 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
830 offset += 8;
831 while (((int)size) && (offset < buffer_size)) {
832 if (size <= blksz)
833 size2 = size;
834 else
835 size2 = blksz;
837 memcpy(dma_buffer, &buffer[offset], size2);
838 ret = wilc->hif_func->hif_block_tx(wilc, addr,
839 dma_buffer, size2);
840 if (ret)
841 break;
843 addr += size2;
844 offset += size2;
845 size -= size2;
847 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
849 if (ret)
850 goto fail;
851 } while (offset < buffer_size);
853 fail:
855 kfree(dma_buffer);
857 return ret;
860 int wilc_wlan_start(struct wilc *wilc)
862 u32 reg = 0;
863 int ret;
864 u32 chipid;
866 if (wilc->io_type == WILC_HIF_SDIO) {
867 reg = 0;
868 reg |= BIT(3);
869 } else if (wilc->io_type == WILC_HIF_SPI) {
870 reg = 1;
872 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
873 ret = wilc->hif_func->hif_write_reg(wilc, WILC_VMM_CORE_CFG, reg);
874 if (ret) {
875 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
876 return ret;
878 reg = 0;
879 if (wilc->io_type == WILC_HIF_SDIO && wilc->dev_irq_num)
880 reg |= WILC_HAVE_SDIO_IRQ_GPIO;
882 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_1, reg);
883 if (ret) {
884 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
885 return ret;
888 wilc->hif_func->hif_sync_ext(wilc, NUM_INT_EXT);
890 ret = wilc->hif_func->hif_read_reg(wilc, 0x1000, &chipid);
891 if (ret) {
892 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
893 return ret;
896 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
897 if ((reg & BIT(10)) == BIT(10)) {
898 reg &= ~BIT(10);
899 wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
900 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
903 reg |= BIT(10);
904 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GLB_RESET_0, reg);
905 wilc->hif_func->hif_read_reg(wilc, WILC_GLB_RESET_0, &reg);
906 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
908 return ret;
911 int wilc_wlan_stop(struct wilc *wilc, struct wilc_vif *vif)
913 u32 reg = 0;
914 int ret;
916 acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
918 ret = wilc->hif_func->hif_read_reg(wilc, WILC_GP_REG_0, &reg);
919 if (ret) {
920 netdev_err(vif->ndev, "Error while reading reg\n");
921 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
922 return ret;
925 ret = wilc->hif_func->hif_write_reg(wilc, WILC_GP_REG_0,
926 (reg | WILC_ABORT_REQ_BIT));
927 if (ret) {
928 netdev_err(vif->ndev, "Error while writing reg\n");
929 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
930 return ret;
933 ret = wilc->hif_func->hif_read_reg(wilc, WILC_FW_HOST_COMM, &reg);
934 if (ret) {
935 netdev_err(vif->ndev, "Error while reading reg\n");
936 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
937 return ret;
939 reg = BIT(0);
941 ret = wilc->hif_func->hif_write_reg(wilc, WILC_FW_HOST_COMM, reg);
942 if (ret) {
943 netdev_err(vif->ndev, "Error while writing reg\n");
944 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
945 return ret;
948 release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
950 return 0;
953 void wilc_wlan_cleanup(struct net_device *dev)
955 struct txq_entry_t *tqe;
956 struct rxq_entry_t *rqe;
957 struct wilc_vif *vif = netdev_priv(dev);
958 struct wilc *wilc = vif->wilc;
960 wilc->quit = 1;
961 while ((tqe = wilc_wlan_txq_remove_from_head(dev))) {
962 if (tqe->tx_complete_func)
963 tqe->tx_complete_func(tqe->priv, 0);
964 kfree(tqe);
967 while ((rqe = wilc_wlan_rxq_remove(wilc)))
968 kfree(rqe);
970 kfree(wilc->rx_buffer);
971 wilc->rx_buffer = NULL;
972 kfree(wilc->tx_buffer);
973 wilc->tx_buffer = NULL;
974 wilc->hif_func->hif_deinit(NULL);
977 static int wilc_wlan_cfg_commit(struct wilc_vif *vif, int type,
978 u32 drv_handler)
980 struct wilc *wilc = vif->wilc;
981 struct wilc_cfg_frame *cfg = &wilc->cfg_frame;
982 int t_len = wilc->cfg_frame_offset + sizeof(struct wilc_cfg_cmd_hdr);
984 if (type == WILC_CFG_SET)
985 cfg->hdr.cmd_type = 'W';
986 else
987 cfg->hdr.cmd_type = 'Q';
989 cfg->hdr.seq_no = wilc->cfg_seq_no % 256;
990 cfg->hdr.total_len = cpu_to_le16(t_len);
991 cfg->hdr.driver_handler = cpu_to_le32(drv_handler);
992 wilc->cfg_seq_no = cfg->hdr.seq_no;
994 if (!wilc_wlan_txq_add_cfg_pkt(vif, (u8 *)&cfg->hdr, t_len))
995 return -1;
997 return 0;
1000 int wilc_wlan_cfg_set(struct wilc_vif *vif, int start, u16 wid, u8 *buffer,
1001 u32 buffer_size, int commit, u32 drv_handler)
1003 u32 offset;
1004 int ret_size;
1005 struct wilc *wilc = vif->wilc;
1007 mutex_lock(&wilc->cfg_cmd_lock);
1009 if (start)
1010 wilc->cfg_frame_offset = 0;
1012 offset = wilc->cfg_frame_offset;
1013 ret_size = wilc_wlan_cfg_set_wid(wilc->cfg_frame.frame, offset,
1014 wid, buffer, buffer_size);
1015 offset += ret_size;
1016 wilc->cfg_frame_offset = offset;
1018 if (!commit) {
1019 mutex_unlock(&wilc->cfg_cmd_lock);
1020 return ret_size;
1023 netdev_dbg(vif->ndev, "%s: seqno[%d]\n", __func__, wilc->cfg_seq_no);
1025 if (wilc_wlan_cfg_commit(vif, WILC_CFG_SET, drv_handler))
1026 ret_size = 0;
1028 if (!wait_for_completion_timeout(&wilc->cfg_event,
1029 WILC_CFG_PKTS_TIMEOUT)) {
1030 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1031 ret_size = 0;
1034 wilc->cfg_frame_offset = 0;
1035 wilc->cfg_seq_no += 1;
1036 mutex_unlock(&wilc->cfg_cmd_lock);
1038 return ret_size;
1041 int wilc_wlan_cfg_get(struct wilc_vif *vif, int start, u16 wid, int commit,
1042 u32 drv_handler)
1044 u32 offset;
1045 int ret_size;
1046 struct wilc *wilc = vif->wilc;
1048 mutex_lock(&wilc->cfg_cmd_lock);
1050 if (start)
1051 wilc->cfg_frame_offset = 0;
1053 offset = wilc->cfg_frame_offset;
1054 ret_size = wilc_wlan_cfg_get_wid(wilc->cfg_frame.frame, offset, wid);
1055 offset += ret_size;
1056 wilc->cfg_frame_offset = offset;
1058 if (!commit) {
1059 mutex_unlock(&wilc->cfg_cmd_lock);
1060 return ret_size;
1063 if (wilc_wlan_cfg_commit(vif, WILC_CFG_QUERY, drv_handler))
1064 ret_size = 0;
1066 if (!wait_for_completion_timeout(&wilc->cfg_event,
1067 WILC_CFG_PKTS_TIMEOUT)) {
1068 netdev_dbg(vif->ndev, "%s: Timed Out\n", __func__);
1069 ret_size = 0;
1071 wilc->cfg_frame_offset = 0;
1072 wilc->cfg_seq_no += 1;
1073 mutex_unlock(&wilc->cfg_cmd_lock);
1075 return ret_size;
1078 int wilc_send_config_pkt(struct wilc_vif *vif, u8 mode, struct wid *wids,
1079 u32 count)
1081 int i;
1082 int ret = 0;
1083 u32 drv = wilc_get_vif_idx(vif);
1085 if (mode == WILC_GET_CFG) {
1086 for (i = 0; i < count; i++) {
1087 if (!wilc_wlan_cfg_get(vif, !i,
1088 wids[i].id,
1089 (i == count - 1),
1090 drv)) {
1091 ret = -ETIMEDOUT;
1092 break;
1095 for (i = 0; i < count; i++) {
1096 wids[i].size = wilc_wlan_cfg_get_val(vif->wilc,
1097 wids[i].id,
1098 wids[i].val,
1099 wids[i].size);
1101 } else if (mode == WILC_SET_CFG) {
1102 for (i = 0; i < count; i++) {
1103 if (!wilc_wlan_cfg_set(vif, !i,
1104 wids[i].id,
1105 wids[i].val,
1106 wids[i].size,
1107 (i == count - 1),
1108 drv)) {
1109 ret = -ETIMEDOUT;
1110 break;
1115 return ret;
1118 static int init_chip(struct net_device *dev)
1120 u32 chipid;
1121 u32 reg;
1122 int ret = 0;
1123 struct wilc_vif *vif = netdev_priv(dev);
1124 struct wilc *wilc = vif->wilc;
1126 acquire_bus(wilc, WILC_BUS_ACQUIRE_ONLY);
1128 chipid = wilc_get_chipid(wilc, true);
1130 if ((chipid & 0xfff) != 0xa0) {
1131 ret = wilc->hif_func->hif_read_reg(wilc, 0x1118, &reg);
1132 if (ret) {
1133 netdev_err(dev, "fail read reg 0x1118\n");
1134 goto release;
1136 reg |= BIT(0);
1137 ret = wilc->hif_func->hif_write_reg(wilc, 0x1118, reg);
1138 if (ret) {
1139 netdev_err(dev, "fail write reg 0x1118\n");
1140 goto release;
1142 ret = wilc->hif_func->hif_write_reg(wilc, 0xc0000, 0x71);
1143 if (ret) {
1144 netdev_err(dev, "fail write reg 0xc0000\n");
1145 goto release;
1149 release:
1150 release_bus(wilc, WILC_BUS_RELEASE_ONLY);
1152 return ret;
1155 u32 wilc_get_chipid(struct wilc *wilc, bool update)
1157 static u32 chipid;
1158 u32 tempchipid = 0;
1159 u32 rfrevid = 0;
1161 if (chipid == 0 || update) {
1162 wilc->hif_func->hif_read_reg(wilc, 0x1000, &tempchipid);
1163 wilc->hif_func->hif_read_reg(wilc, 0x13f4, &rfrevid);
1164 if (!is_wilc1000(tempchipid)) {
1165 chipid = 0;
1166 return chipid;
1168 if (tempchipid == 0x1002a0) {
1169 if (rfrevid != 0x1)
1170 tempchipid = 0x1002a1;
1171 } else if (tempchipid == 0x1002b0) {
1172 if (rfrevid == 0x4)
1173 tempchipid = 0x1002b1;
1174 else if (rfrevid != 0x3)
1175 tempchipid = 0x1002b2;
1178 chipid = tempchipid;
1180 return chipid;
1183 int wilc_wlan_init(struct net_device *dev)
1185 int ret = 0;
1186 struct wilc_vif *vif = netdev_priv(dev);
1187 struct wilc *wilc;
1189 wilc = vif->wilc;
1191 wilc->quit = 0;
1193 if (wilc->hif_func->hif_init(wilc, false)) {
1194 ret = -EIO;
1195 goto fail;
1198 if (!wilc->tx_buffer)
1199 wilc->tx_buffer = kmalloc(WILC_TX_BUFF_SIZE, GFP_KERNEL);
1201 if (!wilc->tx_buffer) {
1202 ret = -ENOBUFS;
1203 goto fail;
1206 if (!wilc->rx_buffer)
1207 wilc->rx_buffer = kmalloc(WILC_RX_BUFF_SIZE, GFP_KERNEL);
1209 if (!wilc->rx_buffer) {
1210 ret = -ENOBUFS;
1211 goto fail;
1214 if (init_chip(dev)) {
1215 ret = -EIO;
1216 goto fail;
1219 return 0;
1221 fail:
1223 kfree(wilc->rx_buffer);
1224 wilc->rx_buffer = NULL;
1225 kfree(wilc->tx_buffer);
1226 wilc->tx_buffer = NULL;
1228 return ret;