aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/microchip/lan966x/lan966x_mdb.c
blob: 2af55268bf4dcdf073584bbb142c0cfd2a5e0d1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// SPDX-License-Identifier: GPL-2.0+

#include <net/switchdev.h>

#include "lan966x_main.h"

struct lan966x_pgid_entry {
	struct list_head list;
	int index;
	refcount_t refcount;
	u16 ports;
};

struct lan966x_mdb_entry {
	struct list_head list;
	unsigned char mac[ETH_ALEN];
	u16 vid;
	u16 ports;
	struct lan966x_pgid_entry *pgid;
	u8 cpu_copy;
};

void lan966x_mdb_init(struct lan966x *lan966x)
{
	INIT_LIST_HEAD(&lan966x->mdb_entries);
	INIT_LIST_HEAD(&lan966x->pgid_entries);
}

static void lan966x_mdb_purge_mdb_entries(struct lan966x *lan966x)
{
	struct lan966x_mdb_entry *mdb_entry, *tmp;

	list_for_each_entry_safe(mdb_entry, tmp, &lan966x->mdb_entries, list) {
		list_del(&mdb_entry->list);
		kfree(mdb_entry);
	}
}

static void lan966x_mdb_purge_pgid_entries(struct lan966x *lan966x)
{
	struct lan966x_pgid_entry *pgid_entry, *tmp;

	list_for_each_entry_safe(pgid_entry, tmp, &lan966x->pgid_entries, list) {
		list_del(&pgid_entry->list);
		kfree(pgid_entry);
	}
}

void lan966x_mdb_deinit(struct lan966x *lan966x)
{
	lan966x_mdb_purge_mdb_entries(lan966x);
	lan966x_mdb_purge_pgid_entries(lan966x);
}

static struct lan966x_mdb_entry *
lan966x_mdb_entry_get(struct lan966x *lan966x,
		      const unsigned char *mac,
		      u16 vid)
{
	struct lan966x_mdb_entry *mdb_entry;

	list_for_each_entry(mdb_entry, &lan966x->mdb_entries, list) {
		if (ether_addr_equal(mdb_entry->mac, mac) &&
		    mdb_entry->vid == vid)
			return mdb_entry;
	}

	return NULL;
}

static struct lan966x_mdb_entry *
lan966x_mdb_entry_add(struct lan966x *lan966x,
		      const struct switchdev_obj_port_mdb *mdb)
{
	struct lan966x_mdb_entry *mdb_entry;

	mdb_entry = kzalloc(sizeof(*mdb_entry), GFP_KERNEL);
	if (!mdb_entry)
		return ERR_PTR(-ENOMEM);

	ether_addr_copy(mdb_entry->mac, mdb->addr);
	mdb_entry->vid = mdb->vid;

	list_add_tail(&mdb_entry->list, &lan966x->mdb_entries);

	return mdb_entry;
}

static void lan966x_mdb_encode_mac(unsigned char *mac,
				   struct lan966x_mdb_entry *mdb_entry,
				   enum macaccess_entry_type type)
{
	ether_addr_copy(mac, mdb_entry->mac);

	if (type == ENTRYTYPE_MACV4) {
		mac[0] = 0;
		mac[1] = mdb_entry->ports >> 8;
		mac[2] = mdb_entry->ports & 0xff;
	} else if (type == ENTRYTYPE_MACV6) {
		mac[0] = mdb_entry->ports >> 8;
		mac[1] = mdb_entry->ports & 0xff;
	}
}

static int lan966x_mdb_ip_add(struct lan966x_port *port,
			      const struct switchdev_obj_port_mdb *mdb,
			      enum macaccess_entry_type type)
{
	bool cpu_port = netif_is_bridge_master(mdb->obj.orig_dev);
	struct lan966x *lan966x = port->lan966x;
	struct lan966x_mdb_entry *mdb_entry;
	unsigned char mac[ETH_ALEN];
	bool cpu_copy = false;

	mdb_entry = lan966x_mdb_entry_get(lan966x, mdb->addr, mdb->vid);
	if (!mdb_entry) {
		mdb_entry = lan966x_mdb_entry_add(lan966x, mdb);
		if (IS_ERR(mdb_entry))
			return PTR_ERR(mdb_entry);
	} else {
		lan966x_mdb_encode_mac(mac, mdb_entry, type);
		lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);
	}

	if (cpu_port)
		mdb_entry->cpu_copy++;
	else
		mdb_entry->ports |= BIT(port->chip_port);

	/* Copy the frame to CPU only if the CPU is in the VLAN */
	if (lan966x_vlan_cpu_member_cpu_vlan_mask(lan966x, mdb_entry->vid) &&
	    mdb_entry->cpu_copy)
		cpu_copy = true;

	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	return lan966x_mac_ip_learn(lan966x, cpu_copy,
				    mac, mdb_entry->vid, type);
}

static int lan966x_mdb_ip_del(struct lan966x_port *port,
			      const struct switchdev_obj_port_mdb *mdb,
			      enum macaccess_entry_type type)
{
	bool cpu_port = netif_is_bridge_master(mdb->obj.orig_dev);
	struct lan966x *lan966x = port->lan966x;
	struct lan966x_mdb_entry *mdb_entry;
	unsigned char mac[ETH_ALEN];
	u16 ports;

	mdb_entry = lan966x_mdb_entry_get(lan966x, mdb->addr, mdb->vid);
	if (!mdb_entry)
		return -ENOENT;

	ports = mdb_entry->ports;
	if (cpu_port) {
		/* If there are still other references to the CPU port then
		 * there is no point to delete and add again the same entry
		 */
		mdb_entry->cpu_copy--;
		if (mdb_entry->cpu_copy)
			return 0;
	} else {
		ports &= ~BIT(port->chip_port);
	}

	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);

	mdb_entry->ports = ports;

	if (!mdb_entry->ports && !mdb_entry->cpu_copy) {
		list_del(&mdb_entry->list);
		kfree(mdb_entry);
		return 0;
	}

	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	return lan966x_mac_ip_learn(lan966x, mdb_entry->cpu_copy,
				    mac, mdb_entry->vid, type);
}

static struct lan966x_pgid_entry *
lan966x_pgid_entry_add(struct lan966x *lan966x, int index, u16 ports)
{
	struct lan966x_pgid_entry *pgid_entry;

	pgid_entry = kzalloc(sizeof(*pgid_entry), GFP_KERNEL);
	if (!pgid_entry)
		return ERR_PTR(-ENOMEM);

	pgid_entry->ports = ports;
	pgid_entry->index = index;
	refcount_set(&pgid_entry->refcount, 1);

	list_add_tail(&pgid_entry->list, &lan966x->pgid_entries);

	return pgid_entry;
}

static struct lan966x_pgid_entry *
lan966x_pgid_entry_get(struct lan966x *lan966x,
		       struct lan966x_mdb_entry *mdb_entry)
{
	struct lan966x_pgid_entry *pgid_entry;
	int index;

	/* Try to find an existing pgid that uses the same ports as the
	 * mdb_entry
	 */
	list_for_each_entry(pgid_entry, &lan966x->pgid_entries, list) {
		if (pgid_entry->ports == mdb_entry->ports) {
			refcount_inc(&pgid_entry->refcount);
			return pgid_entry;
		}
	}

	/* Try to find an empty pgid entry and allocate one in case it finds it,
	 * otherwise it means that there are no more resources
	 */
	for (index = PGID_GP_START; index < PGID_GP_END; index++) {
		bool used = false;

		list_for_each_entry(pgid_entry, &lan966x->pgid_entries, list) {
			if (pgid_entry->index == index) {
				used = true;
				break;
			}
		}

		if (!used)
			return lan966x_pgid_entry_add(lan966x, index,
						      mdb_entry->ports);
	}

	return ERR_PTR(-ENOSPC);
}

static void lan966x_pgid_entry_del(struct lan966x *lan966x,
				   struct lan966x_pgid_entry *pgid_entry)
{
	if (!refcount_dec_and_test(&pgid_entry->refcount))
		return;

	list_del(&pgid_entry->list);
	kfree(pgid_entry);
}

static int lan966x_mdb_l2_add(struct lan966x_port *port,
			      const struct switchdev_obj_port_mdb *mdb,
			      enum macaccess_entry_type type)
{
	bool cpu_port = netif_is_bridge_master(mdb->obj.orig_dev);
	struct lan966x *lan966x = port->lan966x;
	struct lan966x_pgid_entry *pgid_entry;
	struct lan966x_mdb_entry *mdb_entry;
	unsigned char mac[ETH_ALEN];

	mdb_entry = lan966x_mdb_entry_get(lan966x, mdb->addr, mdb->vid);
	if (!mdb_entry) {
		mdb_entry = lan966x_mdb_entry_add(lan966x, mdb);
		if (IS_ERR(mdb_entry))
			return PTR_ERR(mdb_entry);
	} else {
		lan966x_pgid_entry_del(lan966x, mdb_entry->pgid);
		lan966x_mdb_encode_mac(mac, mdb_entry, type);
		lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);
	}

	if (cpu_port) {
		mdb_entry->ports |= BIT(CPU_PORT);
		mdb_entry->cpu_copy++;
	} else {
		mdb_entry->ports |= BIT(port->chip_port);
	}

	pgid_entry = lan966x_pgid_entry_get(lan966x, mdb_entry);
	if (IS_ERR(pgid_entry)) {
		list_del(&mdb_entry->list);
		kfree(mdb_entry);
		return PTR_ERR(pgid_entry);
	}
	mdb_entry->pgid = pgid_entry;

	/* Copy the frame to CPU only if the CPU is in the VLAN */
	if (!lan966x_vlan_cpu_member_cpu_vlan_mask(lan966x, mdb_entry->vid) &&
	    mdb_entry->cpu_copy)
		mdb_entry->ports &= BIT(CPU_PORT);

	lan_rmw(ANA_PGID_PGID_SET(mdb_entry->ports),
		ANA_PGID_PGID,
		lan966x, ANA_PGID(pgid_entry->index));

	return lan966x_mac_learn(lan966x, pgid_entry->index, mdb_entry->mac,
				 mdb_entry->vid, type);
}

static int lan966x_mdb_l2_del(struct lan966x_port *port,
			      const struct switchdev_obj_port_mdb *mdb,
			      enum macaccess_entry_type type)
{
	bool cpu_port = netif_is_bridge_master(mdb->obj.orig_dev);
	struct lan966x *lan966x = port->lan966x;
	struct lan966x_pgid_entry *pgid_entry;
	struct lan966x_mdb_entry *mdb_entry;
	unsigned char mac[ETH_ALEN];
	u16 ports;

	mdb_entry = lan966x_mdb_entry_get(lan966x, mdb->addr, mdb->vid);
	if (!mdb_entry)
		return -ENOENT;

	ports = mdb_entry->ports;
	if (cpu_port) {
		/* If there are still other references to the CPU port then
		 * there is no point to delete and add again the same entry
		 */
		mdb_entry->cpu_copy--;
		if (mdb_entry->cpu_copy)
			return 0;

		ports &= ~BIT(CPU_PORT);
	} else {
		ports &= ~BIT(port->chip_port);
	}

	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);
	lan966x_pgid_entry_del(lan966x, mdb_entry->pgid);

	mdb_entry->ports = ports;

	if (!mdb_entry->ports) {
		list_del(&mdb_entry->list);
		kfree(mdb_entry);
		return 0;
	}

	pgid_entry = lan966x_pgid_entry_get(lan966x, mdb_entry);
	if (IS_ERR(pgid_entry)) {
		list_del(&mdb_entry->list);
		kfree(mdb_entry);
		return PTR_ERR(pgid_entry);
	}
	mdb_entry->pgid = pgid_entry;

	lan_rmw(ANA_PGID_PGID_SET(mdb_entry->ports),
		ANA_PGID_PGID,
		lan966x, ANA_PGID(pgid_entry->index));

	return lan966x_mac_learn(lan966x, pgid_entry->index, mdb_entry->mac,
				 mdb_entry->vid, type);
}

static enum macaccess_entry_type
lan966x_mdb_classify(const unsigned char *mac)
{
	if (mac[0] == 0x01 && mac[1] == 0x00 && mac[2] == 0x5e)
		return ENTRYTYPE_MACV4;
	if (mac[0] == 0x33 && mac[1] == 0x33)
		return ENTRYTYPE_MACV6;
	return ENTRYTYPE_LOCKED;
}

int lan966x_handle_port_mdb_add(struct lan966x_port *port,
				const struct switchdev_obj *obj)
{
	const struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
	enum macaccess_entry_type type;

	/* Split the way the entries are added for ipv4/ipv6 and for l2. The
	 * reason is that for ipv4/ipv6 it doesn't require to use any pgid
	 * entry, while for l2 is required to use pgid entries
	 */
	type = lan966x_mdb_classify(mdb->addr);
	if (type == ENTRYTYPE_MACV4 || type == ENTRYTYPE_MACV6)
		return lan966x_mdb_ip_add(port, mdb, type);

	return lan966x_mdb_l2_add(port, mdb, type);
}

int lan966x_handle_port_mdb_del(struct lan966x_port *port,
				const struct switchdev_obj *obj)
{
	const struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
	enum macaccess_entry_type type;

	/* Split the way the entries are removed for ipv4/ipv6 and for l2. The
	 * reason is that for ipv4/ipv6 it doesn't require to use any pgid
	 * entry, while for l2 is required to use pgid entries
	 */
	type = lan966x_mdb_classify(mdb->addr);
	if (type == ENTRYTYPE_MACV4 || type == ENTRYTYPE_MACV6)
		return lan966x_mdb_ip_del(port, mdb, type);

	return lan966x_mdb_l2_del(port, mdb, type);
}

static void lan966x_mdb_ip_cpu_copy(struct lan966x *lan966x,
				    struct lan966x_mdb_entry *mdb_entry,
				    enum macaccess_entry_type type)
{
	unsigned char mac[ETH_ALEN];

	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);
	lan966x_mac_ip_learn(lan966x, true, mac, mdb_entry->vid, type);
}

static void lan966x_mdb_l2_cpu_copy(struct lan966x *lan966x,
				    struct lan966x_mdb_entry *mdb_entry,
				    enum macaccess_entry_type type)
{
	struct lan966x_pgid_entry *pgid_entry;
	unsigned char mac[ETH_ALEN];

	lan966x_pgid_entry_del(lan966x, mdb_entry->pgid);
	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);

	mdb_entry->ports |= BIT(CPU_PORT);

	pgid_entry = lan966x_pgid_entry_get(lan966x, mdb_entry);
	if (IS_ERR(pgid_entry))
		return;

	mdb_entry->pgid = pgid_entry;

	lan_rmw(ANA_PGID_PGID_SET(mdb_entry->ports),
		ANA_PGID_PGID,
		lan966x, ANA_PGID(pgid_entry->index));

	lan966x_mac_learn(lan966x, pgid_entry->index, mdb_entry->mac,
			  mdb_entry->vid, type);
}

void lan966x_mdb_write_entries(struct lan966x *lan966x, u16 vid)
{
	struct lan966x_mdb_entry *mdb_entry;
	enum macaccess_entry_type type;

	list_for_each_entry(mdb_entry, &lan966x->mdb_entries, list) {
		if (mdb_entry->vid != vid || !mdb_entry->cpu_copy)
			continue;

		type = lan966x_mdb_classify(mdb_entry->mac);
		if (type == ENTRYTYPE_MACV4 || type == ENTRYTYPE_MACV6)
			lan966x_mdb_ip_cpu_copy(lan966x, mdb_entry, type);
		else
			lan966x_mdb_l2_cpu_copy(lan966x, mdb_entry, type);
	}
}

static void lan966x_mdb_ip_cpu_remove(struct lan966x *lan966x,
				      struct lan966x_mdb_entry *mdb_entry,
				      enum macaccess_entry_type type)
{
	unsigned char mac[ETH_ALEN];

	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);
	lan966x_mac_ip_learn(lan966x, false, mac, mdb_entry->vid, type);
}

static void lan966x_mdb_l2_cpu_remove(struct lan966x *lan966x,
				      struct lan966x_mdb_entry *mdb_entry,
				      enum macaccess_entry_type type)
{
	struct lan966x_pgid_entry *pgid_entry;
	unsigned char mac[ETH_ALEN];

	lan966x_pgid_entry_del(lan966x, mdb_entry->pgid);
	lan966x_mdb_encode_mac(mac, mdb_entry, type);
	lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);

	mdb_entry->ports &= ~BIT(CPU_PORT);

	pgid_entry = lan966x_pgid_entry_get(lan966x, mdb_entry);
	if (IS_ERR(pgid_entry))
		return;

	mdb_entry->pgid = pgid_entry;

	lan_rmw(ANA_PGID_PGID_SET(mdb_entry->ports),
		ANA_PGID_PGID,
		lan966x, ANA_PGID(pgid_entry->index));

	lan966x_mac_learn(lan966x, pgid_entry->index, mdb_entry->mac,
			  mdb_entry->vid, type);
}

void lan966x_mdb_erase_entries(struct lan966x *lan966x, u16 vid)
{
	struct lan966x_mdb_entry *mdb_entry;
	enum macaccess_entry_type type;

	list_for_each_entry(mdb_entry, &lan966x->mdb_entries, list) {
		if (mdb_entry->vid != vid || !mdb_entry->cpu_copy)
			continue;

		type = lan966x_mdb_classify(mdb_entry->mac);
		if (type == ENTRYTYPE_MACV4 || type == ENTRYTYPE_MACV6)
			lan966x_mdb_ip_cpu_remove(lan966x, mdb_entry, type);
		else
			lan966x_mdb_l2_cpu_remove(lan966x, mdb_entry, type);
	}
}

void lan966x_mdb_clear_entries(struct lan966x *lan966x)
{
	struct lan966x_mdb_entry *mdb_entry;
	enum macaccess_entry_type type;
	unsigned char mac[ETH_ALEN];

	list_for_each_entry(mdb_entry, &lan966x->mdb_entries, list) {
		type = lan966x_mdb_classify(mdb_entry->mac);

		lan966x_mdb_encode_mac(mac, mdb_entry, type);
		/* Remove just the MAC entry, still keep the PGID in case of L2
		 * entries because this can be restored at later point
		 */
		lan966x_mac_forget(lan966x, mac, mdb_entry->vid, type);
	}
}

void lan966x_mdb_restore_entries(struct lan966x *lan966x)
{
	struct lan966x_mdb_entry *mdb_entry;
	enum macaccess_entry_type type;
	unsigned char mac[ETH_ALEN];
	bool cpu_copy = false;

	list_for_each_entry(mdb_entry, &lan966x->mdb_entries, list) {
		type = lan966x_mdb_classify(mdb_entry->mac);

		lan966x_mdb_encode_mac(mac, mdb_entry, type);
		if (type == ENTRYTYPE_MACV4 || type == ENTRYTYPE_MACV6) {
			/* Copy the frame to CPU only if the CPU is in the VLAN */
			if (lan966x_vlan_cpu_member_cpu_vlan_mask(lan966x,
								  mdb_entry->vid) &&
			    mdb_entry->cpu_copy)
				cpu_copy = true;

			lan966x_mac_ip_learn(lan966x, cpu_copy, mac,
					     mdb_entry->vid, type);
		} else {
			lan966x_mac_learn(lan966x, mdb_entry->pgid->index,
					  mdb_entry->mac,
					  mdb_entry->vid, type);
		}
	}
}