]>
Commit | Line | Data |
---|---|---|
8aa9ebcc VO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH | |
3 | * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com> | |
4 | */ | |
5 | ||
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | ||
8 | #include <linux/delay.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/printk.h> | |
11 | #include <linux/spi/spi.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/gpio/consumer.h> | |
ad9f299a | 14 | #include <linux/phylink.h> |
8aa9ebcc VO |
15 | #include <linux/of.h> |
16 | #include <linux/of_net.h> | |
17 | #include <linux/of_mdio.h> | |
18 | #include <linux/of_device.h> | |
19 | #include <linux/netdev_features.h> | |
20 | #include <linux/netdevice.h> | |
21 | #include <linux/if_bridge.h> | |
22 | #include <linux/if_ether.h> | |
227d07a0 | 23 | #include <linux/dsa/8021q.h> |
8aa9ebcc VO |
24 | #include "sja1105.h" |
25 | ||
26 | static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, | |
27 | unsigned int startup_delay) | |
28 | { | |
29 | gpiod_set_value_cansleep(gpio, 1); | |
30 | /* Wait for minimum reset pulse length */ | |
31 | msleep(pulse_len); | |
32 | gpiod_set_value_cansleep(gpio, 0); | |
33 | /* Wait until chip is ready after reset */ | |
34 | msleep(startup_delay); | |
35 | } | |
36 | ||
37 | static void | |
38 | sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, | |
39 | int from, int to, bool allow) | |
40 | { | |
41 | if (allow) { | |
42 | l2_fwd[from].bc_domain |= BIT(to); | |
43 | l2_fwd[from].reach_port |= BIT(to); | |
44 | l2_fwd[from].fl_domain |= BIT(to); | |
45 | } else { | |
46 | l2_fwd[from].bc_domain &= ~BIT(to); | |
47 | l2_fwd[from].reach_port &= ~BIT(to); | |
48 | l2_fwd[from].fl_domain &= ~BIT(to); | |
49 | } | |
50 | } | |
51 | ||
52 | /* Structure used to temporarily transport device tree | |
53 | * settings into sja1105_setup | |
54 | */ | |
55 | struct sja1105_dt_port { | |
56 | phy_interface_t phy_mode; | |
57 | sja1105_mii_role_t role; | |
58 | }; | |
59 | ||
60 | static int sja1105_init_mac_settings(struct sja1105_private *priv) | |
61 | { | |
62 | struct sja1105_mac_config_entry default_mac = { | |
63 | /* Enable all 8 priority queues on egress. | |
64 | * Every queue i holds top[i] - base[i] frames. | |
65 | * Sum of top[i] - base[i] is 511 (max hardware limit). | |
66 | */ | |
67 | .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, | |
68 | .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, | |
69 | .enabled = {true, true, true, true, true, true, true, true}, | |
70 | /* Keep standard IFG of 12 bytes on egress. */ | |
71 | .ifg = 0, | |
72 | /* Always put the MAC speed in automatic mode, where it can be | |
1fd4a173 | 73 | * adjusted at runtime by PHYLINK. |
8aa9ebcc VO |
74 | */ |
75 | .speed = SJA1105_SPEED_AUTO, | |
76 | /* No static correction for 1-step 1588 events */ | |
77 | .tp_delin = 0, | |
78 | .tp_delout = 0, | |
79 | /* Disable aging for critical TTEthernet traffic */ | |
80 | .maxage = 0xFF, | |
81 | /* Internal VLAN (pvid) to apply to untagged ingress */ | |
82 | .vlanprio = 0, | |
e3502b82 | 83 | .vlanid = 1, |
8aa9ebcc VO |
84 | .ing_mirr = false, |
85 | .egr_mirr = false, | |
86 | /* Don't drop traffic with other EtherType than ETH_P_IP */ | |
87 | .drpnona664 = false, | |
88 | /* Don't drop double-tagged traffic */ | |
89 | .drpdtag = false, | |
90 | /* Don't drop untagged traffic */ | |
91 | .drpuntag = false, | |
92 | /* Don't retag 802.1p (VID 0) traffic with the pvid */ | |
93 | .retag = false, | |
640f763f VO |
94 | /* Disable learning and I/O on user ports by default - |
95 | * STP will enable it. | |
96 | */ | |
97 | .dyn_learn = false, | |
8aa9ebcc VO |
98 | .egress = false, |
99 | .ingress = false, | |
100 | }; | |
101 | struct sja1105_mac_config_entry *mac; | |
102 | struct sja1105_table *table; | |
103 | int i; | |
104 | ||
105 | table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; | |
106 | ||
107 | /* Discard previous MAC Configuration Table */ | |
108 | if (table->entry_count) { | |
109 | kfree(table->entries); | |
110 | table->entry_count = 0; | |
111 | } | |
112 | ||
113 | table->entries = kcalloc(SJA1105_NUM_PORTS, | |
114 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
115 | if (!table->entries) | |
116 | return -ENOMEM; | |
117 | ||
8aa9ebcc VO |
118 | table->entry_count = SJA1105_NUM_PORTS; |
119 | ||
120 | mac = table->entries; | |
121 | ||
640f763f | 122 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
8aa9ebcc | 123 | mac[i] = default_mac; |
640f763f VO |
124 | if (i == dsa_upstream_port(priv->ds, i)) { |
125 | /* STP doesn't get called for CPU port, so we need to | |
126 | * set the I/O parameters statically. | |
127 | */ | |
128 | mac[i].dyn_learn = true; | |
129 | mac[i].ingress = true; | |
130 | mac[i].egress = true; | |
131 | } | |
132 | } | |
8aa9ebcc VO |
133 | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static int sja1105_init_mii_settings(struct sja1105_private *priv, | |
138 | struct sja1105_dt_port *ports) | |
139 | { | |
140 | struct device *dev = &priv->spidev->dev; | |
141 | struct sja1105_xmii_params_entry *mii; | |
142 | struct sja1105_table *table; | |
143 | int i; | |
144 | ||
145 | table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; | |
146 | ||
147 | /* Discard previous xMII Mode Parameters Table */ | |
148 | if (table->entry_count) { | |
149 | kfree(table->entries); | |
150 | table->entry_count = 0; | |
151 | } | |
152 | ||
153 | table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, | |
154 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
155 | if (!table->entries) | |
156 | return -ENOMEM; | |
157 | ||
1fd4a173 | 158 | /* Override table based on PHYLINK DT bindings */ |
8aa9ebcc VO |
159 | table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; |
160 | ||
161 | mii = table->entries; | |
162 | ||
163 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
164 | switch (ports[i].phy_mode) { | |
165 | case PHY_INTERFACE_MODE_MII: | |
166 | mii->xmii_mode[i] = XMII_MODE_MII; | |
167 | break; | |
168 | case PHY_INTERFACE_MODE_RMII: | |
169 | mii->xmii_mode[i] = XMII_MODE_RMII; | |
170 | break; | |
171 | case PHY_INTERFACE_MODE_RGMII: | |
172 | case PHY_INTERFACE_MODE_RGMII_ID: | |
173 | case PHY_INTERFACE_MODE_RGMII_RXID: | |
174 | case PHY_INTERFACE_MODE_RGMII_TXID: | |
175 | mii->xmii_mode[i] = XMII_MODE_RGMII; | |
176 | break; | |
177 | default: | |
178 | dev_err(dev, "Unsupported PHY mode %s!\n", | |
179 | phy_modes(ports[i].phy_mode)); | |
180 | } | |
181 | ||
182 | mii->phy_mac[i] = ports[i].role; | |
183 | } | |
184 | return 0; | |
185 | } | |
186 | ||
187 | static int sja1105_init_static_fdb(struct sja1105_private *priv) | |
188 | { | |
189 | struct sja1105_table *table; | |
190 | ||
191 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; | |
192 | ||
291d1e72 VO |
193 | /* We only populate the FDB table through dynamic |
194 | * L2 Address Lookup entries | |
195 | */ | |
8aa9ebcc VO |
196 | if (table->entry_count) { |
197 | kfree(table->entries); | |
198 | table->entry_count = 0; | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) | |
204 | { | |
205 | struct sja1105_table *table; | |
6c56e167 | 206 | u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS; |
8aa9ebcc | 207 | struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { |
8456721d VO |
208 | /* Learned FDB entries are forgotten after 300 seconds */ |
209 | .maxage = SJA1105_AGEING_TIME_MS(300000), | |
8aa9ebcc VO |
210 | /* All entries within a FDB bin are available for learning */ |
211 | .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, | |
1da73821 VO |
212 | /* And the P/Q/R/S equivalent setting: */ |
213 | .start_dynspc = 0, | |
6c56e167 VO |
214 | .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries, |
215 | max_fdb_entries, max_fdb_entries, }, | |
8aa9ebcc VO |
216 | /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ |
217 | .poly = 0x97, | |
218 | /* This selects between Independent VLAN Learning (IVL) and | |
219 | * Shared VLAN Learning (SVL) | |
220 | */ | |
221 | .shared_learn = false, | |
222 | /* Don't discard management traffic based on ENFPORT - | |
223 | * we don't perform SMAC port enforcement anyway, so | |
224 | * what we are setting here doesn't matter. | |
225 | */ | |
226 | .no_enf_hostprt = false, | |
227 | /* Don't learn SMAC for mac_fltres1 and mac_fltres0. | |
228 | * Maybe correlate with no_linklocal_learn from bridge driver? | |
229 | */ | |
230 | .no_mgmt_learn = true, | |
1da73821 VO |
231 | /* P/Q/R/S only */ |
232 | .use_static = true, | |
233 | /* Dynamically learned FDB entries can overwrite other (older) | |
234 | * dynamic FDB entries | |
235 | */ | |
236 | .owr_dyn = true, | |
237 | .drpnolearn = true, | |
8aa9ebcc VO |
238 | }; |
239 | ||
240 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; | |
241 | ||
242 | if (table->entry_count) { | |
243 | kfree(table->entries); | |
244 | table->entry_count = 0; | |
245 | } | |
246 | ||
247 | table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, | |
248 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
249 | if (!table->entries) | |
250 | return -ENOMEM; | |
251 | ||
252 | table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; | |
253 | ||
254 | /* This table only has a single entry */ | |
255 | ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = | |
256 | default_l2_lookup_params; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | static int sja1105_init_static_vlan(struct sja1105_private *priv) | |
262 | { | |
263 | struct sja1105_table *table; | |
264 | struct sja1105_vlan_lookup_entry pvid = { | |
265 | .ving_mirr = 0, | |
266 | .vegr_mirr = 0, | |
267 | .vmemb_port = 0, | |
268 | .vlan_bc = 0, | |
269 | .tag_port = 0, | |
e3502b82 | 270 | .vlanid = 1, |
8aa9ebcc VO |
271 | }; |
272 | int i; | |
273 | ||
274 | table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; | |
275 | ||
e3502b82 | 276 | /* The static VLAN table will only contain the initial pvid of 1. |
6666cebc VO |
277 | * All other VLANs are to be configured through dynamic entries, |
278 | * and kept in the static configuration table as backing memory. | |
8aa9ebcc VO |
279 | */ |
280 | if (table->entry_count) { | |
281 | kfree(table->entries); | |
282 | table->entry_count = 0; | |
283 | } | |
284 | ||
285 | table->entries = kcalloc(1, table->ops->unpacked_entry_size, | |
286 | GFP_KERNEL); | |
287 | if (!table->entries) | |
288 | return -ENOMEM; | |
289 | ||
290 | table->entry_count = 1; | |
291 | ||
e3502b82 | 292 | /* VLAN 1: all DT-defined ports are members; no restrictions on |
8aa9ebcc VO |
293 | * forwarding; always transmit priority-tagged frames as untagged. |
294 | */ | |
295 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
296 | pvid.vmemb_port |= BIT(i); | |
297 | pvid.vlan_bc |= BIT(i); | |
298 | pvid.tag_port &= ~BIT(i); | |
299 | } | |
300 | ||
301 | ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; | |
302 | return 0; | |
303 | } | |
304 | ||
305 | static int sja1105_init_l2_forwarding(struct sja1105_private *priv) | |
306 | { | |
307 | struct sja1105_l2_forwarding_entry *l2fwd; | |
308 | struct sja1105_table *table; | |
309 | int i, j; | |
310 | ||
311 | table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; | |
312 | ||
313 | if (table->entry_count) { | |
314 | kfree(table->entries); | |
315 | table->entry_count = 0; | |
316 | } | |
317 | ||
318 | table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, | |
319 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
320 | if (!table->entries) | |
321 | return -ENOMEM; | |
322 | ||
323 | table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; | |
324 | ||
325 | l2fwd = table->entries; | |
326 | ||
327 | /* First 5 entries define the forwarding rules */ | |
328 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
329 | unsigned int upstream = dsa_upstream_port(priv->ds, i); | |
330 | ||
331 | for (j = 0; j < SJA1105_NUM_TC; j++) | |
332 | l2fwd[i].vlan_pmap[j] = j; | |
333 | ||
334 | if (i == upstream) | |
335 | continue; | |
336 | ||
337 | sja1105_port_allow_traffic(l2fwd, i, upstream, true); | |
338 | sja1105_port_allow_traffic(l2fwd, upstream, i, true); | |
339 | } | |
340 | /* Next 8 entries define VLAN PCP mapping from ingress to egress. | |
341 | * Create a one-to-one mapping. | |
342 | */ | |
343 | for (i = 0; i < SJA1105_NUM_TC; i++) | |
344 | for (j = 0; j < SJA1105_NUM_PORTS; j++) | |
345 | l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) | |
351 | { | |
352 | struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { | |
353 | /* Disallow dynamic reconfiguration of vlan_pmap */ | |
354 | .max_dynp = 0, | |
355 | /* Use a single memory partition for all ingress queues */ | |
356 | .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, | |
357 | }; | |
358 | struct sja1105_table *table; | |
359 | ||
360 | table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; | |
361 | ||
362 | if (table->entry_count) { | |
363 | kfree(table->entries); | |
364 | table->entry_count = 0; | |
365 | } | |
366 | ||
367 | table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, | |
368 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
369 | if (!table->entries) | |
370 | return -ENOMEM; | |
371 | ||
372 | table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; | |
373 | ||
374 | /* This table only has a single entry */ | |
375 | ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = | |
376 | default_l2fwd_params; | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | static int sja1105_init_general_params(struct sja1105_private *priv) | |
382 | { | |
383 | struct sja1105_general_params_entry default_general_params = { | |
384 | /* Disallow dynamic changing of the mirror port */ | |
385 | .mirr_ptacu = 0, | |
386 | .switchid = priv->ds->index, | |
387 | /* Priority queue for link-local frames trapped to CPU */ | |
08fde09a | 388 | .hostprio = 7, |
8aa9ebcc VO |
389 | .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, |
390 | .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, | |
42824463 | 391 | .incl_srcpt1 = false, |
8aa9ebcc VO |
392 | .send_meta1 = false, |
393 | .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, | |
394 | .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, | |
42824463 | 395 | .incl_srcpt0 = false, |
8aa9ebcc VO |
396 | .send_meta0 = false, |
397 | /* The destination for traffic matching mac_fltres1 and | |
398 | * mac_fltres0 on all ports except host_port. Such traffic | |
399 | * receieved on host_port itself would be dropped, except | |
400 | * by installing a temporary 'management route' | |
401 | */ | |
402 | .host_port = dsa_upstream_port(priv->ds, 0), | |
403 | /* Same as host port */ | |
404 | .mirr_port = dsa_upstream_port(priv->ds, 0), | |
405 | /* Link-local traffic received on casc_port will be forwarded | |
406 | * to host_port without embedding the source port and device ID | |
407 | * info in the destination MAC address (presumably because it | |
408 | * is a cascaded port and a downstream SJA switch already did | |
409 | * that). Default to an invalid port (to disable the feature) | |
410 | * and overwrite this if we find any DSA (cascaded) ports. | |
411 | */ | |
412 | .casc_port = SJA1105_NUM_PORTS, | |
413 | /* No TTEthernet */ | |
414 | .vllupformat = 0, | |
415 | .vlmarker = 0, | |
416 | .vlmask = 0, | |
417 | /* Only update correctionField for 1-step PTP (L2 transport) */ | |
418 | .ignore2stf = 0, | |
6666cebc VO |
419 | /* Forcefully disable VLAN filtering by telling |
420 | * the switch that VLAN has a different EtherType. | |
421 | */ | |
422 | .tpid = ETH_P_SJA1105, | |
423 | .tpid2 = ETH_P_SJA1105, | |
8aa9ebcc VO |
424 | }; |
425 | struct sja1105_table *table; | |
227d07a0 | 426 | int i, k = 0; |
8aa9ebcc | 427 | |
227d07a0 | 428 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
8aa9ebcc VO |
429 | if (dsa_is_dsa_port(priv->ds, i)) |
430 | default_general_params.casc_port = i; | |
227d07a0 VO |
431 | else if (dsa_is_user_port(priv->ds, i)) |
432 | priv->ports[i].mgmt_slot = k++; | |
433 | } | |
8aa9ebcc VO |
434 | |
435 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; | |
436 | ||
437 | if (table->entry_count) { | |
438 | kfree(table->entries); | |
439 | table->entry_count = 0; | |
440 | } | |
441 | ||
442 | table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, | |
443 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
444 | if (!table->entries) | |
445 | return -ENOMEM; | |
446 | ||
447 | table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; | |
448 | ||
449 | /* This table only has a single entry */ | |
450 | ((struct sja1105_general_params_entry *)table->entries)[0] = | |
451 | default_general_params; | |
452 | ||
453 | return 0; | |
454 | } | |
455 | ||
456 | #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) | |
457 | ||
458 | static inline void | |
459 | sja1105_setup_policer(struct sja1105_l2_policing_entry *policing, | |
460 | int index) | |
461 | { | |
462 | policing[index].sharindx = index; | |
463 | policing[index].smax = 65535; /* Burst size in bytes */ | |
464 | policing[index].rate = SJA1105_RATE_MBPS(1000); | |
465 | policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN; | |
466 | policing[index].partition = 0; | |
467 | } | |
468 | ||
469 | static int sja1105_init_l2_policing(struct sja1105_private *priv) | |
470 | { | |
471 | struct sja1105_l2_policing_entry *policing; | |
472 | struct sja1105_table *table; | |
473 | int i, j, k; | |
474 | ||
475 | table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; | |
476 | ||
477 | /* Discard previous L2 Policing Table */ | |
478 | if (table->entry_count) { | |
479 | kfree(table->entries); | |
480 | table->entry_count = 0; | |
481 | } | |
482 | ||
483 | table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, | |
484 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
485 | if (!table->entries) | |
486 | return -ENOMEM; | |
487 | ||
488 | table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; | |
489 | ||
490 | policing = table->entries; | |
491 | ||
492 | /* k sweeps through all unicast policers (0-39). | |
493 | * bcast sweeps through policers 40-44. | |
494 | */ | |
495 | for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) { | |
496 | int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i; | |
497 | ||
498 | for (j = 0; j < SJA1105_NUM_TC; j++, k++) | |
499 | sja1105_setup_policer(policing, k); | |
500 | ||
501 | /* Set up this port's policer for broadcast traffic */ | |
502 | sja1105_setup_policer(policing, bcast); | |
503 | } | |
504 | return 0; | |
505 | } | |
506 | ||
24c01949 VO |
507 | static int sja1105_init_avb_params(struct sja1105_private *priv, |
508 | bool on) | |
509 | { | |
510 | struct sja1105_avb_params_entry *avb; | |
511 | struct sja1105_table *table; | |
512 | ||
513 | table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; | |
514 | ||
515 | /* Discard previous AVB Parameters Table */ | |
516 | if (table->entry_count) { | |
517 | kfree(table->entries); | |
518 | table->entry_count = 0; | |
519 | } | |
520 | ||
521 | /* Configure the reception of meta frames only if requested */ | |
522 | if (!on) | |
523 | return 0; | |
524 | ||
525 | table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT, | |
526 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
527 | if (!table->entries) | |
528 | return -ENOMEM; | |
529 | ||
530 | table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT; | |
531 | ||
532 | avb = table->entries; | |
533 | ||
534 | avb->destmeta = SJA1105_META_DMAC; | |
535 | avb->srcmeta = SJA1105_META_SMAC; | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
8aa9ebcc VO |
540 | static int sja1105_static_config_load(struct sja1105_private *priv, |
541 | struct sja1105_dt_port *ports) | |
542 | { | |
543 | int rc; | |
544 | ||
545 | sja1105_static_config_free(&priv->static_config); | |
546 | rc = sja1105_static_config_init(&priv->static_config, | |
547 | priv->info->static_ops, | |
548 | priv->info->device_id); | |
549 | if (rc) | |
550 | return rc; | |
551 | ||
552 | /* Build static configuration */ | |
553 | rc = sja1105_init_mac_settings(priv); | |
554 | if (rc < 0) | |
555 | return rc; | |
556 | rc = sja1105_init_mii_settings(priv, ports); | |
557 | if (rc < 0) | |
558 | return rc; | |
559 | rc = sja1105_init_static_fdb(priv); | |
560 | if (rc < 0) | |
561 | return rc; | |
562 | rc = sja1105_init_static_vlan(priv); | |
563 | if (rc < 0) | |
564 | return rc; | |
565 | rc = sja1105_init_l2_lookup_params(priv); | |
566 | if (rc < 0) | |
567 | return rc; | |
568 | rc = sja1105_init_l2_forwarding(priv); | |
569 | if (rc < 0) | |
570 | return rc; | |
571 | rc = sja1105_init_l2_forwarding_params(priv); | |
572 | if (rc < 0) | |
573 | return rc; | |
574 | rc = sja1105_init_l2_policing(priv); | |
575 | if (rc < 0) | |
576 | return rc; | |
577 | rc = sja1105_init_general_params(priv); | |
24c01949 VO |
578 | if (rc < 0) |
579 | return rc; | |
580 | rc = sja1105_init_avb_params(priv, false); | |
8aa9ebcc VO |
581 | if (rc < 0) |
582 | return rc; | |
583 | ||
584 | /* Send initial configuration to hardware via SPI */ | |
585 | return sja1105_static_config_upload(priv); | |
586 | } | |
587 | ||
f5b8631c VO |
588 | static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, |
589 | const struct sja1105_dt_port *ports) | |
590 | { | |
591 | int i; | |
592 | ||
593 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
594 | if (ports->role == XMII_MAC) | |
595 | continue; | |
596 | ||
597 | if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || | |
598 | ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) | |
599 | priv->rgmii_rx_delay[i] = true; | |
600 | ||
601 | if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || | |
602 | ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) | |
603 | priv->rgmii_tx_delay[i] = true; | |
604 | ||
605 | if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && | |
606 | !priv->info->setup_rgmii_delay) | |
607 | return -EINVAL; | |
608 | } | |
609 | return 0; | |
610 | } | |
611 | ||
8aa9ebcc VO |
612 | static int sja1105_parse_ports_node(struct sja1105_private *priv, |
613 | struct sja1105_dt_port *ports, | |
614 | struct device_node *ports_node) | |
615 | { | |
616 | struct device *dev = &priv->spidev->dev; | |
617 | struct device_node *child; | |
618 | ||
619 | for_each_child_of_node(ports_node, child) { | |
620 | struct device_node *phy_node; | |
621 | int phy_mode; | |
622 | u32 index; | |
623 | ||
624 | /* Get switch port number from DT */ | |
625 | if (of_property_read_u32(child, "reg", &index) < 0) { | |
626 | dev_err(dev, "Port number not defined in device tree " | |
627 | "(property \"reg\")\n"); | |
628 | return -ENODEV; | |
629 | } | |
630 | ||
631 | /* Get PHY mode from DT */ | |
632 | phy_mode = of_get_phy_mode(child); | |
633 | if (phy_mode < 0) { | |
634 | dev_err(dev, "Failed to read phy-mode or " | |
635 | "phy-interface-type property for port %d\n", | |
636 | index); | |
637 | return -ENODEV; | |
638 | } | |
639 | ports[index].phy_mode = phy_mode; | |
640 | ||
641 | phy_node = of_parse_phandle(child, "phy-handle", 0); | |
642 | if (!phy_node) { | |
643 | if (!of_phy_is_fixed_link(child)) { | |
644 | dev_err(dev, "phy-handle or fixed-link " | |
645 | "properties missing!\n"); | |
646 | return -ENODEV; | |
647 | } | |
648 | /* phy-handle is missing, but fixed-link isn't. | |
649 | * So it's a fixed link. Default to PHY role. | |
650 | */ | |
651 | ports[index].role = XMII_PHY; | |
652 | } else { | |
653 | /* phy-handle present => put port in MAC role */ | |
654 | ports[index].role = XMII_MAC; | |
655 | of_node_put(phy_node); | |
656 | } | |
657 | ||
658 | /* The MAC/PHY role can be overridden with explicit bindings */ | |
659 | if (of_property_read_bool(child, "sja1105,role-mac")) | |
660 | ports[index].role = XMII_MAC; | |
661 | else if (of_property_read_bool(child, "sja1105,role-phy")) | |
662 | ports[index].role = XMII_PHY; | |
663 | } | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | static int sja1105_parse_dt(struct sja1105_private *priv, | |
669 | struct sja1105_dt_port *ports) | |
670 | { | |
671 | struct device *dev = &priv->spidev->dev; | |
672 | struct device_node *switch_node = dev->of_node; | |
673 | struct device_node *ports_node; | |
674 | int rc; | |
675 | ||
676 | ports_node = of_get_child_by_name(switch_node, "ports"); | |
677 | if (!ports_node) { | |
678 | dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); | |
679 | return -ENODEV; | |
680 | } | |
681 | ||
682 | rc = sja1105_parse_ports_node(priv, ports, ports_node); | |
683 | of_node_put(ports_node); | |
684 | ||
685 | return rc; | |
686 | } | |
687 | ||
c44d0535 | 688 | /* Convert link speed from SJA1105 to ethtool encoding */ |
8aa9ebcc | 689 | static int sja1105_speed[] = { |
c44d0535 VO |
690 | [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN, |
691 | [SJA1105_SPEED_10MBPS] = SPEED_10, | |
692 | [SJA1105_SPEED_100MBPS] = SPEED_100, | |
693 | [SJA1105_SPEED_1000MBPS] = SPEED_1000, | |
8aa9ebcc VO |
694 | }; |
695 | ||
8400cff6 | 696 | /* Set link speed in the MAC configuration for a specific port. */ |
8aa9ebcc | 697 | static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, |
8400cff6 | 698 | int speed_mbps) |
8aa9ebcc VO |
699 | { |
700 | struct sja1105_xmii_params_entry *mii; | |
701 | struct sja1105_mac_config_entry *mac; | |
702 | struct device *dev = priv->ds->dev; | |
703 | sja1105_phy_interface_t phy_mode; | |
704 | sja1105_speed_t speed; | |
705 | int rc; | |
706 | ||
8400cff6 VO |
707 | /* On P/Q/R/S, one can read from the device via the MAC reconfiguration |
708 | * tables. On E/T, MAC reconfig tables are not readable, only writable. | |
709 | * We have to *know* what the MAC looks like. For the sake of keeping | |
710 | * the code common, we'll use the static configuration tables as a | |
711 | * reasonable approximation for both E/T and P/Q/R/S. | |
712 | */ | |
8aa9ebcc | 713 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; |
8400cff6 | 714 | mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; |
8aa9ebcc | 715 | |
f4cfcfbd | 716 | switch (speed_mbps) { |
c44d0535 | 717 | case SPEED_UNKNOWN: |
a979a0ab VO |
718 | /* PHYLINK called sja1105_mac_config() to inform us about |
719 | * the state->interface, but AN has not completed and the | |
720 | * speed is not yet valid. UM10944.pdf says that setting | |
721 | * SJA1105_SPEED_AUTO at runtime disables the port, so that is | |
722 | * ok for power consumption in case AN will never complete - | |
723 | * otherwise PHYLINK should come back with a new update. | |
724 | */ | |
f4cfcfbd VO |
725 | speed = SJA1105_SPEED_AUTO; |
726 | break; | |
c44d0535 | 727 | case SPEED_10: |
f4cfcfbd VO |
728 | speed = SJA1105_SPEED_10MBPS; |
729 | break; | |
c44d0535 | 730 | case SPEED_100: |
f4cfcfbd VO |
731 | speed = SJA1105_SPEED_100MBPS; |
732 | break; | |
c44d0535 | 733 | case SPEED_1000: |
f4cfcfbd VO |
734 | speed = SJA1105_SPEED_1000MBPS; |
735 | break; | |
736 | default: | |
8aa9ebcc VO |
737 | dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); |
738 | return -EINVAL; | |
739 | } | |
740 | ||
8400cff6 VO |
741 | /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration |
742 | * table, since this will be used for the clocking setup, and we no | |
743 | * longer need to store it in the static config (already told hardware | |
744 | * we want auto during upload phase). | |
8aa9ebcc | 745 | */ |
f4cfcfbd | 746 | mac[port].speed = speed; |
8aa9ebcc | 747 | |
8aa9ebcc | 748 | /* Write to the dynamic reconfiguration tables */ |
8400cff6 VO |
749 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, |
750 | &mac[port], true); | |
8aa9ebcc VO |
751 | if (rc < 0) { |
752 | dev_err(dev, "Failed to write MAC config: %d\n", rc); | |
753 | return rc; | |
754 | } | |
755 | ||
756 | /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at | |
757 | * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and | |
758 | * RMII no change of the clock setup is required. Actually, changing | |
759 | * the clock setup does interrupt the clock signal for a certain time | |
760 | * which causes trouble for all PHYs relying on this signal. | |
761 | */ | |
8aa9ebcc VO |
762 | phy_mode = mii->xmii_mode[port]; |
763 | if (phy_mode != XMII_MODE_RGMII) | |
764 | return 0; | |
765 | ||
766 | return sja1105_clocking_setup_port(priv, port); | |
767 | } | |
768 | ||
af7cd036 VO |
769 | static void sja1105_mac_config(struct dsa_switch *ds, int port, |
770 | unsigned int link_an_mode, | |
771 | const struct phylink_link_state *state) | |
8aa9ebcc VO |
772 | { |
773 | struct sja1105_private *priv = ds->priv; | |
774 | ||
8400cff6 VO |
775 | sja1105_adjust_port_config(priv, port, state->speed); |
776 | } | |
777 | ||
778 | static void sja1105_mac_link_down(struct dsa_switch *ds, int port, | |
779 | unsigned int mode, | |
780 | phy_interface_t interface) | |
781 | { | |
782 | sja1105_inhibit_tx(ds->priv, BIT(port), true); | |
783 | } | |
784 | ||
785 | static void sja1105_mac_link_up(struct dsa_switch *ds, int port, | |
786 | unsigned int mode, | |
787 | phy_interface_t interface, | |
788 | struct phy_device *phydev) | |
789 | { | |
790 | sja1105_inhibit_tx(ds->priv, BIT(port), false); | |
8aa9ebcc VO |
791 | } |
792 | ||
ad9f299a VO |
793 | static void sja1105_phylink_validate(struct dsa_switch *ds, int port, |
794 | unsigned long *supported, | |
795 | struct phylink_link_state *state) | |
796 | { | |
797 | /* Construct a new mask which exhaustively contains all link features | |
798 | * supported by the MAC, and then apply that (logical AND) to what will | |
799 | * be sent to the PHY for "marketing". | |
800 | */ | |
801 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; | |
802 | struct sja1105_private *priv = ds->priv; | |
803 | struct sja1105_xmii_params_entry *mii; | |
804 | ||
805 | mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; | |
806 | ||
807 | /* The MAC does not support pause frames, and also doesn't | |
808 | * support half-duplex traffic modes. | |
809 | */ | |
810 | phylink_set(mask, Autoneg); | |
811 | phylink_set(mask, MII); | |
812 | phylink_set(mask, 10baseT_Full); | |
813 | phylink_set(mask, 100baseT_Full); | |
814 | if (mii->xmii_mode[port] == XMII_MODE_RGMII) | |
815 | phylink_set(mask, 1000baseT_Full); | |
816 | ||
817 | bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); | |
818 | bitmap_and(state->advertising, state->advertising, mask, | |
819 | __ETHTOOL_LINK_MODE_MASK_NBITS); | |
820 | } | |
821 | ||
60f6053f VO |
822 | static int |
823 | sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, | |
824 | const struct sja1105_l2_lookup_entry *requested) | |
825 | { | |
826 | struct sja1105_l2_lookup_entry *l2_lookup; | |
827 | struct sja1105_table *table; | |
828 | int i; | |
829 | ||
830 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; | |
831 | l2_lookup = table->entries; | |
832 | ||
833 | for (i = 0; i < table->entry_count; i++) | |
834 | if (l2_lookup[i].macaddr == requested->macaddr && | |
835 | l2_lookup[i].vlanid == requested->vlanid && | |
836 | l2_lookup[i].destports & BIT(port)) | |
837 | return i; | |
838 | ||
839 | return -1; | |
840 | } | |
841 | ||
842 | /* We want FDB entries added statically through the bridge command to persist | |
843 | * across switch resets, which are a common thing during normal SJA1105 | |
844 | * operation. So we have to back them up in the static configuration tables | |
845 | * and hence apply them on next static config upload... yay! | |
846 | */ | |
847 | static int | |
848 | sja1105_static_fdb_change(struct sja1105_private *priv, int port, | |
849 | const struct sja1105_l2_lookup_entry *requested, | |
850 | bool keep) | |
851 | { | |
852 | struct sja1105_l2_lookup_entry *l2_lookup; | |
853 | struct sja1105_table *table; | |
854 | int rc, match; | |
855 | ||
856 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; | |
857 | ||
858 | match = sja1105_find_static_fdb_entry(priv, port, requested); | |
859 | if (match < 0) { | |
860 | /* Can't delete a missing entry. */ | |
861 | if (!keep) | |
862 | return 0; | |
863 | ||
864 | /* No match => new entry */ | |
865 | rc = sja1105_table_resize(table, table->entry_count + 1); | |
866 | if (rc) | |
867 | return rc; | |
868 | ||
869 | match = table->entry_count - 1; | |
870 | } | |
871 | ||
872 | /* Assign pointer after the resize (it may be new memory) */ | |
873 | l2_lookup = table->entries; | |
874 | ||
875 | /* We have a match. | |
876 | * If the job was to add this FDB entry, it's already done (mostly | |
877 | * anyway, since the port forwarding mask may have changed, case in | |
878 | * which we update it). | |
879 | * Otherwise we have to delete it. | |
880 | */ | |
881 | if (keep) { | |
882 | l2_lookup[match] = *requested; | |
883 | return 0; | |
884 | } | |
885 | ||
886 | /* To remove, the strategy is to overwrite the element with | |
887 | * the last one, and then reduce the array size by 1 | |
888 | */ | |
889 | l2_lookup[match] = l2_lookup[table->entry_count - 1]; | |
890 | return sja1105_table_resize(table, table->entry_count - 1); | |
891 | } | |
892 | ||
291d1e72 VO |
893 | /* First-generation switches have a 4-way set associative TCAM that |
894 | * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of | |
895 | * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). | |
896 | * For the placement of a newly learnt FDB entry, the switch selects the bin | |
897 | * based on a hash function, and the way within that bin incrementally. | |
898 | */ | |
899 | static inline int sja1105et_fdb_index(int bin, int way) | |
900 | { | |
901 | return bin * SJA1105ET_FDB_BIN_SIZE + way; | |
902 | } | |
903 | ||
9dfa6911 VO |
904 | static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, |
905 | const u8 *addr, u16 vid, | |
906 | struct sja1105_l2_lookup_entry *match, | |
907 | int *last_unused) | |
291d1e72 VO |
908 | { |
909 | int way; | |
910 | ||
911 | for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { | |
912 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
913 | int index = sja1105et_fdb_index(bin, way); | |
914 | ||
915 | /* Skip unused entries, optionally marking them | |
916 | * into the return value | |
917 | */ | |
918 | if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
919 | index, &l2_lookup)) { | |
920 | if (last_unused) | |
921 | *last_unused = way; | |
922 | continue; | |
923 | } | |
924 | ||
925 | if (l2_lookup.macaddr == ether_addr_to_u64(addr) && | |
926 | l2_lookup.vlanid == vid) { | |
927 | if (match) | |
928 | *match = l2_lookup; | |
929 | return way; | |
930 | } | |
931 | } | |
932 | /* Return an invalid entry index if not found */ | |
933 | return -1; | |
934 | } | |
935 | ||
9dfa6911 VO |
936 | int sja1105et_fdb_add(struct dsa_switch *ds, int port, |
937 | const unsigned char *addr, u16 vid) | |
291d1e72 VO |
938 | { |
939 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
940 | struct sja1105_private *priv = ds->priv; | |
941 | struct device *dev = ds->dev; | |
942 | int last_unused = -1; | |
60f6053f | 943 | int bin, way, rc; |
291d1e72 | 944 | |
9dfa6911 | 945 | bin = sja1105et_fdb_hash(priv, addr, vid); |
291d1e72 | 946 | |
9dfa6911 VO |
947 | way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, |
948 | &l2_lookup, &last_unused); | |
291d1e72 VO |
949 | if (way >= 0) { |
950 | /* We have an FDB entry. Is our port in the destination | |
951 | * mask? If yes, we need to do nothing. If not, we need | |
952 | * to rewrite the entry by adding this port to it. | |
953 | */ | |
954 | if (l2_lookup.destports & BIT(port)) | |
955 | return 0; | |
956 | l2_lookup.destports |= BIT(port); | |
957 | } else { | |
958 | int index = sja1105et_fdb_index(bin, way); | |
959 | ||
960 | /* We don't have an FDB entry. We construct a new one and | |
961 | * try to find a place for it within the FDB table. | |
962 | */ | |
963 | l2_lookup.macaddr = ether_addr_to_u64(addr); | |
964 | l2_lookup.destports = BIT(port); | |
965 | l2_lookup.vlanid = vid; | |
966 | ||
967 | if (last_unused >= 0) { | |
968 | way = last_unused; | |
969 | } else { | |
970 | /* Bin is full, need to evict somebody. | |
971 | * Choose victim at random. If you get these messages | |
972 | * often, you may need to consider changing the | |
973 | * distribution function: | |
974 | * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly | |
975 | */ | |
976 | get_random_bytes(&way, sizeof(u8)); | |
977 | way %= SJA1105ET_FDB_BIN_SIZE; | |
978 | dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", | |
979 | bin, addr, way); | |
980 | /* Evict entry */ | |
981 | sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, | |
982 | index, NULL, false); | |
983 | } | |
984 | } | |
985 | l2_lookup.index = sja1105et_fdb_index(bin, way); | |
986 | ||
60f6053f VO |
987 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
988 | l2_lookup.index, &l2_lookup, | |
989 | true); | |
990 | if (rc < 0) | |
991 | return rc; | |
992 | ||
993 | return sja1105_static_fdb_change(priv, port, &l2_lookup, true); | |
291d1e72 VO |
994 | } |
995 | ||
9dfa6911 VO |
996 | int sja1105et_fdb_del(struct dsa_switch *ds, int port, |
997 | const unsigned char *addr, u16 vid) | |
291d1e72 VO |
998 | { |
999 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
1000 | struct sja1105_private *priv = ds->priv; | |
60f6053f | 1001 | int index, bin, way, rc; |
291d1e72 VO |
1002 | bool keep; |
1003 | ||
9dfa6911 VO |
1004 | bin = sja1105et_fdb_hash(priv, addr, vid); |
1005 | way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, | |
1006 | &l2_lookup, NULL); | |
291d1e72 VO |
1007 | if (way < 0) |
1008 | return 0; | |
1009 | index = sja1105et_fdb_index(bin, way); | |
1010 | ||
1011 | /* We have an FDB entry. Is our port in the destination mask? If yes, | |
1012 | * we need to remove it. If the resulting port mask becomes empty, we | |
1013 | * need to completely evict the FDB entry. | |
1014 | * Otherwise we just write it back. | |
1015 | */ | |
7752e937 VO |
1016 | l2_lookup.destports &= ~BIT(port); |
1017 | ||
291d1e72 VO |
1018 | if (l2_lookup.destports) |
1019 | keep = true; | |
1020 | else | |
1021 | keep = false; | |
1022 | ||
60f6053f VO |
1023 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
1024 | index, &l2_lookup, keep); | |
1025 | if (rc < 0) | |
1026 | return rc; | |
1027 | ||
1028 | return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); | |
291d1e72 VO |
1029 | } |
1030 | ||
9dfa6911 VO |
1031 | int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, |
1032 | const unsigned char *addr, u16 vid) | |
1033 | { | |
1da73821 VO |
1034 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
1035 | struct sja1105_private *priv = ds->priv; | |
1036 | int rc, i; | |
1037 | ||
1038 | /* Search for an existing entry in the FDB table */ | |
1039 | l2_lookup.macaddr = ether_addr_to_u64(addr); | |
1040 | l2_lookup.vlanid = vid; | |
1041 | l2_lookup.iotag = SJA1105_S_TAG; | |
1042 | l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); | |
1043 | l2_lookup.mask_vlanid = VLAN_VID_MASK; | |
1044 | l2_lookup.mask_iotag = BIT(0); | |
1045 | l2_lookup.destports = BIT(port); | |
1046 | ||
1047 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
1048 | SJA1105_SEARCH, &l2_lookup); | |
1049 | if (rc == 0) { | |
1050 | /* Found and this port is already in the entry's | |
1051 | * port mask => job done | |
1052 | */ | |
1053 | if (l2_lookup.destports & BIT(port)) | |
1054 | return 0; | |
1055 | /* l2_lookup.index is populated by the switch in case it | |
1056 | * found something. | |
1057 | */ | |
1058 | l2_lookup.destports |= BIT(port); | |
1059 | goto skip_finding_an_index; | |
1060 | } | |
1061 | ||
1062 | /* Not found, so try to find an unused spot in the FDB. | |
1063 | * This is slightly inefficient because the strategy is knock-knock at | |
1064 | * every possible position from 0 to 1023. | |
1065 | */ | |
1066 | for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { | |
1067 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
1068 | i, NULL); | |
1069 | if (rc < 0) | |
1070 | break; | |
1071 | } | |
1072 | if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { | |
1073 | dev_err(ds->dev, "FDB is full, cannot add entry.\n"); | |
1074 | return -EINVAL; | |
1075 | } | |
17ae6555 | 1076 | l2_lookup.lockeds = true; |
1da73821 VO |
1077 | l2_lookup.index = i; |
1078 | ||
1079 | skip_finding_an_index: | |
60f6053f VO |
1080 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
1081 | l2_lookup.index, &l2_lookup, | |
1082 | true); | |
1083 | if (rc < 0) | |
1084 | return rc; | |
1085 | ||
1086 | return sja1105_static_fdb_change(priv, port, &l2_lookup, true); | |
9dfa6911 VO |
1087 | } |
1088 | ||
1089 | int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, | |
1090 | const unsigned char *addr, u16 vid) | |
1091 | { | |
1da73821 VO |
1092 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
1093 | struct sja1105_private *priv = ds->priv; | |
1094 | bool keep; | |
1095 | int rc; | |
1096 | ||
1097 | l2_lookup.macaddr = ether_addr_to_u64(addr); | |
1098 | l2_lookup.vlanid = vid; | |
1099 | l2_lookup.iotag = SJA1105_S_TAG; | |
1100 | l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); | |
1101 | l2_lookup.mask_vlanid = VLAN_VID_MASK; | |
1102 | l2_lookup.mask_iotag = BIT(0); | |
1103 | l2_lookup.destports = BIT(port); | |
1104 | ||
1105 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
1106 | SJA1105_SEARCH, &l2_lookup); | |
1107 | if (rc < 0) | |
1108 | return 0; | |
1109 | ||
1110 | l2_lookup.destports &= ~BIT(port); | |
1111 | ||
1112 | /* Decide whether we remove just this port from the FDB entry, | |
1113 | * or if we remove it completely. | |
1114 | */ | |
1115 | if (l2_lookup.destports) | |
1116 | keep = true; | |
1117 | else | |
1118 | keep = false; | |
1119 | ||
60f6053f VO |
1120 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
1121 | l2_lookup.index, &l2_lookup, keep); | |
1122 | if (rc < 0) | |
1123 | return rc; | |
1124 | ||
1125 | return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); | |
9dfa6911 VO |
1126 | } |
1127 | ||
1128 | static int sja1105_fdb_add(struct dsa_switch *ds, int port, | |
1129 | const unsigned char *addr, u16 vid) | |
1130 | { | |
1131 | struct sja1105_private *priv = ds->priv; | |
b3ee526a VO |
1132 | u16 rx_vid, tx_vid; |
1133 | int rc, i; | |
1134 | ||
1135 | if (dsa_port_is_vlan_filtering(&ds->ports[port])) | |
1136 | return priv->info->fdb_add_cmd(ds, port, addr, vid); | |
93647594 VO |
1137 | |
1138 | /* Since we make use of VLANs even when the bridge core doesn't tell us | |
1139 | * to, translate these FDB entries into the correct dsa_8021q ones. | |
b3ee526a VO |
1140 | * The basic idea (also repeats for removal below) is: |
1141 | * - Each of the other front-panel ports needs to be able to forward a | |
1142 | * pvid-tagged (aka tagged with their rx_vid) frame that matches this | |
1143 | * DMAC. | |
1144 | * - The CPU port (aka the tx_vid of this port) needs to be able to | |
1145 | * send a frame matching this DMAC to the specified port. | |
1146 | * For a better picture see net/dsa/tag_8021q.c. | |
93647594 | 1147 | */ |
b3ee526a VO |
1148 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
1149 | if (i == port) | |
1150 | continue; | |
1151 | if (i == dsa_upstream_port(priv->ds, port)) | |
1152 | continue; | |
9dfa6911 | 1153 | |
b3ee526a VO |
1154 | rx_vid = dsa_8021q_rx_vid(ds, i); |
1155 | rc = priv->info->fdb_add_cmd(ds, port, addr, rx_vid); | |
93647594 VO |
1156 | if (rc < 0) |
1157 | return rc; | |
93647594 | 1158 | } |
b3ee526a VO |
1159 | tx_vid = dsa_8021q_tx_vid(ds, port); |
1160 | return priv->info->fdb_add_cmd(ds, port, addr, tx_vid); | |
9dfa6911 VO |
1161 | } |
1162 | ||
1163 | static int sja1105_fdb_del(struct dsa_switch *ds, int port, | |
1164 | const unsigned char *addr, u16 vid) | |
1165 | { | |
1166 | struct sja1105_private *priv = ds->priv; | |
b3ee526a VO |
1167 | u16 rx_vid, tx_vid; |
1168 | int rc, i; | |
9dfa6911 | 1169 | |
b3ee526a VO |
1170 | if (dsa_port_is_vlan_filtering(&ds->ports[port])) |
1171 | return priv->info->fdb_del_cmd(ds, port, addr, vid); | |
1172 | ||
1173 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
1174 | if (i == port) | |
1175 | continue; | |
1176 | if (i == dsa_upstream_port(priv->ds, port)) | |
1177 | continue; | |
93647594 | 1178 | |
b3ee526a VO |
1179 | rx_vid = dsa_8021q_rx_vid(ds, i); |
1180 | rc = priv->info->fdb_del_cmd(ds, port, addr, rx_vid); | |
93647594 VO |
1181 | if (rc < 0) |
1182 | return rc; | |
93647594 | 1183 | } |
b3ee526a VO |
1184 | tx_vid = dsa_8021q_tx_vid(ds, port); |
1185 | return priv->info->fdb_del_cmd(ds, port, addr, tx_vid); | |
9dfa6911 VO |
1186 | } |
1187 | ||
291d1e72 VO |
1188 | static int sja1105_fdb_dump(struct dsa_switch *ds, int port, |
1189 | dsa_fdb_dump_cb_t *cb, void *data) | |
1190 | { | |
1191 | struct sja1105_private *priv = ds->priv; | |
1192 | struct device *dev = ds->dev; | |
b3ee526a | 1193 | u16 rx_vid, tx_vid; |
291d1e72 VO |
1194 | int i; |
1195 | ||
b3ee526a VO |
1196 | rx_vid = dsa_8021q_rx_vid(ds, port); |
1197 | tx_vid = dsa_8021q_tx_vid(ds, port); | |
1198 | ||
291d1e72 VO |
1199 | for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { |
1200 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
1201 | u8 macaddr[ETH_ALEN]; | |
1202 | int rc; | |
1203 | ||
1204 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
1205 | i, &l2_lookup); | |
1206 | /* No fdb entry at i, not an issue */ | |
def84604 | 1207 | if (rc == -ENOENT) |
291d1e72 VO |
1208 | continue; |
1209 | if (rc) { | |
1210 | dev_err(dev, "Failed to dump FDB: %d\n", rc); | |
1211 | return rc; | |
1212 | } | |
1213 | ||
1214 | /* FDB dump callback is per port. This means we have to | |
1215 | * disregard a valid entry if it's not for this port, even if | |
1216 | * only to revisit it later. This is inefficient because the | |
1217 | * 1024-sized FDB table needs to be traversed 4 times through | |
1218 | * SPI during a 'bridge fdb show' command. | |
1219 | */ | |
1220 | if (!(l2_lookup.destports & BIT(port))) | |
1221 | continue; | |
1222 | u64_to_ether_addr(l2_lookup.macaddr, macaddr); | |
93647594 | 1223 | |
d7637782 VO |
1224 | /* On SJA1105 E/T, the switch doesn't implement the LOCKEDS |
1225 | * bit, so it doesn't tell us whether a FDB entry is static | |
1226 | * or not. | |
1227 | * But, of course, we can find out - we're the ones who added | |
1228 | * it in the first place. | |
1229 | */ | |
1230 | if (priv->info->device_id == SJA1105E_DEVICE_ID || | |
1231 | priv->info->device_id == SJA1105T_DEVICE_ID) { | |
1232 | int match; | |
1233 | ||
1234 | match = sja1105_find_static_fdb_entry(priv, port, | |
1235 | &l2_lookup); | |
1236 | l2_lookup.lockeds = (match >= 0); | |
1237 | } | |
1238 | ||
b3ee526a VO |
1239 | /* We need to hide the dsa_8021q VLANs from the user. This |
1240 | * basically means hiding the duplicates and only showing | |
1241 | * the pvid that is supposed to be active in standalone and | |
1242 | * non-vlan_filtering modes (aka 1). | |
1243 | * - For statically added FDB entries (bridge fdb add), we | |
1244 | * can convert the TX VID (coming from the CPU port) into the | |
1245 | * pvid and ignore the RX VIDs of the other ports. | |
1246 | * - For dynamically learned FDB entries, a single entry with | |
1247 | * no duplicates is learned - that which has the real port's | |
1248 | * pvid, aka RX VID. | |
93647594 | 1249 | */ |
b3ee526a VO |
1250 | if (!dsa_port_is_vlan_filtering(&ds->ports[port])) { |
1251 | if (l2_lookup.vlanid == tx_vid || | |
1252 | l2_lookup.vlanid == rx_vid) | |
1253 | l2_lookup.vlanid = 1; | |
1254 | else | |
1255 | continue; | |
1256 | } | |
17ae6555 | 1257 | cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); |
291d1e72 VO |
1258 | } |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | /* This callback needs to be present */ | |
1263 | static int sja1105_mdb_prepare(struct dsa_switch *ds, int port, | |
1264 | const struct switchdev_obj_port_mdb *mdb) | |
1265 | { | |
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | static void sja1105_mdb_add(struct dsa_switch *ds, int port, | |
1270 | const struct switchdev_obj_port_mdb *mdb) | |
1271 | { | |
1272 | sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); | |
1273 | } | |
1274 | ||
1275 | static int sja1105_mdb_del(struct dsa_switch *ds, int port, | |
1276 | const struct switchdev_obj_port_mdb *mdb) | |
1277 | { | |
1278 | return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); | |
1279 | } | |
1280 | ||
8aa9ebcc VO |
1281 | static int sja1105_bridge_member(struct dsa_switch *ds, int port, |
1282 | struct net_device *br, bool member) | |
1283 | { | |
1284 | struct sja1105_l2_forwarding_entry *l2_fwd; | |
1285 | struct sja1105_private *priv = ds->priv; | |
1286 | int i, rc; | |
1287 | ||
1288 | l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; | |
1289 | ||
1290 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
1291 | /* Add this port to the forwarding matrix of the | |
1292 | * other ports in the same bridge, and viceversa. | |
1293 | */ | |
1294 | if (!dsa_is_user_port(ds, i)) | |
1295 | continue; | |
1296 | /* For the ports already under the bridge, only one thing needs | |
1297 | * to be done, and that is to add this port to their | |
1298 | * reachability domain. So we can perform the SPI write for | |
1299 | * them immediately. However, for this port itself (the one | |
1300 | * that is new to the bridge), we need to add all other ports | |
1301 | * to its reachability domain. So we do that incrementally in | |
1302 | * this loop, and perform the SPI write only at the end, once | |
1303 | * the domain contains all other bridge ports. | |
1304 | */ | |
1305 | if (i == port) | |
1306 | continue; | |
1307 | if (dsa_to_port(ds, i)->bridge_dev != br) | |
1308 | continue; | |
1309 | sja1105_port_allow_traffic(l2_fwd, i, port, member); | |
1310 | sja1105_port_allow_traffic(l2_fwd, port, i, member); | |
1311 | ||
1312 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, | |
1313 | i, &l2_fwd[i], true); | |
1314 | if (rc < 0) | |
1315 | return rc; | |
1316 | } | |
1317 | ||
1318 | return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, | |
1319 | port, &l2_fwd[port], true); | |
1320 | } | |
1321 | ||
640f763f VO |
1322 | static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, |
1323 | u8 state) | |
1324 | { | |
1325 | struct sja1105_private *priv = ds->priv; | |
1326 | struct sja1105_mac_config_entry *mac; | |
1327 | ||
1328 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; | |
1329 | ||
1330 | switch (state) { | |
1331 | case BR_STATE_DISABLED: | |
1332 | case BR_STATE_BLOCKING: | |
1333 | /* From UM10944 description of DRPDTAG (why put this there?): | |
1334 | * "Management traffic flows to the port regardless of the state | |
1335 | * of the INGRESS flag". So BPDUs are still be allowed to pass. | |
1336 | * At the moment no difference between DISABLED and BLOCKING. | |
1337 | */ | |
1338 | mac[port].ingress = false; | |
1339 | mac[port].egress = false; | |
1340 | mac[port].dyn_learn = false; | |
1341 | break; | |
1342 | case BR_STATE_LISTENING: | |
1343 | mac[port].ingress = true; | |
1344 | mac[port].egress = false; | |
1345 | mac[port].dyn_learn = false; | |
1346 | break; | |
1347 | case BR_STATE_LEARNING: | |
1348 | mac[port].ingress = true; | |
1349 | mac[port].egress = false; | |
1350 | mac[port].dyn_learn = true; | |
1351 | break; | |
1352 | case BR_STATE_FORWARDING: | |
1353 | mac[port].ingress = true; | |
1354 | mac[port].egress = true; | |
1355 | mac[port].dyn_learn = true; | |
1356 | break; | |
1357 | default: | |
1358 | dev_err(ds->dev, "invalid STP state: %d\n", state); | |
1359 | return; | |
1360 | } | |
1361 | ||
1362 | sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, | |
1363 | &mac[port], true); | |
1364 | } | |
1365 | ||
8aa9ebcc VO |
1366 | static int sja1105_bridge_join(struct dsa_switch *ds, int port, |
1367 | struct net_device *br) | |
1368 | { | |
1369 | return sja1105_bridge_member(ds, port, br, true); | |
1370 | } | |
1371 | ||
1372 | static void sja1105_bridge_leave(struct dsa_switch *ds, int port, | |
1373 | struct net_device *br) | |
1374 | { | |
1375 | sja1105_bridge_member(ds, port, br, false); | |
1376 | } | |
1377 | ||
6666cebc VO |
1378 | /* For situations where we need to change a setting at runtime that is only |
1379 | * available through the static configuration, resetting the switch in order | |
1380 | * to upload the new static config is unavoidable. Back up the settings we | |
1381 | * modify at runtime (currently only MAC) and restore them after uploading, | |
1382 | * such that this operation is relatively seamless. | |
1383 | */ | |
1384 | static int sja1105_static_config_reload(struct sja1105_private *priv) | |
1385 | { | |
1386 | struct sja1105_mac_config_entry *mac; | |
1387 | int speed_mbps[SJA1105_NUM_PORTS]; | |
1388 | int rc, i; | |
1389 | ||
1390 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; | |
1391 | ||
8400cff6 VO |
1392 | /* Back up the dynamic link speed changed by sja1105_adjust_port_config |
1393 | * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the | |
1394 | * switch wants to see in the static config in order to allow us to | |
1395 | * change it through the dynamic interface later. | |
6666cebc VO |
1396 | */ |
1397 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
1398 | speed_mbps[i] = sja1105_speed[mac[i].speed]; | |
1399 | mac[i].speed = SJA1105_SPEED_AUTO; | |
1400 | } | |
1401 | ||
1402 | /* Reset switch and send updated static configuration */ | |
1403 | rc = sja1105_static_config_upload(priv); | |
1404 | if (rc < 0) | |
1405 | goto out; | |
1406 | ||
1407 | /* Configure the CGU (PLLs) for MII and RMII PHYs. | |
1408 | * For these interfaces there is no dynamic configuration | |
1409 | * needed, since PLLs have same settings at all speeds. | |
1410 | */ | |
1411 | rc = sja1105_clocking_setup(priv); | |
1412 | if (rc < 0) | |
1413 | goto out; | |
1414 | ||
1415 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
8400cff6 | 1416 | rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); |
6666cebc VO |
1417 | if (rc < 0) |
1418 | goto out; | |
1419 | } | |
1420 | out: | |
1421 | return rc; | |
1422 | } | |
1423 | ||
6666cebc VO |
1424 | static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) |
1425 | { | |
1426 | struct sja1105_mac_config_entry *mac; | |
1427 | ||
1428 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; | |
1429 | ||
1430 | mac[port].vlanid = pvid; | |
1431 | ||
1432 | return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, | |
1433 | &mac[port], true); | |
1434 | } | |
1435 | ||
1436 | static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) | |
1437 | { | |
1438 | struct sja1105_vlan_lookup_entry *vlan; | |
1439 | int count, i; | |
1440 | ||
1441 | vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; | |
1442 | count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; | |
1443 | ||
1444 | for (i = 0; i < count; i++) | |
1445 | if (vlan[i].vlanid == vid) | |
1446 | return i; | |
1447 | ||
1448 | /* Return an invalid entry index if not found */ | |
1449 | return -1; | |
1450 | } | |
1451 | ||
1452 | static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid, | |
1453 | bool enabled, bool untagged) | |
1454 | { | |
1455 | struct sja1105_vlan_lookup_entry *vlan; | |
1456 | struct sja1105_table *table; | |
1457 | bool keep = true; | |
1458 | int match, rc; | |
1459 | ||
1460 | table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; | |
1461 | ||
1462 | match = sja1105_is_vlan_configured(priv, vid); | |
1463 | if (match < 0) { | |
1464 | /* Can't delete a missing entry. */ | |
1465 | if (!enabled) | |
1466 | return 0; | |
1467 | rc = sja1105_table_resize(table, table->entry_count + 1); | |
1468 | if (rc) | |
1469 | return rc; | |
1470 | match = table->entry_count - 1; | |
1471 | } | |
1472 | /* Assign pointer after the resize (it's new memory) */ | |
1473 | vlan = table->entries; | |
1474 | vlan[match].vlanid = vid; | |
1475 | if (enabled) { | |
1476 | vlan[match].vlan_bc |= BIT(port); | |
1477 | vlan[match].vmemb_port |= BIT(port); | |
1478 | } else { | |
1479 | vlan[match].vlan_bc &= ~BIT(port); | |
1480 | vlan[match].vmemb_port &= ~BIT(port); | |
1481 | } | |
1482 | /* Also unset tag_port if removing this VLAN was requested, | |
1483 | * just so we don't have a confusing bitmap (no practical purpose). | |
1484 | */ | |
1485 | if (untagged || !enabled) | |
1486 | vlan[match].tag_port &= ~BIT(port); | |
1487 | else | |
1488 | vlan[match].tag_port |= BIT(port); | |
1489 | /* If there's no port left as member of this VLAN, | |
1490 | * it's time for it to go. | |
1491 | */ | |
1492 | if (!vlan[match].vmemb_port) | |
1493 | keep = false; | |
1494 | ||
1495 | dev_dbg(priv->ds->dev, | |
1496 | "%s: port %d, vid %llu, broadcast domain 0x%llx, " | |
1497 | "port members 0x%llx, tagged ports 0x%llx, keep %d\n", | |
1498 | __func__, port, vlan[match].vlanid, vlan[match].vlan_bc, | |
1499 | vlan[match].vmemb_port, vlan[match].tag_port, keep); | |
1500 | ||
1501 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, | |
1502 | &vlan[match], keep); | |
1503 | if (rc < 0) | |
1504 | return rc; | |
1505 | ||
1506 | if (!keep) | |
1507 | return sja1105_table_delete_entry(table, match); | |
1508 | ||
1509 | return 0; | |
1510 | } | |
1511 | ||
227d07a0 VO |
1512 | static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) |
1513 | { | |
1514 | int rc, i; | |
1515 | ||
1516 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
1517 | rc = dsa_port_setup_8021q_tagging(ds, i, enabled); | |
1518 | if (rc < 0) { | |
1519 | dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n", | |
1520 | i, rc); | |
1521 | return rc; | |
1522 | } | |
1523 | } | |
1524 | dev_info(ds->dev, "%s switch tagging\n", | |
1525 | enabled ? "Enabled" : "Disabled"); | |
1526 | return 0; | |
1527 | } | |
1528 | ||
8aa9ebcc VO |
1529 | static enum dsa_tag_protocol |
1530 | sja1105_get_tag_protocol(struct dsa_switch *ds, int port) | |
1531 | { | |
227d07a0 | 1532 | return DSA_TAG_PROTO_SJA1105; |
8aa9ebcc VO |
1533 | } |
1534 | ||
6666cebc VO |
1535 | /* This callback needs to be present */ |
1536 | static int sja1105_vlan_prepare(struct dsa_switch *ds, int port, | |
1537 | const struct switchdev_obj_port_vlan *vlan) | |
1538 | { | |
1539 | return 0; | |
1540 | } | |
1541 | ||
070ca3bb VO |
1542 | /* The TPID setting belongs to the General Parameters table, |
1543 | * which can only be partially reconfigured at runtime (and not the TPID). | |
1544 | * So a switch reset is required. | |
1545 | */ | |
6666cebc VO |
1546 | static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) |
1547 | { | |
070ca3bb | 1548 | struct sja1105_general_params_entry *general_params; |
6666cebc | 1549 | struct sja1105_private *priv = ds->priv; |
070ca3bb VO |
1550 | struct sja1105_table *table; |
1551 | u16 tpid, tpid2; | |
6666cebc VO |
1552 | int rc; |
1553 | ||
070ca3bb | 1554 | if (enabled) { |
6666cebc | 1555 | /* Enable VLAN filtering. */ |
f9a1a764 VO |
1556 | tpid = ETH_P_8021AD; |
1557 | tpid2 = ETH_P_8021Q; | |
070ca3bb | 1558 | } else { |
6666cebc | 1559 | /* Disable VLAN filtering. */ |
070ca3bb VO |
1560 | tpid = ETH_P_SJA1105; |
1561 | tpid2 = ETH_P_SJA1105; | |
1562 | } | |
1563 | ||
1564 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; | |
1565 | general_params = table->entries; | |
f9a1a764 | 1566 | /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ |
070ca3bb | 1567 | general_params->tpid = tpid; |
f9a1a764 | 1568 | /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ |
070ca3bb | 1569 | general_params->tpid2 = tpid2; |
42824463 VO |
1570 | /* When VLAN filtering is on, we need to at least be able to |
1571 | * decode management traffic through the "backup plan". | |
1572 | */ | |
1573 | general_params->incl_srcpt1 = enabled; | |
1574 | general_params->incl_srcpt0 = enabled; | |
070ca3bb VO |
1575 | |
1576 | rc = sja1105_static_config_reload(priv); | |
6666cebc VO |
1577 | if (rc) |
1578 | dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); | |
1579 | ||
227d07a0 VO |
1580 | /* Switch port identification based on 802.1Q is only passable |
1581 | * if we are not under a vlan_filtering bridge. So make sure | |
1582 | * the two configurations are mutually exclusive. | |
1583 | */ | |
1584 | return sja1105_setup_8021q_tagging(ds, !enabled); | |
6666cebc VO |
1585 | } |
1586 | ||
1587 | static void sja1105_vlan_add(struct dsa_switch *ds, int port, | |
1588 | const struct switchdev_obj_port_vlan *vlan) | |
1589 | { | |
1590 | struct sja1105_private *priv = ds->priv; | |
1591 | u16 vid; | |
1592 | int rc; | |
1593 | ||
1594 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { | |
1595 | rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags & | |
1596 | BRIDGE_VLAN_INFO_UNTAGGED); | |
1597 | if (rc < 0) { | |
1598 | dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", | |
1599 | vid, port, rc); | |
1600 | return; | |
1601 | } | |
1602 | if (vlan->flags & BRIDGE_VLAN_INFO_PVID) { | |
1603 | rc = sja1105_pvid_apply(ds->priv, port, vid); | |
1604 | if (rc < 0) { | |
1605 | dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n", | |
1606 | vid, port, rc); | |
1607 | return; | |
1608 | } | |
1609 | } | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | static int sja1105_vlan_del(struct dsa_switch *ds, int port, | |
1614 | const struct switchdev_obj_port_vlan *vlan) | |
1615 | { | |
1616 | struct sja1105_private *priv = ds->priv; | |
1617 | u16 vid; | |
1618 | int rc; | |
1619 | ||
1620 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { | |
1621 | rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags & | |
1622 | BRIDGE_VLAN_INFO_UNTAGGED); | |
1623 | if (rc < 0) { | |
1624 | dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", | |
1625 | vid, port, rc); | |
1626 | return rc; | |
1627 | } | |
1628 | } | |
1629 | return 0; | |
1630 | } | |
1631 | ||
8aa9ebcc VO |
1632 | /* The programming model for the SJA1105 switch is "all-at-once" via static |
1633 | * configuration tables. Some of these can be dynamically modified at runtime, | |
1634 | * but not the xMII mode parameters table. | |
1635 | * Furthermode, some PHYs may not have crystals for generating their clocks | |
1636 | * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's | |
1637 | * ref_clk pin. So port clocking needs to be initialized early, before | |
1638 | * connecting to PHYs is attempted, otherwise they won't respond through MDIO. | |
1639 | * Setting correct PHY link speed does not matter now. | |
1640 | * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY | |
1641 | * bindings are not yet parsed by DSA core. We need to parse early so that we | |
1642 | * can populate the xMII mode parameters table. | |
1643 | */ | |
1644 | static int sja1105_setup(struct dsa_switch *ds) | |
1645 | { | |
1646 | struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; | |
1647 | struct sja1105_private *priv = ds->priv; | |
1648 | int rc; | |
1649 | ||
1650 | rc = sja1105_parse_dt(priv, ports); | |
1651 | if (rc < 0) { | |
1652 | dev_err(ds->dev, "Failed to parse DT: %d\n", rc); | |
1653 | return rc; | |
1654 | } | |
f5b8631c VO |
1655 | |
1656 | /* Error out early if internal delays are required through DT | |
1657 | * and we can't apply them. | |
1658 | */ | |
1659 | rc = sja1105_parse_rgmii_delays(priv, ports); | |
1660 | if (rc < 0) { | |
1661 | dev_err(ds->dev, "RGMII delay not supported\n"); | |
1662 | return rc; | |
1663 | } | |
1664 | ||
bb77f36a VO |
1665 | rc = sja1105_ptp_clock_register(priv); |
1666 | if (rc < 0) { | |
1667 | dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); | |
1668 | return rc; | |
1669 | } | |
8aa9ebcc VO |
1670 | /* Create and send configuration down to device */ |
1671 | rc = sja1105_static_config_load(priv, ports); | |
1672 | if (rc < 0) { | |
1673 | dev_err(ds->dev, "Failed to load static config: %d\n", rc); | |
1674 | return rc; | |
1675 | } | |
1676 | /* Configure the CGU (PHY link modes and speeds) */ | |
1677 | rc = sja1105_clocking_setup(priv); | |
1678 | if (rc < 0) { | |
1679 | dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); | |
1680 | return rc; | |
1681 | } | |
6666cebc VO |
1682 | /* On SJA1105, VLAN filtering per se is always enabled in hardware. |
1683 | * The only thing we can do to disable it is lie about what the 802.1Q | |
1684 | * EtherType is. | |
1685 | * So it will still try to apply VLAN filtering, but all ingress | |
1686 | * traffic (except frames received with EtherType of ETH_P_SJA1105) | |
1687 | * will be internally tagged with a distorted VLAN header where the | |
1688 | * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. | |
1689 | */ | |
1690 | ds->vlan_filtering_is_global = true; | |
8aa9ebcc | 1691 | |
227d07a0 VO |
1692 | /* The DSA/switchdev model brings up switch ports in standalone mode by |
1693 | * default, and that means vlan_filtering is 0 since they're not under | |
1694 | * a bridge, so it's safe to set up switch tagging at this time. | |
1695 | */ | |
1696 | return sja1105_setup_8021q_tagging(ds, true); | |
1697 | } | |
1698 | ||
f3097be2 VO |
1699 | static void sja1105_teardown(struct dsa_switch *ds) |
1700 | { | |
1701 | struct sja1105_private *priv = ds->priv; | |
1702 | ||
1703 | cancel_work_sync(&priv->tagger_data.rxtstamp_work); | |
1704 | skb_queue_purge(&priv->tagger_data.skb_rxtstamp_queue); | |
1705 | } | |
1706 | ||
227d07a0 | 1707 | static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, |
47ed985e | 1708 | struct sk_buff *skb, bool takets) |
227d07a0 VO |
1709 | { |
1710 | struct sja1105_mgmt_entry mgmt_route = {0}; | |
1711 | struct sja1105_private *priv = ds->priv; | |
1712 | struct ethhdr *hdr; | |
1713 | int timeout = 10; | |
1714 | int rc; | |
1715 | ||
1716 | hdr = eth_hdr(skb); | |
1717 | ||
1718 | mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); | |
1719 | mgmt_route.destports = BIT(port); | |
1720 | mgmt_route.enfport = 1; | |
47ed985e VO |
1721 | mgmt_route.tsreg = 0; |
1722 | mgmt_route.takets = takets; | |
227d07a0 VO |
1723 | |
1724 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, | |
1725 | slot, &mgmt_route, true); | |
1726 | if (rc < 0) { | |
1727 | kfree_skb(skb); | |
1728 | return rc; | |
1729 | } | |
1730 | ||
1731 | /* Transfer skb to the host port. */ | |
1732 | dsa_enqueue_skb(skb, ds->ports[port].slave); | |
1733 | ||
1734 | /* Wait until the switch has processed the frame */ | |
1735 | do { | |
1736 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, | |
1737 | slot, &mgmt_route); | |
1738 | if (rc < 0) { | |
1739 | dev_err_ratelimited(priv->ds->dev, | |
1740 | "failed to poll for mgmt route\n"); | |
1741 | continue; | |
1742 | } | |
1743 | ||
1744 | /* UM10944: The ENFPORT flag of the respective entry is | |
1745 | * cleared when a match is found. The host can use this | |
1746 | * flag as an acknowledgment. | |
1747 | */ | |
1748 | cpu_relax(); | |
1749 | } while (mgmt_route.enfport && --timeout); | |
1750 | ||
1751 | if (!timeout) { | |
1752 | /* Clean up the management route so that a follow-up | |
1753 | * frame may not match on it by mistake. | |
2a7e7409 VO |
1754 | * This is only hardware supported on P/Q/R/S - on E/T it is |
1755 | * a no-op and we are silently discarding the -EOPNOTSUPP. | |
227d07a0 VO |
1756 | */ |
1757 | sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, | |
1758 | slot, &mgmt_route, false); | |
1759 | dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); | |
1760 | } | |
1761 | ||
1762 | return NETDEV_TX_OK; | |
1763 | } | |
1764 | ||
1765 | /* Deferred work is unfortunately necessary because setting up the management | |
1766 | * route cannot be done from atomit context (SPI transfer takes a sleepable | |
1767 | * lock on the bus) | |
1768 | */ | |
1769 | static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port, | |
1770 | struct sk_buff *skb) | |
1771 | { | |
1772 | struct sja1105_private *priv = ds->priv; | |
1773 | struct sja1105_port *sp = &priv->ports[port]; | |
47ed985e | 1774 | struct skb_shared_hwtstamps shwt = {0}; |
227d07a0 | 1775 | int slot = sp->mgmt_slot; |
47ed985e VO |
1776 | struct sk_buff *clone; |
1777 | u64 now, ts; | |
1778 | int rc; | |
227d07a0 VO |
1779 | |
1780 | /* The tragic fact about the switch having 4x2 slots for installing | |
1781 | * management routes is that all of them except one are actually | |
1782 | * useless. | |
1783 | * If 2 slots are simultaneously configured for two BPDUs sent to the | |
1784 | * same (multicast) DMAC but on different egress ports, the switch | |
1785 | * would confuse them and redirect first frame it receives on the CPU | |
1786 | * port towards the port configured on the numerically first slot | |
1787 | * (therefore wrong port), then second received frame on second slot | |
1788 | * (also wrong port). | |
1789 | * So for all practical purposes, there needs to be a lock that | |
1790 | * prevents that from happening. The slot used here is utterly useless | |
1791 | * (could have simply been 0 just as fine), but we are doing it | |
1792 | * nonetheless, in case a smarter idea ever comes up in the future. | |
1793 | */ | |
1794 | mutex_lock(&priv->mgmt_lock); | |
1795 | ||
47ed985e VO |
1796 | /* The clone, if there, was made by dsa_skb_tx_timestamp */ |
1797 | clone = DSA_SKB_CB(skb)->clone; | |
1798 | ||
1799 | sja1105_mgmt_xmit(ds, port, slot, skb, !!clone); | |
1800 | ||
1801 | if (!clone) | |
1802 | goto out; | |
1803 | ||
1804 | skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; | |
1805 | ||
1806 | mutex_lock(&priv->ptp_lock); | |
1807 | ||
1808 | now = priv->tstamp_cc.read(&priv->tstamp_cc); | |
1809 | ||
1810 | rc = sja1105_ptpegr_ts_poll(priv, slot, &ts); | |
1811 | if (rc < 0) { | |
1812 | dev_err(ds->dev, "xmit: timed out polling for tstamp\n"); | |
1813 | kfree_skb(clone); | |
1814 | goto out_unlock_ptp; | |
1815 | } | |
1816 | ||
1817 | ts = sja1105_tstamp_reconstruct(priv, now, ts); | |
1818 | ts = timecounter_cyc2time(&priv->tstamp_tc, ts); | |
227d07a0 | 1819 | |
47ed985e VO |
1820 | shwt.hwtstamp = ns_to_ktime(ts); |
1821 | skb_complete_tx_timestamp(clone, &shwt); | |
1822 | ||
1823 | out_unlock_ptp: | |
1824 | mutex_unlock(&priv->ptp_lock); | |
1825 | out: | |
227d07a0 VO |
1826 | mutex_unlock(&priv->mgmt_lock); |
1827 | return NETDEV_TX_OK; | |
8aa9ebcc VO |
1828 | } |
1829 | ||
8456721d VO |
1830 | /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, |
1831 | * which cannot be reconfigured at runtime. So a switch reset is required. | |
1832 | */ | |
1833 | static int sja1105_set_ageing_time(struct dsa_switch *ds, | |
1834 | unsigned int ageing_time) | |
1835 | { | |
1836 | struct sja1105_l2_lookup_params_entry *l2_lookup_params; | |
1837 | struct sja1105_private *priv = ds->priv; | |
1838 | struct sja1105_table *table; | |
1839 | unsigned int maxage; | |
1840 | ||
1841 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; | |
1842 | l2_lookup_params = table->entries; | |
1843 | ||
1844 | maxage = SJA1105_AGEING_TIME_MS(ageing_time); | |
1845 | ||
1846 | if (l2_lookup_params->maxage == maxage) | |
1847 | return 0; | |
1848 | ||
1849 | l2_lookup_params->maxage = maxage; | |
1850 | ||
1851 | return sja1105_static_config_reload(priv); | |
1852 | } | |
1853 | ||
a602afd2 VO |
1854 | /* Caller must hold priv->tagger_data.meta_lock */ |
1855 | static int sja1105_change_rxtstamping(struct sja1105_private *priv, | |
1856 | bool on) | |
1857 | { | |
1858 | struct sja1105_general_params_entry *general_params; | |
1859 | struct sja1105_table *table; | |
1860 | int rc; | |
1861 | ||
1862 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; | |
1863 | general_params = table->entries; | |
1864 | general_params->send_meta1 = on; | |
1865 | general_params->send_meta0 = on; | |
1866 | ||
1867 | rc = sja1105_init_avb_params(priv, on); | |
1868 | if (rc < 0) | |
1869 | return rc; | |
1870 | ||
1871 | /* Initialize the meta state machine to a known state */ | |
1872 | if (priv->tagger_data.stampable_skb) { | |
1873 | kfree_skb(priv->tagger_data.stampable_skb); | |
1874 | priv->tagger_data.stampable_skb = NULL; | |
1875 | } | |
1876 | ||
1877 | return sja1105_static_config_reload(priv); | |
1878 | } | |
1879 | ||
1880 | static int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, | |
1881 | struct ifreq *ifr) | |
1882 | { | |
1883 | struct sja1105_private *priv = ds->priv; | |
1884 | struct hwtstamp_config config; | |
1885 | bool rx_on; | |
1886 | int rc; | |
1887 | ||
1888 | if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) | |
1889 | return -EFAULT; | |
1890 | ||
1891 | switch (config.tx_type) { | |
1892 | case HWTSTAMP_TX_OFF: | |
1893 | priv->ports[port].hwts_tx_en = false; | |
1894 | break; | |
1895 | case HWTSTAMP_TX_ON: | |
1896 | priv->ports[port].hwts_tx_en = true; | |
1897 | break; | |
1898 | default: | |
1899 | return -ERANGE; | |
1900 | } | |
1901 | ||
1902 | switch (config.rx_filter) { | |
1903 | case HWTSTAMP_FILTER_NONE: | |
1904 | rx_on = false; | |
1905 | break; | |
1906 | default: | |
1907 | rx_on = true; | |
1908 | break; | |
1909 | } | |
1910 | ||
1911 | if (rx_on != priv->tagger_data.hwts_rx_en) { | |
1912 | spin_lock(&priv->tagger_data.meta_lock); | |
1913 | rc = sja1105_change_rxtstamping(priv, rx_on); | |
1914 | spin_unlock(&priv->tagger_data.meta_lock); | |
1915 | if (rc < 0) { | |
1916 | dev_err(ds->dev, | |
1917 | "Failed to change RX timestamping: %d\n", rc); | |
1918 | return -EFAULT; | |
1919 | } | |
1920 | priv->tagger_data.hwts_rx_en = rx_on; | |
1921 | } | |
1922 | ||
1923 | if (copy_to_user(ifr->ifr_data, &config, sizeof(config))) | |
1924 | return -EFAULT; | |
1925 | return 0; | |
1926 | } | |
1927 | ||
1928 | static int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, | |
1929 | struct ifreq *ifr) | |
1930 | { | |
1931 | struct sja1105_private *priv = ds->priv; | |
1932 | struct hwtstamp_config config; | |
1933 | ||
1934 | config.flags = 0; | |
1935 | if (priv->ports[port].hwts_tx_en) | |
1936 | config.tx_type = HWTSTAMP_TX_ON; | |
1937 | else | |
1938 | config.tx_type = HWTSTAMP_TX_OFF; | |
1939 | if (priv->tagger_data.hwts_rx_en) | |
1940 | config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; | |
1941 | else | |
1942 | config.rx_filter = HWTSTAMP_FILTER_NONE; | |
1943 | ||
1944 | return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? | |
1945 | -EFAULT : 0; | |
1946 | } | |
1947 | ||
f3097be2 VO |
1948 | #define to_tagger(d) \ |
1949 | container_of((d), struct sja1105_tagger_data, rxtstamp_work) | |
1950 | #define to_sja1105(d) \ | |
1951 | container_of((d), struct sja1105_private, tagger_data) | |
1952 | ||
1953 | static void sja1105_rxtstamp_work(struct work_struct *work) | |
1954 | { | |
1955 | struct sja1105_tagger_data *data = to_tagger(work); | |
1956 | struct sja1105_private *priv = to_sja1105(data); | |
1957 | struct sk_buff *skb; | |
1958 | u64 now; | |
1959 | ||
1960 | mutex_lock(&priv->ptp_lock); | |
1961 | ||
1962 | now = priv->tstamp_cc.read(&priv->tstamp_cc); | |
1963 | ||
1964 | while ((skb = skb_dequeue(&data->skb_rxtstamp_queue)) != NULL) { | |
1965 | struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb); | |
1966 | u64 ts; | |
1967 | ||
1968 | *shwt = (struct skb_shared_hwtstamps) {0}; | |
1969 | ||
1970 | ts = SJA1105_SKB_CB(skb)->meta_tstamp; | |
1971 | ts = sja1105_tstamp_reconstruct(priv, now, ts); | |
1972 | ts = timecounter_cyc2time(&priv->tstamp_tc, ts); | |
1973 | ||
1974 | shwt->hwtstamp = ns_to_ktime(ts); | |
1975 | netif_rx_ni(skb); | |
1976 | } | |
1977 | ||
1978 | mutex_unlock(&priv->ptp_lock); | |
1979 | } | |
1980 | ||
1981 | /* Called from dsa_skb_defer_rx_timestamp */ | |
1dbb9869 Y |
1982 | static bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port, |
1983 | struct sk_buff *skb, unsigned int type) | |
f3097be2 VO |
1984 | { |
1985 | struct sja1105_private *priv = ds->priv; | |
1986 | struct sja1105_tagger_data *data = &priv->tagger_data; | |
1987 | ||
1988 | if (!data->hwts_rx_en) | |
1989 | return false; | |
1990 | ||
1991 | /* We need to read the full PTP clock to reconstruct the Rx | |
1992 | * timestamp. For that we need a sleepable context. | |
1993 | */ | |
1994 | skb_queue_tail(&data->skb_rxtstamp_queue, skb); | |
1995 | schedule_work(&data->rxtstamp_work); | |
1996 | return true; | |
1997 | } | |
1998 | ||
47ed985e VO |
1999 | /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone |
2000 | * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit | |
2001 | * callback, where we will timestamp it synchronously. | |
2002 | */ | |
1dbb9869 Y |
2003 | static bool sja1105_port_txtstamp(struct dsa_switch *ds, int port, |
2004 | struct sk_buff *skb, unsigned int type) | |
47ed985e VO |
2005 | { |
2006 | struct sja1105_private *priv = ds->priv; | |
2007 | struct sja1105_port *sp = &priv->ports[port]; | |
2008 | ||
2009 | if (!sp->hwts_tx_en) | |
2010 | return false; | |
2011 | ||
2012 | return true; | |
2013 | } | |
2014 | ||
8aa9ebcc VO |
2015 | static const struct dsa_switch_ops sja1105_switch_ops = { |
2016 | .get_tag_protocol = sja1105_get_tag_protocol, | |
2017 | .setup = sja1105_setup, | |
f3097be2 | 2018 | .teardown = sja1105_teardown, |
8456721d | 2019 | .set_ageing_time = sja1105_set_ageing_time, |
ad9f299a | 2020 | .phylink_validate = sja1105_phylink_validate, |
af7cd036 | 2021 | .phylink_mac_config = sja1105_mac_config, |
8400cff6 VO |
2022 | .phylink_mac_link_up = sja1105_mac_link_up, |
2023 | .phylink_mac_link_down = sja1105_mac_link_down, | |
52c34e6e VO |
2024 | .get_strings = sja1105_get_strings, |
2025 | .get_ethtool_stats = sja1105_get_ethtool_stats, | |
2026 | .get_sset_count = sja1105_get_sset_count, | |
bb77f36a | 2027 | .get_ts_info = sja1105_get_ts_info, |
291d1e72 VO |
2028 | .port_fdb_dump = sja1105_fdb_dump, |
2029 | .port_fdb_add = sja1105_fdb_add, | |
2030 | .port_fdb_del = sja1105_fdb_del, | |
8aa9ebcc VO |
2031 | .port_bridge_join = sja1105_bridge_join, |
2032 | .port_bridge_leave = sja1105_bridge_leave, | |
640f763f | 2033 | .port_stp_state_set = sja1105_bridge_stp_state_set, |
6666cebc VO |
2034 | .port_vlan_prepare = sja1105_vlan_prepare, |
2035 | .port_vlan_filtering = sja1105_vlan_filtering, | |
2036 | .port_vlan_add = sja1105_vlan_add, | |
2037 | .port_vlan_del = sja1105_vlan_del, | |
291d1e72 VO |
2038 | .port_mdb_prepare = sja1105_mdb_prepare, |
2039 | .port_mdb_add = sja1105_mdb_add, | |
2040 | .port_mdb_del = sja1105_mdb_del, | |
227d07a0 | 2041 | .port_deferred_xmit = sja1105_port_deferred_xmit, |
a602afd2 VO |
2042 | .port_hwtstamp_get = sja1105_hwtstamp_get, |
2043 | .port_hwtstamp_set = sja1105_hwtstamp_set, | |
f3097be2 | 2044 | .port_rxtstamp = sja1105_port_rxtstamp, |
47ed985e | 2045 | .port_txtstamp = sja1105_port_txtstamp, |
8aa9ebcc VO |
2046 | }; |
2047 | ||
2048 | static int sja1105_check_device_id(struct sja1105_private *priv) | |
2049 | { | |
2050 | const struct sja1105_regs *regs = priv->info->regs; | |
2051 | u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; | |
2052 | struct device *dev = &priv->spidev->dev; | |
2053 | u64 device_id; | |
2054 | u64 part_no; | |
2055 | int rc; | |
2056 | ||
2057 | rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id, | |
2058 | &device_id, SJA1105_SIZE_DEVICE_ID); | |
2059 | if (rc < 0) | |
2060 | return rc; | |
2061 | ||
2062 | if (device_id != priv->info->device_id) { | |
2063 | dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n", | |
2064 | priv->info->device_id, device_id); | |
2065 | return -ENODEV; | |
2066 | } | |
2067 | ||
2068 | rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id, | |
2069 | prod_id, SJA1105_SIZE_DEVICE_ID); | |
2070 | if (rc < 0) | |
2071 | return rc; | |
2072 | ||
2073 | sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); | |
2074 | ||
2075 | if (part_no != priv->info->part_no) { | |
2076 | dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n", | |
2077 | priv->info->part_no, part_no); | |
2078 | return -ENODEV; | |
2079 | } | |
2080 | ||
2081 | return 0; | |
2082 | } | |
2083 | ||
2084 | static int sja1105_probe(struct spi_device *spi) | |
2085 | { | |
844d7edc | 2086 | struct sja1105_tagger_data *tagger_data; |
8aa9ebcc VO |
2087 | struct device *dev = &spi->dev; |
2088 | struct sja1105_private *priv; | |
2089 | struct dsa_switch *ds; | |
227d07a0 | 2090 | int rc, i; |
8aa9ebcc VO |
2091 | |
2092 | if (!dev->of_node) { | |
2093 | dev_err(dev, "No DTS bindings for SJA1105 driver\n"); | |
2094 | return -EINVAL; | |
2095 | } | |
2096 | ||
2097 | priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); | |
2098 | if (!priv) | |
2099 | return -ENOMEM; | |
2100 | ||
2101 | /* Configure the optional reset pin and bring up switch */ | |
2102 | priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); | |
2103 | if (IS_ERR(priv->reset_gpio)) | |
2104 | dev_dbg(dev, "reset-gpios not defined, ignoring\n"); | |
2105 | else | |
2106 | sja1105_hw_reset(priv->reset_gpio, 1, 1); | |
2107 | ||
2108 | /* Populate our driver private structure (priv) based on | |
2109 | * the device tree node that was probed (spi) | |
2110 | */ | |
2111 | priv->spidev = spi; | |
2112 | spi_set_drvdata(spi, priv); | |
2113 | ||
2114 | /* Configure the SPI bus */ | |
2115 | spi->bits_per_word = 8; | |
2116 | rc = spi_setup(spi); | |
2117 | if (rc < 0) { | |
2118 | dev_err(dev, "Could not init SPI\n"); | |
2119 | return rc; | |
2120 | } | |
2121 | ||
2122 | priv->info = of_device_get_match_data(dev); | |
2123 | ||
2124 | /* Detect hardware device */ | |
2125 | rc = sja1105_check_device_id(priv); | |
2126 | if (rc < 0) { | |
2127 | dev_err(dev, "Device ID check failed: %d\n", rc); | |
2128 | return rc; | |
2129 | } | |
2130 | ||
2131 | dev_info(dev, "Probed switch chip: %s\n", priv->info->name); | |
2132 | ||
2133 | ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS); | |
2134 | if (!ds) | |
2135 | return -ENOMEM; | |
2136 | ||
2137 | ds->ops = &sja1105_switch_ops; | |
2138 | ds->priv = priv; | |
2139 | priv->ds = ds; | |
2140 | ||
844d7edc VO |
2141 | tagger_data = &priv->tagger_data; |
2142 | skb_queue_head_init(&tagger_data->skb_rxtstamp_queue); | |
f3097be2 | 2143 | INIT_WORK(&tagger_data->rxtstamp_work, sja1105_rxtstamp_work); |
844d7edc | 2144 | |
227d07a0 VO |
2145 | /* Connections between dsa_port and sja1105_port */ |
2146 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
2147 | struct sja1105_port *sp = &priv->ports[i]; | |
2148 | ||
2149 | ds->ports[i].priv = sp; | |
2150 | sp->dp = &ds->ports[i]; | |
844d7edc | 2151 | sp->data = tagger_data; |
227d07a0 VO |
2152 | } |
2153 | mutex_init(&priv->mgmt_lock); | |
2154 | ||
8aa9ebcc VO |
2155 | return dsa_register_switch(priv->ds); |
2156 | } | |
2157 | ||
2158 | static int sja1105_remove(struct spi_device *spi) | |
2159 | { | |
2160 | struct sja1105_private *priv = spi_get_drvdata(spi); | |
2161 | ||
bb77f36a | 2162 | sja1105_ptp_clock_unregister(priv); |
8aa9ebcc VO |
2163 | dsa_unregister_switch(priv->ds); |
2164 | sja1105_static_config_free(&priv->static_config); | |
2165 | return 0; | |
2166 | } | |
2167 | ||
2168 | static const struct of_device_id sja1105_dt_ids[] = { | |
2169 | { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, | |
2170 | { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, | |
2171 | { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, | |
2172 | { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, | |
2173 | { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, | |
2174 | { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, | |
2175 | { /* sentinel */ }, | |
2176 | }; | |
2177 | MODULE_DEVICE_TABLE(of, sja1105_dt_ids); | |
2178 | ||
2179 | static struct spi_driver sja1105_driver = { | |
2180 | .driver = { | |
2181 | .name = "sja1105", | |
2182 | .owner = THIS_MODULE, | |
2183 | .of_match_table = of_match_ptr(sja1105_dt_ids), | |
2184 | }, | |
2185 | .probe = sja1105_probe, | |
2186 | .remove = sja1105_remove, | |
2187 | }; | |
2188 | ||
2189 | module_spi_driver(sja1105_driver); | |
2190 | ||
2191 | MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); | |
2192 | MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); | |
2193 | MODULE_DESCRIPTION("SJA1105 Driver"); | |
2194 | MODULE_LICENSE("GPL v2"); |