enetc: Drop redundant ____cacheline_aligned_in_smp
[linux-2.6-microblaze.git] / drivers / net / ethernet / freescale / enetc / enetc.h
1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /* Copyright 2017-2019 NXP */
3
4 #include <linux/timer.h>
5 #include <linux/pci.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/skbuff.h>
10 #include <linux/ethtool.h>
11 #include <linux/if_vlan.h>
12 #include <linux/phy.h>
13
14 #include "enetc_hw.h"
15
16 #define ENETC_MAC_MAXFRM_SIZE   9600
17 #define ENETC_MAX_MTU           (ENETC_MAC_MAXFRM_SIZE - \
18                                 (ETH_FCS_LEN + ETH_HLEN + VLAN_HLEN))
19
20 struct enetc_tx_swbd {
21         struct sk_buff *skb;
22         dma_addr_t dma;
23         u16 len;
24         u8 is_dma_page:1;
25         u8 check_wb:1;
26         u8 do_tstamp:1;
27 };
28
29 #define ENETC_RX_MAXFRM_SIZE    ENETC_MAC_MAXFRM_SIZE
30 #define ENETC_RXB_TRUESIZE      2048 /* PAGE_SIZE >> 1 */
31 #define ENETC_RXB_PAD           NET_SKB_PAD /* add extra space if needed */
32 #define ENETC_RXB_DMA_SIZE      \
33         (SKB_WITH_OVERHEAD(ENETC_RXB_TRUESIZE) - ENETC_RXB_PAD)
34
35 struct enetc_rx_swbd {
36         dma_addr_t dma;
37         struct page *page;
38         u16 page_offset;
39 };
40
41 struct enetc_ring_stats {
42         unsigned int packets;
43         unsigned int bytes;
44         unsigned int rx_alloc_errs;
45 };
46
47 #define ENETC_RX_RING_DEFAULT_SIZE      512
48 #define ENETC_TX_RING_DEFAULT_SIZE      256
49 #define ENETC_DEFAULT_TX_WORK           (ENETC_TX_RING_DEFAULT_SIZE / 2)
50
51 struct enetc_bdr {
52         struct device *dev; /* for DMA mapping */
53         struct net_device *ndev;
54         void *bd_base; /* points to Rx or Tx BD ring */
55         union {
56                 void __iomem *tpir;
57                 void __iomem *rcir;
58         };
59         u16 index;
60         int bd_count; /* # of BDs */
61         int next_to_use;
62         int next_to_clean;
63         union {
64                 struct enetc_tx_swbd *tx_swbd;
65                 struct enetc_rx_swbd *rx_swbd;
66         };
67         union {
68                 void __iomem *tcir; /* Tx */
69                 int next_to_alloc; /* Rx */
70         };
71         void __iomem *idr; /* Interrupt Detect Register pointer */
72
73         struct enetc_ring_stats stats;
74
75         dma_addr_t bd_dma_base;
76         u8 tsd_enable; /* Time specific departure */
77         bool ext_en; /* enable h/w descriptor extensions */
78 } ____cacheline_aligned_in_smp;
79
80 static inline void enetc_bdr_idx_inc(struct enetc_bdr *bdr, int *i)
81 {
82         if (unlikely(++*i == bdr->bd_count))
83                 *i = 0;
84 }
85
86 static inline int enetc_bd_unused(struct enetc_bdr *bdr)
87 {
88         if (bdr->next_to_clean > bdr->next_to_use)
89                 return bdr->next_to_clean - bdr->next_to_use - 1;
90
91         return bdr->bd_count + bdr->next_to_clean - bdr->next_to_use - 1;
92 }
93
94 /* Control BD ring */
95 #define ENETC_CBDR_DEFAULT_SIZE 64
96 struct enetc_cbdr {
97         void *bd_base; /* points to Rx or Tx BD ring */
98         void __iomem *pir;
99         void __iomem *cir;
100
101         int bd_count; /* # of BDs */
102         int next_to_use;
103         int next_to_clean;
104
105         dma_addr_t bd_dma_base;
106 };
107
108 #define ENETC_TXBD(BDR, i) (&(((union enetc_tx_bd *)((BDR).bd_base))[i]))
109
110 static inline union enetc_rx_bd *enetc_rxbd(struct enetc_bdr *rx_ring, int i)
111 {
112         int hw_idx = i;
113
114 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
115         if (rx_ring->ext_en)
116                 hw_idx = 2 * i;
117 #endif
118         return &(((union enetc_rx_bd *)rx_ring->bd_base)[hw_idx]);
119 }
120
121 static inline union enetc_rx_bd *enetc_rxbd_next(struct enetc_bdr *rx_ring,
122                                                  union enetc_rx_bd *rxbd,
123                                                  int i)
124 {
125         rxbd++;
126 #ifdef CONFIG_FSL_ENETC_PTP_CLOCK
127         if (rx_ring->ext_en)
128                 rxbd++;
129 #endif
130         if (unlikely(++i == rx_ring->bd_count))
131                 rxbd = rx_ring->bd_base;
132
133         return rxbd;
134 }
135
136 static inline union enetc_rx_bd *enetc_rxbd_ext(union enetc_rx_bd *rxbd)
137 {
138         return ++rxbd;
139 }
140
141 struct enetc_msg_swbd {
142         void *vaddr;
143         dma_addr_t dma;
144         int size;
145 };
146
147 #define ENETC_REV1      0x1
148 enum enetc_errata {
149         ENETC_ERR_TXCSUM        = BIT(0),
150         ENETC_ERR_VLAN_ISOL     = BIT(1),
151         ENETC_ERR_UCMCSWP       = BIT(2),
152 };
153
154 #define ENETC_SI_F_QBV BIT(0)
155 #define ENETC_SI_F_PSFP BIT(1)
156
157 /* PCI IEP device data */
158 struct enetc_si {
159         struct pci_dev *pdev;
160         struct enetc_hw hw;
161         enum enetc_errata errata;
162
163         struct net_device *ndev; /* back ref. */
164
165         struct enetc_cbdr cbd_ring;
166
167         int num_rx_rings; /* how many rings are available in the SI */
168         int num_tx_rings;
169         int num_fs_entries;
170         int num_rss; /* number of RSS buckets */
171         unsigned short pad;
172         int hw_features;
173 };
174
175 #define ENETC_SI_ALIGN  32
176
177 static inline void *enetc_si_priv(const struct enetc_si *si)
178 {
179         return (char *)si + ALIGN(sizeof(struct enetc_si), ENETC_SI_ALIGN);
180 }
181
182 static inline bool enetc_si_is_pf(struct enetc_si *si)
183 {
184         return !!(si->hw.port);
185 }
186
187 #define ENETC_MAX_NUM_TXQS      8
188 #define ENETC_INT_NAME_MAX      (IFNAMSIZ + 8)
189
190 struct enetc_int_vector {
191         void __iomem *rbier;
192         void __iomem *tbier_base;
193         unsigned long tx_rings_map;
194         int count_tx_rings;
195         struct napi_struct napi;
196         char name[ENETC_INT_NAME_MAX];
197
198         struct enetc_bdr rx_ring;
199         struct enetc_bdr tx_ring[];
200 };
201
202 struct enetc_cls_rule {
203         struct ethtool_rx_flow_spec fs;
204         int used;
205 };
206
207 #define ENETC_MAX_BDR_INT       2 /* fixed to max # of available cpus */
208 struct psfp_cap {
209         u32 max_streamid;
210         u32 max_psfp_filter;
211         u32 max_psfp_gate;
212         u32 max_psfp_gatelist;
213         u32 max_psfp_meter;
214 };
215
216 /* TODO: more hardware offloads */
217 enum enetc_active_offloads {
218         ENETC_F_RX_TSTAMP       = BIT(0),
219         ENETC_F_TX_TSTAMP       = BIT(1),
220         ENETC_F_QBV             = BIT(2),
221         ENETC_F_QCI             = BIT(3),
222 };
223
224 struct enetc_ndev_priv {
225         struct net_device *ndev;
226         struct device *dev; /* dma-mapping device */
227         struct enetc_si *si;
228
229         int bdr_int_num; /* number of Rx/Tx ring interrupts */
230         struct enetc_int_vector *int_vector[ENETC_MAX_BDR_INT];
231         u16 num_rx_rings, num_tx_rings;
232         u16 rx_bd_count, tx_bd_count;
233
234         u16 msg_enable;
235         int active_offloads;
236
237         u32 speed; /* store speed for compare update pspeed */
238
239         struct enetc_bdr *tx_ring[16];
240         struct enetc_bdr *rx_ring[16];
241
242         struct enetc_cls_rule *cls_rules;
243
244         struct psfp_cap psfp_cap;
245
246         struct device_node *phy_node;
247         phy_interface_t if_mode;
248 };
249
250 /* Messaging */
251
252 /* VF-PF set primary MAC address message format */
253 struct enetc_msg_cmd_set_primary_mac {
254         struct enetc_msg_cmd_header header;
255         struct sockaddr mac;
256 };
257
258 #define ENETC_CBD(R, i) (&(((struct enetc_cbd *)((R).bd_base))[i]))
259
260 #define ENETC_CBDR_TIMEOUT      1000 /* usecs */
261
262 /* PTP driver exports */
263 extern int enetc_phc_index;
264
265 /* SI common */
266 int enetc_pci_probe(struct pci_dev *pdev, const char *name, int sizeof_priv);
267 void enetc_pci_remove(struct pci_dev *pdev);
268 int enetc_alloc_msix(struct enetc_ndev_priv *priv);
269 void enetc_free_msix(struct enetc_ndev_priv *priv);
270 void enetc_get_si_caps(struct enetc_si *si);
271 void enetc_init_si_rings_params(struct enetc_ndev_priv *priv);
272 int enetc_alloc_si_resources(struct enetc_ndev_priv *priv);
273 void enetc_free_si_resources(struct enetc_ndev_priv *priv);
274
275 int enetc_open(struct net_device *ndev);
276 int enetc_close(struct net_device *ndev);
277 netdev_tx_t enetc_xmit(struct sk_buff *skb, struct net_device *ndev);
278 struct net_device_stats *enetc_get_stats(struct net_device *ndev);
279 int enetc_set_features(struct net_device *ndev,
280                        netdev_features_t features);
281 int enetc_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd);
282 int enetc_setup_tc(struct net_device *ndev, enum tc_setup_type type,
283                    void *type_data);
284
285 /* ethtool */
286 void enetc_set_ethtool_ops(struct net_device *ndev);
287
288 /* control buffer descriptor ring (CBDR) */
289 int enetc_set_mac_flt_entry(struct enetc_si *si, int index,
290                             char *mac_addr, int si_map);
291 int enetc_clear_mac_flt_entry(struct enetc_si *si, int index);
292 int enetc_set_fs_entry(struct enetc_si *si, struct enetc_cmd_rfse *rfse,
293                        int index);
294 void enetc_set_rss_key(struct enetc_hw *hw, const u8 *bytes);
295 int enetc_get_rss_table(struct enetc_si *si, u32 *table, int count);
296 int enetc_set_rss_table(struct enetc_si *si, const u32 *table, int count);
297 int enetc_send_cmd(struct enetc_si *si, struct enetc_cbd *cbd);
298
299 #ifdef CONFIG_FSL_ENETC_QOS
300 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data);
301 void enetc_sched_speed_set(struct net_device *ndev);
302 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data);
303 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data);
304 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
305                             void *cb_priv);
306 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data);
307 int enetc_psfp_init(struct enetc_ndev_priv *priv);
308 int enetc_psfp_clean(struct enetc_ndev_priv *priv);
309
310 static inline void enetc_get_max_cap(struct enetc_ndev_priv *priv)
311 {
312         u32 reg;
313
314         reg = enetc_port_rd(&priv->si->hw, ENETC_PSIDCAPR);
315         priv->psfp_cap.max_streamid = reg & ENETC_PSIDCAPR_MSK;
316         /* Port stream filter capability */
317         reg = enetc_port_rd(&priv->si->hw, ENETC_PSFCAPR);
318         priv->psfp_cap.max_psfp_filter = reg & ENETC_PSFCAPR_MSK;
319         /* Port stream gate capability */
320         reg = enetc_port_rd(&priv->si->hw, ENETC_PSGCAPR);
321         priv->psfp_cap.max_psfp_gate = (reg & ENETC_PSGCAPR_SGIT_MSK);
322         priv->psfp_cap.max_psfp_gatelist = (reg & ENETC_PSGCAPR_GCL_MSK) >> 16;
323         /* Port flow meter capability */
324         reg = enetc_port_rd(&priv->si->hw, ENETC_PFMCAPR);
325         priv->psfp_cap.max_psfp_meter = reg & ENETC_PFMCAPR_MSK;
326 }
327
328 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
329 {
330         struct enetc_hw *hw = &priv->si->hw;
331         int err;
332
333         enetc_get_max_cap(priv);
334
335         err = enetc_psfp_init(priv);
336         if (err)
337                 return err;
338
339         enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) |
340                  ENETC_PPSFPMR_PSFPEN | ENETC_PPSFPMR_VS |
341                  ENETC_PPSFPMR_PVC | ENETC_PPSFPMR_PVZC);
342
343         return 0;
344 }
345
346 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
347 {
348         struct enetc_hw *hw = &priv->si->hw;
349         int err;
350
351         err = enetc_psfp_clean(priv);
352         if (err)
353                 return err;
354
355         enetc_wr(hw, ENETC_PPSFPMR, enetc_rd(hw, ENETC_PPSFPMR) &
356                  ~ENETC_PPSFPMR_PSFPEN & ~ENETC_PPSFPMR_VS &
357                  ~ENETC_PPSFPMR_PVC & ~ENETC_PPSFPMR_PVZC);
358
359         memset(&priv->psfp_cap, 0, sizeof(struct psfp_cap));
360
361         return 0;
362 }
363
364 #else
365 #define enetc_setup_tc_taprio(ndev, type_data) -EOPNOTSUPP
366 #define enetc_sched_speed_set(ndev) (void)0
367 #define enetc_setup_tc_cbs(ndev, type_data) -EOPNOTSUPP
368 #define enetc_setup_tc_txtime(ndev, type_data) -EOPNOTSUPP
369 #define enetc_setup_tc_psfp(ndev, type_data) -EOPNOTSUPP
370 #define enetc_setup_tc_block_cb NULL
371
372 #define enetc_get_max_cap(p)            \
373         memset(&((p)->psfp_cap), 0, sizeof(struct psfp_cap))
374
375 static inline int enetc_psfp_enable(struct enetc_ndev_priv *priv)
376 {
377         return 0;
378 }
379
380 static inline int enetc_psfp_disable(struct enetc_ndev_priv *priv)
381 {
382         return 0;
383 }
384 #endif