xref: /linux/drivers/net/ethernet/mscc/ocelot_net.c (revision 0cc55e694e851921667dc80972a86feb1ca331ef)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch driver
3  *
4  * Copyright (c) 2017, 2019 Microsemi Corporation
5  */
6 
7 #include <linux/if_bridge.h>
8 #include "ocelot.h"
9 #include "ocelot_vcap.h"
10 
11 int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
12 			       struct flow_cls_offload *f,
13 			       bool ingress)
14 {
15 	struct ocelot *ocelot = priv->port.ocelot;
16 	int port = priv->chip_port;
17 
18 	if (!ingress)
19 		return -EOPNOTSUPP;
20 
21 	switch (f->command) {
22 	case FLOW_CLS_REPLACE:
23 		return ocelot_cls_flower_replace(ocelot, port, f, ingress);
24 	case FLOW_CLS_DESTROY:
25 		return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
26 	case FLOW_CLS_STATS:
27 		return ocelot_cls_flower_stats(ocelot, port, f, ingress);
28 	default:
29 		return -EOPNOTSUPP;
30 	}
31 }
32 
33 static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
34 					struct tc_cls_matchall_offload *f,
35 					bool ingress)
36 {
37 	struct netlink_ext_ack *extack = f->common.extack;
38 	struct ocelot *ocelot = priv->port.ocelot;
39 	struct ocelot_policer pol = { 0 };
40 	struct flow_action_entry *action;
41 	int port = priv->chip_port;
42 	int err;
43 
44 	if (!ingress) {
45 		NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
46 		return -EOPNOTSUPP;
47 	}
48 
49 	switch (f->command) {
50 	case TC_CLSMATCHALL_REPLACE:
51 		if (!flow_offload_has_one_action(&f->rule->action)) {
52 			NL_SET_ERR_MSG_MOD(extack,
53 					   "Only one action is supported");
54 			return -EOPNOTSUPP;
55 		}
56 
57 		if (priv->tc.block_shared) {
58 			NL_SET_ERR_MSG_MOD(extack,
59 					   "Rate limit is not supported on shared blocks");
60 			return -EOPNOTSUPP;
61 		}
62 
63 		action = &f->rule->action.entries[0];
64 
65 		if (action->id != FLOW_ACTION_POLICE) {
66 			NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
67 			return -EOPNOTSUPP;
68 		}
69 
70 		if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
71 			NL_SET_ERR_MSG_MOD(extack,
72 					   "Only one policer per port is supported");
73 			return -EEXIST;
74 		}
75 
76 		pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
77 		pol.burst = (u32)div_u64(action->police.rate_bytes_ps *
78 					 PSCHED_NS2TICKS(action->police.burst),
79 					 PSCHED_TICKS_PER_SEC);
80 
81 		err = ocelot_port_policer_add(ocelot, port, &pol);
82 		if (err) {
83 			NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
84 			return err;
85 		}
86 
87 		priv->tc.police_id = f->cookie;
88 		priv->tc.offload_cnt++;
89 		return 0;
90 	case TC_CLSMATCHALL_DESTROY:
91 		if (priv->tc.police_id != f->cookie)
92 			return -ENOENT;
93 
94 		err = ocelot_port_policer_del(ocelot, port);
95 		if (err) {
96 			NL_SET_ERR_MSG_MOD(extack,
97 					   "Could not delete policer");
98 			return err;
99 		}
100 		priv->tc.police_id = 0;
101 		priv->tc.offload_cnt--;
102 		return 0;
103 	case TC_CLSMATCHALL_STATS:
104 	default:
105 		return -EOPNOTSUPP;
106 	}
107 }
108 
109 static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
110 				    void *type_data,
111 				    void *cb_priv, bool ingress)
112 {
113 	struct ocelot_port_private *priv = cb_priv;
114 
115 	if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
116 		return -EOPNOTSUPP;
117 
118 	switch (type) {
119 	case TC_SETUP_CLSMATCHALL:
120 		return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
121 	case TC_SETUP_CLSFLOWER:
122 		return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
123 	default:
124 		return -EOPNOTSUPP;
125 	}
126 }
127 
128 static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
129 				       void *type_data,
130 				       void *cb_priv)
131 {
132 	return ocelot_setup_tc_block_cb(type, type_data,
133 					cb_priv, true);
134 }
135 
136 static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
137 				       void *type_data,
138 				       void *cb_priv)
139 {
140 	return ocelot_setup_tc_block_cb(type, type_data,
141 					cb_priv, false);
142 }
143 
144 static LIST_HEAD(ocelot_block_cb_list);
145 
146 static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
147 				 struct flow_block_offload *f)
148 {
149 	struct flow_block_cb *block_cb;
150 	flow_setup_cb_t *cb;
151 
152 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
153 		cb = ocelot_setup_tc_block_cb_ig;
154 		priv->tc.block_shared = f->block_shared;
155 	} else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
156 		cb = ocelot_setup_tc_block_cb_eg;
157 	} else {
158 		return -EOPNOTSUPP;
159 	}
160 
161 	f->driver_block_list = &ocelot_block_cb_list;
162 
163 	switch (f->command) {
164 	case FLOW_BLOCK_BIND:
165 		if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
166 			return -EBUSY;
167 
168 		block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
169 		if (IS_ERR(block_cb))
170 			return PTR_ERR(block_cb);
171 
172 		flow_block_cb_add(block_cb, f);
173 		list_add_tail(&block_cb->driver_list, f->driver_block_list);
174 		return 0;
175 	case FLOW_BLOCK_UNBIND:
176 		block_cb = flow_block_cb_lookup(f->block, cb, priv);
177 		if (!block_cb)
178 			return -ENOENT;
179 
180 		flow_block_cb_remove(block_cb, f);
181 		list_del(&block_cb->driver_list);
182 		return 0;
183 	default:
184 		return -EOPNOTSUPP;
185 	}
186 }
187 
188 static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
189 			   void *type_data)
190 {
191 	struct ocelot_port_private *priv = netdev_priv(dev);
192 
193 	switch (type) {
194 	case TC_SETUP_BLOCK:
195 		return ocelot_setup_tc_block(priv, type_data);
196 	default:
197 		return -EOPNOTSUPP;
198 	}
199 	return 0;
200 }
201 
202 static void ocelot_port_adjust_link(struct net_device *dev)
203 {
204 	struct ocelot_port_private *priv = netdev_priv(dev);
205 	struct ocelot *ocelot = priv->port.ocelot;
206 	int port = priv->chip_port;
207 
208 	ocelot_adjust_link(ocelot, port, dev->phydev);
209 }
210 
211 static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
212 			       bool untagged)
213 {
214 	struct ocelot_port_private *priv = netdev_priv(dev);
215 	struct ocelot_port *ocelot_port = &priv->port;
216 	struct ocelot *ocelot = ocelot_port->ocelot;
217 	int port = priv->chip_port;
218 	int ret;
219 
220 	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
221 	if (ret)
222 		return ret;
223 
224 	/* Add the port MAC address to with the right VLAN information */
225 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
226 			  ENTRYTYPE_LOCKED);
227 
228 	return 0;
229 }
230 
231 static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
232 {
233 	struct ocelot_port_private *priv = netdev_priv(dev);
234 	struct ocelot *ocelot = priv->port.ocelot;
235 	int port = priv->chip_port;
236 	int ret;
237 
238 	/* 8021q removes VID 0 on module unload for all interfaces
239 	 * with VLAN filtering feature. We need to keep it to receive
240 	 * untagged traffic.
241 	 */
242 	if (vid == 0)
243 		return 0;
244 
245 	ret = ocelot_vlan_del(ocelot, port, vid);
246 	if (ret)
247 		return ret;
248 
249 	/* Del the port MAC address to with the right VLAN information */
250 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
251 
252 	return 0;
253 }
254 
255 static int ocelot_port_open(struct net_device *dev)
256 {
257 	struct ocelot_port_private *priv = netdev_priv(dev);
258 	struct ocelot_port *ocelot_port = &priv->port;
259 	struct ocelot *ocelot = ocelot_port->ocelot;
260 	int port = priv->chip_port;
261 	int err;
262 
263 	if (priv->serdes) {
264 		err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
265 				       ocelot_port->phy_mode);
266 		if (err) {
267 			netdev_err(dev, "Could not set mode of SerDes\n");
268 			return err;
269 		}
270 	}
271 
272 	err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
273 				 ocelot_port->phy_mode);
274 	if (err) {
275 		netdev_err(dev, "Could not attach to PHY\n");
276 		return err;
277 	}
278 
279 	dev->phydev = priv->phy;
280 
281 	phy_attached_info(priv->phy);
282 	phy_start(priv->phy);
283 
284 	ocelot_port_enable(ocelot, port, priv->phy);
285 
286 	return 0;
287 }
288 
289 static int ocelot_port_stop(struct net_device *dev)
290 {
291 	struct ocelot_port_private *priv = netdev_priv(dev);
292 	struct ocelot *ocelot = priv->port.ocelot;
293 	int port = priv->chip_port;
294 
295 	phy_disconnect(priv->phy);
296 
297 	dev->phydev = NULL;
298 
299 	ocelot_port_disable(ocelot, port);
300 
301 	return 0;
302 }
303 
304 /* Generate the IFH for frame injection
305  *
306  * The IFH is a 128bit-value
307  * bit 127: bypass the analyzer processing
308  * bit 56-67: destination mask
309  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
310  * bit 20-27: cpu extraction queue mask
311  * bit 16: tag type 0: C-tag, 1: S-tag
312  * bit 0-11: VID
313  */
314 static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
315 {
316 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
317 	ifh[1] = (0xf00 & info->port) >> 8;
318 	ifh[2] = (0xff & info->port) << 24;
319 	ifh[3] = (info->tag_type << 16) | info->vid;
320 
321 	return 0;
322 }
323 
324 static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
325 {
326 	struct ocelot_port_private *priv = netdev_priv(dev);
327 	struct skb_shared_info *shinfo = skb_shinfo(skb);
328 	struct ocelot_port *ocelot_port = &priv->port;
329 	struct ocelot *ocelot = ocelot_port->ocelot;
330 	u32 val, ifh[OCELOT_TAG_LEN / 4];
331 	struct frame_info info = {};
332 	u8 grp = 0; /* Send everything on CPU group 0 */
333 	unsigned int i, count, last;
334 	int port = priv->chip_port;
335 
336 	val = ocelot_read(ocelot, QS_INJ_STATUS);
337 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
338 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
339 		return NETDEV_TX_BUSY;
340 
341 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
342 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
343 
344 	info.port = BIT(port);
345 	info.tag_type = IFH_TAG_TYPE_C;
346 	info.vid = skb_vlan_tag_get(skb);
347 
348 	/* Check if timestamping is needed */
349 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
350 		info.rew_op = ocelot_port->ptp_cmd;
351 		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
352 			info.rew_op |= (ocelot_port->ts_id  % 4) << 3;
353 	}
354 
355 	ocelot_gen_ifh(ifh, &info);
356 
357 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
358 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
359 				 QS_INJ_WR, grp);
360 
361 	count = (skb->len + 3) / 4;
362 	last = skb->len % 4;
363 	for (i = 0; i < count; i++)
364 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
365 
366 	/* Add padding */
367 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
368 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
369 		i++;
370 	}
371 
372 	/* Indicate EOF and valid bytes in last word */
373 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
374 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
375 			 QS_INJ_CTRL_EOF,
376 			 QS_INJ_CTRL, grp);
377 
378 	/* Add dummy CRC */
379 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
380 	skb_tx_timestamp(skb);
381 
382 	dev->stats.tx_packets++;
383 	dev->stats.tx_bytes += skb->len;
384 
385 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, skb)) {
386 		ocelot_port->ts_id++;
387 		return NETDEV_TX_OK;
388 	}
389 
390 	dev_kfree_skb_any(skb);
391 	return NETDEV_TX_OK;
392 }
393 
394 static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
395 {
396 	struct ocelot_port_private *priv = netdev_priv(dev);
397 	struct ocelot_port *ocelot_port = &priv->port;
398 	struct ocelot *ocelot = ocelot_port->ocelot;
399 
400 	return ocelot_mact_forget(ocelot, addr, ocelot_port->pvid);
401 }
402 
403 static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
404 {
405 	struct ocelot_port_private *priv = netdev_priv(dev);
406 	struct ocelot_port *ocelot_port = &priv->port;
407 	struct ocelot *ocelot = ocelot_port->ocelot;
408 
409 	return ocelot_mact_learn(ocelot, PGID_CPU, addr, ocelot_port->pvid,
410 				 ENTRYTYPE_LOCKED);
411 }
412 
413 static void ocelot_set_rx_mode(struct net_device *dev)
414 {
415 	struct ocelot_port_private *priv = netdev_priv(dev);
416 	struct ocelot *ocelot = priv->port.ocelot;
417 	u32 val;
418 	int i;
419 
420 	/* This doesn't handle promiscuous mode because the bridge core is
421 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
422 	 * forwarded to the CPU port.
423 	 */
424 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
425 	for_each_nonreserved_multicast_dest_pgid(ocelot, i)
426 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
427 
428 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
429 }
430 
431 static int ocelot_port_get_phys_port_name(struct net_device *dev,
432 					  char *buf, size_t len)
433 {
434 	struct ocelot_port_private *priv = netdev_priv(dev);
435 	int port = priv->chip_port;
436 	int ret;
437 
438 	ret = snprintf(buf, len, "p%d", port);
439 	if (ret >= len)
440 		return -EINVAL;
441 
442 	return 0;
443 }
444 
445 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
446 {
447 	struct ocelot_port_private *priv = netdev_priv(dev);
448 	struct ocelot_port *ocelot_port = &priv->port;
449 	struct ocelot *ocelot = ocelot_port->ocelot;
450 	const struct sockaddr *addr = p;
451 
452 	/* Learn the new net device MAC address in the mac table. */
453 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, ocelot_port->pvid,
454 			  ENTRYTYPE_LOCKED);
455 	/* Then forget the previous one. */
456 	ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid);
457 
458 	ether_addr_copy(dev->dev_addr, addr->sa_data);
459 	return 0;
460 }
461 
462 static void ocelot_get_stats64(struct net_device *dev,
463 			       struct rtnl_link_stats64 *stats)
464 {
465 	struct ocelot_port_private *priv = netdev_priv(dev);
466 	struct ocelot *ocelot = priv->port.ocelot;
467 	int port = priv->chip_port;
468 
469 	/* Configure the port to read the stats from */
470 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
471 		     SYS_STAT_CFG);
472 
473 	/* Get Rx stats */
474 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
475 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
476 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
477 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
478 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
479 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
480 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
481 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
482 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
483 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
484 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
485 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
486 	stats->rx_dropped = dev->stats.rx_dropped;
487 
488 	/* Get Tx stats */
489 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
490 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
491 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
492 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
493 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
494 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
495 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
496 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
497 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
498 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
499 }
500 
501 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
502 			       struct net_device *dev,
503 			       const unsigned char *addr,
504 			       u16 vid, u16 flags,
505 			       struct netlink_ext_ack *extack)
506 {
507 	struct ocelot_port_private *priv = netdev_priv(dev);
508 	struct ocelot *ocelot = priv->port.ocelot;
509 	int port = priv->chip_port;
510 
511 	return ocelot_fdb_add(ocelot, port, addr, vid);
512 }
513 
514 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
515 			       struct net_device *dev,
516 			       const unsigned char *addr, u16 vid)
517 {
518 	struct ocelot_port_private *priv = netdev_priv(dev);
519 	struct ocelot *ocelot = priv->port.ocelot;
520 	int port = priv->chip_port;
521 
522 	return ocelot_fdb_del(ocelot, port, addr, vid);
523 }
524 
525 static int ocelot_port_fdb_dump(struct sk_buff *skb,
526 				struct netlink_callback *cb,
527 				struct net_device *dev,
528 				struct net_device *filter_dev, int *idx)
529 {
530 	struct ocelot_port_private *priv = netdev_priv(dev);
531 	struct ocelot *ocelot = priv->port.ocelot;
532 	struct ocelot_dump_ctx dump = {
533 		.dev = dev,
534 		.skb = skb,
535 		.cb = cb,
536 		.idx = *idx,
537 	};
538 	int port = priv->chip_port;
539 	int ret;
540 
541 	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
542 
543 	*idx = dump.idx;
544 
545 	return ret;
546 }
547 
548 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
549 				  u16 vid)
550 {
551 	return ocelot_vlan_vid_add(dev, vid, false, false);
552 }
553 
554 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
555 				   u16 vid)
556 {
557 	return ocelot_vlan_vid_del(dev, vid);
558 }
559 
560 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
561 			     netdev_features_t features)
562 {
563 	u32 val;
564 
565 	/* Filtering */
566 	val = ocelot_read(ocelot, ANA_VLANMASK);
567 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
568 		val |= BIT(port);
569 	else
570 		val &= ~BIT(port);
571 	ocelot_write(ocelot, val, ANA_VLANMASK);
572 }
573 
574 static int ocelot_set_features(struct net_device *dev,
575 			       netdev_features_t features)
576 {
577 	netdev_features_t changed = dev->features ^ features;
578 	struct ocelot_port_private *priv = netdev_priv(dev);
579 	struct ocelot *ocelot = priv->port.ocelot;
580 	int port = priv->chip_port;
581 
582 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
583 	    priv->tc.offload_cnt) {
584 		netdev_err(dev,
585 			   "Cannot disable HW TC offload while offloads active\n");
586 		return -EBUSY;
587 	}
588 
589 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
590 		ocelot_vlan_mode(ocelot, port, features);
591 
592 	return 0;
593 }
594 
595 static int ocelot_get_port_parent_id(struct net_device *dev,
596 				     struct netdev_phys_item_id *ppid)
597 {
598 	struct ocelot_port_private *priv = netdev_priv(dev);
599 	struct ocelot *ocelot = priv->port.ocelot;
600 
601 	ppid->id_len = sizeof(ocelot->base_mac);
602 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
603 
604 	return 0;
605 }
606 
607 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
608 {
609 	struct ocelot_port_private *priv = netdev_priv(dev);
610 	struct ocelot *ocelot = priv->port.ocelot;
611 	int port = priv->chip_port;
612 
613 	/* If the attached PHY device isn't capable of timestamping operations,
614 	 * use our own (when possible).
615 	 */
616 	if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
617 		switch (cmd) {
618 		case SIOCSHWTSTAMP:
619 			return ocelot_hwstamp_set(ocelot, port, ifr);
620 		case SIOCGHWTSTAMP:
621 			return ocelot_hwstamp_get(ocelot, port, ifr);
622 		}
623 	}
624 
625 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
626 }
627 
628 static const struct net_device_ops ocelot_port_netdev_ops = {
629 	.ndo_open			= ocelot_port_open,
630 	.ndo_stop			= ocelot_port_stop,
631 	.ndo_start_xmit			= ocelot_port_xmit,
632 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
633 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
634 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
635 	.ndo_get_stats64		= ocelot_get_stats64,
636 	.ndo_fdb_add			= ocelot_port_fdb_add,
637 	.ndo_fdb_del			= ocelot_port_fdb_del,
638 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
639 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
640 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
641 	.ndo_set_features		= ocelot_set_features,
642 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
643 	.ndo_setup_tc			= ocelot_setup_tc,
644 	.ndo_do_ioctl			= ocelot_ioctl,
645 };
646 
647 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
648 				    u8 *data)
649 {
650 	struct ocelot_port_private *priv = netdev_priv(netdev);
651 	struct ocelot *ocelot = priv->port.ocelot;
652 	int port = priv->chip_port;
653 
654 	ocelot_get_strings(ocelot, port, sset, data);
655 }
656 
657 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
658 					  struct ethtool_stats *stats,
659 					  u64 *data)
660 {
661 	struct ocelot_port_private *priv = netdev_priv(dev);
662 	struct ocelot *ocelot = priv->port.ocelot;
663 	int port = priv->chip_port;
664 
665 	ocelot_get_ethtool_stats(ocelot, port, data);
666 }
667 
668 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
669 {
670 	struct ocelot_port_private *priv = netdev_priv(dev);
671 	struct ocelot *ocelot = priv->port.ocelot;
672 	int port = priv->chip_port;
673 
674 	return ocelot_get_sset_count(ocelot, port, sset);
675 }
676 
677 static int ocelot_port_get_ts_info(struct net_device *dev,
678 				   struct ethtool_ts_info *info)
679 {
680 	struct ocelot_port_private *priv = netdev_priv(dev);
681 	struct ocelot *ocelot = priv->port.ocelot;
682 	int port = priv->chip_port;
683 
684 	if (!ocelot->ptp)
685 		return ethtool_op_get_ts_info(dev, info);
686 
687 	return ocelot_get_ts_info(ocelot, port, info);
688 }
689 
690 static const struct ethtool_ops ocelot_ethtool_ops = {
691 	.get_strings		= ocelot_port_get_strings,
692 	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
693 	.get_sset_count		= ocelot_port_get_sset_count,
694 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
695 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
696 	.get_ts_info		= ocelot_port_get_ts_info,
697 };
698 
699 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
700 					   struct switchdev_trans *trans,
701 					   u8 state)
702 {
703 	if (switchdev_trans_ph_prepare(trans))
704 		return;
705 
706 	ocelot_bridge_stp_state_set(ocelot, port, state);
707 }
708 
709 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
710 					unsigned long ageing_clock_t)
711 {
712 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
713 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
714 
715 	ocelot_set_ageing_time(ocelot, ageing_time);
716 }
717 
718 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
719 {
720 	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
721 			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
722 			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
723 	u32 val = 0;
724 
725 	if (mc)
726 		val = cpu_fwd_mcast;
727 
728 	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
729 		       ANA_PORT_CPU_FWD_CFG, port);
730 }
731 
732 static int ocelot_port_attr_set(struct net_device *dev,
733 				const struct switchdev_attr *attr,
734 				struct switchdev_trans *trans)
735 {
736 	struct ocelot_port_private *priv = netdev_priv(dev);
737 	struct ocelot *ocelot = priv->port.ocelot;
738 	int port = priv->chip_port;
739 	int err = 0;
740 
741 	switch (attr->id) {
742 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
743 		ocelot_port_attr_stp_state_set(ocelot, port, trans,
744 					       attr->u.stp_state);
745 		break;
746 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
747 		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
748 		break;
749 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
750 		ocelot_port_vlan_filtering(ocelot, port,
751 					   attr->u.vlan_filtering);
752 		break;
753 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
754 		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
755 		break;
756 	default:
757 		err = -EOPNOTSUPP;
758 		break;
759 	}
760 
761 	return err;
762 }
763 
764 static int ocelot_port_obj_add_vlan(struct net_device *dev,
765 				    const struct switchdev_obj_port_vlan *vlan,
766 				    struct switchdev_trans *trans)
767 {
768 	int ret;
769 	u16 vid;
770 
771 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
772 		ret = ocelot_vlan_vid_add(dev, vid,
773 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
774 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
775 		if (ret)
776 			return ret;
777 	}
778 
779 	return 0;
780 }
781 
782 static int ocelot_port_vlan_del_vlan(struct net_device *dev,
783 				     const struct switchdev_obj_port_vlan *vlan)
784 {
785 	int ret;
786 	u16 vid;
787 
788 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
789 		ret = ocelot_vlan_vid_del(dev, vid);
790 
791 		if (ret)
792 			return ret;
793 	}
794 
795 	return 0;
796 }
797 
798 static int ocelot_port_obj_add_mdb(struct net_device *dev,
799 				   const struct switchdev_obj_port_mdb *mdb,
800 				   struct switchdev_trans *trans)
801 {
802 	struct ocelot_port_private *priv = netdev_priv(dev);
803 	struct ocelot_port *ocelot_port = &priv->port;
804 	struct ocelot *ocelot = ocelot_port->ocelot;
805 	int port = priv->chip_port;
806 
807 	if (switchdev_trans_ph_prepare(trans))
808 		return 0;
809 
810 	return ocelot_port_mdb_add(ocelot, port, mdb);
811 }
812 
813 static int ocelot_port_obj_del_mdb(struct net_device *dev,
814 				   const struct switchdev_obj_port_mdb *mdb)
815 {
816 	struct ocelot_port_private *priv = netdev_priv(dev);
817 	struct ocelot_port *ocelot_port = &priv->port;
818 	struct ocelot *ocelot = ocelot_port->ocelot;
819 	int port = priv->chip_port;
820 
821 	return ocelot_port_mdb_del(ocelot, port, mdb);
822 }
823 
824 static int ocelot_port_obj_add(struct net_device *dev,
825 			       const struct switchdev_obj *obj,
826 			       struct switchdev_trans *trans,
827 			       struct netlink_ext_ack *extack)
828 {
829 	int ret = 0;
830 
831 	switch (obj->id) {
832 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
833 		ret = ocelot_port_obj_add_vlan(dev,
834 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
835 					       trans);
836 		break;
837 	case SWITCHDEV_OBJ_ID_PORT_MDB:
838 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
839 					      trans);
840 		break;
841 	default:
842 		return -EOPNOTSUPP;
843 	}
844 
845 	return ret;
846 }
847 
848 static int ocelot_port_obj_del(struct net_device *dev,
849 			       const struct switchdev_obj *obj)
850 {
851 	int ret = 0;
852 
853 	switch (obj->id) {
854 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
855 		ret = ocelot_port_vlan_del_vlan(dev,
856 						SWITCHDEV_OBJ_PORT_VLAN(obj));
857 		break;
858 	case SWITCHDEV_OBJ_ID_PORT_MDB:
859 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
860 		break;
861 	default:
862 		return -EOPNOTSUPP;
863 	}
864 
865 	return ret;
866 }
867 
868 /* Checks if the net_device instance given to us originate from our driver. */
869 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
870 {
871 	return dev->netdev_ops == &ocelot_port_netdev_ops;
872 }
873 
874 static int ocelot_netdevice_port_event(struct net_device *dev,
875 				       unsigned long event,
876 				       struct netdev_notifier_changeupper_info *info)
877 {
878 	struct ocelot_port_private *priv = netdev_priv(dev);
879 	struct ocelot_port *ocelot_port = &priv->port;
880 	struct ocelot *ocelot = ocelot_port->ocelot;
881 	int port = priv->chip_port;
882 	int err = 0;
883 
884 	switch (event) {
885 	case NETDEV_CHANGEUPPER:
886 		if (netif_is_bridge_master(info->upper_dev)) {
887 			if (info->linking) {
888 				err = ocelot_port_bridge_join(ocelot, port,
889 							      info->upper_dev);
890 			} else {
891 				err = ocelot_port_bridge_leave(ocelot, port,
892 							       info->upper_dev);
893 			}
894 		}
895 		if (netif_is_lag_master(info->upper_dev)) {
896 			if (info->linking)
897 				err = ocelot_port_lag_join(ocelot, port,
898 							   info->upper_dev);
899 			else
900 				ocelot_port_lag_leave(ocelot, port,
901 						      info->upper_dev);
902 		}
903 		break;
904 	default:
905 		break;
906 	}
907 
908 	return err;
909 }
910 
911 static int ocelot_netdevice_event(struct notifier_block *unused,
912 				  unsigned long event, void *ptr)
913 {
914 	struct netdev_notifier_changeupper_info *info = ptr;
915 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
916 	int ret = 0;
917 
918 	if (!ocelot_netdevice_dev_check(dev))
919 		return 0;
920 
921 	if (event == NETDEV_PRECHANGEUPPER &&
922 	    netif_is_lag_master(info->upper_dev)) {
923 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
924 		struct netlink_ext_ack *extack;
925 
926 		if (lag_upper_info &&
927 		    lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
928 			extack = netdev_notifier_info_to_extack(&info->info);
929 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
930 
931 			ret = -EINVAL;
932 			goto notify;
933 		}
934 	}
935 
936 	if (netif_is_lag_master(dev)) {
937 		struct net_device *slave;
938 		struct list_head *iter;
939 
940 		netdev_for_each_lower_dev(dev, slave, iter) {
941 			ret = ocelot_netdevice_port_event(slave, event, info);
942 			if (ret)
943 				goto notify;
944 		}
945 	} else {
946 		ret = ocelot_netdevice_port_event(dev, event, info);
947 	}
948 
949 notify:
950 	return notifier_from_errno(ret);
951 }
952 
953 struct notifier_block ocelot_netdevice_nb __read_mostly = {
954 	.notifier_call = ocelot_netdevice_event,
955 };
956 EXPORT_SYMBOL(ocelot_netdevice_nb);
957 
958 static int ocelot_switchdev_event(struct notifier_block *unused,
959 				  unsigned long event, void *ptr)
960 {
961 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
962 	int err;
963 
964 	switch (event) {
965 	case SWITCHDEV_PORT_ATTR_SET:
966 		err = switchdev_handle_port_attr_set(dev, ptr,
967 						     ocelot_netdevice_dev_check,
968 						     ocelot_port_attr_set);
969 		return notifier_from_errno(err);
970 	}
971 
972 	return NOTIFY_DONE;
973 }
974 
975 struct notifier_block ocelot_switchdev_nb __read_mostly = {
976 	.notifier_call = ocelot_switchdev_event,
977 };
978 EXPORT_SYMBOL(ocelot_switchdev_nb);
979 
980 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
981 					   unsigned long event, void *ptr)
982 {
983 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
984 	int err;
985 
986 	switch (event) {
987 		/* Blocking events. */
988 	case SWITCHDEV_PORT_OBJ_ADD:
989 		err = switchdev_handle_port_obj_add(dev, ptr,
990 						    ocelot_netdevice_dev_check,
991 						    ocelot_port_obj_add);
992 		return notifier_from_errno(err);
993 	case SWITCHDEV_PORT_OBJ_DEL:
994 		err = switchdev_handle_port_obj_del(dev, ptr,
995 						    ocelot_netdevice_dev_check,
996 						    ocelot_port_obj_del);
997 		return notifier_from_errno(err);
998 	case SWITCHDEV_PORT_ATTR_SET:
999 		err = switchdev_handle_port_attr_set(dev, ptr,
1000 						     ocelot_netdevice_dev_check,
1001 						     ocelot_port_attr_set);
1002 		return notifier_from_errno(err);
1003 	}
1004 
1005 	return NOTIFY_DONE;
1006 }
1007 
1008 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1009 	.notifier_call = ocelot_switchdev_blocking_event,
1010 };
1011 EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1012 
1013 int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1014 		      void __iomem *regs,
1015 		      struct phy_device *phy)
1016 {
1017 	struct ocelot_port_private *priv;
1018 	struct ocelot_port *ocelot_port;
1019 	struct net_device *dev;
1020 	int err;
1021 
1022 	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1023 	if (!dev)
1024 		return -ENOMEM;
1025 	SET_NETDEV_DEV(dev, ocelot->dev);
1026 	priv = netdev_priv(dev);
1027 	priv->dev = dev;
1028 	priv->phy = phy;
1029 	priv->chip_port = port;
1030 	ocelot_port = &priv->port;
1031 	ocelot_port->ocelot = ocelot;
1032 	ocelot_port->regs = regs;
1033 	ocelot->ports[port] = ocelot_port;
1034 
1035 	dev->netdev_ops = &ocelot_port_netdev_ops;
1036 	dev->ethtool_ops = &ocelot_ethtool_ops;
1037 
1038 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1039 		NETIF_F_HW_TC;
1040 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1041 
1042 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1043 	dev->dev_addr[ETH_ALEN - 1] += port;
1044 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
1045 			  ENTRYTYPE_LOCKED);
1046 
1047 	ocelot_init_port(ocelot, port);
1048 
1049 	err = register_netdev(dev);
1050 	if (err) {
1051 		dev_err(ocelot->dev, "register_netdev failed\n");
1052 		free_netdev(dev);
1053 	}
1054 
1055 	return err;
1056 }
1057 EXPORT_SYMBOL(ocelot_probe_port);
1058