1 #include <linux/export.h>
2 #include <linux/kref.h>
3 #include <linux/list.h>
4 #include <linux/mutex.h>
5 #include <linux/phylink.h>
6 #include <linux/rtnetlink.h>
7 #include <linux/slab.h>
13 struct list_head node;
14 struct device_node *device_node;
16 const struct sfp_socket_ops *socket_ops;
17 struct device *sfp_dev;
20 const struct sfp_upstream_ops *upstream_ops;
22 struct net_device *netdev;
23 struct phy_device *phydev;
29 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
30 unsigned long *support)
34 /* port is the physical connector, set this from the connector field. */
35 switch (id->base.connector) {
36 case SFP_CONNECTOR_SC:
37 case SFP_CONNECTOR_FIBERJACK:
38 case SFP_CONNECTOR_LC:
39 case SFP_CONNECTOR_MT_RJ:
40 case SFP_CONNECTOR_MU:
41 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
43 phylink_set(support, FIBRE);
47 case SFP_CONNECTOR_RJ45:
49 phylink_set(support, TP);
53 case SFP_CONNECTOR_UNSPEC:
54 if (id->base.e1000_base_t) {
56 phylink_set(support, TP);
61 case SFP_CONNECTOR_SG: /* guess */
62 case SFP_CONNECTOR_MPO_1X12:
63 case SFP_CONNECTOR_MPO_2X16:
64 case SFP_CONNECTOR_HSSDC_II:
65 case SFP_CONNECTOR_COPPER_PIGTAIL:
66 case SFP_CONNECTOR_NOSEPARATE:
67 case SFP_CONNECTOR_MXC_2X16:
71 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
79 EXPORT_SYMBOL_GPL(sfp_parse_port);
81 phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
82 const struct sfp_eeprom_id *id)
84 phy_interface_t iface;
86 /* Setting the serdes link mode is guesswork: there's no field in
87 * the EEPROM which indicates what mode should be used.
89 * If the module wants 64b66b, then it must be >= 10G.
91 * If it's a gigabit-only fiber module, it probably does not have
92 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
93 * to SGMII mode (which is required to support non-gigabit speeds).
95 switch (id->base.encoding) {
96 case SFP_ENCODING_8472_64B66B:
97 iface = PHY_INTERFACE_MODE_10GKR;
100 case SFP_ENCODING_8B10B:
101 if (!id->base.e1000_base_t &&
102 !id->base.e100_base_lx &&
103 !id->base.e100_base_fx)
104 iface = PHY_INTERFACE_MODE_1000BASEX;
106 iface = PHY_INTERFACE_MODE_SGMII;
110 iface = PHY_INTERFACE_MODE_NA;
111 dev_err(bus->sfp_dev,
112 "SFP module encoding does not support 8b10b nor 64b66b\n");
118 EXPORT_SYMBOL_GPL(sfp_parse_interface);
120 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
121 unsigned long *support)
123 phylink_set(support, Autoneg);
124 phylink_set(support, Pause);
125 phylink_set(support, Asym_Pause);
127 /* Set ethtool support from the compliance fields. */
128 if (id->base.e10g_base_sr)
129 phylink_set(support, 10000baseSR_Full);
130 if (id->base.e10g_base_lr)
131 phylink_set(support, 10000baseLR_Full);
132 if (id->base.e10g_base_lrm)
133 phylink_set(support, 10000baseLRM_Full);
134 if (id->base.e10g_base_er)
135 phylink_set(support, 10000baseER_Full);
136 if (id->base.e1000_base_sx ||
137 id->base.e1000_base_lx ||
138 id->base.e1000_base_cx)
139 phylink_set(support, 1000baseX_Full);
140 if (id->base.e1000_base_t) {
141 phylink_set(support, 1000baseT_Half);
142 phylink_set(support, 1000baseT_Full);
145 switch (id->base.extended_cc) {
146 case 0x00: /* Unspecified */
148 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
149 phylink_set(support, 100000baseSR4_Full);
150 phylink_set(support, 25000baseSR_Full);
152 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
153 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
154 phylink_set(support, 100000baseLR4_ER4_Full);
156 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
157 case 0x0c: /* 25Gbase-CR CA-S */
158 case 0x0d: /* 25Gbase-CR CA-N */
159 phylink_set(support, 100000baseCR4_Full);
160 phylink_set(support, 25000baseCR_Full);
163 dev_warn(bus->sfp_dev,
164 "Unknown/unsupported extended compliance code: 0x%02x\n",
165 id->base.extended_cc);
169 /* For fibre channel SFP, derive possible BaseX modes */
170 if (id->base.fc_speed_100 ||
171 id->base.fc_speed_200 ||
172 id->base.fc_speed_400) {
173 if (id->base.br_nominal >= 31)
174 phylink_set(support, 2500baseX_Full);
175 if (id->base.br_nominal >= 12)
176 phylink_set(support, 1000baseX_Full);
179 switch (id->base.connector) {
180 case SFP_CONNECTOR_SC:
181 case SFP_CONNECTOR_FIBERJACK:
182 case SFP_CONNECTOR_LC:
183 case SFP_CONNECTOR_MT_RJ:
184 case SFP_CONNECTOR_MU:
185 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
188 case SFP_CONNECTOR_UNSPEC:
189 if (id->base.e1000_base_t)
192 case SFP_CONNECTOR_SG: /* guess */
193 case SFP_CONNECTOR_MPO_1X12:
194 case SFP_CONNECTOR_MPO_2X16:
195 case SFP_CONNECTOR_HSSDC_II:
196 case SFP_CONNECTOR_COPPER_PIGTAIL:
197 case SFP_CONNECTOR_NOSEPARATE:
198 case SFP_CONNECTOR_MXC_2X16:
200 /* a guess at the supported link modes */
201 dev_warn(bus->sfp_dev,
202 "Guessing link modes, please report...\n");
203 phylink_set(support, 1000baseT_Half);
204 phylink_set(support, 1000baseT_Full);
208 EXPORT_SYMBOL_GPL(sfp_parse_support);
210 static LIST_HEAD(sfp_buses);
211 static DEFINE_MUTEX(sfp_mutex);
213 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
215 return bus->registered ? bus->upstream_ops : NULL;
218 static struct sfp_bus *sfp_bus_get(struct device_node *np)
220 struct sfp_bus *sfp, *new, *found = NULL;
222 new = kzalloc(sizeof(*new), GFP_KERNEL);
224 mutex_lock(&sfp_mutex);
226 list_for_each_entry(sfp, &sfp_buses, node) {
227 if (sfp->device_node == np) {
228 kref_get(&sfp->kref);
235 kref_init(&new->kref);
236 new->device_node = np;
237 list_add(&new->node, &sfp_buses);
242 mutex_unlock(&sfp_mutex);
249 static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex)
251 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
253 list_del(&bus->node);
254 mutex_unlock(&sfp_mutex);
258 static void sfp_bus_put(struct sfp_bus *bus)
260 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
263 static int sfp_register_bus(struct sfp_bus *bus)
265 const struct sfp_upstream_ops *ops = bus->upstream_ops;
270 ops->link_down(bus->upstream);
271 if (ops->connect_phy && bus->phydev) {
272 ret = ops->connect_phy(bus->upstream, bus->phydev);
278 bus->socket_ops->start(bus->sfp);
279 bus->registered = true;
283 static void sfp_unregister_bus(struct sfp_bus *bus)
285 const struct sfp_upstream_ops *ops = bus->upstream_ops;
287 if (bus->registered) {
289 bus->socket_ops->stop(bus->sfp);
290 if (bus->phydev && ops && ops->disconnect_phy)
291 ops->disconnect_phy(bus->upstream);
293 bus->registered = false;
296 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
298 if (!bus->registered)
300 return bus->socket_ops->module_info(bus->sfp, modinfo);
302 EXPORT_SYMBOL_GPL(sfp_get_module_info);
304 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
307 if (!bus->registered)
309 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
311 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
313 void sfp_upstream_start(struct sfp_bus *bus)
316 bus->socket_ops->start(bus->sfp);
319 EXPORT_SYMBOL_GPL(sfp_upstream_start);
321 void sfp_upstream_stop(struct sfp_bus *bus)
324 bus->socket_ops->stop(bus->sfp);
325 bus->started = false;
327 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
329 struct sfp_bus *sfp_register_upstream(struct device_node *np,
330 struct net_device *ndev, void *upstream,
331 const struct sfp_upstream_ops *ops)
333 struct sfp_bus *bus = sfp_bus_get(np);
338 bus->upstream_ops = ops;
339 bus->upstream = upstream;
343 ret = sfp_register_bus(bus);
354 EXPORT_SYMBOL_GPL(sfp_register_upstream);
356 void sfp_unregister_upstream(struct sfp_bus *bus)
360 sfp_unregister_bus(bus);
361 bus->upstream = NULL;
367 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
369 /* Socket driver entry points */
370 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
372 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
375 if (ops && ops->connect_phy)
376 ret = ops->connect_phy(bus->upstream, phydev);
379 bus->phydev = phydev;
383 EXPORT_SYMBOL_GPL(sfp_add_phy);
385 void sfp_remove_phy(struct sfp_bus *bus)
387 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
389 if (ops && ops->disconnect_phy)
390 ops->disconnect_phy(bus->upstream);
393 EXPORT_SYMBOL_GPL(sfp_remove_phy);
395 void sfp_link_up(struct sfp_bus *bus)
397 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
399 if (ops && ops->link_up)
400 ops->link_up(bus->upstream);
402 EXPORT_SYMBOL_GPL(sfp_link_up);
404 void sfp_link_down(struct sfp_bus *bus)
406 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
408 if (ops && ops->link_down)
409 ops->link_down(bus->upstream);
411 EXPORT_SYMBOL_GPL(sfp_link_down);
413 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
415 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
418 if (ops && ops->module_insert)
419 ret = ops->module_insert(bus->upstream, id);
423 EXPORT_SYMBOL_GPL(sfp_module_insert);
425 void sfp_module_remove(struct sfp_bus *bus)
427 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
429 if (ops && ops->module_remove)
430 ops->module_remove(bus->upstream);
432 EXPORT_SYMBOL_GPL(sfp_module_remove);
434 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
435 const struct sfp_socket_ops *ops)
437 struct sfp_bus *bus = sfp_bus_get(dev->of_node);
444 bus->socket_ops = ops;
447 ret = sfp_register_bus(bus);
458 EXPORT_SYMBOL_GPL(sfp_register_socket);
460 void sfp_unregister_socket(struct sfp_bus *bus)
464 sfp_unregister_bus(bus);
467 bus->socket_ops = NULL;
472 EXPORT_SYMBOL_GPL(sfp_unregister_socket);