1*2108df3cSPrameela Rani Garnepudi /** 2*2108df3cSPrameela Rani Garnepudi * Copyright (c) 2018 Redpine Signals Inc. 3*2108df3cSPrameela Rani Garnepudi * 4*2108df3cSPrameela Rani Garnepudi * Permission to use, copy, modify, and/or distribute this software for any 5*2108df3cSPrameela Rani Garnepudi * purpose with or without fee is hereby granted, provided that the above 6*2108df3cSPrameela Rani Garnepudi * copyright notice and this permission notice appear in all copies. 7*2108df3cSPrameela Rani Garnepudi * 8*2108df3cSPrameela Rani Garnepudi * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9*2108df3cSPrameela Rani Garnepudi * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10*2108df3cSPrameela Rani Garnepudi * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11*2108df3cSPrameela Rani Garnepudi * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12*2108df3cSPrameela Rani Garnepudi * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13*2108df3cSPrameela Rani Garnepudi * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14*2108df3cSPrameela Rani Garnepudi * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15*2108df3cSPrameela Rani Garnepudi */ 16*2108df3cSPrameela Rani Garnepudi 17*2108df3cSPrameela Rani Garnepudi #include "rsi_main.h" 18*2108df3cSPrameela Rani Garnepudi #include "rsi_coex.h" 19*2108df3cSPrameela Rani Garnepudi #include "rsi_mgmt.h" 20*2108df3cSPrameela Rani Garnepudi #include "rsi_hal.h" 21*2108df3cSPrameela Rani Garnepudi 22*2108df3cSPrameela Rani Garnepudi static enum rsi_coex_queues rsi_coex_determine_coex_q 23*2108df3cSPrameela Rani Garnepudi (struct rsi_coex_ctrl_block *coex_cb) 24*2108df3cSPrameela Rani Garnepudi { 25*2108df3cSPrameela Rani Garnepudi enum rsi_coex_queues q_num = RSI_COEX_Q_INVALID; 26*2108df3cSPrameela Rani Garnepudi 27*2108df3cSPrameela Rani Garnepudi if (skb_queue_len(&coex_cb->coex_tx_qs[RSI_COEX_Q_COMMON]) > 0) 28*2108df3cSPrameela Rani Garnepudi q_num = RSI_COEX_Q_COMMON; 29*2108df3cSPrameela Rani Garnepudi if (skb_queue_len(&coex_cb->coex_tx_qs[RSI_COEX_Q_BT]) > 0) 30*2108df3cSPrameela Rani Garnepudi q_num = RSI_COEX_Q_BT; 31*2108df3cSPrameela Rani Garnepudi if (skb_queue_len(&coex_cb->coex_tx_qs[RSI_COEX_Q_WLAN]) > 0) 32*2108df3cSPrameela Rani Garnepudi q_num = RSI_COEX_Q_WLAN; 33*2108df3cSPrameela Rani Garnepudi 34*2108df3cSPrameela Rani Garnepudi return q_num; 35*2108df3cSPrameela Rani Garnepudi } 36*2108df3cSPrameela Rani Garnepudi 37*2108df3cSPrameela Rani Garnepudi static void rsi_coex_sched_tx_pkts(struct rsi_coex_ctrl_block *coex_cb) 38*2108df3cSPrameela Rani Garnepudi { 39*2108df3cSPrameela Rani Garnepudi enum rsi_coex_queues coex_q = RSI_COEX_Q_INVALID; 40*2108df3cSPrameela Rani Garnepudi struct sk_buff *skb; 41*2108df3cSPrameela Rani Garnepudi 42*2108df3cSPrameela Rani Garnepudi do { 43*2108df3cSPrameela Rani Garnepudi coex_q = rsi_coex_determine_coex_q(coex_cb); 44*2108df3cSPrameela Rani Garnepudi rsi_dbg(INFO_ZONE, "queue = %d\n", coex_q); 45*2108df3cSPrameela Rani Garnepudi 46*2108df3cSPrameela Rani Garnepudi if (coex_q == RSI_COEX_Q_BT) 47*2108df3cSPrameela Rani Garnepudi skb = skb_dequeue(&coex_cb->coex_tx_qs[RSI_COEX_Q_BT]); 48*2108df3cSPrameela Rani Garnepudi } while (coex_q != RSI_COEX_Q_INVALID); 49*2108df3cSPrameela Rani Garnepudi } 50*2108df3cSPrameela Rani Garnepudi 51*2108df3cSPrameela Rani Garnepudi static void rsi_coex_scheduler_thread(struct rsi_common *common) 52*2108df3cSPrameela Rani Garnepudi { 53*2108df3cSPrameela Rani Garnepudi struct rsi_coex_ctrl_block *coex_cb = 54*2108df3cSPrameela Rani Garnepudi (struct rsi_coex_ctrl_block *)common->coex_cb; 55*2108df3cSPrameela Rani Garnepudi u32 timeout = EVENT_WAIT_FOREVER; 56*2108df3cSPrameela Rani Garnepudi 57*2108df3cSPrameela Rani Garnepudi do { 58*2108df3cSPrameela Rani Garnepudi rsi_wait_event(&coex_cb->coex_tx_thread.event, timeout); 59*2108df3cSPrameela Rani Garnepudi rsi_reset_event(&coex_cb->coex_tx_thread.event); 60*2108df3cSPrameela Rani Garnepudi 61*2108df3cSPrameela Rani Garnepudi rsi_coex_sched_tx_pkts(coex_cb); 62*2108df3cSPrameela Rani Garnepudi } while (atomic_read(&coex_cb->coex_tx_thread.thread_done) == 0); 63*2108df3cSPrameela Rani Garnepudi 64*2108df3cSPrameela Rani Garnepudi complete_and_exit(&coex_cb->coex_tx_thread.completion, 0); 65*2108df3cSPrameela Rani Garnepudi } 66*2108df3cSPrameela Rani Garnepudi 67*2108df3cSPrameela Rani Garnepudi int rsi_coex_recv_pkt(struct rsi_common *common, u8 *msg) 68*2108df3cSPrameela Rani Garnepudi { 69*2108df3cSPrameela Rani Garnepudi u8 msg_type = msg[RSI_RX_DESC_MSG_TYPE_OFFSET]; 70*2108df3cSPrameela Rani Garnepudi 71*2108df3cSPrameela Rani Garnepudi switch (msg_type) { 72*2108df3cSPrameela Rani Garnepudi case COMMON_CARD_READY_IND: 73*2108df3cSPrameela Rani Garnepudi rsi_dbg(INFO_ZONE, "common card ready received\n"); 74*2108df3cSPrameela Rani Garnepudi rsi_handle_card_ready(common, msg); 75*2108df3cSPrameela Rani Garnepudi break; 76*2108df3cSPrameela Rani Garnepudi case SLEEP_NOTIFY_IND: 77*2108df3cSPrameela Rani Garnepudi rsi_dbg(INFO_ZONE, "sleep notify received\n"); 78*2108df3cSPrameela Rani Garnepudi rsi_mgmt_pkt_recv(common, msg); 79*2108df3cSPrameela Rani Garnepudi break; 80*2108df3cSPrameela Rani Garnepudi } 81*2108df3cSPrameela Rani Garnepudi 82*2108df3cSPrameela Rani Garnepudi return 0; 83*2108df3cSPrameela Rani Garnepudi } 84*2108df3cSPrameela Rani Garnepudi 85*2108df3cSPrameela Rani Garnepudi static inline int rsi_map_coex_q(u8 hal_queue) 86*2108df3cSPrameela Rani Garnepudi { 87*2108df3cSPrameela Rani Garnepudi switch (hal_queue) { 88*2108df3cSPrameela Rani Garnepudi case RSI_COEX_Q: 89*2108df3cSPrameela Rani Garnepudi return RSI_COEX_Q_COMMON; 90*2108df3cSPrameela Rani Garnepudi case RSI_WLAN_Q: 91*2108df3cSPrameela Rani Garnepudi return RSI_COEX_Q_WLAN; 92*2108df3cSPrameela Rani Garnepudi case RSI_BT_Q: 93*2108df3cSPrameela Rani Garnepudi return RSI_COEX_Q_BT; 94*2108df3cSPrameela Rani Garnepudi } 95*2108df3cSPrameela Rani Garnepudi return RSI_COEX_Q_INVALID; 96*2108df3cSPrameela Rani Garnepudi } 97*2108df3cSPrameela Rani Garnepudi 98*2108df3cSPrameela Rani Garnepudi int rsi_coex_send_pkt(void *priv, struct sk_buff *skb, u8 hal_queue) 99*2108df3cSPrameela Rani Garnepudi { 100*2108df3cSPrameela Rani Garnepudi struct rsi_common *common = (struct rsi_common *)priv; 101*2108df3cSPrameela Rani Garnepudi struct rsi_coex_ctrl_block *coex_cb = 102*2108df3cSPrameela Rani Garnepudi (struct rsi_coex_ctrl_block *)common->coex_cb; 103*2108df3cSPrameela Rani Garnepudi struct skb_info *tx_params = NULL; 104*2108df3cSPrameela Rani Garnepudi enum rsi_coex_queues coex_q; 105*2108df3cSPrameela Rani Garnepudi int status; 106*2108df3cSPrameela Rani Garnepudi 107*2108df3cSPrameela Rani Garnepudi coex_q = rsi_map_coex_q(hal_queue); 108*2108df3cSPrameela Rani Garnepudi if (coex_q == RSI_COEX_Q_INVALID) { 109*2108df3cSPrameela Rani Garnepudi rsi_dbg(ERR_ZONE, "Invalid coex queue\n"); 110*2108df3cSPrameela Rani Garnepudi return -EINVAL; 111*2108df3cSPrameela Rani Garnepudi } 112*2108df3cSPrameela Rani Garnepudi if (coex_q != RSI_COEX_Q_COMMON && 113*2108df3cSPrameela Rani Garnepudi coex_q != RSI_COEX_Q_WLAN) { 114*2108df3cSPrameela Rani Garnepudi skb_queue_tail(&coex_cb->coex_tx_qs[coex_q], skb); 115*2108df3cSPrameela Rani Garnepudi rsi_set_event(&coex_cb->coex_tx_thread.event); 116*2108df3cSPrameela Rani Garnepudi return 0; 117*2108df3cSPrameela Rani Garnepudi } 118*2108df3cSPrameela Rani Garnepudi if (common->iface_down) { 119*2108df3cSPrameela Rani Garnepudi tx_params = 120*2108df3cSPrameela Rani Garnepudi (struct skb_info *)&IEEE80211_SKB_CB(skb)->driver_data; 121*2108df3cSPrameela Rani Garnepudi 122*2108df3cSPrameela Rani Garnepudi if (!(tx_params->flags & INTERNAL_MGMT_PKT)) { 123*2108df3cSPrameela Rani Garnepudi rsi_indicate_tx_status(common->priv, skb, -EINVAL); 124*2108df3cSPrameela Rani Garnepudi return 0; 125*2108df3cSPrameela Rani Garnepudi } 126*2108df3cSPrameela Rani Garnepudi } 127*2108df3cSPrameela Rani Garnepudi 128*2108df3cSPrameela Rani Garnepudi /* Send packet to hal */ 129*2108df3cSPrameela Rani Garnepudi if (skb->priority == MGMT_SOFT_Q) 130*2108df3cSPrameela Rani Garnepudi status = rsi_send_mgmt_pkt(common, skb); 131*2108df3cSPrameela Rani Garnepudi else 132*2108df3cSPrameela Rani Garnepudi status = rsi_send_data_pkt(common, skb); 133*2108df3cSPrameela Rani Garnepudi 134*2108df3cSPrameela Rani Garnepudi return status; 135*2108df3cSPrameela Rani Garnepudi } 136*2108df3cSPrameela Rani Garnepudi 137*2108df3cSPrameela Rani Garnepudi int rsi_coex_attach(struct rsi_common *common) 138*2108df3cSPrameela Rani Garnepudi { 139*2108df3cSPrameela Rani Garnepudi struct rsi_coex_ctrl_block *coex_cb; 140*2108df3cSPrameela Rani Garnepudi int cnt; 141*2108df3cSPrameela Rani Garnepudi 142*2108df3cSPrameela Rani Garnepudi coex_cb = kzalloc(sizeof(*coex_cb), GFP_KERNEL); 143*2108df3cSPrameela Rani Garnepudi if (!coex_cb) 144*2108df3cSPrameela Rani Garnepudi return -ENOMEM; 145*2108df3cSPrameela Rani Garnepudi 146*2108df3cSPrameela Rani Garnepudi common->coex_cb = (void *)coex_cb; 147*2108df3cSPrameela Rani Garnepudi coex_cb->priv = common; 148*2108df3cSPrameela Rani Garnepudi 149*2108df3cSPrameela Rani Garnepudi /* Initialize co-ex queues */ 150*2108df3cSPrameela Rani Garnepudi for (cnt = 0; cnt < NUM_COEX_TX_QUEUES; cnt++) 151*2108df3cSPrameela Rani Garnepudi skb_queue_head_init(&coex_cb->coex_tx_qs[cnt]); 152*2108df3cSPrameela Rani Garnepudi rsi_init_event(&coex_cb->coex_tx_thread.event); 153*2108df3cSPrameela Rani Garnepudi 154*2108df3cSPrameela Rani Garnepudi /* Initialize co-ex thread */ 155*2108df3cSPrameela Rani Garnepudi if (rsi_create_kthread(common, 156*2108df3cSPrameela Rani Garnepudi &coex_cb->coex_tx_thread, 157*2108df3cSPrameela Rani Garnepudi rsi_coex_scheduler_thread, 158*2108df3cSPrameela Rani Garnepudi "Coex-Tx-Thread")) { 159*2108df3cSPrameela Rani Garnepudi rsi_dbg(ERR_ZONE, "%s: Unable to init tx thrd\n", __func__); 160*2108df3cSPrameela Rani Garnepudi return -EINVAL; 161*2108df3cSPrameela Rani Garnepudi } 162*2108df3cSPrameela Rani Garnepudi return 0; 163*2108df3cSPrameela Rani Garnepudi } 164*2108df3cSPrameela Rani Garnepudi 165*2108df3cSPrameela Rani Garnepudi void rsi_coex_detach(struct rsi_common *common) 166*2108df3cSPrameela Rani Garnepudi { 167*2108df3cSPrameela Rani Garnepudi struct rsi_coex_ctrl_block *coex_cb = 168*2108df3cSPrameela Rani Garnepudi (struct rsi_coex_ctrl_block *)common->coex_cb; 169*2108df3cSPrameela Rani Garnepudi int cnt; 170*2108df3cSPrameela Rani Garnepudi 171*2108df3cSPrameela Rani Garnepudi rsi_kill_thread(&coex_cb->coex_tx_thread); 172*2108df3cSPrameela Rani Garnepudi 173*2108df3cSPrameela Rani Garnepudi for (cnt = 0; cnt < NUM_COEX_TX_QUEUES; cnt++) 174*2108df3cSPrameela Rani Garnepudi skb_queue_purge(&coex_cb->coex_tx_qs[cnt]); 175*2108df3cSPrameela Rani Garnepudi 176*2108df3cSPrameela Rani Garnepudi kfree(coex_cb); 177*2108df3cSPrameela Rani Garnepudi } 178