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