blob: 5f85133e4ba55fcca58d47dc7d383a19dcaa7e2d [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;
developer6059eb22023-05-10 21:48:10 +0800679@@ -303,6 +313,155 @@ 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),
787+ SFP_QUIRK_F("JESS-LINK", "P60000BBC001-1", '\0', sfp_fixup_rollball),
developerc9bd9ae2022-12-23 16:54:36 +0800788+};
789+
790+static size_t sfp_strlen(const char *str, size_t maxlen)
791+{
792+ size_t size, i;
793+
794+ /* Trailing characters should be filled with space chars, but
795+ * some manufacturers can't read SFF-8472 and use NUL.
796+ */
797+ for (i = 0, size = 0; i < maxlen; i++)
798+ if (str[i] != ' ' && str[i] != '\0')
799+ size = i + 1;
800+
801+ return size;
802+}
803+
804+static bool sfp_match(const char *qs, const char *str, size_t len)
805+{
806+ if (!qs)
807+ return true;
808+ if (strlen(qs) != len)
809+ return false;
810+ return !strncmp(qs, str, len);
811+}
812+
813+static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
814+{
815+ const struct sfp_quirk *q;
816+ unsigned int i;
developer6059eb22023-05-10 21:48:10 +0800817+ size_t vs, ps, rs;
developerc9bd9ae2022-12-23 16:54:36 +0800818+
819+ vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
820+ ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
developer6059eb22023-05-10 21:48:10 +0800821+ rs = sfp_strlen(id->base.vendor_rev, ARRAY_SIZE(id->base.vendor_rev));
developerc9bd9ae2022-12-23 16:54:36 +0800822+
823+ for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
824+ if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
developer6059eb22023-05-10 21:48:10 +0800825+ sfp_match(q->part, id->base.vendor_pn, ps) &&
826+ sfp_match(q->revision, id->base.vendor_rev, rs))
developerc9bd9ae2022-12-23 16:54:36 +0800827+ return q;
828+
829+ return NULL;
830+}
831+
832 static unsigned long poll_jiffies;
833
834 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
developer90020932023-03-10 18:51:34 +0800835@@ -414,9 +554,6 @@ static int sfp_i2c_write(struct sfp *sfp
developerc9bd9ae2022-12-23 16:54:36 +0800836
837 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
838 {
839- struct mii_bus *i2c_mii;
840- int ret;
841-
842 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
843 return -EINVAL;
844
developer90020932023-03-10 18:51:34 +0800845@@ -424,7 +561,15 @@ static int sfp_i2c_configure(struct sfp
developerc9bd9ae2022-12-23 16:54:36 +0800846 sfp->read = sfp_i2c_read;
847 sfp->write = sfp_i2c_write;
848
849- i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
850+ return 0;
851+}
852+
853+static int sfp_i2c_mdiobus_create(struct sfp *sfp)
854+{
855+ struct mii_bus *i2c_mii;
856+ int ret;
857+
858+ i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol);
859 if (IS_ERR(i2c_mii))
860 return PTR_ERR(i2c_mii);
861
developer90020932023-03-10 18:51:34 +0800862@@ -442,6 +587,12 @@ static int sfp_i2c_configure(struct sfp
developerc9bd9ae2022-12-23 16:54:36 +0800863 return 0;
864 }
865
866+static void sfp_i2c_mdiobus_destroy(struct sfp *sfp)
867+{
868+ mdiobus_unregister(sfp->i2c_mii);
869+ sfp->i2c_mii = NULL;
870+}
871+
872 /* Interface */
873 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
874 {
developer90020932023-03-10 18:51:34 +0800875@@ -487,17 +638,18 @@ static void sfp_soft_set_state(struct sf
developerc9bd9ae2022-12-23 16:54:36 +0800876 static void sfp_soft_start_poll(struct sfp *sfp)
877 {
878 const struct sfp_eeprom_id *id = &sfp->id;
879+ unsigned int mask = 0;
880
881 sfp->state_soft_mask = 0;
882- if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
883- !sfp->gpio[GPIO_TX_DISABLE])
884- sfp->state_soft_mask |= SFP_F_TX_DISABLE;
885- if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
886- !sfp->gpio[GPIO_TX_FAULT])
887- sfp->state_soft_mask |= SFP_F_TX_FAULT;
888- if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
889- !sfp->gpio[GPIO_LOS])
890- sfp->state_soft_mask |= SFP_F_LOS;
891+ if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE)
892+ mask |= SFP_F_TX_DISABLE;
893+ if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT)
894+ mask |= SFP_F_TX_FAULT;
895+ if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS)
896+ mask |= SFP_F_LOS;
897+
898+ // Poll the soft state for hardware pins we want to ignore
899+ sfp->state_soft_mask = ~sfp->state_hw_mask & mask;
900
901 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
902 !sfp->need_poll)
developer90020932023-03-10 18:51:34 +0800903@@ -511,10 +663,11 @@ static void sfp_soft_stop_poll(struct sf
developerc9bd9ae2022-12-23 16:54:36 +0800904
905 static unsigned int sfp_get_state(struct sfp *sfp)
906 {
907- unsigned int state = sfp->get_state(sfp);
908+ unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT);
909+ unsigned int state;
910
911- if (state & SFP_F_PRESENT &&
912- sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
913+ state = sfp->get_state(sfp) & sfp->state_hw_mask;
914+ if (state & SFP_F_PRESENT && soft)
915 state |= sfp_soft_get_state(sfp);
916
917 return state;
developer90020932023-03-10 18:51:34 +0800918@@ -1448,12 +1601,12 @@ static void sfp_sm_phy_detach(struct sfp
developerc9bd9ae2022-12-23 16:54:36 +0800919 sfp->mod_phy = NULL;
920 }
921
922-static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45)
923+static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45)
924 {
925 struct phy_device *phy;
926 int err;
927
928- phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45);
929+ phy = get_phy_device(sfp->i2c_mii, addr, is_c45);
930 if (phy == ERR_PTR(-ENODEV))
931 return PTR_ERR(phy);
932 if (IS_ERR(phy)) {
developer90020932023-03-10 18:51:34 +0800933@@ -1548,6 +1701,14 @@ static void sfp_sm_fault(struct sfp *sfp
developerc9bd9ae2022-12-23 16:54:36 +0800934 }
935 }
936
937+static int sfp_sm_add_mdio_bus(struct sfp *sfp)
938+{
939+ if (sfp->mdio_protocol != MDIO_I2C_NONE)
940+ return sfp_i2c_mdiobus_create(sfp);
941+
942+ return 0;
943+}
944+
945 /* Probe a SFP for a PHY device if the module supports copper - the PHY
946 * normally sits at I2C bus address 0x56, and may either be a clause 22
947 * or clause 45 PHY.
developer90020932023-03-10 18:51:34 +0800948@@ -1563,19 +1724,23 @@ static int sfp_sm_probe_for_phy(struct s
developerc9bd9ae2022-12-23 16:54:36 +0800949 {
950 int err = 0;
951
952- switch (sfp->id.base.extended_cc) {
953- case SFF8024_ECC_10GBASE_T_SFI:
954- case SFF8024_ECC_10GBASE_T_SR:
955- case SFF8024_ECC_5GBASE_T:
956- case SFF8024_ECC_2_5GBASE_T:
957- err = sfp_sm_probe_phy(sfp, true);
958+ switch (sfp->mdio_protocol) {
959+ case MDIO_I2C_NONE:
960 break;
961
962- default:
963- if (sfp->id.base.e1000_base_t)
964- err = sfp_sm_probe_phy(sfp, false);
965+ case MDIO_I2C_MARVELL_C22:
966+ err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false);
967+ break;
968+
969+ case MDIO_I2C_C45:
970+ err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true);
971+ break;
972+
973+ case MDIO_I2C_ROLLBALL:
974+ err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true);
975 break;
976 }
977+
978 return err;
979 }
980
developer0d8565e2023-05-11 13:34:51 +0800981@@ -1755,17 +1783,29 @@ static int sfp_sm_probe_for_phy(struct sfp *sfp)
982 static int sfp_module_parse_power(struct sfp *sfp)
983 {
984 u32 power_mW = 1000;
985+ bool supports_a2;
986
987- if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
988+ if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
989+ sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
990 power_mW = 1500;
991- if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
992+ /* Added in Rev 11.9, but there is no compliance code for this */
993+ if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV11_4 &&
994+ sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
995 power_mW = 2000;
996
997+ /* Power level 1 modules (max. 1W) are always supported. */
998+ if (power_mW <= 1000) {
999+ sfp->module_power_mW = power_mW;
1000+ return 0;
1001+ }
1002+
1003+ supports_a2 = sfp->id.ext.sff8472_compliance !=
1004+ SFP_SFF8472_COMPLIANCE_NONE ||
1005+ sfp->id.ext.diagmon & SFP_DIAGMON_DDM;
1006+
1007 if (power_mW > sfp->max_power_mW) {
1008 /* Module power specification exceeds the allowed maximum. */
1009- if (sfp->id.ext.sff8472_compliance ==
1010- SFP_SFF8472_COMPLIANCE_NONE &&
1011- !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1012+ if (!supports_a2) {
1013 /* The module appears not to implement bus address
1014 * 0xa2, so assume that the module powers up in the
1015 * indicated mode.
1016@@ -1782,13 +1822,21 @@ static int sfp_module_parse_power(struct sfp *sfp)
1017 }
1018 }
1019
1020+ if (!supports_a2) {
1021+ /* The module power level is below the host maximum and the
1022+ * module appears not to implement bus address 0xa2, so assume
1023+ * that the module powers up in the indicated mode.
1024+ */
1025+ return 0;
1026+ }
1027+
1028 /* If the module requires a higher power mode, but also requires
1029 * an address change sequence, warn the user that the module may
1030 * not be functional.
1031 */
1032- if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
1033+ if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) {
1034 dev_warn(sfp->dev,
1035- "Address Change Sequence not supported but module requies %u.%uW, module may not be functional\n",
1036+ "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
1037 power_mW / 1000, (power_mW / 100) % 10);
1038 return 0;
1039 }
developer90020932023-03-10 18:51:34 +08001040@@ -1819,11 +1984,33 @@ static int sfp_sm_mod_probe(struct sfp *
developerc9bd9ae2022-12-23 16:54:36 +08001041 if (ret < 0)
1042 return ret;
1043
1044- if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) &&
1045- !memcmp(id.base.vendor_pn, "3FE46541AA ", 16))
1046- sfp->module_t_start_up = T_START_UP_BAD_GPON;
1047+ /* Initialise state bits to use from hardware */
1048+ sfp->state_hw_mask = SFP_F_PRESENT;
1049+ if (sfp->gpio[GPIO_TX_DISABLE])
1050+ sfp->state_hw_mask |= SFP_F_TX_DISABLE;
1051+ if (sfp->gpio[GPIO_TX_FAULT])
1052+ sfp->state_hw_mask |= SFP_F_TX_FAULT;
1053+ if (sfp->gpio[GPIO_LOS])
1054+ sfp->state_hw_mask |= SFP_F_LOS;
1055+
1056+ sfp->module_t_start_up = T_START_UP;
1057+ sfp->module_t_wait = T_WAIT;
1058+
1059+ sfp->tx_fault_ignore = false;
1060+
1061+ if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI ||
1062+ sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR ||
1063+ sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T ||
1064+ sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T)
1065+ sfp->mdio_protocol = MDIO_I2C_C45;
1066+ else if (sfp->id.base.e1000_base_t)
1067+ sfp->mdio_protocol = MDIO_I2C_MARVELL_C22;
1068 else
1069- sfp->module_t_start_up = T_START_UP;
1070+ sfp->mdio_protocol = MDIO_I2C_NONE;
1071+
1072+ sfp->quirk = sfp_lookup_quirk(&id);
1073+ if (sfp->quirk && sfp->quirk->fixup)
1074+ sfp->quirk->fixup(sfp);
1075
1076 return 0;
1077 }
developer90020932023-03-10 18:51:34 +08001078@@ -1936,7 +2123,8 @@ static void sfp_sm_module(struct sfp *sf
developerc9bd9ae2022-12-23 16:54:36 +08001079 break;
1080
1081 /* Report the module insertion to the upstream device */
1082- err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1083+ err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
1084+ sfp->quirk);
1085 if (err < 0) {
1086 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1087 break;
developer90020932023-03-10 18:51:34 +08001088@@ -1995,6 +2183,8 @@ static void sfp_sm_main(struct sfp *sfp,
developerc9bd9ae2022-12-23 16:54:36 +08001089 sfp_module_stop(sfp->sfp_bus);
1090 if (sfp->mod_phy)
1091 sfp_sm_phy_detach(sfp);
1092+ if (sfp->i2c_mii)
1093+ sfp_i2c_mdiobus_destroy(sfp);
1094 sfp_module_tx_disable(sfp);
1095 sfp_soft_stop_poll(sfp);
1096 sfp_sm_next(sfp, SFP_S_DOWN, 0);
developer90020932023-03-10 18:51:34 +08001097@@ -2018,9 +2208,10 @@ static void sfp_sm_main(struct sfp *sfp,
developerc9bd9ae2022-12-23 16:54:36 +08001098
1099 /* We need to check the TX_FAULT state, which is not defined
1100 * while TX_DISABLE is asserted. The earliest we want to do
1101- * anything (such as probe for a PHY) is 50ms.
1102+ * anything (such as probe for a PHY) is 50ms (or more on
1103+ * specific modules).
1104 */
1105- sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1106+ sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait);
1107 break;
1108
1109 case SFP_S_WAIT:
developer90020932023-03-10 18:51:34 +08001110@@ -2034,8 +2225,8 @@ static void sfp_sm_main(struct sfp *sfp,
developerc9bd9ae2022-12-23 16:54:36 +08001111 * deasserting.
1112 */
1113 timeout = sfp->module_t_start_up;
1114- if (timeout > T_WAIT)
1115- timeout -= T_WAIT;
1116+ if (timeout > sfp->module_t_wait)
1117+ timeout -= sfp->module_t_wait;
1118 else
1119 timeout = 1;
1120
developer90020932023-03-10 18:51:34 +08001121@@ -2057,6 +2248,12 @@ static void sfp_sm_main(struct sfp *sfp,
developerc9bd9ae2022-12-23 16:54:36 +08001122 sfp->sm_fault_retries == N_FAULT_INIT);
1123 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1124 init_done:
1125+ /* Create mdiobus and start trying for PHY */
1126+ ret = sfp_sm_add_mdio_bus(sfp);
1127+ if (ret < 0) {
1128+ sfp_sm_next(sfp, SFP_S_FAIL, 0);
1129+ break;
1130+ }
1131 sfp->sm_phy_retries = R_PHY_RETRY;
1132 goto phy_probe;
1133 }
developer90020932023-03-10 18:51:34 +08001134@@ -2409,6 +2606,8 @@ static int sfp_probe(struct platform_dev
developerc9bd9ae2022-12-23 16:54:36 +08001135 return PTR_ERR(sfp->gpio[i]);
1136 }
1137
1138+ sfp->state_hw_mask = SFP_F_PRESENT;
1139+
1140 sfp->get_state = sfp_gpio_get_state;
1141 sfp->set_state = sfp_gpio_set_state;
1142
developerc9bd9ae2022-12-23 16:54:36 +08001143--- a/drivers/net/phy/sfp.h
1144+++ b/drivers/net/phy/sfp.h
developer6059eb22023-05-10 21:48:10 +08001145@@ -6,6 +6,14 @@
developerc9bd9ae2022-12-23 16:54:36 +08001146
1147 struct sfp;
1148
1149+struct sfp_quirk {
1150+ const char *vendor;
1151+ const char *part;
developer6059eb22023-05-10 21:48:10 +08001152+ const char *revision;
developerc9bd9ae2022-12-23 16:54:36 +08001153+ void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
1154+ void (*fixup)(struct sfp *sfp);
1155+};
1156+
1157 struct sfp_socket_ops {
1158 void (*attach)(struct sfp *sfp);
1159 void (*detach)(struct sfp *sfp);
developer90020932023-03-10 18:51:34 +08001160@@ -20,7 +27,8 @@ int sfp_add_phy(struct sfp_bus *bus, str
developerc9bd9ae2022-12-23 16:54:36 +08001161 void sfp_remove_phy(struct sfp_bus *bus);
1162 void sfp_link_up(struct sfp_bus *bus);
1163 void sfp_link_down(struct sfp_bus *bus);
1164-int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id);
1165+int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
1166+ const struct sfp_quirk *quirk);
1167 void sfp_module_remove(struct sfp_bus *bus);
1168 int sfp_module_start(struct sfp_bus *bus);
1169 void sfp_module_stop(struct sfp_bus *bus);
developerc9bd9ae2022-12-23 16:54:36 +08001170--- a/drivers/net/phy/marvell10g.c
1171+++ b/drivers/net/phy/marvell10g.c
developerf1e143b2023-01-09 16:25:59 +08001172@@ -32,6 +32,15 @@
1173 #define MV_PHY_ALASKA_NBT_QUIRK_REV (MARVELL_PHY_ID_88X3310 | 0xa)
1174
1175 enum {
1176+ MV_PMA_21X0_PORT_CTRL = 0xc04a,
1177+ MV_PMA_21X0_PORT_CTRL_SWRST = BIT(15),
1178+ MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK = 0x7,
1179+ MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII = 0x0,
1180+ MV_PMA_2180_PORT_CTRL_MACTYPE_DXGMII = 0x1,
1181+ MV_PMA_2180_PORT_CTRL_MACTYPE_QXGMII = 0x2,
1182+ MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER = 0x4,
1183+ MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN = 0x5,
1184+ MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH = 0x6,
1185 MV_PMA_BOOT = 0xc050,
1186 MV_PMA_BOOT_FATAL = BIT(0),
1187
1188@@ -53,7 +62,18 @@ enum {
1189
1190 /* Vendor2 MMD registers */
1191 MV_V2_PORT_CTRL = 0xf001,
1192- MV_V2_PORT_CTRL_PWRDOWN = 0x0800,
1193+ MV_V2_PORT_CTRL_PWRDOWN = BIT(11),
1194+ MV_V2_33X0_PORT_CTRL_SWRST = BIT(15),
1195+ MV_V2_33X0_PORT_CTRL_MACTYPE_MASK = 0x7,
1196+ MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI = 0x0,
1197+ MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH = 0x1,
1198+ MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN = 0x1,
1199+ MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH = 0x2,
1200+ MV_V2_3310_PORT_CTRL_MACTYPE_XAUI = 0x3,
1201+ MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER = 0x4,
1202+ MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN = 0x5,
1203+ MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH = 0x6,
1204+ MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII = 0x7,
1205 MV_V2_TEMP_CTRL = 0xf08a,
1206 MV_V2_TEMP_CTRL_MASK = 0xc000,
1207 MV_V2_TEMP_CTRL_SAMPLE = 0x0000,
1208@@ -62,11 +82,24 @@ enum {
1209 MV_V2_TEMP_UNKNOWN = 0x9600, /* unknown function */
1210 };
1211
1212+struct mv3310_chip {
1213+ int (*get_mactype)(struct phy_device *phydev);
1214+ int (*init_interface)(struct phy_device *phydev, int mactype);
1215+};
1216+
1217 struct mv3310_priv {
1218+ bool rate_match;
1219+ phy_interface_t const_interface;
1220+
1221 struct device *hwmon_dev;
1222 char *hwmon_name;
1223 };
1224
1225+static const struct mv3310_chip *to_mv3310_chip(struct phy_device *phydev)
1226+{
1227+ return phydev->drv->driver_data;
1228+}
1229+
1230 #ifdef CONFIG_HWMON
1231 static umode_t mv3310_hwmon_is_visible(const void *data,
1232 enum hwmon_sensor_types type,
developer90020932023-03-10 18:51:34 +08001233@@ -155,13 +188,6 @@ static int mv3310_hwmon_config(struct ph
developerc9bd9ae2022-12-23 16:54:36 +08001234 MV_V2_TEMP_CTRL_MASK, val);
1235 }
1236
1237-static void mv3310_hwmon_disable(void *data)
1238-{
1239- struct phy_device *phydev = data;
1240-
1241- mv3310_hwmon_config(phydev, false);
1242-}
1243-
1244 static int mv3310_hwmon_probe(struct phy_device *phydev)
1245 {
1246 struct device *dev = &phydev->mdio.dev;
developer90020932023-03-10 18:51:34 +08001247@@ -185,10 +211,6 @@ static int mv3310_hwmon_probe(struct phy
developerc9bd9ae2022-12-23 16:54:36 +08001248 if (ret)
1249 return ret;
1250
1251- ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev);
1252- if (ret)
1253- return ret;
1254-
1255 priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
1256 priv->hwmon_name, phydev,
1257 &mv3310_hwmon_chip_info, NULL);
developer90020932023-03-10 18:51:34 +08001258@@ -262,6 +284,11 @@ static int mv3310_probe(struct phy_devic
developerc9bd9ae2022-12-23 16:54:36 +08001259 return phy_sfp_probe(phydev, &mv3310_sfp_ops);
1260 }
1261
1262+static void mv3310_remove(struct phy_device *phydev)
1263+{
1264+ mv3310_hwmon_config(phydev, false);
1265+}
1266+
1267 static int mv3310_suspend(struct phy_device *phydev)
1268 {
1269 return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
developer90020932023-03-10 18:51:34 +08001270@@ -297,8 +324,84 @@ static bool mv3310_has_pma_ngbaset_quirk
developerf1e143b2023-01-09 16:25:59 +08001271 MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
1272 }
1273
1274+static int mv2110_get_mactype(struct phy_device *phydev)
1275+{
1276+ int mactype;
1277+
1278+ mactype = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_21X0_PORT_CTRL);
1279+ if (mactype < 0)
1280+ return mactype;
1281+
1282+ return mactype & MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK;
1283+}
1284+
1285+static int mv3310_get_mactype(struct phy_device *phydev)
1286+{
1287+ int mactype;
1288+
1289+ mactype = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL);
1290+ if (mactype < 0)
1291+ return mactype;
1292+
1293+ return mactype & MV_V2_33X0_PORT_CTRL_MACTYPE_MASK;
1294+}
1295+
1296+static int mv2110_init_interface(struct phy_device *phydev, int mactype)
1297+{
1298+ struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1299+
1300+ priv->rate_match = false;
1301+
1302+ if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
1303+ priv->rate_match = true;
1304+
1305+ if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII)
1306+ priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
1307+ else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
1308+ priv->const_interface = PHY_INTERFACE_MODE_10GKR;
1309+ else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER ||
1310+ mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN)
1311+ priv->const_interface = PHY_INTERFACE_MODE_NA;
1312+ else
1313+ return -EINVAL;
1314+
1315+ return 0;
1316+}
1317+
1318+static int mv3310_init_interface(struct phy_device *phydev, int mactype)
1319+{
1320+ struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1321+
1322+ priv->rate_match = false;
1323+
1324+ if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
1325+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
1326+ mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH)
1327+ priv->rate_match = true;
1328+
1329+ if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII)
1330+ priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
1331+ else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
1332+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN ||
1333+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER)
1334+ priv->const_interface = PHY_INTERFACE_MODE_10GKR;
1335+ else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
1336+ mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI)
1337+ priv->const_interface = PHY_INTERFACE_MODE_RXAUI;
1338+ else if (mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH ||
1339+ mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI)
1340+ priv->const_interface = PHY_INTERFACE_MODE_XAUI;
1341+ else
1342+ return -EINVAL;
1343+
1344+ return 0;
1345+}
1346+
1347 static int mv3310_config_init(struct phy_device *phydev)
1348 {
1349+ const struct mv3310_chip *chip = to_mv3310_chip(phydev);
1350+ int err, mactype;
1351+
1352 /* Check that the PHY interface type is compatible */
1353 if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
1354 phydev->interface != PHY_INTERFACE_MODE_2500BASEX &&
developer90020932023-03-10 18:51:34 +08001355@@ -307,6 +410,16 @@ static int mv3310_config_init(struct phy
developerf1e143b2023-01-09 16:25:59 +08001356 phydev->interface != PHY_INTERFACE_MODE_10GKR)
1357 return -ENODEV;
1358
1359+ mactype = chip->get_mactype(phydev);
1360+ if (mactype < 0)
1361+ return mactype;
1362+
1363+ err = chip->init_interface(phydev, mactype);
1364+ if (err) {
1365+ phydev_err(phydev, "MACTYPE configuration invalid\n");
1366+ return err;
1367+ }
1368+
1369 return 0;
1370 }
1371
developer90020932023-03-10 18:51:34 +08001372@@ -384,6 +497,23 @@ static int mv3310_aneg_done(struct phy_d
developerf1e143b2023-01-09 16:25:59 +08001373
1374 static void mv3310_update_interface(struct phy_device *phydev)
1375 {
1376+ struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1377+
1378+ if (!phydev->link)
1379+ return;
1380+
1381+ /* In all of the "* with Rate Matching" modes the PHY interface is fixed
1382+ * at 10Gb. The PHY adapts the rate to actual wire speed with help of
1383+ * internal 16KB buffer.
1384+ *
1385+ * In USXGMII mode the PHY interface mode is also fixed.
1386+ */
1387+ if (priv->rate_match ||
1388+ priv->const_interface == PHY_INTERFACE_MODE_USXGMII) {
1389+ phydev->interface = priv->const_interface;
1390+ return;
1391+ }
1392+
1393 if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
1394 phydev->interface == PHY_INTERFACE_MODE_2500BASEX ||
developer90020932023-03-10 18:51:34 +08001395 phydev->interface == PHY_INTERFACE_MODE_5GBASER ||
1396@@ -503,11 +633,22 @@ static int mv3310_read_status(struct phy
developerf1e143b2023-01-09 16:25:59 +08001397 return 0;
1398 }
1399
1400+static const struct mv3310_chip mv3310_type = {
1401+ .get_mactype = mv3310_get_mactype,
1402+ .init_interface = mv3310_init_interface,
1403+};
1404+
1405+static const struct mv3310_chip mv2111_type = {
1406+ .get_mactype = mv2110_get_mactype,
1407+ .init_interface = mv2110_init_interface,
1408+};
1409+
1410 static struct phy_driver mv3310_drivers[] = {
1411 {
1412 .phy_id = MARVELL_PHY_ID_88X3310,
1413 .phy_id_mask = MARVELL_PHY_ID_MASK,
1414 .name = "mv88x3310",
1415+ .driver_data = &mv3310_type,
1416 .get_features = mv3310_get_features,
1417 .soft_reset = genphy_no_soft_reset,
1418 .config_init = mv3310_config_init,
developer90020932023-03-10 18:51:34 +08001419@@ -517,11 +658,13 @@ static struct phy_driver mv3310_drivers[
developerc9bd9ae2022-12-23 16:54:36 +08001420 .config_aneg = mv3310_config_aneg,
1421 .aneg_done = mv3310_aneg_done,
1422 .read_status = mv3310_read_status,
1423+ .remove = mv3310_remove,
1424 },
1425 {
1426 .phy_id = MARVELL_PHY_ID_88E2110,
developerf1e143b2023-01-09 16:25:59 +08001427 .phy_id_mask = MARVELL_PHY_ID_MASK,
1428 .name = "mv88x2110",
1429+ .driver_data = &mv2111_type,
1430 .probe = mv3310_probe,
1431 .suspend = mv3310_suspend,
1432 .resume = mv3310_resume,
developer90020932023-03-10 18:51:34 +08001433@@ -530,6 +673,7 @@ static struct phy_driver mv3310_drivers[
developerc9bd9ae2022-12-23 16:54:36 +08001434 .config_aneg = mv3310_config_aneg,
1435 .aneg_done = mv3310_aneg_done,
1436 .read_status = mv3310_read_status,
1437+ .remove = mv3310_remove,
1438 },
1439 };
1440