8a3008c865348c166016345bbbb83f8af607f8ce
[linux-2.6-microblaze.git] / drivers / net / ethernet / microchip / sparx5 / sparx5_netdev.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip Sparx5 Switch driver
3  *
4  * Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
5  */
6
7 #include "sparx5_main_regs.h"
8 #include "sparx5_main.h"
9 #include "sparx5_port.h"
10
11 /* The IFH bit position of the first VSTAX bit. This is because the
12  * VSTAX bit positions in Data sheet is starting from zero.
13  */
14 #define VSTAX 73
15
16 static void ifh_encode_bitfield(void *ifh, u64 value, u32 pos, u32 width)
17 {
18         u8 *ifh_hdr = ifh;
19         /* Calculate the Start IFH byte position of this IFH bit position */
20         u32 byte = (35 - (pos / 8));
21         /* Calculate the Start bit position in the Start IFH byte */
22         u32 bit  = (pos % 8);
23         u64 encode = GENMASK(bit + width - 1, bit) & (value << bit);
24
25         /* Max width is 5 bytes - 40 bits. In worst case this will
26          * spread over 6 bytes - 48 bits
27          */
28         compiletime_assert(width <= 40, "Unsupported width, must be <= 40");
29
30         /* The b0-b7 goes into the start IFH byte */
31         if (encode & 0xFF)
32                 ifh_hdr[byte] |= (u8)((encode & 0xFF));
33         /* The b8-b15 goes into the next IFH byte */
34         if (encode & 0xFF00)
35                 ifh_hdr[byte - 1] |= (u8)((encode & 0xFF00) >> 8);
36         /* The b16-b23 goes into the next IFH byte */
37         if (encode & 0xFF0000)
38                 ifh_hdr[byte - 2] |= (u8)((encode & 0xFF0000) >> 16);
39         /* The b24-b31 goes into the next IFH byte */
40         if (encode & 0xFF000000)
41                 ifh_hdr[byte - 3] |= (u8)((encode & 0xFF000000) >> 24);
42         /* The b32-b39 goes into the next IFH byte */
43         if (encode & 0xFF00000000)
44                 ifh_hdr[byte - 4] |= (u8)((encode & 0xFF00000000) >> 32);
45         /* The b40-b47 goes into the next IFH byte */
46         if (encode & 0xFF0000000000)
47                 ifh_hdr[byte - 5] |= (u8)((encode & 0xFF0000000000) >> 40);
48 }
49
50 static void sparx5_set_port_ifh(void *ifh_hdr, u16 portno)
51 {
52         /* VSTAX.RSV = 1. MSBit must be 1 */
53         ifh_encode_bitfield(ifh_hdr, 1, VSTAX + 79,  1);
54         /* VSTAX.INGR_DROP_MODE = Enable. Don't make head-of-line blocking */
55         ifh_encode_bitfield(ifh_hdr, 1, VSTAX + 55,  1);
56         /* MISC.CPU_MASK/DPORT = Destination port */
57         ifh_encode_bitfield(ifh_hdr, portno,   29, 8);
58         /* MISC.PIPELINE_PT */
59         ifh_encode_bitfield(ifh_hdr, 16,       37, 5);
60         /* MISC.PIPELINE_ACT */
61         ifh_encode_bitfield(ifh_hdr, 1,        42, 3);
62         /* FWD.SRC_PORT = CPU */
63         ifh_encode_bitfield(ifh_hdr, SPX5_PORT_CPU, 46, 7);
64         /* FWD.SFLOW_ID (disable SFlow sampling) */
65         ifh_encode_bitfield(ifh_hdr, 124,      57, 7);
66         /* FWD.UPDATE_FCS = Enable. Enforce update of FCS. */
67         ifh_encode_bitfield(ifh_hdr, 1,        67, 1);
68 }
69
70 static int sparx5_port_open(struct net_device *ndev)
71 {
72         struct sparx5_port *port = netdev_priv(ndev);
73         int err = 0;
74
75         sparx5_port_enable(port, true);
76         err = phylink_of_phy_connect(port->phylink, port->of_node, 0);
77         if (err) {
78                 netdev_err(ndev, "Could not attach to PHY\n");
79                 return err;
80         }
81
82         phylink_start(port->phylink);
83
84         if (!ndev->phydev) {
85                 /* power up serdes */
86                 port->conf.power_down = false;
87                 if (port->conf.serdes_reset)
88                         err = sparx5_serdes_set(port->sparx5, port, &port->conf);
89                 else
90                         err = phy_power_on(port->serdes);
91                 if (err)
92                         netdev_err(ndev, "%s failed\n", __func__);
93         }
94
95         return err;
96 }
97
98 static int sparx5_port_stop(struct net_device *ndev)
99 {
100         struct sparx5_port *port = netdev_priv(ndev);
101         int err = 0;
102
103         sparx5_port_enable(port, false);
104         phylink_stop(port->phylink);
105         phylink_disconnect_phy(port->phylink);
106
107         if (!ndev->phydev) {
108                 /* power down serdes */
109                 port->conf.power_down = true;
110                 if (port->conf.serdes_reset)
111                         err = sparx5_serdes_set(port->sparx5, port, &port->conf);
112                 else
113                         err = phy_power_off(port->serdes);
114                 if (err)
115                         netdev_err(ndev, "%s failed\n", __func__);
116         }
117         return 0;
118 }
119
120 static int sparx5_port_get_phys_port_name(struct net_device *dev,
121                                           char *buf, size_t len)
122 {
123         struct sparx5_port *port = netdev_priv(dev);
124         int ret;
125
126         ret = snprintf(buf, len, "p%d", port->portno);
127         if (ret >= len)
128                 return -EINVAL;
129
130         return 0;
131 }
132
133 static int sparx5_set_mac_address(struct net_device *dev, void *p)
134 {
135         struct sparx5_port *port = netdev_priv(dev);
136         struct sparx5 *sparx5 = port->sparx5;
137         const struct sockaddr *addr = p;
138
139         if (!is_valid_ether_addr(addr->sa_data))
140                 return -EADDRNOTAVAIL;
141
142         /* Remove current */
143         sparx5_mact_forget(sparx5, dev->dev_addr,  port->pvid);
144
145         /* Add new */
146         sparx5_mact_learn(sparx5, PGID_CPU, addr->sa_data, port->pvid);
147
148         /* Record the address */
149         ether_addr_copy(dev->dev_addr, addr->sa_data);
150
151         return 0;
152 }
153
154 static int sparx5_get_port_parent_id(struct net_device *dev,
155                                      struct netdev_phys_item_id *ppid)
156 {
157         struct sparx5_port *sparx5_port = netdev_priv(dev);
158         struct sparx5 *sparx5 = sparx5_port->sparx5;
159
160         ppid->id_len = sizeof(sparx5->base_mac);
161         memcpy(&ppid->id, &sparx5->base_mac, ppid->id_len);
162
163         return 0;
164 }
165
166 static const struct net_device_ops sparx5_port_netdev_ops = {
167         .ndo_open               = sparx5_port_open,
168         .ndo_stop               = sparx5_port_stop,
169         .ndo_start_xmit         = sparx5_port_xmit_impl,
170         .ndo_get_phys_port_name = sparx5_port_get_phys_port_name,
171         .ndo_set_mac_address    = sparx5_set_mac_address,
172         .ndo_validate_addr      = eth_validate_addr,
173         .ndo_get_port_parent_id = sparx5_get_port_parent_id,
174 };
175
176 bool sparx5_netdevice_check(const struct net_device *dev)
177 {
178         return dev && (dev->netdev_ops == &sparx5_port_netdev_ops);
179 }
180
181 struct net_device *sparx5_create_netdev(struct sparx5 *sparx5, u32 portno)
182 {
183         struct sparx5_port *spx5_port;
184         struct net_device *ndev;
185         u64 val;
186
187         ndev = devm_alloc_etherdev(sparx5->dev, sizeof(struct sparx5_port));
188         if (!ndev)
189                 return ERR_PTR(-ENOMEM);
190
191         SET_NETDEV_DEV(ndev, sparx5->dev);
192         spx5_port = netdev_priv(ndev);
193         spx5_port->ndev = ndev;
194         spx5_port->sparx5 = sparx5;
195         spx5_port->portno = portno;
196         sparx5_set_port_ifh(spx5_port->ifh, portno);
197
198         ndev->netdev_ops = &sparx5_port_netdev_ops;
199
200         val = ether_addr_to_u64(sparx5->base_mac) + portno + 1;
201         u64_to_ether_addr(val, ndev->dev_addr);
202
203         return ndev;
204 }
205
206 int sparx5_register_netdevs(struct sparx5 *sparx5)
207 {
208         int portno;
209         int err;
210
211         for (portno = 0; portno < SPX5_PORTS; portno++)
212                 if (sparx5->ports[portno]) {
213                         err = register_netdev(sparx5->ports[portno]->ndev);
214                         if (err) {
215                                 dev_err(sparx5->dev,
216                                         "port: %02u: netdev registration failed\n",
217                                         portno);
218                                 return err;
219                         }
220                         sparx5_port_inj_timer_setup(sparx5->ports[portno]);
221                 }
222         return 0;
223 }
224
225 void sparx5_destroy_netdevs(struct sparx5 *sparx5)
226 {
227         struct sparx5_port *port;
228         int portno;
229
230         for (portno = 0; portno < SPX5_PORTS; portno++) {
231                 port = sparx5->ports[portno];
232                 if (port && port->phylink) {
233                         /* Disconnect the phy */
234                         rtnl_lock();
235                         sparx5_port_stop(port->ndev);
236                         phylink_disconnect_phy(port->phylink);
237                         rtnl_unlock();
238                         phylink_destroy(port->phylink);
239                         port->phylink = NULL;
240                 }
241         }
242 }
243
244 void sparx5_unregister_netdevs(struct sparx5 *sparx5)
245 {
246         int portno;
247
248         for (portno = 0; portno < SPX5_PORTS; portno++)
249                 if (sparx5->ports[portno])
250                         unregister_netdev(sparx5->ports[portno]->ndev);
251 }
252