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