generic: backport support for MT7530 DSA port mirroring
[oweals/openwrt.git] / target / linux / generic / backport-5.4 / 742-v5.5-net-sfp-add-support-for-module-quirks.patch
1 From 8df5dd55cef48c0769379e04dbc085a899b106d4 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@armlinux.org.uk>
3 Date: Fri, 8 Mar 2019 14:02:25 +0000
4 Subject: [PATCH 640/660] net: sfp: add support for module quirks
5
6 Add support for applying module quirks to the list of supported
7 ethtool link modes.
8
9 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
10 ---
11  drivers/net/phy/sfp-bus.c | 54 +++++++++++++++++++++++++++++++++++++++
12  1 file changed, 54 insertions(+)
13
14 --- a/drivers/net/phy/sfp-bus.c
15 +++ b/drivers/net/phy/sfp-bus.c
16 @@ -10,6 +10,12 @@
17  
18  #include "sfp.h"
19  
20 +struct sfp_quirk {
21 +       const char *vendor;
22 +       const char *part;
23 +       void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
24 +};
25 +
26  /**
27   * struct sfp_bus - internal representation of a sfp bus
28   */
29 @@ -22,6 +28,7 @@ struct sfp_bus {
30         const struct sfp_socket_ops *socket_ops;
31         struct device *sfp_dev;
32         struct sfp *sfp;
33 +       const struct sfp_quirk *sfp_quirk;
34  
35         const struct sfp_upstream_ops *upstream_ops;
36         void *upstream;
37 @@ -31,6 +38,46 @@ struct sfp_bus {
38         bool started;
39  };
40  
41 +static const struct sfp_quirk sfp_quirks[] = {
42 +};
43 +
44 +static size_t sfp_strlen(const char *str, size_t maxlen)
45 +{
46 +       size_t size, i;
47 +
48 +       /* Trailing characters should be filled with space chars */
49 +       for (i = 0, size = 0; i < maxlen; i++)
50 +               if (str[i] != ' ')
51 +                       size = i + 1;
52 +
53 +       return size;
54 +}
55 +
56 +static bool sfp_match(const char *qs, const char *str, size_t len)
57 +{
58 +       if (!qs)
59 +               return true;
60 +       if (strlen(qs) != len)
61 +               return false;
62 +       return !strncmp(qs, str, len);
63 +}
64 +
65 +static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
66 +{
67 +       const struct sfp_quirk *q;
68 +       unsigned int i;
69 +       size_t vs, ps;
70 +
71 +       vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
72 +       ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
73 +
74 +       for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
75 +               if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
76 +                   sfp_match(q->part, id->base.vendor_pn, ps))
77 +                       return q;
78 +
79 +       return NULL;
80 +}
81  /**
82   * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
83   * @bus: a pointer to the &struct sfp_bus structure for the sfp module
84 @@ -234,6 +281,9 @@ void sfp_parse_support(struct sfp_bus *b
85                         phylink_set(modes, 1000baseX_Full);
86         }
87  
88 +       if (bus->sfp_quirk)
89 +               bus->sfp_quirk->modes(id, modes);
90 +
91         bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
92  
93         phylink_set(support, Autoneg);
94 @@ -610,6 +660,8 @@ int sfp_module_insert(struct sfp_bus *bu
95         const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
96         int ret = 0;
97  
98 +       bus->sfp_quirk = sfp_lookup_quirk(id);
99 +
100         if (ops && ops->module_insert)
101                 ret = ops->module_insert(bus->upstream, id);
102  
103 @@ -623,6 +675,8 @@ void sfp_module_remove(struct sfp_bus *b
104  
105         if (ops && ops->module_remove)
106                 ops->module_remove(bus->upstream);
107 +
108 +       bus->sfp_quirk = NULL;
109  }
110  EXPORT_SYMBOL_GPL(sfp_module_remove);
111