blob: 623304651209fea731a6c45dccbf9e6fcf476c9d [file] [log] [blame]
developerc9bd9ae2022-12-23 16:54:36 +08001diff --git a/drivers/net/phy/mdio-i2c.c b/drivers/net/phy/mdio-i2c.c
2index 0746e2c..81c8fe7 100644
3--- a/drivers/net/phy/mdio-i2c.c
4+++ b/drivers/net/phy/mdio-i2c.c
5@@ -11,6 +11,7 @@
6 */
7 #include <linux/i2c.h>
8 #include <linux/phy.h>
9+#include <linux/sfp.h>
10
11 #include "mdio-i2c.h"
12
13@@ -29,7 +30,7 @@ static unsigned int i2c_mii_phy_addr(int phy_id)
14 return phy_id + 0x40;
15 }
16
17-static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
18+static int i2c_mii_read_default(struct mii_bus *bus, int phy_id, int reg)
19 {
20 struct i2c_adapter *i2c = bus->priv;
21 struct i2c_msg msgs[2];
22@@ -63,7 +64,8 @@ static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
23 return data[0] << 8 | data[1];
24 }
25
26-static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
27+static int i2c_mii_write_default(struct mii_bus *bus, int phy_id, int reg,
28+ u16 val)
29 {
30 struct i2c_adapter *i2c = bus->priv;
31 struct i2c_msg msg;
32@@ -92,9 +94,288 @@ static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
33 return ret < 0 ? ret : 0;
34 }
35
36-struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
37+/* RollBall SFPs do not access internal PHY via I2C address 0x56, but
38+ * instead via address 0x51, when SFP page is set to 0x03 and password to
39+ * 0xffffffff.
40+ *
41+ * address size contents description
42+ * ------- ---- -------- -----------
43+ * 0x80 1 CMD 0x01/0x02/0x04 for write/read/done
44+ * 0x81 1 DEV Clause 45 device
45+ * 0x82 2 REG Clause 45 register
46+ * 0x84 2 VAL Register value
47+ */
48+#define ROLLBALL_PHY_I2C_ADDR 0x51
49+
50+#define ROLLBALL_PASSWORD (SFP_VSL + 3)
51+
52+#define ROLLBALL_CMD_ADDR 0x80
53+#define ROLLBALL_DATA_ADDR 0x81
54+
55+#define ROLLBALL_CMD_WRITE 0x01
56+#define ROLLBALL_CMD_READ 0x02
57+#define ROLLBALL_CMD_DONE 0x04
58+
59+#define SFP_PAGE_ROLLBALL_MDIO 3
60+
61+static int __i2c_transfer_err(struct i2c_adapter *i2c, struct i2c_msg *msgs,
62+ int num)
63+{
64+ int ret;
65+
66+ ret = __i2c_transfer(i2c, msgs, num);
67+ if (ret < 0)
68+ return ret;
69+ else if (ret != num)
70+ return -EIO;
71+ else
72+ return 0;
73+}
74+
75+static int __i2c_rollball_get_page(struct i2c_adapter *i2c, int bus_addr,
76+ u8 *page)
77+{
78+ struct i2c_msg msgs[2];
79+ u8 addr = SFP_PAGE;
80+
81+ msgs[0].addr = bus_addr;
82+ msgs[0].flags = 0;
83+ msgs[0].len = 1;
84+ msgs[0].buf = &addr;
85+
86+ msgs[1].addr = bus_addr;
87+ msgs[1].flags = I2C_M_RD;
88+ msgs[1].len = 1;
89+ msgs[1].buf = page;
90+
91+ return __i2c_transfer_err(i2c, msgs, 2);
92+}
93+
94+static int __i2c_rollball_set_page(struct i2c_adapter *i2c, int bus_addr,
95+ u8 page)
96+{
97+ struct i2c_msg msg;
98+ u8 buf[2];
99+
100+ buf[0] = SFP_PAGE;
101+ buf[1] = page;
102+
103+ msg.addr = bus_addr;
104+ msg.flags = 0;
105+ msg.len = 2;
106+ msg.buf = buf;
107+
108+ return __i2c_transfer_err(i2c, &msg, 1);
109+}
110+
111+/* In order to not interfere with other SFP code (which possibly may manipulate
112+ * SFP_PAGE), for every transfer we do this:
113+ * 1. lock the bus
114+ * 2. save content of SFP_PAGE
115+ * 3. set SFP_PAGE to 3
116+ * 4. do the transfer
117+ * 5. restore original SFP_PAGE
118+ * 6. unlock the bus
119+ * Note that one might think that steps 2 to 5 could be theoretically done all
120+ * in one call to i2c_transfer (by constructing msgs array in such a way), but
121+ * unfortunately tests show that this does not work :-( Changed SFP_PAGE does
122+ * not take into account until i2c_transfer() is done.
123+ */
124+static int i2c_transfer_rollball(struct i2c_adapter *i2c,
125+ struct i2c_msg *msgs, int num)
126+{
127+ int ret, main_err = 0;
128+ u8 saved_page;
129+
130+ i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
131+
132+ /* save original page */
133+ ret = __i2c_rollball_get_page(i2c, msgs->addr, &saved_page);
134+ if (ret)
135+ goto unlock;
136+
137+ /* change to RollBall MDIO page */
138+ ret = __i2c_rollball_set_page(i2c, msgs->addr, SFP_PAGE_ROLLBALL_MDIO);
139+ if (ret)
140+ goto unlock;
141+
142+ /* do the transfer; we try to restore original page if this fails */
143+ ret = __i2c_transfer_err(i2c, msgs, num);
144+ if (ret)
145+ main_err = ret;
146+
147+ /* restore original page */
148+ ret = __i2c_rollball_set_page(i2c, msgs->addr, saved_page);
149+
150+unlock:
151+ i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
152+
153+ return main_err ? : ret;
154+}
155+
156+static int i2c_rollball_mii_poll(struct mii_bus *bus, int bus_addr, u8 *buf,
157+ size_t len)
158+{
159+ struct i2c_adapter *i2c = bus->priv;
160+ struct i2c_msg msgs[2];
161+ u8 cmd_addr, tmp, *res;
162+ int i, ret;
163+
164+ cmd_addr = ROLLBALL_CMD_ADDR;
165+
166+ res = buf ? buf : &tmp;
167+ len = buf ? len : 1;
168+
169+ msgs[0].addr = bus_addr;
170+ msgs[0].flags = 0;
171+ msgs[0].len = 1;
172+ msgs[0].buf = &cmd_addr;
173+
174+ msgs[1].addr = bus_addr;
175+ msgs[1].flags = I2C_M_RD;
176+ msgs[1].len = len;
177+ msgs[1].buf = res;
178+
179+ /* By experiment it takes up to 70 ms to access a register for these
180+ * SFPs. Sleep 20ms between iterations and try 10 times.
181+ */
182+ i = 10;
183+ do {
184+ msleep(20);
185+
186+ ret = i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
187+ if (ret)
188+ return ret;
189+
190+ if (*res == ROLLBALL_CMD_DONE)
191+ return 0;
192+ } while (i-- > 0);
193+
194+ dev_info(&bus->dev, "poll timed out\n");
195+
196+ return -ETIMEDOUT;
197+}
198+
199+static int i2c_rollball_mii_cmd(struct mii_bus *bus, int bus_addr, u8 cmd,
200+ u8 *data, size_t len)
201+{
202+ struct i2c_adapter *i2c = bus->priv;
203+ struct i2c_msg msgs[2];
204+ u8 cmdbuf[2];
205+
206+ cmdbuf[0] = ROLLBALL_CMD_ADDR;
207+ cmdbuf[1] = cmd;
208+
209+ msgs[0].addr = bus_addr;
210+ msgs[0].flags = 0;
211+ msgs[0].len = len;
212+ msgs[0].buf = data;
213+
214+ msgs[1].addr = bus_addr;
215+ msgs[1].flags = 0;
216+ msgs[1].len = sizeof(cmdbuf);
217+ msgs[1].buf = cmdbuf;
218+
219+ return i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
220+}
221+
222+static int i2c_mii_read_rollball(struct mii_bus *bus, int phy_id, int reg)
223+{
224+ u8 buf[4], res[6];
225+ int bus_addr, ret;
226+ u16 val;
227+
228+ if (!(reg & MII_ADDR_C45))
229+ return -EOPNOTSUPP;
230+
231+ bus_addr = i2c_mii_phy_addr(phy_id);
232+ if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
233+ return 0xffff;
234+
235+ buf[0] = ROLLBALL_DATA_ADDR;
236+ buf[1] = (reg >> 16) & 0x1f;
237+ buf[2] = (reg >> 8) & 0xff;
238+ buf[3] = reg & 0xff;
239+
240+ ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_READ, buf,
241+ sizeof(buf));
242+ if (ret < 0)
243+ return ret;
244+
245+ ret = i2c_rollball_mii_poll(bus, bus_addr, res, sizeof(res));
246+ if (ret == -ETIMEDOUT)
247+ return 0xffff;
248+ else if (ret < 0)
249+ return ret;
250+
251+ val = res[4] << 8 | res[5];
252+
253+ return val;
254+}
255+
256+static int i2c_mii_write_rollball(struct mii_bus *bus, int phy_id, int reg,
257+ u16 val)
258+{
259+ int bus_addr, ret;
260+ u8 buf[6];
261+
262+ if (!(reg & MII_ADDR_C45))
263+ return -EOPNOTSUPP;
264+
265+ bus_addr = i2c_mii_phy_addr(phy_id);
266+ if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
267+ return 0;
268+
269+ buf[0] = ROLLBALL_DATA_ADDR;
270+ buf[1] = (reg >> 16) & 0x1f;
271+ buf[2] = (reg >> 8) & 0xff;
272+ buf[3] = reg & 0xff;
273+ buf[4] = val >> 8;
274+ buf[5] = val & 0xff;
275+
276+ ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_WRITE, buf,
277+ sizeof(buf));
278+ if (ret < 0)
279+ return ret;
280+
281+ ret = i2c_rollball_mii_poll(bus, bus_addr, NULL, 0);
282+ if (ret < 0)
283+ return ret;
284+
285+ return 0;
286+}
287+
288+static int i2c_mii_init_rollball(struct i2c_adapter *i2c)
289+{
290+ struct i2c_msg msg;
291+ u8 pw[5];
292+ int ret;
293+
294+ pw[0] = ROLLBALL_PASSWORD;
295+ pw[1] = 0xff;
296+ pw[2] = 0xff;
297+ pw[3] = 0xff;
298+ pw[4] = 0xff;
299+
300+ msg.addr = ROLLBALL_PHY_I2C_ADDR;
301+ msg.flags = 0;
302+ msg.len = sizeof(pw);
303+ msg.buf = pw;
304+
305+ ret = i2c_transfer(i2c, &msg, 1);
306+ if (ret < 0)
307+ return ret;
308+ else if (ret != 1)
309+ return -EIO;
310+ else
311+ return 0;
312+}
313+
314+struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
315+ enum mdio_i2c_proto protocol)
316 {
317 struct mii_bus *mii;
318+ int ret;
319
320 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
321 return ERR_PTR(-EINVAL);
322@@ -105,10 +386,28 @@ struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
323
324 snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
325 mii->parent = parent;
326- mii->read = i2c_mii_read;
327- mii->write = i2c_mii_write;
328 mii->priv = i2c;
329
330+ switch (protocol) {
331+ case MDIO_I2C_ROLLBALL:
332+ ret = i2c_mii_init_rollball(i2c);
333+ if (ret < 0) {
334+ dev_err(parent,
335+ "Cannot initialize RollBall MDIO I2C protocol: %d\n",
336+ ret);
337+ mdiobus_free(mii);
338+ return ERR_PTR(ret);
339+ }
340+
341+ mii->read = i2c_mii_read_rollball;
342+ mii->write = i2c_mii_write_rollball;
343+ break;
344+ default:
345+ mii->read = i2c_mii_read_default;
346+ mii->write = i2c_mii_write_default;
347+ break;
348+ }
349+
350 return mii;
351 }
352 EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
developer9a8b5e32023-02-21 12:01:56 +0800353diff --git a/include/linux/mdio/mdio-i2c.h b/include/linux/mdio/mdio-i2c.h
354index 751dab2..1c21140 100644
developer45b00642023-02-20 10:32:20 +0800355--- a/include/linux/mdio/mdio-i2c.h
356+++ b/include/linux/mdio/mdio-i2c.h
developerc9bd9ae2022-12-23 16:54:36 +0800357@@ -11,6 +11,14 @@ struct device;
358 struct i2c_adapter;
359 struct mii_bus;
360
361-struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c);
362+enum mdio_i2c_proto {
363+ MDIO_I2C_NONE,
364+ MDIO_I2C_MARVELL_C22,
365+ MDIO_I2C_C45,
366+ MDIO_I2C_ROLLBALL,
367+};
368+
369+struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
370+ enum mdio_i2c_proto protocol);
371
372 #endif
developer82eae452023-02-13 10:04:09 +0800373diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
374index f360d92..67f34ed 100644
375--- a/drivers/net/phy/phylink.c
376+++ b/drivers/net/phy/phylink.c
377@@ -483,62 +483,105 @@ static void phylink_resolve(struct work_struct *w)
378 struct phylink *pl = container_of(w, struct phylink, resolve);
379 struct phylink_link_state link_state;
380 struct net_device *ndev = pl->netdev;
381- int link_changed;
382+ bool mac_config = false;
383+ bool retrigger = false;
384+ bool cur_link_state;
385
386 mutex_lock(&pl->state_mutex);
387+ if (pl->netdev)
388+ cur_link_state = netif_carrier_ok(ndev);
389+ else
390+ cur_link_state = pl->old_link_state;
391+
392 if (pl->phylink_disable_state) {
393 pl->mac_link_dropped = false;
394 link_state.link = false;
395 } else if (pl->mac_link_dropped) {
396 link_state.link = false;
397+ retrigger = true;
398 } else {
399 switch (pl->cur_link_an_mode) {
400 case MLO_AN_PHY:
401 link_state = pl->phy_state;
402 phylink_resolve_flow(pl, &link_state);
403- phylink_mac_config_up(pl, &link_state);
404+ mac_config = link_state.link;
405 break;
406
407 case MLO_AN_FIXED:
408 phylink_get_fixed_state(pl, &link_state);
409- phylink_mac_config_up(pl, &link_state);
410+ mac_config = link_state.link;
411 break;
412
413 case MLO_AN_INBAND:
414 phylink_get_mac_state(pl, &link_state);
415
416+ /* The PCS may have a latching link-fail indicator.
417+ * If the link was up, bring the link down and
418+ * re-trigger the resolve. Otherwise, re-read the
419+ * PCS state to get the current status of the link.
420+ */
421+ if (!link_state.link) {
422+ if (cur_link_state)
423+ retrigger = true;
424+ else
425+ phylink_get_mac_state(pl,
426+ &link_state);
427+ }
428+
429 /* If we have a phy, the "up" state is the union of
430- * both the PHY and the MAC */
431+ * both the PHY and the MAC
432+ */
433 if (pl->phydev)
434 link_state.link &= pl->phy_state.link;
435
436 /* Only update if the PHY link is up */
437 if (pl->phydev && pl->phy_state.link) {
438+ /* If the interface has changed, force a
439+ * link down event if the link isn't already
440+ * down, and re-resolve.
441+ */
442+ if (link_state.interface !=
443+ pl->phy_state.interface) {
444+ retrigger = true;
445+ link_state.link = false;
446+ }
447 link_state.interface = pl->phy_state.interface;
448
449 /* If we have a PHY, we need to update with
450- * the pause mode bits. */
451- link_state.pause |= pl->phy_state.pause;
452- phylink_resolve_flow(pl, &link_state);
453- phylink_mac_config(pl, &link_state);
454+ * the PHY flow control bits.
455+ */
456+ link_state.pause = pl->phy_state.pause;
457+ mac_config = true;
458 }
459+ phylink_resolve_flow(pl, &link_state);
460 break;
461 }
462 }
463
464- if (pl->netdev)
465- link_changed = (link_state.link != netif_carrier_ok(ndev));
466- else
467- link_changed = (link_state.link != pl->old_link_state);
468+ if (mac_config) {
469+ if (link_state.interface != pl->link_config.interface) {
470+ /* The interface has changed, force the link down and
471+ * then reconfigure.
472+ */
473+ if (cur_link_state) {
474+ phylink_mac_link_down(pl);
475+ cur_link_state = false;
476+ }
477+ phylink_mac_config(pl, &link_state);
478+ pl->link_config.interface = link_state.interface;
479+ } else {
480+ phylink_mac_config(pl, &link_state);
481+ }
482+ }
483
484- if (link_changed) {
485+ if (link_state.link != cur_link_state) {
486 pl->old_link_state = link_state.link;
487 if (!link_state.link)
488 phylink_mac_link_down(pl);
489 else
490 phylink_mac_link_up(pl, link_state);
491 }
492- if (!link_state.link && pl->mac_link_dropped) {
493+ if (!link_state.link && retrigger) {
494 pl->mac_link_dropped = false;
495 queue_work(system_power_efficient_wq, &pl->resolve);
496 }
497@@ -1014,7 +1057,8 @@ void phylink_start(struct phylink *pl)
498 if (irq <= 0)
499 mod_timer(&pl->link_poll, jiffies + HZ);
500 }
501- if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->get_fixed_state)
502+ if ((pl->cfg_link_an_mode == MLO_AN_FIXED && pl->get_fixed_state) ||
503+ (pl->cfg_link_an_mode == MLO_AN_INBAND))
504 mod_timer(&pl->link_poll, jiffies + HZ);
505 if (pl->phydev)
506 phy_start(pl->phydev);
developerc9bd9ae2022-12-23 16:54:36 +0800507diff --git a/drivers/net/phy/sfp-bus.c b/drivers/net/phy/sfp-bus.c
508index 42f0441..0d5ac2a 100644
509--- a/drivers/net/phy/sfp-bus.c
510+++ b/drivers/net/phy/sfp-bus.c
511@@ -10,12 +10,6 @@
512
513 #include "sfp.h"
514
515-struct sfp_quirk {
516- const char *vendor;
517- const char *part;
518- void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
519-};
520-
521 /**
522 * struct sfp_bus - internal representation of a sfp bus
523 */
524@@ -38,87 +32,6 @@ struct sfp_bus {
525 bool started;
526 };
527
528-static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
529- unsigned long *modes)
530-{
531- phylink_set(modes, 2500baseX_Full);
532-}
533-
534-static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
535- unsigned long *modes)
536-{
537- /* Ubiquiti U-Fiber Instant module claims that support all transceiver
538- * types including 10G Ethernet which is not truth. So clear all claimed
539- * modes and set only one mode which module supports: 1000baseX_Full.
540- */
541- phylink_zero(modes);
542- phylink_set(modes, 1000baseX_Full);
543-}
544-
545-static const struct sfp_quirk sfp_quirks[] = {
546- {
547- // Alcatel Lucent G-010S-P can operate at 2500base-X, but
548- // incorrectly report 2500MBd NRZ in their EEPROM
549- .vendor = "ALCATELLUCENT",
550- .part = "G010SP",
551- .modes = sfp_quirk_2500basex,
552- }, {
553- // Alcatel Lucent G-010S-A can operate at 2500base-X, but
554- // report 3.2GBd NRZ in their EEPROM
555- .vendor = "ALCATELLUCENT",
556- .part = "3FE46541AA",
557- .modes = sfp_quirk_2500basex,
558- }, {
559- // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
560- // NRZ in their EEPROM
561- .vendor = "HUAWEI",
562- .part = "MA5671A",
563- .modes = sfp_quirk_2500basex,
564- }, {
565- .vendor = "UBNT",
566- .part = "UF-INSTANT",
567- .modes = sfp_quirk_ubnt_uf_instant,
568- },
569-};
570-
571-static size_t sfp_strlen(const char *str, size_t maxlen)
572-{
573- size_t size, i;
574-
575- /* Trailing characters should be filled with space chars */
576- for (i = 0, size = 0; i < maxlen; i++)
577- if (str[i] != ' ')
578- size = i + 1;
579-
580- return size;
581-}
582-
583-static bool sfp_match(const char *qs, const char *str, size_t len)
584-{
585- if (!qs)
586- return true;
587- if (strlen(qs) != len)
588- return false;
589- return !strncmp(qs, str, len);
590-}
591-
592-static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
593-{
594- const struct sfp_quirk *q;
595- unsigned int i;
596- size_t vs, ps;
597-
598- vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
599- ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
600-
601- for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
602- if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
603- sfp_match(q->part, id->base.vendor_pn, ps))
604- return q;
605-
606- return NULL;
607-}
608-
609 /**
610 * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
611 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
612@@ -359,7 +272,7 @@ void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
613 phylink_set(modes, 1000baseX_Full);
614 }
615
616- if (bus->sfp_quirk)
617+ if (bus->sfp_quirk && bus->sfp_quirk->modes)
618 bus->sfp_quirk->modes(id, modes);
619
620 bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
621@@ -734,12 +647,13 @@ void sfp_link_down(struct sfp_bus *bus)
622 }
623 EXPORT_SYMBOL_GPL(sfp_link_down);
624
625-int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
626+int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
627+ const struct sfp_quirk *quirk)
628 {
629 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
630 int ret = 0;
631
632- bus->sfp_quirk = sfp_lookup_quirk(id);
633+ bus->sfp_quirk = quirk;
634
635 if (ops && ops->module_insert)
636 ret = ops->module_insert(bus->upstream, id);
637diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
638index 3253366..8d95f49 100644
639--- a/drivers/net/phy/sfp.c
640+++ b/drivers/net/phy/sfp.c
641@@ -165,6 +165,7 @@ static const enum gpiod_flags gpio_flags[] = {
642 * on board (for a copper SFP) time to initialise.
643 */
644 #define T_WAIT msecs_to_jiffies(50)
645+#define T_WAIT_ROLLBALL msecs_to_jiffies(25000)
646 #define T_START_UP msecs_to_jiffies(300)
647 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
648
649@@ -204,8 +205,11 @@ static const enum gpiod_flags gpio_flags[] = {
650
651 /* SFP modules appear to always have their PHY configured for bus address
652 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
653+ * RollBall SFPs access phy via SFP Enhanced Digital Diagnostic Interface
654+ * via address 0x51 (mdio-i2c will use RollBall protocol on this address).
655 */
656-#define SFP_PHY_ADDR 22
657+#define SFP_PHY_ADDR 22
658+#define SFP_PHY_ADDR_ROLLBALL 17
659
660 struct sff_data {
661 unsigned int gpios;
662@@ -217,6 +221,7 @@ struct sfp {
663 struct i2c_adapter *i2c;
664 struct mii_bus *i2c_mii;
665 struct sfp_bus *sfp_bus;
666+ enum mdio_i2c_proto mdio_protocol;
667 struct phy_device *mod_phy;
668 const struct sff_data *type;
669 size_t i2c_block_size;
670@@ -233,6 +238,7 @@ struct sfp {
671 bool need_poll;
672
673 struct mutex st_mutex; /* Protects state */
674+ unsigned int state_hw_mask;
675 unsigned int state_soft_mask;
676 unsigned int state;
677 struct delayed_work poll;
678@@ -249,6 +255,10 @@ struct sfp {
679 struct sfp_eeprom_id id;
680 unsigned int module_power_mW;
681 unsigned int module_t_start_up;
682+ unsigned int module_t_wait;
683+ bool tx_fault_ignore;
684+
685+ const struct sfp_quirk *quirk;
686
687 #if IS_ENABLED(CONFIG_HWMON)
688 struct sfp_diag diag;
developer82eae452023-02-13 10:04:09 +0800689@@ -303,6 +313,136 @@ static const struct of_device_id sfp_of_match[] = {
developerc9bd9ae2022-12-23 16:54:36 +0800690 };
691 MODULE_DEVICE_TABLE(of, sfp_of_match);
692
693+static void sfp_fixup_long_startup(struct sfp *sfp)
694+{
695+ sfp->module_t_start_up = T_START_UP_BAD_GPON;
696+}
697+
698+static void sfp_fixup_ignore_tx_fault(struct sfp *sfp)
699+{
700+ sfp->tx_fault_ignore = true;
701+}
702+
703+static void sfp_fixup_halny_gsfp(struct sfp *sfp)
704+{
705+ /* Ignore the TX_FAULT and LOS signals on this module.
706+ * these are possibly used for other purposes on this
707+ * module, e.g. a serial port.
708+ */
709+ sfp->state_hw_mask &= ~(SFP_F_TX_FAULT | SFP_F_LOS);
710+}
711+
712+static void sfp_fixup_rollball(struct sfp *sfp)
713+{
714+ sfp->mdio_protocol = MDIO_I2C_ROLLBALL;
715+ sfp->module_t_wait = T_WAIT_ROLLBALL;
716+}
717+
718+static void sfp_fixup_rollball_cc(struct sfp *sfp)
719+{
720+ sfp_fixup_rollball(sfp);
721+
722+ /* Some RollBall SFPs may have wrong (zero) extended compliance code
723+ * burned in EEPROM. For PHY probing we need the correct one.
724+ */
725+ sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SFI;
726+}
727+
728+static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
729+ unsigned long *modes)
730+{
731+ linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, modes);
732+}
733+
734+static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
735+ unsigned long *modes)
736+{
737+ /* Ubiquiti U-Fiber Instant module claims that support all transceiver
738+ * types including 10G Ethernet which is not truth. So clear all claimed
739+ * modes and set only one mode which module supports: 1000baseX_Full.
740+ */
741+ linkmode_zero(modes);
742+ linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, modes);
743+}
744+
745+#define SFP_QUIRK(_v, _p, _m, _f) \
746+ { .vendor = _v, .part = _p, .modes = _m, .fixup = _f, }
747+#define SFP_QUIRK_M(_v, _p, _m) SFP_QUIRK(_v, _p, _m, NULL)
748+#define SFP_QUIRK_F(_v, _p, _f) SFP_QUIRK(_v, _p, NULL, _f)
749+
750+static const struct sfp_quirk sfp_quirks[] = {
751+ // Alcatel Lucent G-010S-P can operate at 2500base-X, but incorrectly
752+ // report 2500MBd NRZ in their EEPROM
753+ SFP_QUIRK_M("ALCATELLUCENT", "G010SP", sfp_quirk_2500basex),
754+
755+ // Alcatel Lucent G-010S-A can operate at 2500base-X, but report 3.2GBd
756+ // NRZ in their EEPROM
757+ SFP_QUIRK("ALCATELLUCENT", "3FE46541AA", sfp_quirk_2500basex,
758+ sfp_fixup_long_startup),
759+
760+ SFP_QUIRK_F("HALNy", "HL-GSFP", sfp_fixup_halny_gsfp),
761+
762+ // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd NRZ in
763+ // their EEPROM
764+ SFP_QUIRK("HUAWEI", "MA5671A", sfp_quirk_2500basex,
765+ sfp_fixup_ignore_tx_fault),
766+
767+ // Lantech 8330-262D-E can operate at 2500base-X, but incorrectly report
768+ // 2500MBd NRZ in their EEPROM
769+ SFP_QUIRK_M("Lantech", "8330-262D-E", sfp_quirk_2500basex),
770+
771+ SFP_QUIRK_M("UBNT", "UF-INSTANT", sfp_quirk_ubnt_uf_instant),
772+
developer82eae452023-02-13 10:04:09 +0800773+ SFP_QUIRK_F("ETU", "ESP-T5-R", sfp_fixup_rollball_cc),
developerc9bd9ae2022-12-23 16:54:36 +0800774+ SFP_QUIRK_F("OEM", "SFP-10G-T", sfp_fixup_rollball_cc),
775+ SFP_QUIRK_F("OEM", "RTSFP-10", sfp_fixup_rollball_cc),
776+ SFP_QUIRK_F("OEM", "RTSFP-10G", sfp_fixup_rollball_cc),
777+ SFP_QUIRK_F("OEM", "TNBYV02-C0X-C3", sfp_fixup_rollball_cc),
778+ SFP_QUIRK_F("Turris", "RTSFP-10", sfp_fixup_rollball),
779+ SFP_QUIRK_F("Turris", "RTSFP-10G", sfp_fixup_rollball),
780+ SFP_QUIRK_F("JESS-LINK", "P60000BBC001-1", sfp_fixup_rollball),
781+};
782+
783+static size_t sfp_strlen(const char *str, size_t maxlen)
784+{
785+ size_t size, i;
786+
787+ /* Trailing characters should be filled with space chars, but
788+ * some manufacturers can't read SFF-8472 and use NUL.
789+ */
790+ for (i = 0, size = 0; i < maxlen; i++)
791+ if (str[i] != ' ' && str[i] != '\0')
792+ size = i + 1;
793+
794+ return size;
795+}
796+
797+static bool sfp_match(const char *qs, const char *str, size_t len)
798+{
799+ if (!qs)
800+ return true;
801+ if (strlen(qs) != len)
802+ return false;
803+ return !strncmp(qs, str, len);
804+}
805+
806+static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
807+{
808+ const struct sfp_quirk *q;
809+ unsigned int i;
810+ size_t vs, ps;
811+
812+ vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
813+ ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
814+
815+ for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
816+ if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
817+ sfp_match(q->part, id->base.vendor_pn, ps))
818+ return q;
819+
820+ return NULL;
821+}
822+
823 static unsigned long poll_jiffies;
824
825 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
826@@ -414,9 +553,6 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
827
828 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
829 {
830- struct mii_bus *i2c_mii;
831- int ret;
832-
833 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
834 return -EINVAL;
835
836@@ -424,7 +560,15 @@ static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
837 sfp->read = sfp_i2c_read;
838 sfp->write = sfp_i2c_write;
839
840- i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
841+ return 0;
842+}
843+
844+static int sfp_i2c_mdiobus_create(struct sfp *sfp)
845+{
846+ struct mii_bus *i2c_mii;
847+ int ret;
848+
849+ i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol);
850 if (IS_ERR(i2c_mii))
851 return PTR_ERR(i2c_mii);
852
853@@ -442,6 +586,12 @@ static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
854 return 0;
855 }
856
857+static void sfp_i2c_mdiobus_destroy(struct sfp *sfp)
858+{
859+ mdiobus_unregister(sfp->i2c_mii);
860+ sfp->i2c_mii = NULL;
861+}
862+
863 /* Interface */
864 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
865 {
866@@ -487,17 +637,18 @@ static void sfp_soft_set_state(struct sfp *sfp, unsigned int state)
867 static void sfp_soft_start_poll(struct sfp *sfp)
868 {
869 const struct sfp_eeprom_id *id = &sfp->id;
870+ unsigned int mask = 0;
871
872 sfp->state_soft_mask = 0;
873- if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
874- !sfp->gpio[GPIO_TX_DISABLE])
875- sfp->state_soft_mask |= SFP_F_TX_DISABLE;
876- if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
877- !sfp->gpio[GPIO_TX_FAULT])
878- sfp->state_soft_mask |= SFP_F_TX_FAULT;
879- if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
880- !sfp->gpio[GPIO_LOS])
881- sfp->state_soft_mask |= SFP_F_LOS;
882+ if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE)
883+ mask |= SFP_F_TX_DISABLE;
884+ if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT)
885+ mask |= SFP_F_TX_FAULT;
886+ if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS)
887+ mask |= SFP_F_LOS;
888+
889+ // Poll the soft state for hardware pins we want to ignore
890+ sfp->state_soft_mask = ~sfp->state_hw_mask & mask;
891
892 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
893 !sfp->need_poll)
894@@ -511,10 +662,11 @@ static void sfp_soft_stop_poll(struct sfp *sfp)
895
896 static unsigned int sfp_get_state(struct sfp *sfp)
897 {
898- unsigned int state = sfp->get_state(sfp);
899+ unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT);
900+ unsigned int state;
901
902- if (state & SFP_F_PRESENT &&
903- sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
904+ state = sfp->get_state(sfp) & sfp->state_hw_mask;
905+ if (state & SFP_F_PRESENT && soft)
906 state |= sfp_soft_get_state(sfp);
907
908 return state;
909@@ -1448,12 +1600,12 @@ static void sfp_sm_phy_detach(struct sfp *sfp)
910 sfp->mod_phy = NULL;
911 }
912
913-static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45)
914+static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45)
915 {
916 struct phy_device *phy;
917 int err;
918
919- phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45);
920+ phy = get_phy_device(sfp->i2c_mii, addr, is_c45);
921 if (phy == ERR_PTR(-ENODEV))
922 return PTR_ERR(phy);
923 if (IS_ERR(phy)) {
924@@ -1548,6 +1700,14 @@ static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
925 }
926 }
927
928+static int sfp_sm_add_mdio_bus(struct sfp *sfp)
929+{
930+ if (sfp->mdio_protocol != MDIO_I2C_NONE)
931+ return sfp_i2c_mdiobus_create(sfp);
932+
933+ return 0;
934+}
935+
936 /* Probe a SFP for a PHY device if the module supports copper - the PHY
937 * normally sits at I2C bus address 0x56, and may either be a clause 22
938 * or clause 45 PHY.
939@@ -1563,19 +1723,23 @@ static int sfp_sm_probe_for_phy(struct sfp *sfp)
940 {
941 int err = 0;
942
943- switch (sfp->id.base.extended_cc) {
944- case SFF8024_ECC_10GBASE_T_SFI:
945- case SFF8024_ECC_10GBASE_T_SR:
946- case SFF8024_ECC_5GBASE_T:
947- case SFF8024_ECC_2_5GBASE_T:
948- err = sfp_sm_probe_phy(sfp, true);
949+ switch (sfp->mdio_protocol) {
950+ case MDIO_I2C_NONE:
951 break;
952
953- default:
954- if (sfp->id.base.e1000_base_t)
955- err = sfp_sm_probe_phy(sfp, false);
956+ case MDIO_I2C_MARVELL_C22:
957+ err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false);
958+ break;
959+
960+ case MDIO_I2C_C45:
961+ err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true);
962+ break;
963+
964+ case MDIO_I2C_ROLLBALL:
965+ err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true);
966 break;
967 }
968+
969 return err;
970 }
971
972@@ -1819,11 +1983,33 @@ static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
973 if (ret < 0)
974 return ret;
975
976- if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) &&
977- !memcmp(id.base.vendor_pn, "3FE46541AA ", 16))
978- sfp->module_t_start_up = T_START_UP_BAD_GPON;
979+ /* Initialise state bits to use from hardware */
980+ sfp->state_hw_mask = SFP_F_PRESENT;
981+ if (sfp->gpio[GPIO_TX_DISABLE])
982+ sfp->state_hw_mask |= SFP_F_TX_DISABLE;
983+ if (sfp->gpio[GPIO_TX_FAULT])
984+ sfp->state_hw_mask |= SFP_F_TX_FAULT;
985+ if (sfp->gpio[GPIO_LOS])
986+ sfp->state_hw_mask |= SFP_F_LOS;
987+
988+ sfp->module_t_start_up = T_START_UP;
989+ sfp->module_t_wait = T_WAIT;
990+
991+ sfp->tx_fault_ignore = false;
992+
993+ if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI ||
994+ sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR ||
995+ sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T ||
996+ sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T)
997+ sfp->mdio_protocol = MDIO_I2C_C45;
998+ else if (sfp->id.base.e1000_base_t)
999+ sfp->mdio_protocol = MDIO_I2C_MARVELL_C22;
1000 else
1001- sfp->module_t_start_up = T_START_UP;
1002+ sfp->mdio_protocol = MDIO_I2C_NONE;
1003+
1004+ sfp->quirk = sfp_lookup_quirk(&id);
1005+ if (sfp->quirk && sfp->quirk->fixup)
1006+ sfp->quirk->fixup(sfp);
1007
1008 return 0;
1009 }
1010@@ -1936,7 +2122,8 @@ static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1011 break;
1012
1013 /* Report the module insertion to the upstream device */
1014- err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1015+ err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
1016+ sfp->quirk);
1017 if (err < 0) {
1018 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1019 break;
1020@@ -1995,6 +2182,8 @@ static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1021 sfp_module_stop(sfp->sfp_bus);
1022 if (sfp->mod_phy)
1023 sfp_sm_phy_detach(sfp);
1024+ if (sfp->i2c_mii)
1025+ sfp_i2c_mdiobus_destroy(sfp);
1026 sfp_module_tx_disable(sfp);
1027 sfp_soft_stop_poll(sfp);
1028 sfp_sm_next(sfp, SFP_S_DOWN, 0);
1029@@ -2018,9 +2207,10 @@ static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1030
1031 /* We need to check the TX_FAULT state, which is not defined
1032 * while TX_DISABLE is asserted. The earliest we want to do
1033- * anything (such as probe for a PHY) is 50ms.
1034+ * anything (such as probe for a PHY) is 50ms (or more on
1035+ * specific modules).
1036 */
1037- sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1038+ sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait);
1039 break;
1040
1041 case SFP_S_WAIT:
1042@@ -2034,8 +2224,8 @@ static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1043 * deasserting.
1044 */
1045 timeout = sfp->module_t_start_up;
1046- if (timeout > T_WAIT)
1047- timeout -= T_WAIT;
1048+ if (timeout > sfp->module_t_wait)
1049+ timeout -= sfp->module_t_wait;
1050 else
1051 timeout = 1;
1052
1053@@ -2057,6 +2247,12 @@ static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1054 sfp->sm_fault_retries == N_FAULT_INIT);
1055 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1056 init_done:
1057+ /* Create mdiobus and start trying for PHY */
1058+ ret = sfp_sm_add_mdio_bus(sfp);
1059+ if (ret < 0) {
1060+ sfp_sm_next(sfp, SFP_S_FAIL, 0);
1061+ break;
1062+ }
1063 sfp->sm_phy_retries = R_PHY_RETRY;
1064 goto phy_probe;
1065 }
1066@@ -2409,6 +2605,8 @@ static int sfp_probe(struct platform_device *pdev)
1067 return PTR_ERR(sfp->gpio[i]);
1068 }
1069
1070+ sfp->state_hw_mask = SFP_F_PRESENT;
1071+
1072 sfp->get_state = sfp_gpio_get_state;
1073 sfp->set_state = sfp_gpio_set_state;
1074
1075diff --git a/drivers/net/phy/sfp.h b/drivers/net/phy/sfp.h
1076index b83f705..ef06d35 100644
1077--- a/drivers/net/phy/sfp.h
1078+++ b/drivers/net/phy/sfp.h
1079@@ -6,6 +6,13 @@
1080
1081 struct sfp;
1082
1083+struct sfp_quirk {
1084+ const char *vendor;
1085+ const char *part;
1086+ void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
1087+ void (*fixup)(struct sfp *sfp);
1088+};
1089+
1090 struct sfp_socket_ops {
1091 void (*attach)(struct sfp *sfp);
1092 void (*detach)(struct sfp *sfp);
1093@@ -20,7 +27,8 @@ int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev);
1094 void sfp_remove_phy(struct sfp_bus *bus);
1095 void sfp_link_up(struct sfp_bus *bus);
1096 void sfp_link_down(struct sfp_bus *bus);
1097-int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id);
1098+int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
1099+ const struct sfp_quirk *quirk);
1100 void sfp_module_remove(struct sfp_bus *bus);
1101 int sfp_module_start(struct sfp_bus *bus);
1102 void sfp_module_stop(struct sfp_bus *bus);
1103diff --git a/drivers/net/phy/marvell10g.c b/drivers/net/phy/marvell10g.c
developerf1e143b2023-01-09 16:25:59 +08001104index 512f27b..daed73a 100644
developerc9bd9ae2022-12-23 16:54:36 +08001105--- a/drivers/net/phy/marvell10g.c
1106+++ b/drivers/net/phy/marvell10g.c
developerf1e143b2023-01-09 16:25:59 +08001107@@ -32,6 +32,15 @@
1108 #define MV_PHY_ALASKA_NBT_QUIRK_REV (MARVELL_PHY_ID_88X3310 | 0xa)
1109
1110 enum {
1111+ MV_PMA_21X0_PORT_CTRL = 0xc04a,
1112+ MV_PMA_21X0_PORT_CTRL_SWRST = BIT(15),
1113+ MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK = 0x7,
1114+ MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII = 0x0,
1115+ MV_PMA_2180_PORT_CTRL_MACTYPE_DXGMII = 0x1,
1116+ MV_PMA_2180_PORT_CTRL_MACTYPE_QXGMII = 0x2,
1117+ MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER = 0x4,
1118+ MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN = 0x5,
1119+ MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH = 0x6,
1120 MV_PMA_BOOT = 0xc050,
1121 MV_PMA_BOOT_FATAL = BIT(0),
1122
1123@@ -53,7 +62,18 @@ enum {
1124
1125 /* Vendor2 MMD registers */
1126 MV_V2_PORT_CTRL = 0xf001,
1127- MV_V2_PORT_CTRL_PWRDOWN = 0x0800,
1128+ MV_V2_PORT_CTRL_PWRDOWN = BIT(11),
1129+ MV_V2_33X0_PORT_CTRL_SWRST = BIT(15),
1130+ MV_V2_33X0_PORT_CTRL_MACTYPE_MASK = 0x7,
1131+ MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI = 0x0,
1132+ MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH = 0x1,
1133+ MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN = 0x1,
1134+ MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH = 0x2,
1135+ MV_V2_3310_PORT_CTRL_MACTYPE_XAUI = 0x3,
1136+ MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER = 0x4,
1137+ MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN = 0x5,
1138+ MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH = 0x6,
1139+ MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII = 0x7,
1140 MV_V2_TEMP_CTRL = 0xf08a,
1141 MV_V2_TEMP_CTRL_MASK = 0xc000,
1142 MV_V2_TEMP_CTRL_SAMPLE = 0x0000,
1143@@ -62,11 +82,24 @@ enum {
1144 MV_V2_TEMP_UNKNOWN = 0x9600, /* unknown function */
1145 };
1146
1147+struct mv3310_chip {
1148+ int (*get_mactype)(struct phy_device *phydev);
1149+ int (*init_interface)(struct phy_device *phydev, int mactype);
1150+};
1151+
1152 struct mv3310_priv {
1153+ bool rate_match;
1154+ phy_interface_t const_interface;
1155+
1156 struct device *hwmon_dev;
1157 char *hwmon_name;
1158 };
1159
1160+static const struct mv3310_chip *to_mv3310_chip(struct phy_device *phydev)
1161+{
1162+ return phydev->drv->driver_data;
1163+}
1164+
1165 #ifdef CONFIG_HWMON
1166 static umode_t mv3310_hwmon_is_visible(const void *data,
1167 enum hwmon_sensor_types type,
1168@@ -155,13 +188,6 @@ static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
developerc9bd9ae2022-12-23 16:54:36 +08001169 MV_V2_TEMP_CTRL_MASK, val);
1170 }
1171
1172-static void mv3310_hwmon_disable(void *data)
1173-{
1174- struct phy_device *phydev = data;
1175-
1176- mv3310_hwmon_config(phydev, false);
1177-}
1178-
1179 static int mv3310_hwmon_probe(struct phy_device *phydev)
1180 {
1181 struct device *dev = &phydev->mdio.dev;
developerf1e143b2023-01-09 16:25:59 +08001182@@ -185,10 +211,6 @@ static int mv3310_hwmon_probe(struct phy_device *phydev)
developerc9bd9ae2022-12-23 16:54:36 +08001183 if (ret)
1184 return ret;
1185
1186- ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev);
1187- if (ret)
1188- return ret;
1189-
1190 priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
1191 priv->hwmon_name, phydev,
1192 &mv3310_hwmon_chip_info, NULL);
developerf1e143b2023-01-09 16:25:59 +08001193@@ -262,6 +284,11 @@ static int mv3310_probe(struct phy_device *phydev)
developerc9bd9ae2022-12-23 16:54:36 +08001194 return phy_sfp_probe(phydev, &mv3310_sfp_ops);
1195 }
1196
1197+static void mv3310_remove(struct phy_device *phydev)
1198+{
1199+ mv3310_hwmon_config(phydev, false);
1200+}
1201+
1202 static int mv3310_suspend(struct phy_device *phydev)
1203 {
1204 return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
developerf1e143b2023-01-09 16:25:59 +08001205@@ -297,8 +324,84 @@ static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
1206 MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
1207 }
1208
1209+static int mv2110_get_mactype(struct phy_device *phydev)
1210+{
1211+ int mactype;
1212+
1213+ mactype = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_21X0_PORT_CTRL);
1214+ if (mactype < 0)
1215+ return mactype;
1216+
1217+ return mactype & MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK;
1218+}
1219+
1220+static int mv3310_get_mactype(struct phy_device *phydev)
1221+{
1222+ int mactype;
1223+
1224+ mactype = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL);
1225+ if (mactype < 0)
1226+ return mactype;
1227+
1228+ return mactype & MV_V2_33X0_PORT_CTRL_MACTYPE_MASK;
1229+}
1230+
1231+static int mv2110_init_interface(struct phy_device *phydev, int mactype)
1232+{
1233+ struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1234+
1235+ priv->rate_match = false;
1236+
1237+ if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
1238+ priv->rate_match = true;
1239+
1240+ if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII)
1241+ priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
1242+ else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
1243+ priv->const_interface = PHY_INTERFACE_MODE_10GKR;
1244+ else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER ||
1245+ mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN)
1246+ priv->const_interface = PHY_INTERFACE_MODE_NA;
1247+ else
1248+ return -EINVAL;
1249+
1250+ return 0;
1251+}
1252+
1253+static int mv3310_init_interface(struct phy_device *phydev, int mactype)
1254+{
1255+ struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1256+
1257+ priv->rate_match = false;
1258+
1259+ if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
1260+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
1261+ mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH)
1262+ priv->rate_match = true;
1263+
1264+ if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII)
1265+ priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
1266+ else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
1267+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN ||
1268+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER)
1269+ priv->const_interface = PHY_INTERFACE_MODE_10GKR;
1270+ else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
1271+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI)
1272+ priv->const_interface = PHY_INTERFACE_MODE_RXAUI;
1273+ else if (mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH ||
1274+ mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI)
1275+ priv->const_interface = PHY_INTERFACE_MODE_XAUI;
1276+ else
1277+ return -EINVAL;
1278+
1279+ return 0;
1280+}
1281+
1282 static int mv3310_config_init(struct phy_device *phydev)
1283 {
1284+ const struct mv3310_chip *chip = to_mv3310_chip(phydev);
1285+ int err, mactype;
1286+
1287 /* Check that the PHY interface type is compatible */
1288 if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
1289 phydev->interface != PHY_INTERFACE_MODE_2500BASEX &&
1290@@ -307,6 +410,16 @@ static int mv3310_config_init(struct phy_device *phydev)
1291 phydev->interface != PHY_INTERFACE_MODE_10GKR)
1292 return -ENODEV;
1293
1294+ mactype = chip->get_mactype(phydev);
1295+ if (mactype < 0)
1296+ return mactype;
1297+
1298+ err = chip->init_interface(phydev, mactype);
1299+ if (err) {
1300+ phydev_err(phydev, "MACTYPE configuration invalid\n");
1301+ return err;
1302+ }
1303+
1304 return 0;
1305 }
1306
1307@@ -384,6 +497,23 @@ static int mv3310_aneg_done(struct phy_device *phydev)
1308
1309 static void mv3310_update_interface(struct phy_device *phydev)
1310 {
1311+ struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1312+
1313+ if (!phydev->link)
1314+ return;
1315+
1316+ /* In all of the "* with Rate Matching" modes the PHY interface is fixed
1317+ * at 10Gb. The PHY adapts the rate to actual wire speed with help of
1318+ * internal 16KB buffer.
1319+ *
1320+ * In USXGMII mode the PHY interface mode is also fixed.
1321+ */
1322+ if (priv->rate_match ||
1323+ priv->const_interface == PHY_INTERFACE_MODE_USXGMII) {
1324+ phydev->interface = priv->const_interface;
1325+ return;
1326+ }
1327+
1328 if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
1329 phydev->interface == PHY_INTERFACE_MODE_2500BASEX ||
1330 phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) {
1331@@ -499,11 +629,22 @@ static int mv3310_read_status(struct phy_device *phydev)
1332 return 0;
1333 }
1334
1335+static const struct mv3310_chip mv3310_type = {
1336+ .get_mactype = mv3310_get_mactype,
1337+ .init_interface = mv3310_init_interface,
1338+};
1339+
1340+static const struct mv3310_chip mv2111_type = {
1341+ .get_mactype = mv2110_get_mactype,
1342+ .init_interface = mv2110_init_interface,
1343+};
1344+
1345 static struct phy_driver mv3310_drivers[] = {
1346 {
1347 .phy_id = MARVELL_PHY_ID_88X3310,
1348 .phy_id_mask = MARVELL_PHY_ID_MASK,
1349 .name = "mv88x3310",
1350+ .driver_data = &mv3310_type,
1351 .get_features = mv3310_get_features,
1352 .soft_reset = genphy_no_soft_reset,
1353 .config_init = mv3310_config_init,
1354@@ -513,11 +654,13 @@ static struct phy_driver mv3310_drivers[] = {
developerc9bd9ae2022-12-23 16:54:36 +08001355 .config_aneg = mv3310_config_aneg,
1356 .aneg_done = mv3310_aneg_done,
1357 .read_status = mv3310_read_status,
1358+ .remove = mv3310_remove,
1359 },
1360 {
1361 .phy_id = MARVELL_PHY_ID_88E2110,
developerf1e143b2023-01-09 16:25:59 +08001362 .phy_id_mask = MARVELL_PHY_ID_MASK,
1363 .name = "mv88x2110",
1364+ .driver_data = &mv2111_type,
1365 .probe = mv3310_probe,
1366 .suspend = mv3310_suspend,
1367 .resume = mv3310_resume,
1368@@ -526,6 +669,7 @@ static struct phy_driver mv3310_drivers[] = {
developerc9bd9ae2022-12-23 16:54:36 +08001369 .config_aneg = mv3310_config_aneg,
1370 .aneg_done = mv3310_aneg_done,
1371 .read_status = mv3310_read_status,
1372+ .remove = mv3310_remove,
1373 },
1374 };
1375