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