blob: 749df9e6681ed7e2f6d39468bfec8634ec62db3e [file] [log] [blame]
Rakesh Nair9bcf2602017-01-06 16:02:16 +05301/*
2 * Copyright (c) 2014 - 2017, The Linux Foundation. All rights reserved.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15
16#ifndef _EDMA_H_
17#define _EDMA_H_
18
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/module.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/skbuff.h>
27#include <linux/io.h>
28#include <linux/vmalloc.h>
29#include <linux/pagemap.h>
30#include <linux/smp.h>
31#include <linux/platform_device.h>
32#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/kernel.h>
35#include <linux/device.h>
36#include <linux/sysctl.h>
37#include <linux/phy.h>
38#include <linux/of_net.h>
39#include <net/checksum.h>
40#include <net/ip6_checksum.h>
41#include <asm-generic/bug.h>
42#include <linux/version.h>
43#include "ess_edma.h"
44
45#define EDMA_CPU_CORES_SUPPORTED 4
46#define EDMA_MAX_PORTID_SUPPORTED 5
47#define EDMA_MAX_VLAN_SUPPORTED EDMA_MAX_PORTID_SUPPORTED
48#define EDMA_MAX_PORTID_BITMAP_INDEX (EDMA_MAX_PORTID_SUPPORTED + 1)
49#define EDMA_MAX_PORTID_BITMAP_SUPPORTED 0x1f /* 0001_1111 = 0x1f */
50#define EDMA_MAX_NETDEV_PER_QUEUE 4 /* 3 Netdev per queue, 1 space for indexing */
51
52#define EDMA_MAX_RECEIVE_QUEUE 8
53#define EDMA_MAX_TRANSMIT_QUEUE 16
54
55/* WAN/LAN adapter number */
56#define EDMA_WAN 0
57#define EDMA_LAN 1
58
59/* VLAN tag */
60#define EDMA_LAN_DEFAULT_VLAN 1
61#define EDMA_WAN_DEFAULT_VLAN 2
62
63#define EDMA_DEFAULT_GROUP1_VLAN 2
64#define EDMA_DEFAULT_GROUP2_VLAN 1
65#define EDMA_DEFAULT_GROUP3_VLAN 3
66#define EDMA_DEFAULT_GROUP4_VLAN 4
67#define EDMA_DEFAULT_GROUP5_VLAN 5
68
69#define EDMA_DEFAULT_GROUP1_BMP 0x20
70#define EDMA_DEFAULT_GROUP2_BMP 0x1e
71
72#define EDMA_DEFAULT_DISABLE_RSS 0
73#define EDMA_RSS_DISABLE 1
74#define EDMA_RSS_ENABLE 0
75
76/* Queues exposed to linux kernel */
77#define EDMA_NETDEV_TX_QUEUE 4
78#define EDMA_NETDEV_RX_QUEUE 4
79
80/* Number of queues per core */
81#define EDMA_NUM_TXQ_PER_CORE 4
82#define EDMA_NUM_RXQ_PER_CORE 2
83
84#define EDMA_TPD_EOP_SHIFT 31
85
86#define EDMA_PORT_ID_SHIFT 12
87#define EDMA_PORT_ID_MASK 0x7
88
89/* tpd word 3 bit 18-28 */
90#define EDMA_TPD_PORT_BITMAP_SHIFT 18
91
92#define EDMA_TPD_FROM_CPU_SHIFT 25
93
94#define EDMA_FROM_CPU_MASK 0x80
95#define EDMA_SKB_PRIORITY_MASK 0x38
96
97/* TX/RX descriptor ring count */
98/* should be a power of 2 */
99#define EDMA_RX_RING_SIZE 512
100#define EDMA_TX_RING_SIZE 512
101
102/* Flags used in paged/non paged mode */
103#define EDMA_RX_HEAD_BUFF_SIZE_JUMBO 256
104#define EDMA_RX_HEAD_BUFF_SIZE 1540
105
106/* MAX frame size supported by switch */
107#define EDMA_MAX_JUMBO_FRAME_SIZE 9216
108
109/* Configurations */
110#define EDMA_INTR_CLEAR_TYPE 0
111#define EDMA_INTR_SW_IDX_W_TYPE 0
112#define EDMA_FIFO_THRESH_TYPE 0
113#define EDMA_RSS_TYPE 0
114#define EDMA_RX_IMT 0x0020
115#define EDMA_TX_IMT 0x0050
116#define EDMA_TPD_BURST 5
117#define EDMA_TXF_BURST 0x100
118#define EDMA_RFD_BURST 8
119#define EDMA_RFD_THR 16
120#define EDMA_RFD_LTHR 0
121
122/* RX/TX per CPU based mask/shift */
123#define EDMA_TX_PER_CPU_MASK 0xF
124#define EDMA_RX_PER_CPU_MASK 0x3
125#define EDMA_TX_PER_CPU_MASK_SHIFT 0x2
126#define EDMA_RX_PER_CPU_MASK_SHIFT 0x1
127#define EDMA_TX_CPU_START_SHIFT 0x2
128#define EDMA_RX_CPU_START_SHIFT 0x1
129
130/* FLags used in transmit direction */
131#define EDMA_HW_CHECKSUM 0x00000001
132#define EDMA_VLAN_TX_TAG_INSERT_FLAG 0x00000002
133#define EDMA_VLAN_TX_TAG_INSERT_DEFAULT_FLAG 0x00000004
134
135#define EDMA_SW_DESC_FLAG_LAST 0x1
136#define EDMA_SW_DESC_FLAG_SKB_HEAD 0x2
137#define EDMA_SW_DESC_FLAG_SKB_FRAG 0x4
138#define EDMA_SW_DESC_FLAG_SKB_FRAGLIST 0x8
139#define EDMA_SW_DESC_FLAG_SKB_NONE 0x10
140#define EDMA_SW_DESC_FLAG_SKB_REUSE 0x20
141
142
143#define EDMA_MAX_SKB_FRAGS (MAX_SKB_FRAGS + 1)
144
145/* Ethtool specific list of EDMA supported features */
146#define EDMA_SUPPORTED_FEATURES (SUPPORTED_10baseT_Half \
147 | SUPPORTED_10baseT_Full \
148 | SUPPORTED_100baseT_Half \
149 | SUPPORTED_100baseT_Full \
150 | SUPPORTED_1000baseT_Full)
151
152/* Recevie side atheros Header */
153#define EDMA_RX_ATH_HDR_VERSION 0x2
154#define EDMA_RX_ATH_HDR_VERSION_SHIFT 14
155#define EDMA_RX_ATH_HDR_PRIORITY_SHIFT 11
156#define EDMA_RX_ATH_PORT_TYPE_SHIFT 6
157#define EDMA_RX_ATH_HDR_RSTP_PORT_TYPE 0x4
158
159/* Transmit side atheros Header */
160#define EDMA_TX_ATH_HDR_PORT_BITMAP_MASK 0x7F
161#define EDMA_TX_ATH_HDR_FROM_CPU_MASK 0x80
162#define EDMA_TX_ATH_HDR_FROM_CPU_SHIFT 7
163
164#define EDMA_TXQ_START_CORE0 8
165#define EDMA_TXQ_START_CORE1 12
166#define EDMA_TXQ_START_CORE2 0
167#define EDMA_TXQ_START_CORE3 4
168
169#define EDMA_TXQ_IRQ_MASK_CORE0 0x0F00
170#define EDMA_TXQ_IRQ_MASK_CORE1 0xF000
171#define EDMA_TXQ_IRQ_MASK_CORE2 0x000F
172#define EDMA_TXQ_IRQ_MASK_CORE3 0x00F0
173
174#define EDMA_ETH_HDR_LEN 12
175#define EDMA_ETH_TYPE_MASK 0xFFFF
176
177#define EDMA_RX_BUFFER_WRITE 16
178#define EDMA_RFD_AVAIL_THR 80
179
180#define EDMA_GMAC_NO_MDIO_PHY PHY_MAX_ADDR
181
182extern int ssdk_rfs_ipct_rule_set(__be32 ip_src, __be32 ip_dst,
183 __be16 sport, __be16 dport,
184 uint8_t proto, u16 loadbalance, bool action);
185struct edma_ethtool_statistics {
Bhaskar Valabojue429bab2017-03-15 09:01:23 +0530186 u64 tx_q0_pkt;
187 u64 tx_q1_pkt;
188 u64 tx_q2_pkt;
189 u64 tx_q3_pkt;
190 u64 tx_q4_pkt;
191 u64 tx_q5_pkt;
192 u64 tx_q6_pkt;
193 u64 tx_q7_pkt;
194 u64 tx_q8_pkt;
195 u64 tx_q9_pkt;
196 u64 tx_q10_pkt;
197 u64 tx_q11_pkt;
198 u64 tx_q12_pkt;
199 u64 tx_q13_pkt;
200 u64 tx_q14_pkt;
201 u64 tx_q15_pkt;
202 u64 tx_q0_byte;
203 u64 tx_q1_byte;
204 u64 tx_q2_byte;
205 u64 tx_q3_byte;
206 u64 tx_q4_byte;
207 u64 tx_q5_byte;
208 u64 tx_q6_byte;
209 u64 tx_q7_byte;
210 u64 tx_q8_byte;
211 u64 tx_q9_byte;
212 u64 tx_q10_byte;
213 u64 tx_q11_byte;
214 u64 tx_q12_byte;
215 u64 tx_q13_byte;
216 u64 tx_q14_byte;
217 u64 tx_q15_byte;
218 u64 rx_q0_pkt;
219 u64 rx_q1_pkt;
220 u64 rx_q2_pkt;
221 u64 rx_q3_pkt;
222 u64 rx_q4_pkt;
223 u64 rx_q5_pkt;
224 u64 rx_q6_pkt;
225 u64 rx_q7_pkt;
226 u64 rx_q0_byte;
227 u64 rx_q1_byte;
228 u64 rx_q2_byte;
229 u64 rx_q3_byte;
230 u64 rx_q4_byte;
231 u64 rx_q5_byte;
232 u64 rx_q6_byte;
233 u64 rx_q7_byte;
234 u64 tx_desc_error;
Rakesh Nair03b586c2017-04-03 18:28:58 +0530235 u64 rx_alloc_fail_ctr;
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530236};
237
238struct edma_mdio_data {
239 struct mii_bus *mii_bus;
240 void __iomem *membase;
241 int phy_irq[PHY_MAX_ADDR];
242};
243
244/* EDMA LINK state */
245enum edma_link_state {
246 __EDMA_LINKUP, /* Indicate link is UP */
247 __EDMA_LINKDOWN /* Indicate link is down */
248};
249
250/* EDMA GMAC state */
251enum edma_gmac_state {
252 __EDMA_UP /* use to indicate GMAC is up */
253};
254
255/* edma transmit descriptor */
256struct edma_tx_desc {
257 __le16 len; /* full packet including CRC */
258 __le16 svlan_tag; /* vlan tag */
259 __le32 word1; /* byte 4-7 */
260 __le32 addr; /* address of buffer */
261 __le32 word3; /* byte 12 */
262};
263
264/* edma receive return descriptor */
265struct edma_rx_return_desc {
266 u16 rrd0;
267 u16 rrd1;
268 u16 rrd2;
269 u16 rrd3;
270 u16 rrd4;
271 u16 rrd5;
272 u16 rrd6;
273 u16 rrd7;
274};
275
276/* RFD descriptor */
277struct edma_rx_free_desc {
278 __le32 buffer_addr; /* buffer address */
279};
280
281/* edma hw specific data */
282struct edma_hw {
283 u32 __iomem *hw_addr; /* inner register address */
284 struct edma_adapter *adapter; /* netdevice adapter */
285 u32 rx_intr_mask; /*rx interrupt mask */
286 u32 tx_intr_mask; /* tx interrupt nask */
287 u32 misc_intr_mask; /* misc interrupt mask */
288 u32 wol_intr_mask; /* wake on lan interrupt mask */
289 bool intr_clear_type; /* interrupt clear */
290 bool intr_sw_idx_w; /* interrupt software index */
291 u32 rx_head_buff_size; /* Rx buffer size */
292 u8 rss_type; /* rss protocol type */
293};
294
295/* edma_sw_desc stores software descriptor
296 * SW descriptor has 1:1 map with HW descriptor
297 */
298struct edma_sw_desc {
299 struct sk_buff *skb;
300 dma_addr_t dma; /* dma address */
301 u16 length; /* Tx/Rx buffer length */
302 u32 flags;
303};
304
305/* per core related information */
306struct edma_per_cpu_queues_info {
307 struct napi_struct napi; /* napi associated with the core */
308 u32 tx_mask; /* tx interrupt mask */
309 u32 rx_mask; /* rx interrupt mask */
310 u32 tx_status; /* tx interrupt status */
311 u32 rx_status; /* rx interrupt status */
312 u32 tx_start; /* tx queue start */
313 u32 rx_start; /* rx queue start */
314 struct edma_common_info *edma_cinfo; /* edma common info */
315};
316
317/* edma specific common info */
318struct edma_common_info {
319 struct edma_tx_desc_ring *tpd_ring[16]; /* 16 Tx queues */
320 struct edma_rfd_desc_ring *rfd_ring[8]; /* 8 Rx queues */
321 struct platform_device *pdev; /* device structure */
322 struct net_device *netdev[EDMA_MAX_PORTID_SUPPORTED];
323 struct net_device *portid_netdev_lookup_tbl[EDMA_MAX_PORTID_BITMAP_INDEX];
324 struct ctl_table_header *edma_ctl_table_hdr;
325 int num_gmac;
326 struct edma_ethtool_statistics edma_ethstats; /* ethtool stats */
327 u32 num_rx_queues; /* number of rx queue */
328 u32 num_tx_queues; /* number of tx queue */
329 u32 tx_irq[16]; /* number of tx irq */
330 u32 rx_irq[8]; /* number of rx irq */
331 u32 from_cpu; /* from CPU TPD field */
332 u32 num_rxq_per_core; /* Rx queues per core */
333 u32 num_txq_per_core; /* Tx queues per core */
334 u16 tx_ring_count; /* Tx ring count */
335 u16 rx_ring_count; /* Rx ring*/
336 u16 rx_head_buffer_len; /* rx buffer length */
337 u16 rx_page_buffer_len; /* rx buffer length */
338 u32 page_mode; /* Jumbo frame supported flag */
339 u32 fraglist_mode; /* fraglist supported flag */
340 struct edma_hw hw; /* edma hw specific structure */
341 struct edma_per_cpu_queues_info edma_percpu_info[CONFIG_NR_CPUS]; /* per cpu information */
342 spinlock_t stats_lock; /* protect edma stats area for updation */
343};
344
345/* transimit packet descriptor (tpd) ring */
346struct edma_tx_desc_ring {
347 struct netdev_queue *nq[EDMA_MAX_NETDEV_PER_QUEUE]; /* Linux queue index */
348 struct net_device *netdev[EDMA_MAX_NETDEV_PER_QUEUE];
349 /* Array of netdevs associated with the tpd ring */
350 void *hw_desc; /* descriptor ring virtual address */
351 struct edma_sw_desc *sw_desc; /* buffer associated with ring */
352 int netdev_bmp; /* Bitmap for per-ring netdevs */
353 u32 size; /* descriptor ring length in bytes */
354 u16 count; /* number of descriptors in the ring */
355 dma_addr_t dma; /* descriptor ring physical address */
356 u16 sw_next_to_fill; /* next Tx descriptor to fill */
357 u16 sw_next_to_clean; /* next Tx descriptor to clean */
358};
359
360/* receive free descriptor (rfd) ring */
361struct edma_rfd_desc_ring {
362 struct edma_rx_free_desc *hw_desc; /* descriptor ring virtual address */
363 struct edma_sw_desc *sw_desc; /* buffer associated with ring */
364 u16 size; /* bytes allocated to sw_desc */
365 u16 count; /* number of descriptors in the ring */
366 dma_addr_t dma; /* descriptor ring physical address */
367 u16 sw_next_to_fill; /* next descriptor to fill */
368 u16 sw_next_to_clean; /* next descriptor to clean */
Rakesh Nair03b586c2017-04-03 18:28:58 +0530369 u16 pending_fill; /* fill pending from previous iteration */
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530370};
371
372/* edma_rfs_flter_node - rfs filter node in hash table */
373struct edma_rfs_filter_node {
374 struct flow_keys keys;
375 u32 flow_id; /* flow_id of filter provided by kernel */
376 u16 filter_id; /* filter id of filter returned by adaptor */
377 u16 rq_id; /* desired rq index */
378 struct hlist_node node; /* edma rfs list node */
379};
380
381/* edma_rfs_flow_tbl - rfs flow table */
382struct edma_rfs_flow_table {
383 u16 max_num_filter; /* Maximum number of filters edma supports */
384 u16 hashtoclean; /* hash table index to clean next */
385 int filter_available; /* Number of free filters available */
386 struct hlist_head hlist_head[EDMA_RFS_FLOW_ENTRIES];
387 spinlock_t rfs_ftab_lock;
388 struct timer_list expire_rfs; /* timer function for edma_rps_may_expire_flow */
389};
390
391/* EDMA net device structure */
392struct edma_adapter {
393 struct net_device *netdev; /* netdevice */
394 struct platform_device *pdev; /* platform device */
395 struct edma_common_info *edma_cinfo; /* edma common info */
396 struct phy_device *phydev; /* Phy device */
397 struct edma_rfs_flow_table rfs; /* edma rfs flow table */
Bhaskar Valabojue429bab2017-03-15 09:01:23 +0530398 struct rtnl_link_stats64 stats; /* netdev statistics */
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530399#ifdef CONFIG_RFS_ACCEL
400 set_rfs_filter_callback_t set_rfs_rule;
401#endif
402 u32 flags;/* status flags */
403 unsigned long state_flags; /* GMAC up/down flags */
404 u32 forced_speed; /* link force speed */
405 u32 forced_duplex; /* link force duplex */
406 u32 link_state; /* phy link state */
407 u32 phy_mdio_addr; /* PHY device address on MII interface */
408 u32 poll_required; /* check if link polling is required */
409 u32 poll_required_dynamic; /* dynamic polling flag */
410 u32 tx_start_offset[CONFIG_NR_CPUS]; /* tx queue start */
411 u32 default_vlan_tag; /* vlan tag */
412 u32 dp_bitmap;
413 uint8_t phy_id[MII_BUS_ID_SIZE + 3];
Rakesh Naired29f6b2017-04-04 15:48:08 +0530414 struct mutex poll_mutex; /* Lock to protect polling flag change */
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530415};
416
417int edma_alloc_queues_tx(struct edma_common_info *edma_cinfo);
418int edma_alloc_queues_rx(struct edma_common_info *edma_cinfo);
419int edma_open(struct net_device *netdev);
420int edma_close(struct net_device *netdev);
421void edma_free_tx_resources(struct edma_common_info *edma_c_info);
422void edma_free_rx_resources(struct edma_common_info *edma_c_info);
423int edma_alloc_tx_rings(struct edma_common_info *edma_cinfo);
424int edma_alloc_rx_rings(struct edma_common_info *edma_cinfo);
425void edma_free_tx_rings(struct edma_common_info *edma_cinfo);
426void edma_free_rx_rings(struct edma_common_info *edma_cinfo);
427void edma_free_queues(struct edma_common_info *edma_cinfo);
428void edma_irq_disable(struct edma_common_info *edma_cinfo);
429int edma_reset(struct edma_common_info *edma_cinfo);
430int edma_poll(struct napi_struct *napi, int budget);
431netdev_tx_t edma_xmit(struct sk_buff *skb,
432 struct net_device *netdev);
433int edma_configure(struct edma_common_info *edma_cinfo);
434void edma_irq_enable(struct edma_common_info *edma_cinfo);
435void edma_enable_tx_ctrl(struct edma_hw *hw);
436void edma_enable_rx_ctrl(struct edma_hw *hw);
437void edma_stop_rx_tx(struct edma_hw *hw);
438void edma_free_irqs(struct edma_adapter *adapter);
439irqreturn_t edma_interrupt(int irq, void *dev);
440void edma_write_reg(u16 reg_addr, u32 reg_value);
441void edma_read_reg(u16 reg_addr, volatile u32 *reg_value);
Bhaskar Valabojue429bab2017-03-15 09:01:23 +0530442struct rtnl_link_stats64 *edma_get_stats64(struct net_device *dev,
443 struct rtnl_link_stats64 *stats);
Rakesh Nair9bcf2602017-01-06 16:02:16 +0530444int edma_set_mac_addr(struct net_device *netdev, void *p);
445int edma_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
446 u16 rxq, u32 flow_id);
447#ifdef CONFIG_RFS_ACCEL
448int edma_register_rfs_filter(struct net_device *netdev,
449 set_rfs_filter_callback_t set_filter);
450#endif
451void edma_flow_may_expire(unsigned long data);
452void edma_set_ethtool_ops(struct net_device *netdev);
453int edma_change_mtu(struct net_device *netdev, int new_mtu);
454void edma_set_stp_rstp(bool tag);
455void edma_assign_ath_hdr_type(int tag);
456int edma_get_default_vlan_tag(struct net_device *netdev);
457void edma_adjust_link(struct net_device *netdev);
458int edma_fill_netdev(struct edma_common_info *edma_cinfo, int qid, int num, int txq_id);
459u16 edma_select_xps_queue(struct net_device *dev, struct sk_buff *skb,
460 void *accel_priv, select_queue_fallback_t fallback);
461void edma_read_append_stats(struct edma_common_info *edma_cinfo);
462void edma_change_tx_coalesce(int usecs);
463void edma_change_rx_coalesce(int usecs);
464void edma_get_tx_rx_coalesce(u32 *reg_val);
465void edma_clear_irq_status(void);
466#endif /* _EDMA_H_ */