0f064889ea0840aaa92fd13c7aa98e70aba9a29a
[cascardo/linux.git] / drivers / net / dsa / mv88e6xxx.c
1 /*
2  * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3  * Copyright (c) 2008 Marvell Semiconductor
4  *
5  * Copyright (c) 2015 CMC Electronics, Inc.
6  *      Added support for VLAN Table Unit operations
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/delay.h>
15 #include <linux/etherdevice.h>
16 #include <linux/ethtool.h>
17 #include <linux/if_bridge.h>
18 #include <linux/jiffies.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/phy.h>
24 #include <net/dsa.h>
25 #include <net/switchdev.h>
26 #include "mv88e6xxx.h"
27
28 static void assert_smi_lock(struct dsa_switch *ds)
29 {
30         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
31
32         if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
33                 dev_err(ds->master_dev, "SMI lock not held!\n");
34                 dump_stack();
35         }
36 }
37
38 /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
39  * use all 32 SMI bus addresses on its SMI bus, and all switch registers
40  * will be directly accessible on some {device address,register address}
41  * pair.  If the ADDR[4:0] pins are not strapped to zero, the switch
42  * will only respond to SMI transactions to that specific address, and
43  * an indirect addressing mechanism needs to be used to access its
44  * registers.
45  */
46 static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
47 {
48         int ret;
49         int i;
50
51         for (i = 0; i < 16; i++) {
52                 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
53                 if (ret < 0)
54                         return ret;
55
56                 if ((ret & SMI_CMD_BUSY) == 0)
57                         return 0;
58         }
59
60         return -ETIMEDOUT;
61 }
62
63 static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
64                                 int reg)
65 {
66         int ret;
67
68         if (sw_addr == 0)
69                 return mdiobus_read_nested(bus, addr, reg);
70
71         /* Wait for the bus to become free. */
72         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
73         if (ret < 0)
74                 return ret;
75
76         /* Transmit the read command. */
77         ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
78                                    SMI_CMD_OP_22_READ | (addr << 5) | reg);
79         if (ret < 0)
80                 return ret;
81
82         /* Wait for the read command to complete. */
83         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
84         if (ret < 0)
85                 return ret;
86
87         /* Read the data. */
88         ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
89         if (ret < 0)
90                 return ret;
91
92         return ret & 0xffff;
93 }
94
95 static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
96 {
97         struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
98         int ret;
99
100         assert_smi_lock(ds);
101
102         if (bus == NULL)
103                 return -EINVAL;
104
105         ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
106         if (ret < 0)
107                 return ret;
108
109         dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
110                 addr, reg, ret);
111
112         return ret;
113 }
114
115 int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
116 {
117         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
118         int ret;
119
120         mutex_lock(&ps->smi_mutex);
121         ret = _mv88e6xxx_reg_read(ds, addr, reg);
122         mutex_unlock(&ps->smi_mutex);
123
124         return ret;
125 }
126
127 static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
128                                  int reg, u16 val)
129 {
130         int ret;
131
132         if (sw_addr == 0)
133                 return mdiobus_write_nested(bus, addr, reg, val);
134
135         /* Wait for the bus to become free. */
136         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
137         if (ret < 0)
138                 return ret;
139
140         /* Transmit the data to write. */
141         ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
142         if (ret < 0)
143                 return ret;
144
145         /* Transmit the write command. */
146         ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
147                                    SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
148         if (ret < 0)
149                 return ret;
150
151         /* Wait for the write command to complete. */
152         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
153         if (ret < 0)
154                 return ret;
155
156         return 0;
157 }
158
159 static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
160                                 u16 val)
161 {
162         struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
163
164         assert_smi_lock(ds);
165
166         if (bus == NULL)
167                 return -EINVAL;
168
169         dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
170                 addr, reg, val);
171
172         return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
173 }
174
175 int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
176 {
177         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
178         int ret;
179
180         mutex_lock(&ps->smi_mutex);
181         ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
182         mutex_unlock(&ps->smi_mutex);
183
184         return ret;
185 }
186
187 int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
188 {
189         REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
190         REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
191         REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
192
193         return 0;
194 }
195
196 int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
197 {
198         int i;
199         int ret;
200
201         for (i = 0; i < 6; i++) {
202                 int j;
203
204                 /* Write the MAC address byte. */
205                 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
206                           GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
207
208                 /* Wait for the write to complete. */
209                 for (j = 0; j < 16; j++) {
210                         ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
211                         if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
212                                 break;
213                 }
214                 if (j == 16)
215                         return -ETIMEDOUT;
216         }
217
218         return 0;
219 }
220
221 static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
222 {
223         if (addr >= 0)
224                 return _mv88e6xxx_reg_read(ds, addr, regnum);
225         return 0xffff;
226 }
227
228 static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
229                                 u16 val)
230 {
231         if (addr >= 0)
232                 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
233         return 0;
234 }
235
236 #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
237 static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
238 {
239         int ret;
240         unsigned long timeout;
241
242         ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
243         REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
244                   ret & ~GLOBAL_CONTROL_PPU_ENABLE);
245
246         timeout = jiffies + 1 * HZ;
247         while (time_before(jiffies, timeout)) {
248                 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
249                 usleep_range(1000, 2000);
250                 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
251                     GLOBAL_STATUS_PPU_POLLING)
252                         return 0;
253         }
254
255         return -ETIMEDOUT;
256 }
257
258 static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
259 {
260         int ret;
261         unsigned long timeout;
262
263         ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
264         REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
265
266         timeout = jiffies + 1 * HZ;
267         while (time_before(jiffies, timeout)) {
268                 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
269                 usleep_range(1000, 2000);
270                 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
271                     GLOBAL_STATUS_PPU_POLLING)
272                         return 0;
273         }
274
275         return -ETIMEDOUT;
276 }
277
278 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
279 {
280         struct mv88e6xxx_priv_state *ps;
281
282         ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
283         if (mutex_trylock(&ps->ppu_mutex)) {
284                 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
285
286                 if (mv88e6xxx_ppu_enable(ds) == 0)
287                         ps->ppu_disabled = 0;
288                 mutex_unlock(&ps->ppu_mutex);
289         }
290 }
291
292 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
293 {
294         struct mv88e6xxx_priv_state *ps = (void *)_ps;
295
296         schedule_work(&ps->ppu_work);
297 }
298
299 static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
300 {
301         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
302         int ret;
303
304         mutex_lock(&ps->ppu_mutex);
305
306         /* If the PHY polling unit is enabled, disable it so that
307          * we can access the PHY registers.  If it was already
308          * disabled, cancel the timer that is going to re-enable
309          * it.
310          */
311         if (!ps->ppu_disabled) {
312                 ret = mv88e6xxx_ppu_disable(ds);
313                 if (ret < 0) {
314                         mutex_unlock(&ps->ppu_mutex);
315                         return ret;
316                 }
317                 ps->ppu_disabled = 1;
318         } else {
319                 del_timer(&ps->ppu_timer);
320                 ret = 0;
321         }
322
323         return ret;
324 }
325
326 static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
327 {
328         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
329
330         /* Schedule a timer to re-enable the PHY polling unit. */
331         mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
332         mutex_unlock(&ps->ppu_mutex);
333 }
334
335 void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
336 {
337         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
338
339         mutex_init(&ps->ppu_mutex);
340         INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
341         init_timer(&ps->ppu_timer);
342         ps->ppu_timer.data = (unsigned long)ps;
343         ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
344 }
345
346 int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
347 {
348         int ret;
349
350         ret = mv88e6xxx_ppu_access_get(ds);
351         if (ret >= 0) {
352                 ret = mv88e6xxx_reg_read(ds, addr, regnum);
353                 mv88e6xxx_ppu_access_put(ds);
354         }
355
356         return ret;
357 }
358
359 int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
360                             int regnum, u16 val)
361 {
362         int ret;
363
364         ret = mv88e6xxx_ppu_access_get(ds);
365         if (ret >= 0) {
366                 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
367                 mv88e6xxx_ppu_access_put(ds);
368         }
369
370         return ret;
371 }
372 #endif
373
374 static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
375 {
376         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
377
378         switch (ps->id) {
379         case PORT_SWITCH_ID_6031:
380         case PORT_SWITCH_ID_6061:
381         case PORT_SWITCH_ID_6035:
382         case PORT_SWITCH_ID_6065:
383                 return true;
384         }
385         return false;
386 }
387
388 static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
389 {
390         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
391
392         switch (ps->id) {
393         case PORT_SWITCH_ID_6092:
394         case PORT_SWITCH_ID_6095:
395                 return true;
396         }
397         return false;
398 }
399
400 static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
401 {
402         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
403
404         switch (ps->id) {
405         case PORT_SWITCH_ID_6046:
406         case PORT_SWITCH_ID_6085:
407         case PORT_SWITCH_ID_6096:
408         case PORT_SWITCH_ID_6097:
409                 return true;
410         }
411         return false;
412 }
413
414 static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
415 {
416         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
417
418         switch (ps->id) {
419         case PORT_SWITCH_ID_6123:
420         case PORT_SWITCH_ID_6161:
421         case PORT_SWITCH_ID_6165:
422                 return true;
423         }
424         return false;
425 }
426
427 static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
428 {
429         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
430
431         switch (ps->id) {
432         case PORT_SWITCH_ID_6121:
433         case PORT_SWITCH_ID_6122:
434         case PORT_SWITCH_ID_6152:
435         case PORT_SWITCH_ID_6155:
436         case PORT_SWITCH_ID_6182:
437         case PORT_SWITCH_ID_6185:
438         case PORT_SWITCH_ID_6108:
439         case PORT_SWITCH_ID_6131:
440                 return true;
441         }
442         return false;
443 }
444
445 static bool mv88e6xxx_6320_family(struct dsa_switch *ds)
446 {
447         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
448
449         switch (ps->id) {
450         case PORT_SWITCH_ID_6320:
451         case PORT_SWITCH_ID_6321:
452                 return true;
453         }
454         return false;
455 }
456
457 static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
458 {
459         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
460
461         switch (ps->id) {
462         case PORT_SWITCH_ID_6171:
463         case PORT_SWITCH_ID_6175:
464         case PORT_SWITCH_ID_6350:
465         case PORT_SWITCH_ID_6351:
466                 return true;
467         }
468         return false;
469 }
470
471 static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
472 {
473         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
474
475         switch (ps->id) {
476         case PORT_SWITCH_ID_6172:
477         case PORT_SWITCH_ID_6176:
478         case PORT_SWITCH_ID_6240:
479         case PORT_SWITCH_ID_6352:
480                 return true;
481         }
482         return false;
483 }
484
485 /* We expect the switch to perform auto negotiation if there is a real
486  * phy. However, in the case of a fixed link phy, we force the port
487  * settings from the fixed link settings.
488  */
489 void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
490                            struct phy_device *phydev)
491 {
492         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
493         u32 reg;
494         int ret;
495
496         if (!phy_is_pseudo_fixed_link(phydev))
497                 return;
498
499         mutex_lock(&ps->smi_mutex);
500
501         ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
502         if (ret < 0)
503                 goto out;
504
505         reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
506                       PORT_PCS_CTRL_FORCE_LINK |
507                       PORT_PCS_CTRL_DUPLEX_FULL |
508                       PORT_PCS_CTRL_FORCE_DUPLEX |
509                       PORT_PCS_CTRL_UNFORCED);
510
511         reg |= PORT_PCS_CTRL_FORCE_LINK;
512         if (phydev->link)
513                         reg |= PORT_PCS_CTRL_LINK_UP;
514
515         if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100)
516                 goto out;
517
518         switch (phydev->speed) {
519         case SPEED_1000:
520                 reg |= PORT_PCS_CTRL_1000;
521                 break;
522         case SPEED_100:
523                 reg |= PORT_PCS_CTRL_100;
524                 break;
525         case SPEED_10:
526                 reg |= PORT_PCS_CTRL_10;
527                 break;
528         default:
529                 pr_info("Unknown speed");
530                 goto out;
531         }
532
533         reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
534         if (phydev->duplex == DUPLEX_FULL)
535                 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
536
537         if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) &&
538             (port >= ps->num_ports - 2)) {
539                 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
540                         reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
541                 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
542                         reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
543                 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
544                         reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
545                                 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
546         }
547         _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg);
548
549 out:
550         mutex_unlock(&ps->smi_mutex);
551 }
552
553 static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
554 {
555         int ret;
556         int i;
557
558         for (i = 0; i < 10; i++) {
559                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
560                 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
561                         return 0;
562         }
563
564         return -ETIMEDOUT;
565 }
566
567 static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
568 {
569         int ret;
570
571         if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
572                 port = (port + 1) << 5;
573
574         /* Snapshot the hardware statistics counters for this port. */
575         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
576                                    GLOBAL_STATS_OP_CAPTURE_PORT |
577                                    GLOBAL_STATS_OP_HIST_RX_TX | port);
578         if (ret < 0)
579                 return ret;
580
581         /* Wait for the snapshotting to complete. */
582         ret = _mv88e6xxx_stats_wait(ds);
583         if (ret < 0)
584                 return ret;
585
586         return 0;
587 }
588
589 static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
590 {
591         u32 _val;
592         int ret;
593
594         *val = 0;
595
596         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
597                                    GLOBAL_STATS_OP_READ_CAPTURED |
598                                    GLOBAL_STATS_OP_HIST_RX_TX | stat);
599         if (ret < 0)
600                 return;
601
602         ret = _mv88e6xxx_stats_wait(ds);
603         if (ret < 0)
604                 return;
605
606         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
607         if (ret < 0)
608                 return;
609
610         _val = ret << 16;
611
612         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
613         if (ret < 0)
614                 return;
615
616         *val = _val | ret;
617 }
618
619 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
620         { "in_good_octets",     8, 0x00, BANK0, },
621         { "in_bad_octets",      4, 0x02, BANK0, },
622         { "in_unicast",         4, 0x04, BANK0, },
623         { "in_broadcasts",      4, 0x06, BANK0, },
624         { "in_multicasts",      4, 0x07, BANK0, },
625         { "in_pause",           4, 0x16, BANK0, },
626         { "in_undersize",       4, 0x18, BANK0, },
627         { "in_fragments",       4, 0x19, BANK0, },
628         { "in_oversize",        4, 0x1a, BANK0, },
629         { "in_jabber",          4, 0x1b, BANK0, },
630         { "in_rx_error",        4, 0x1c, BANK0, },
631         { "in_fcs_error",       4, 0x1d, BANK0, },
632         { "out_octets",         8, 0x0e, BANK0, },
633         { "out_unicast",        4, 0x10, BANK0, },
634         { "out_broadcasts",     4, 0x13, BANK0, },
635         { "out_multicasts",     4, 0x12, BANK0, },
636         { "out_pause",          4, 0x15, BANK0, },
637         { "excessive",          4, 0x11, BANK0, },
638         { "collisions",         4, 0x1e, BANK0, },
639         { "deferred",           4, 0x05, BANK0, },
640         { "single",             4, 0x14, BANK0, },
641         { "multiple",           4, 0x17, BANK0, },
642         { "out_fcs_error",      4, 0x03, BANK0, },
643         { "late",               4, 0x1f, BANK0, },
644         { "hist_64bytes",       4, 0x08, BANK0, },
645         { "hist_65_127bytes",   4, 0x09, BANK0, },
646         { "hist_128_255bytes",  4, 0x0a, BANK0, },
647         { "hist_256_511bytes",  4, 0x0b, BANK0, },
648         { "hist_512_1023bytes", 4, 0x0c, BANK0, },
649         { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
650         { "sw_in_discards",     4, 0x10, PORT, },
651         { "sw_in_filtered",     2, 0x12, PORT, },
652         { "sw_out_filtered",    2, 0x13, PORT, },
653         { "in_discards",        4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
654         { "in_filtered",        4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
655         { "in_accepted",        4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
656         { "in_bad_accepted",    4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
657         { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
658         { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
659         { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
660         { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
661         { "tcam_counter_0",     4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
662         { "tcam_counter_1",     4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
663         { "tcam_counter_2",     4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
664         { "tcam_counter_3",     4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
665         { "in_da_unknown",      4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
666         { "in_management",      4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
667         { "out_queue_0",        4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
668         { "out_queue_1",        4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
669         { "out_queue_2",        4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
670         { "out_queue_3",        4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
671         { "out_queue_4",        4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
672         { "out_queue_5",        4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
673         { "out_queue_6",        4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
674         { "out_queue_7",        4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
675         { "out_cut_through",    4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
676         { "out_octets_a",       4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
677         { "out_octets_b",       4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
678         { "out_management",     4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
679 };
680
681 static bool mv88e6xxx_has_stat(struct dsa_switch *ds,
682                                struct mv88e6xxx_hw_stat *stat)
683 {
684         switch (stat->type) {
685         case BANK0:
686                 return true;
687         case BANK1:
688                 return mv88e6xxx_6320_family(ds);
689         case PORT:
690                 return mv88e6xxx_6095_family(ds) ||
691                         mv88e6xxx_6185_family(ds) ||
692                         mv88e6xxx_6097_family(ds) ||
693                         mv88e6xxx_6165_family(ds) ||
694                         mv88e6xxx_6351_family(ds) ||
695                         mv88e6xxx_6352_family(ds);
696         }
697         return false;
698 }
699
700 static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
701                                             struct mv88e6xxx_hw_stat *s,
702                                             int port)
703 {
704         u32 low;
705         u32 high = 0;
706         int ret;
707         u64 value;
708
709         switch (s->type) {
710         case PORT:
711                 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), s->reg);
712                 if (ret < 0)
713                         return UINT64_MAX;
714
715                 low = ret;
716                 if (s->sizeof_stat == 4) {
717                         ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
718                                                   s->reg + 1);
719                         if (ret < 0)
720                                 return UINT64_MAX;
721                         high = ret;
722                 }
723                 break;
724         case BANK0:
725         case BANK1:
726                 _mv88e6xxx_stats_read(ds, s->reg, &low);
727                 if (s->sizeof_stat == 8)
728                         _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
729         }
730         value = (((u64)high) << 16) | low;
731         return value;
732 }
733
734 void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
735 {
736         struct mv88e6xxx_hw_stat *stat;
737         int i, j;
738
739         for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
740                 stat = &mv88e6xxx_hw_stats[i];
741                 if (mv88e6xxx_has_stat(ds, stat)) {
742                         memcpy(data + j * ETH_GSTRING_LEN, stat->string,
743                                ETH_GSTRING_LEN);
744                         j++;
745                 }
746         }
747 }
748
749 int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
750 {
751         struct mv88e6xxx_hw_stat *stat;
752         int i, j;
753
754         for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
755                 stat = &mv88e6xxx_hw_stats[i];
756                 if (mv88e6xxx_has_stat(ds, stat))
757                         j++;
758         }
759         return j;
760 }
761
762 void
763 mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
764                             int port, uint64_t *data)
765 {
766         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
767         struct mv88e6xxx_hw_stat *stat;
768         int ret;
769         int i, j;
770
771         mutex_lock(&ps->smi_mutex);
772
773         ret = _mv88e6xxx_stats_snapshot(ds, port);
774         if (ret < 0) {
775                 mutex_unlock(&ps->smi_mutex);
776                 return;
777         }
778         for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
779                 stat = &mv88e6xxx_hw_stats[i];
780                 if (mv88e6xxx_has_stat(ds, stat)) {
781                         data[j] = _mv88e6xxx_get_ethtool_stat(ds, stat, port);
782                         j++;
783                 }
784         }
785
786         mutex_unlock(&ps->smi_mutex);
787 }
788
789 int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
790 {
791         return 32 * sizeof(u16);
792 }
793
794 void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
795                         struct ethtool_regs *regs, void *_p)
796 {
797         u16 *p = _p;
798         int i;
799
800         regs->version = 0;
801
802         memset(p, 0xff, 32 * sizeof(u16));
803
804         for (i = 0; i < 32; i++) {
805                 int ret;
806
807                 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
808                 if (ret >= 0)
809                         p[i] = ret;
810         }
811 }
812
813 static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
814                            u16 mask)
815 {
816         unsigned long timeout = jiffies + HZ / 10;
817
818         while (time_before(jiffies, timeout)) {
819                 int ret;
820
821                 ret = _mv88e6xxx_reg_read(ds, reg, offset);
822                 if (ret < 0)
823                         return ret;
824                 if (!(ret & mask))
825                         return 0;
826
827                 usleep_range(1000, 2000);
828         }
829         return -ETIMEDOUT;
830 }
831
832 static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
833 {
834         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
835         int ret;
836
837         mutex_lock(&ps->smi_mutex);
838         ret = _mv88e6xxx_wait(ds, reg, offset, mask);
839         mutex_unlock(&ps->smi_mutex);
840
841         return ret;
842 }
843
844 static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
845 {
846         return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
847                                GLOBAL2_SMI_OP_BUSY);
848 }
849
850 int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
851 {
852         return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
853                               GLOBAL2_EEPROM_OP_LOAD);
854 }
855
856 int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
857 {
858         return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
859                               GLOBAL2_EEPROM_OP_BUSY);
860 }
861
862 static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
863 {
864         return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
865                                GLOBAL_ATU_OP_BUSY);
866 }
867
868 static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
869                                         int regnum)
870 {
871         int ret;
872
873         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
874                                    GLOBAL2_SMI_OP_22_READ | (addr << 5) |
875                                    regnum);
876         if (ret < 0)
877                 return ret;
878
879         ret = _mv88e6xxx_phy_wait(ds);
880         if (ret < 0)
881                 return ret;
882
883         return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
884 }
885
886 static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
887                                          int regnum, u16 val)
888 {
889         int ret;
890
891         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
892         if (ret < 0)
893                 return ret;
894
895         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
896                                    GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
897                                    regnum);
898
899         return _mv88e6xxx_phy_wait(ds);
900 }
901
902 int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
903 {
904         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
905         int reg;
906
907         mutex_lock(&ps->smi_mutex);
908
909         reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
910         if (reg < 0)
911                 goto out;
912
913         e->eee_enabled = !!(reg & 0x0200);
914         e->tx_lpi_enabled = !!(reg & 0x0100);
915
916         reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
917         if (reg < 0)
918                 goto out;
919
920         e->eee_active = !!(reg & PORT_STATUS_EEE);
921         reg = 0;
922
923 out:
924         mutex_unlock(&ps->smi_mutex);
925         return reg;
926 }
927
928 int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
929                       struct phy_device *phydev, struct ethtool_eee *e)
930 {
931         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
932         int reg;
933         int ret;
934
935         mutex_lock(&ps->smi_mutex);
936
937         ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
938         if (ret < 0)
939                 goto out;
940
941         reg = ret & ~0x0300;
942         if (e->eee_enabled)
943                 reg |= 0x0200;
944         if (e->tx_lpi_enabled)
945                 reg |= 0x0100;
946
947         ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
948 out:
949         mutex_unlock(&ps->smi_mutex);
950
951         return ret;
952 }
953
954 static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 cmd)
955 {
956         int ret;
957
958         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
959         if (ret < 0)
960                 return ret;
961
962         return _mv88e6xxx_atu_wait(ds);
963 }
964
965 static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds,
966                                      struct mv88e6xxx_atu_entry *entry)
967 {
968         u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
969
970         if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
971                 unsigned int mask, shift;
972
973                 if (entry->trunk) {
974                         data |= GLOBAL_ATU_DATA_TRUNK;
975                         mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
976                         shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
977                 } else {
978                         mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
979                         shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
980                 }
981
982                 data |= (entry->portv_trunkid << shift) & mask;
983         }
984
985         return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data);
986 }
987
988 static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds,
989                                      struct mv88e6xxx_atu_entry *entry,
990                                      bool static_too)
991 {
992         int op;
993         int err;
994
995         err = _mv88e6xxx_atu_wait(ds);
996         if (err)
997                 return err;
998
999         err = _mv88e6xxx_atu_data_write(ds, entry);
1000         if (err)
1001                 return err;
1002
1003         if (entry->fid) {
1004                 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID,
1005                                            entry->fid);
1006                 if (err)
1007                         return err;
1008
1009                 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1010                         GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1011         } else {
1012                 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1013                         GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1014         }
1015
1016         return _mv88e6xxx_atu_cmd(ds, op);
1017 }
1018
1019 static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too)
1020 {
1021         struct mv88e6xxx_atu_entry entry = {
1022                 .fid = fid,
1023                 .state = 0, /* EntryState bits must be 0 */
1024         };
1025
1026         return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1027 }
1028
1029 static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port,
1030                                int to_port, bool static_too)
1031 {
1032         struct mv88e6xxx_atu_entry entry = {
1033                 .trunk = false,
1034                 .fid = fid,
1035         };
1036
1037         /* EntryState bits must be 0xF */
1038         entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1039
1040         /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1041         entry.portv_trunkid = (to_port & 0x0f) << 4;
1042         entry.portv_trunkid |= from_port & 0x0f;
1043
1044         return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1045 }
1046
1047 static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port,
1048                                  bool static_too)
1049 {
1050         /* Destination port 0xF means remove the entries */
1051         return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too);
1052 }
1053
1054 static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
1055 {
1056         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1057         int reg, ret = 0;
1058         u8 oldstate;
1059
1060         mutex_lock(&ps->smi_mutex);
1061
1062         reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
1063         if (reg < 0) {
1064                 ret = reg;
1065                 goto abort;
1066         }
1067
1068         oldstate = reg & PORT_CONTROL_STATE_MASK;
1069         if (oldstate != state) {
1070                 /* Flush forwarding database if we're moving a port
1071                  * from Learning or Forwarding state to Disabled or
1072                  * Blocking or Listening state.
1073                  */
1074                 if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
1075                     state <= PORT_CONTROL_STATE_BLOCKING) {
1076                         ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
1077                         if (ret)
1078                                 goto abort;
1079                 }
1080                 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1081                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1082                                            reg);
1083         }
1084
1085 abort:
1086         mutex_unlock(&ps->smi_mutex);
1087         return ret;
1088 }
1089
1090 static int _mv88e6xxx_port_vlan_map_set(struct dsa_switch *ds, int port,
1091                                         u16 output_ports)
1092 {
1093         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1094         const u16 mask = (1 << ps->num_ports) - 1;
1095         int reg;
1096
1097         reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1098         if (reg < 0)
1099                 return reg;
1100
1101         reg &= ~mask;
1102         reg |= output_ports & mask;
1103
1104         return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
1105 }
1106
1107 int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1108 {
1109         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1110         int stp_state;
1111
1112         switch (state) {
1113         case BR_STATE_DISABLED:
1114                 stp_state = PORT_CONTROL_STATE_DISABLED;
1115                 break;
1116         case BR_STATE_BLOCKING:
1117         case BR_STATE_LISTENING:
1118                 stp_state = PORT_CONTROL_STATE_BLOCKING;
1119                 break;
1120         case BR_STATE_LEARNING:
1121                 stp_state = PORT_CONTROL_STATE_LEARNING;
1122                 break;
1123         case BR_STATE_FORWARDING:
1124         default:
1125                 stp_state = PORT_CONTROL_STATE_FORWARDING;
1126                 break;
1127         }
1128
1129         netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
1130
1131         /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1132          * so we can not update the port state directly but need to schedule it.
1133          */
1134         ps->ports[port].state = stp_state;
1135         set_bit(port, &ps->port_state_update_mask);
1136         schedule_work(&ps->bridge_work);
1137
1138         return 0;
1139 }
1140
1141 static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1142 {
1143         int ret;
1144
1145         ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1146         if (ret < 0)
1147                 return ret;
1148
1149         *pvid = ret & PORT_DEFAULT_VLAN_MASK;
1150
1151         return 0;
1152 }
1153
1154 static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
1155 {
1156         return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
1157                                    pvid & PORT_DEFAULT_VLAN_MASK);
1158 }
1159
1160 static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1161 {
1162         return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1163                                GLOBAL_VTU_OP_BUSY);
1164 }
1165
1166 static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1167 {
1168         int ret;
1169
1170         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1171         if (ret < 0)
1172                 return ret;
1173
1174         return _mv88e6xxx_vtu_wait(ds);
1175 }
1176
1177 static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1178 {
1179         int ret;
1180
1181         ret = _mv88e6xxx_vtu_wait(ds);
1182         if (ret < 0)
1183                 return ret;
1184
1185         return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1186 }
1187
1188 static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1189                                         struct mv88e6xxx_vtu_stu_entry *entry,
1190                                         unsigned int nibble_offset)
1191 {
1192         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1193         u16 regs[3];
1194         int i;
1195         int ret;
1196
1197         for (i = 0; i < 3; ++i) {
1198                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1199                                           GLOBAL_VTU_DATA_0_3 + i);
1200                 if (ret < 0)
1201                         return ret;
1202
1203                 regs[i] = ret;
1204         }
1205
1206         for (i = 0; i < ps->num_ports; ++i) {
1207                 unsigned int shift = (i % 4) * 4 + nibble_offset;
1208                 u16 reg = regs[i / 4];
1209
1210                 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1211         }
1212
1213         return 0;
1214 }
1215
1216 static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1217                                          struct mv88e6xxx_vtu_stu_entry *entry,
1218                                          unsigned int nibble_offset)
1219 {
1220         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1221         u16 regs[3] = { 0 };
1222         int i;
1223         int ret;
1224
1225         for (i = 0; i < ps->num_ports; ++i) {
1226                 unsigned int shift = (i % 4) * 4 + nibble_offset;
1227                 u8 data = entry->data[i];
1228
1229                 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1230         }
1231
1232         for (i = 0; i < 3; ++i) {
1233                 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1234                                            GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1235                 if (ret < 0)
1236                         return ret;
1237         }
1238
1239         return 0;
1240 }
1241
1242 static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1243 {
1244         return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1245                                     vid & GLOBAL_VTU_VID_MASK);
1246 }
1247
1248 static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
1249                                   struct mv88e6xxx_vtu_stu_entry *entry)
1250 {
1251         struct mv88e6xxx_vtu_stu_entry next = { 0 };
1252         int ret;
1253
1254         ret = _mv88e6xxx_vtu_wait(ds);
1255         if (ret < 0)
1256                 return ret;
1257
1258         ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1259         if (ret < 0)
1260                 return ret;
1261
1262         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1263         if (ret < 0)
1264                 return ret;
1265
1266         next.vid = ret & GLOBAL_VTU_VID_MASK;
1267         next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1268
1269         if (next.valid) {
1270                 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1271                 if (ret < 0)
1272                         return ret;
1273
1274                 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1275                     mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1276                         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1277                                                   GLOBAL_VTU_FID);
1278                         if (ret < 0)
1279                                 return ret;
1280
1281                         next.fid = ret & GLOBAL_VTU_FID_MASK;
1282
1283                         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1284                                                   GLOBAL_VTU_SID);
1285                         if (ret < 0)
1286                                 return ret;
1287
1288                         next.sid = ret & GLOBAL_VTU_SID_MASK;
1289                 }
1290         }
1291
1292         *entry = next;
1293         return 0;
1294 }
1295
1296 int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1297                              struct switchdev_obj_port_vlan *vlan,
1298                              int (*cb)(struct switchdev_obj *obj))
1299 {
1300         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1301         struct mv88e6xxx_vtu_stu_entry next;
1302         u16 pvid;
1303         int err;
1304
1305         mutex_lock(&ps->smi_mutex);
1306
1307         err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1308         if (err)
1309                 goto unlock;
1310
1311         err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1312         if (err)
1313                 goto unlock;
1314
1315         do {
1316                 err = _mv88e6xxx_vtu_getnext(ds, &next);
1317                 if (err)
1318                         break;
1319
1320                 if (!next.valid)
1321                         break;
1322
1323                 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1324                         continue;
1325
1326                 /* reinit and dump this VLAN obj */
1327                 vlan->vid_begin = vlan->vid_end = next.vid;
1328                 vlan->flags = 0;
1329
1330                 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1331                         vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1332
1333                 if (next.vid == pvid)
1334                         vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1335
1336                 err = cb(&vlan->obj);
1337                 if (err)
1338                         break;
1339         } while (next.vid < GLOBAL_VTU_VID_MASK);
1340
1341 unlock:
1342         mutex_unlock(&ps->smi_mutex);
1343
1344         return err;
1345 }
1346
1347 static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1348                                     struct mv88e6xxx_vtu_stu_entry *entry)
1349 {
1350         u16 reg = 0;
1351         int ret;
1352
1353         ret = _mv88e6xxx_vtu_wait(ds);
1354         if (ret < 0)
1355                 return ret;
1356
1357         if (!entry->valid)
1358                 goto loadpurge;
1359
1360         /* Write port member tags */
1361         ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1362         if (ret < 0)
1363                 return ret;
1364
1365         if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1366             mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1367                 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1368                 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1369                 if (ret < 0)
1370                         return ret;
1371
1372                 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1373                 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1374                 if (ret < 0)
1375                         return ret;
1376         }
1377
1378         reg = GLOBAL_VTU_VID_VALID;
1379 loadpurge:
1380         reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1381         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1382         if (ret < 0)
1383                 return ret;
1384
1385         return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_LOAD_PURGE);
1386 }
1387
1388 static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1389                                   struct mv88e6xxx_vtu_stu_entry *entry)
1390 {
1391         struct mv88e6xxx_vtu_stu_entry next = { 0 };
1392         int ret;
1393
1394         ret = _mv88e6xxx_vtu_wait(ds);
1395         if (ret < 0)
1396                 return ret;
1397
1398         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1399                                    sid & GLOBAL_VTU_SID_MASK);
1400         if (ret < 0)
1401                 return ret;
1402
1403         ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1404         if (ret < 0)
1405                 return ret;
1406
1407         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1408         if (ret < 0)
1409                 return ret;
1410
1411         next.sid = ret & GLOBAL_VTU_SID_MASK;
1412
1413         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1414         if (ret < 0)
1415                 return ret;
1416
1417         next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1418
1419         if (next.valid) {
1420                 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1421                 if (ret < 0)
1422                         return ret;
1423         }
1424
1425         *entry = next;
1426         return 0;
1427 }
1428
1429 static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1430                                     struct mv88e6xxx_vtu_stu_entry *entry)
1431 {
1432         u16 reg = 0;
1433         int ret;
1434
1435         ret = _mv88e6xxx_vtu_wait(ds);
1436         if (ret < 0)
1437                 return ret;
1438
1439         if (!entry->valid)
1440                 goto loadpurge;
1441
1442         /* Write port states */
1443         ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1444         if (ret < 0)
1445                 return ret;
1446
1447         reg = GLOBAL_VTU_VID_VALID;
1448 loadpurge:
1449         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1450         if (ret < 0)
1451                 return ret;
1452
1453         reg = entry->sid & GLOBAL_VTU_SID_MASK;
1454         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1455         if (ret < 0)
1456                 return ret;
1457
1458         return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1459 }
1460
1461 static int _mv88e6xxx_port_fid(struct dsa_switch *ds, int port, u16 *new,
1462                                u16 *old)
1463 {
1464         u16 fid;
1465         int ret;
1466
1467         /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1468         ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1469         if (ret < 0)
1470                 return ret;
1471
1472         fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1473
1474         if (new) {
1475                 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1476                 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1477
1478                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN,
1479                                            ret);
1480                 if (ret < 0)
1481                         return ret;
1482         }
1483
1484         /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1485         ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_1);
1486         if (ret < 0)
1487                 return ret;
1488
1489         fid |= (ret & PORT_CONTROL_1_FID_11_4_MASK) << 4;
1490
1491         if (new) {
1492                 ret &= ~PORT_CONTROL_1_FID_11_4_MASK;
1493                 ret |= (*new >> 4) & PORT_CONTROL_1_FID_11_4_MASK;
1494
1495                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1,
1496                                            ret);
1497                 if (ret < 0)
1498                         return ret;
1499
1500                 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1501         }
1502
1503         if (old)
1504                 *old = fid;
1505
1506         return 0;
1507 }
1508
1509 static int _mv88e6xxx_port_fid_get(struct dsa_switch *ds, int port, u16 *fid)
1510 {
1511         return _mv88e6xxx_port_fid(ds, port, NULL, fid);
1512 }
1513
1514 static int _mv88e6xxx_port_fid_set(struct dsa_switch *ds, int port, u16 fid)
1515 {
1516         return _mv88e6xxx_port_fid(ds, port, &fid, NULL);
1517 }
1518
1519 static int _mv88e6xxx_fid_new(struct dsa_switch *ds, u16 *fid)
1520 {
1521         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1522         DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1523         struct mv88e6xxx_vtu_stu_entry vlan;
1524         int i, err;
1525
1526         bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1527
1528         /* Set every FID bit used by the (un)bridged ports */
1529         for (i = 0; i < ps->num_ports; ++i) {
1530                 err = _mv88e6xxx_port_fid_get(ds, i, fid);
1531                 if (err)
1532                         return err;
1533
1534                 set_bit(*fid, fid_bitmap);
1535         }
1536
1537         /* Set every FID bit used by the VLAN entries */
1538         err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1539         if (err)
1540                 return err;
1541
1542         do {
1543                 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1544                 if (err)
1545                         return err;
1546
1547                 if (!vlan.valid)
1548                         break;
1549
1550                 set_bit(vlan.fid, fid_bitmap);
1551         } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1552
1553         /* The reset value 0x000 is used to indicate that multiple address
1554          * databases are not needed. Return the next positive available.
1555          */
1556         *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1557         if (unlikely(*fid == MV88E6XXX_N_FID))
1558                 return -ENOSPC;
1559
1560         /* Clear the database */
1561         return _mv88e6xxx_atu_flush(ds, *fid, true);
1562 }
1563
1564 static int _mv88e6xxx_vtu_new(struct dsa_switch *ds, u16 vid,
1565                               struct mv88e6xxx_vtu_stu_entry *entry)
1566 {
1567         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1568         struct mv88e6xxx_vtu_stu_entry vlan = {
1569                 .valid = true,
1570                 .vid = vid,
1571         };
1572         int i, err;
1573
1574         err = _mv88e6xxx_fid_new(ds, &vlan.fid);
1575         if (err)
1576                 return err;
1577
1578         /* exclude all ports except the CPU and DSA ports */
1579         for (i = 0; i < ps->num_ports; ++i)
1580                 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1581                         ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1582                         : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1583
1584         if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1585             mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1586                 struct mv88e6xxx_vtu_stu_entry vstp;
1587
1588                 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1589                  * implemented, only one STU entry is needed to cover all VTU
1590                  * entries. Thus, validate the SID 0.
1591                  */
1592                 vlan.sid = 0;
1593                 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1594                 if (err)
1595                         return err;
1596
1597                 if (vstp.sid != vlan.sid || !vstp.valid) {
1598                         memset(&vstp, 0, sizeof(vstp));
1599                         vstp.valid = true;
1600                         vstp.sid = vlan.sid;
1601
1602                         err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1603                         if (err)
1604                                 return err;
1605                 }
1606         }
1607
1608         *entry = vlan;
1609         return 0;
1610 }
1611
1612 static int _mv88e6xxx_vtu_get(struct dsa_switch *ds, u16 vid,
1613                               struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1614 {
1615         int err;
1616
1617         if (!vid)
1618                 return -EINVAL;
1619
1620         err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1621         if (err)
1622                 return err;
1623
1624         err = _mv88e6xxx_vtu_getnext(ds, entry);
1625         if (err)
1626                 return err;
1627
1628         if (entry->vid != vid || !entry->valid) {
1629                 if (!creat)
1630                         return -EOPNOTSUPP;
1631                 /* -ENOENT would've been more appropriate, but switchdev expects
1632                  * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1633                  */
1634
1635                 err = _mv88e6xxx_vtu_new(ds, vid, entry);
1636         }
1637
1638         return err;
1639 }
1640
1641 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1642                                         u16 vid_begin, u16 vid_end)
1643 {
1644         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1645         struct mv88e6xxx_vtu_stu_entry vlan;
1646         int i, err;
1647
1648         if (!vid_begin)
1649                 return -EOPNOTSUPP;
1650
1651         mutex_lock(&ps->smi_mutex);
1652
1653         err = _mv88e6xxx_vtu_vid_write(ds, vid_begin - 1);
1654         if (err)
1655                 goto unlock;
1656
1657         do {
1658                 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1659                 if (err)
1660                         goto unlock;
1661
1662                 if (!vlan.valid)
1663                         break;
1664
1665                 if (vlan.vid > vid_end)
1666                         break;
1667
1668                 for (i = 0; i < ps->num_ports; ++i) {
1669                         if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1670                                 continue;
1671
1672                         if (vlan.data[i] ==
1673                             GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1674                                 continue;
1675
1676                         if (ps->ports[i].bridge_dev ==
1677                             ps->ports[port].bridge_dev)
1678                                 break; /* same bridge, check next VLAN */
1679
1680                         netdev_warn(ds->ports[port],
1681                                     "hardware VLAN %d already used by %s\n",
1682                                     vlan.vid,
1683                                     netdev_name(ps->ports[i].bridge_dev));
1684                         err = -EOPNOTSUPP;
1685                         goto unlock;
1686                 }
1687         } while (vlan.vid < vid_end);
1688
1689 unlock:
1690         mutex_unlock(&ps->smi_mutex);
1691
1692         return err;
1693 }
1694
1695 int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1696                                 const struct switchdev_obj_port_vlan *vlan,
1697                                 struct switchdev_trans *trans)
1698 {
1699         int err;
1700
1701         /* We reserve a few VLANs to isolate unbridged ports */
1702         if (vlan->vid_end >= 4000)
1703                 return -EOPNOTSUPP;
1704
1705         /* If the requested port doesn't belong to the same bridge as the VLAN
1706          * members, do not support it (yet) and fallback to software VLAN.
1707          */
1708         err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1709                                            vlan->vid_end);
1710         if (err)
1711                 return err;
1712
1713         /* We don't need any dynamic resource from the kernel (yet),
1714          * so skip the prepare phase.
1715          */
1716         return 0;
1717 }
1718
1719 static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1720                                     bool untagged)
1721 {
1722         struct mv88e6xxx_vtu_stu_entry vlan;
1723         int err;
1724
1725         err = _mv88e6xxx_vtu_get(ds, vid, &vlan, true);
1726         if (err)
1727                 return err;
1728
1729         vlan.data[port] = untagged ?
1730                 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1731                 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1732
1733         return _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1734 }
1735
1736 int mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1737                             const struct switchdev_obj_port_vlan *vlan,
1738                             struct switchdev_trans *trans)
1739 {
1740         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1741         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1742         bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1743         u16 vid;
1744         int err = 0;
1745
1746         mutex_lock(&ps->smi_mutex);
1747
1748         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1749                 err = _mv88e6xxx_port_vlan_add(ds, port, vid, untagged);
1750                 if (err)
1751                         goto unlock;
1752         }
1753
1754         /* no PVID with ranges, otherwise it's a bug */
1755         if (pvid)
1756                 err = _mv88e6xxx_port_pvid_set(ds, port, vlan->vid_end);
1757 unlock:
1758         mutex_unlock(&ps->smi_mutex);
1759
1760         return err;
1761 }
1762
1763 static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
1764 {
1765         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1766         struct mv88e6xxx_vtu_stu_entry vlan;
1767         int i, err;
1768
1769         err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
1770         if (err)
1771                 return err;
1772
1773         /* Tell switchdev if this VLAN is handled in software */
1774         if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1775                 return -EOPNOTSUPP;
1776
1777         vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1778
1779         /* keep the VLAN unless all ports are excluded */
1780         vlan.valid = false;
1781         for (i = 0; i < ps->num_ports; ++i) {
1782                 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1783                         continue;
1784
1785                 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
1786                         vlan.valid = true;
1787                         break;
1788                 }
1789         }
1790
1791         err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1792         if (err)
1793                 return err;
1794
1795         return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
1796 }
1797
1798 int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1799                             const struct switchdev_obj_port_vlan *vlan)
1800 {
1801         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1802         const u16 defpvid = 4000 + ds->index * DSA_MAX_PORTS + port;
1803         u16 pvid, vid;
1804         int err = 0;
1805
1806         mutex_lock(&ps->smi_mutex);
1807
1808         err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1809         if (err)
1810                 goto unlock;
1811
1812         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1813                 err = _mv88e6xxx_port_vlan_del(ds, port, vid);
1814                 if (err)
1815                         goto unlock;
1816
1817                 if (vid == pvid) {
1818                         /* restore reserved VLAN ID */
1819                         err = _mv88e6xxx_port_pvid_set(ds, port, defpvid);
1820                         if (err)
1821                                 goto unlock;
1822                 }
1823         }
1824
1825 unlock:
1826         mutex_unlock(&ps->smi_mutex);
1827
1828         return err;
1829 }
1830
1831 static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
1832                                     const unsigned char *addr)
1833 {
1834         int i, ret;
1835
1836         for (i = 0; i < 3; i++) {
1837                 ret = _mv88e6xxx_reg_write(
1838                         ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1839                         (addr[i * 2] << 8) | addr[i * 2 + 1]);
1840                 if (ret < 0)
1841                         return ret;
1842         }
1843
1844         return 0;
1845 }
1846
1847 static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
1848 {
1849         int i, ret;
1850
1851         for (i = 0; i < 3; i++) {
1852                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1853                                           GLOBAL_ATU_MAC_01 + i);
1854                 if (ret < 0)
1855                         return ret;
1856                 addr[i * 2] = ret >> 8;
1857                 addr[i * 2 + 1] = ret & 0xff;
1858         }
1859
1860         return 0;
1861 }
1862
1863 static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
1864                                struct mv88e6xxx_atu_entry *entry)
1865 {
1866         int ret;
1867
1868         ret = _mv88e6xxx_atu_wait(ds);
1869         if (ret < 0)
1870                 return ret;
1871
1872         ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
1873         if (ret < 0)
1874                 return ret;
1875
1876         ret = _mv88e6xxx_atu_data_write(ds, entry);
1877         if (ret < 0)
1878                 return ret;
1879
1880         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, entry->fid);
1881         if (ret < 0)
1882                 return ret;
1883
1884         return _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_LOAD_DB);
1885 }
1886
1887 static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
1888                                     const unsigned char *addr, u16 vid,
1889                                     u8 state)
1890 {
1891         struct mv88e6xxx_atu_entry entry = { 0 };
1892         struct mv88e6xxx_vtu_stu_entry vlan;
1893         int err;
1894
1895         /* Null VLAN ID corresponds to the port private database */
1896         if (vid == 0)
1897                 err = _mv88e6xxx_port_fid_get(ds, port, &vlan.fid);
1898         else
1899                 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
1900         if (err)
1901                 return err;
1902
1903         entry.fid = vlan.fid;
1904         entry.state = state;
1905         ether_addr_copy(entry.mac, addr);
1906         if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1907                 entry.trunk = false;
1908                 entry.portv_trunkid = BIT(port);
1909         }
1910
1911         return _mv88e6xxx_atu_load(ds, &entry);
1912 }
1913
1914 int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
1915                                const struct switchdev_obj_port_fdb *fdb,
1916                                struct switchdev_trans *trans)
1917 {
1918         /* We don't need any dynamic resource from the kernel (yet),
1919          * so skip the prepare phase.
1920          */
1921         return 0;
1922 }
1923
1924 int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
1925                            const struct switchdev_obj_port_fdb *fdb,
1926                            struct switchdev_trans *trans)
1927 {
1928         int state = is_multicast_ether_addr(fdb->addr) ?
1929                 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1930                 GLOBAL_ATU_DATA_STATE_UC_STATIC;
1931         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1932         int ret;
1933
1934         mutex_lock(&ps->smi_mutex);
1935         ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state);
1936         mutex_unlock(&ps->smi_mutex);
1937
1938         return ret;
1939 }
1940
1941 int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
1942                            const struct switchdev_obj_port_fdb *fdb)
1943 {
1944         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1945         int ret;
1946
1947         mutex_lock(&ps->smi_mutex);
1948         ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
1949                                        GLOBAL_ATU_DATA_STATE_UNUSED);
1950         mutex_unlock(&ps->smi_mutex);
1951
1952         return ret;
1953 }
1954
1955 static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
1956                                   struct mv88e6xxx_atu_entry *entry)
1957 {
1958         struct mv88e6xxx_atu_entry next = { 0 };
1959         int ret;
1960
1961         next.fid = fid;
1962
1963         ret = _mv88e6xxx_atu_wait(ds);
1964         if (ret < 0)
1965                 return ret;
1966
1967         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
1968         if (ret < 0)
1969                 return ret;
1970
1971         ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB);
1972         if (ret < 0)
1973                 return ret;
1974
1975         ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
1976         if (ret < 0)
1977                 return ret;
1978
1979         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1980         if (ret < 0)
1981                 return ret;
1982
1983         next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
1984         if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1985                 unsigned int mask, shift;
1986
1987                 if (ret & GLOBAL_ATU_DATA_TRUNK) {
1988                         next.trunk = true;
1989                         mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1990                         shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1991                 } else {
1992                         next.trunk = false;
1993                         mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1994                         shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1995                 }
1996
1997                 next.portv_trunkid = (ret & mask) >> shift;
1998         }
1999
2000         *entry = next;
2001         return 0;
2002 }
2003
2004 static int _mv88e6xxx_port_fdb_dump_one(struct dsa_switch *ds, u16 fid, u16 vid,
2005                                         int port,
2006                                         struct switchdev_obj_port_fdb *fdb,
2007                                         int (*cb)(struct switchdev_obj *obj))
2008 {
2009         struct mv88e6xxx_atu_entry addr = {
2010                 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2011         };
2012         int err;
2013
2014         err = _mv88e6xxx_atu_mac_write(ds, addr.mac);
2015         if (err)
2016                 return err;
2017
2018         do {
2019                 err = _mv88e6xxx_atu_getnext(ds, fid, &addr);
2020                 if (err)
2021                         break;
2022
2023                 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2024                         break;
2025
2026                 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2027                         bool is_static = addr.state ==
2028                                 (is_multicast_ether_addr(addr.mac) ?
2029                                  GLOBAL_ATU_DATA_STATE_MC_STATIC :
2030                                  GLOBAL_ATU_DATA_STATE_UC_STATIC);
2031
2032                         fdb->vid = vid;
2033                         ether_addr_copy(fdb->addr, addr.mac);
2034                         fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2035
2036                         err = cb(&fdb->obj);
2037                         if (err)
2038                                 break;
2039                 }
2040         } while (!is_broadcast_ether_addr(addr.mac));
2041
2042         return err;
2043 }
2044
2045 int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2046                             struct switchdev_obj_port_fdb *fdb,
2047                             int (*cb)(struct switchdev_obj *obj))
2048 {
2049         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2050         struct mv88e6xxx_vtu_stu_entry vlan = {
2051                 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2052         };
2053         u16 fid;
2054         int err;
2055
2056         mutex_lock(&ps->smi_mutex);
2057
2058         /* Dump port's default Filtering Information Database (VLAN ID 0) */
2059         err = _mv88e6xxx_port_fid_get(ds, port, &fid);
2060         if (err)
2061                 goto unlock;
2062
2063         err = _mv88e6xxx_port_fdb_dump_one(ds, fid, 0, port, fdb, cb);
2064         if (err)
2065                 goto unlock;
2066
2067         /* Dump VLANs' Filtering Information Databases */
2068         err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid);
2069         if (err)
2070                 goto unlock;
2071
2072         do {
2073                 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
2074                 if (err)
2075                         break;
2076
2077                 if (!vlan.valid)
2078                         break;
2079
2080                 err = _mv88e6xxx_port_fdb_dump_one(ds, vlan.fid, vlan.vid, port,
2081                                                    fdb, cb);
2082                 if (err)
2083                         break;
2084         } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2085
2086 unlock:
2087         mutex_unlock(&ps->smi_mutex);
2088
2089         return err;
2090 }
2091
2092 int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2093                                struct net_device *bridge)
2094 {
2095         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2096
2097         ps->ports[port].bridge_dev = bridge;
2098
2099         return 0;
2100 }
2101
2102 int mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2103 {
2104         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2105
2106         ps->ports[port].bridge_dev = NULL;
2107
2108         return 0;
2109 }
2110
2111 static int mv88e6xxx_setup_port_default_vlan(struct dsa_switch *ds, int port)
2112 {
2113         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2114         const u16 pvid = 4000 + ds->index * DSA_MAX_PORTS + port;
2115         int err;
2116
2117         mutex_lock(&ps->smi_mutex);
2118         err = _mv88e6xxx_port_vlan_add(ds, port, pvid, true);
2119         if (!err)
2120                 err = _mv88e6xxx_port_pvid_set(ds, port, pvid);
2121         mutex_unlock(&ps->smi_mutex);
2122         return err;
2123 }
2124
2125 static void mv88e6xxx_bridge_work(struct work_struct *work)
2126 {
2127         struct mv88e6xxx_priv_state *ps;
2128         struct dsa_switch *ds;
2129         int port;
2130
2131         ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
2132         ds = ((struct dsa_switch *)ps) - 1;
2133
2134         while (ps->port_state_update_mask) {
2135                 port = __ffs(ps->port_state_update_mask);
2136                 clear_bit(port, &ps->port_state_update_mask);
2137                 mv88e6xxx_set_port_state(ds, port, ps->ports[port].state);
2138         }
2139 }
2140
2141 static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
2142 {
2143         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2144         int ret;
2145         u16 reg;
2146
2147         mutex_lock(&ps->smi_mutex);
2148
2149         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2150             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2151             mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2152             mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
2153                 /* MAC Forcing register: don't force link, speed,
2154                  * duplex or flow control state to any particular
2155                  * values on physical ports, but force the CPU port
2156                  * and all DSA ports to their maximum bandwidth and
2157                  * full duplex.
2158                  */
2159                 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
2160                 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2161                         reg &= ~PORT_PCS_CTRL_UNFORCED;
2162                         reg |= PORT_PCS_CTRL_FORCE_LINK |
2163                                 PORT_PCS_CTRL_LINK_UP |
2164                                 PORT_PCS_CTRL_DUPLEX_FULL |
2165                                 PORT_PCS_CTRL_FORCE_DUPLEX;
2166                         if (mv88e6xxx_6065_family(ds))
2167                                 reg |= PORT_PCS_CTRL_100;
2168                         else
2169                                 reg |= PORT_PCS_CTRL_1000;
2170                 } else {
2171                         reg |= PORT_PCS_CTRL_UNFORCED;
2172                 }
2173
2174                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2175                                            PORT_PCS_CTRL, reg);
2176                 if (ret)
2177                         goto abort;
2178         }
2179
2180         /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2181          * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2182          * tunneling, determine priority by looking at 802.1p and IP
2183          * priority fields (IP prio has precedence), and set STP state
2184          * to Forwarding.
2185          *
2186          * If this is the CPU link, use DSA or EDSA tagging depending
2187          * on which tagging mode was configured.
2188          *
2189          * If this is a link to another switch, use DSA tagging mode.
2190          *
2191          * If this is the upstream port for this switch, enable
2192          * forwarding of unknown unicasts and multicasts.
2193          */
2194         reg = 0;
2195         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2196             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2197             mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
2198             mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
2199                 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2200                 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2201                 PORT_CONTROL_STATE_FORWARDING;
2202         if (dsa_is_cpu_port(ds, port)) {
2203                 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2204                         reg |= PORT_CONTROL_DSA_TAG;
2205                 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2206                     mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2207                     mv88e6xxx_6320_family(ds)) {
2208                         if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2209                                 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2210                         else
2211                                 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2212                         reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2213                                 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2214                 }
2215
2216                 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2217                     mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2218                     mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
2219                     mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
2220                         if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2221                                 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2222                 }
2223         }
2224         if (dsa_is_dsa_port(ds, port)) {
2225                 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2226                         reg |= PORT_CONTROL_DSA_TAG;
2227                 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2228                     mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2229                     mv88e6xxx_6320_family(ds)) {
2230                         reg |= PORT_CONTROL_FRAME_MODE_DSA;
2231                 }
2232
2233                 if (port == dsa_upstream_port(ds))
2234                         reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2235                                 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2236         }
2237         if (reg) {
2238                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2239                                            PORT_CONTROL, reg);
2240                 if (ret)
2241                         goto abort;
2242         }
2243
2244         /* Port Control 2: don't force a good FCS, set the maximum frame size to
2245          * 10240 bytes, enable secure 802.1q tags, don't discard tagged or
2246          * untagged frames on this port, do a destination address lookup on all
2247          * received packets as usual, disable ARP mirroring and don't send a
2248          * copy of all transmitted/received frames on this port to the CPU.
2249          */
2250         reg = 0;
2251         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2252             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2253             mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds))
2254                 reg = PORT_CONTROL_2_MAP_DA;
2255
2256         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2257             mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
2258                 reg |= PORT_CONTROL_2_JUMBO_10240;
2259
2260         if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
2261                 /* Set the upstream port this port should use */
2262                 reg |= dsa_upstream_port(ds);
2263                 /* enable forwarding of unknown multicast addresses to
2264                  * the upstream port
2265                  */
2266                 if (port == dsa_upstream_port(ds))
2267                         reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2268         }
2269
2270         reg |= PORT_CONTROL_2_8021Q_SECURE;
2271
2272         if (reg) {
2273                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2274                                            PORT_CONTROL_2, reg);
2275                 if (ret)
2276                         goto abort;
2277         }
2278
2279         /* Port Association Vector: when learning source addresses
2280          * of packets, add the address to the address database using
2281          * a port bitmap that has only the bit for this port set and
2282          * the other bits clear.
2283          */
2284         reg = 1 << port;
2285         /* Disable learning for DSA and CPU ports */
2286         if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2287                 reg = PORT_ASSOC_VECTOR_LOCKED_PORT;
2288
2289         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
2290         if (ret)
2291                 goto abort;
2292
2293         /* Egress rate control 2: disable egress rate control. */
2294         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2295                                    0x0000);
2296         if (ret)
2297                 goto abort;
2298
2299         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2300             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2301             mv88e6xxx_6320_family(ds)) {
2302                 /* Do not limit the period of time that this port can
2303                  * be paused for by the remote end or the period of
2304                  * time that this port can pause the remote end.
2305                  */
2306                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2307                                            PORT_PAUSE_CTRL, 0x0000);
2308                 if (ret)
2309                         goto abort;
2310
2311                 /* Port ATU control: disable limiting the number of
2312                  * address database entries that this port is allowed
2313                  * to use.
2314                  */
2315                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2316                                            PORT_ATU_CONTROL, 0x0000);
2317                 /* Priority Override: disable DA, SA and VTU priority
2318                  * override.
2319                  */
2320                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2321                                            PORT_PRI_OVERRIDE, 0x0000);
2322                 if (ret)
2323                         goto abort;
2324
2325                 /* Port Ethertype: use the Ethertype DSA Ethertype
2326                  * value.
2327                  */
2328                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2329                                            PORT_ETH_TYPE, ETH_P_EDSA);
2330                 if (ret)
2331                         goto abort;
2332                 /* Tag Remap: use an identity 802.1p prio -> switch
2333                  * prio mapping.
2334                  */
2335                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2336                                            PORT_TAG_REGMAP_0123, 0x3210);
2337                 if (ret)
2338                         goto abort;
2339
2340                 /* Tag Remap 2: use an identity 802.1p prio -> switch
2341                  * prio mapping.
2342                  */
2343                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2344                                            PORT_TAG_REGMAP_4567, 0x7654);
2345                 if (ret)
2346                         goto abort;
2347         }
2348
2349         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2350             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2351             mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2352             mv88e6xxx_6320_family(ds)) {
2353                 /* Rate Control: disable ingress rate limiting. */
2354                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2355                                            PORT_RATE_CONTROL, 0x0001);
2356                 if (ret)
2357                         goto abort;
2358         }
2359
2360         /* Port Control 1: disable trunking, disable sending
2361          * learning messages to this port.
2362          */
2363         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
2364         if (ret)
2365                 goto abort;
2366
2367         /* Port based VLAN map: give each port its own address
2368          * database, and allow every port to egress frames on all other ports.
2369          */
2370         ret = _mv88e6xxx_port_fid_set(ds, port, port + 1);
2371         if (ret)
2372                 goto abort;
2373
2374         reg = BIT(ps->num_ports) - 1; /* all ports */
2375         reg &= ~BIT(port); /* except itself */
2376         ret = _mv88e6xxx_port_vlan_map_set(ds, port, reg);
2377         if (ret)
2378                 goto abort;
2379
2380         /* Default VLAN ID and priority: don't set a default VLAN
2381          * ID, and set the default packet priority to zero.
2382          */
2383         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2384                                    0x0000);
2385 abort:
2386         mutex_unlock(&ps->smi_mutex);
2387         return ret;
2388 }
2389
2390 int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2391 {
2392         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2393         int ret;
2394         int i;
2395
2396         for (i = 0; i < ps->num_ports; i++) {
2397                 ret = mv88e6xxx_setup_port(ds, i);
2398                 if (ret < 0)
2399                         return ret;
2400
2401                 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2402                         continue;
2403
2404                 ret = mv88e6xxx_setup_port_default_vlan(ds, i);
2405                 if (ret < 0)
2406                         return ret;
2407         }
2408         return 0;
2409 }
2410
2411 int mv88e6xxx_setup_common(struct dsa_switch *ds)
2412 {
2413         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2414
2415         mutex_init(&ps->smi_mutex);
2416
2417         ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
2418
2419         INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2420
2421         return 0;
2422 }
2423
2424 int mv88e6xxx_setup_global(struct dsa_switch *ds)
2425 {
2426         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2427         int ret;
2428         int i;
2429
2430         /* Set the default address aging time to 5 minutes, and
2431          * enable address learn messages to be sent to all message
2432          * ports.
2433          */
2434         REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
2435                   0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2436
2437         /* Configure the IP ToS mapping registers. */
2438         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2439         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2440         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2441         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2442         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2443         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2444         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2445         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2446
2447         /* Configure the IEEE 802.1p priority mapping register. */
2448         REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2449
2450         /* Send all frames with destination addresses matching
2451          * 01:80:c2:00:00:0x to the CPU port.
2452          */
2453         REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2454
2455         /* Ignore removed tag data on doubly tagged packets, disable
2456          * flow control messages, force flow control priority to the
2457          * highest, and send all special multicast frames to the CPU
2458          * port at the highest priority.
2459          */
2460         REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2461                   0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2462                   GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2463
2464         /* Program the DSA routing table. */
2465         for (i = 0; i < 32; i++) {
2466                 int nexthop = 0x1f;
2467
2468                 if (ds->pd->rtable &&
2469                     i != ds->index && i < ds->dst->pd->nr_chips)
2470                         nexthop = ds->pd->rtable[i] & 0x1f;
2471
2472                 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2473                           GLOBAL2_DEVICE_MAPPING_UPDATE |
2474                           (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
2475                           nexthop);
2476         }
2477
2478         /* Clear all trunk masks. */
2479         for (i = 0; i < 8; i++)
2480                 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2481                           0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
2482                           ((1 << ps->num_ports) - 1));
2483
2484         /* Clear all trunk mappings. */
2485         for (i = 0; i < 16; i++)
2486                 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
2487                           GLOBAL2_TRUNK_MAPPING_UPDATE |
2488                           (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2489
2490         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2491             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2492             mv88e6xxx_6320_family(ds)) {
2493                 /* Send all frames with destination addresses matching
2494                  * 01:80:c2:00:00:2x to the CPU port.
2495                  */
2496                 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2497
2498                 /* Initialise cross-chip port VLAN table to reset
2499                  * defaults.
2500                  */
2501                 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2502
2503                 /* Clear the priority override table. */
2504                 for (i = 0; i < 16; i++)
2505                         REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2506                                   0x8000 | (i << 8));
2507         }
2508
2509         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2510             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2511             mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2512             mv88e6xxx_6320_family(ds)) {
2513                 /* Disable ingress rate limiting by resetting all
2514                  * ingress rate limit registers to their initial
2515                  * state.
2516                  */
2517                 for (i = 0; i < ps->num_ports; i++)
2518                         REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2519                                   0x9000 | (i << 8));
2520         }
2521
2522         /* Clear the statistics counters for all ports */
2523         REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2524
2525         /* Wait for the flush to complete. */
2526         mutex_lock(&ps->smi_mutex);
2527         ret = _mv88e6xxx_stats_wait(ds);
2528         if (ret < 0)
2529                 goto unlock;
2530
2531         /* Clear all ATU entries */
2532         ret = _mv88e6xxx_atu_flush(ds, 0, true);
2533         if (ret < 0)
2534                 goto unlock;
2535
2536         /* Clear all the VTU and STU entries */
2537         ret = _mv88e6xxx_vtu_stu_flush(ds);
2538 unlock:
2539         mutex_unlock(&ps->smi_mutex);
2540
2541         return ret;
2542 }
2543
2544 int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2545 {
2546         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2547         u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2548         struct gpio_desc *gpiod = ds->pd->reset;
2549         unsigned long timeout;
2550         int ret;
2551         int i;
2552
2553         /* Set all ports to the disabled state. */
2554         for (i = 0; i < ps->num_ports; i++) {
2555                 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2556                 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
2557         }
2558
2559         /* Wait for transmit queues to drain. */
2560         usleep_range(2000, 4000);
2561
2562         /* If there is a gpio connected to the reset pin, toggle it */
2563         if (gpiod) {
2564                 gpiod_set_value_cansleep(gpiod, 1);
2565                 usleep_range(10000, 20000);
2566                 gpiod_set_value_cansleep(gpiod, 0);
2567                 usleep_range(10000, 20000);
2568         }
2569
2570         /* Reset the switch. Keep the PPU active if requested. The PPU
2571          * needs to be active to support indirect phy register access
2572          * through global registers 0x18 and 0x19.
2573          */
2574         if (ppu_active)
2575                 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2576         else
2577                 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2578
2579         /* Wait up to one second for reset to complete. */
2580         timeout = jiffies + 1 * HZ;
2581         while (time_before(jiffies, timeout)) {
2582                 ret = REG_READ(REG_GLOBAL, 0x00);
2583                 if ((ret & is_reset) == is_reset)
2584                         break;
2585                 usleep_range(1000, 2000);
2586         }
2587         if (time_after(jiffies, timeout))
2588                 return -ETIMEDOUT;
2589
2590         return 0;
2591 }
2592
2593 int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2594 {
2595         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2596         int ret;
2597
2598         mutex_lock(&ps->smi_mutex);
2599         ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2600         if (ret < 0)
2601                 goto error;
2602         ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
2603 error:
2604         _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2605         mutex_unlock(&ps->smi_mutex);
2606         return ret;
2607 }
2608
2609 int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2610                              int reg, int val)
2611 {
2612         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2613         int ret;
2614
2615         mutex_lock(&ps->smi_mutex);
2616         ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2617         if (ret < 0)
2618                 goto error;
2619
2620         ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
2621 error:
2622         _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2623         mutex_unlock(&ps->smi_mutex);
2624         return ret;
2625 }
2626
2627 static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2628 {
2629         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2630
2631         if (port >= 0 && port < ps->num_ports)
2632                 return port;
2633         return -EINVAL;
2634 }
2635
2636 int
2637 mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2638 {
2639         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2640         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2641         int ret;
2642
2643         if (addr < 0)
2644                 return addr;
2645
2646         mutex_lock(&ps->smi_mutex);
2647         ret = _mv88e6xxx_phy_read(ds, addr, regnum);
2648         mutex_unlock(&ps->smi_mutex);
2649         return ret;
2650 }
2651
2652 int
2653 mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2654 {
2655         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2656         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2657         int ret;
2658
2659         if (addr < 0)
2660                 return addr;
2661
2662         mutex_lock(&ps->smi_mutex);
2663         ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
2664         mutex_unlock(&ps->smi_mutex);
2665         return ret;
2666 }
2667
2668 int
2669 mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2670 {
2671         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2672         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2673         int ret;
2674
2675         if (addr < 0)
2676                 return addr;
2677
2678         mutex_lock(&ps->smi_mutex);
2679         ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
2680         mutex_unlock(&ps->smi_mutex);
2681         return ret;
2682 }
2683
2684 int
2685 mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2686                              u16 val)
2687 {
2688         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2689         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2690         int ret;
2691
2692         if (addr < 0)
2693                 return addr;
2694
2695         mutex_lock(&ps->smi_mutex);
2696         ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
2697         mutex_unlock(&ps->smi_mutex);
2698         return ret;
2699 }
2700
2701 #ifdef CONFIG_NET_DSA_HWMON
2702
2703 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2704 {
2705         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2706         int ret;
2707         int val;
2708
2709         *temp = 0;
2710
2711         mutex_lock(&ps->smi_mutex);
2712
2713         ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
2714         if (ret < 0)
2715                 goto error;
2716
2717         /* Enable temperature sensor */
2718         ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2719         if (ret < 0)
2720                 goto error;
2721
2722         ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
2723         if (ret < 0)
2724                 goto error;
2725
2726         /* Wait for temperature to stabilize */
2727         usleep_range(10000, 12000);
2728
2729         val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2730         if (val < 0) {
2731                 ret = val;
2732                 goto error;
2733         }
2734
2735         /* Disable temperature sensor */
2736         ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
2737         if (ret < 0)
2738                 goto error;
2739
2740         *temp = ((val & 0x1f) - 5) * 5;
2741
2742 error:
2743         _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
2744         mutex_unlock(&ps->smi_mutex);
2745         return ret;
2746 }
2747
2748 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2749 {
2750         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2751         int ret;
2752
2753         *temp = 0;
2754
2755         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
2756         if (ret < 0)
2757                 return ret;
2758
2759         *temp = (ret & 0xff) - 25;
2760
2761         return 0;
2762 }
2763
2764 int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
2765 {
2766         if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
2767                 return mv88e63xx_get_temp(ds, temp);
2768
2769         return mv88e61xx_get_temp(ds, temp);
2770 }
2771
2772 int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
2773 {
2774         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2775         int ret;
2776
2777         if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2778                 return -EOPNOTSUPP;
2779
2780         *temp = 0;
2781
2782         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2783         if (ret < 0)
2784                 return ret;
2785
2786         *temp = (((ret >> 8) & 0x1f) * 5) - 25;
2787
2788         return 0;
2789 }
2790
2791 int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
2792 {
2793         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2794         int ret;
2795
2796         if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2797                 return -EOPNOTSUPP;
2798
2799         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2800         if (ret < 0)
2801                 return ret;
2802         temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
2803         return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
2804                                         (ret & 0xe0ff) | (temp << 8));
2805 }
2806
2807 int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
2808 {
2809         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2810         int ret;
2811
2812         if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2813                 return -EOPNOTSUPP;
2814
2815         *alarm = false;
2816
2817         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2818         if (ret < 0)
2819                 return ret;
2820
2821         *alarm = !!(ret & 0x40);
2822
2823         return 0;
2824 }
2825 #endif /* CONFIG_NET_DSA_HWMON */
2826
2827 char *mv88e6xxx_lookup_name(struct device *host_dev, int sw_addr,
2828                             const struct mv88e6xxx_switch_id *table,
2829                             unsigned int num)
2830 {
2831         struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
2832         int i, ret;
2833
2834         if (!bus)
2835                 return NULL;
2836
2837         ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
2838         if (ret < 0)
2839                 return NULL;
2840
2841         /* Look up the exact switch ID */
2842         for (i = 0; i < num; ++i)
2843                 if (table[i].id == ret)
2844                         return table[i].name;
2845
2846         /* Look up only the product number */
2847         for (i = 0; i < num; ++i) {
2848                 if (table[i].id == (ret & PORT_SWITCH_ID_PROD_NUM_MASK)) {
2849                         dev_warn(host_dev, "unknown revision %d, using base switch 0x%x\n",
2850                                  ret & PORT_SWITCH_ID_REV_MASK,
2851                                  ret & PORT_SWITCH_ID_PROD_NUM_MASK);
2852                         return table[i].name;
2853                 }
2854         }
2855
2856         return NULL;
2857 }
2858
2859 static int __init mv88e6xxx_init(void)
2860 {
2861 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2862         register_switch_driver(&mv88e6131_switch_driver);
2863 #endif
2864 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2865         register_switch_driver(&mv88e6123_61_65_switch_driver);
2866 #endif
2867 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2868         register_switch_driver(&mv88e6352_switch_driver);
2869 #endif
2870 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2871         register_switch_driver(&mv88e6171_switch_driver);
2872 #endif
2873         return 0;
2874 }
2875 module_init(mv88e6xxx_init);
2876
2877 static void __exit mv88e6xxx_cleanup(void)
2878 {
2879 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2880         unregister_switch_driver(&mv88e6171_switch_driver);
2881 #endif
2882 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2883         unregister_switch_driver(&mv88e6352_switch_driver);
2884 #endif
2885 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2886         unregister_switch_driver(&mv88e6123_61_65_switch_driver);
2887 #endif
2888 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2889         unregister_switch_driver(&mv88e6131_switch_driver);
2890 #endif
2891 }
2892 module_exit(mv88e6xxx_cleanup);
2893
2894 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
2895 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
2896 MODULE_LICENSE("GPL");