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