xref: /openbmc/linux/drivers/net/dsa/microchip/ksz_common.c (revision 48c77bdf729a91fa7f65765d3f60f01e0ac320c5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Microchip switch driver main logic
4  *
5  * Copyright (C) 2017-2019 Microchip Technology Inc.
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/export.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_data/microchip-ksz.h>
14 #include <linux/phy.h>
15 #include <linux/etherdevice.h>
16 #include <linux/if_bridge.h>
17 #include <linux/of_net.h>
18 #include <net/dsa.h>
19 #include <net/switchdev.h>
20 
21 #include "ksz_common.h"
22 
23 void ksz_update_port_member(struct ksz_device *dev, int port)
24 {
25 	struct ksz_port *p = &dev->ports[port];
26 	struct dsa_switch *ds = dev->ds;
27 	u8 port_member = 0, cpu_port;
28 	const struct dsa_port *dp;
29 	int i;
30 
31 	if (!dsa_is_user_port(ds, port))
32 		return;
33 
34 	dp = dsa_to_port(ds, port);
35 	cpu_port = BIT(dsa_upstream_port(ds, port));
36 
37 	for (i = 0; i < ds->num_ports; i++) {
38 		const struct dsa_port *other_dp = dsa_to_port(ds, i);
39 		struct ksz_port *other_p = &dev->ports[i];
40 		u8 val = 0;
41 
42 		if (!dsa_is_user_port(ds, i))
43 			continue;
44 		if (port == i)
45 			continue;
46 		if (!dsa_port_bridge_same(dp, other_dp))
47 			continue;
48 
49 		if (other_p->stp_state == BR_STATE_FORWARDING &&
50 		    p->stp_state == BR_STATE_FORWARDING) {
51 			val |= BIT(port);
52 			port_member |= BIT(i);
53 		}
54 
55 		dev->dev_ops->cfg_port_member(dev, i, val | cpu_port);
56 	}
57 
58 	dev->dev_ops->cfg_port_member(dev, port, port_member | cpu_port);
59 }
60 EXPORT_SYMBOL_GPL(ksz_update_port_member);
61 
62 static void port_r_cnt(struct ksz_device *dev, int port)
63 {
64 	struct ksz_port_mib *mib = &dev->ports[port].mib;
65 	u64 *dropped;
66 
67 	/* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
68 	while (mib->cnt_ptr < dev->reg_mib_cnt) {
69 		dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
70 					&mib->counters[mib->cnt_ptr]);
71 		++mib->cnt_ptr;
72 	}
73 
74 	/* last one in storage */
75 	dropped = &mib->counters[dev->mib_cnt];
76 
77 	/* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
78 	while (mib->cnt_ptr < dev->mib_cnt) {
79 		dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
80 					dropped, &mib->counters[mib->cnt_ptr]);
81 		++mib->cnt_ptr;
82 	}
83 	mib->cnt_ptr = 0;
84 }
85 
86 static void ksz_mib_read_work(struct work_struct *work)
87 {
88 	struct ksz_device *dev = container_of(work, struct ksz_device,
89 					      mib_read.work);
90 	struct ksz_port_mib *mib;
91 	struct ksz_port *p;
92 	int i;
93 
94 	for (i = 0; i < dev->port_cnt; i++) {
95 		if (dsa_is_unused_port(dev->ds, i))
96 			continue;
97 
98 		p = &dev->ports[i];
99 		mib = &p->mib;
100 		mutex_lock(&mib->cnt_mutex);
101 
102 		/* Only read MIB counters when the port is told to do.
103 		 * If not, read only dropped counters when link is not up.
104 		 */
105 		if (!p->read) {
106 			const struct dsa_port *dp = dsa_to_port(dev->ds, i);
107 
108 			if (!netif_carrier_ok(dp->slave))
109 				mib->cnt_ptr = dev->reg_mib_cnt;
110 		}
111 		port_r_cnt(dev, i);
112 		p->read = false;
113 
114 		if (dev->dev_ops->r_mib_stat64)
115 			dev->dev_ops->r_mib_stat64(dev, i);
116 
117 		mutex_unlock(&mib->cnt_mutex);
118 	}
119 
120 	schedule_delayed_work(&dev->mib_read, dev->mib_read_interval);
121 }
122 
123 void ksz_init_mib_timer(struct ksz_device *dev)
124 {
125 	int i;
126 
127 	INIT_DELAYED_WORK(&dev->mib_read, ksz_mib_read_work);
128 
129 	for (i = 0; i < dev->port_cnt; i++)
130 		dev->dev_ops->port_init_cnt(dev, i);
131 }
132 EXPORT_SYMBOL_GPL(ksz_init_mib_timer);
133 
134 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
135 {
136 	struct ksz_device *dev = ds->priv;
137 	u16 val = 0xffff;
138 
139 	dev->dev_ops->r_phy(dev, addr, reg, &val);
140 
141 	return val;
142 }
143 EXPORT_SYMBOL_GPL(ksz_phy_read16);
144 
145 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
146 {
147 	struct ksz_device *dev = ds->priv;
148 
149 	dev->dev_ops->w_phy(dev, addr, reg, val);
150 
151 	return 0;
152 }
153 EXPORT_SYMBOL_GPL(ksz_phy_write16);
154 
155 void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
156 		       phy_interface_t interface)
157 {
158 	struct ksz_device *dev = ds->priv;
159 	struct ksz_port *p = &dev->ports[port];
160 
161 	/* Read all MIB counters when the link is going down. */
162 	p->read = true;
163 	/* timer started */
164 	if (dev->mib_read_interval)
165 		schedule_delayed_work(&dev->mib_read, 0);
166 }
167 EXPORT_SYMBOL_GPL(ksz_mac_link_down);
168 
169 int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
170 {
171 	struct ksz_device *dev = ds->priv;
172 
173 	if (sset != ETH_SS_STATS)
174 		return 0;
175 
176 	return dev->mib_cnt;
177 }
178 EXPORT_SYMBOL_GPL(ksz_sset_count);
179 
180 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf)
181 {
182 	const struct dsa_port *dp = dsa_to_port(ds, port);
183 	struct ksz_device *dev = ds->priv;
184 	struct ksz_port_mib *mib;
185 
186 	mib = &dev->ports[port].mib;
187 	mutex_lock(&mib->cnt_mutex);
188 
189 	/* Only read dropped counters if no link. */
190 	if (!netif_carrier_ok(dp->slave))
191 		mib->cnt_ptr = dev->reg_mib_cnt;
192 	port_r_cnt(dev, port);
193 	memcpy(buf, mib->counters, dev->mib_cnt * sizeof(u64));
194 	mutex_unlock(&mib->cnt_mutex);
195 }
196 EXPORT_SYMBOL_GPL(ksz_get_ethtool_stats);
197 
198 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
199 			 struct dsa_bridge bridge,
200 			 bool *tx_fwd_offload)
201 {
202 	/* port_stp_state_set() will be called after to put the port in
203 	 * appropriate state so there is no need to do anything.
204 	 */
205 
206 	return 0;
207 }
208 EXPORT_SYMBOL_GPL(ksz_port_bridge_join);
209 
210 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
211 			   struct dsa_bridge bridge)
212 {
213 	/* port_stp_state_set() will be called after to put the port in
214 	 * forwarding state so there is no need to do anything.
215 	 */
216 }
217 EXPORT_SYMBOL_GPL(ksz_port_bridge_leave);
218 
219 void ksz_port_fast_age(struct dsa_switch *ds, int port)
220 {
221 	struct ksz_device *dev = ds->priv;
222 
223 	dev->dev_ops->flush_dyn_mac_table(dev, port);
224 }
225 EXPORT_SYMBOL_GPL(ksz_port_fast_age);
226 
227 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
228 		      void *data)
229 {
230 	struct ksz_device *dev = ds->priv;
231 	int ret = 0;
232 	u16 i = 0;
233 	u16 entries = 0;
234 	u8 timestamp = 0;
235 	u8 fid;
236 	u8 member;
237 	struct alu_struct alu;
238 
239 	do {
240 		alu.is_static = false;
241 		ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid,
242 						    &member, &timestamp,
243 						    &entries);
244 		if (!ret && (member & BIT(port))) {
245 			ret = cb(alu.mac, alu.fid, alu.is_static, data);
246 			if (ret)
247 				break;
248 		}
249 		i++;
250 	} while (i < entries);
251 	if (i >= entries)
252 		ret = 0;
253 
254 	return ret;
255 }
256 EXPORT_SYMBOL_GPL(ksz_port_fdb_dump);
257 
258 int ksz_port_mdb_add(struct dsa_switch *ds, int port,
259 		     const struct switchdev_obj_port_mdb *mdb)
260 {
261 	struct ksz_device *dev = ds->priv;
262 	struct alu_struct alu;
263 	int index;
264 	int empty = 0;
265 
266 	alu.port_forward = 0;
267 	for (index = 0; index < dev->num_statics; index++) {
268 		if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
269 			/* Found one already in static MAC table. */
270 			if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
271 			    alu.fid == mdb->vid)
272 				break;
273 		/* Remember the first empty entry. */
274 		} else if (!empty) {
275 			empty = index + 1;
276 		}
277 	}
278 
279 	/* no available entry */
280 	if (index == dev->num_statics && !empty)
281 		return -ENOSPC;
282 
283 	/* add entry */
284 	if (index == dev->num_statics) {
285 		index = empty - 1;
286 		memset(&alu, 0, sizeof(alu));
287 		memcpy(alu.mac, mdb->addr, ETH_ALEN);
288 		alu.is_static = true;
289 	}
290 	alu.port_forward |= BIT(port);
291 	if (mdb->vid) {
292 		alu.is_use_fid = true;
293 
294 		/* Need a way to map VID to FID. */
295 		alu.fid = mdb->vid;
296 	}
297 	dev->dev_ops->w_sta_mac_table(dev, index, &alu);
298 
299 	return 0;
300 }
301 EXPORT_SYMBOL_GPL(ksz_port_mdb_add);
302 
303 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
304 		     const struct switchdev_obj_port_mdb *mdb)
305 {
306 	struct ksz_device *dev = ds->priv;
307 	struct alu_struct alu;
308 	int index;
309 
310 	for (index = 0; index < dev->num_statics; index++) {
311 		if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
312 			/* Found one already in static MAC table. */
313 			if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
314 			    alu.fid == mdb->vid)
315 				break;
316 		}
317 	}
318 
319 	/* no available entry */
320 	if (index == dev->num_statics)
321 		goto exit;
322 
323 	/* clear port */
324 	alu.port_forward &= ~BIT(port);
325 	if (!alu.port_forward)
326 		alu.is_static = false;
327 	dev->dev_ops->w_sta_mac_table(dev, index, &alu);
328 
329 exit:
330 	return 0;
331 }
332 EXPORT_SYMBOL_GPL(ksz_port_mdb_del);
333 
334 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
335 {
336 	struct ksz_device *dev = ds->priv;
337 
338 	if (!dsa_is_user_port(ds, port))
339 		return 0;
340 
341 	/* setup slave port */
342 	dev->dev_ops->port_setup(dev, port, false);
343 
344 	/* port_stp_state_set() will be called after to enable the port so
345 	 * there is no need to do anything.
346 	 */
347 
348 	return 0;
349 }
350 EXPORT_SYMBOL_GPL(ksz_enable_port);
351 
352 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
353 {
354 	struct dsa_switch *ds;
355 	struct ksz_device *swdev;
356 
357 	ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
358 	if (!ds)
359 		return NULL;
360 
361 	ds->dev = base;
362 	ds->num_ports = DSA_MAX_PORTS;
363 
364 	swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
365 	if (!swdev)
366 		return NULL;
367 
368 	ds->priv = swdev;
369 	swdev->dev = base;
370 
371 	swdev->ds = ds;
372 	swdev->priv = priv;
373 
374 	return swdev;
375 }
376 EXPORT_SYMBOL(ksz_switch_alloc);
377 
378 int ksz_switch_register(struct ksz_device *dev,
379 			const struct ksz_dev_ops *ops)
380 {
381 	struct device_node *port, *ports;
382 	phy_interface_t interface;
383 	unsigned int port_num;
384 	int ret;
385 
386 	if (dev->pdata)
387 		dev->chip_id = dev->pdata->chip_id;
388 
389 	dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset",
390 						  GPIOD_OUT_LOW);
391 	if (IS_ERR(dev->reset_gpio))
392 		return PTR_ERR(dev->reset_gpio);
393 
394 	if (dev->reset_gpio) {
395 		gpiod_set_value_cansleep(dev->reset_gpio, 1);
396 		usleep_range(10000, 12000);
397 		gpiod_set_value_cansleep(dev->reset_gpio, 0);
398 		msleep(100);
399 	}
400 
401 	mutex_init(&dev->dev_mutex);
402 	mutex_init(&dev->regmap_mutex);
403 	mutex_init(&dev->alu_mutex);
404 	mutex_init(&dev->vlan_mutex);
405 
406 	dev->dev_ops = ops;
407 
408 	if (dev->dev_ops->detect(dev))
409 		return -EINVAL;
410 
411 	ret = dev->dev_ops->init(dev);
412 	if (ret)
413 		return ret;
414 
415 	/* Host port interface will be self detected, or specifically set in
416 	 * device tree.
417 	 */
418 	for (port_num = 0; port_num < dev->port_cnt; ++port_num)
419 		dev->ports[port_num].interface = PHY_INTERFACE_MODE_NA;
420 	if (dev->dev->of_node) {
421 		ret = of_get_phy_mode(dev->dev->of_node, &interface);
422 		if (ret == 0)
423 			dev->compat_interface = interface;
424 		ports = of_get_child_by_name(dev->dev->of_node, "ethernet-ports");
425 		if (!ports)
426 			ports = of_get_child_by_name(dev->dev->of_node, "ports");
427 		if (ports)
428 			for_each_available_child_of_node(ports, port) {
429 				if (of_property_read_u32(port, "reg",
430 							 &port_num))
431 					continue;
432 				if (!(dev->port_mask & BIT(port_num))) {
433 					of_node_put(port);
434 					return -EINVAL;
435 				}
436 				of_get_phy_mode(port,
437 						&dev->ports[port_num].interface);
438 			}
439 		dev->synclko_125 = of_property_read_bool(dev->dev->of_node,
440 							 "microchip,synclko-125");
441 		dev->synclko_disable = of_property_read_bool(dev->dev->of_node,
442 							     "microchip,synclko-disable");
443 		if (dev->synclko_125 && dev->synclko_disable) {
444 			dev_err(dev->dev, "inconsistent synclko settings\n");
445 			return -EINVAL;
446 		}
447 	}
448 
449 	ret = dsa_register_switch(dev->ds);
450 	if (ret) {
451 		dev->dev_ops->exit(dev);
452 		return ret;
453 	}
454 
455 	/* Read MIB counters every 30 seconds to avoid overflow. */
456 	dev->mib_read_interval = msecs_to_jiffies(30000);
457 
458 	/* Start the MIB timer. */
459 	schedule_delayed_work(&dev->mib_read, 0);
460 
461 	return 0;
462 }
463 EXPORT_SYMBOL(ksz_switch_register);
464 
465 void ksz_switch_remove(struct ksz_device *dev)
466 {
467 	/* timer started */
468 	if (dev->mib_read_interval) {
469 		dev->mib_read_interval = 0;
470 		cancel_delayed_work_sync(&dev->mib_read);
471 	}
472 
473 	dev->dev_ops->exit(dev);
474 	dsa_unregister_switch(dev->ds);
475 
476 	if (dev->reset_gpio)
477 		gpiod_set_value_cansleep(dev->reset_gpio, 1);
478 
479 }
480 EXPORT_SYMBOL(ksz_switch_remove);
481 
482 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
483 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
484 MODULE_LICENSE("GPL");
485