hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/net/phy/sfp-bus.c
....@@ -1,8 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 #include <linux/export.h>
23 #include <linux/kref.h>
34 #include <linux/list.h>
45 #include <linux/mutex.h>
56 #include <linux/phylink.h>
7
+#include <linux/property.h>
68 #include <linux/rtnetlink.h>
79 #include <linux/slab.h>
810
....@@ -30,7 +32,6 @@
3032
3133 const struct sfp_upstream_ops *upstream_ops;
3234 void *upstream;
33
- struct net_device *netdev;
3435 struct phy_device *phydev;
3536
3637 bool registered;
....@@ -41,6 +42,17 @@
4142 unsigned long *modes)
4243 {
4344 phylink_set(modes, 2500baseX_Full);
45
+}
46
+
47
+static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
48
+ unsigned long *modes)
49
+{
50
+ /* Ubiquiti U-Fiber Instant module claims that support all transceiver
51
+ * types including 10G Ethernet which is not truth. So clear all claimed
52
+ * modes and set only one mode which module supports: 1000baseX_Full.
53
+ */
54
+ phylink_zero(modes);
55
+ phylink_set(modes, 1000baseX_Full);
4456 }
4557
4658 static const struct sfp_quirk sfp_quirks[] = {
....@@ -62,6 +74,16 @@
6274 .vendor = "HUAWEI",
6375 .part = "MA5671A",
6476 .modes = sfp_quirk_2500basex,
77
+ }, {
78
+ // Lantech 8330-262D-E can operate at 2500base-X, but
79
+ // incorrectly report 2500MBd NRZ in their EEPROM
80
+ .vendor = "Lantech",
81
+ .part = "8330-262D-E",
82
+ .modes = sfp_quirk_2500basex,
83
+ }, {
84
+ .vendor = "UBNT",
85
+ .part = "UF-INSTANT",
86
+ .modes = sfp_quirk_ubnt_uf_instant,
6587 },
6688 };
6789
....@@ -102,6 +124,7 @@
102124
103125 return NULL;
104126 }
127
+
105128 /**
106129 * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
107130 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
....@@ -123,35 +146,35 @@
123146
124147 /* port is the physical connector, set this from the connector field. */
125148 switch (id->base.connector) {
126
- case SFP_CONNECTOR_SC:
127
- case SFP_CONNECTOR_FIBERJACK:
128
- case SFP_CONNECTOR_LC:
129
- case SFP_CONNECTOR_MT_RJ:
130
- case SFP_CONNECTOR_MU:
131
- case SFP_CONNECTOR_OPTICAL_PIGTAIL:
149
+ case SFF8024_CONNECTOR_SC:
150
+ case SFF8024_CONNECTOR_FIBERJACK:
151
+ case SFF8024_CONNECTOR_LC:
152
+ case SFF8024_CONNECTOR_MT_RJ:
153
+ case SFF8024_CONNECTOR_MU:
154
+ case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
155
+ case SFF8024_CONNECTOR_MPO_1X12:
156
+ case SFF8024_CONNECTOR_MPO_2X16:
132157 port = PORT_FIBRE;
133158 break;
134159
135
- case SFP_CONNECTOR_RJ45:
160
+ case SFF8024_CONNECTOR_RJ45:
136161 port = PORT_TP;
137162 break;
138163
139
- case SFP_CONNECTOR_COPPER_PIGTAIL:
164
+ case SFF8024_CONNECTOR_COPPER_PIGTAIL:
140165 port = PORT_DA;
141166 break;
142167
143
- case SFP_CONNECTOR_UNSPEC:
168
+ case SFF8024_CONNECTOR_UNSPEC:
144169 if (id->base.e1000_base_t) {
145170 port = PORT_TP;
146171 break;
147172 }
148
- /* fallthrough */
149
- case SFP_CONNECTOR_SG: /* guess */
150
- case SFP_CONNECTOR_MPO_1X12:
151
- case SFP_CONNECTOR_MPO_2X16:
152
- case SFP_CONNECTOR_HSSDC_II:
153
- case SFP_CONNECTOR_NOSEPARATE:
154
- case SFP_CONNECTOR_MXC_2X16:
173
+ fallthrough;
174
+ case SFF8024_CONNECTOR_SG: /* guess */
175
+ case SFF8024_CONNECTOR_HSSDC_II:
176
+ case SFF8024_CONNECTOR_NOSEPARATE:
177
+ case SFF8024_CONNECTOR_MXC_2X16:
155178 port = PORT_OTHER;
156179 break;
157180 default:
....@@ -176,6 +199,33 @@
176199 return port;
177200 }
178201 EXPORT_SYMBOL_GPL(sfp_parse_port);
202
+
203
+/**
204
+ * sfp_may_have_phy() - indicate whether the module may have a PHY
205
+ * @bus: a pointer to the &struct sfp_bus structure for the sfp module
206
+ * @id: a pointer to the module's &struct sfp_eeprom_id
207
+ *
208
+ * Parse the EEPROM identification given in @id, and return whether
209
+ * this module may have a PHY.
210
+ */
211
+bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
212
+{
213
+ if (id->base.e1000_base_t)
214
+ return true;
215
+
216
+ if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
217
+ switch (id->base.extended_cc) {
218
+ case SFF8024_ECC_10GBASE_T_SFI:
219
+ case SFF8024_ECC_10GBASE_T_SR:
220
+ case SFF8024_ECC_5GBASE_T:
221
+ case SFF8024_ECC_2_5GBASE_T:
222
+ return true;
223
+ }
224
+ }
225
+
226
+ return false;
227
+}
228
+EXPORT_SYMBOL_GPL(sfp_may_have_phy);
179229
180230 /**
181231 * sfp_parse_support() - Parse the eeprom id for supported link modes
....@@ -260,21 +310,32 @@
260310 }
261311
262312 switch (id->base.extended_cc) {
263
- case 0x00: /* Unspecified */
313
+ case SFF8024_ECC_UNSPEC:
264314 break;
265
- case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
315
+ case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
266316 phylink_set(modes, 100000baseSR4_Full);
267317 phylink_set(modes, 25000baseSR_Full);
268318 break;
269
- case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
270
- case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
319
+ case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
320
+ case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
271321 phylink_set(modes, 100000baseLR4_ER4_Full);
272322 break;
273
- case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
274
- case 0x0c: /* 25Gbase-CR CA-S */
275
- case 0x0d: /* 25Gbase-CR CA-N */
323
+ case SFF8024_ECC_100GBASE_CR4:
276324 phylink_set(modes, 100000baseCR4_Full);
325
+ fallthrough;
326
+ case SFF8024_ECC_25GBASE_CR_S:
327
+ case SFF8024_ECC_25GBASE_CR_N:
277328 phylink_set(modes, 25000baseCR_Full);
329
+ break;
330
+ case SFF8024_ECC_10GBASE_T_SFI:
331
+ case SFF8024_ECC_10GBASE_T_SR:
332
+ phylink_set(modes, 10000baseT_Full);
333
+ break;
334
+ case SFF8024_ECC_5GBASE_T:
335
+ phylink_set(modes, 5000baseT_Full);
336
+ break;
337
+ case SFF8024_ECC_2_5GBASE_T:
338
+ phylink_set(modes, 2500baseT_Full);
278339 break;
279340 default:
280341 dev_warn(bus->sfp_dev,
....@@ -294,14 +355,13 @@
294355 }
295356
296357 /* If we haven't discovered any modes that this module supports, try
297
- * the encoding and bitrate to determine supported modes. Some BiDi
298
- * modules (eg, 1310nm/1550nm) are not 1000BASE-BX compliant due to
299
- * the differing wavelengths, so do not set any transceiver bits.
358
+ * the bitrate to determine supported modes. Some BiDi modules (eg,
359
+ * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
360
+ * wavelengths, so do not set any transceiver bits.
300361 */
301362 if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
302
- /* If the encoding and bit rate allows 1000baseX */
303
- if (id->base.encoding == SFP_ENCODING_8B10B && br_nom &&
304
- br_min <= 1300 && br_max >= 1200)
363
+ /* If the bit rate allows 1000baseX */
364
+ if (br_nom && br_min <= 1300 && br_max >= 1200)
305365 phylink_set(modes, 1000baseX_Full);
306366 }
307367
....@@ -319,31 +379,27 @@
319379 /**
320380 * sfp_select_interface() - Select appropriate phy_interface_t mode
321381 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
322
- * @id: a pointer to the module's &struct sfp_eeprom_id
323382 * @link_modes: ethtool link modes mask
324383 *
325
- * Derive the phy_interface_t mode for the information found in the
326
- * module's identifying EEPROM and the link modes mask. There is no
327
- * standard or defined way to derive this information, so we decide
328
- * based upon the link mode mask.
384
+ * Derive the phy_interface_t mode for the SFP module from the link
385
+ * modes mask.
329386 */
330387 phy_interface_t sfp_select_interface(struct sfp_bus *bus,
331
- const struct sfp_eeprom_id *id,
332388 unsigned long *link_modes)
333389 {
334390 if (phylink_test(link_modes, 10000baseCR_Full) ||
335391 phylink_test(link_modes, 10000baseSR_Full) ||
336392 phylink_test(link_modes, 10000baseLR_Full) ||
337393 phylink_test(link_modes, 10000baseLRM_Full) ||
338
- phylink_test(link_modes, 10000baseER_Full))
339
- return PHY_INTERFACE_MODE_10GKR;
394
+ phylink_test(link_modes, 10000baseER_Full) ||
395
+ phylink_test(link_modes, 10000baseT_Full))
396
+ return PHY_INTERFACE_MODE_10GBASER;
340397
341398 if (phylink_test(link_modes, 2500baseX_Full))
342399 return PHY_INTERFACE_MODE_2500BASEX;
343400
344
- if (id->base.e1000_base_t ||
345
- id->base.e100_base_lx ||
346
- id->base.e100_base_fx)
401
+ if (phylink_test(link_modes, 1000baseT_Half) ||
402
+ phylink_test(link_modes, 1000baseT_Full))
347403 return PHY_INTERFACE_MODE_SGMII;
348404
349405 if (phylink_test(link_modes, 1000baseX_Full))
....@@ -403,10 +459,19 @@
403459 kfree(bus);
404460 }
405461
406
-static void sfp_bus_put(struct sfp_bus *bus)
462
+/**
463
+ * sfp_bus_put() - put a reference on the &struct sfp_bus
464
+ * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
465
+ *
466
+ * Put a reference on the &struct sfp_bus and free the underlying structure
467
+ * if this was the last reference.
468
+ */
469
+void sfp_bus_put(struct sfp_bus *bus)
407470 {
408
- kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
471
+ if (bus)
472
+ kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
409473 }
474
+EXPORT_SYMBOL_GPL(sfp_bus_put);
410475
411476 static int sfp_register_bus(struct sfp_bus *bus)
412477 {
....@@ -422,11 +487,11 @@
422487 return ret;
423488 }
424489 }
490
+ bus->registered = true;
425491 bus->socket_ops->attach(bus->sfp);
426492 if (bus->started)
427493 bus->socket_ops->start(bus->sfp);
428
- bus->netdev->sfp_bus = bus;
429
- bus->registered = true;
494
+ bus->upstream_ops->attach(bus->upstream, bus);
430495 return 0;
431496 }
432497
....@@ -434,8 +499,8 @@
434499 {
435500 const struct sfp_upstream_ops *ops = bus->upstream_ops;
436501
437
- bus->netdev->sfp_bus = NULL;
438502 if (bus->registered) {
503
+ bus->upstream_ops->detach(bus->upstream, bus);
439504 if (bus->started)
440505 bus->socket_ops->stop(bus->sfp);
441506 bus->socket_ops->detach(bus->sfp);
....@@ -517,70 +582,123 @@
517582 {
518583 bus->upstream_ops = NULL;
519584 bus->upstream = NULL;
520
- bus->netdev = NULL;
521585 }
522586
523587 /**
524
- * sfp_register_upstream() - Register the neighbouring device
525
- * @fwnode: firmware node for the SFP bus
526
- * @ndev: network device associated with the interface
527
- * @upstream: the upstream private data
528
- * @ops: the upstream's &struct sfp_upstream_ops
588
+ * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
589
+ * @fwnode: firmware node for the parent device (MAC or PHY)
529590 *
530
- * Register the upstream device (eg, PHY) with the SFP bus. MAC drivers
531
- * should use phylink, which will call this function for them. Returns
532
- * a pointer to the allocated &struct sfp_bus.
591
+ * Parse the parent device's firmware node for a SFP bus, and locate
592
+ * the sfp_bus structure, incrementing its reference count. This must
593
+ * be put via sfp_bus_put() when done.
533594 *
534
- * On error, returns %NULL.
595
+ * Returns:
596
+ * - on success, a pointer to the sfp_bus structure,
597
+ * - %NULL if no SFP is specified,
598
+ * - on failure, an error pointer value:
599
+ *
600
+ * - corresponding to the errors detailed for
601
+ * fwnode_property_get_reference_args().
602
+ * - %-ENOMEM if we failed to allocate the bus.
603
+ * - an error from the upstream's connect_phy() method.
535604 */
536
-struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
537
- struct net_device *ndev, void *upstream,
538
- const struct sfp_upstream_ops *ops)
605
+struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
539606 {
540
- struct sfp_bus *bus = sfp_bus_get(fwnode);
541
- int ret = 0;
607
+ struct fwnode_reference_args ref;
608
+ struct sfp_bus *bus;
609
+ int ret;
542610
543
- if (bus) {
544
- rtnl_lock();
545
- bus->upstream_ops = ops;
546
- bus->upstream = upstream;
547
- bus->netdev = ndev;
611
+ ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
612
+ 0, 0, &ref);
613
+ if (ret == -ENOENT)
614
+ return NULL;
615
+ else if (ret < 0)
616
+ return ERR_PTR(ret);
548617
549
- if (bus->sfp) {
550
- ret = sfp_register_bus(bus);
551
- if (ret)
552
- sfp_upstream_clear(bus);
553
- }
554
- rtnl_unlock();
618
+ if (!fwnode_device_is_available(ref.fwnode)) {
619
+ fwnode_handle_put(ref.fwnode);
620
+ return NULL;
555621 }
556622
557
- if (ret) {
558
- sfp_bus_put(bus);
559
- bus = NULL;
560
- }
623
+ bus = sfp_bus_get(ref.fwnode);
624
+ fwnode_handle_put(ref.fwnode);
625
+ if (!bus)
626
+ return ERR_PTR(-ENOMEM);
561627
562628 return bus;
563629 }
564
-EXPORT_SYMBOL_GPL(sfp_register_upstream);
630
+EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
565631
566632 /**
567
- * sfp_unregister_upstream() - Unregister sfp bus
568
- * @bus: a pointer to the &struct sfp_bus structure for the sfp module
633
+ * sfp_bus_add_upstream() - parse and register the neighbouring device
634
+ * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
635
+ * @upstream: the upstream private data
636
+ * @ops: the upstream's &struct sfp_upstream_ops
569637 *
570
- * Unregister a previously registered upstream connection for the SFP
571
- * module. @bus is returned from sfp_register_upstream().
638
+ * Add upstream driver for the SFP bus, and if the bus is complete, register
639
+ * the SFP bus using sfp_register_upstream(). This takes a reference on the
640
+ * bus, so it is safe to put the bus after this call.
641
+ *
642
+ * Returns:
643
+ * - on success, a pointer to the sfp_bus structure,
644
+ * - %NULL if no SFP is specified,
645
+ * - on failure, an error pointer value:
646
+ *
647
+ * - corresponding to the errors detailed for
648
+ * fwnode_property_get_reference_args().
649
+ * - %-ENOMEM if we failed to allocate the bus.
650
+ * - an error from the upstream's connect_phy() method.
572651 */
573
-void sfp_unregister_upstream(struct sfp_bus *bus)
652
+int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
653
+ const struct sfp_upstream_ops *ops)
574654 {
655
+ int ret;
656
+
657
+ /* If no bus, return success */
658
+ if (!bus)
659
+ return 0;
660
+
575661 rtnl_lock();
576
- if (bus->sfp)
577
- sfp_unregister_bus(bus);
578
- sfp_upstream_clear(bus);
662
+ kref_get(&bus->kref);
663
+ bus->upstream_ops = ops;
664
+ bus->upstream = upstream;
665
+
666
+ if (bus->sfp) {
667
+ ret = sfp_register_bus(bus);
668
+ if (ret)
669
+ sfp_upstream_clear(bus);
670
+ } else {
671
+ ret = 0;
672
+ }
579673 rtnl_unlock();
580674
581
- sfp_bus_put(bus);
675
+ if (ret)
676
+ sfp_bus_put(bus);
677
+
678
+ return ret;
582679 }
583
-EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
680
+EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
681
+
682
+/**
683
+ * sfp_bus_del_upstream() - Delete a sfp bus
684
+ * @bus: a pointer to the &struct sfp_bus structure for the sfp module
685
+ *
686
+ * Delete a previously registered upstream connection for the SFP
687
+ * module. @bus should have been added by sfp_bus_add_upstream().
688
+ */
689
+void sfp_bus_del_upstream(struct sfp_bus *bus)
690
+{
691
+ if (bus) {
692
+ rtnl_lock();
693
+ if (bus->sfp)
694
+ sfp_unregister_bus(bus);
695
+ sfp_upstream_clear(bus);
696
+ rtnl_unlock();
697
+
698
+ sfp_bus_put(bus);
699
+ }
700
+}
701
+EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
584702
585703 /* Socket driver entry points */
586704 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
....@@ -651,6 +769,27 @@
651769 }
652770 EXPORT_SYMBOL_GPL(sfp_module_remove);
653771
772
+int sfp_module_start(struct sfp_bus *bus)
773
+{
774
+ const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
775
+ int ret = 0;
776
+
777
+ if (ops && ops->module_start)
778
+ ret = ops->module_start(bus->upstream);
779
+
780
+ return ret;
781
+}
782
+EXPORT_SYMBOL_GPL(sfp_module_start);
783
+
784
+void sfp_module_stop(struct sfp_bus *bus)
785
+{
786
+ const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
787
+
788
+ if (ops && ops->module_stop)
789
+ ops->module_stop(bus->upstream);
790
+}
791
+EXPORT_SYMBOL_GPL(sfp_module_stop);
792
+
654793 static void sfp_socket_clear(struct sfp_bus *bus)
655794 {
656795 bus->sfp_dev = NULL;
....@@ -670,7 +809,7 @@
670809 bus->sfp = sfp;
671810 bus->socket_ops = ops;
672811
673
- if (bus->netdev) {
812
+ if (bus->upstream_ops) {
674813 ret = sfp_register_bus(bus);
675814 if (ret)
676815 sfp_socket_clear(bus);
....@@ -690,7 +829,7 @@
690829 void sfp_unregister_socket(struct sfp_bus *bus)
691830 {
692831 rtnl_lock();
693
- if (bus->netdev)
832
+ if (bus->upstream_ops)
694833 sfp_unregister_bus(bus);
695834 sfp_socket_clear(bus);
696835 rtnl_unlock();