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