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