From a1b9b9c7c004eb82a66b471d17ad46a1ff8726b6 Mon Sep 17 00:00:00 2001
From: Shayne Chen <shayne.chen@mediatek.com>
Date: Wed, 8 Feb 2023 13:54:34 +0800
Subject: [PATCH 1001/1002] mt76: mt7996: add linux tracing support

Add static tracepoint support for besra.

Signed-off-by: Shayne Chen <shayne.chen@mediatek.com>
Change-Id: I3209ba360b49c28e49c7cf464ea130aeba993ef7
---
 agg-rx.c              |   2 +
 mac80211.c            |   3 +
 mt7996/Makefile       |   2 +-
 mt7996/mac.c          |   7 +++
 mt7996/mcu.c          |   8 +++
 mt7996/mt7996_trace.h | 141 ++++++++++++++++++++++++++++++++++++++++++
 mt7996/trace.c        |  12 ++++
 trace.h               |  58 +++++++++++++++++
 8 files changed, 232 insertions(+), 1 deletion(-)
 create mode 100644 mt7996/mt7996_trace.h
 create mode 100644 mt7996/trace.c

diff --git a/agg-rx.c b/agg-rx.c
index 10cbd9e5..0a089e66 100644
--- a/agg-rx.c
+++ b/agg-rx.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
  */
 #include "mt76.h"
+#include "trace.h"
 
 static unsigned long mt76_aggr_tid_to_timeo(u8 tidno)
 {
@@ -187,6 +188,7 @@ void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames)
 	seqno = status->seqno;
 	size = tid->size;
 	sn_less = ieee80211_sn_less(seqno, head);
+	trace_mt76_rx_aggr_reorder(tid->dev, wcid, head, seqno, sn_less);
 
 	if (!tid->started) {
 		if (sn_less)
diff --git a/mac80211.c b/mac80211.c
index c50e97e5..b5f5597b 100644
--- a/mac80211.c
+++ b/mac80211.c
@@ -5,6 +5,7 @@
 #include <linux/sched.h>
 #include <linux/of.h>
 #include "mt76.h"
+#include "trace.h"
 
 #define CHAN2G(_idx, _freq) {			\
 	.band = NL80211_BAND_2GHZ,		\
@@ -1299,6 +1300,7 @@ void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
 
 		mt76_check_ccmp_pn(skb);
 		skb_shinfo(skb)->frag_list = NULL;
+		trace_mt76_rx_complete(dev, (struct mt76_rx_status *)skb->cb, 0);
 		mt76_rx_convert(dev, skb, &hw, &sta);
 		ieee80211_rx_list(hw, sta, skb, &list);
 
@@ -1308,6 +1310,7 @@ void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
 			nskb = nskb->next;
 			skb->next = NULL;
 
+			trace_mt76_rx_complete(dev, (struct mt76_rx_status *)skb->cb, 1);
 			mt76_rx_convert(dev, skb, &hw, &sta);
 			ieee80211_rx_list(hw, sta, skb, &list);
 		}
diff --git a/mt7996/Makefile b/mt7996/Makefile
index 039b64a1..fe4bb315 100644
--- a/mt7996/Makefile
+++ b/mt7996/Makefile
@@ -9,4 +9,4 @@ mt7996e-y := pci.o init.o dma.o eeprom.o main.o mcu.o mac.o \
 
 mt7996e-$(CONFIG_DEV_COREDUMP) += coredump.o
 
-mt7996e-y += mtk_debugfs.o mtk_mcu.o
+mt7996e-y += mtk_debugfs.o mtk_mcu.o trace.o
diff --git a/mt7996/mac.c b/mt7996/mac.c
index ce18138f..fb3d02a0 100644
--- a/mt7996/mac.c
+++ b/mt7996/mac.c
@@ -10,6 +10,7 @@
 #include "../dma.h"
 #include "mac.h"
 #include "mcu.h"
+#include "mt7996_trace.h"
 
 #define to_rssi(field, rcpi)	((FIELD_GET(field, rcpi) - 220) / 2)
 
@@ -665,6 +666,8 @@ mt7996_mac_fill_rx(struct mt7996_dev *dev, struct sk_buff *skb)
 	phy = mphy->priv;
 	status->phy_idx = mphy->band_idx;
 
+	trace_mt7996_fill_rx(phy, skb->data, skb->len);
+
 	if (!test_bit(MT76_STATE_RUNNING, &mphy->state))
 		return -EINVAL;
 
@@ -888,6 +891,8 @@ mt7996_mac_fill_rx(struct mt7996_dev *dev, struct sk_buff *skb)
 	if (rxv && mode >= MT_PHY_TYPE_HE_SU && !(status->flag & RX_FLAG_8023))
 		mt7996_mac_decode_he_radiotap(skb, rxv, mode);
 
+	trace_mt7996_fill_rx_done(phy, status->seqno, hdr_gap);
+
 	if (!status->wcid || !ieee80211_is_data_qos(fc) || hw_aggr)
 		return 0;
 
@@ -1221,6 +1226,8 @@ int mt7996_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
 	tx_info->buf[1].skip_unmap = true;
 	tx_info->nbuf = MT_CT_DMA_BUF_NUM;
 
+	trace_mt7996_tx_prepare(dev, wcid, qid, txwi, t->skb->data, t->skb->len);
+
 	return 0;
 }
 
diff --git a/mt7996/mcu.c b/mt7996/mcu.c
index 05edab0b..0dd9bdff 100644
--- a/mt7996/mcu.c
+++ b/mt7996/mcu.c
@@ -10,6 +10,7 @@
 #include "mcu.h"
 #include "mac.h"
 #include "eeprom.h"
+#include "mt7996_trace.h"
 
 struct mt7996_patch_hdr {
 	char build_date[16];
@@ -273,6 +274,9 @@ mt7996_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
 		else if (cmd & __MCU_CMD_FIELD_WM)
 			uni_txd->s2d_index = MCU_S2D_H2N;
 
+		trace_mt7996_mcu_cmd(dev, 1, uni_txd->cid, 0,
+				    skb->data, skb->len);
+
 		goto exit;
 	}
 
@@ -300,6 +304,8 @@ mt7996_mcu_send_message(struct mt76_dev *mdev, struct sk_buff *skb,
 	else
 		mcu_txd->s2d_index = MCU_S2D_H2N;
 
+	trace_mt7996_mcu_cmd(dev, 0, mcu_txd->cid, mcu_txd->ext_cid,
+			    skb->data, skb->len);
 exit:
 	if (wait_seq)
 		*wait_seq = seq;
@@ -512,6 +518,8 @@ void mt7996_mcu_rx_event(struct mt7996_dev *dev, struct sk_buff *skb)
 {
 	struct mt7996_mcu_rxd *rxd = (struct mt7996_mcu_rxd *)skb->data;
 
+	trace_mt7996_mcu_event(dev, rxd->option & MCU_UNI_CMD_UNSOLICITED_EVENT,
+			      rxd->eid, rxd->ext_eid, skb->data, skb->len);
 	if (rxd->option & MCU_UNI_CMD_UNSOLICITED_EVENT) {
 		mt7996_mcu_uni_rx_unsolicited_event(dev, skb);
 		return;
diff --git a/mt7996/mt7996_trace.h b/mt7996/mt7996_trace.h
new file mode 100644
index 00000000..5fa73482
--- /dev/null
+++ b/mt7996/mt7996_trace.h
@@ -0,0 +1,141 @@
+/* SPDX-License-Identifier: ISC */
+/*
+ * Copyright (C) 2022 MediaTek Inc.
+ */
+
+#if !defined(__MT7996_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
+#define __MT7996_TRACE_H
+
+#include <linux/types.h>
+#include <linux/tracepoint.h>
+#include "mt7996.h"
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM mt7996
+
+#define MAXNAME		32
+#define DEV_ENTRY	__array(char, wiphy_name, 32)
+#define DEV_ASSIGN(_w)	strlcpy(__entry->wiphy_name, wiphy_name(_w), MAXNAME)
+#define DEV_PR_FMT	"%s"
+#define DEV_PR_ARG	__entry->wiphy_name
+
+DECLARE_EVENT_CLASS(mt7996_mcu_debug,
+	TP_PROTO(struct mt7996_dev *dev, bool uni, u8 id, u8 ext_id,
+		 const void *data, size_t len),
+	TP_ARGS(dev, uni, id, ext_id, data, len),
+	TP_STRUCT__entry(
+		__field(bool, uni)
+		__field(u8, id)
+		__field(u8, ext_id)
+		__field(size_t, len)
+		__dynamic_array(u8, data, len)
+	),
+	TP_fast_assign(
+		__entry->uni = uni;
+		__entry->id = id;
+		__entry->ext_id = ext_id;
+		__entry->len = len;
+		memcpy(__get_dynamic_array(data), data, len);
+	),
+	TP_printk(
+		"uni: %d, id: %u, ext_id: %u, len: %zu",
+		__entry->uni,
+		__entry->id,
+		__entry->ext_id,
+		__entry->len
+	)
+);
+
+DEFINE_EVENT(mt7996_mcu_debug, mt7996_mcu_cmd,
+	TP_PROTO(struct mt7996_dev *dev, bool uni, u8 id, u8 ext_id,
+		 const void *data, size_t len),
+	TP_ARGS(dev, uni, id, ext_id, data, len)
+);
+
+DEFINE_EVENT(mt7996_mcu_debug, mt7996_mcu_event,
+	TP_PROTO(struct mt7996_dev *dev, bool uni, u8 id, u8 ext_id,
+		 const void *data, size_t len),
+	TP_ARGS(dev, uni, id, ext_id, data, len)
+);
+
+TRACE_EVENT(mt7996_tx_prepare,
+	TP_PROTO(struct mt7996_dev *dev, struct mt76_wcid *wcid, enum mt76_txq_id qid,
+		 const void *txwi, const void *data, size_t len),
+	TP_ARGS(dev, wcid, qid, txwi, data, len),
+
+	TP_STRUCT__entry(
+		DEV_ENTRY
+		__field(u16, wcid)
+		__field(u8, qid)
+		__array(u8, txwi, MT_TXD_SIZE)
+		__field(size_t, len)
+		__dynamic_array(u8, data, len)
+	),
+
+	TP_fast_assign(
+		DEV_ASSIGN(dev->mt76.phys[wcid->phy_idx]->hw->wiphy);
+		__entry->wcid = wcid->idx;
+		__entry->qid = qid;
+		memcpy(__entry->txwi, txwi, MT_TXD_SIZE);
+		__entry->len = len;
+		memcpy(__get_dynamic_array(data), data, len);
+	),
+
+	TP_printk(
+		DEV_PR_FMT " wcid: %u, qid: %u, len: %zu",
+		DEV_PR_ARG, __entry->wcid, __entry->qid, __entry->len
+	)
+);
+
+TRACE_EVENT(mt7996_fill_rx,
+	TP_PROTO(struct mt7996_phy *phy, const void *data, size_t len),
+	TP_ARGS(phy, data, len),
+
+	TP_STRUCT__entry(
+		DEV_ENTRY
+		__field(size_t, len)
+		__dynamic_array(u8, data, len)
+	),
+
+	TP_fast_assign(
+		DEV_ASSIGN(phy->mt76->hw->wiphy);
+		__entry->len = len;
+		memcpy(__get_dynamic_array(data), data, len);
+	),
+
+	TP_printk(
+		DEV_PR_FMT " len: %zu",
+		DEV_PR_ARG, __entry->len
+	)
+);
+
+TRACE_EVENT(mt7996_fill_rx_done,
+	TP_PROTO(struct mt7996_phy *phy, u16 seqno, u16 hdr_gap),
+	TP_ARGS(phy, seqno, hdr_gap),
+
+	TP_STRUCT__entry(
+		DEV_ENTRY
+		__field(u16, seqno)
+		__field(u16, hdr_gap)
+	),
+
+	TP_fast_assign(
+		DEV_ASSIGN(phy->mt76->hw->wiphy);
+		__entry->seqno = seqno;
+		__entry->hdr_gap = hdr_gap;
+	),
+
+	TP_printk(
+		DEV_PR_FMT " seqno: %u, hdr_gap: %u",
+		DEV_PR_ARG, __entry->seqno, __entry->hdr_gap
+	)
+);
+
+#endif
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH ./mt7996
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE mt7996_trace
+
+#include <trace/define_trace.h>
diff --git a/mt7996/trace.c b/mt7996/trace.c
new file mode 100644
index 00000000..ba36f79a
--- /dev/null
+++ b/mt7996/trace.c
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: ISC */
+/*
+ * Copyright (C) 2022 MediaTek Inc.
+ */
+
+#include <linux/module.h>
+
+#ifndef __CHECKER__
+#define CREATE_TRACE_POINTS
+#include "mt7996_trace.h"
+
+#endif
diff --git a/trace.h b/trace.h
index c3d0ef8e..57c94c6b 100644
--- a/trace.h
+++ b/trace.h
@@ -101,6 +101,64 @@ DEFINE_EVENT(dev_txid_evt, mac_txdone,
 	TP_ARGS(dev, wcid, pktid)
 );
 
+TRACE_EVENT(mt76_rx_complete,
+	TP_PROTO(struct mt76_dev *dev, struct mt76_rx_status *status, bool sub),
+	TP_ARGS(dev, status, sub),
+
+	TP_STRUCT__entry(
+		DEV_ENTRY
+		__field(u16, seqno)
+		__field(u16, wcid)
+		__field(u8, first_amsdu)
+		__field(bool, sub)
+	),
+
+	TP_fast_assign(
+		strlcpy(__entry->wiphy_name,
+			wiphy_name(mt76_dev_phy(dev, status->phy_idx)->hw->wiphy),
+			MAXNAME);
+		__entry->seqno = status->seqno;
+		__entry->wcid = status->wcid ? status->wcid->idx : 0;
+		__entry->first_amsdu = status->first_amsdu;
+		__entry->sub = sub;
+	),
+
+	TP_printk(
+		DEV_PR_FMT " seqno: %u, wcid: %u, first_amsdu: %x, sub: %d",
+		DEV_PR_ARG, __entry->seqno, __entry->wcid, __entry->first_amsdu,
+		__entry->sub
+	)
+);
+
+TRACE_EVENT(mt76_rx_aggr_reorder,
+	TP_PROTO(struct mt76_dev *dev, struct mt76_wcid *wcid, u16 _head, u16 seqno, bool sn_less),
+	TP_ARGS(dev, wcid, _head, seqno, sn_less),
+
+	TP_STRUCT__entry(
+		DEV_ENTRY
+		__field(u16, wcid)
+		__field(u16, _head)
+		__field(u16, seqno)
+		__field(bool, sn_less)
+	),
+
+	TP_fast_assign(
+		strlcpy(__entry->wiphy_name,
+			wiphy_name(dev->phys[wcid->phy_idx]->hw->wiphy),
+			MAXNAME);
+		__entry->wcid = wcid->idx;
+		__entry->_head = _head;
+		__entry->seqno = seqno;
+		__entry->sn_less = sn_less;
+	),
+
+	TP_printk(
+		DEV_PR_FMT " wcid: %u, head: %u, seqno: %u, sn_less: %d",
+		DEV_PR_ARG, __entry->wcid, __entry->_head, __entry->seqno,
+		__entry->sn_less
+	)
+);
+
 #endif
 
 #undef TRACE_INCLUDE_PATH
-- 
2.39.2

