OSDN Git Service

Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[android-x86/kernel.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  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/etherdevice.h>
14 #include <linux/if_bridge.h>
15 #include <linux/jiffies.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/phy.h>
20 #include <linux/seq_file.h>
21 #include <net/dsa.h>
22 #include "mv88e6xxx.h"
23
24 /* MDIO bus access can be nested in the case of PHYs connected to the
25  * internal MDIO bus of the switch, which is accessed via MDIO bus of
26  * the Ethernet interface. Avoid lockdep false positives by using
27  * mutex_lock_nested().
28  */
29 static int mv88e6xxx_mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
30 {
31         int ret;
32
33         mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
34         ret = bus->read(bus, addr, regnum);
35         mutex_unlock(&bus->mdio_lock);
36
37         return ret;
38 }
39
40 static int mv88e6xxx_mdiobus_write(struct mii_bus *bus, int addr, u32 regnum,
41                                    u16 val)
42 {
43         int ret;
44
45         mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
46         ret = bus->write(bus, addr, regnum, val);
47         mutex_unlock(&bus->mdio_lock);
48
49         return ret;
50 }
51
52 /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
53  * use all 32 SMI bus addresses on its SMI bus, and all switch registers
54  * will be directly accessible on some {device address,register address}
55  * pair.  If the ADDR[4:0] pins are not strapped to zero, the switch
56  * will only respond to SMI transactions to that specific address, and
57  * an indirect addressing mechanism needs to be used to access its
58  * registers.
59  */
60 static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
61 {
62         int ret;
63         int i;
64
65         for (i = 0; i < 16; i++) {
66                 ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_CMD);
67                 if (ret < 0)
68                         return ret;
69
70                 if ((ret & SMI_CMD_BUSY) == 0)
71                         return 0;
72         }
73
74         return -ETIMEDOUT;
75 }
76
77 int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
78 {
79         int ret;
80
81         if (sw_addr == 0)
82                 return mv88e6xxx_mdiobus_read(bus, addr, reg);
83
84         /* Wait for the bus to become free. */
85         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
86         if (ret < 0)
87                 return ret;
88
89         /* Transmit the read command. */
90         ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
91                                       SMI_CMD_OP_22_READ | (addr << 5) | reg);
92         if (ret < 0)
93                 return ret;
94
95         /* Wait for the read command to complete. */
96         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
97         if (ret < 0)
98                 return ret;
99
100         /* Read the data. */
101         ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_DATA);
102         if (ret < 0)
103                 return ret;
104
105         return ret & 0xffff;
106 }
107
108 /* Must be called with SMI mutex held */
109 static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
110 {
111         struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
112         int ret;
113
114         if (bus == NULL)
115                 return -EINVAL;
116
117         ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
118         if (ret < 0)
119                 return ret;
120
121         dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
122                 addr, reg, ret);
123
124         return ret;
125 }
126
127 int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
128 {
129         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
130         int ret;
131
132         mutex_lock(&ps->smi_mutex);
133         ret = _mv88e6xxx_reg_read(ds, addr, reg);
134         mutex_unlock(&ps->smi_mutex);
135
136         return ret;
137 }
138
139 int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
140                           int reg, u16 val)
141 {
142         int ret;
143
144         if (sw_addr == 0)
145                 return mv88e6xxx_mdiobus_write(bus, addr, reg, val);
146
147         /* Wait for the bus to become free. */
148         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
149         if (ret < 0)
150                 return ret;
151
152         /* Transmit the data to write. */
153         ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_DATA, val);
154         if (ret < 0)
155                 return ret;
156
157         /* Transmit the write command. */
158         ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
159                                       SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
160         if (ret < 0)
161                 return ret;
162
163         /* Wait for the write command to complete. */
164         ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
165         if (ret < 0)
166                 return ret;
167
168         return 0;
169 }
170
171 /* Must be called with SMI mutex held */
172 static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
173                                 u16 val)
174 {
175         struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
176
177         if (bus == NULL)
178                 return -EINVAL;
179
180         dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
181                 addr, reg, val);
182
183         return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
184 }
185
186 int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
187 {
188         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
189         int ret;
190
191         mutex_lock(&ps->smi_mutex);
192         ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
193         mutex_unlock(&ps->smi_mutex);
194
195         return ret;
196 }
197
198 int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
199 {
200         REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
201         REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
202         REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
203
204         return 0;
205 }
206
207 int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
208 {
209         int i;
210         int ret;
211
212         for (i = 0; i < 6; i++) {
213                 int j;
214
215                 /* Write the MAC address byte. */
216                 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
217                           GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
218
219                 /* Wait for the write to complete. */
220                 for (j = 0; j < 16; j++) {
221                         ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
222                         if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
223                                 break;
224                 }
225                 if (j == 16)
226                         return -ETIMEDOUT;
227         }
228
229         return 0;
230 }
231
232 /* Must be called with SMI mutex held */
233 static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
234 {
235         if (addr >= 0)
236                 return _mv88e6xxx_reg_read(ds, addr, regnum);
237         return 0xffff;
238 }
239
240 /* Must be called with SMI mutex held */
241 static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
242                                 u16 val)
243 {
244         if (addr >= 0)
245                 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
246         return 0;
247 }
248
249 #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
250 static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
251 {
252         int ret;
253         unsigned long timeout;
254
255         ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
256         REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
257                   ret & ~GLOBAL_CONTROL_PPU_ENABLE);
258
259         timeout = jiffies + 1 * HZ;
260         while (time_before(jiffies, timeout)) {
261                 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
262                 usleep_range(1000, 2000);
263                 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
264                     GLOBAL_STATUS_PPU_POLLING)
265                         return 0;
266         }
267
268         return -ETIMEDOUT;
269 }
270
271 static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
272 {
273         int ret;
274         unsigned long timeout;
275
276         ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
277         REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
278
279         timeout = jiffies + 1 * HZ;
280         while (time_before(jiffies, timeout)) {
281                 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
282                 usleep_range(1000, 2000);
283                 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
284                     GLOBAL_STATUS_PPU_POLLING)
285                         return 0;
286         }
287
288         return -ETIMEDOUT;
289 }
290
291 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
292 {
293         struct mv88e6xxx_priv_state *ps;
294
295         ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
296         if (mutex_trylock(&ps->ppu_mutex)) {
297                 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
298
299                 if (mv88e6xxx_ppu_enable(ds) == 0)
300                         ps->ppu_disabled = 0;
301                 mutex_unlock(&ps->ppu_mutex);
302         }
303 }
304
305 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
306 {
307         struct mv88e6xxx_priv_state *ps = (void *)_ps;
308
309         schedule_work(&ps->ppu_work);
310 }
311
312 static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
313 {
314         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
315         int ret;
316
317         mutex_lock(&ps->ppu_mutex);
318
319         /* If the PHY polling unit is enabled, disable it so that
320          * we can access the PHY registers.  If it was already
321          * disabled, cancel the timer that is going to re-enable
322          * it.
323          */
324         if (!ps->ppu_disabled) {
325                 ret = mv88e6xxx_ppu_disable(ds);
326                 if (ret < 0) {
327                         mutex_unlock(&ps->ppu_mutex);
328                         return ret;
329                 }
330                 ps->ppu_disabled = 1;
331         } else {
332                 del_timer(&ps->ppu_timer);
333                 ret = 0;
334         }
335
336         return ret;
337 }
338
339 static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
340 {
341         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
342
343         /* Schedule a timer to re-enable the PHY polling unit. */
344         mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
345         mutex_unlock(&ps->ppu_mutex);
346 }
347
348 void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
349 {
350         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
351
352         mutex_init(&ps->ppu_mutex);
353         INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
354         init_timer(&ps->ppu_timer);
355         ps->ppu_timer.data = (unsigned long)ps;
356         ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
357 }
358
359 int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
360 {
361         int ret;
362
363         ret = mv88e6xxx_ppu_access_get(ds);
364         if (ret >= 0) {
365                 ret = mv88e6xxx_reg_read(ds, addr, regnum);
366                 mv88e6xxx_ppu_access_put(ds);
367         }
368
369         return ret;
370 }
371
372 int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
373                             int regnum, u16 val)
374 {
375         int ret;
376
377         ret = mv88e6xxx_ppu_access_get(ds);
378         if (ret >= 0) {
379                 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
380                 mv88e6xxx_ppu_access_put(ds);
381         }
382
383         return ret;
384 }
385 #endif
386
387 void mv88e6xxx_poll_link(struct dsa_switch *ds)
388 {
389         int i;
390
391         for (i = 0; i < DSA_MAX_PORTS; i++) {
392                 struct net_device *dev;
393                 int uninitialized_var(port_status);
394                 int link;
395                 int speed;
396                 int duplex;
397                 int fc;
398
399                 dev = ds->ports[i];
400                 if (dev == NULL)
401                         continue;
402
403                 link = 0;
404                 if (dev->flags & IFF_UP) {
405                         port_status = mv88e6xxx_reg_read(ds, REG_PORT(i),
406                                                          PORT_STATUS);
407                         if (port_status < 0)
408                                 continue;
409
410                         link = !!(port_status & PORT_STATUS_LINK);
411                 }
412
413                 if (!link) {
414                         if (netif_carrier_ok(dev)) {
415                                 netdev_info(dev, "link down\n");
416                                 netif_carrier_off(dev);
417                         }
418                         continue;
419                 }
420
421                 switch (port_status & PORT_STATUS_SPEED_MASK) {
422                 case PORT_STATUS_SPEED_10:
423                         speed = 10;
424                         break;
425                 case PORT_STATUS_SPEED_100:
426                         speed = 100;
427                         break;
428                 case PORT_STATUS_SPEED_1000:
429                         speed = 1000;
430                         break;
431                 default:
432                         speed = -1;
433                         break;
434                 }
435                 duplex = (port_status & PORT_STATUS_DUPLEX) ? 1 : 0;
436                 fc = (port_status & PORT_STATUS_PAUSE_EN) ? 1 : 0;
437
438                 if (!netif_carrier_ok(dev)) {
439                         netdev_info(dev,
440                                     "link up, %d Mb/s, %s duplex, flow control %sabled\n",
441                                     speed,
442                                     duplex ? "full" : "half",
443                                     fc ? "en" : "dis");
444                         netif_carrier_on(dev);
445                 }
446         }
447 }
448
449 static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
450 {
451         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
452
453         switch (ps->id) {
454         case PORT_SWITCH_ID_6031:
455         case PORT_SWITCH_ID_6061:
456         case PORT_SWITCH_ID_6035:
457         case PORT_SWITCH_ID_6065:
458                 return true;
459         }
460         return false;
461 }
462
463 static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
464 {
465         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
466
467         switch (ps->id) {
468         case PORT_SWITCH_ID_6092:
469         case PORT_SWITCH_ID_6095:
470                 return true;
471         }
472         return false;
473 }
474
475 static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
476 {
477         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
478
479         switch (ps->id) {
480         case PORT_SWITCH_ID_6046:
481         case PORT_SWITCH_ID_6085:
482         case PORT_SWITCH_ID_6096:
483         case PORT_SWITCH_ID_6097:
484                 return true;
485         }
486         return false;
487 }
488
489 static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
490 {
491         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
492
493         switch (ps->id) {
494         case PORT_SWITCH_ID_6123:
495         case PORT_SWITCH_ID_6161:
496         case PORT_SWITCH_ID_6165:
497                 return true;
498         }
499         return false;
500 }
501
502 static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
503 {
504         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
505
506         switch (ps->id) {
507         case PORT_SWITCH_ID_6121:
508         case PORT_SWITCH_ID_6122:
509         case PORT_SWITCH_ID_6152:
510         case PORT_SWITCH_ID_6155:
511         case PORT_SWITCH_ID_6182:
512         case PORT_SWITCH_ID_6185:
513         case PORT_SWITCH_ID_6108:
514         case PORT_SWITCH_ID_6131:
515                 return true;
516         }
517         return false;
518 }
519
520 bool mv88e6xxx_6320_family(struct dsa_switch *ds)
521 {
522         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
523
524         switch (ps->id) {
525         case PORT_SWITCH_ID_6320:
526         case PORT_SWITCH_ID_6321:
527                 return true;
528         }
529         return false;
530 }
531
532 static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
533 {
534         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
535
536         switch (ps->id) {
537         case PORT_SWITCH_ID_6171:
538         case PORT_SWITCH_ID_6175:
539         case PORT_SWITCH_ID_6350:
540         case PORT_SWITCH_ID_6351:
541                 return true;
542         }
543         return false;
544 }
545
546 static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
547 {
548         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
549
550         switch (ps->id) {
551         case PORT_SWITCH_ID_6172:
552         case PORT_SWITCH_ID_6176:
553         case PORT_SWITCH_ID_6240:
554         case PORT_SWITCH_ID_6352:
555                 return true;
556         }
557         return false;
558 }
559
560 /* Must be called with SMI mutex held */
561 static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
562 {
563         int ret;
564         int i;
565
566         for (i = 0; i < 10; i++) {
567                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
568                 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
569                         return 0;
570         }
571
572         return -ETIMEDOUT;
573 }
574
575 /* Must be called with SMI mutex held */
576 static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
577 {
578         int ret;
579
580         if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
581                 port = (port + 1) << 5;
582
583         /* Snapshot the hardware statistics counters for this port. */
584         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
585                                    GLOBAL_STATS_OP_CAPTURE_PORT |
586                                    GLOBAL_STATS_OP_HIST_RX_TX | port);
587         if (ret < 0)
588                 return ret;
589
590         /* Wait for the snapshotting to complete. */
591         ret = _mv88e6xxx_stats_wait(ds);
592         if (ret < 0)
593                 return ret;
594
595         return 0;
596 }
597
598 /* Must be called with SMI mutex held */
599 static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
600 {
601         u32 _val;
602         int ret;
603
604         *val = 0;
605
606         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
607                                    GLOBAL_STATS_OP_READ_CAPTURED |
608                                    GLOBAL_STATS_OP_HIST_RX_TX | stat);
609         if (ret < 0)
610                 return;
611
612         ret = _mv88e6xxx_stats_wait(ds);
613         if (ret < 0)
614                 return;
615
616         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
617         if (ret < 0)
618                 return;
619
620         _val = ret << 16;
621
622         ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
623         if (ret < 0)
624                 return;
625
626         *val = _val | ret;
627 }
628
629 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
630         { "in_good_octets", 8, 0x00, },
631         { "in_bad_octets", 4, 0x02, },
632         { "in_unicast", 4, 0x04, },
633         { "in_broadcasts", 4, 0x06, },
634         { "in_multicasts", 4, 0x07, },
635         { "in_pause", 4, 0x16, },
636         { "in_undersize", 4, 0x18, },
637         { "in_fragments", 4, 0x19, },
638         { "in_oversize", 4, 0x1a, },
639         { "in_jabber", 4, 0x1b, },
640         { "in_rx_error", 4, 0x1c, },
641         { "in_fcs_error", 4, 0x1d, },
642         { "out_octets", 8, 0x0e, },
643         { "out_unicast", 4, 0x10, },
644         { "out_broadcasts", 4, 0x13, },
645         { "out_multicasts", 4, 0x12, },
646         { "out_pause", 4, 0x15, },
647         { "excessive", 4, 0x11, },
648         { "collisions", 4, 0x1e, },
649         { "deferred", 4, 0x05, },
650         { "single", 4, 0x14, },
651         { "multiple", 4, 0x17, },
652         { "out_fcs_error", 4, 0x03, },
653         { "late", 4, 0x1f, },
654         { "hist_64bytes", 4, 0x08, },
655         { "hist_65_127bytes", 4, 0x09, },
656         { "hist_128_255bytes", 4, 0x0a, },
657         { "hist_256_511bytes", 4, 0x0b, },
658         { "hist_512_1023bytes", 4, 0x0c, },
659         { "hist_1024_max_bytes", 4, 0x0d, },
660         /* Not all devices have the following counters */
661         { "sw_in_discards", 4, 0x110, },
662         { "sw_in_filtered", 2, 0x112, },
663         { "sw_out_filtered", 2, 0x113, },
664
665 };
666
667 static bool have_sw_in_discards(struct dsa_switch *ds)
668 {
669         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
670
671         switch (ps->id) {
672         case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161:
673         case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171:
674         case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176:
675         case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185:
676         case PORT_SWITCH_ID_6352:
677                 return true;
678         default:
679                 return false;
680         }
681 }
682
683 static void _mv88e6xxx_get_strings(struct dsa_switch *ds,
684                                    int nr_stats,
685                                    struct mv88e6xxx_hw_stat *stats,
686                                    int port, uint8_t *data)
687 {
688         int i;
689
690         for (i = 0; i < nr_stats; i++) {
691                 memcpy(data + i * ETH_GSTRING_LEN,
692                        stats[i].string, ETH_GSTRING_LEN);
693         }
694 }
695
696 static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
697                                             int stat,
698                                             struct mv88e6xxx_hw_stat *stats,
699                                             int port)
700 {
701         struct mv88e6xxx_hw_stat *s = stats + stat;
702         u32 low;
703         u32 high = 0;
704         int ret;
705         u64 value;
706
707         if (s->reg >= 0x100) {
708                 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
709                                           s->reg - 0x100);
710                 if (ret < 0)
711                         return UINT64_MAX;
712
713                 low = ret;
714                 if (s->sizeof_stat == 4) {
715                         ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
716                                                   s->reg - 0x100 + 1);
717                         if (ret < 0)
718                                 return UINT64_MAX;
719                         high = ret;
720                 }
721         } else {
722                 _mv88e6xxx_stats_read(ds, s->reg, &low);
723                 if (s->sizeof_stat == 8)
724                         _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
725         }
726         value = (((u64)high) << 16) | low;
727         return value;
728 }
729
730 static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
731                                          int nr_stats,
732                                          struct mv88e6xxx_hw_stat *stats,
733                                          int port, uint64_t *data)
734 {
735         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
736         int ret;
737         int i;
738
739         mutex_lock(&ps->smi_mutex);
740
741         ret = _mv88e6xxx_stats_snapshot(ds, port);
742         if (ret < 0) {
743                 mutex_unlock(&ps->smi_mutex);
744                 return;
745         }
746
747         /* Read each of the counters. */
748         for (i = 0; i < nr_stats; i++)
749                 data[i] = _mv88e6xxx_get_ethtool_stat(ds, i, stats, port);
750
751         mutex_unlock(&ps->smi_mutex);
752 }
753
754 /* All the statistics in the table */
755 void
756 mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
757 {
758         if (have_sw_in_discards(ds))
759                 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
760                                        mv88e6xxx_hw_stats, port, data);
761         else
762                 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
763                                        mv88e6xxx_hw_stats, port, data);
764 }
765
766 int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
767 {
768         if (have_sw_in_discards(ds))
769                 return ARRAY_SIZE(mv88e6xxx_hw_stats);
770         return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
771 }
772
773 void
774 mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
775                             int port, uint64_t *data)
776 {
777         if (have_sw_in_discards(ds))
778                 _mv88e6xxx_get_ethtool_stats(
779                         ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
780                         mv88e6xxx_hw_stats, port, data);
781         else
782                 _mv88e6xxx_get_ethtool_stats(
783                         ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
784                         mv88e6xxx_hw_stats, port, data);
785 }
786
787 int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
788 {
789         return 32 * sizeof(u16);
790 }
791
792 void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
793                         struct ethtool_regs *regs, void *_p)
794 {
795         u16 *p = _p;
796         int i;
797
798         regs->version = 0;
799
800         memset(p, 0xff, 32 * sizeof(u16));
801
802         for (i = 0; i < 32; i++) {
803                 int ret;
804
805                 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
806                 if (ret >= 0)
807                         p[i] = ret;
808         }
809 }
810
811 #ifdef CONFIG_NET_DSA_HWMON
812
813 int  mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
814 {
815         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
816         int ret;
817         int val;
818
819         *temp = 0;
820
821         mutex_lock(&ps->smi_mutex);
822
823         ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
824         if (ret < 0)
825                 goto error;
826
827         /* Enable temperature sensor */
828         ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
829         if (ret < 0)
830                 goto error;
831
832         ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
833         if (ret < 0)
834                 goto error;
835
836         /* Wait for temperature to stabilize */
837         usleep_range(10000, 12000);
838
839         val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
840         if (val < 0) {
841                 ret = val;
842                 goto error;
843         }
844
845         /* Disable temperature sensor */
846         ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
847         if (ret < 0)
848                 goto error;
849
850         *temp = ((val & 0x1f) - 5) * 5;
851
852 error:
853         _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
854         mutex_unlock(&ps->smi_mutex);
855         return ret;
856 }
857 #endif /* CONFIG_NET_DSA_HWMON */
858
859 /* Must be called with SMI lock held */
860 static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
861                            u16 mask)
862 {
863         unsigned long timeout = jiffies + HZ / 10;
864
865         while (time_before(jiffies, timeout)) {
866                 int ret;
867
868                 ret = _mv88e6xxx_reg_read(ds, reg, offset);
869                 if (ret < 0)
870                         return ret;
871                 if (!(ret & mask))
872                         return 0;
873
874                 usleep_range(1000, 2000);
875         }
876         return -ETIMEDOUT;
877 }
878
879 static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
880 {
881         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
882         int ret;
883
884         mutex_lock(&ps->smi_mutex);
885         ret = _mv88e6xxx_wait(ds, reg, offset, mask);
886         mutex_unlock(&ps->smi_mutex);
887
888         return ret;
889 }
890
891 static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
892 {
893         return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
894                                GLOBAL2_SMI_OP_BUSY);
895 }
896
897 int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
898 {
899         return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
900                               GLOBAL2_EEPROM_OP_LOAD);
901 }
902
903 int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
904 {
905         return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
906                               GLOBAL2_EEPROM_OP_BUSY);
907 }
908
909 /* Must be called with SMI lock held */
910 static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
911 {
912         return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
913                                GLOBAL_ATU_OP_BUSY);
914 }
915
916 /* Must be called with SMI lock held */
917 static int _mv88e6xxx_scratch_wait(struct dsa_switch *ds)
918 {
919         return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC,
920                                GLOBAL2_SCRATCH_BUSY);
921 }
922
923 /* Must be called with SMI mutex held */
924 static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
925                                         int regnum)
926 {
927         int ret;
928
929         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
930                                    GLOBAL2_SMI_OP_22_READ | (addr << 5) |
931                                    regnum);
932         if (ret < 0)
933                 return ret;
934
935         ret = _mv88e6xxx_phy_wait(ds);
936         if (ret < 0)
937                 return ret;
938
939         return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
940 }
941
942 /* Must be called with SMI mutex held */
943 static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
944                                          int regnum, u16 val)
945 {
946         int ret;
947
948         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
949         if (ret < 0)
950                 return ret;
951
952         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
953                                    GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
954                                    regnum);
955
956         return _mv88e6xxx_phy_wait(ds);
957 }
958
959 int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
960 {
961         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
962         int reg;
963
964         mutex_lock(&ps->smi_mutex);
965
966         reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
967         if (reg < 0)
968                 goto out;
969
970         e->eee_enabled = !!(reg & 0x0200);
971         e->tx_lpi_enabled = !!(reg & 0x0100);
972
973         reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
974         if (reg < 0)
975                 goto out;
976
977         e->eee_active = !!(reg & PORT_STATUS_EEE);
978         reg = 0;
979
980 out:
981         mutex_unlock(&ps->smi_mutex);
982         return reg;
983 }
984
985 int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
986                       struct phy_device *phydev, struct ethtool_eee *e)
987 {
988         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
989         int reg;
990         int ret;
991
992         mutex_lock(&ps->smi_mutex);
993
994         ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
995         if (ret < 0)
996                 goto out;
997
998         reg = ret & ~0x0300;
999         if (e->eee_enabled)
1000                 reg |= 0x0200;
1001         if (e->tx_lpi_enabled)
1002                 reg |= 0x0100;
1003
1004         ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
1005 out:
1006         mutex_unlock(&ps->smi_mutex);
1007
1008         return ret;
1009 }
1010
1011 static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, int fid, u16 cmd)
1012 {
1013         int ret;
1014
1015         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x01, fid);
1016         if (ret < 0)
1017                 return ret;
1018
1019         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1020         if (ret < 0)
1021                 return ret;
1022
1023         return _mv88e6xxx_atu_wait(ds);
1024 }
1025
1026 static int _mv88e6xxx_flush_fid(struct dsa_switch *ds, int fid)
1027 {
1028         int ret;
1029
1030         ret = _mv88e6xxx_atu_wait(ds);
1031         if (ret < 0)
1032                 return ret;
1033
1034         return _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_FLUSH_NON_STATIC_DB);
1035 }
1036
1037 static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
1038 {
1039         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1040         int reg, ret = 0;
1041         u8 oldstate;
1042
1043         mutex_lock(&ps->smi_mutex);
1044
1045         reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
1046         if (reg < 0) {
1047                 ret = reg;
1048                 goto abort;
1049         }
1050
1051         oldstate = reg & PORT_CONTROL_STATE_MASK;
1052         if (oldstate != state) {
1053                 /* Flush forwarding database if we're moving a port
1054                  * from Learning or Forwarding state to Disabled or
1055                  * Blocking or Listening state.
1056                  */
1057                 if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
1058                     state <= PORT_CONTROL_STATE_BLOCKING) {
1059                         ret = _mv88e6xxx_flush_fid(ds, ps->fid[port]);
1060                         if (ret)
1061                                 goto abort;
1062                 }
1063                 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1064                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1065                                            reg);
1066         }
1067
1068 abort:
1069         mutex_unlock(&ps->smi_mutex);
1070         return ret;
1071 }
1072
1073 /* Must be called with smi lock held */
1074 static int _mv88e6xxx_update_port_config(struct dsa_switch *ds, int port)
1075 {
1076         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1077         u8 fid = ps->fid[port];
1078         u16 reg = fid << 12;
1079
1080         if (dsa_is_cpu_port(ds, port))
1081                 reg |= ds->phys_port_mask;
1082         else
1083                 reg |= (ps->bridge_mask[fid] |
1084                        (1 << dsa_upstream_port(ds))) & ~(1 << port);
1085
1086         return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
1087 }
1088
1089 /* Must be called with smi lock held */
1090 static int _mv88e6xxx_update_bridge_config(struct dsa_switch *ds, int fid)
1091 {
1092         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1093         int port;
1094         u32 mask;
1095         int ret;
1096
1097         mask = ds->phys_port_mask;
1098         while (mask) {
1099                 port = __ffs(mask);
1100                 mask &= ~(1 << port);
1101                 if (ps->fid[port] != fid)
1102                         continue;
1103
1104                 ret = _mv88e6xxx_update_port_config(ds, port);
1105                 if (ret)
1106                         return ret;
1107         }
1108
1109         return _mv88e6xxx_flush_fid(ds, fid);
1110 }
1111
1112 /* Bridge handling functions */
1113
1114 int mv88e6xxx_join_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
1115 {
1116         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1117         int ret = 0;
1118         u32 nmask;
1119         int fid;
1120
1121         /* If the bridge group is not empty, join that group.
1122          * Otherwise create a new group.
1123          */
1124         fid = ps->fid[port];
1125         nmask = br_port_mask & ~(1 << port);
1126         if (nmask)
1127                 fid = ps->fid[__ffs(nmask)];
1128
1129         nmask = ps->bridge_mask[fid] | (1 << port);
1130         if (nmask != br_port_mask) {
1131                 netdev_err(ds->ports[port],
1132                            "join: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
1133                            fid, br_port_mask, nmask);
1134                 return -EINVAL;
1135         }
1136
1137         mutex_lock(&ps->smi_mutex);
1138
1139         ps->bridge_mask[fid] = br_port_mask;
1140
1141         if (fid != ps->fid[port]) {
1142                 ps->fid_mask |= 1 << ps->fid[port];
1143                 ps->fid[port] = fid;
1144                 ret = _mv88e6xxx_update_bridge_config(ds, fid);
1145         }
1146
1147         mutex_unlock(&ps->smi_mutex);
1148
1149         return ret;
1150 }
1151
1152 int mv88e6xxx_leave_bridge(struct dsa_switch *ds, int port, u32 br_port_mask)
1153 {
1154         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1155         u8 fid, newfid;
1156         int ret;
1157
1158         fid = ps->fid[port];
1159
1160         if (ps->bridge_mask[fid] != br_port_mask) {
1161                 netdev_err(ds->ports[port],
1162                            "leave: Bridge port mask mismatch fid=%d mask=0x%x expected 0x%x\n",
1163                            fid, br_port_mask, ps->bridge_mask[fid]);
1164                 return -EINVAL;
1165         }
1166
1167         /* If the port was the last port of a bridge, we are done.
1168          * Otherwise assign a new fid to the port, and fix up
1169          * the bridge configuration.
1170          */
1171         if (br_port_mask == (1 << port))
1172                 return 0;
1173
1174         mutex_lock(&ps->smi_mutex);
1175
1176         newfid = __ffs(ps->fid_mask);
1177         ps->fid[port] = newfid;
1178         ps->fid_mask &= ~(1 << newfid);
1179         ps->bridge_mask[fid] &= ~(1 << port);
1180         ps->bridge_mask[newfid] = 1 << port;
1181
1182         ret = _mv88e6xxx_update_bridge_config(ds, fid);
1183         if (!ret)
1184                 ret = _mv88e6xxx_update_bridge_config(ds, newfid);
1185
1186         mutex_unlock(&ps->smi_mutex);
1187
1188         return ret;
1189 }
1190
1191 int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1192 {
1193         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1194         int stp_state;
1195
1196         switch (state) {
1197         case BR_STATE_DISABLED:
1198                 stp_state = PORT_CONTROL_STATE_DISABLED;
1199                 break;
1200         case BR_STATE_BLOCKING:
1201         case BR_STATE_LISTENING:
1202                 stp_state = PORT_CONTROL_STATE_BLOCKING;
1203                 break;
1204         case BR_STATE_LEARNING:
1205                 stp_state = PORT_CONTROL_STATE_LEARNING;
1206                 break;
1207         case BR_STATE_FORWARDING:
1208         default:
1209                 stp_state = PORT_CONTROL_STATE_FORWARDING;
1210                 break;
1211         }
1212
1213         netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
1214
1215         /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1216          * so we can not update the port state directly but need to schedule it.
1217          */
1218         ps->port_state[port] = stp_state;
1219         set_bit(port, &ps->port_state_update_mask);
1220         schedule_work(&ps->bridge_work);
1221
1222         return 0;
1223 }
1224
1225 static int __mv88e6xxx_write_addr(struct dsa_switch *ds,
1226                                   const unsigned char *addr)
1227 {
1228         int i, ret;
1229
1230         for (i = 0; i < 3; i++) {
1231                 ret = _mv88e6xxx_reg_write(
1232                         ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1233                         (addr[i * 2] << 8) | addr[i * 2 + 1]);
1234                 if (ret < 0)
1235                         return ret;
1236         }
1237
1238         return 0;
1239 }
1240
1241 static int __mv88e6xxx_read_addr(struct dsa_switch *ds, unsigned char *addr)
1242 {
1243         int i, ret;
1244
1245         for (i = 0; i < 3; i++) {
1246                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1247                                           GLOBAL_ATU_MAC_01 + i);
1248                 if (ret < 0)
1249                         return ret;
1250                 addr[i * 2] = ret >> 8;
1251                 addr[i * 2 + 1] = ret & 0xff;
1252         }
1253
1254         return 0;
1255 }
1256
1257 static int __mv88e6xxx_port_fdb_cmd(struct dsa_switch *ds, int port,
1258                                     const unsigned char *addr, int state)
1259 {
1260         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1261         u8 fid = ps->fid[port];
1262         int ret;
1263
1264         ret = _mv88e6xxx_atu_wait(ds);
1265         if (ret < 0)
1266                 return ret;
1267
1268         ret = __mv88e6xxx_write_addr(ds, addr);
1269         if (ret < 0)
1270                 return ret;
1271
1272         ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA,
1273                                    (0x10 << port) | state);
1274         if (ret)
1275                 return ret;
1276
1277         ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_LOAD_DB);
1278
1279         return ret;
1280 }
1281
1282 int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
1283                            const unsigned char *addr, u16 vid)
1284 {
1285         int state = is_multicast_ether_addr(addr) ?
1286                 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1287                 GLOBAL_ATU_DATA_STATE_UC_STATIC;
1288         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1289         int ret;
1290
1291         mutex_lock(&ps->smi_mutex);
1292         ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr, state);
1293         mutex_unlock(&ps->smi_mutex);
1294
1295         return ret;
1296 }
1297
1298 int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
1299                            const unsigned char *addr, u16 vid)
1300 {
1301         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1302         int ret;
1303
1304         mutex_lock(&ps->smi_mutex);
1305         ret = __mv88e6xxx_port_fdb_cmd(ds, port, addr,
1306                                        GLOBAL_ATU_DATA_STATE_UNUSED);
1307         mutex_unlock(&ps->smi_mutex);
1308
1309         return ret;
1310 }
1311
1312 static int __mv88e6xxx_port_getnext(struct dsa_switch *ds, int port,
1313                                     unsigned char *addr, bool *is_static)
1314 {
1315         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1316         u8 fid = ps->fid[port];
1317         int ret, state;
1318
1319         ret = _mv88e6xxx_atu_wait(ds);
1320         if (ret < 0)
1321                 return ret;
1322
1323         ret = __mv88e6xxx_write_addr(ds, addr);
1324         if (ret < 0)
1325                 return ret;
1326
1327         do {
1328                 ret = _mv88e6xxx_atu_cmd(ds, fid,  GLOBAL_ATU_OP_GET_NEXT_DB);
1329                 if (ret < 0)
1330                         return ret;
1331
1332                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1333                 if (ret < 0)
1334                         return ret;
1335                 state = ret & GLOBAL_ATU_DATA_STATE_MASK;
1336                 if (state == GLOBAL_ATU_DATA_STATE_UNUSED)
1337                         return -ENOENT;
1338         } while (!(((ret >> 4) & 0xff) & (1 << port)));
1339
1340         ret = __mv88e6xxx_read_addr(ds, addr);
1341         if (ret < 0)
1342                 return ret;
1343
1344         *is_static = state == (is_multicast_ether_addr(addr) ?
1345                                GLOBAL_ATU_DATA_STATE_MC_STATIC :
1346                                GLOBAL_ATU_DATA_STATE_UC_STATIC);
1347
1348         return 0;
1349 }
1350
1351 /* get next entry for port */
1352 int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port,
1353                                unsigned char *addr, bool *is_static)
1354 {
1355         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1356         int ret;
1357
1358         mutex_lock(&ps->smi_mutex);
1359         ret = __mv88e6xxx_port_getnext(ds, port, addr, is_static);
1360         mutex_unlock(&ps->smi_mutex);
1361
1362         return ret;
1363 }
1364
1365 static void mv88e6xxx_bridge_work(struct work_struct *work)
1366 {
1367         struct mv88e6xxx_priv_state *ps;
1368         struct dsa_switch *ds;
1369         int port;
1370
1371         ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
1372         ds = ((struct dsa_switch *)ps) - 1;
1373
1374         while (ps->port_state_update_mask) {
1375                 port = __ffs(ps->port_state_update_mask);
1376                 clear_bit(port, &ps->port_state_update_mask);
1377                 mv88e6xxx_set_port_state(ds, port, ps->port_state[port]);
1378         }
1379 }
1380
1381 static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
1382 {
1383         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1384         int ret, fid;
1385         u16 reg;
1386
1387         mutex_lock(&ps->smi_mutex);
1388
1389         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1390             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1391             mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
1392             mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
1393                 /* MAC Forcing register: don't force link, speed,
1394                  * duplex or flow control state to any particular
1395                  * values on physical ports, but force the CPU port
1396                  * and all DSA ports to their maximum bandwidth and
1397                  * full duplex.
1398                  */
1399                 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
1400                 if (dsa_is_cpu_port(ds, port) ||
1401                     ds->dsa_port_mask & (1 << port)) {
1402                         reg |= PORT_PCS_CTRL_FORCE_LINK |
1403                                 PORT_PCS_CTRL_LINK_UP |
1404                                 PORT_PCS_CTRL_DUPLEX_FULL |
1405                                 PORT_PCS_CTRL_FORCE_DUPLEX;
1406                         if (mv88e6xxx_6065_family(ds))
1407                                 reg |= PORT_PCS_CTRL_100;
1408                         else
1409                                 reg |= PORT_PCS_CTRL_1000;
1410                 } else {
1411                         reg |= PORT_PCS_CTRL_UNFORCED;
1412                 }
1413
1414                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1415                                            PORT_PCS_CTRL, reg);
1416                 if (ret)
1417                         goto abort;
1418         }
1419
1420         /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
1421          * disable Header mode, enable IGMP/MLD snooping, disable VLAN
1422          * tunneling, determine priority by looking at 802.1p and IP
1423          * priority fields (IP prio has precedence), and set STP state
1424          * to Forwarding.
1425          *
1426          * If this is the CPU link, use DSA or EDSA tagging depending
1427          * on which tagging mode was configured.
1428          *
1429          * If this is a link to another switch, use DSA tagging mode.
1430          *
1431          * If this is the upstream port for this switch, enable
1432          * forwarding of unknown unicasts and multicasts.
1433          */
1434         reg = 0;
1435         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1436             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1437             mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
1438             mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
1439                 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
1440                 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
1441                 PORT_CONTROL_STATE_FORWARDING;
1442         if (dsa_is_cpu_port(ds, port)) {
1443                 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
1444                         reg |= PORT_CONTROL_DSA_TAG;
1445                 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1446                     mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1447                     mv88e6xxx_6320_family(ds)) {
1448                         if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1449                                 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
1450                         else
1451                                 reg |= PORT_CONTROL_FRAME_MODE_DSA;
1452                 }
1453
1454                 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1455                     mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1456                     mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
1457                     mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
1458                         if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1459                                 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
1460                 }
1461         }
1462         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1463             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1464             mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
1465             mv88e6xxx_6320_family(ds)) {
1466                 if (ds->dsa_port_mask & (1 << port))
1467                         reg |= PORT_CONTROL_FRAME_MODE_DSA;
1468                 if (port == dsa_upstream_port(ds))
1469                         reg |= PORT_CONTROL_FORWARD_UNKNOWN |
1470                                 PORT_CONTROL_FORWARD_UNKNOWN_MC;
1471         }
1472         if (reg) {
1473                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1474                                            PORT_CONTROL, reg);
1475                 if (ret)
1476                         goto abort;
1477         }
1478
1479         /* Port Control 2: don't force a good FCS, set the maximum
1480          * frame size to 10240 bytes, don't let the switch add or
1481          * strip 802.1q tags, don't discard tagged or untagged frames
1482          * on this port, do a destination address lookup on all
1483          * received packets as usual, disable ARP mirroring and don't
1484          * send a copy of all transmitted/received frames on this port
1485          * to the CPU.
1486          */
1487         reg = 0;
1488         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1489             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1490             mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds))
1491                 reg = PORT_CONTROL_2_MAP_DA;
1492
1493         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1494             mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
1495                 reg |= PORT_CONTROL_2_JUMBO_10240;
1496
1497         if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
1498                 /* Set the upstream port this port should use */
1499                 reg |= dsa_upstream_port(ds);
1500                 /* enable forwarding of unknown multicast addresses to
1501                  * the upstream port
1502                  */
1503                 if (port == dsa_upstream_port(ds))
1504                         reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
1505         }
1506
1507         if (reg) {
1508                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1509                                            PORT_CONTROL_2, reg);
1510                 if (ret)
1511                         goto abort;
1512         }
1513
1514         /* Port Association Vector: when learning source addresses
1515          * of packets, add the address to the address database using
1516          * a port bitmap that has only the bit for this port set and
1517          * the other bits clear.
1518          */
1519         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR,
1520                                    1 << port);
1521         if (ret)
1522                 goto abort;
1523
1524         /* Egress rate control 2: disable egress rate control. */
1525         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
1526                                    0x0000);
1527         if (ret)
1528                 goto abort;
1529
1530         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1531             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1532             mv88e6xxx_6320_family(ds)) {
1533                 /* Do not limit the period of time that this port can
1534                  * be paused for by the remote end or the period of
1535                  * time that this port can pause the remote end.
1536                  */
1537                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1538                                            PORT_PAUSE_CTRL, 0x0000);
1539                 if (ret)
1540                         goto abort;
1541
1542                 /* Port ATU control: disable limiting the number of
1543                  * address database entries that this port is allowed
1544                  * to use.
1545                  */
1546                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1547                                            PORT_ATU_CONTROL, 0x0000);
1548                 /* Priority Override: disable DA, SA and VTU priority
1549                  * override.
1550                  */
1551                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1552                                            PORT_PRI_OVERRIDE, 0x0000);
1553                 if (ret)
1554                         goto abort;
1555
1556                 /* Port Ethertype: use the Ethertype DSA Ethertype
1557                  * value.
1558                  */
1559                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1560                                            PORT_ETH_TYPE, ETH_P_EDSA);
1561                 if (ret)
1562                         goto abort;
1563                 /* Tag Remap: use an identity 802.1p prio -> switch
1564                  * prio mapping.
1565                  */
1566                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1567                                            PORT_TAG_REGMAP_0123, 0x3210);
1568                 if (ret)
1569                         goto abort;
1570
1571                 /* Tag Remap 2: use an identity 802.1p prio -> switch
1572                  * prio mapping.
1573                  */
1574                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1575                                            PORT_TAG_REGMAP_4567, 0x7654);
1576                 if (ret)
1577                         goto abort;
1578         }
1579
1580         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1581             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1582             mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
1583             mv88e6xxx_6320_family(ds)) {
1584                 /* Rate Control: disable ingress rate limiting. */
1585                 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1586                                            PORT_RATE_CONTROL, 0x0001);
1587                 if (ret)
1588                         goto abort;
1589         }
1590
1591         /* Port Control 1: disable trunking, disable sending
1592          * learning messages to this port.
1593          */
1594         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
1595         if (ret)
1596                 goto abort;
1597
1598         /* Port based VLAN map: give each port its own address
1599          * database, allow the CPU port to talk to each of the 'real'
1600          * ports, and allow each of the 'real' ports to only talk to
1601          * the upstream port.
1602          */
1603         fid = __ffs(ps->fid_mask);
1604         ps->fid[port] = fid;
1605         ps->fid_mask &= ~(1 << fid);
1606
1607         if (!dsa_is_cpu_port(ds, port))
1608                 ps->bridge_mask[fid] = 1 << port;
1609
1610         ret = _mv88e6xxx_update_port_config(ds, port);
1611         if (ret)
1612                 goto abort;
1613
1614         /* Default VLAN ID and priority: don't set a default VLAN
1615          * ID, and set the default packet priority to zero.
1616          */
1617         ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
1618                                    0x0000);
1619 abort:
1620         mutex_unlock(&ps->smi_mutex);
1621         return ret;
1622 }
1623
1624 int mv88e6xxx_setup_ports(struct dsa_switch *ds)
1625 {
1626         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1627         int ret;
1628         int i;
1629
1630         for (i = 0; i < ps->num_ports; i++) {
1631                 ret = mv88e6xxx_setup_port(ds, i);
1632                 if (ret < 0)
1633                         return ret;
1634         }
1635         return 0;
1636 }
1637
1638 static int mv88e6xxx_regs_show(struct seq_file *s, void *p)
1639 {
1640         struct dsa_switch *ds = s->private;
1641
1642         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1643         int reg, port;
1644
1645         seq_puts(s, "    GLOBAL GLOBAL2 ");
1646         for (port = 0 ; port < ps->num_ports; port++)
1647                 seq_printf(s, " %2d  ", port);
1648         seq_puts(s, "\n");
1649
1650         for (reg = 0; reg < 32; reg++) {
1651                 seq_printf(s, "%2x: ", reg);
1652                 seq_printf(s, " %4x    %4x  ",
1653                            mv88e6xxx_reg_read(ds, REG_GLOBAL, reg),
1654                            mv88e6xxx_reg_read(ds, REG_GLOBAL2, reg));
1655
1656                 for (port = 0 ; port < ps->num_ports; port++)
1657                         seq_printf(s, "%4x ",
1658                                    mv88e6xxx_reg_read(ds, REG_PORT(port), reg));
1659                 seq_puts(s, "\n");
1660         }
1661
1662         return 0;
1663 }
1664
1665 static int mv88e6xxx_regs_open(struct inode *inode, struct file *file)
1666 {
1667         return single_open(file, mv88e6xxx_regs_show, inode->i_private);
1668 }
1669
1670 static const struct file_operations mv88e6xxx_regs_fops = {
1671         .open   = mv88e6xxx_regs_open,
1672         .read   = seq_read,
1673         .llseek = no_llseek,
1674         .release = single_release,
1675         .owner  = THIS_MODULE,
1676 };
1677
1678 static void mv88e6xxx_atu_show_header(struct seq_file *s)
1679 {
1680         seq_puts(s, "DB   T/P  Vec State Addr\n");
1681 }
1682
1683 static void mv88e6xxx_atu_show_entry(struct seq_file *s, int dbnum,
1684                                      unsigned char *addr, int data)
1685 {
1686         bool trunk = !!(data & GLOBAL_ATU_DATA_TRUNK);
1687         int portvec = ((data & GLOBAL_ATU_DATA_PORT_VECTOR_MASK) >>
1688                        GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT);
1689         int state = data & GLOBAL_ATU_DATA_STATE_MASK;
1690
1691         seq_printf(s, "%03x %5s %10pb   %x   %pM\n",
1692                    dbnum, (trunk ? "Trunk" : "Port"), &portvec, state, addr);
1693 }
1694
1695 static int mv88e6xxx_atu_show_db(struct seq_file *s, struct dsa_switch *ds,
1696                                  int dbnum)
1697 {
1698         unsigned char bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1699         unsigned char addr[6];
1700         int ret, data, state;
1701
1702         ret = __mv88e6xxx_write_addr(ds, bcast);
1703         if (ret < 0)
1704                 return ret;
1705
1706         do {
1707                 ret = _mv88e6xxx_atu_cmd(ds, dbnum, GLOBAL_ATU_OP_GET_NEXT_DB);
1708                 if (ret < 0)
1709                         return ret;
1710                 data = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1711                 if (data < 0)
1712                         return data;
1713
1714                 state = data & GLOBAL_ATU_DATA_STATE_MASK;
1715                 if (state == GLOBAL_ATU_DATA_STATE_UNUSED)
1716                         break;
1717                 ret = __mv88e6xxx_read_addr(ds, addr);
1718                 if (ret < 0)
1719                         return ret;
1720                 mv88e6xxx_atu_show_entry(s, dbnum, addr, data);
1721         } while (state != GLOBAL_ATU_DATA_STATE_UNUSED);
1722
1723         return 0;
1724 }
1725
1726 static int mv88e6xxx_atu_show(struct seq_file *s, void *p)
1727 {
1728         struct dsa_switch *ds = s->private;
1729         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1730         int dbnum;
1731
1732         mv88e6xxx_atu_show_header(s);
1733
1734         for (dbnum = 0; dbnum < 255; dbnum++) {
1735                 mutex_lock(&ps->smi_mutex);
1736                 mv88e6xxx_atu_show_db(s, ds, dbnum);
1737                 mutex_unlock(&ps->smi_mutex);
1738         }
1739
1740         return 0;
1741 }
1742
1743 static int mv88e6xxx_atu_open(struct inode *inode, struct file *file)
1744 {
1745         return single_open(file, mv88e6xxx_atu_show, inode->i_private);
1746 }
1747
1748 static const struct file_operations mv88e6xxx_atu_fops = {
1749         .open   = mv88e6xxx_atu_open,
1750         .read   = seq_read,
1751         .llseek = no_llseek,
1752         .release = single_release,
1753         .owner  = THIS_MODULE,
1754 };
1755
1756 static void mv88e6xxx_stats_show_header(struct seq_file *s,
1757                                         struct mv88e6xxx_priv_state *ps)
1758 {
1759         int port;
1760
1761         seq_puts(s, "      Statistic       ");
1762         for (port = 0 ; port < ps->num_ports; port++)
1763                 seq_printf(s, "Port %2d  ", port);
1764         seq_puts(s, "\n");
1765 }
1766
1767 static int mv88e6xxx_stats_show(struct seq_file *s, void *p)
1768 {
1769         struct dsa_switch *ds = s->private;
1770         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1771         struct mv88e6xxx_hw_stat *stats = mv88e6xxx_hw_stats;
1772         int port, stat, max_stats;
1773         uint64_t value;
1774
1775         if (have_sw_in_discards(ds))
1776                 max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats);
1777         else
1778                 max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
1779
1780         mv88e6xxx_stats_show_header(s, ps);
1781
1782         mutex_lock(&ps->smi_mutex);
1783
1784         for (stat = 0; stat < max_stats; stat++) {
1785                 seq_printf(s, "%19s: ", stats[stat].string);
1786                 for (port = 0 ; port < ps->num_ports; port++) {
1787                         _mv88e6xxx_stats_snapshot(ds, port);
1788                         value = _mv88e6xxx_get_ethtool_stat(ds, stat, stats,
1789                                                             port);
1790                         seq_printf(s, "%8llu ", value);
1791                 }
1792                 seq_puts(s, "\n");
1793         }
1794         mutex_unlock(&ps->smi_mutex);
1795
1796         return 0;
1797 }
1798
1799 static int mv88e6xxx_stats_open(struct inode *inode, struct file *file)
1800 {
1801         return single_open(file, mv88e6xxx_stats_show, inode->i_private);
1802 }
1803
1804 static const struct file_operations mv88e6xxx_stats_fops = {
1805         .open   = mv88e6xxx_stats_open,
1806         .read   = seq_read,
1807         .llseek = no_llseek,
1808         .release = single_release,
1809         .owner  = THIS_MODULE,
1810 };
1811
1812 static int mv88e6xxx_device_map_show(struct seq_file *s, void *p)
1813 {
1814         struct dsa_switch *ds = s->private;
1815         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1816         int target, ret;
1817
1818         seq_puts(s, "Target Port\n");
1819
1820         mutex_lock(&ps->smi_mutex);
1821         for (target = 0; target < 32; target++) {
1822                 ret = _mv88e6xxx_reg_write(
1823                         ds, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
1824                         target << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT);
1825                 if (ret < 0)
1826                         goto out;
1827                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2,
1828                                           GLOBAL2_DEVICE_MAPPING);
1829                 seq_printf(s, "  %2d   %2d\n", target,
1830                            ret & GLOBAL2_DEVICE_MAPPING_PORT_MASK);
1831         }
1832 out:
1833         mutex_unlock(&ps->smi_mutex);
1834
1835         return 0;
1836 }
1837
1838 static int mv88e6xxx_device_map_open(struct inode *inode, struct file *file)
1839 {
1840         return single_open(file, mv88e6xxx_device_map_show, inode->i_private);
1841 }
1842
1843 static const struct file_operations mv88e6xxx_device_map_fops = {
1844         .open   = mv88e6xxx_device_map_open,
1845         .read   = seq_read,
1846         .llseek = no_llseek,
1847         .release = single_release,
1848         .owner  = THIS_MODULE,
1849 };
1850
1851 static int mv88e6xxx_scratch_show(struct seq_file *s, void *p)
1852 {
1853         struct dsa_switch *ds = s->private;
1854         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1855         int reg, ret;
1856
1857         seq_puts(s, "Register Value\n");
1858
1859         mutex_lock(&ps->smi_mutex);
1860         for (reg = 0; reg < 0x80; reg++) {
1861                 ret = _mv88e6xxx_reg_write(
1862                         ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC,
1863                         reg << GLOBAL2_SCRATCH_REGISTER_SHIFT);
1864                 if (ret < 0)
1865                         goto out;
1866
1867                 ret = _mv88e6xxx_scratch_wait(ds);
1868                 if (ret < 0)
1869                         goto out;
1870
1871                 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2,
1872                                           GLOBAL2_SCRATCH_MISC);
1873                 seq_printf(s, "  %2x   %2x\n", reg,
1874                            ret & GLOBAL2_SCRATCH_VALUE_MASK);
1875         }
1876 out:
1877         mutex_unlock(&ps->smi_mutex);
1878
1879         return 0;
1880 }
1881
1882 static int mv88e6xxx_scratch_open(struct inode *inode, struct file *file)
1883 {
1884         return single_open(file, mv88e6xxx_scratch_show, inode->i_private);
1885 }
1886
1887 static const struct file_operations mv88e6xxx_scratch_fops = {
1888         .open   = mv88e6xxx_scratch_open,
1889         .read   = seq_read,
1890         .llseek = no_llseek,
1891         .release = single_release,
1892         .owner  = THIS_MODULE,
1893 };
1894
1895 int mv88e6xxx_setup_common(struct dsa_switch *ds)
1896 {
1897         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1898         char *name;
1899
1900         mutex_init(&ps->smi_mutex);
1901
1902         ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
1903
1904         ps->fid_mask = (1 << DSA_MAX_PORTS) - 1;
1905
1906         INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
1907
1908         name = kasprintf(GFP_KERNEL, "dsa%d", ds->index);
1909         ps->dbgfs = debugfs_create_dir(name, NULL);
1910         kfree(name);
1911
1912         debugfs_create_file("regs", S_IRUGO, ps->dbgfs, ds,
1913                             &mv88e6xxx_regs_fops);
1914
1915         debugfs_create_file("atu", S_IRUGO, ps->dbgfs, ds,
1916                             &mv88e6xxx_atu_fops);
1917
1918         debugfs_create_file("stats", S_IRUGO, ps->dbgfs, ds,
1919                             &mv88e6xxx_stats_fops);
1920
1921         debugfs_create_file("device_map", S_IRUGO, ps->dbgfs, ds,
1922                             &mv88e6xxx_device_map_fops);
1923
1924         debugfs_create_file("scratch", S_IRUGO, ps->dbgfs, ds,
1925                             &mv88e6xxx_scratch_fops);
1926         return 0;
1927 }
1928
1929 int mv88e6xxx_setup_global(struct dsa_switch *ds)
1930 {
1931         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1932         int i;
1933
1934         /* Set the default address aging time to 5 minutes, and
1935          * enable address learn messages to be sent to all message
1936          * ports.
1937          */
1938         REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
1939                   0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
1940
1941         /* Configure the IP ToS mapping registers. */
1942         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
1943         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
1944         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
1945         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
1946         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
1947         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
1948         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
1949         REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
1950
1951         /* Configure the IEEE 802.1p priority mapping register. */
1952         REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
1953
1954         /* Send all frames with destination addresses matching
1955          * 01:80:c2:00:00:0x to the CPU port.
1956          */
1957         REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
1958
1959         /* Ignore removed tag data on doubly tagged packets, disable
1960          * flow control messages, force flow control priority to the
1961          * highest, and send all special multicast frames to the CPU
1962          * port at the highest priority.
1963          */
1964         REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
1965                   0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
1966                   GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
1967
1968         /* Program the DSA routing table. */
1969         for (i = 0; i < 32; i++) {
1970                 int nexthop = 0x1f;
1971
1972                 if (ds->pd->rtable &&
1973                     i != ds->index && i < ds->dst->pd->nr_chips)
1974                         nexthop = ds->pd->rtable[i] & 0x1f;
1975
1976                 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
1977                           GLOBAL2_DEVICE_MAPPING_UPDATE |
1978                           (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
1979                           nexthop);
1980         }
1981
1982         /* Clear all trunk masks. */
1983         for (i = 0; i < 8; i++)
1984                 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
1985                           0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
1986                           ((1 << ps->num_ports) - 1));
1987
1988         /* Clear all trunk mappings. */
1989         for (i = 0; i < 16; i++)
1990                 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
1991                           GLOBAL2_TRUNK_MAPPING_UPDATE |
1992                           (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
1993
1994         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1995             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1996             mv88e6xxx_6320_family(ds)) {
1997                 /* Send all frames with destination addresses matching
1998                  * 01:80:c2:00:00:2x to the CPU port.
1999                  */
2000                 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2001
2002                 /* Initialise cross-chip port VLAN table to reset
2003                  * defaults.
2004                  */
2005                 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2006
2007                 /* Clear the priority override table. */
2008                 for (i = 0; i < 16; i++)
2009                         REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2010                                   0x8000 | (i << 8));
2011         }
2012
2013         if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2014             mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2015             mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2016             mv88e6xxx_6320_family(ds)) {
2017                 /* Disable ingress rate limiting by resetting all
2018                  * ingress rate limit registers to their initial
2019                  * state.
2020                  */
2021                 for (i = 0; i < ps->num_ports; i++)
2022                         REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2023                                   0x9000 | (i << 8));
2024         }
2025
2026         /* Clear the statistics counters for all ports */
2027         REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2028
2029         /* Wait for the flush to complete. */
2030         _mv88e6xxx_stats_wait(ds);
2031
2032         return 0;
2033 }
2034
2035 int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2036 {
2037         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2038         u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2039         unsigned long timeout;
2040         int ret;
2041         int i;
2042
2043         /* Set all ports to the disabled state. */
2044         for (i = 0; i < ps->num_ports; i++) {
2045                 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2046                 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
2047         }
2048
2049         /* Wait for transmit queues to drain. */
2050         usleep_range(2000, 4000);
2051
2052         /* Reset the switch. Keep the PPU active if requested. The PPU
2053          * needs to be active to support indirect phy register access
2054          * through global registers 0x18 and 0x19.
2055          */
2056         if (ppu_active)
2057                 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2058         else
2059                 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2060
2061         /* Wait up to one second for reset to complete. */
2062         timeout = jiffies + 1 * HZ;
2063         while (time_before(jiffies, timeout)) {
2064                 ret = REG_READ(REG_GLOBAL, 0x00);
2065                 if ((ret & is_reset) == is_reset)
2066                         break;
2067                 usleep_range(1000, 2000);
2068         }
2069         if (time_after(jiffies, timeout))
2070                 return -ETIMEDOUT;
2071
2072         return 0;
2073 }
2074
2075 int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2076 {
2077         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2078         int ret;
2079
2080         mutex_lock(&ps->smi_mutex);
2081         ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2082         if (ret < 0)
2083                 goto error;
2084         ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
2085 error:
2086         _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2087         mutex_unlock(&ps->smi_mutex);
2088         return ret;
2089 }
2090
2091 int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2092                              int reg, int val)
2093 {
2094         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2095         int ret;
2096
2097         mutex_lock(&ps->smi_mutex);
2098         ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2099         if (ret < 0)
2100                 goto error;
2101
2102         ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
2103 error:
2104         _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2105         mutex_unlock(&ps->smi_mutex);
2106         return ret;
2107 }
2108
2109 static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2110 {
2111         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2112
2113         if (port >= 0 && port < ps->num_ports)
2114                 return port;
2115         return -EINVAL;
2116 }
2117
2118 int
2119 mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2120 {
2121         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2122         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2123         int ret;
2124
2125         if (addr < 0)
2126                 return addr;
2127
2128         mutex_lock(&ps->smi_mutex);
2129         ret = _mv88e6xxx_phy_read(ds, addr, regnum);
2130         mutex_unlock(&ps->smi_mutex);
2131         return ret;
2132 }
2133
2134 int
2135 mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2136 {
2137         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2138         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2139         int ret;
2140
2141         if (addr < 0)
2142                 return addr;
2143
2144         mutex_lock(&ps->smi_mutex);
2145         ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
2146         mutex_unlock(&ps->smi_mutex);
2147         return ret;
2148 }
2149
2150 int
2151 mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2152 {
2153         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2154         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2155         int ret;
2156
2157         if (addr < 0)
2158                 return addr;
2159
2160         mutex_lock(&ps->smi_mutex);
2161         ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
2162         mutex_unlock(&ps->smi_mutex);
2163         return ret;
2164 }
2165
2166 int
2167 mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2168                              u16 val)
2169 {
2170         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2171         int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2172         int ret;
2173
2174         if (addr < 0)
2175                 return addr;
2176
2177         mutex_lock(&ps->smi_mutex);
2178         ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
2179         mutex_unlock(&ps->smi_mutex);
2180         return ret;
2181 }
2182
2183 static int __init mv88e6xxx_init(void)
2184 {
2185 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2186         register_switch_driver(&mv88e6131_switch_driver);
2187 #endif
2188 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2189         register_switch_driver(&mv88e6123_61_65_switch_driver);
2190 #endif
2191 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2192         register_switch_driver(&mv88e6352_switch_driver);
2193 #endif
2194 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2195         register_switch_driver(&mv88e6171_switch_driver);
2196 #endif
2197         return 0;
2198 }
2199 module_init(mv88e6xxx_init);
2200
2201 static void __exit mv88e6xxx_cleanup(void)
2202 {
2203 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2204         unregister_switch_driver(&mv88e6171_switch_driver);
2205 #endif
2206 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2207         unregister_switch_driver(&mv88e6352_switch_driver);
2208 #endif
2209 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2210         unregister_switch_driver(&mv88e6123_61_65_switch_driver);
2211 #endif
2212 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2213         unregister_switch_driver(&mv88e6131_switch_driver);
2214 #endif
2215 }
2216 module_exit(mv88e6xxx_cleanup);
2217
2218 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
2219 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
2220 MODULE_LICENSE("GPL");