mvebu: add linux 4.9 support
[openwrt/staging/yousong.git] / target / linux / mvebu / patches-4.9 / 417-sfp-add-phylink-based-SFP-module-support.patch
1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Sat, 12 Sep 2015 18:43:39 +0100
3 Subject: [PATCH] sfp: add phylink based SFP module support
4
5 Add support for SFP hotpluggable modules via phylink. This supports
6 both copper and optical SFP modules, which require different Serdes
7 modes in order to properly negotiate the link.
8
9 Optical SFP modules typically require the Serdes link to be talking
10 1000base-X mode - this is the gigabit ethernet mode defined by the
11 802.3 standard.
12
13 Copper SFP modules typically integrate a PHY in the module to convert
14 from Serdes to copper, and the PHY will be configured by the vendor
15 to either present a 1000base-X Serdes link (for fixed 1000base-T) or
16 a SGMII Serdes link. However, this is vendor defined, so we instead
17 detect the PHY, switch the link to SGMII mode, and use traditional
18 PHY based negotiation.
19
20 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
21
22 - set port and port capability depending on connector type
23 - move autoneg mode setting to probe function
24 - set "supported" speed capabilities depending on reported ethernet
25 capabilities
26 - checks for short read
27 - dump eeprom base ID when checksum fails
28 ---
29 create mode 100644 drivers/net/phy/sfp.c
30 create mode 100644 include/linux/sfp.h
31
32 --- a/drivers/net/phy/Kconfig
33 +++ b/drivers/net/phy/Kconfig
34 @@ -256,6 +256,11 @@ endif # RTL8366_SMI
35
36 comment "MII PHY device drivers"
37
38 +config SFP
39 + tristate "SFP cage support"
40 + depends on I2C && PHYLINK
41 + select MDIO_I2C
42 +
43 config AMD_PHY
44 tristate "AMD PHYs"
45 ---help---
46 --- a/drivers/net/phy/Makefile
47 +++ b/drivers/net/phy/Makefile
48 @@ -41,6 +41,8 @@ obj-$(CONFIG_MDIO_SUN4I) += mdio-sun4i.o
49 obj-$(CONFIG_MDIO_THUNDER) += mdio-thunder.o
50 obj-$(CONFIG_MDIO_XGENE) += mdio-xgene.o
51
52 +obj-$(CONFIG_SFP) += sfp.o
53 +
54 obj-$(CONFIG_AMD_PHY) += amd.o
55 obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
56 obj-$(CONFIG_AT803X_PHY) += at803x.o
57 --- /dev/null
58 +++ b/drivers/net/phy/sfp.c
59 @@ -0,0 +1,1071 @@
60 +#include <linux/delay.h>
61 +#include <linux/gpio.h>
62 +#include <linux/i2c.h>
63 +#include <linux/interrupt.h>
64 +#include <linux/jiffies.h>
65 +#include <linux/module.h>
66 +#include <linux/mutex.h>
67 +#include <linux/netdevice.h>
68 +#include <linux/of.h>
69 +#include <linux/of_net.h>
70 +#include <linux/phylink.h>
71 +#include <linux/platform_device.h>
72 +#include <linux/sfp.h>
73 +#include <linux/slab.h>
74 +#include <linux/workqueue.h>
75 +
76 +#include "mdio-i2c.h"
77 +#include "swphy.h"
78 +
79 +enum {
80 + GPIO_MODDEF0,
81 + GPIO_LOS,
82 + GPIO_TX_FAULT,
83 + GPIO_TX_DISABLE,
84 + GPIO_RATE_SELECT,
85 + GPIO_MAX,
86 +
87 + SFP_F_PRESENT = BIT(GPIO_MODDEF0),
88 + SFP_F_LOS = BIT(GPIO_LOS),
89 + SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
90 + SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
91 + SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
92 +
93 + SFP_E_INSERT = 0,
94 + SFP_E_REMOVE,
95 + SFP_E_DEV_DOWN,
96 + SFP_E_DEV_UP,
97 + SFP_E_TX_FAULT,
98 + SFP_E_TX_CLEAR,
99 + SFP_E_LOS_HIGH,
100 + SFP_E_LOS_LOW,
101 + SFP_E_TIMEOUT,
102 +
103 + SFP_MOD_EMPTY = 0,
104 + SFP_MOD_PROBE,
105 + SFP_MOD_PRESENT,
106 + SFP_MOD_ERROR,
107 +
108 + SFP_DEV_DOWN = 0,
109 + SFP_DEV_UP,
110 +
111 + SFP_S_DOWN = 0,
112 + SFP_S_INIT,
113 + SFP_S_WAIT_LOS,
114 + SFP_S_LINK_UP,
115 + SFP_S_TX_FAULT,
116 + SFP_S_REINIT,
117 + SFP_S_TX_DISABLE,
118 +};
119 +
120 +static const char *gpio_of_names[] = {
121 + "moddef0",
122 + "los",
123 + "tx-fault",
124 + "tx-disable",
125 + "rate-select",
126 +};
127 +
128 +static const enum gpiod_flags gpio_flags[] = {
129 + GPIOD_IN,
130 + GPIOD_IN,
131 + GPIOD_IN,
132 + GPIOD_ASIS,
133 + GPIOD_ASIS,
134 +};
135 +
136 +#define T_INIT_JIFFIES msecs_to_jiffies(300)
137 +#define T_RESET_US 10
138 +#define T_FAULT_RECOVER msecs_to_jiffies(1000)
139 +
140 +/* SFP module presence detection is poor: the three MOD DEF signals are
141 + * the same length on the PCB, which means it's possible for MOD DEF 0 to
142 + * connect before the I2C bus on MOD DEF 1/2.
143 + *
144 + * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
145 + * be deasserted) but makes no mention of the earliest time before we can
146 + * access the I2C EEPROM. However, Avago modules require 300ms.
147 + */
148 +#define T_PROBE_INIT msecs_to_jiffies(300)
149 +#define T_PROBE_RETRY msecs_to_jiffies(100)
150 +
151 +/*
152 + * SFP modules appear to always have their PHY configured for bus address
153 + * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
154 + */
155 +#define SFP_PHY_ADDR 22
156 +
157 +/*
158 + * Give this long for the PHY to reset.
159 + */
160 +#define T_PHY_RESET_MS 50
161 +
162 +static DEFINE_MUTEX(sfp_mutex);
163 +
164 +struct sfp {
165 + struct device *dev;
166 + struct i2c_adapter *i2c;
167 + struct mii_bus *i2c_mii;
168 + struct net_device *ndev;
169 + struct phylink *phylink;
170 + struct phy_device *mod_phy;
171 +
172 + unsigned int (*get_state)(struct sfp *);
173 + void (*set_state)(struct sfp *, unsigned int);
174 + int (*read)(struct sfp *, bool, u8, void *, size_t);
175 +
176 + struct gpio_desc *gpio[GPIO_MAX];
177 +
178 + unsigned int state;
179 + struct delayed_work poll;
180 + struct delayed_work timeout;
181 + struct mutex sm_mutex;
182 + unsigned char sm_mod_state;
183 + unsigned char sm_dev_state;
184 + unsigned short sm_state;
185 + unsigned int sm_retries;
186 +
187 + struct sfp_eeprom_id id;
188 +
189 + struct notifier_block netdev_nb;
190 +};
191 +
192 +static unsigned long poll_jiffies;
193 +
194 +static unsigned int sfp_gpio_get_state(struct sfp *sfp)
195 +{
196 + unsigned int i, state, v;
197 +
198 + for (i = state = 0; i < GPIO_MAX; i++) {
199 + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
200 + continue;
201 +
202 + v = gpiod_get_value_cansleep(sfp->gpio[i]);
203 + if (v)
204 + state |= BIT(i);
205 + }
206 +
207 + return state;
208 +}
209 +
210 +static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
211 +{
212 + if (state & SFP_F_PRESENT) {
213 + /* If the module is present, drive the signals */
214 + if (sfp->gpio[GPIO_TX_DISABLE])
215 + gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
216 + state & SFP_F_TX_DISABLE);
217 + if (state & SFP_F_RATE_SELECT)
218 + gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
219 + state & SFP_F_RATE_SELECT);
220 + } else {
221 + /* Otherwise, let them float to the pull-ups */
222 + if (sfp->gpio[GPIO_TX_DISABLE])
223 + gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
224 + if (state & SFP_F_RATE_SELECT)
225 + gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
226 + }
227 +}
228 +
229 +static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
230 + void *buf, size_t len)
231 +{
232 + struct i2c_msg msgs[2];
233 + int ret;
234 +
235 + msgs[0].addr = bus_addr;
236 + msgs[0].flags = 0;
237 + msgs[0].len = 1;
238 + msgs[0].buf = &dev_addr;
239 + msgs[1].addr = bus_addr;
240 + msgs[1].flags = I2C_M_RD;
241 + msgs[1].len = len;
242 + msgs[1].buf = buf;
243 +
244 + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
245 + if (ret < 0)
246 + return ret;
247 +
248 + return ret == ARRAY_SIZE(msgs) ? len : 0;
249 +}
250 +
251 +static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
252 + size_t len)
253 +{
254 + return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
255 +}
256 +
257 +static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
258 +{
259 + struct mii_bus *i2c_mii;
260 + int ret;
261 +
262 + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
263 + return -EINVAL;
264 +
265 + sfp->i2c = i2c;
266 + sfp->read = sfp_i2c_read;
267 +
268 + i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
269 + if (IS_ERR(i2c_mii))
270 + return PTR_ERR(i2c_mii);
271 +
272 + i2c_mii->name = "SFP I2C Bus";
273 + i2c_mii->phy_mask = ~0;
274 +
275 + ret = mdiobus_register(i2c_mii);
276 + if (ret < 0) {
277 + mdiobus_free(i2c_mii);
278 + return ret;
279 + }
280 +
281 + sfp->i2c_mii = i2c_mii;
282 +
283 + return 0;
284 +}
285 +
286 +
287 +/* Interface */
288 +static unsigned int sfp_get_state(struct sfp *sfp)
289 +{
290 + return sfp->get_state(sfp);
291 +}
292 +
293 +static void sfp_set_state(struct sfp *sfp, unsigned int state)
294 +{
295 + sfp->set_state(sfp, state);
296 +}
297 +
298 +static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
299 +{
300 + return sfp->read(sfp, a2, addr, buf, len);
301 +}
302 +
303 +static unsigned int sfp_check(void *buf, size_t len)
304 +{
305 + u8 *p, check;
306 +
307 + for (p = buf, check = 0; len; p++, len--)
308 + check += *p;
309 +
310 + return check;
311 +}
312 +
313 +/* Helpers */
314 +static void sfp_module_tx_disable(struct sfp *sfp)
315 +{
316 + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
317 + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
318 + sfp->state |= SFP_F_TX_DISABLE;
319 + sfp_set_state(sfp, sfp->state);
320 +}
321 +
322 +static void sfp_module_tx_enable(struct sfp *sfp)
323 +{
324 + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
325 + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
326 + sfp->state &= ~SFP_F_TX_DISABLE;
327 + sfp_set_state(sfp, sfp->state);
328 +}
329 +
330 +static void sfp_module_tx_fault_reset(struct sfp *sfp)
331 +{
332 + unsigned int state = sfp->state;
333 +
334 + if (state & SFP_F_TX_DISABLE)
335 + return;
336 +
337 + sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
338 +
339 + udelay(T_RESET_US);
340 +
341 + sfp_set_state(sfp, state);
342 +}
343 +
344 +/* SFP state machine */
345 +static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
346 +{
347 + if (timeout)
348 + mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
349 + timeout);
350 + else
351 + cancel_delayed_work(&sfp->timeout);
352 +}
353 +
354 +static void sfp_sm_next(struct sfp *sfp, unsigned int state,
355 + unsigned int timeout)
356 +{
357 + sfp->sm_state = state;
358 + sfp_sm_set_timer(sfp, timeout);
359 +}
360 +
361 +static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, unsigned int timeout)
362 +{
363 + sfp->sm_mod_state = state;
364 + sfp_sm_set_timer(sfp, timeout);
365 +}
366 +
367 +static void sfp_sm_phy_detach(struct sfp *sfp)
368 +{
369 + phy_stop(sfp->mod_phy);
370 + if (sfp->phylink)
371 + phylink_disconnect_phy(sfp->phylink);
372 + phy_device_remove(sfp->mod_phy);
373 + phy_device_free(sfp->mod_phy);
374 + sfp->mod_phy = NULL;
375 +}
376 +
377 +static void sfp_sm_probe_phy(struct sfp *sfp)
378 +{
379 + struct phy_device *phy;
380 + int err;
381 +
382 + msleep(T_PHY_RESET_MS);
383 +
384 + phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
385 + if (IS_ERR(phy)) {
386 + dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
387 + return;
388 + }
389 + if (!phy) {
390 + dev_info(sfp->dev, "no PHY detected\n");
391 + return;
392 + }
393 +
394 + if (sfp->phylink) {
395 + err = phylink_connect_phy(sfp->phylink, phy);
396 + if (err) {
397 + phy_device_remove(phy);
398 + phy_device_free(phy);
399 + dev_err(sfp->dev, "phylink_connect_phy failed: %d\n",
400 + err);
401 + return;
402 + }
403 + }
404 +
405 + sfp->mod_phy = phy;
406 + phy_start(phy);
407 +}
408 +
409 +static void sfp_sm_link_up(struct sfp *sfp)
410 +{
411 + if (sfp->phylink)
412 + phylink_enable(sfp->phylink);
413 +
414 + sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
415 +}
416 +
417 +static void sfp_sm_link_down(struct sfp *sfp)
418 +{
419 + if (sfp->phylink)
420 + phylink_disable(sfp->phylink);
421 +}
422 +
423 +static void sfp_sm_link_check_los(struct sfp *sfp)
424 +{
425 + unsigned int los = sfp->state & SFP_F_LOS;
426 +
427 + /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
428 + * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
429 + * the same as SFP_OPTIONS_LOS_NORMAL set.
430 + */
431 + if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED)
432 + los ^= SFP_F_LOS;
433 +
434 + if (los)
435 + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
436 + else
437 + sfp_sm_link_up(sfp);
438 +}
439 +
440 +static void sfp_sm_fault(struct sfp *sfp, bool warn)
441 +{
442 + if (sfp->sm_retries && !--sfp->sm_retries) {
443 + dev_err(sfp->dev, "module persistently indicates fault, disabling\n");
444 + sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
445 + } else {
446 + if (warn)
447 + dev_err(sfp->dev, "module transmit fault indicated\n");
448 +
449 + sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
450 + }
451 +}
452 +
453 +static void sfp_sm_mod_init(struct sfp *sfp)
454 +{
455 + sfp_module_tx_enable(sfp);
456 +
457 + /* Wait t_init before indicating that the link is up, provided the
458 + * current state indicates no TX_FAULT. If TX_FAULT clears before
459 + * this time, that's fine too.
460 + */
461 + sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
462 + sfp->sm_retries = 5;
463 +
464 + if (sfp->phylink) {
465 + /* Setting the serdes link mode is guesswork: there's no
466 + * field in the EEPROM which indicates what mode should
467 + * be used.
468 + *
469 + * If it's a gigabit-only fiber module, it probably does
470 + * not have a PHY, so switch to 802.3z negotiation mode.
471 + * Otherwise, switch to SGMII mode (which is required to
472 + * support non-gigabit speeds) and probe for a PHY.
473 + */
474 + if (sfp->id.base.e1000_base_t ||
475 + sfp->id.base.e100_base_lx ||
476 + sfp->id.base.e100_base_fx)
477 + sfp_sm_probe_phy(sfp);
478 + }
479 +}
480 +
481 +static int sfp_sm_mod_probe(struct sfp *sfp)
482 +{
483 + /* SFP module inserted - read I2C data */
484 + struct sfp_eeprom_id id;
485 + char vendor[17];
486 + char part[17];
487 + char sn[17];
488 + char date[9];
489 + char rev[5];
490 + u8 check;
491 + int err;
492 +
493 + err = sfp_read(sfp, false, 0, &id, sizeof(id));
494 + if (err < 0) {
495 + dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
496 + return -EAGAIN;
497 + }
498 +
499 + if (err != sizeof(id)) {
500 + dev_err(sfp->dev, "EEPROM short read: %d\n", err);
501 + return -EAGAIN;
502 + }
503 +
504 + /* Validate the checksum over the base structure */
505 + check = sfp_check(&id.base, sizeof(id.base) - 1);
506 + if (check != id.base.cc_base) {
507 + dev_err(sfp->dev,
508 + "EEPROM base structure checksum failure: 0x%02x\n",
509 + check);
510 + print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
511 + 16, 1, &id, sizeof(id.base) - 1, true);
512 + return -EINVAL;
513 + }
514 +
515 + check = sfp_check(&id.ext, sizeof(id.ext) - 1);
516 + if (check != id.ext.cc_ext) {
517 + dev_err(sfp->dev,
518 + "EEPROM extended structure checksum failure: 0x%02x\n",
519 + check);
520 + memset(&id.ext, 0, sizeof(id.ext));
521 + }
522 +
523 + sfp->id = id;
524 +
525 + memcpy(vendor, sfp->id.base.vendor_name, 16);
526 + vendor[16] = '\0';
527 + memcpy(part, sfp->id.base.vendor_pn, 16);
528 + part[16] = '\0';
529 + memcpy(rev, sfp->id.base.vendor_rev, 4);
530 + rev[4] = '\0';
531 + memcpy(sn, sfp->id.ext.vendor_sn, 16);
532 + sn[16] = '\0';
533 + memcpy(date, sfp->id.ext.datecode, 8);
534 + date[8] = '\0';
535 +
536 + dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date);
537 +
538 + /* We only support SFP modules, not the legacy GBIC modules. */
539 + if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP ||
540 + sfp->id.base.phys_ext_id != SFP_PHYS_EXT_ID_SFP) {
541 + dev_err(sfp->dev, "module is not SFP - phys id 0x%02x 0x%02x\n",
542 + sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
543 + return -EINVAL;
544 + }
545 +
546 + /*
547 + * What isn't clear from the SFP documentation is whether this
548 + * specifies the encoding expected on the TD/RD lines, or whether
549 + * the TD/RD lines are always 8b10b encoded, but the transceiver
550 + * converts. Eg, think of a copper SFP supporting 1G/100M/10M
551 + * ethernet: this requires 8b10b encoding for 1G, 4b5b for 100M,
552 + * and manchester for 10M.
553 + */
554 + /* 1Gbit ethernet requires 8b10b encoding */
555 + if (sfp->id.base.encoding != SFP_ENCODING_8B10B) {
556 + dev_err(sfp->dev, "module does not support 8B10B encoding\n");
557 + return -EINVAL;
558 + }
559 +
560 + if (sfp->phylink) {
561 + __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
562 + int mode;
563 + u8 port;
564 +
565 + phylink_set(support, Autoneg);
566 + phylink_set(support, Pause);
567 + phylink_set(support, Asym_Pause);
568 +
569 + /* Set ethtool support from the compliance fields. */
570 + if (sfp->id.base.e10g_base_sr)
571 + phylink_set(support, 10000baseSR_Full);
572 + if (sfp->id.base.e10g_base_lr)
573 + phylink_set(support, 10000baseLR_Full);
574 + if (sfp->id.base.e10g_base_lrm)
575 + phylink_set(support, 10000baseLRM_Full);
576 + if (sfp->id.base.e10g_base_er)
577 + phylink_set(support, 10000baseER_Full);
578 + if (sfp->id.base.e1000_base_sx ||
579 + sfp->id.base.e1000_base_lx ||
580 + sfp->id.base.e1000_base_cx)
581 + phylink_set(support, 1000baseX_Full);
582 + if (sfp->id.base.e1000_base_t) {
583 + phylink_set(support, 1000baseT_Half);
584 + phylink_set(support, 1000baseT_Full);
585 + }
586 +
587 + /* port is the physical connector, set this from the
588 + * connector field.
589 + */
590 + switch (sfp->id.base.connector) {
591 + case SFP_CONNECTOR_SC:
592 + case SFP_CONNECTOR_FIBERJACK:
593 + case SFP_CONNECTOR_LC:
594 + case SFP_CONNECTOR_MT_RJ:
595 + case SFP_CONNECTOR_MU:
596 + case SFP_CONNECTOR_OPTICAL_PIGTAIL:
597 + phylink_set(support, FIBRE);
598 + port = PORT_FIBRE;
599 + break;
600 +
601 + case SFP_CONNECTOR_RJ45:
602 + phylink_set(support, TP);
603 + port = PORT_TP;
604 + break;
605 +
606 + case SFP_CONNECTOR_UNSPEC:
607 + if (sfp->id.base.e1000_base_t) {
608 + phylink_set(support, TP);
609 + port = PORT_TP;
610 + break;
611 + }
612 + /* fallthrough */
613 + case SFP_CONNECTOR_SG: /* guess */
614 + case SFP_CONNECTOR_MPO_1X12:
615 + case SFP_CONNECTOR_MPO_2X16:
616 + case SFP_CONNECTOR_HSSDC_II:
617 + case SFP_CONNECTOR_COPPER_PIGTAIL:
618 + case SFP_CONNECTOR_NOSEPARATE:
619 + case SFP_CONNECTOR_MXC_2X16:
620 + default:
621 + /* a guess at the supported link modes */
622 + dev_warn(sfp->dev, "Guessing link modes, please report...\n");
623 + phylink_set(support, 1000baseT_Half);
624 + phylink_set(support, 1000baseT_Full);
625 + port = PORT_OTHER;
626 + break;
627 + }
628 +
629 + /* Setting the serdes link mode is guesswork: there's no
630 + * field in the EEPROM which indicates what mode should
631 + * be used.
632 + *
633 + * If it's a gigabit-only fiber module, it probably does
634 + * not have a PHY, so switch to 802.3z negotiation mode.
635 + * Otherwise, switch to SGMII mode (which is required to
636 + * support non-gigabit speeds) and probe for a PHY.
637 + */
638 + if (!sfp->id.base.e1000_base_t &&
639 + !sfp->id.base.e100_base_lx &&
640 + !sfp->id.base.e100_base_fx) {
641 + mode = MLO_AN_8023Z;
642 + } else {
643 + mode = MLO_AN_SGMII;
644 + }
645 +
646 + phylink_set_link(sfp->phylink, mode, port, support);
647 + }
648 +
649 + return 0;
650 +}
651 +
652 +static void sfp_sm_mod_remove(struct sfp *sfp)
653 +{
654 + if (sfp->mod_phy)
655 + sfp_sm_phy_detach(sfp);
656 +
657 + sfp_module_tx_disable(sfp);
658 +
659 + memset(&sfp->id, 0, sizeof(sfp->id));
660 +
661 + dev_info(sfp->dev, "module removed\n");
662 +}
663 +
664 +static void sfp_sm_event(struct sfp *sfp, unsigned int event)
665 +{
666 + mutex_lock(&sfp->sm_mutex);
667 +
668 + dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
669 + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
670 +
671 + /* This state machine tracks the insert/remove state of
672 + * the module, and handles probing the on-board EEPROM.
673 + */
674 + switch (sfp->sm_mod_state) {
675 + default:
676 + if (event == SFP_E_INSERT) {
677 + sfp_module_tx_disable(sfp);
678 + sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
679 + }
680 + break;
681 +
682 + case SFP_MOD_PROBE:
683 + if (event == SFP_E_REMOVE) {
684 + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
685 + } else if (event == SFP_E_TIMEOUT) {
686 + int err = sfp_sm_mod_probe(sfp);
687 +
688 + if (err == 0)
689 + sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
690 + else if (err == -EAGAIN)
691 + sfp_sm_set_timer(sfp, T_PROBE_RETRY);
692 + else
693 + sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
694 + }
695 + break;
696 +
697 + case SFP_MOD_PRESENT:
698 + case SFP_MOD_ERROR:
699 + if (event == SFP_E_REMOVE) {
700 + sfp_sm_mod_remove(sfp);
701 + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
702 + }
703 + break;
704 + }
705 +
706 + /* This state machine tracks the netdev up/down state */
707 + switch (sfp->sm_dev_state) {
708 + default:
709 + if (event == SFP_E_DEV_UP)
710 + sfp->sm_dev_state = SFP_DEV_UP;
711 + break;
712 +
713 + case SFP_DEV_UP:
714 + if (event == SFP_E_DEV_DOWN) {
715 + /* If the module has a PHY, avoid raising TX disable
716 + * as this resets the PHY. Otherwise, raise it to
717 + * turn the laser off.
718 + */
719 + if (!sfp->mod_phy)
720 + sfp_module_tx_disable(sfp);
721 + sfp->sm_dev_state = SFP_DEV_DOWN;
722 + }
723 + break;
724 + }
725 +
726 + /* Some events are global */
727 + if (sfp->sm_state != SFP_S_DOWN &&
728 + (sfp->sm_mod_state != SFP_MOD_PRESENT ||
729 + sfp->sm_dev_state != SFP_DEV_UP)) {
730 + if (sfp->sm_state == SFP_S_LINK_UP &&
731 + sfp->sm_dev_state == SFP_DEV_UP)
732 + sfp_sm_link_down(sfp);
733 + if (sfp->mod_phy)
734 + sfp_sm_phy_detach(sfp);
735 + sfp_sm_next(sfp, SFP_S_DOWN, 0);
736 + mutex_unlock(&sfp->sm_mutex);
737 + return;
738 + }
739 +
740 + /* The main state machine */
741 + switch (sfp->sm_state) {
742 + case SFP_S_DOWN:
743 + if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
744 + sfp->sm_dev_state == SFP_DEV_UP)
745 + sfp_sm_mod_init(sfp);
746 + break;
747 +
748 + case SFP_S_INIT:
749 + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
750 + sfp_sm_fault(sfp, true);
751 + else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
752 + sfp_sm_link_check_los(sfp);
753 + break;
754 +
755 + case SFP_S_WAIT_LOS:
756 + if (event == SFP_E_TX_FAULT)
757 + sfp_sm_fault(sfp, true);
758 + else if (event ==
759 + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
760 + SFP_E_LOS_HIGH : SFP_E_LOS_LOW))
761 + sfp_sm_link_up(sfp);
762 + break;
763 +
764 + case SFP_S_LINK_UP:
765 + if (event == SFP_E_TX_FAULT) {
766 + sfp_sm_link_down(sfp);
767 + sfp_sm_fault(sfp, true);
768 + } else if (event ==
769 + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
770 + SFP_E_LOS_LOW : SFP_E_LOS_HIGH)) {
771 + sfp_sm_link_down(sfp);
772 + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
773 + }
774 + break;
775 +
776 + case SFP_S_TX_FAULT:
777 + if (event == SFP_E_TIMEOUT) {
778 + sfp_module_tx_fault_reset(sfp);
779 + sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
780 + }
781 + break;
782 +
783 + case SFP_S_REINIT:
784 + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
785 + sfp_sm_fault(sfp, false);
786 + } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
787 + dev_info(sfp->dev, "module transmit fault recovered\n");
788 + sfp_sm_link_check_los(sfp);
789 + }
790 + break;
791 +
792 + case SFP_S_TX_DISABLE:
793 + break;
794 + }
795 +
796 + dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
797 + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
798 +
799 + mutex_unlock(&sfp->sm_mutex);
800 +}
801 +
802 +#if 0
803 +static int sfp_phy_module_info(struct phy_device *phy,
804 + struct ethtool_modinfo *modinfo)
805 +{
806 + struct sfp *sfp = phy->priv;
807 +
808 + /* locking... and check module is present */
809 +
810 + if (sfp->id.ext.sff8472_compliance) {
811 + modinfo->type = ETH_MODULE_SFF_8472;
812 + modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
813 + } else {
814 + modinfo->type = ETH_MODULE_SFF_8079;
815 + modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
816 + }
817 + return 0;
818 +}
819 +
820 +static int sfp_phy_module_eeprom(struct phy_device *phy,
821 + struct ethtool_eeprom *ee, u8 *data)
822 +{
823 + struct sfp *sfp = phy->priv;
824 + unsigned int first, last, len;
825 + int ret;
826 +
827 + if (ee->len == 0)
828 + return -EINVAL;
829 +
830 + first = ee->offset;
831 + last = ee->offset + ee->len;
832 + if (first < ETH_MODULE_SFF_8079_LEN) {
833 + len = last;
834 + if (len > ETH_MODULE_SFF_8079_LEN)
835 + len = ETH_MODULE_SFF_8079_LEN;
836 + len -= first;
837 +
838 + ret = sfp->read(sfp, false, first, data, len);
839 + if (ret < 0)
840 + return ret;
841 +
842 + first += len;
843 + data += len;
844 + }
845 + if (first >= ETH_MODULE_SFF_8079_LEN && last > first) {
846 + len = last - first;
847 +
848 + ret = sfp->read(sfp, true, first, data, len);
849 + if (ret < 0)
850 + return ret;
851 + }
852 + return 0;
853 +}
854 +#endif
855 +
856 +static void sfp_timeout(struct work_struct *work)
857 +{
858 + struct sfp *sfp = container_of(work, struct sfp, timeout.work);
859 +
860 + sfp_sm_event(sfp, SFP_E_TIMEOUT);
861 +}
862 +
863 +static void sfp_check_state(struct sfp *sfp)
864 +{
865 + unsigned int state, i, changed;
866 +
867 + state = sfp_get_state(sfp);
868 + changed = state ^ sfp->state;
869 + changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
870 +
871 + for (i = 0; i < GPIO_MAX; i++)
872 + if (changed & BIT(i))
873 + dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
874 + !!(sfp->state & BIT(i)), !!(state & BIT(i)));
875 +
876 + state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
877 + sfp->state = state;
878 +
879 + if (changed & SFP_F_PRESENT)
880 + sfp_sm_event(sfp, state & SFP_F_PRESENT ?
881 + SFP_E_INSERT : SFP_E_REMOVE);
882 +
883 + if (changed & SFP_F_TX_FAULT)
884 + sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
885 + SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
886 +
887 + if (changed & SFP_F_LOS)
888 + sfp_sm_event(sfp, state & SFP_F_LOS ?
889 + SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
890 +}
891 +
892 +static irqreturn_t sfp_irq(int irq, void *data)
893 +{
894 + struct sfp *sfp = data;
895 +
896 + sfp_check_state(sfp);
897 +
898 + return IRQ_HANDLED;
899 +}
900 +
901 +static void sfp_poll(struct work_struct *work)
902 +{
903 + struct sfp *sfp = container_of(work, struct sfp, poll.work);
904 +
905 + sfp_check_state(sfp);
906 + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
907 +}
908 +
909 +static int sfp_netdev_notify(struct notifier_block *nb, unsigned long act, void *data)
910 +{
911 + struct sfp *sfp = container_of(nb, struct sfp, netdev_nb);
912 + struct netdev_notifier_info *info = data;
913 + struct net_device *ndev = info->dev;
914 +
915 + if (!sfp->ndev || ndev != sfp->ndev)
916 + return NOTIFY_DONE;
917 +
918 + switch (act) {
919 + case NETDEV_UP:
920 + sfp_sm_event(sfp, SFP_E_DEV_UP);
921 + break;
922 +
923 + case NETDEV_GOING_DOWN:
924 + sfp_sm_event(sfp, SFP_E_DEV_DOWN);
925 + break;
926 +
927 + case NETDEV_UNREGISTER:
928 + if (sfp->mod_phy && sfp->phylink)
929 + phylink_disconnect_phy(sfp->phylink);
930 + sfp->phylink = NULL;
931 + dev_put(sfp->ndev);
932 + sfp->ndev = NULL;
933 + break;
934 + }
935 + return NOTIFY_OK;
936 +}
937 +
938 +static struct sfp *sfp_alloc(struct device *dev)
939 +{
940 + struct sfp *sfp;
941 +
942 + sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
943 + if (!sfp)
944 + return ERR_PTR(-ENOMEM);
945 +
946 + sfp->dev = dev;
947 +
948 + mutex_init(&sfp->sm_mutex);
949 + INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
950 + INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
951 +
952 + sfp->netdev_nb.notifier_call = sfp_netdev_notify;
953 +
954 + return sfp;
955 +}
956 +
957 +static void sfp_destroy(struct sfp *sfp)
958 +{
959 + cancel_delayed_work_sync(&sfp->poll);
960 + cancel_delayed_work_sync(&sfp->timeout);
961 + if (sfp->i2c_mii) {
962 + mdiobus_unregister(sfp->i2c_mii);
963 + mdiobus_free(sfp->i2c_mii);
964 + }
965 + if (sfp->i2c)
966 + i2c_put_adapter(sfp->i2c);
967 + of_node_put(sfp->dev->of_node);
968 + kfree(sfp);
969 +}
970 +
971 +static void sfp_cleanup(void *data)
972 +{
973 + struct sfp *sfp = data;
974 +
975 + sfp_destroy(sfp);
976 +}
977 +
978 +static int sfp_probe(struct platform_device *pdev)
979 +{
980 + struct sfp *sfp;
981 + bool poll = false;
982 + int irq, err, i;
983 +
984 + sfp = sfp_alloc(&pdev->dev);
985 + if (IS_ERR(sfp))
986 + return PTR_ERR(sfp);
987 +
988 + platform_set_drvdata(pdev, sfp);
989 +
990 + err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
991 + if (err < 0)
992 + return err;
993 +
994 + if (pdev->dev.of_node) {
995 + struct device_node *node = pdev->dev.of_node;
996 + struct device_node *np;
997 +
998 + np = of_parse_phandle(node, "i2c-bus", 0);
999 + if (np) {
1000 + struct i2c_adapter *i2c;
1001 +
1002 + i2c = of_find_i2c_adapter_by_node(np);
1003 + of_node_put(np);
1004 + if (!i2c)
1005 + return -EPROBE_DEFER;
1006 +
1007 + err = sfp_i2c_configure(sfp, i2c);
1008 + if (err < 0) {
1009 + i2c_put_adapter(i2c);
1010 + return err;
1011 + }
1012 + }
1013 +
1014 + for (i = 0; i < GPIO_MAX; i++) {
1015 + sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1016 + gpio_of_names[i], gpio_flags[i]);
1017 + if (IS_ERR(sfp->gpio[i]))
1018 + return PTR_ERR(sfp->gpio[i]);
1019 + }
1020 +
1021 + sfp->get_state = sfp_gpio_get_state;
1022 + sfp->set_state = sfp_gpio_set_state;
1023 +
1024 + np = of_parse_phandle(node, "sfp,ethernet", 0);
1025 + if (!np) {
1026 + dev_err(sfp->dev, "missing sfp,ethernet property\n");
1027 + return -EINVAL;
1028 + }
1029 +
1030 + sfp->ndev = of_find_net_device_by_node(np);
1031 + if (!sfp->ndev) {
1032 + dev_err(sfp->dev, "ethernet device not found\n");
1033 + return -EPROBE_DEFER;
1034 + }
1035 +
1036 + dev_hold(sfp->ndev);
1037 + put_device(&sfp->ndev->dev);
1038 +
1039 + sfp->phylink = phylink_lookup_by_netdev(sfp->ndev);
1040 + if (!sfp->phylink) {
1041 + dev_err(sfp->dev, "phylink for %s not found\n",
1042 + netdev_name(sfp->ndev));
1043 + return -EPROBE_DEFER;
1044 + }
1045 +
1046 + phylink_disable(sfp->phylink);
1047 + }
1048 +
1049 + sfp->state = sfp_get_state(sfp);
1050 + if (sfp->gpio[GPIO_TX_DISABLE] &&
1051 + gpiod_get_value_cansleep(sfp->gpio[GPIO_TX_DISABLE]))
1052 + sfp->state |= SFP_F_TX_DISABLE;
1053 + if (sfp->gpio[GPIO_RATE_SELECT] &&
1054 + gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1055 + sfp->state |= SFP_F_RATE_SELECT;
1056 + sfp_set_state(sfp, sfp->state);
1057 + sfp_module_tx_disable(sfp);
1058 + if (sfp->state & SFP_F_PRESENT)
1059 + sfp_sm_event(sfp, SFP_E_INSERT);
1060 +
1061 + for (i = 0; i < GPIO_MAX; i++) {
1062 + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1063 + continue;
1064 +
1065 + irq = gpiod_to_irq(sfp->gpio[i]);
1066 + if (!irq) {
1067 + poll = true;
1068 + continue;
1069 + }
1070 +
1071 + err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1072 + IRQF_ONESHOT |
1073 + IRQF_TRIGGER_RISING |
1074 + IRQF_TRIGGER_FALLING,
1075 + dev_name(sfp->dev), sfp);
1076 + if (err)
1077 + poll = true;
1078 + }
1079 +
1080 + if (poll)
1081 + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1082 +
1083 + register_netdevice_notifier(&sfp->netdev_nb);
1084 +
1085 + return 0;
1086 +}
1087 +
1088 +static int sfp_remove(struct platform_device *pdev)
1089 +{
1090 + struct sfp *sfp = platform_get_drvdata(pdev);
1091 +
1092 + unregister_netdevice_notifier(&sfp->netdev_nb);
1093 + if (sfp->ndev)
1094 + dev_put(sfp->ndev);
1095 +
1096 + return 0;
1097 +}
1098 +
1099 +static const struct of_device_id sfp_of_match[] = {
1100 + { .compatible = "sff,sfp", },
1101 + { },
1102 +};
1103 +MODULE_DEVICE_TABLE(of, sfp_of_match);
1104 +
1105 +static struct platform_driver sfp_driver = {
1106 + .probe = sfp_probe,
1107 + .remove = sfp_remove,
1108 + .driver = {
1109 + .name = "sfp",
1110 + .of_match_table = sfp_of_match,
1111 + },
1112 +};
1113 +
1114 +static int sfp_init(void)
1115 +{
1116 + poll_jiffies = msecs_to_jiffies(100);
1117 +
1118 + return platform_driver_register(&sfp_driver);
1119 +}
1120 +module_init(sfp_init);
1121 +
1122 +static void sfp_exit(void)
1123 +{
1124 + platform_driver_unregister(&sfp_driver);
1125 +}
1126 +module_exit(sfp_exit);
1127 +
1128 +MODULE_ALIAS("platform:sfp");
1129 +MODULE_AUTHOR("Russell King");
1130 +MODULE_LICENSE("GPL v2");
1131 --- /dev/null
1132 +++ b/include/linux/sfp.h
1133 @@ -0,0 +1,344 @@
1134 +#ifndef LINUX_SFP_H
1135 +#define LINUX_SFP_H
1136 +
1137 +struct __packed sfp_eeprom_base {
1138 + u8 phys_id;
1139 + u8 phys_ext_id;
1140 + u8 connector;
1141 +#if defined __BIG_ENDIAN_BITFIELD
1142 + u8 e10g_base_er:1;
1143 + u8 e10g_base_lrm:1;
1144 + u8 e10g_base_lr:1;
1145 + u8 e10g_base_sr:1;
1146 + u8 if_1x_sx:1;
1147 + u8 if_1x_lx:1;
1148 + u8 if_1x_copper_active:1;
1149 + u8 if_1x_copper_passive:1;
1150 +
1151 + u8 escon_mmf_1310_led:1;
1152 + u8 escon_smf_1310_laser:1;
1153 + u8 sonet_oc192_short_reach:1;
1154 + u8 sonet_reach_bit1:1;
1155 + u8 sonet_reach_bit2:1;
1156 + u8 sonet_oc48_long_reach:1;
1157 + u8 sonet_oc48_intermediate_reach:1;
1158 + u8 sonet_oc48_short_reach:1;
1159 +
1160 + u8 unallocated_5_7:1;
1161 + u8 sonet_oc12_smf_long_reach:1;
1162 + u8 sonet_oc12_smf_intermediate_reach:1;
1163 + u8 sonet_oc12_short_reach:1;
1164 + u8 unallocated_5_3:1;
1165 + u8 sonet_oc3_smf_long_reach:1;
1166 + u8 sonet_oc3_smf_intermediate_reach:1;
1167 + u8 sonet_oc3_short_reach:1;
1168 +
1169 + u8 e_base_px:1;
1170 + u8 e_base_bx10:1;
1171 + u8 e100_base_fx:1;
1172 + u8 e100_base_lx:1;
1173 + u8 e1000_base_t:1;
1174 + u8 e1000_base_cx:1;
1175 + u8 e1000_base_lx:1;
1176 + u8 e1000_base_sx:1;
1177 +
1178 + u8 fc_ll_v:1;
1179 + u8 fc_ll_s:1;
1180 + u8 fc_ll_i:1;
1181 + u8 fc_ll_l:1;
1182 + u8 fc_ll_m:1;
1183 + u8 fc_tech_sa:1;
1184 + u8 fc_tech_lc:1;
1185 + u8 fc_tech_electrical_inter_enclosure:1;
1186 +
1187 + u8 fc_tech_electrical_intra_enclosure:1;
1188 + u8 fc_tech_sn:1;
1189 + u8 fc_tech_sl:1;
1190 + u8 fc_tech_ll:1;
1191 + u8 sfp_ct_active:1;
1192 + u8 sfp_ct_passive:1;
1193 + u8 unallocated_8_1:1;
1194 + u8 unallocated_8_0:1;
1195 +
1196 + u8 fc_media_tw:1;
1197 + u8 fc_media_tp:1;
1198 + u8 fc_media_mi:1;
1199 + u8 fc_media_tv:1;
1200 + u8 fc_media_m6:1;
1201 + u8 fc_media_m5:1;
1202 + u8 unallocated_9_1:1;
1203 + u8 fc_media_sm:1;
1204 +
1205 + u8 fc_speed_1200:1;
1206 + u8 fc_speed_800:1;
1207 + u8 fc_speed_1600:1;
1208 + u8 fc_speed_400:1;
1209 + u8 fc_speed_3200:1;
1210 + u8 fc_speed_200:1;
1211 + u8 unallocated_10_1:1;
1212 + u8 fc_speed_100:1;
1213 +#elif defined __LITTLE_ENDIAN_BITFIELD
1214 + u8 if_1x_copper_passive:1;
1215 + u8 if_1x_copper_active:1;
1216 + u8 if_1x_lx:1;
1217 + u8 if_1x_sx:1;
1218 + u8 e10g_base_sr:1;
1219 + u8 e10g_base_lr:1;
1220 + u8 e10g_base_lrm:1;
1221 + u8 e10g_base_er:1;
1222 +
1223 + u8 sonet_oc3_short_reach:1;
1224 + u8 sonet_oc3_smf_intermediate_reach:1;
1225 + u8 sonet_oc3_smf_long_reach:1;
1226 + u8 unallocated_5_3:1;
1227 + u8 sonet_oc12_short_reach:1;
1228 + u8 sonet_oc12_smf_intermediate_reach:1;
1229 + u8 sonet_oc12_smf_long_reach:1;
1230 + u8 unallocated_5_7:1;
1231 +
1232 + u8 sonet_oc48_short_reach:1;
1233 + u8 sonet_oc48_intermediate_reach:1;
1234 + u8 sonet_oc48_long_reach:1;
1235 + u8 sonet_reach_bit2:1;
1236 + u8 sonet_reach_bit1:1;
1237 + u8 sonet_oc192_short_reach:1;
1238 + u8 escon_smf_1310_laser:1;
1239 + u8 escon_mmf_1310_led:1;
1240 +
1241 + u8 e1000_base_sx:1;
1242 + u8 e1000_base_lx:1;
1243 + u8 e1000_base_cx:1;
1244 + u8 e1000_base_t:1;
1245 + u8 e100_base_lx:1;
1246 + u8 e100_base_fx:1;
1247 + u8 e_base_bx10:1;
1248 + u8 e_base_px:1;
1249 +
1250 + u8 fc_tech_electrical_inter_enclosure:1;
1251 + u8 fc_tech_lc:1;
1252 + u8 fc_tech_sa:1;
1253 + u8 fc_ll_m:1;
1254 + u8 fc_ll_l:1;
1255 + u8 fc_ll_i:1;
1256 + u8 fc_ll_s:1;
1257 + u8 fc_ll_v:1;
1258 +
1259 + u8 unallocated_8_0:1;
1260 + u8 unallocated_8_1:1;
1261 + u8 sfp_ct_passive:1;
1262 + u8 sfp_ct_active:1;
1263 + u8 fc_tech_ll:1;
1264 + u8 fc_tech_sl:1;
1265 + u8 fc_tech_sn:1;
1266 + u8 fc_tech_electrical_intra_enclosure:1;
1267 +
1268 + u8 fc_media_sm:1;
1269 + u8 unallocated_9_1:1;
1270 + u8 fc_media_m5:1;
1271 + u8 fc_media_m6:1;
1272 + u8 fc_media_tv:1;
1273 + u8 fc_media_mi:1;
1274 + u8 fc_media_tp:1;
1275 + u8 fc_media_tw:1;
1276 +
1277 + u8 fc_speed_100:1;
1278 + u8 unallocated_10_1:1;
1279 + u8 fc_speed_200:1;
1280 + u8 fc_speed_3200:1;
1281 + u8 fc_speed_400:1;
1282 + u8 fc_speed_1600:1;
1283 + u8 fc_speed_800:1;
1284 + u8 fc_speed_1200:1;
1285 +#else
1286 +#error Unknown Endian
1287 +#endif
1288 + u8 encoding;
1289 + u8 br_nominal;
1290 + u8 rate_id;
1291 + u8 link_len[6];
1292 + char vendor_name[16];
1293 + u8 reserved36;
1294 + char vendor_oui[3];
1295 + char vendor_pn[16];
1296 + char vendor_rev[4];
1297 + union {
1298 + __be16 optical_wavelength;
1299 + u8 cable_spec;
1300 + };
1301 + u8 reserved62;
1302 + u8 cc_base;
1303 +};
1304 +
1305 +struct __packed sfp_eeprom_ext {
1306 + __be16 options;
1307 + u8 br_max;
1308 + u8 br_min;
1309 + char vendor_sn[16];
1310 + char datecode[8];
1311 + u8 diagmon;
1312 + u8 enhopts;
1313 + u8 sff8472_compliance;
1314 + u8 cc_ext;
1315 +};
1316 +
1317 +struct __packed sfp_eeprom_id {
1318 + struct sfp_eeprom_base base;
1319 + struct sfp_eeprom_ext ext;
1320 +};
1321 +
1322 +/* SFP EEPROM registers */
1323 +enum {
1324 + SFP_PHYS_ID = 0x00,
1325 + SFP_PHYS_EXT_ID = 0x01,
1326 + SFP_CONNECTOR = 0x02,
1327 + SFP_COMPLIANCE = 0x03,
1328 + SFP_ENCODING = 0x0b,
1329 + SFP_BR_NOMINAL = 0x0c,
1330 + SFP_RATE_ID = 0x0d,
1331 + SFP_LINK_LEN_SM_KM = 0x0e,
1332 + SFP_LINK_LEN_SM_100M = 0x0f,
1333 + SFP_LINK_LEN_50UM_OM2_10M = 0x10,
1334 + SFP_LINK_LEN_62_5UM_OM1_10M = 0x11,
1335 + SFP_LINK_LEN_COPPER_1M = 0x12,
1336 + SFP_LINK_LEN_50UM_OM4_10M = 0x12,
1337 + SFP_LINK_LEN_50UM_OM3_10M = 0x13,
1338 + SFP_VENDOR_NAME = 0x14,
1339 + SFP_VENDOR_OUI = 0x25,
1340 + SFP_VENDOR_PN = 0x28,
1341 + SFP_VENDOR_REV = 0x38,
1342 + SFP_OPTICAL_WAVELENGTH_MSB = 0x3c,
1343 + SFP_OPTICAL_WAVELENGTH_LSB = 0x3d,
1344 + SFP_CABLE_SPEC = 0x3c,
1345 + SFP_CC_BASE = 0x3f,
1346 + SFP_OPTIONS = 0x40, /* 2 bytes, MSB, LSB */
1347 + SFP_BR_MAX = 0x42,
1348 + SFP_BR_MIN = 0x43,
1349 + SFP_VENDOR_SN = 0x44,
1350 + SFP_DATECODE = 0x54,
1351 + SFP_DIAGMON = 0x5c,
1352 + SFP_ENHOPTS = 0x5d,
1353 + SFP_SFF8472_COMPLIANCE = 0x5e,
1354 + SFP_CC_EXT = 0x5f,
1355 +
1356 + SFP_PHYS_ID_SFP = 0x03,
1357 + SFP_PHYS_EXT_ID_SFP = 0x04,
1358 + SFP_CONNECTOR_UNSPEC = 0x00,
1359 + /* codes 01-05 not supportable on SFP, but some modules have single SC */
1360 + SFP_CONNECTOR_SC = 0x01,
1361 + SFP_CONNECTOR_FIBERJACK = 0x06,
1362 + SFP_CONNECTOR_LC = 0x07,
1363 + SFP_CONNECTOR_MT_RJ = 0x08,
1364 + SFP_CONNECTOR_MU = 0x09,
1365 + SFP_CONNECTOR_SG = 0x0a,
1366 + SFP_CONNECTOR_OPTICAL_PIGTAIL = 0x0b,
1367 + SFP_CONNECTOR_MPO_1X12 = 0x0c,
1368 + SFP_CONNECTOR_MPO_2X16 = 0x0d,
1369 + SFP_CONNECTOR_HSSDC_II = 0x20,
1370 + SFP_CONNECTOR_COPPER_PIGTAIL = 0x21,
1371 + SFP_CONNECTOR_RJ45 = 0x22,
1372 + SFP_CONNECTOR_NOSEPARATE = 0x23,
1373 + SFP_CONNECTOR_MXC_2X16 = 0x24,
1374 + SFP_ENCODING_UNSPEC = 0x00,
1375 + SFP_ENCODING_8B10B = 0x01,
1376 + SFP_ENCODING_4B5B = 0x02,
1377 + SFP_ENCODING_NRZ = 0x03,
1378 + SFP_ENCODING_MANCHESTER = 0x04,
1379 + SFP_OPTIONS_HIGH_POWER_LEVEL = BIT(13),
1380 + SFP_OPTIONS_PAGING_A2 = BIT(12),
1381 + SFP_OPTIONS_RETIMER = BIT(11),
1382 + SFP_OPTIONS_COOLED_XCVR = BIT(10),
1383 + SFP_OPTIONS_POWER_DECL = BIT(9),
1384 + SFP_OPTIONS_RX_LINEAR_OUT = BIT(8),
1385 + SFP_OPTIONS_RX_DECISION_THRESH = BIT(7),
1386 + SFP_OPTIONS_TUNABLE_TX = BIT(6),
1387 + SFP_OPTIONS_RATE_SELECT = BIT(5),
1388 + SFP_OPTIONS_TX_DISABLE = BIT(4),
1389 + SFP_OPTIONS_TX_FAULT = BIT(3),
1390 + SFP_OPTIONS_LOS_INVERTED = BIT(2),
1391 + SFP_OPTIONS_LOS_NORMAL = BIT(1),
1392 + SFP_DIAGMON_DDM = BIT(6),
1393 + SFP_DIAGMON_INT_CAL = BIT(5),
1394 + SFP_DIAGMON_EXT_CAL = BIT(4),
1395 + SFP_DIAGMON_RXPWR_AVG = BIT(3),
1396 + SFP_DIAGMON_ADDRMODE = BIT(2),
1397 + SFP_ENHOPTS_ALARMWARN = BIT(7),
1398 + SFP_ENHOPTS_SOFT_TX_DISABLE = BIT(6),
1399 + SFP_ENHOPTS_SOFT_TX_FAULT = BIT(5),
1400 + SFP_ENHOPTS_SOFT_RX_LOS = BIT(4),
1401 + SFP_ENHOPTS_SOFT_RATE_SELECT = BIT(3),
1402 + SFP_ENHOPTS_APP_SELECT_SFF8079 = BIT(2),
1403 + SFP_ENHOPTS_SOFT_RATE_SFF8431 = BIT(1),
1404 + SFP_SFF8472_COMPLIANCE_NONE = 0x00,
1405 + SFP_SFF8472_COMPLIANCE_REV9_3 = 0x01,
1406 + SFP_SFF8472_COMPLIANCE_REV9_5 = 0x02,
1407 + SFP_SFF8472_COMPLIANCE_REV10_2 = 0x03,
1408 + SFP_SFF8472_COMPLIANCE_REV10_4 = 0x04,
1409 + SFP_SFF8472_COMPLIANCE_REV11_0 = 0x05,
1410 + SFP_SFF8472_COMPLIANCE_REV11_3 = 0x06,
1411 + SFP_SFF8472_COMPLIANCE_REV11_4 = 0x07,
1412 + SFP_SFF8472_COMPLIANCE_REV12_0 = 0x08,
1413 +};
1414 +
1415 +/* SFP Diagnostics */
1416 +enum {
1417 + /* Alarm and warnings stored MSB at lower address then LSB */
1418 + SFP_TEMP_HIGH_ALARM = 0x00,
1419 + SFP_TEMP_LOW_ALARM = 0x02,
1420 + SFP_TEMP_HIGH_WARN = 0x04,
1421 + SFP_TEMP_LOW_WARN = 0x06,
1422 + SFP_VOLT_HIGH_ALARM = 0x08,
1423 + SFP_VOLT_LOW_ALARM = 0x0a,
1424 + SFP_VOLT_HIGH_WARN = 0x0c,
1425 + SFP_VOLT_LOW_WARN = 0x0e,
1426 + SFP_BIAS_HIGH_ALARM = 0x10,
1427 + SFP_BIAS_LOW_ALARM = 0x12,
1428 + SFP_BIAS_HIGH_WARN = 0x14,
1429 + SFP_BIAS_LOW_WARN = 0x16,
1430 + SFP_TXPWR_HIGH_ALARM = 0x18,
1431 + SFP_TXPWR_LOW_ALARM = 0x1a,
1432 + SFP_TXPWR_HIGH_WARN = 0x1c,
1433 + SFP_TXPWR_LOW_WARN = 0x1e,
1434 + SFP_RXPWR_HIGH_ALARM = 0x20,
1435 + SFP_RXPWR_LOW_ALARM = 0x22,
1436 + SFP_RXPWR_HIGH_WARN = 0x24,
1437 + SFP_RXPWR_LOW_WARN = 0x26,
1438 + SFP_LASER_TEMP_HIGH_ALARM = 0x28,
1439 + SFP_LASER_TEMP_LOW_ALARM = 0x2a,
1440 + SFP_LASER_TEMP_HIGH_WARN = 0x2c,
1441 + SFP_LASER_TEMP_LOW_WARN = 0x2e,
1442 + SFP_TEC_CUR_HIGH_ALARM = 0x30,
1443 + SFP_TEC_CUR_LOW_ALARM = 0x32,
1444 + SFP_TEC_CUR_HIGH_WARN = 0x34,
1445 + SFP_TEC_CUR_LOW_WARN = 0x36,
1446 + SFP_CAL_RXPWR4 = 0x38,
1447 + SFP_CAL_RXPWR3 = 0x3c,
1448 + SFP_CAL_RXPWR2 = 0x40,
1449 + SFP_CAL_RXPWR1 = 0x44,
1450 + SFP_CAL_RXPWR0 = 0x48,
1451 + SFP_CAL_TXI_SLOPE = 0x4c,
1452 + SFP_CAL_TXI_OFFSET = 0x4e,
1453 + SFP_CAL_TXPWR_SLOPE = 0x50,
1454 + SFP_CAL_TXPWR_OFFSET = 0x52,
1455 + SFP_CAL_T_SLOPE = 0x54,
1456 + SFP_CAL_T_OFFSET = 0x56,
1457 + SFP_CAL_V_SLOPE = 0x58,
1458 + SFP_CAL_V_OFFSET = 0x5a,
1459 + SFP_CHKSUM = 0x5f,
1460 +
1461 + SFP_TEMP = 0x60,
1462 + SFP_VCC = 0x62,
1463 + SFP_TX_BIAS = 0x64,
1464 + SFP_TX_POWER = 0x66,
1465 + SFP_RX_POWER = 0x68,
1466 + SFP_LASER_TEMP = 0x6a,
1467 + SFP_TEC_CUR = 0x6c,
1468 +
1469 + SFP_STATUS = 0x6e,
1470 + SFP_ALARM = 0x70,
1471 +
1472 + SFP_EXT_STATUS = 0x76,
1473 + SFP_VSL = 0x78,
1474 + SFP_PAGE = 0x7f,
1475 +};
1476 +
1477 +#endif