1 #ifndef __MTK_WED_H
2 #define __MTK_WED_H
3 
4 #include <linux/kernel.h>
5 #include <linux/rcupdate.h>
6 #include <linux/regmap.h>
7 #include <linux/pci.h>
8 #include <linux/skbuff.h>
9 #include <linux/netdevice.h>
10 
11 #define MTK_WED_TX_QUEUES		2
12 #define MTK_WED_RX_QUEUES		2
13 
14 #define WED_WO_STA_REC			0x6
15 
16 struct mtk_wed_hw;
17 struct mtk_wdma_desc;
18 
19 enum mtk_wed_wo_cmd {
20 	MTK_WED_WO_CMD_WED_CFG,
21 	MTK_WED_WO_CMD_WED_RX_STAT,
22 	MTK_WED_WO_CMD_RRO_SER,
23 	MTK_WED_WO_CMD_DBG_INFO,
24 	MTK_WED_WO_CMD_DEV_INFO,
25 	MTK_WED_WO_CMD_BSS_INFO,
26 	MTK_WED_WO_CMD_STA_REC,
27 	MTK_WED_WO_CMD_DEV_INFO_DUMP,
28 	MTK_WED_WO_CMD_BSS_INFO_DUMP,
29 	MTK_WED_WO_CMD_STA_REC_DUMP,
30 	MTK_WED_WO_CMD_BA_INFO_DUMP,
31 	MTK_WED_WO_CMD_FBCMD_Q_DUMP,
32 	MTK_WED_WO_CMD_FW_LOG_CTRL,
33 	MTK_WED_WO_CMD_LOG_FLUSH,
34 	MTK_WED_WO_CMD_CHANGE_STATE,
35 	MTK_WED_WO_CMD_CPU_STATS_ENABLE,
36 	MTK_WED_WO_CMD_CPU_STATS_DUMP,
37 	MTK_WED_WO_CMD_EXCEPTION_INIT,
38 	MTK_WED_WO_CMD_PROF_CTRL,
39 	MTK_WED_WO_CMD_STA_BA_DUMP,
40 	MTK_WED_WO_CMD_BA_CTRL_DUMP,
41 	MTK_WED_WO_CMD_RXCNT_CTRL,
42 	MTK_WED_WO_CMD_RXCNT_INFO,
43 	MTK_WED_WO_CMD_SET_CAP,
44 	MTK_WED_WO_CMD_CCIF_RING_DUMP,
45 	MTK_WED_WO_CMD_WED_END
46 };
47 
48 struct mtk_rxbm_desc {
49 	__le32 buf0;
50 	__le32 token;
51 } __packed __aligned(4);
52 
53 enum mtk_wed_bus_tye {
54 	MTK_WED_BUS_PCIE,
55 	MTK_WED_BUS_AXI,
56 };
57 
58 #define MTK_WED_RING_CONFIGURED		BIT(0)
59 struct mtk_wed_ring {
60 	struct mtk_wdma_desc *desc;
61 	dma_addr_t desc_phys;
62 	u32 desc_size;
63 	int size;
64 	u32 flags;
65 
66 	u32 reg_base;
67 	void __iomem *wpdma;
68 };
69 
70 struct mtk_wed_wo_rx_stats {
71 	__le16 wlan_idx;
72 	__le16 tid;
73 	__le32 rx_pkt_cnt;
74 	__le32 rx_byte_cnt;
75 	__le32 rx_err_cnt;
76 	__le32 rx_drop_cnt;
77 };
78 
79 struct mtk_wed_device {
80 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
81 	const struct mtk_wed_ops *ops;
82 	struct device *dev;
83 	struct mtk_wed_hw *hw;
84 	bool init_done, running;
85 	int wdma_idx;
86 	int irq;
87 	u8 version;
88 
89 	/* used by wlan driver */
90 	u32 rev_id;
91 
92 	struct mtk_wed_ring tx_ring[MTK_WED_TX_QUEUES];
93 	struct mtk_wed_ring rx_ring[MTK_WED_RX_QUEUES];
94 	struct mtk_wed_ring txfree_ring;
95 	struct mtk_wed_ring tx_wdma[MTK_WED_TX_QUEUES];
96 	struct mtk_wed_ring rx_wdma[MTK_WED_RX_QUEUES];
97 
98 	struct {
99 		int size;
100 		void **pages;
101 		struct mtk_wdma_desc *desc;
102 		dma_addr_t desc_phys;
103 	} tx_buf_ring;
104 
105 	struct {
106 		int size;
107 		struct mtk_rxbm_desc *desc;
108 		dma_addr_t desc_phys;
109 	} rx_buf_ring;
110 
111 	struct {
112 		struct mtk_wed_ring ring;
113 		dma_addr_t miod_phys;
114 		dma_addr_t fdbk_phys;
115 	} rro;
116 
117 	/* filled by driver: */
118 	struct {
119 		union {
120 			struct platform_device *platform_dev;
121 			struct pci_dev *pci_dev;
122 		};
123 		enum mtk_wed_bus_tye bus_type;
124 		void __iomem *base;
125 		u32 phy_base;
126 
127 		u32 wpdma_phys;
128 		u32 wpdma_int;
129 		u32 wpdma_mask;
130 		u32 wpdma_tx;
131 		u32 wpdma_txfree;
132 		u32 wpdma_rx_glo;
133 		u32 wpdma_rx;
134 
135 		bool wcid_512;
136 
137 		u16 token_start;
138 		unsigned int nbuf;
139 		unsigned int rx_nbuf;
140 		unsigned int rx_npkt;
141 		unsigned int rx_size;
142 
143 		u8 tx_tbit[MTK_WED_TX_QUEUES];
144 		u8 rx_tbit[MTK_WED_RX_QUEUES];
145 		u8 txfree_tbit;
146 
147 		u32 (*init_buf)(void *ptr, dma_addr_t phys, int token_id);
148 		int (*offload_enable)(struct mtk_wed_device *wed);
149 		void (*offload_disable)(struct mtk_wed_device *wed);
150 		u32 (*init_rx_buf)(struct mtk_wed_device *wed, int size);
151 		void (*release_rx_buf)(struct mtk_wed_device *wed);
152 		void (*update_wo_rx_stats)(struct mtk_wed_device *wed,
153 					   struct mtk_wed_wo_rx_stats *stats);
154 		int (*reset)(struct mtk_wed_device *wed);
155 		void (*reset_complete)(struct mtk_wed_device *wed);
156 	} wlan;
157 #endif
158 };
159 
160 struct mtk_wed_ops {
161 	int (*attach)(struct mtk_wed_device *dev);
162 	int (*tx_ring_setup)(struct mtk_wed_device *dev, int ring,
163 			     void __iomem *regs, bool reset);
164 	int (*rx_ring_setup)(struct mtk_wed_device *dev, int ring,
165 			     void __iomem *regs, bool reset);
166 	int (*txfree_ring_setup)(struct mtk_wed_device *dev,
167 				 void __iomem *regs);
168 	int (*msg_update)(struct mtk_wed_device *dev, int cmd_id,
169 			  void *data, int len);
170 	void (*detach)(struct mtk_wed_device *dev);
171 	void (*ppe_check)(struct mtk_wed_device *dev, struct sk_buff *skb,
172 			  u32 reason, u32 hash);
173 
174 	void (*stop)(struct mtk_wed_device *dev);
175 	void (*start)(struct mtk_wed_device *dev, u32 irq_mask);
176 	void (*reset_dma)(struct mtk_wed_device *dev);
177 
178 	u32 (*reg_read)(struct mtk_wed_device *dev, u32 reg);
179 	void (*reg_write)(struct mtk_wed_device *dev, u32 reg, u32 val);
180 
181 	u32 (*irq_get)(struct mtk_wed_device *dev, u32 mask);
182 	void (*irq_set_mask)(struct mtk_wed_device *dev, u32 mask);
183 	int (*setup_tc)(struct mtk_wed_device *wed, struct net_device *dev,
184 			enum tc_setup_type type, void *type_data);
185 };
186 
187 extern const struct mtk_wed_ops __rcu *mtk_soc_wed_ops;
188 
189 static inline int
190 mtk_wed_device_attach(struct mtk_wed_device *dev)
191 {
192 	int ret = -ENODEV;
193 
194 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
195 	rcu_read_lock();
196 	dev->ops = rcu_dereference(mtk_soc_wed_ops);
197 	if (dev->ops)
198 		ret = dev->ops->attach(dev);
199 	else
200 		rcu_read_unlock();
201 
202 	if (ret)
203 		dev->ops = NULL;
204 #endif
205 
206 	return ret;
207 }
208 
209 static inline bool
210 mtk_wed_get_rx_capa(struct mtk_wed_device *dev)
211 {
212 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
213 	return dev->version != 1;
214 #else
215 	return false;
216 #endif
217 }
218 
219 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
220 #define mtk_wed_device_active(_dev) !!(_dev)->ops
221 #define mtk_wed_device_detach(_dev) (_dev)->ops->detach(_dev)
222 #define mtk_wed_device_start(_dev, _mask) (_dev)->ops->start(_dev, _mask)
223 #define mtk_wed_device_tx_ring_setup(_dev, _ring, _regs, _reset) \
224 	(_dev)->ops->tx_ring_setup(_dev, _ring, _regs, _reset)
225 #define mtk_wed_device_txfree_ring_setup(_dev, _regs) \
226 	(_dev)->ops->txfree_ring_setup(_dev, _regs)
227 #define mtk_wed_device_reg_read(_dev, _reg) \
228 	(_dev)->ops->reg_read(_dev, _reg)
229 #define mtk_wed_device_reg_write(_dev, _reg, _val) \
230 	(_dev)->ops->reg_write(_dev, _reg, _val)
231 #define mtk_wed_device_irq_get(_dev, _mask) \
232 	(_dev)->ops->irq_get(_dev, _mask)
233 #define mtk_wed_device_irq_set_mask(_dev, _mask) \
234 	(_dev)->ops->irq_set_mask(_dev, _mask)
235 #define mtk_wed_device_rx_ring_setup(_dev, _ring, _regs, _reset) \
236 	(_dev)->ops->rx_ring_setup(_dev, _ring, _regs, _reset)
237 #define mtk_wed_device_ppe_check(_dev, _skb, _reason, _hash) \
238 	(_dev)->ops->ppe_check(_dev, _skb, _reason, _hash)
239 #define mtk_wed_device_update_msg(_dev, _id, _msg, _len) \
240 	(_dev)->ops->msg_update(_dev, _id, _msg, _len)
241 #define mtk_wed_device_stop(_dev) (_dev)->ops->stop(_dev)
242 #define mtk_wed_device_dma_reset(_dev) (_dev)->ops->reset_dma(_dev)
243 #define mtk_wed_device_setup_tc(_dev, _netdev, _type, _type_data) \
244 	(_dev)->ops->setup_tc(_dev, _netdev, _type, _type_data)
245 #else
246 static inline bool mtk_wed_device_active(struct mtk_wed_device *dev)
247 {
248 	return false;
249 }
250 #define mtk_wed_device_detach(_dev) do {} while (0)
251 #define mtk_wed_device_start(_dev, _mask) do {} while (0)
252 #define mtk_wed_device_tx_ring_setup(_dev, _ring, _regs, _reset) -ENODEV
253 #define mtk_wed_device_txfree_ring_setup(_dev, _ring, _regs) -ENODEV
254 #define mtk_wed_device_reg_read(_dev, _reg) 0
255 #define mtk_wed_device_reg_write(_dev, _reg, _val) do {} while (0)
256 #define mtk_wed_device_irq_get(_dev, _mask) 0
257 #define mtk_wed_device_irq_set_mask(_dev, _mask) do {} while (0)
258 #define mtk_wed_device_rx_ring_setup(_dev, _ring, _regs, _reset) -ENODEV
259 #define mtk_wed_device_ppe_check(_dev, _skb, _reason, _hash)  do {} while (0)
260 #define mtk_wed_device_update_msg(_dev, _id, _msg, _len) -ENODEV
261 #define mtk_wed_device_stop(_dev) do {} while (0)
262 #define mtk_wed_device_dma_reset(_dev) do {} while (0)
263 #define mtk_wed_device_setup_tc(_dev, _netdev, _type, _type_data) -EOPNOTSUPP
264 #endif
265 
266 #endif
267