198f44cb0SIgor Mitsyanko /* 298f44cb0SIgor Mitsyanko * Copyright (c) 2015-2016 Quantenna Communications, Inc. 398f44cb0SIgor Mitsyanko * All rights reserved. 498f44cb0SIgor Mitsyanko * 598f44cb0SIgor Mitsyanko * This program is free software; you can redistribute it and/or 698f44cb0SIgor Mitsyanko * modify it under the terms of the GNU General Public License 798f44cb0SIgor Mitsyanko * as published by the Free Software Foundation; either version 2 898f44cb0SIgor Mitsyanko * of the License, or (at your option) any later version. 998f44cb0SIgor Mitsyanko * 1098f44cb0SIgor Mitsyanko * This program is distributed in the hope that it will be useful, 1198f44cb0SIgor Mitsyanko * but WITHOUT ANY WARRANTY; without even the implied warranty of 1298f44cb0SIgor Mitsyanko * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1398f44cb0SIgor Mitsyanko * GNU General Public License for more details. 1498f44cb0SIgor Mitsyanko * 1598f44cb0SIgor Mitsyanko */ 1698f44cb0SIgor Mitsyanko 1798f44cb0SIgor Mitsyanko #include <linux/types.h> 1898f44cb0SIgor Mitsyanko #include <linux/export.h> 1998f44cb0SIgor Mitsyanko #include <linux/slab.h> 2098f44cb0SIgor Mitsyanko 2198f44cb0SIgor Mitsyanko #include "core.h" 2298f44cb0SIgor Mitsyanko #include "commands.h" 2398f44cb0SIgor Mitsyanko #include "event.h" 2498f44cb0SIgor Mitsyanko #include "bus.h" 2598f44cb0SIgor Mitsyanko 2698f44cb0SIgor Mitsyanko #define QTNF_DEF_SYNC_CMD_TIMEOUT (5 * HZ) 2798f44cb0SIgor Mitsyanko 2898f44cb0SIgor Mitsyanko int qtnf_trans_send_cmd_with_resp(struct qtnf_bus *bus, struct sk_buff *cmd_skb, 2998f44cb0SIgor Mitsyanko struct sk_buff **response_skb) 3098f44cb0SIgor Mitsyanko { 3198f44cb0SIgor Mitsyanko struct qtnf_cmd_ctl_node *ctl_node = &bus->trans.curr_cmd; 3298f44cb0SIgor Mitsyanko struct qlink_cmd *cmd = (void *)cmd_skb->data; 3398f44cb0SIgor Mitsyanko int ret = 0; 3498f44cb0SIgor Mitsyanko long status; 3598f44cb0SIgor Mitsyanko bool resp_not_handled = true; 3698f44cb0SIgor Mitsyanko struct sk_buff *resp_skb = NULL; 3798f44cb0SIgor Mitsyanko 38*b60769e2SDmitry Lebed if (unlikely(!response_skb)) { 39*b60769e2SDmitry Lebed dev_kfree_skb(cmd_skb); 4098f44cb0SIgor Mitsyanko return -EFAULT; 41*b60769e2SDmitry Lebed } 4298f44cb0SIgor Mitsyanko 4398f44cb0SIgor Mitsyanko spin_lock(&ctl_node->resp_lock); 4498f44cb0SIgor Mitsyanko ctl_node->seq_num++; 4598f44cb0SIgor Mitsyanko cmd->seq_num = cpu_to_le16(ctl_node->seq_num); 4698f44cb0SIgor Mitsyanko WARN(ctl_node->resp_skb, "qtnfmac: response skb not empty\n"); 4798f44cb0SIgor Mitsyanko ctl_node->waiting_for_resp = true; 4898f44cb0SIgor Mitsyanko spin_unlock(&ctl_node->resp_lock); 4998f44cb0SIgor Mitsyanko 5098f44cb0SIgor Mitsyanko ret = qtnf_bus_control_tx(bus, cmd_skb); 5198f44cb0SIgor Mitsyanko dev_kfree_skb(cmd_skb); 5298f44cb0SIgor Mitsyanko 5398f44cb0SIgor Mitsyanko if (unlikely(ret)) 5498f44cb0SIgor Mitsyanko goto out; 5598f44cb0SIgor Mitsyanko 5698f44cb0SIgor Mitsyanko status = wait_for_completion_interruptible_timeout( 5798f44cb0SIgor Mitsyanko &ctl_node->cmd_resp_completion, 5898f44cb0SIgor Mitsyanko QTNF_DEF_SYNC_CMD_TIMEOUT); 5998f44cb0SIgor Mitsyanko 6098f44cb0SIgor Mitsyanko spin_lock(&ctl_node->resp_lock); 6198f44cb0SIgor Mitsyanko resp_not_handled = ctl_node->waiting_for_resp; 6298f44cb0SIgor Mitsyanko resp_skb = ctl_node->resp_skb; 6398f44cb0SIgor Mitsyanko ctl_node->resp_skb = NULL; 6498f44cb0SIgor Mitsyanko ctl_node->waiting_for_resp = false; 6598f44cb0SIgor Mitsyanko spin_unlock(&ctl_node->resp_lock); 6698f44cb0SIgor Mitsyanko 6798f44cb0SIgor Mitsyanko if (unlikely(status <= 0)) { 6898f44cb0SIgor Mitsyanko if (status == 0) { 6998f44cb0SIgor Mitsyanko ret = -ETIMEDOUT; 7098f44cb0SIgor Mitsyanko pr_err("response timeout\n"); 7198f44cb0SIgor Mitsyanko } else { 7298f44cb0SIgor Mitsyanko ret = -EINTR; 7398f44cb0SIgor Mitsyanko pr_debug("interrupted\n"); 7498f44cb0SIgor Mitsyanko } 7598f44cb0SIgor Mitsyanko } 7698f44cb0SIgor Mitsyanko 7798f44cb0SIgor Mitsyanko if (unlikely(!resp_skb || resp_not_handled)) { 7898f44cb0SIgor Mitsyanko if (!ret) 7998f44cb0SIgor Mitsyanko ret = -EFAULT; 8098f44cb0SIgor Mitsyanko 8198f44cb0SIgor Mitsyanko goto out; 8298f44cb0SIgor Mitsyanko } 8398f44cb0SIgor Mitsyanko 8498f44cb0SIgor Mitsyanko ret = 0; 8598f44cb0SIgor Mitsyanko *response_skb = resp_skb; 8698f44cb0SIgor Mitsyanko 8798f44cb0SIgor Mitsyanko out: 8898f44cb0SIgor Mitsyanko if (unlikely(resp_skb && resp_not_handled)) 8998f44cb0SIgor Mitsyanko dev_kfree_skb(resp_skb); 9098f44cb0SIgor Mitsyanko 9198f44cb0SIgor Mitsyanko return ret; 9298f44cb0SIgor Mitsyanko } 9398f44cb0SIgor Mitsyanko 9498f44cb0SIgor Mitsyanko static void qtnf_trans_signal_cmdresp(struct qtnf_bus *bus, struct sk_buff *skb) 9598f44cb0SIgor Mitsyanko { 9698f44cb0SIgor Mitsyanko struct qtnf_cmd_ctl_node *ctl_node = &bus->trans.curr_cmd; 9798f44cb0SIgor Mitsyanko const struct qlink_resp *resp = (const struct qlink_resp *)skb->data; 9898f44cb0SIgor Mitsyanko const u16 recvd_seq_num = le16_to_cpu(resp->seq_num); 9998f44cb0SIgor Mitsyanko 10098f44cb0SIgor Mitsyanko spin_lock(&ctl_node->resp_lock); 10198f44cb0SIgor Mitsyanko 10298f44cb0SIgor Mitsyanko if (unlikely(!ctl_node->waiting_for_resp)) { 10398f44cb0SIgor Mitsyanko pr_err("unexpected response\n"); 10498f44cb0SIgor Mitsyanko goto out_err; 10598f44cb0SIgor Mitsyanko } 10698f44cb0SIgor Mitsyanko 10798f44cb0SIgor Mitsyanko if (unlikely(recvd_seq_num != ctl_node->seq_num)) { 10898f44cb0SIgor Mitsyanko pr_err("seq num mismatch\n"); 10998f44cb0SIgor Mitsyanko goto out_err; 11098f44cb0SIgor Mitsyanko } 11198f44cb0SIgor Mitsyanko 11298f44cb0SIgor Mitsyanko ctl_node->resp_skb = skb; 11398f44cb0SIgor Mitsyanko ctl_node->waiting_for_resp = false; 11498f44cb0SIgor Mitsyanko 11598f44cb0SIgor Mitsyanko spin_unlock(&ctl_node->resp_lock); 11698f44cb0SIgor Mitsyanko 11798f44cb0SIgor Mitsyanko complete(&ctl_node->cmd_resp_completion); 11898f44cb0SIgor Mitsyanko return; 11998f44cb0SIgor Mitsyanko 12098f44cb0SIgor Mitsyanko out_err: 12198f44cb0SIgor Mitsyanko spin_unlock(&ctl_node->resp_lock); 12298f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 12398f44cb0SIgor Mitsyanko } 12498f44cb0SIgor Mitsyanko 12598f44cb0SIgor Mitsyanko static int qtnf_trans_event_enqueue(struct qtnf_bus *bus, struct sk_buff *skb) 12698f44cb0SIgor Mitsyanko { 12798f44cb0SIgor Mitsyanko struct qtnf_qlink_transport *trans = &bus->trans; 12898f44cb0SIgor Mitsyanko 12998f44cb0SIgor Mitsyanko if (likely(skb_queue_len(&trans->event_queue) < 13098f44cb0SIgor Mitsyanko trans->event_queue_max_len)) { 13198f44cb0SIgor Mitsyanko skb_queue_tail(&trans->event_queue, skb); 13298f44cb0SIgor Mitsyanko queue_work(bus->workqueue, &bus->event_work); 13398f44cb0SIgor Mitsyanko } else { 13498f44cb0SIgor Mitsyanko pr_warn("event dropped due to queue overflow\n"); 13598f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 13698f44cb0SIgor Mitsyanko return -1; 13798f44cb0SIgor Mitsyanko } 13898f44cb0SIgor Mitsyanko 13998f44cb0SIgor Mitsyanko return 0; 14098f44cb0SIgor Mitsyanko } 14198f44cb0SIgor Mitsyanko 14298f44cb0SIgor Mitsyanko void qtnf_trans_init(struct qtnf_bus *bus) 14398f44cb0SIgor Mitsyanko { 14498f44cb0SIgor Mitsyanko struct qtnf_qlink_transport *trans = &bus->trans; 14598f44cb0SIgor Mitsyanko 14698f44cb0SIgor Mitsyanko init_completion(&trans->curr_cmd.cmd_resp_completion); 14798f44cb0SIgor Mitsyanko spin_lock_init(&trans->curr_cmd.resp_lock); 14898f44cb0SIgor Mitsyanko 14998f44cb0SIgor Mitsyanko spin_lock(&trans->curr_cmd.resp_lock); 15098f44cb0SIgor Mitsyanko trans->curr_cmd.seq_num = 0; 15198f44cb0SIgor Mitsyanko trans->curr_cmd.waiting_for_resp = false; 15298f44cb0SIgor Mitsyanko trans->curr_cmd.resp_skb = NULL; 15398f44cb0SIgor Mitsyanko spin_unlock(&trans->curr_cmd.resp_lock); 15498f44cb0SIgor Mitsyanko 15598f44cb0SIgor Mitsyanko /* Init event handling related fields */ 15698f44cb0SIgor Mitsyanko skb_queue_head_init(&trans->event_queue); 15798f44cb0SIgor Mitsyanko trans->event_queue_max_len = QTNF_MAX_EVENT_QUEUE_LEN; 15898f44cb0SIgor Mitsyanko } 15998f44cb0SIgor Mitsyanko 16098f44cb0SIgor Mitsyanko static void qtnf_trans_free_events(struct qtnf_bus *bus) 16198f44cb0SIgor Mitsyanko { 16298f44cb0SIgor Mitsyanko struct sk_buff_head *event_queue = &bus->trans.event_queue; 16398f44cb0SIgor Mitsyanko struct sk_buff *current_event_skb = skb_dequeue(event_queue); 16498f44cb0SIgor Mitsyanko 16598f44cb0SIgor Mitsyanko while (current_event_skb) { 16698f44cb0SIgor Mitsyanko dev_kfree_skb_any(current_event_skb); 16798f44cb0SIgor Mitsyanko current_event_skb = skb_dequeue(event_queue); 16898f44cb0SIgor Mitsyanko } 16998f44cb0SIgor Mitsyanko } 17098f44cb0SIgor Mitsyanko 17198f44cb0SIgor Mitsyanko void qtnf_trans_free(struct qtnf_bus *bus) 17298f44cb0SIgor Mitsyanko { 17398f44cb0SIgor Mitsyanko if (!bus) { 17498f44cb0SIgor Mitsyanko pr_err("invalid bus pointer\n"); 17598f44cb0SIgor Mitsyanko return; 17698f44cb0SIgor Mitsyanko } 17798f44cb0SIgor Mitsyanko 17898f44cb0SIgor Mitsyanko qtnf_trans_free_events(bus); 17998f44cb0SIgor Mitsyanko } 18098f44cb0SIgor Mitsyanko 18198f44cb0SIgor Mitsyanko int qtnf_trans_handle_rx_ctl_packet(struct qtnf_bus *bus, struct sk_buff *skb) 18298f44cb0SIgor Mitsyanko { 18398f44cb0SIgor Mitsyanko const struct qlink_msg_header *header = (void *)skb->data; 18498f44cb0SIgor Mitsyanko int ret = -1; 18598f44cb0SIgor Mitsyanko 18698f44cb0SIgor Mitsyanko if (unlikely(skb->len < sizeof(*header))) { 18798f44cb0SIgor Mitsyanko pr_warn("packet is too small: %u\n", skb->len); 18898f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 18998f44cb0SIgor Mitsyanko return -EINVAL; 19098f44cb0SIgor Mitsyanko } 19198f44cb0SIgor Mitsyanko 19298f44cb0SIgor Mitsyanko if (unlikely(skb->len != le16_to_cpu(header->len))) { 19398f44cb0SIgor Mitsyanko pr_warn("cmd reply length mismatch: %u != %u\n", 19498f44cb0SIgor Mitsyanko skb->len, le16_to_cpu(header->len)); 19598f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 19698f44cb0SIgor Mitsyanko return -EFAULT; 19798f44cb0SIgor Mitsyanko } 19898f44cb0SIgor Mitsyanko 19998f44cb0SIgor Mitsyanko switch (le16_to_cpu(header->type)) { 20098f44cb0SIgor Mitsyanko case QLINK_MSG_TYPE_CMDRSP: 20198f44cb0SIgor Mitsyanko if (unlikely(skb->len < sizeof(struct qlink_cmd))) { 20298f44cb0SIgor Mitsyanko pr_warn("cmd reply too short: %u\n", skb->len); 20398f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 20498f44cb0SIgor Mitsyanko break; 20598f44cb0SIgor Mitsyanko } 20698f44cb0SIgor Mitsyanko 20798f44cb0SIgor Mitsyanko qtnf_trans_signal_cmdresp(bus, skb); 20898f44cb0SIgor Mitsyanko break; 20998f44cb0SIgor Mitsyanko case QLINK_MSG_TYPE_EVENT: 21098f44cb0SIgor Mitsyanko if (unlikely(skb->len < sizeof(struct qlink_event))) { 21198f44cb0SIgor Mitsyanko pr_warn("event too short: %u\n", skb->len); 21298f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 21398f44cb0SIgor Mitsyanko break; 21498f44cb0SIgor Mitsyanko } 21598f44cb0SIgor Mitsyanko 21698f44cb0SIgor Mitsyanko ret = qtnf_trans_event_enqueue(bus, skb); 21798f44cb0SIgor Mitsyanko break; 21898f44cb0SIgor Mitsyanko default: 21998f44cb0SIgor Mitsyanko pr_warn("unknown packet type: %x\n", le16_to_cpu(header->type)); 22098f44cb0SIgor Mitsyanko dev_kfree_skb(skb); 22198f44cb0SIgor Mitsyanko break; 22298f44cb0SIgor Mitsyanko } 22398f44cb0SIgor Mitsyanko 22498f44cb0SIgor Mitsyanko return ret; 22598f44cb0SIgor Mitsyanko } 22698f44cb0SIgor Mitsyanko EXPORT_SYMBOL_GPL(qtnf_trans_handle_rx_ctl_packet); 227