net: phylink: add ability to validate a set of interface modes
[linux-2.6-block.git] / drivers / net / phy / sfp-bus.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
ce0aa27f
RK
2#include <linux/export.h>
3#include <linux/kref.h>
4#include <linux/list.h>
5#include <linux/mutex.h>
6#include <linux/phylink.h>
2203cbf2 7#include <linux/property.h>
ce0aa27f
RK
8#include <linux/rtnetlink.h>
9#include <linux/slab.h>
10
11#include "sfp.h"
12
0a6fcd3f
RK
13/**
14 * struct sfp_bus - internal representation of a sfp bus
15 */
ce0aa27f 16struct sfp_bus {
0a6fcd3f 17 /* private: */
ce0aa27f
RK
18 struct kref kref;
19 struct list_head node;
c19bb000 20 struct fwnode_handle *fwnode;
ce0aa27f
RK
21
22 const struct sfp_socket_ops *socket_ops;
23 struct device *sfp_dev;
24 struct sfp *sfp;
b34bb2cb 25 const struct sfp_quirk *sfp_quirk;
ce0aa27f
RK
26
27 const struct sfp_upstream_ops *upstream_ops;
28 void *upstream;
ce0aa27f
RK
29 struct phy_device *phydev;
30
31 bool registered;
32 bool started;
33};
34
0a6fcd3f
RK
35/**
36 * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
37 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
38 * @id: a pointer to the module's &struct sfp_eeprom_id
39 * @support: optional pointer to an array of unsigned long for the
40 * ethtool support mask
41 *
42 * Parse the EEPROM identification given in @id, and return one of
43 * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
44 * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
45 * the connector type.
46 *
47 * If the port type is not known, returns %PORT_OTHER.
48 */
ce0aa27f
RK
49int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
50 unsigned long *support)
51{
52 int port;
53
54 /* port is the physical connector, set this from the connector field. */
55 switch (id->base.connector) {
0fbd26a9
RK
56 case SFF8024_CONNECTOR_SC:
57 case SFF8024_CONNECTOR_FIBERJACK:
58 case SFF8024_CONNECTOR_LC:
59 case SFF8024_CONNECTOR_MT_RJ:
60 case SFF8024_CONNECTOR_MU:
61 case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
62 case SFF8024_CONNECTOR_MPO_1X12:
63 case SFF8024_CONNECTOR_MPO_2X16:
ce0aa27f
RK
64 port = PORT_FIBRE;
65 break;
66
0fbd26a9 67 case SFF8024_CONNECTOR_RJ45:
ce0aa27f
RK
68 port = PORT_TP;
69 break;
70
0fbd26a9 71 case SFF8024_CONNECTOR_COPPER_PIGTAIL:
f10fcbcf
RK
72 port = PORT_DA;
73 break;
74
0fbd26a9 75 case SFF8024_CONNECTOR_UNSPEC:
ce0aa27f 76 if (id->base.e1000_base_t) {
ce0aa27f
RK
77 port = PORT_TP;
78 break;
79 }
df561f66 80 fallthrough;
0fbd26a9
RK
81 case SFF8024_CONNECTOR_SG: /* guess */
82 case SFF8024_CONNECTOR_HSSDC_II:
83 case SFF8024_CONNECTOR_NOSEPARATE:
84 case SFF8024_CONNECTOR_MXC_2X16:
ce0aa27f
RK
85 port = PORT_OTHER;
86 break;
87 default:
88 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
89 id->base.connector);
90 port = PORT_OTHER;
91 break;
92 }
93
f10fcbcf
RK
94 if (support) {
95 switch (port) {
96 case PORT_FIBRE:
97 phylink_set(support, FIBRE);
98 break;
99
100 case PORT_TP:
101 phylink_set(support, TP);
102 break;
103 }
104 }
105
ce0aa27f
RK
106 return port;
107}
108EXPORT_SYMBOL_GPL(sfp_parse_port);
109
52c95600
RK
110/**
111 * sfp_may_have_phy() - indicate whether the module may have a PHY
112 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
113 * @id: a pointer to the module's &struct sfp_eeprom_id
114 *
115 * Parse the EEPROM identification given in @id, and return whether
116 * this module may have a PHY.
117 */
118bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
119{
120 if (id->base.e1000_base_t)
121 return true;
122
123 if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
124 switch (id->base.extended_cc) {
125 case SFF8024_ECC_10GBASE_T_SFI:
126 case SFF8024_ECC_10GBASE_T_SR:
127 case SFF8024_ECC_5GBASE_T:
128 case SFF8024_ECC_2_5GBASE_T:
129 return true;
130 }
131 }
132
133 return false;
134}
135EXPORT_SYMBOL_GPL(sfp_may_have_phy);
136
0a6fcd3f
RK
137/**
138 * sfp_parse_support() - Parse the eeprom id for supported link modes
139 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
140 * @id: a pointer to the module's &struct sfp_eeprom_id
141 * @support: pointer to an array of unsigned long for the ethtool support mask
142 *
143 * Parse the EEPROM identification information and derive the supported
144 * ethtool link modes for the module.
145 */
ce0aa27f
RK
146void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
147 unsigned long *support)
148{
9962acf7 149 unsigned int br_min, br_nom, br_max;
03145864 150 __ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
ce0aa27f 151
9962acf7
RK
152 /* Decode the bitrate information to MBd */
153 br_min = br_nom = br_max = 0;
154 if (id->base.br_nominal) {
155 if (id->base.br_nominal != 255) {
156 br_nom = id->base.br_nominal * 100;
52c5cd1b 157 br_min = br_nom - id->base.br_nominal * id->ext.br_min;
9962acf7
RK
158 br_max = br_nom + id->base.br_nominal * id->ext.br_max;
159 } else if (id->ext.br_max) {
160 br_nom = 250 * id->ext.br_max;
161 br_max = br_nom + br_nom * id->ext.br_min / 100;
162 br_min = br_nom - br_nom * id->ext.br_min / 100;
163 }
2b999ba8
AT
164
165 /* When using passive cables, in case neither BR,min nor BR,max
166 * are specified, set br_min to 0 as the nominal value is then
167 * used as the maximum.
168 */
169 if (br_min == br_max && id->base.sfp_ct_passive)
170 br_min = 0;
9962acf7
RK
171 }
172
ce0aa27f
RK
173 /* Set ethtool support from the compliance fields. */
174 if (id->base.e10g_base_sr)
03145864 175 phylink_set(modes, 10000baseSR_Full);
ce0aa27f 176 if (id->base.e10g_base_lr)
03145864 177 phylink_set(modes, 10000baseLR_Full);
ce0aa27f 178 if (id->base.e10g_base_lrm)
03145864 179 phylink_set(modes, 10000baseLRM_Full);
ce0aa27f 180 if (id->base.e10g_base_er)
03145864 181 phylink_set(modes, 10000baseER_Full);
ce0aa27f
RK
182 if (id->base.e1000_base_sx ||
183 id->base.e1000_base_lx ||
184 id->base.e1000_base_cx)
03145864 185 phylink_set(modes, 1000baseX_Full);
ce0aa27f 186 if (id->base.e1000_base_t) {
03145864
RK
187 phylink_set(modes, 1000baseT_Half);
188 phylink_set(modes, 1000baseT_Full);
ce0aa27f
RK
189 }
190
9962acf7
RK
191 /* 1000Base-PX or 1000Base-BX10 */
192 if ((id->base.e_base_px || id->base.e_base_bx10) &&
193 br_min <= 1300 && br_max >= 1200)
d7f7e001 194 phylink_set(modes, 1000baseX_Full);
9962acf7 195
6e12f35c
BJ
196 /* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */
197 if (id->base.e100_base_fx || id->base.e100_base_lx)
198 phylink_set(modes, 100baseFX_Full);
199 if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100)
200 phylink_set(modes, 100baseFX_Full);
201
f10fcbcf
RK
202 /* For active or passive cables, select the link modes
203 * based on the bit rates and the cable compliance bytes.
204 */
205 if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
206 /* This may look odd, but some manufacturers use 12000MBd */
207 if (br_min <= 12000 && br_max >= 10300)
03145864 208 phylink_set(modes, 10000baseCR_Full);
f10fcbcf 209 if (br_min <= 3200 && br_max >= 3100)
03145864 210 phylink_set(modes, 2500baseX_Full);
f10fcbcf 211 if (br_min <= 1300 && br_max >= 1200)
03145864 212 phylink_set(modes, 1000baseX_Full);
f10fcbcf
RK
213 }
214 if (id->base.sfp_ct_passive) {
215 if (id->base.passive.sff8431_app_e)
03145864 216 phylink_set(modes, 10000baseCR_Full);
f10fcbcf
RK
217 }
218 if (id->base.sfp_ct_active) {
219 if (id->base.active.sff8431_app_e ||
220 id->base.active.sff8431_lim) {
03145864 221 phylink_set(modes, 10000baseCR_Full);
f10fcbcf
RK
222 }
223 }
224
ce0aa27f 225 switch (id->base.extended_cc) {
0fbd26a9 226 case SFF8024_ECC_UNSPEC:
ce0aa27f 227 break;
0fbd26a9 228 case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
03145864
RK
229 phylink_set(modes, 100000baseSR4_Full);
230 phylink_set(modes, 25000baseSR_Full);
ce0aa27f 231 break;
0fbd26a9
RK
232 case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
233 case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
03145864 234 phylink_set(modes, 100000baseLR4_ER4_Full);
ce0aa27f 235 break;
0fbd26a9 236 case SFF8024_ECC_100GBASE_CR4:
03145864 237 phylink_set(modes, 100000baseCR4_Full);
df561f66 238 fallthrough;
0fbd26a9
RK
239 case SFF8024_ECC_25GBASE_CR_S:
240 case SFF8024_ECC_25GBASE_CR_N:
03145864 241 phylink_set(modes, 25000baseCR_Full);
ce0aa27f 242 break;
0fbd26a9
RK
243 case SFF8024_ECC_10GBASE_T_SFI:
244 case SFF8024_ECC_10GBASE_T_SR:
245 phylink_set(modes, 10000baseT_Full);
246 break;
247 case SFF8024_ECC_5GBASE_T:
248 phylink_set(modes, 5000baseT_Full);
249 break;
250 case SFF8024_ECC_2_5GBASE_T:
251 phylink_set(modes, 2500baseT_Full);
252 break;
ce0aa27f
RK
253 default:
254 dev_warn(bus->sfp_dev,
255 "Unknown/unsupported extended compliance code: 0x%02x\n",
256 id->base.extended_cc);
257 break;
258 }
259
260 /* For fibre channel SFP, derive possible BaseX modes */
261 if (id->base.fc_speed_100 ||
262 id->base.fc_speed_200 ||
263 id->base.fc_speed_400) {
264 if (id->base.br_nominal >= 31)
03145864 265 phylink_set(modes, 2500baseX_Full);
ce0aa27f 266 if (id->base.br_nominal >= 12)
03145864 267 phylink_set(modes, 1000baseX_Full);
ce0aa27f 268 }
03145864
RK
269
270 /* If we haven't discovered any modes that this module supports, try
7a77233e
RK
271 * the bitrate to determine supported modes. Some BiDi modules (eg,
272 * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
273 * wavelengths, so do not set any transceiver bits.
a006dbf0
RK
274 *
275 * Do the same for modules supporting 2500BASE-X. Note that some
276 * modules use 2500Mbaud rather than 3100 or 3200Mbaud for
277 * 2500BASE-X, so we allow some slack here.
03145864 278 */
a006dbf0
RK
279 if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS) && br_nom) {
280 if (br_min <= 1300 && br_max >= 1200)
03145864 281 phylink_set(modes, 1000baseX_Full);
a006dbf0
RK
282 if (br_min <= 3200 && br_max >= 2500)
283 phylink_set(modes, 2500baseX_Full);
03145864
RK
284 }
285
73472c83 286 if (bus->sfp_quirk && bus->sfp_quirk->modes)
b34bb2cb
RK
287 bus->sfp_quirk->modes(id, modes);
288
4973056c 289 linkmode_or(support, support, modes);
03145864
RK
290
291 phylink_set(support, Autoneg);
292 phylink_set(support, Pause);
293 phylink_set(support, Asym_Pause);
ce0aa27f
RK
294}
295EXPORT_SYMBOL_GPL(sfp_parse_support);
296
a9c79364
RK
297/**
298 * sfp_select_interface() - Select appropriate phy_interface_t mode
299 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
a9c79364
RK
300 * @link_modes: ethtool link modes mask
301 *
a4516c70
RK
302 * Derive the phy_interface_t mode for the SFP module from the link
303 * modes mask.
a9c79364
RK
304 */
305phy_interface_t sfp_select_interface(struct sfp_bus *bus,
a9c79364
RK
306 unsigned long *link_modes)
307{
452d2c6f
SH
308 if (phylink_test(link_modes, 25000baseCR_Full) ||
309 phylink_test(link_modes, 25000baseKR_Full) ||
310 phylink_test(link_modes, 25000baseSR_Full))
311 return PHY_INTERFACE_MODE_25GBASER;
312
a9c79364
RK
313 if (phylink_test(link_modes, 10000baseCR_Full) ||
314 phylink_test(link_modes, 10000baseSR_Full) ||
315 phylink_test(link_modes, 10000baseLR_Full) ||
316 phylink_test(link_modes, 10000baseLRM_Full) ||
0fbd26a9
RK
317 phylink_test(link_modes, 10000baseER_Full) ||
318 phylink_test(link_modes, 10000baseT_Full))
e0f909bc 319 return PHY_INTERFACE_MODE_10GBASER;
a9c79364 320
cfb971de
MB
321 if (phylink_test(link_modes, 5000baseT_Full))
322 return PHY_INTERFACE_MODE_5GBASER;
323
a9c79364
RK
324 if (phylink_test(link_modes, 2500baseX_Full))
325 return PHY_INTERFACE_MODE_2500BASEX;
326
a4516c70
RK
327 if (phylink_test(link_modes, 1000baseT_Half) ||
328 phylink_test(link_modes, 1000baseT_Full))
a9c79364
RK
329 return PHY_INTERFACE_MODE_SGMII;
330
331 if (phylink_test(link_modes, 1000baseX_Full))
332 return PHY_INTERFACE_MODE_1000BASEX;
333
6e12f35c
BJ
334 if (phylink_test(link_modes, 100baseFX_Full))
335 return PHY_INTERFACE_MODE_100BASEX;
336
a9c79364
RK
337 dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
338
339 return PHY_INTERFACE_MODE_NA;
340}
341EXPORT_SYMBOL_GPL(sfp_select_interface);
342
ce0aa27f
RK
343static LIST_HEAD(sfp_buses);
344static DEFINE_MUTEX(sfp_mutex);
345
346static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
347{
348 return bus->registered ? bus->upstream_ops : NULL;
349}
350
c19bb000 351static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
ce0aa27f
RK
352{
353 struct sfp_bus *sfp, *new, *found = NULL;
354
355 new = kzalloc(sizeof(*new), GFP_KERNEL);
356
357 mutex_lock(&sfp_mutex);
358
359 list_for_each_entry(sfp, &sfp_buses, node) {
c19bb000 360 if (sfp->fwnode == fwnode) {
ce0aa27f
RK
361 kref_get(&sfp->kref);
362 found = sfp;
363 break;
364 }
365 }
366
367 if (!found && new) {
368 kref_init(&new->kref);
c19bb000 369 new->fwnode = fwnode;
ce0aa27f
RK
370 list_add(&new->node, &sfp_buses);
371 found = new;
372 new = NULL;
373 }
374
375 mutex_unlock(&sfp_mutex);
376
377 kfree(new);
378
379 return found;
380}
381
b6e67d6d 382static void sfp_bus_release(struct kref *kref)
ce0aa27f
RK
383{
384 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
385
386 list_del(&bus->node);
387 mutex_unlock(&sfp_mutex);
388 kfree(bus);
389}
390
727b3668
RK
391/**
392 * sfp_bus_put() - put a reference on the &struct sfp_bus
2fca4ac9 393 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
727b3668
RK
394 *
395 * Put a reference on the &struct sfp_bus and free the underlying structure
396 * if this was the last reference.
397 */
398void sfp_bus_put(struct sfp_bus *bus)
ce0aa27f 399{
727b3668
RK
400 if (bus)
401 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
ce0aa27f 402}
727b3668 403EXPORT_SYMBOL_GPL(sfp_bus_put);
ce0aa27f
RK
404
405static int sfp_register_bus(struct sfp_bus *bus)
406{
407 const struct sfp_upstream_ops *ops = bus->upstream_ops;
408 int ret;
409
410 if (ops) {
411 if (ops->link_down)
412 ops->link_down(bus->upstream);
413 if (ops->connect_phy && bus->phydev) {
414 ret = ops->connect_phy(bus->upstream, bus->phydev);
415 if (ret)
416 return ret;
417 }
418 }
727b3668 419 bus->registered = true;
b5bfc21a 420 bus->socket_ops->attach(bus->sfp);
ce0aa27f
RK
421 if (bus->started)
422 bus->socket_ops->start(bus->sfp);
320587e6 423 bus->upstream_ops->attach(bus->upstream, bus);
ce0aa27f
RK
424 return 0;
425}
426
427static void sfp_unregister_bus(struct sfp_bus *bus)
428{
429 const struct sfp_upstream_ops *ops = bus->upstream_ops;
430
431 if (bus->registered) {
320587e6 432 bus->upstream_ops->detach(bus->upstream, bus);
ce0aa27f
RK
433 if (bus->started)
434 bus->socket_ops->stop(bus->sfp);
b5bfc21a 435 bus->socket_ops->detach(bus->sfp);
ce0aa27f
RK
436 if (bus->phydev && ops && ops->disconnect_phy)
437 ops->disconnect_phy(bus->upstream);
438 }
439 bus->registered = false;
440}
441
0a6fcd3f
RK
442/**
443 * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
444 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
445 * @modinfo: a &struct ethtool_modinfo
446 *
447 * Fill in the type and eeprom_len parameters in @modinfo for a module on
448 * the sfp bus specified by @bus.
449 *
450 * Returns 0 on success or a negative errno number.
451 */
ce0aa27f
RK
452int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
453{
ce0aa27f
RK
454 return bus->socket_ops->module_info(bus->sfp, modinfo);
455}
456EXPORT_SYMBOL_GPL(sfp_get_module_info);
457
0a6fcd3f
RK
458/**
459 * sfp_get_module_eeprom() - Read the SFP module EEPROM
460 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
461 * @ee: a &struct ethtool_eeprom
462 * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
463 *
464 * Read the EEPROM as specified by the supplied @ee. See the documentation
465 * for &struct ethtool_eeprom for the region to be read.
466 *
467 * Returns 0 on success or a negative errno number.
468 */
ce0aa27f 469int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
516b29ed 470 u8 *data)
ce0aa27f 471{
ce0aa27f
RK
472 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
473}
474EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
475
d740513f
AL
476/**
477 * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM
478 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
479 * @page: a &struct ethtool_module_eeprom
480 * @extack: extack for reporting problems
481 *
482 * Read an EEPROM page as specified by the supplied @page. See the
483 * documentation for &struct ethtool_module_eeprom for the page to be read.
484 *
485 * Returns 0 on success or a negative errno number. More error
486 * information might be provided via extack
487 */
488int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
489 const struct ethtool_module_eeprom *page,
490 struct netlink_ext_ack *extack)
491{
492 return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack);
493}
494EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page);
495
0a6fcd3f
RK
496/**
497 * sfp_upstream_start() - Inform the SFP that the network device is up
498 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
499 *
500 * Inform the SFP socket that the network device is now up, so that the
501 * module can be enabled by allowing TX_DISABLE to be deasserted. This
502 * should be called from the network device driver's &struct net_device_ops
503 * ndo_open() method.
504 */
ce0aa27f
RK
505void sfp_upstream_start(struct sfp_bus *bus)
506{
507 if (bus->registered)
508 bus->socket_ops->start(bus->sfp);
509 bus->started = true;
510}
511EXPORT_SYMBOL_GPL(sfp_upstream_start);
512
0a6fcd3f
RK
513/**
514 * sfp_upstream_stop() - Inform the SFP that the network device is down
515 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
516 *
517 * Inform the SFP socket that the network device is now up, so that the
518 * module can be disabled by asserting TX_DISABLE, disabling the laser
519 * in optical modules. This should be called from the network device
520 * driver's &struct net_device_ops ndo_stop() method.
521 */
ce0aa27f
RK
522void sfp_upstream_stop(struct sfp_bus *bus)
523{
524 if (bus->registered)
525 bus->socket_ops->stop(bus->sfp);
526 bus->started = false;
527}
528EXPORT_SYMBOL_GPL(sfp_upstream_stop);
529
f20a4c46
RK
530static void sfp_upstream_clear(struct sfp_bus *bus)
531{
532 bus->upstream_ops = NULL;
533 bus->upstream = NULL;
f20a4c46
RK
534}
535
0a6fcd3f 536/**
727b3668 537 * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
2203cbf2 538 * @fwnode: firmware node for the parent device (MAC or PHY)
0a6fcd3f 539 *
727b3668
RK
540 * Parse the parent device's firmware node for a SFP bus, and locate
541 * the sfp_bus structure, incrementing its reference count. This must
542 * be put via sfp_bus_put() when done.
0a6fcd3f 543 *
6497ca07 544 * Returns:
3bdee6a8
WL
545 * - on success, a pointer to the sfp_bus structure,
546 * - %NULL if no SFP is specified,
547 * - on failure, an error pointer value:
6497ca07 548 *
3bdee6a8
WL
549 * - corresponding to the errors detailed for
550 * fwnode_property_get_reference_args().
551 * - %-ENOMEM if we failed to allocate the bus.
552 * - an error from the upstream's connect_phy() method.
0a6fcd3f 553 */
727b3668 554struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
ce0aa27f 555{
2203cbf2
RK
556 struct fwnode_reference_args ref;
557 struct sfp_bus *bus;
558 int ret;
ce0aa27f 559
2203cbf2
RK
560 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
561 0, 0, &ref);
562 if (ret == -ENOENT)
563 return NULL;
564 else if (ret < 0)
565 return ERR_PTR(ret);
ce0aa27f 566
2148927e
MB
567 if (!fwnode_device_is_available(ref.fwnode)) {
568 fwnode_handle_put(ref.fwnode);
569 return NULL;
570 }
571
2203cbf2
RK
572 bus = sfp_bus_get(ref.fwnode);
573 fwnode_handle_put(ref.fwnode);
574 if (!bus)
575 return ERR_PTR(-ENOMEM);
576
727b3668
RK
577 return bus;
578}
579EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
580
581/**
582 * sfp_bus_add_upstream() - parse and register the neighbouring device
583 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
584 * @upstream: the upstream private data
585 * @ops: the upstream's &struct sfp_upstream_ops
586 *
587 * Add upstream driver for the SFP bus, and if the bus is complete, register
588 * the SFP bus using sfp_register_upstream(). This takes a reference on the
589 * bus, so it is safe to put the bus after this call.
590 *
6497ca07 591 * Returns:
3bdee6a8
WL
592 * - on success, a pointer to the sfp_bus structure,
593 * - %NULL if no SFP is specified,
594 * - on failure, an error pointer value:
6497ca07 595 *
3bdee6a8
WL
596 * - corresponding to the errors detailed for
597 * fwnode_property_get_reference_args().
598 * - %-ENOMEM if we failed to allocate the bus.
599 * - an error from the upstream's connect_phy() method.
727b3668
RK
600 */
601int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
602 const struct sfp_upstream_ops *ops)
603{
604 int ret;
605
606 /* If no bus, return success */
607 if (!bus)
608 return 0;
609
2203cbf2 610 rtnl_lock();
727b3668 611 kref_get(&bus->kref);
2203cbf2
RK
612 bus->upstream_ops = ops;
613 bus->upstream = upstream;
614
615 if (bus->sfp) {
616 ret = sfp_register_bus(bus);
617 if (ret)
618 sfp_upstream_clear(bus);
619 } else {
620 ret = 0;
ce0aa27f 621 }
2203cbf2 622 rtnl_unlock();
ce0aa27f 623
727b3668 624 if (ret)
ce0aa27f 625 sfp_bus_put(bus);
ce0aa27f 626
727b3668 627 return ret;
ce0aa27f 628}
727b3668 629EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
ce0aa27f 630
0a6fcd3f 631/**
727b3668 632 * sfp_bus_del_upstream() - Delete a sfp bus
0a6fcd3f
RK
633 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
634 *
727b3668
RK
635 * Delete a previously registered upstream connection for the SFP
636 * module. @bus should have been added by sfp_bus_add_upstream().
0a6fcd3f 637 */
727b3668 638void sfp_bus_del_upstream(struct sfp_bus *bus)
ce0aa27f 639{
727b3668
RK
640 if (bus) {
641 rtnl_lock();
642 if (bus->sfp)
643 sfp_unregister_bus(bus);
644 sfp_upstream_clear(bus);
645 rtnl_unlock();
ce0aa27f 646
727b3668
RK
647 sfp_bus_put(bus);
648 }
ce0aa27f 649}
727b3668 650EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
ce0aa27f 651
ce0aa27f
RK
652/* Socket driver entry points */
653int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
654{
655 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
656 int ret = 0;
657
658 if (ops && ops->connect_phy)
659 ret = ops->connect_phy(bus->upstream, phydev);
660
661 if (ret == 0)
662 bus->phydev = phydev;
663
664 return ret;
665}
666EXPORT_SYMBOL_GPL(sfp_add_phy);
667
668void sfp_remove_phy(struct sfp_bus *bus)
669{
670 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
671
672 if (ops && ops->disconnect_phy)
673 ops->disconnect_phy(bus->upstream);
674 bus->phydev = NULL;
675}
676EXPORT_SYMBOL_GPL(sfp_remove_phy);
677
ce0aa27f
RK
678void sfp_link_up(struct sfp_bus *bus)
679{
680 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
681
682 if (ops && ops->link_up)
683 ops->link_up(bus->upstream);
684}
685EXPORT_SYMBOL_GPL(sfp_link_up);
686
687void sfp_link_down(struct sfp_bus *bus)
688{
689 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
690
691 if (ops && ops->link_down)
692 ops->link_down(bus->upstream);
693}
694EXPORT_SYMBOL_GPL(sfp_link_down);
695
23571c7b
RKO
696int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
697 const struct sfp_quirk *quirk)
ce0aa27f
RK
698{
699 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
700 int ret = 0;
701
23571c7b 702 bus->sfp_quirk = quirk;
b34bb2cb 703
ce0aa27f
RK
704 if (ops && ops->module_insert)
705 ret = ops->module_insert(bus->upstream, id);
706
707 return ret;
708}
709EXPORT_SYMBOL_GPL(sfp_module_insert);
710
711void sfp_module_remove(struct sfp_bus *bus)
712{
713 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
714
715 if (ops && ops->module_remove)
716 ops->module_remove(bus->upstream);
b34bb2cb
RK
717
718 bus->sfp_quirk = NULL;
ce0aa27f
RK
719}
720EXPORT_SYMBOL_GPL(sfp_module_remove);
721
74c551ca
RK
722int sfp_module_start(struct sfp_bus *bus)
723{
724 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
725 int ret = 0;
726
727 if (ops && ops->module_start)
728 ret = ops->module_start(bus->upstream);
729
730 return ret;
731}
732EXPORT_SYMBOL_GPL(sfp_module_start);
733
734void sfp_module_stop(struct sfp_bus *bus)
735{
736 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
737
738 if (ops && ops->module_stop)
739 ops->module_stop(bus->upstream);
740}
741EXPORT_SYMBOL_GPL(sfp_module_stop);
742
f20a4c46
RK
743static void sfp_socket_clear(struct sfp_bus *bus)
744{
745 bus->sfp_dev = NULL;
746 bus->sfp = NULL;
747 bus->socket_ops = NULL;
748}
749
ce0aa27f
RK
750struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
751 const struct sfp_socket_ops *ops)
752{
c19bb000 753 struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
ce0aa27f
RK
754 int ret = 0;
755
756 if (bus) {
757 rtnl_lock();
758 bus->sfp_dev = dev;
759 bus->sfp = sfp;
760 bus->socket_ops = ops;
761
54f70b3b 762 if (bus->upstream_ops) {
ce0aa27f 763 ret = sfp_register_bus(bus);
f20a4c46
RK
764 if (ret)
765 sfp_socket_clear(bus);
766 }
ce0aa27f
RK
767 rtnl_unlock();
768 }
769
770 if (ret) {
771 sfp_bus_put(bus);
772 bus = NULL;
773 }
774
775 return bus;
776}
777EXPORT_SYMBOL_GPL(sfp_register_socket);
778
779void sfp_unregister_socket(struct sfp_bus *bus)
780{
781 rtnl_lock();
54f70b3b 782 if (bus->upstream_ops)
0b2122e4 783 sfp_unregister_bus(bus);
f20a4c46 784 sfp_socket_clear(bus);
ce0aa27f
RK
785 rtnl_unlock();
786
787 sfp_bus_put(bus);
788}
789EXPORT_SYMBOL_GPL(sfp_unregister_socket);