xref: /linux/drivers/net/phy/sfp-bus.c (revision 6ebe6dbd6886af07b102aca42e44edbee94a22d9)
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>
8 
9 #include "sfp.h"
10 
11 /**
12  * struct sfp_bus - internal representation of a sfp bus
13  */
14 struct sfp_bus {
15 	/* private: */
16 	struct kref kref;
17 	struct list_head node;
18 	struct fwnode_handle *fwnode;
19 
20 	const struct sfp_socket_ops *socket_ops;
21 	struct device *sfp_dev;
22 	struct sfp *sfp;
23 
24 	const struct sfp_upstream_ops *upstream_ops;
25 	void *upstream;
26 	struct net_device *netdev;
27 	struct phy_device *phydev;
28 
29 	bool registered;
30 	bool started;
31 };
32 
33 /**
34  * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
35  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
36  * @id: a pointer to the module's &struct sfp_eeprom_id
37  * @support: optional pointer to an array of unsigned long for the
38  *   ethtool support mask
39  *
40  * Parse the EEPROM identification given in @id, and return one of
41  * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
42  * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
43  * the connector type.
44  *
45  * If the port type is not known, returns %PORT_OTHER.
46  */
47 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
48 		   unsigned long *support)
49 {
50 	int port;
51 
52 	/* port is the physical connector, set this from the connector field. */
53 	switch (id->base.connector) {
54 	case SFP_CONNECTOR_SC:
55 	case SFP_CONNECTOR_FIBERJACK:
56 	case SFP_CONNECTOR_LC:
57 	case SFP_CONNECTOR_MT_RJ:
58 	case SFP_CONNECTOR_MU:
59 	case SFP_CONNECTOR_OPTICAL_PIGTAIL:
60 		if (support)
61 			phylink_set(support, FIBRE);
62 		port = PORT_FIBRE;
63 		break;
64 
65 	case SFP_CONNECTOR_RJ45:
66 		if (support)
67 			phylink_set(support, TP);
68 		port = PORT_TP;
69 		break;
70 
71 	case SFP_CONNECTOR_UNSPEC:
72 		if (id->base.e1000_base_t) {
73 			if (support)
74 				phylink_set(support, TP);
75 			port = PORT_TP;
76 			break;
77 		}
78 		/* fallthrough */
79 	case SFP_CONNECTOR_SG: /* guess */
80 	case SFP_CONNECTOR_MPO_1X12:
81 	case SFP_CONNECTOR_MPO_2X16:
82 	case SFP_CONNECTOR_HSSDC_II:
83 	case SFP_CONNECTOR_COPPER_PIGTAIL:
84 	case SFP_CONNECTOR_NOSEPARATE:
85 	case SFP_CONNECTOR_MXC_2X16:
86 		port = PORT_OTHER;
87 		break;
88 	default:
89 		dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
90 			 id->base.connector);
91 		port = PORT_OTHER;
92 		break;
93 	}
94 
95 	return port;
96 }
97 EXPORT_SYMBOL_GPL(sfp_parse_port);
98 
99 /**
100  * sfp_parse_interface() - Parse the phy_interface_t
101  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
102  * @id: a pointer to the module's &struct sfp_eeprom_id
103  *
104  * Derive the phy_interface_t mode for the information found in the
105  * module's identifying EEPROM. There is no standard or defined way
106  * to derive this information, so we use some heuristics.
107  *
108  * If the encoding is 64b66b, then the module must be >= 10G, so
109  * return %PHY_INTERFACE_MODE_10GKR.
110  *
111  * If it's 8b10b, then it's 1G or slower. If it's definitely a fibre
112  * module, return %PHY_INTERFACE_MODE_1000BASEX mode, otherwise return
113  * %PHY_INTERFACE_MODE_SGMII mode.
114  *
115  * If the encoding is not known, return %PHY_INTERFACE_MODE_NA.
116  */
117 phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
118 				    const struct sfp_eeprom_id *id)
119 {
120 	phy_interface_t iface;
121 
122 	/* Setting the serdes link mode is guesswork: there's no field in
123 	 * the EEPROM which indicates what mode should be used.
124 	 *
125 	 * If the module wants 64b66b, then it must be >= 10G.
126 	 *
127 	 * If it's a gigabit-only fiber module, it probably does not have
128 	 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
129 	 * to SGMII mode (which is required to support non-gigabit speeds).
130 	 */
131 	switch (id->base.encoding) {
132 	case SFP_ENCODING_8472_64B66B:
133 		iface = PHY_INTERFACE_MODE_10GKR;
134 		break;
135 
136 	case SFP_ENCODING_8B10B:
137 		if (!id->base.e1000_base_t &&
138 		    !id->base.e100_base_lx &&
139 		    !id->base.e100_base_fx)
140 			iface = PHY_INTERFACE_MODE_1000BASEX;
141 		else
142 			iface = PHY_INTERFACE_MODE_SGMII;
143 		break;
144 
145 	default:
146 		iface = PHY_INTERFACE_MODE_NA;
147 		dev_err(bus->sfp_dev,
148 			"SFP module encoding does not support 8b10b nor 64b66b\n");
149 		break;
150 	}
151 
152 	return iface;
153 }
154 EXPORT_SYMBOL_GPL(sfp_parse_interface);
155 
156 /**
157  * sfp_parse_support() - Parse the eeprom id for supported link modes
158  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
159  * @id: a pointer to the module's &struct sfp_eeprom_id
160  * @support: pointer to an array of unsigned long for the ethtool support mask
161  *
162  * Parse the EEPROM identification information and derive the supported
163  * ethtool link modes for the module.
164  */
165 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
166 		       unsigned long *support)
167 {
168 	phylink_set(support, Autoneg);
169 	phylink_set(support, Pause);
170 	phylink_set(support, Asym_Pause);
171 
172 	/* Set ethtool support from the compliance fields. */
173 	if (id->base.e10g_base_sr)
174 		phylink_set(support, 10000baseSR_Full);
175 	if (id->base.e10g_base_lr)
176 		phylink_set(support, 10000baseLR_Full);
177 	if (id->base.e10g_base_lrm)
178 		phylink_set(support, 10000baseLRM_Full);
179 	if (id->base.e10g_base_er)
180 		phylink_set(support, 10000baseER_Full);
181 	if (id->base.e1000_base_sx ||
182 	    id->base.e1000_base_lx ||
183 	    id->base.e1000_base_cx)
184 		phylink_set(support, 1000baseX_Full);
185 	if (id->base.e1000_base_t) {
186 		phylink_set(support, 1000baseT_Half);
187 		phylink_set(support, 1000baseT_Full);
188 	}
189 
190 	switch (id->base.extended_cc) {
191 	case 0x00: /* Unspecified */
192 		break;
193 	case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
194 		phylink_set(support, 100000baseSR4_Full);
195 		phylink_set(support, 25000baseSR_Full);
196 		break;
197 	case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
198 	case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
199 		phylink_set(support, 100000baseLR4_ER4_Full);
200 		break;
201 	case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
202 	case 0x0c: /* 25Gbase-CR CA-S */
203 	case 0x0d: /* 25Gbase-CR CA-N */
204 		phylink_set(support, 100000baseCR4_Full);
205 		phylink_set(support, 25000baseCR_Full);
206 		break;
207 	default:
208 		dev_warn(bus->sfp_dev,
209 			 "Unknown/unsupported extended compliance code: 0x%02x\n",
210 			 id->base.extended_cc);
211 		break;
212 	}
213 
214 	/* For fibre channel SFP, derive possible BaseX modes */
215 	if (id->base.fc_speed_100 ||
216 	    id->base.fc_speed_200 ||
217 	    id->base.fc_speed_400) {
218 		if (id->base.br_nominal >= 31)
219 			phylink_set(support, 2500baseX_Full);
220 		if (id->base.br_nominal >= 12)
221 			phylink_set(support, 1000baseX_Full);
222 	}
223 
224 	switch (id->base.connector) {
225 	case SFP_CONNECTOR_SC:
226 	case SFP_CONNECTOR_FIBERJACK:
227 	case SFP_CONNECTOR_LC:
228 	case SFP_CONNECTOR_MT_RJ:
229 	case SFP_CONNECTOR_MU:
230 	case SFP_CONNECTOR_OPTICAL_PIGTAIL:
231 		break;
232 
233 	case SFP_CONNECTOR_UNSPEC:
234 		if (id->base.e1000_base_t)
235 			break;
236 
237 	case SFP_CONNECTOR_SG: /* guess */
238 	case SFP_CONNECTOR_MPO_1X12:
239 	case SFP_CONNECTOR_MPO_2X16:
240 	case SFP_CONNECTOR_HSSDC_II:
241 	case SFP_CONNECTOR_COPPER_PIGTAIL:
242 	case SFP_CONNECTOR_NOSEPARATE:
243 	case SFP_CONNECTOR_MXC_2X16:
244 	default:
245 		/* a guess at the supported link modes */
246 		dev_warn(bus->sfp_dev,
247 			 "Guessing link modes, please report...\n");
248 		phylink_set(support, 1000baseT_Half);
249 		phylink_set(support, 1000baseT_Full);
250 		break;
251 	}
252 }
253 EXPORT_SYMBOL_GPL(sfp_parse_support);
254 
255 static LIST_HEAD(sfp_buses);
256 static DEFINE_MUTEX(sfp_mutex);
257 
258 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
259 {
260 	return bus->registered ? bus->upstream_ops : NULL;
261 }
262 
263 static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
264 {
265 	struct sfp_bus *sfp, *new, *found = NULL;
266 
267 	new = kzalloc(sizeof(*new), GFP_KERNEL);
268 
269 	mutex_lock(&sfp_mutex);
270 
271 	list_for_each_entry(sfp, &sfp_buses, node) {
272 		if (sfp->fwnode == fwnode) {
273 			kref_get(&sfp->kref);
274 			found = sfp;
275 			break;
276 		}
277 	}
278 
279 	if (!found && new) {
280 		kref_init(&new->kref);
281 		new->fwnode = fwnode;
282 		list_add(&new->node, &sfp_buses);
283 		found = new;
284 		new = NULL;
285 	}
286 
287 	mutex_unlock(&sfp_mutex);
288 
289 	kfree(new);
290 
291 	return found;
292 }
293 
294 static void sfp_bus_release(struct kref *kref)
295 {
296 	struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
297 
298 	list_del(&bus->node);
299 	mutex_unlock(&sfp_mutex);
300 	kfree(bus);
301 }
302 
303 static void sfp_bus_put(struct sfp_bus *bus)
304 {
305 	kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
306 }
307 
308 static int sfp_register_bus(struct sfp_bus *bus)
309 {
310 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
311 	int ret;
312 
313 	if (ops) {
314 		if (ops->link_down)
315 			ops->link_down(bus->upstream);
316 		if (ops->connect_phy && bus->phydev) {
317 			ret = ops->connect_phy(bus->upstream, bus->phydev);
318 			if (ret)
319 				return ret;
320 		}
321 	}
322 	if (bus->started)
323 		bus->socket_ops->start(bus->sfp);
324 	bus->registered = true;
325 	return 0;
326 }
327 
328 static void sfp_unregister_bus(struct sfp_bus *bus)
329 {
330 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
331 
332 	if (bus->registered) {
333 		if (bus->started)
334 			bus->socket_ops->stop(bus->sfp);
335 		if (bus->phydev && ops && ops->disconnect_phy)
336 			ops->disconnect_phy(bus->upstream);
337 	}
338 	bus->registered = false;
339 }
340 
341 /**
342  * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
343  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
344  * @modinfo: a &struct ethtool_modinfo
345  *
346  * Fill in the type and eeprom_len parameters in @modinfo for a module on
347  * the sfp bus specified by @bus.
348  *
349  * Returns 0 on success or a negative errno number.
350  */
351 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
352 {
353 	if (!bus->registered)
354 		return -ENOIOCTLCMD;
355 	return bus->socket_ops->module_info(bus->sfp, modinfo);
356 }
357 EXPORT_SYMBOL_GPL(sfp_get_module_info);
358 
359 /**
360  * sfp_get_module_eeprom() - Read the SFP module EEPROM
361  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
362  * @ee: a &struct ethtool_eeprom
363  * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
364  *
365  * Read the EEPROM as specified by the supplied @ee. See the documentation
366  * for &struct ethtool_eeprom for the region to be read.
367  *
368  * Returns 0 on success or a negative errno number.
369  */
370 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
371 			  u8 *data)
372 {
373 	if (!bus->registered)
374 		return -ENOIOCTLCMD;
375 	return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
376 }
377 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
378 
379 /**
380  * sfp_upstream_start() - Inform the SFP that the network device is up
381  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
382  *
383  * Inform the SFP socket that the network device is now up, so that the
384  * module can be enabled by allowing TX_DISABLE to be deasserted. This
385  * should be called from the network device driver's &struct net_device_ops
386  * ndo_open() method.
387  */
388 void sfp_upstream_start(struct sfp_bus *bus)
389 {
390 	if (bus->registered)
391 		bus->socket_ops->start(bus->sfp);
392 	bus->started = true;
393 }
394 EXPORT_SYMBOL_GPL(sfp_upstream_start);
395 
396 /**
397  * sfp_upstream_stop() - Inform the SFP that the network device is down
398  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
399  *
400  * Inform the SFP socket that the network device is now up, so that the
401  * module can be disabled by asserting TX_DISABLE, disabling the laser
402  * in optical modules. This should be called from the network device
403  * driver's &struct net_device_ops ndo_stop() method.
404  */
405 void sfp_upstream_stop(struct sfp_bus *bus)
406 {
407 	if (bus->registered)
408 		bus->socket_ops->stop(bus->sfp);
409 	bus->started = false;
410 }
411 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
412 
413 /**
414  * sfp_register_upstream() - Register the neighbouring device
415  * @np: device node for the SFP bus
416  * @ndev: network device associated with the interface
417  * @upstream: the upstream private data
418  * @ops: the upstream's &struct sfp_upstream_ops
419  *
420  * Register the upstream device (eg, PHY) with the SFP bus. MAC drivers
421  * should use phylink, which will call this function for them. Returns
422  * a pointer to the allocated &struct sfp_bus.
423  *
424  * On error, returns %NULL.
425  */
426 struct sfp_bus *sfp_register_upstream(struct fwnode_handle *fwnode,
427 				      struct net_device *ndev, void *upstream,
428 				      const struct sfp_upstream_ops *ops)
429 {
430 	struct sfp_bus *bus = sfp_bus_get(fwnode);
431 	int ret = 0;
432 
433 	if (bus) {
434 		rtnl_lock();
435 		bus->upstream_ops = ops;
436 		bus->upstream = upstream;
437 		bus->netdev = ndev;
438 
439 		if (bus->sfp)
440 			ret = sfp_register_bus(bus);
441 		rtnl_unlock();
442 	}
443 
444 	if (ret) {
445 		sfp_bus_put(bus);
446 		bus = NULL;
447 	}
448 
449 	return bus;
450 }
451 EXPORT_SYMBOL_GPL(sfp_register_upstream);
452 
453 /**
454  * sfp_unregister_upstream() - Unregister sfp bus
455  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
456  *
457  * Unregister a previously registered upstream connection for the SFP
458  * module. @bus is returned from sfp_register_upstream().
459  */
460 void sfp_unregister_upstream(struct sfp_bus *bus)
461 {
462 	rtnl_lock();
463 	sfp_unregister_bus(bus);
464 	bus->upstream = NULL;
465 	bus->netdev = NULL;
466 	rtnl_unlock();
467 
468 	sfp_bus_put(bus);
469 }
470 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
471 
472 /* Socket driver entry points */
473 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
474 {
475 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
476 	int ret = 0;
477 
478 	if (ops && ops->connect_phy)
479 		ret = ops->connect_phy(bus->upstream, phydev);
480 
481 	if (ret == 0)
482 		bus->phydev = phydev;
483 
484 	return ret;
485 }
486 EXPORT_SYMBOL_GPL(sfp_add_phy);
487 
488 void sfp_remove_phy(struct sfp_bus *bus)
489 {
490 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
491 
492 	if (ops && ops->disconnect_phy)
493 		ops->disconnect_phy(bus->upstream);
494 	bus->phydev = NULL;
495 }
496 EXPORT_SYMBOL_GPL(sfp_remove_phy);
497 
498 void sfp_link_up(struct sfp_bus *bus)
499 {
500 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
501 
502 	if (ops && ops->link_up)
503 		ops->link_up(bus->upstream);
504 }
505 EXPORT_SYMBOL_GPL(sfp_link_up);
506 
507 void sfp_link_down(struct sfp_bus *bus)
508 {
509 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
510 
511 	if (ops && ops->link_down)
512 		ops->link_down(bus->upstream);
513 }
514 EXPORT_SYMBOL_GPL(sfp_link_down);
515 
516 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
517 {
518 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
519 	int ret = 0;
520 
521 	if (ops && ops->module_insert)
522 		ret = ops->module_insert(bus->upstream, id);
523 
524 	return ret;
525 }
526 EXPORT_SYMBOL_GPL(sfp_module_insert);
527 
528 void sfp_module_remove(struct sfp_bus *bus)
529 {
530 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
531 
532 	if (ops && ops->module_remove)
533 		ops->module_remove(bus->upstream);
534 }
535 EXPORT_SYMBOL_GPL(sfp_module_remove);
536 
537 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
538 				    const struct sfp_socket_ops *ops)
539 {
540 	struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
541 	int ret = 0;
542 
543 	if (bus) {
544 		rtnl_lock();
545 		bus->sfp_dev = dev;
546 		bus->sfp = sfp;
547 		bus->socket_ops = ops;
548 
549 		if (bus->netdev)
550 			ret = sfp_register_bus(bus);
551 		rtnl_unlock();
552 	}
553 
554 	if (ret) {
555 		sfp_bus_put(bus);
556 		bus = NULL;
557 	}
558 
559 	return bus;
560 }
561 EXPORT_SYMBOL_GPL(sfp_register_socket);
562 
563 void sfp_unregister_socket(struct sfp_bus *bus)
564 {
565 	rtnl_lock();
566 	sfp_unregister_bus(bus);
567 	bus->sfp_dev = NULL;
568 	bus->sfp = NULL;
569 	bus->socket_ops = NULL;
570 	rtnl_unlock();
571 
572 	sfp_bus_put(bus);
573 }
574 EXPORT_SYMBOL_GPL(sfp_unregister_socket);
575