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;
30 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
31 unsigned long *support)
35 /* port is the physical connector, set this from the connector field. */
36 switch (id->base.connector) {
37 case SFP_CONNECTOR_SC:
38 case SFP_CONNECTOR_FIBERJACK:
39 case SFP_CONNECTOR_LC:
40 case SFP_CONNECTOR_MT_RJ:
41 case SFP_CONNECTOR_MU:
42 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
44 phylink_set(support, FIBRE);
48 case SFP_CONNECTOR_RJ45:
50 phylink_set(support, TP);
54 case SFP_CONNECTOR_UNSPEC:
55 if (id->base.e1000_base_t) {
57 phylink_set(support, TP);
62 case SFP_CONNECTOR_SG: /* guess */
63 case SFP_CONNECTOR_MPO_1X12:
64 case SFP_CONNECTOR_MPO_2X16:
65 case SFP_CONNECTOR_HSSDC_II:
66 case SFP_CONNECTOR_COPPER_PIGTAIL:
67 case SFP_CONNECTOR_NOSEPARATE:
68 case SFP_CONNECTOR_MXC_2X16:
72 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
80 EXPORT_SYMBOL_GPL(sfp_parse_port);
82 phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
83 const struct sfp_eeprom_id *id)
85 phy_interface_t iface;
87 /* Setting the serdes link mode is guesswork: there's no field in
88 * the EEPROM which indicates what mode should be used.
90 * If the module wants 64b66b, then it must be >= 10G.
92 * If it's a gigabit-only fiber module, it probably does not have
93 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
94 * to SGMII mode (which is required to support non-gigabit speeds).
96 switch (id->base.encoding) {
97 case SFP_ENCODING_8472_64B66B:
98 iface = PHY_INTERFACE_MODE_10GKR;
101 case SFP_ENCODING_8B10B:
102 if (!id->base.e1000_base_t &&
103 !id->base.e100_base_lx &&
104 !id->base.e100_base_fx)
105 iface = PHY_INTERFACE_MODE_1000BASEX;
107 iface = PHY_INTERFACE_MODE_SGMII;
111 iface = PHY_INTERFACE_MODE_NA;
112 dev_err(bus->sfp_dev,
113 "SFP module encoding does not support 8b10b nor 64b66b\n");
119 EXPORT_SYMBOL_GPL(sfp_parse_interface);
121 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
122 unsigned long *support)
124 phylink_set(support, Autoneg);
125 phylink_set(support, Pause);
126 phylink_set(support, Asym_Pause);
128 /* Set ethtool support from the compliance fields. */
129 if (id->base.e10g_base_sr)
130 phylink_set(support, 10000baseSR_Full);
131 if (id->base.e10g_base_lr)
132 phylink_set(support, 10000baseLR_Full);
133 if (id->base.e10g_base_lrm)
134 phylink_set(support, 10000baseLRM_Full);
135 if (id->base.e10g_base_er)
136 phylink_set(support, 10000baseER_Full);
137 if (id->base.e1000_base_sx ||
138 id->base.e1000_base_lx ||
139 id->base.e1000_base_cx)
140 phylink_set(support, 1000baseX_Full);
141 if (id->base.e1000_base_t) {
142 phylink_set(support, 1000baseT_Half);
143 phylink_set(support, 1000baseT_Full);
146 switch (id->base.extended_cc) {
147 case 0x00: /* Unspecified */
149 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
150 phylink_set(support, 100000baseSR4_Full);
151 phylink_set(support, 25000baseSR_Full);
153 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
154 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
155 phylink_set(support, 100000baseLR4_ER4_Full);
157 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
158 case 0x0c: /* 25Gbase-CR CA-S */
159 case 0x0d: /* 25Gbase-CR CA-N */
160 phylink_set(support, 100000baseCR4_Full);
161 phylink_set(support, 25000baseCR_Full);
164 dev_warn(bus->sfp_dev,
165 "Unknown/unsupported extended compliance code: 0x%02x\n",
166 id->base.extended_cc);
170 /* For fibre channel SFP, derive possible BaseX modes */
171 if (id->base.fc_speed_100 ||
172 id->base.fc_speed_200 ||
173 id->base.fc_speed_400) {
174 if (id->base.br_nominal >= 31)
175 phylink_set(support, 2500baseX_Full);
176 if (id->base.br_nominal >= 12)
177 phylink_set(support, 1000baseX_Full);
180 switch (id->base.connector) {
181 case SFP_CONNECTOR_SC:
182 case SFP_CONNECTOR_FIBERJACK:
183 case SFP_CONNECTOR_LC:
184 case SFP_CONNECTOR_MT_RJ:
185 case SFP_CONNECTOR_MU:
186 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
189 case SFP_CONNECTOR_UNSPEC:
190 if (id->base.e1000_base_t)
193 case SFP_CONNECTOR_SG: /* guess */
194 case SFP_CONNECTOR_MPO_1X12:
195 case SFP_CONNECTOR_MPO_2X16:
196 case SFP_CONNECTOR_HSSDC_II:
197 case SFP_CONNECTOR_COPPER_PIGTAIL:
198 case SFP_CONNECTOR_NOSEPARATE:
199 case SFP_CONNECTOR_MXC_2X16:
201 /* a guess at the supported link modes */
202 dev_warn(bus->sfp_dev,
203 "Guessing link modes, please report...\n");
204 phylink_set(support, 1000baseT_Half);
205 phylink_set(support, 1000baseT_Full);
209 EXPORT_SYMBOL_GPL(sfp_parse_support);
212 static LIST_HEAD(sfp_buses);
213 static DEFINE_MUTEX(sfp_mutex);
215 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
217 return bus->registered ? bus->upstream_ops : NULL;
220 static struct sfp_bus *sfp_bus_get(struct device_node *np)
222 struct sfp_bus *sfp, *new, *found = NULL;
224 new = kzalloc(sizeof(*new), GFP_KERNEL);
226 mutex_lock(&sfp_mutex);
228 list_for_each_entry(sfp, &sfp_buses, node) {
229 if (sfp->device_node == np) {
230 kref_get(&sfp->kref);
237 kref_init(&new->kref);
238 new->device_node = np;
239 list_add(&new->node, &sfp_buses);
244 mutex_unlock(&sfp_mutex);
251 static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex)
253 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
255 list_del(&bus->node);
256 mutex_unlock(&sfp_mutex);
260 static void sfp_bus_put(struct sfp_bus *bus)
262 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
265 static int sfp_register_bus(struct sfp_bus *bus)
267 const struct sfp_upstream_ops *ops = bus->upstream_ops;
272 ops->link_down(bus->upstream);
273 if (ops->connect_phy && bus->phydev) {
274 ret = ops->connect_phy(bus->upstream, bus->phydev);
279 bus->socket_ops->attach(bus->sfp);
281 bus->socket_ops->start(bus->sfp);
282 bus->registered = true;
286 static void sfp_unregister_bus(struct sfp_bus *bus)
288 const struct sfp_upstream_ops *ops = bus->upstream_ops;
290 if (bus->registered) {
292 bus->socket_ops->stop(bus->sfp);
293 bus->socket_ops->detach(bus->sfp);
294 if (bus->phydev && ops && ops->disconnect_phy)
295 ops->disconnect_phy(bus->upstream);
297 bus->registered = false;
301 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
303 if (!bus->registered)
305 return bus->socket_ops->module_info(bus->sfp, modinfo);
307 EXPORT_SYMBOL_GPL(sfp_get_module_info);
309 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
312 if (!bus->registered)
314 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
316 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
318 void sfp_upstream_start(struct sfp_bus *bus)
321 bus->socket_ops->start(bus->sfp);
324 EXPORT_SYMBOL_GPL(sfp_upstream_start);
326 void sfp_upstream_stop(struct sfp_bus *bus)
329 bus->socket_ops->stop(bus->sfp);
330 bus->started = false;
332 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
334 struct sfp_bus *sfp_register_upstream(struct device_node *np,
335 struct net_device *ndev, void *upstream,
336 const struct sfp_upstream_ops *ops)
338 struct sfp_bus *bus = sfp_bus_get(np);
343 bus->upstream_ops = ops;
344 bus->upstream = upstream;
348 ret = sfp_register_bus(bus);
359 EXPORT_SYMBOL_GPL(sfp_register_upstream);
361 void sfp_unregister_upstream(struct sfp_bus *bus)
365 sfp_unregister_bus(bus);
366 bus->upstream = NULL;
372 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
375 /* Socket driver entry points */
376 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
378 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
381 if (ops && ops->connect_phy)
382 ret = ops->connect_phy(bus->upstream, phydev);
385 bus->phydev = phydev;
389 EXPORT_SYMBOL_GPL(sfp_add_phy);
391 void sfp_remove_phy(struct sfp_bus *bus)
393 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
395 if (ops && ops->disconnect_phy)
396 ops->disconnect_phy(bus->upstream);
399 EXPORT_SYMBOL_GPL(sfp_remove_phy);
402 void sfp_link_up(struct sfp_bus *bus)
404 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
406 if (ops && ops->link_up)
407 ops->link_up(bus->upstream);
409 EXPORT_SYMBOL_GPL(sfp_link_up);
411 void sfp_link_down(struct sfp_bus *bus)
413 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
415 if (ops && ops->link_down)
416 ops->link_down(bus->upstream);
418 EXPORT_SYMBOL_GPL(sfp_link_down);
420 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
422 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
425 if (ops && ops->module_insert)
426 ret = ops->module_insert(bus->upstream, id);
430 EXPORT_SYMBOL_GPL(sfp_module_insert);
432 void sfp_module_remove(struct sfp_bus *bus)
434 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
436 if (ops && ops->module_remove)
437 ops->module_remove(bus->upstream);
439 EXPORT_SYMBOL_GPL(sfp_module_remove);
441 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
442 const struct sfp_socket_ops *ops)
444 struct sfp_bus *bus = sfp_bus_get(dev->of_node);
451 bus->socket_ops = ops;
454 ret = sfp_register_bus(bus);
465 EXPORT_SYMBOL_GPL(sfp_register_socket);
467 void sfp_unregister_socket(struct sfp_bus *bus)
471 sfp_unregister_bus(bus);
474 bus->socket_ops = NULL;
479 EXPORT_SYMBOL_GPL(sfp_unregister_socket);