mei: me: add cannon point device ids
[linux/fpc-iii.git] / drivers / net / phy / sfp-bus.c
blobab64a142b832c20328f73c691fb566b78c2c8399
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>
9 #include "sfp.h"
11 struct sfp_bus {
12 struct kref kref;
13 struct list_head node;
14 struct device_node *device_node;
16 const struct sfp_socket_ops *socket_ops;
17 struct device *sfp_dev;
18 struct sfp *sfp;
20 const struct sfp_upstream_ops *upstream_ops;
21 void *upstream;
22 struct net_device *netdev;
23 struct phy_device *phydev;
25 bool registered;
26 bool started;
29 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
30 unsigned long *support)
32 int port;
34 /* port is the physical connector, set this from the connector field. */
35 switch (id->base.connector) {
36 case SFP_CONNECTOR_SC:
37 case SFP_CONNECTOR_FIBERJACK:
38 case SFP_CONNECTOR_LC:
39 case SFP_CONNECTOR_MT_RJ:
40 case SFP_CONNECTOR_MU:
41 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
42 if (support)
43 phylink_set(support, FIBRE);
44 port = PORT_FIBRE;
45 break;
47 case SFP_CONNECTOR_RJ45:
48 if (support)
49 phylink_set(support, TP);
50 port = PORT_TP;
51 break;
53 case SFP_CONNECTOR_UNSPEC:
54 if (id->base.e1000_base_t) {
55 if (support)
56 phylink_set(support, TP);
57 port = PORT_TP;
58 break;
60 /* fallthrough */
61 case SFP_CONNECTOR_SG: /* guess */
62 case SFP_CONNECTOR_MPO_1X12:
63 case SFP_CONNECTOR_MPO_2X16:
64 case SFP_CONNECTOR_HSSDC_II:
65 case SFP_CONNECTOR_COPPER_PIGTAIL:
66 case SFP_CONNECTOR_NOSEPARATE:
67 case SFP_CONNECTOR_MXC_2X16:
68 port = PORT_OTHER;
69 break;
70 default:
71 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
72 id->base.connector);
73 port = PORT_OTHER;
74 break;
77 return port;
79 EXPORT_SYMBOL_GPL(sfp_parse_port);
81 phy_interface_t sfp_parse_interface(struct sfp_bus *bus,
82 const struct sfp_eeprom_id *id)
84 phy_interface_t iface;
86 /* Setting the serdes link mode is guesswork: there's no field in
87 * the EEPROM which indicates what mode should be used.
89 * If the module wants 64b66b, then it must be >= 10G.
91 * If it's a gigabit-only fiber module, it probably does not have
92 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch
93 * to SGMII mode (which is required to support non-gigabit speeds).
95 switch (id->base.encoding) {
96 case SFP_ENCODING_8472_64B66B:
97 iface = PHY_INTERFACE_MODE_10GKR;
98 break;
100 case SFP_ENCODING_8B10B:
101 if (!id->base.e1000_base_t &&
102 !id->base.e100_base_lx &&
103 !id->base.e100_base_fx)
104 iface = PHY_INTERFACE_MODE_1000BASEX;
105 else
106 iface = PHY_INTERFACE_MODE_SGMII;
107 break;
109 default:
110 iface = PHY_INTERFACE_MODE_NA;
111 dev_err(bus->sfp_dev,
112 "SFP module encoding does not support 8b10b nor 64b66b\n");
113 break;
116 return iface;
118 EXPORT_SYMBOL_GPL(sfp_parse_interface);
120 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
121 unsigned long *support)
123 phylink_set(support, Autoneg);
124 phylink_set(support, Pause);
125 phylink_set(support, Asym_Pause);
127 /* Set ethtool support from the compliance fields. */
128 if (id->base.e10g_base_sr)
129 phylink_set(support, 10000baseSR_Full);
130 if (id->base.e10g_base_lr)
131 phylink_set(support, 10000baseLR_Full);
132 if (id->base.e10g_base_lrm)
133 phylink_set(support, 10000baseLRM_Full);
134 if (id->base.e10g_base_er)
135 phylink_set(support, 10000baseER_Full);
136 if (id->base.e1000_base_sx ||
137 id->base.e1000_base_lx ||
138 id->base.e1000_base_cx)
139 phylink_set(support, 1000baseX_Full);
140 if (id->base.e1000_base_t) {
141 phylink_set(support, 1000baseT_Half);
142 phylink_set(support, 1000baseT_Full);
145 switch (id->base.extended_cc) {
146 case 0x00: /* Unspecified */
147 break;
148 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */
149 phylink_set(support, 100000baseSR4_Full);
150 phylink_set(support, 25000baseSR_Full);
151 break;
152 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */
153 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */
154 phylink_set(support, 100000baseLR4_ER4_Full);
155 break;
156 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */
157 case 0x0c: /* 25Gbase-CR CA-S */
158 case 0x0d: /* 25Gbase-CR CA-N */
159 phylink_set(support, 100000baseCR4_Full);
160 phylink_set(support, 25000baseCR_Full);
161 break;
162 default:
163 dev_warn(bus->sfp_dev,
164 "Unknown/unsupported extended compliance code: 0x%02x\n",
165 id->base.extended_cc);
166 break;
169 /* For fibre channel SFP, derive possible BaseX modes */
170 if (id->base.fc_speed_100 ||
171 id->base.fc_speed_200 ||
172 id->base.fc_speed_400) {
173 if (id->base.br_nominal >= 31)
174 phylink_set(support, 2500baseX_Full);
175 if (id->base.br_nominal >= 12)
176 phylink_set(support, 1000baseX_Full);
179 switch (id->base.connector) {
180 case SFP_CONNECTOR_SC:
181 case SFP_CONNECTOR_FIBERJACK:
182 case SFP_CONNECTOR_LC:
183 case SFP_CONNECTOR_MT_RJ:
184 case SFP_CONNECTOR_MU:
185 case SFP_CONNECTOR_OPTICAL_PIGTAIL:
186 break;
188 case SFP_CONNECTOR_UNSPEC:
189 if (id->base.e1000_base_t)
190 break;
192 case SFP_CONNECTOR_SG: /* guess */
193 case SFP_CONNECTOR_MPO_1X12:
194 case SFP_CONNECTOR_MPO_2X16:
195 case SFP_CONNECTOR_HSSDC_II:
196 case SFP_CONNECTOR_COPPER_PIGTAIL:
197 case SFP_CONNECTOR_NOSEPARATE:
198 case SFP_CONNECTOR_MXC_2X16:
199 default:
200 /* a guess at the supported link modes */
201 dev_warn(bus->sfp_dev,
202 "Guessing link modes, please report...\n");
203 phylink_set(support, 1000baseT_Half);
204 phylink_set(support, 1000baseT_Full);
205 break;
208 EXPORT_SYMBOL_GPL(sfp_parse_support);
210 static LIST_HEAD(sfp_buses);
211 static DEFINE_MUTEX(sfp_mutex);
213 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
215 return bus->registered ? bus->upstream_ops : NULL;
218 static struct sfp_bus *sfp_bus_get(struct device_node *np)
220 struct sfp_bus *sfp, *new, *found = NULL;
222 new = kzalloc(sizeof(*new), GFP_KERNEL);
224 mutex_lock(&sfp_mutex);
226 list_for_each_entry(sfp, &sfp_buses, node) {
227 if (sfp->device_node == np) {
228 kref_get(&sfp->kref);
229 found = sfp;
230 break;
234 if (!found && new) {
235 kref_init(&new->kref);
236 new->device_node = np;
237 list_add(&new->node, &sfp_buses);
238 found = new;
239 new = NULL;
242 mutex_unlock(&sfp_mutex);
244 kfree(new);
246 return found;
249 static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex)
251 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
253 list_del(&bus->node);
254 mutex_unlock(&sfp_mutex);
255 kfree(bus);
258 static void sfp_bus_put(struct sfp_bus *bus)
260 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
263 static int sfp_register_bus(struct sfp_bus *bus)
265 const struct sfp_upstream_ops *ops = bus->upstream_ops;
266 int ret;
268 if (ops) {
269 if (ops->link_down)
270 ops->link_down(bus->upstream);
271 if (ops->connect_phy && bus->phydev) {
272 ret = ops->connect_phy(bus->upstream, bus->phydev);
273 if (ret)
274 return ret;
277 if (bus->started)
278 bus->socket_ops->start(bus->sfp);
279 bus->registered = true;
280 return 0;
283 static void sfp_unregister_bus(struct sfp_bus *bus)
285 const struct sfp_upstream_ops *ops = bus->upstream_ops;
287 if (bus->registered) {
288 if (bus->started)
289 bus->socket_ops->stop(bus->sfp);
290 if (bus->phydev && ops && ops->disconnect_phy)
291 ops->disconnect_phy(bus->upstream);
293 bus->registered = false;
296 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
298 if (!bus->registered)
299 return -ENOIOCTLCMD;
300 return bus->socket_ops->module_info(bus->sfp, modinfo);
302 EXPORT_SYMBOL_GPL(sfp_get_module_info);
304 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
305 u8 *data)
307 if (!bus->registered)
308 return -ENOIOCTLCMD;
309 return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
311 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
313 void sfp_upstream_start(struct sfp_bus *bus)
315 if (bus->registered)
316 bus->socket_ops->start(bus->sfp);
317 bus->started = true;
319 EXPORT_SYMBOL_GPL(sfp_upstream_start);
321 void sfp_upstream_stop(struct sfp_bus *bus)
323 if (bus->registered)
324 bus->socket_ops->stop(bus->sfp);
325 bus->started = false;
327 EXPORT_SYMBOL_GPL(sfp_upstream_stop);
329 struct sfp_bus *sfp_register_upstream(struct device_node *np,
330 struct net_device *ndev, void *upstream,
331 const struct sfp_upstream_ops *ops)
333 struct sfp_bus *bus = sfp_bus_get(np);
334 int ret = 0;
336 if (bus) {
337 rtnl_lock();
338 bus->upstream_ops = ops;
339 bus->upstream = upstream;
340 bus->netdev = ndev;
342 if (bus->sfp)
343 ret = sfp_register_bus(bus);
344 rtnl_unlock();
347 if (ret) {
348 sfp_bus_put(bus);
349 bus = NULL;
352 return bus;
354 EXPORT_SYMBOL_GPL(sfp_register_upstream);
356 void sfp_unregister_upstream(struct sfp_bus *bus)
358 rtnl_lock();
359 if (bus->sfp)
360 sfp_unregister_bus(bus);
361 bus->upstream = NULL;
362 bus->netdev = NULL;
363 rtnl_unlock();
365 sfp_bus_put(bus);
367 EXPORT_SYMBOL_GPL(sfp_unregister_upstream);
369 /* Socket driver entry points */
370 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
372 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
373 int ret = 0;
375 if (ops && ops->connect_phy)
376 ret = ops->connect_phy(bus->upstream, phydev);
378 if (ret == 0)
379 bus->phydev = phydev;
381 return ret;
383 EXPORT_SYMBOL_GPL(sfp_add_phy);
385 void sfp_remove_phy(struct sfp_bus *bus)
387 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
389 if (ops && ops->disconnect_phy)
390 ops->disconnect_phy(bus->upstream);
391 bus->phydev = NULL;
393 EXPORT_SYMBOL_GPL(sfp_remove_phy);
395 void sfp_link_up(struct sfp_bus *bus)
397 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
399 if (ops && ops->link_up)
400 ops->link_up(bus->upstream);
402 EXPORT_SYMBOL_GPL(sfp_link_up);
404 void sfp_link_down(struct sfp_bus *bus)
406 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
408 if (ops && ops->link_down)
409 ops->link_down(bus->upstream);
411 EXPORT_SYMBOL_GPL(sfp_link_down);
413 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
415 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
416 int ret = 0;
418 if (ops && ops->module_insert)
419 ret = ops->module_insert(bus->upstream, id);
421 return ret;
423 EXPORT_SYMBOL_GPL(sfp_module_insert);
425 void sfp_module_remove(struct sfp_bus *bus)
427 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
429 if (ops && ops->module_remove)
430 ops->module_remove(bus->upstream);
432 EXPORT_SYMBOL_GPL(sfp_module_remove);
434 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
435 const struct sfp_socket_ops *ops)
437 struct sfp_bus *bus = sfp_bus_get(dev->of_node);
438 int ret = 0;
440 if (bus) {
441 rtnl_lock();
442 bus->sfp_dev = dev;
443 bus->sfp = sfp;
444 bus->socket_ops = ops;
446 if (bus->netdev)
447 ret = sfp_register_bus(bus);
448 rtnl_unlock();
451 if (ret) {
452 sfp_bus_put(bus);
453 bus = NULL;
456 return bus;
458 EXPORT_SYMBOL_GPL(sfp_register_socket);
460 void sfp_unregister_socket(struct sfp_bus *bus)
462 rtnl_lock();
463 if (bus->netdev)
464 sfp_unregister_bus(bus);
465 bus->sfp_dev = NULL;
466 bus->sfp = NULL;
467 bus->socket_ops = NULL;
468 rtnl_unlock();
470 sfp_bus_put(bus);
472 EXPORT_SYMBOL_GPL(sfp_unregister_socket);