Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/* Realtek SMI library helpers for the RTL8366x variants
  3 * RTL8366RB and RTL8366S
  4 *
  5 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  6 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  7 * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
  8 * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
  9 * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
 10 */
 11#include <linux/if_bridge.h>
 12#include <net/dsa.h>
 13
 14#include "realtek-smi-core.h"
 15
 16int rtl8366_mc_is_used(struct realtek_smi *smi, int mc_index, int *used)
 17{
 18	int ret;
 19	int i;
 20
 21	*used = 0;
 22	for (i = 0; i < smi->num_ports; i++) {
 23		int index = 0;
 24
 25		ret = smi->ops->get_mc_index(smi, i, &index);
 26		if (ret)
 27			return ret;
 28
 29		if (mc_index == index) {
 30			*used = 1;
 31			break;
 32		}
 33	}
 34
 35	return 0;
 36}
 37EXPORT_SYMBOL_GPL(rtl8366_mc_is_used);
 38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 39int rtl8366_set_vlan(struct realtek_smi *smi, int vid, u32 member,
 40		     u32 untag, u32 fid)
 41{
 
 42	struct rtl8366_vlan_4k vlan4k;
 
 43	int ret;
 44	int i;
 
 
 45
 46	dev_dbg(smi->dev,
 47		"setting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
 48		vid, member, untag);
 49
 50	/* Update the 4K table */
 51	ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
 52	if (ret)
 53		return ret;
 54
 55	vlan4k.member |= member;
 56	vlan4k.untag |= untag;
 57	vlan4k.fid = fid;
 58	ret = smi->ops->set_vlan_4k(smi, &vlan4k);
 59	if (ret)
 60		return ret;
 61
 62	dev_dbg(smi->dev,
 63		"resulting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
 64		vid, vlan4k.member, vlan4k.untag);
 65
 66	/* Try to find an existing MC entry for this VID */
 67	for (i = 0; i < smi->num_vlan_mc; i++) {
 68		struct rtl8366_vlan_mc vlanmc;
 69
 70		ret = smi->ops->get_vlan_mc(smi, i, &vlanmc);
 71		if (ret)
 72			return ret;
 73
 74		if (vid == vlanmc.vid) {
 75			/* update the MC entry */
 76			vlanmc.member |= member;
 77			vlanmc.untag |= untag;
 78			vlanmc.fid = fid;
 79
 80			ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
 81
 82			dev_dbg(smi->dev,
 83				"resulting VLAN%d MC members: 0x%02x, untagged: 0x%02x\n",
 84				vid, vlanmc.member, vlanmc.untag);
 
 85
 86			break;
 87		}
 88	}
 
 
 
 
 
 
 89
 90	return ret;
 91}
 92EXPORT_SYMBOL_GPL(rtl8366_set_vlan);
 93
 94int rtl8366_get_pvid(struct realtek_smi *smi, int port, int *val)
 95{
 96	struct rtl8366_vlan_mc vlanmc;
 97	int ret;
 98	int index;
 99
100	ret = smi->ops->get_mc_index(smi, port, &index);
101	if (ret)
102		return ret;
103
104	ret = smi->ops->get_vlan_mc(smi, index, &vlanmc);
105	if (ret)
106		return ret;
107
108	*val = vlanmc.vid;
109	return 0;
110}
111EXPORT_SYMBOL_GPL(rtl8366_get_pvid);
112
113int rtl8366_set_pvid(struct realtek_smi *smi, unsigned int port,
114		     unsigned int vid)
115{
116	struct rtl8366_vlan_mc vlanmc;
117	struct rtl8366_vlan_4k vlan4k;
118	int ret;
119	int i;
120
121	/* Try to find an existing MC entry for this VID */
122	for (i = 0; i < smi->num_vlan_mc; i++) {
123		ret = smi->ops->get_vlan_mc(smi, i, &vlanmc);
124		if (ret)
125			return ret;
126
127		if (vid == vlanmc.vid) {
128			ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
129			if (ret)
130				return ret;
131
132			ret = smi->ops->set_mc_index(smi, port, i);
133			return ret;
134		}
135	}
136
137	/* We have no MC entry for this VID, try to find an empty one */
138	for (i = 0; i < smi->num_vlan_mc; i++) {
139		ret = smi->ops->get_vlan_mc(smi, i, &vlanmc);
140		if (ret)
141			return ret;
142
143		if (vlanmc.vid == 0 && vlanmc.member == 0) {
144			/* Update the entry from the 4K table */
145			ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
146			if (ret)
147				return ret;
148
149			vlanmc.vid = vid;
150			vlanmc.member = vlan4k.member;
151			vlanmc.untag = vlan4k.untag;
152			vlanmc.fid = vlan4k.fid;
153			ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
154			if (ret)
155				return ret;
156
157			ret = smi->ops->set_mc_index(smi, port, i);
158			return ret;
159		}
160	}
161
162	/* MC table is full, try to find an unused entry and replace it */
163	for (i = 0; i < smi->num_vlan_mc; i++) {
164		int used;
165
166		ret = rtl8366_mc_is_used(smi, i, &used);
167		if (ret)
168			return ret;
169
170		if (!used) {
171			/* Update the entry from the 4K table */
172			ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
173			if (ret)
174				return ret;
175
176			vlanmc.vid = vid;
177			vlanmc.member = vlan4k.member;
178			vlanmc.untag = vlan4k.untag;
179			vlanmc.fid = vlan4k.fid;
180			ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
181			if (ret)
182				return ret;
183
184			ret = smi->ops->set_mc_index(smi, port, i);
185			return ret;
186		}
 
 
187	}
188
189	dev_err(smi->dev,
190		"all VLAN member configurations are in use\n");
191
192	return -ENOSPC;
193}
194EXPORT_SYMBOL_GPL(rtl8366_set_pvid);
195
196int rtl8366_enable_vlan4k(struct realtek_smi *smi, bool enable)
197{
198	int ret;
199
200	/* To enable 4k VLAN, ordinary VLAN must be enabled first,
201	 * but if we disable 4k VLAN it is fine to leave ordinary
202	 * VLAN enabled.
203	 */
204	if (enable) {
205		/* Make sure VLAN is ON */
206		ret = smi->ops->enable_vlan(smi, true);
207		if (ret)
208			return ret;
209
210		smi->vlan_enabled = true;
211	}
212
213	ret = smi->ops->enable_vlan4k(smi, enable);
214	if (ret)
215		return ret;
216
217	smi->vlan4k_enabled = enable;
218	return 0;
219}
220EXPORT_SYMBOL_GPL(rtl8366_enable_vlan4k);
221
222int rtl8366_enable_vlan(struct realtek_smi *smi, bool enable)
223{
224	int ret;
225
226	ret = smi->ops->enable_vlan(smi, enable);
227	if (ret)
228		return ret;
229
230	smi->vlan_enabled = enable;
231
232	/* If we turn VLAN off, make sure that we turn off
233	 * 4k VLAN as well, if that happened to be on.
234	 */
235	if (!enable) {
236		smi->vlan4k_enabled = false;
237		ret = smi->ops->enable_vlan4k(smi, false);
238	}
239
240	return ret;
241}
242EXPORT_SYMBOL_GPL(rtl8366_enable_vlan);
243
244int rtl8366_reset_vlan(struct realtek_smi *smi)
245{
246	struct rtl8366_vlan_mc vlanmc;
247	int ret;
248	int i;
249
250	rtl8366_enable_vlan(smi, false);
251	rtl8366_enable_vlan4k(smi, false);
252
253	/* Clear the 16 VLAN member configurations */
254	vlanmc.vid = 0;
255	vlanmc.priority = 0;
256	vlanmc.member = 0;
257	vlanmc.untag = 0;
258	vlanmc.fid = 0;
259	for (i = 0; i < smi->num_vlan_mc; i++) {
260		ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
261		if (ret)
262			return ret;
263	}
264
265	return 0;
266}
267EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
268
269int rtl8366_init_vlan(struct realtek_smi *smi)
270{
271	int port;
272	int ret;
273
274	ret = rtl8366_reset_vlan(smi);
275	if (ret)
276		return ret;
277
278	/* Loop over the available ports, for each port, associate
279	 * it with the VLAN (port+1)
280	 */
281	for (port = 0; port < smi->num_ports; port++) {
282		u32 mask;
283
284		if (port == smi->cpu_port)
285			/* For the CPU port, make all ports members of this
286			 * VLAN.
287			 */
288			mask = GENMASK((int)smi->num_ports - 1, 0);
289		else
290			/* For all other ports, enable itself plus the
291			 * CPU port.
292			 */
293			mask = BIT(port) | BIT(smi->cpu_port);
294
295		/* For each port, set the port as member of VLAN (port+1)
296		 * and untagged, except for the CPU port: the CPU port (5) is
297		 * member of VLAN 6 and so are ALL the other ports as well.
298		 * Use filter 0 (no filter).
299		 */
300		dev_info(smi->dev, "VLAN%d port mask for port %d, %08x\n",
301			 (port + 1), port, mask);
302		ret = rtl8366_set_vlan(smi, (port + 1), mask, mask, 0);
303		if (ret)
304			return ret;
305
306		dev_info(smi->dev, "VLAN%d port %d, PVID set to %d\n",
307			 (port + 1), port, (port + 1));
308		ret = rtl8366_set_pvid(smi, port, (port + 1));
309		if (ret)
310			return ret;
311	}
312
313	return rtl8366_enable_vlan(smi, true);
314}
315EXPORT_SYMBOL_GPL(rtl8366_init_vlan);
316
317int rtl8366_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
 
318{
319	struct realtek_smi *smi = ds->priv;
320	struct rtl8366_vlan_4k vlan4k;
321	int ret;
322
323	/* Use VLAN nr port + 1 since VLAN0 is not valid */
324	if (!smi->ops->is_vlan_valid(smi, port + 1))
325		return -EINVAL;
326
327	dev_info(smi->dev, "%s filtering on port %d\n",
328		 vlan_filtering ? "enable" : "disable",
329		 port);
330
331	/* TODO:
332	 * The hardware support filter ID (FID) 0..7, I have no clue how to
333	 * support this in the driver when the callback only says on/off.
334	 */
335	ret = smi->ops->get_vlan_4k(smi, port + 1, &vlan4k);
336	if (ret)
337		return ret;
338
339	/* Just set the filter to FID 1 for now then */
340	ret = rtl8366_set_vlan(smi, port + 1,
341			       vlan4k.member,
342			       vlan4k.untag,
343			       1);
344	if (ret)
345		return ret;
346
347	return 0;
348}
349EXPORT_SYMBOL_GPL(rtl8366_vlan_filtering);
350
351int rtl8366_vlan_prepare(struct dsa_switch *ds, int port,
352			 const struct switchdev_obj_port_vlan *vlan)
 
353{
 
 
354	struct realtek_smi *smi = ds->priv;
355	u16 vid;
 
356	int ret;
357
358	for (vid = vlan->vid_begin; vid < vlan->vid_end; vid++)
359		if (!smi->ops->is_vlan_valid(smi, vid))
360			return -EINVAL;
361
362	dev_info(smi->dev, "prepare VLANs %04x..%04x\n",
363		 vlan->vid_begin, vlan->vid_end);
364
365	/* Enable VLAN in the hardware
366	 * FIXME: what's with this 4k business?
367	 * Just rtl8366_enable_vlan() seems inconclusive.
368	 */
369	ret = rtl8366_enable_vlan4k(smi, true);
370	if (ret)
 
371		return ret;
 
372
373	return 0;
374}
375EXPORT_SYMBOL_GPL(rtl8366_vlan_prepare);
376
377void rtl8366_vlan_add(struct dsa_switch *ds, int port,
378		      const struct switchdev_obj_port_vlan *vlan)
379{
380	bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
381	bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
382	struct realtek_smi *smi = ds->priv;
383	u32 member = 0;
384	u32 untag = 0;
385	u16 vid;
386	int ret;
387
388	for (vid = vlan->vid_begin; vid < vlan->vid_end; vid++)
389		if (!smi->ops->is_vlan_valid(smi, vid))
390			return;
391
392	dev_info(smi->dev, "add VLAN on port %d, %s, %s\n",
393		 port,
394		 untagged ? "untagged" : "tagged",
395		 pvid ? " PVID" : "no PVID");
396
397	if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
398		dev_err(smi->dev, "port is DSA or CPU port\n");
399
400	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
401		int pvid_val = 0;
402
403		dev_info(smi->dev, "add VLAN %04x\n", vid);
404		member |= BIT(port);
405
406		if (untagged)
407			untag |= BIT(port);
 
 
 
408
409		/* To ensure that we have a valid MC entry for this VLAN,
410		 * initialize the port VLAN ID here.
411		 */
412		ret = rtl8366_get_pvid(smi, port, &pvid_val);
413		if (ret < 0) {
414			dev_err(smi->dev, "could not lookup PVID for port %d\n",
415				port);
416			return;
417		}
418		if (pvid_val == 0) {
419			ret = rtl8366_set_pvid(smi, port, vid);
420			if (ret < 0)
421				return;
422		}
423
424		ret = rtl8366_set_vlan(smi, vid, member, untag, 0);
425		if (ret)
426			dev_err(smi->dev,
427				"failed to set up VLAN %04x",
428				vid);
429	}
 
 
430}
431EXPORT_SYMBOL_GPL(rtl8366_vlan_add);
432
433int rtl8366_vlan_del(struct dsa_switch *ds, int port,
434		     const struct switchdev_obj_port_vlan *vlan)
435{
436	struct realtek_smi *smi = ds->priv;
437	u16 vid;
438	int ret;
439
440	dev_info(smi->dev, "del VLAN on port %d\n", port);
441
442	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
443		int i;
444
445		dev_info(smi->dev, "del VLAN %04x\n", vid);
 
446
447		for (i = 0; i < smi->num_vlan_mc; i++) {
448			struct rtl8366_vlan_mc vlanmc;
 
449
450			ret = smi->ops->get_vlan_mc(smi, i, &vlanmc);
451			if (ret)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452				return ret;
453
454			if (vid == vlanmc.vid) {
455				/* Remove this port from the VLAN */
456				vlanmc.member &= ~BIT(port);
457				vlanmc.untag &= ~BIT(port);
458				/*
459				 * If no ports are members of this VLAN
460				 * anymore then clear the whole member
461				 * config so it can be reused.
462				 */
463				if (!vlanmc.member && vlanmc.untag) {
464					vlanmc.vid = 0;
465					vlanmc.priority = 0;
466					vlanmc.fid = 0;
467				}
468				ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
469				if (ret) {
470					dev_err(smi->dev,
471						"failed to remove VLAN %04x\n",
472						vid);
473					return ret;
474				}
475				break;
476			}
 
477		}
478	}
479
480	return 0;
481}
482EXPORT_SYMBOL_GPL(rtl8366_vlan_del);
483
484void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset,
485			 uint8_t *data)
486{
487	struct realtek_smi *smi = ds->priv;
488	struct rtl8366_mib_counter *mib;
489	int i;
490
491	if (port >= smi->num_ports)
492		return;
493
494	for (i = 0; i < smi->num_mib_counters; i++) {
495		mib = &smi->mib_counters[i];
496		strncpy(data + i * ETH_GSTRING_LEN,
497			mib->name, ETH_GSTRING_LEN);
498	}
499}
500EXPORT_SYMBOL_GPL(rtl8366_get_strings);
501
502int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset)
503{
504	struct realtek_smi *smi = ds->priv;
505
506	/* We only support SS_STATS */
507	if (sset != ETH_SS_STATS)
508		return 0;
509	if (port >= smi->num_ports)
510		return -EINVAL;
511
512	return smi->num_mib_counters;
513}
514EXPORT_SYMBOL_GPL(rtl8366_get_sset_count);
515
516void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
517{
518	struct realtek_smi *smi = ds->priv;
519	int i;
520	int ret;
521
522	if (port >= smi->num_ports)
523		return;
524
525	for (i = 0; i < smi->num_mib_counters; i++) {
526		struct rtl8366_mib_counter *mib;
527		u64 mibvalue = 0;
528
529		mib = &smi->mib_counters[i];
530		ret = smi->ops->get_mib_counter(smi, port, mib, &mibvalue);
531		if (ret) {
532			dev_err(smi->dev, "error reading MIB counter %s\n",
533				mib->name);
534		}
535		data[i] = mibvalue;
536	}
537}
538EXPORT_SYMBOL_GPL(rtl8366_get_ethtool_stats);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/* Realtek SMI library helpers for the RTL8366x variants
  3 * RTL8366RB and RTL8366S
  4 *
  5 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  6 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
  7 * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
  8 * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
  9 * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
 10 */
 11#include <linux/if_bridge.h>
 12#include <net/dsa.h>
 13
 14#include "realtek-smi-core.h"
 15
 16int rtl8366_mc_is_used(struct realtek_smi *smi, int mc_index, int *used)
 17{
 18	int ret;
 19	int i;
 20
 21	*used = 0;
 22	for (i = 0; i < smi->num_ports; i++) {
 23		int index = 0;
 24
 25		ret = smi->ops->get_mc_index(smi, i, &index);
 26		if (ret)
 27			return ret;
 28
 29		if (mc_index == index) {
 30			*used = 1;
 31			break;
 32		}
 33	}
 34
 35	return 0;
 36}
 37EXPORT_SYMBOL_GPL(rtl8366_mc_is_used);
 38
 39/**
 40 * rtl8366_obtain_mc() - retrieve or allocate a VLAN member configuration
 41 * @smi: the Realtek SMI device instance
 42 * @vid: the VLAN ID to look up or allocate
 43 * @vlanmc: the pointer will be assigned to a pointer to a valid member config
 44 * if successful
 45 * @return: index of a new member config or negative error number
 46 */
 47static int rtl8366_obtain_mc(struct realtek_smi *smi, int vid,
 48			     struct rtl8366_vlan_mc *vlanmc)
 49{
 50	struct rtl8366_vlan_4k vlan4k;
 51	int ret;
 52	int i;
 53
 54	/* Try to find an existing member config entry for this VID */
 55	for (i = 0; i < smi->num_vlan_mc; i++) {
 56		ret = smi->ops->get_vlan_mc(smi, i, vlanmc);
 57		if (ret) {
 58			dev_err(smi->dev, "error searching for VLAN MC %d for VID %d\n",
 59				i, vid);
 60			return ret;
 61		}
 62
 63		if (vid == vlanmc->vid)
 64			return i;
 65	}
 66
 67	/* We have no MC entry for this VID, try to find an empty one */
 68	for (i = 0; i < smi->num_vlan_mc; i++) {
 69		ret = smi->ops->get_vlan_mc(smi, i, vlanmc);
 70		if (ret) {
 71			dev_err(smi->dev, "error searching for VLAN MC %d for VID %d\n",
 72				i, vid);
 73			return ret;
 74		}
 75
 76		if (vlanmc->vid == 0 && vlanmc->member == 0) {
 77			/* Update the entry from the 4K table */
 78			ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
 79			if (ret) {
 80				dev_err(smi->dev, "error looking for 4K VLAN MC %d for VID %d\n",
 81					i, vid);
 82				return ret;
 83			}
 84
 85			vlanmc->vid = vid;
 86			vlanmc->member = vlan4k.member;
 87			vlanmc->untag = vlan4k.untag;
 88			vlanmc->fid = vlan4k.fid;
 89			ret = smi->ops->set_vlan_mc(smi, i, vlanmc);
 90			if (ret) {
 91				dev_err(smi->dev, "unable to set/update VLAN MC %d for VID %d\n",
 92					i, vid);
 93				return ret;
 94			}
 95
 96			dev_dbg(smi->dev, "created new MC at index %d for VID %d\n",
 97				i, vid);
 98			return i;
 99		}
100	}
101
102	/* MC table is full, try to find an unused entry and replace it */
103	for (i = 0; i < smi->num_vlan_mc; i++) {
104		int used;
105
106		ret = rtl8366_mc_is_used(smi, i, &used);
107		if (ret)
108			return ret;
109
110		if (!used) {
111			/* Update the entry from the 4K table */
112			ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
113			if (ret)
114				return ret;
115
116			vlanmc->vid = vid;
117			vlanmc->member = vlan4k.member;
118			vlanmc->untag = vlan4k.untag;
119			vlanmc->fid = vlan4k.fid;
120			ret = smi->ops->set_vlan_mc(smi, i, vlanmc);
121			if (ret) {
122				dev_err(smi->dev, "unable to set/update VLAN MC %d for VID %d\n",
123					i, vid);
124				return ret;
125			}
126			dev_dbg(smi->dev, "recycled MC at index %i for VID %d\n",
127				i, vid);
128			return i;
129		}
130	}
131
132	dev_err(smi->dev, "all VLAN member configurations are in use\n");
133	return -ENOSPC;
134}
135
136int rtl8366_set_vlan(struct realtek_smi *smi, int vid, u32 member,
137		     u32 untag, u32 fid)
138{
139	struct rtl8366_vlan_mc vlanmc;
140	struct rtl8366_vlan_4k vlan4k;
141	int mc;
142	int ret;
143
144	if (!smi->ops->is_vlan_valid(smi, vid))
145		return -EINVAL;
146
147	dev_dbg(smi->dev,
148		"setting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
149		vid, member, untag);
150
151	/* Update the 4K table */
152	ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
153	if (ret)
154		return ret;
155
156	vlan4k.member |= member;
157	vlan4k.untag |= untag;
158	vlan4k.fid = fid;
159	ret = smi->ops->set_vlan_4k(smi, &vlan4k);
160	if (ret)
161		return ret;
162
163	dev_dbg(smi->dev,
164		"resulting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
165		vid, vlan4k.member, vlan4k.untag);
166
167	/* Find or allocate a member config for this VID */
168	ret = rtl8366_obtain_mc(smi, vid, &vlanmc);
169	if (ret < 0)
170		return ret;
171	mc = ret;
 
 
 
 
 
 
 
 
 
 
172
173	/* Update the MC entry */
174	vlanmc.member |= member;
175	vlanmc.untag |= untag;
176	vlanmc.fid = fid;
177
178	/* Commit updates to the MC entry */
179	ret = smi->ops->set_vlan_mc(smi, mc, &vlanmc);
180	if (ret)
181		dev_err(smi->dev, "failed to commit changes to VLAN MC index %d for VID %d\n",
182			mc, vid);
183	else
184		dev_dbg(smi->dev,
185			"resulting VLAN%d MC members: 0x%02x, untagged: 0x%02x\n",
186			vid, vlanmc.member, vlanmc.untag);
187
188	return ret;
189}
190EXPORT_SYMBOL_GPL(rtl8366_set_vlan);
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192int rtl8366_set_pvid(struct realtek_smi *smi, unsigned int port,
193		     unsigned int vid)
194{
195	struct rtl8366_vlan_mc vlanmc;
196	int mc;
197	int ret;
 
198
199	if (!smi->ops->is_vlan_valid(smi, vid))
200		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
202	/* Find or allocate a member config for this VID */
203	ret = rtl8366_obtain_mc(smi, vid, &vlanmc);
204	if (ret < 0)
205		return ret;
206	mc = ret;
 
 
207
208	ret = smi->ops->set_mc_index(smi, port, mc);
209	if (ret) {
210		dev_err(smi->dev, "set PVID: failed to set MC index %d for port %d\n",
211			mc, port);
212		return ret;
213	}
214
215	dev_dbg(smi->dev, "set PVID: the PVID for port %d set to %d using existing MC index %d\n",
216		port, vid, mc);
217
218	return 0;
219}
220EXPORT_SYMBOL_GPL(rtl8366_set_pvid);
221
222int rtl8366_enable_vlan4k(struct realtek_smi *smi, bool enable)
223{
224	int ret;
225
226	/* To enable 4k VLAN, ordinary VLAN must be enabled first,
227	 * but if we disable 4k VLAN it is fine to leave ordinary
228	 * VLAN enabled.
229	 */
230	if (enable) {
231		/* Make sure VLAN is ON */
232		ret = smi->ops->enable_vlan(smi, true);
233		if (ret)
234			return ret;
235
236		smi->vlan_enabled = true;
237	}
238
239	ret = smi->ops->enable_vlan4k(smi, enable);
240	if (ret)
241		return ret;
242
243	smi->vlan4k_enabled = enable;
244	return 0;
245}
246EXPORT_SYMBOL_GPL(rtl8366_enable_vlan4k);
247
248int rtl8366_enable_vlan(struct realtek_smi *smi, bool enable)
249{
250	int ret;
251
252	ret = smi->ops->enable_vlan(smi, enable);
253	if (ret)
254		return ret;
255
256	smi->vlan_enabled = enable;
257
258	/* If we turn VLAN off, make sure that we turn off
259	 * 4k VLAN as well, if that happened to be on.
260	 */
261	if (!enable) {
262		smi->vlan4k_enabled = false;
263		ret = smi->ops->enable_vlan4k(smi, false);
264	}
265
266	return ret;
267}
268EXPORT_SYMBOL_GPL(rtl8366_enable_vlan);
269
270int rtl8366_reset_vlan(struct realtek_smi *smi)
271{
272	struct rtl8366_vlan_mc vlanmc;
273	int ret;
274	int i;
275
276	rtl8366_enable_vlan(smi, false);
277	rtl8366_enable_vlan4k(smi, false);
278
279	/* Clear the 16 VLAN member configurations */
280	vlanmc.vid = 0;
281	vlanmc.priority = 0;
282	vlanmc.member = 0;
283	vlanmc.untag = 0;
284	vlanmc.fid = 0;
285	for (i = 0; i < smi->num_vlan_mc; i++) {
286		ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
287		if (ret)
288			return ret;
289	}
290
291	return 0;
292}
293EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
294
295int rtl8366_init_vlan(struct realtek_smi *smi)
296{
297	int port;
298	int ret;
299
300	ret = rtl8366_reset_vlan(smi);
301	if (ret)
302		return ret;
303
304	/* Loop over the available ports, for each port, associate
305	 * it with the VLAN (port+1)
306	 */
307	for (port = 0; port < smi->num_ports; port++) {
308		u32 mask;
309
310		if (port == smi->cpu_port)
311			/* For the CPU port, make all ports members of this
312			 * VLAN.
313			 */
314			mask = GENMASK((int)smi->num_ports - 1, 0);
315		else
316			/* For all other ports, enable itself plus the
317			 * CPU port.
318			 */
319			mask = BIT(port) | BIT(smi->cpu_port);
320
321		/* For each port, set the port as member of VLAN (port+1)
322		 * and untagged, except for the CPU port: the CPU port (5) is
323		 * member of VLAN 6 and so are ALL the other ports as well.
324		 * Use filter 0 (no filter).
325		 */
326		dev_info(smi->dev, "VLAN%d port mask for port %d, %08x\n",
327			 (port + 1), port, mask);
328		ret = rtl8366_set_vlan(smi, (port + 1), mask, mask, 0);
329		if (ret)
330			return ret;
331
332		dev_info(smi->dev, "VLAN%d port %d, PVID set to %d\n",
333			 (port + 1), port, (port + 1));
334		ret = rtl8366_set_pvid(smi, port, (port + 1));
335		if (ret)
336			return ret;
337	}
338
339	return rtl8366_enable_vlan(smi, true);
340}
341EXPORT_SYMBOL_GPL(rtl8366_init_vlan);
342
343int rtl8366_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
344			   struct netlink_ext_ack *extack)
345{
346	struct realtek_smi *smi = ds->priv;
347	struct rtl8366_vlan_4k vlan4k;
348	int ret;
349
350	/* Use VLAN nr port + 1 since VLAN0 is not valid */
351	if (!smi->ops->is_vlan_valid(smi, port + 1))
352		return -EINVAL;
353
354	dev_info(smi->dev, "%s filtering on port %d\n",
355		 vlan_filtering ? "enable" : "disable",
356		 port);
357
358	/* TODO:
359	 * The hardware support filter ID (FID) 0..7, I have no clue how to
360	 * support this in the driver when the callback only says on/off.
361	 */
362	ret = smi->ops->get_vlan_4k(smi, port + 1, &vlan4k);
363	if (ret)
364		return ret;
365
366	/* Just set the filter to FID 1 for now then */
367	ret = rtl8366_set_vlan(smi, port + 1,
368			       vlan4k.member,
369			       vlan4k.untag,
370			       1);
371	if (ret)
372		return ret;
373
374	return 0;
375}
376EXPORT_SYMBOL_GPL(rtl8366_vlan_filtering);
377
378int rtl8366_vlan_add(struct dsa_switch *ds, int port,
379		     const struct switchdev_obj_port_vlan *vlan,
380		     struct netlink_ext_ack *extack)
381{
382	bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
383	bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
384	struct realtek_smi *smi = ds->priv;
385	u32 member = 0;
386	u32 untag = 0;
387	int ret;
388
389	if (!smi->ops->is_vlan_valid(smi, vlan->vid)) {
390		NL_SET_ERR_MSG_MOD(extack, "VLAN ID not valid");
391		return -EINVAL;
392	}
 
 
393
394	/* Enable VLAN in the hardware
395	 * FIXME: what's with this 4k business?
396	 * Just rtl8366_enable_vlan() seems inconclusive.
397	 */
398	ret = rtl8366_enable_vlan4k(smi, true);
399	if (ret) {
400		NL_SET_ERR_MSG_MOD(extack, "Failed to enable VLAN 4K");
401		return ret;
402	}
403
404	dev_info(smi->dev, "add VLAN %d on port %d, %s, %s\n",
405		 vlan->vid, port, untagged ? "untagged" : "tagged",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406		 pvid ? " PVID" : "no PVID");
407
408	if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
409		dev_err(smi->dev, "port is DSA or CPU port\n");
410
411	member |= BIT(port);
 
412
413	if (untagged)
414		untag |= BIT(port);
415
416	ret = rtl8366_set_vlan(smi, vlan->vid, member, untag, 0);
417	if (ret) {
418		dev_err(smi->dev, "failed to set up VLAN %04x", vlan->vid);
419		return ret;
420	}
421
422	if (!pvid)
423		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
424
425	ret = rtl8366_set_pvid(smi, port, vlan->vid);
426	if (ret) {
427		dev_err(smi->dev, "failed to set PVID on port %d to VLAN %04x",
428			port, vlan->vid);
429		return ret;
430	}
431
432	return 0;
433}
434EXPORT_SYMBOL_GPL(rtl8366_vlan_add);
435
436int rtl8366_vlan_del(struct dsa_switch *ds, int port,
437		     const struct switchdev_obj_port_vlan *vlan)
438{
439	struct realtek_smi *smi = ds->priv;
440	int ret, i;
 
 
 
441
442	dev_info(smi->dev, "del VLAN %04x on port %d\n", vlan->vid, port);
 
443
444	for (i = 0; i < smi->num_vlan_mc; i++) {
445		struct rtl8366_vlan_mc vlanmc;
446
447		ret = smi->ops->get_vlan_mc(smi, i, &vlanmc);
448		if (ret)
449			return ret;
450
451		if (vlan->vid == vlanmc.vid) {
452			/* Remove this port from the VLAN */
453			vlanmc.member &= ~BIT(port);
454			vlanmc.untag &= ~BIT(port);
455			/*
456			 * If no ports are members of this VLAN
457			 * anymore then clear the whole member
458			 * config so it can be reused.
459			 */
460			if (!vlanmc.member && vlanmc.untag) {
461				vlanmc.vid = 0;
462				vlanmc.priority = 0;
463				vlanmc.fid = 0;
464			}
465			ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
466			if (ret) {
467				dev_err(smi->dev,
468					"failed to remove VLAN %04x\n",
469					vlan->vid);
470				return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471			}
472			break;
473		}
474	}
475
476	return 0;
477}
478EXPORT_SYMBOL_GPL(rtl8366_vlan_del);
479
480void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset,
481			 uint8_t *data)
482{
483	struct realtek_smi *smi = ds->priv;
484	struct rtl8366_mib_counter *mib;
485	int i;
486
487	if (port >= smi->num_ports)
488		return;
489
490	for (i = 0; i < smi->num_mib_counters; i++) {
491		mib = &smi->mib_counters[i];
492		strncpy(data + i * ETH_GSTRING_LEN,
493			mib->name, ETH_GSTRING_LEN);
494	}
495}
496EXPORT_SYMBOL_GPL(rtl8366_get_strings);
497
498int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset)
499{
500	struct realtek_smi *smi = ds->priv;
501
502	/* We only support SS_STATS */
503	if (sset != ETH_SS_STATS)
504		return 0;
505	if (port >= smi->num_ports)
506		return -EINVAL;
507
508	return smi->num_mib_counters;
509}
510EXPORT_SYMBOL_GPL(rtl8366_get_sset_count);
511
512void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
513{
514	struct realtek_smi *smi = ds->priv;
515	int i;
516	int ret;
517
518	if (port >= smi->num_ports)
519		return;
520
521	for (i = 0; i < smi->num_mib_counters; i++) {
522		struct rtl8366_mib_counter *mib;
523		u64 mibvalue = 0;
524
525		mib = &smi->mib_counters[i];
526		ret = smi->ops->get_mib_counter(smi, port, mib, &mibvalue);
527		if (ret) {
528			dev_err(smi->dev, "error reading MIB counter %s\n",
529				mib->name);
530		}
531		data[i] = mibvalue;
532	}
533}
534EXPORT_SYMBOL_GPL(rtl8366_get_ethtool_stats);