blob: 96511557eb2c1a3be1a9f92bc6ff94b4ba4752f2 [file] [log] [blame]
Russell King73970052017-07-25 15:03:39 +01001#include <linux/delay.h>
Florian Fainelli54a2fc62017-10-30 21:42:58 -07002#include <linux/gpio/consumer.h>
Russell King73970052017-07-25 15:03:39 +01003#include <linux/i2c.h>
4#include <linux/interrupt.h>
5#include <linux/jiffies.h>
6#include <linux/module.h>
7#include <linux/mutex.h>
8#include <linux/of.h>
9#include <linux/phy.h>
10#include <linux/platform_device.h>
11#include <linux/rtnetlink.h>
12#include <linux/slab.h>
13#include <linux/workqueue.h>
14
15#include "mdio-i2c.h"
16#include "sfp.h"
17#include "swphy.h"
18
19enum {
20 GPIO_MODDEF0,
21 GPIO_LOS,
22 GPIO_TX_FAULT,
23 GPIO_TX_DISABLE,
24 GPIO_RATE_SELECT,
25 GPIO_MAX,
26
27 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
28 SFP_F_LOS = BIT(GPIO_LOS),
29 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
30 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
31 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
32
33 SFP_E_INSERT = 0,
34 SFP_E_REMOVE,
35 SFP_E_DEV_DOWN,
36 SFP_E_DEV_UP,
37 SFP_E_TX_FAULT,
38 SFP_E_TX_CLEAR,
39 SFP_E_LOS_HIGH,
40 SFP_E_LOS_LOW,
41 SFP_E_TIMEOUT,
42
43 SFP_MOD_EMPTY = 0,
44 SFP_MOD_PROBE,
45 SFP_MOD_PRESENT,
46 SFP_MOD_ERROR,
47
48 SFP_DEV_DOWN = 0,
49 SFP_DEV_UP,
50
51 SFP_S_DOWN = 0,
52 SFP_S_INIT,
53 SFP_S_WAIT_LOS,
54 SFP_S_LINK_UP,
55 SFP_S_TX_FAULT,
56 SFP_S_REINIT,
57 SFP_S_TX_DISABLE,
58};
59
60static const char *gpio_of_names[] = {
Baruch Siach25ee0792017-09-07 12:25:50 +030061 "mod-def0",
Russell King73970052017-07-25 15:03:39 +010062 "los",
63 "tx-fault",
64 "tx-disable",
Baruch Siach25ee0792017-09-07 12:25:50 +030065 "rate-select0",
Russell King73970052017-07-25 15:03:39 +010066};
67
68static const enum gpiod_flags gpio_flags[] = {
69 GPIOD_IN,
70 GPIOD_IN,
71 GPIOD_IN,
72 GPIOD_ASIS,
73 GPIOD_ASIS,
74};
75
76#define T_INIT_JIFFIES msecs_to_jiffies(300)
77#define T_RESET_US 10
78#define T_FAULT_RECOVER msecs_to_jiffies(1000)
79
80/* SFP module presence detection is poor: the three MOD DEF signals are
81 * the same length on the PCB, which means it's possible for MOD DEF 0 to
82 * connect before the I2C bus on MOD DEF 1/2.
83 *
84 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
85 * be deasserted) but makes no mention of the earliest time before we can
86 * access the I2C EEPROM. However, Avago modules require 300ms.
87 */
88#define T_PROBE_INIT msecs_to_jiffies(300)
89#define T_PROBE_RETRY msecs_to_jiffies(100)
90
Florian Fainelli516b29e2017-10-30 21:42:57 -070091/* SFP modules appear to always have their PHY configured for bus address
Russell King73970052017-07-25 15:03:39 +010092 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
93 */
94#define SFP_PHY_ADDR 22
95
Florian Fainelli516b29e2017-10-30 21:42:57 -070096/* Give this long for the PHY to reset. */
Russell King73970052017-07-25 15:03:39 +010097#define T_PHY_RESET_MS 50
98
99static DEFINE_MUTEX(sfp_mutex);
100
Russell King259c8612017-12-14 10:27:47 +0000101struct sff_data {
102 unsigned int gpios;
103 bool (*module_supported)(const struct sfp_eeprom_id *id);
104};
105
Russell King73970052017-07-25 15:03:39 +0100106struct sfp {
107 struct device *dev;
108 struct i2c_adapter *i2c;
109 struct mii_bus *i2c_mii;
110 struct sfp_bus *sfp_bus;
111 struct phy_device *mod_phy;
Russell King259c8612017-12-14 10:27:47 +0000112 const struct sff_data *type;
Russell King73970052017-07-25 15:03:39 +0100113
114 unsigned int (*get_state)(struct sfp *);
115 void (*set_state)(struct sfp *, unsigned int);
116 int (*read)(struct sfp *, bool, u8, void *, size_t);
117
118 struct gpio_desc *gpio[GPIO_MAX];
119
120 unsigned int state;
121 struct delayed_work poll;
122 struct delayed_work timeout;
123 struct mutex sm_mutex;
124 unsigned char sm_mod_state;
125 unsigned char sm_dev_state;
126 unsigned short sm_state;
127 unsigned int sm_retries;
128
129 struct sfp_eeprom_id id;
130};
131
Russell King259c8612017-12-14 10:27:47 +0000132static bool sff_module_supported(const struct sfp_eeprom_id *id)
133{
134 return id->base.phys_id == SFP_PHYS_ID_SFF &&
135 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
136}
137
138static const struct sff_data sff_data = {
139 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
140 .module_supported = sff_module_supported,
141};
142
143static bool sfp_module_supported(const struct sfp_eeprom_id *id)
144{
145 return id->base.phys_id == SFP_PHYS_ID_SFP &&
146 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
147}
148
149static const struct sff_data sfp_data = {
150 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
151 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
152 .module_supported = sfp_module_supported,
153};
154
155static const struct of_device_id sfp_of_match[] = {
156 { .compatible = "sff,sff", .data = &sff_data, },
157 { .compatible = "sff,sfp", .data = &sfp_data, },
158 { },
159};
160MODULE_DEVICE_TABLE(of, sfp_of_match);
161
Russell King73970052017-07-25 15:03:39 +0100162static unsigned long poll_jiffies;
163
164static unsigned int sfp_gpio_get_state(struct sfp *sfp)
165{
166 unsigned int i, state, v;
167
168 for (i = state = 0; i < GPIO_MAX; i++) {
169 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
170 continue;
171
172 v = gpiod_get_value_cansleep(sfp->gpio[i]);
173 if (v)
174 state |= BIT(i);
175 }
176
177 return state;
178}
179
Russell King259c8612017-12-14 10:27:47 +0000180static unsigned int sff_gpio_get_state(struct sfp *sfp)
181{
182 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
183}
184
Russell King73970052017-07-25 15:03:39 +0100185static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
186{
187 if (state & SFP_F_PRESENT) {
188 /* If the module is present, drive the signals */
189 if (sfp->gpio[GPIO_TX_DISABLE])
190 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700191 state & SFP_F_TX_DISABLE);
Russell King73970052017-07-25 15:03:39 +0100192 if (state & SFP_F_RATE_SELECT)
193 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700194 state & SFP_F_RATE_SELECT);
Russell King73970052017-07-25 15:03:39 +0100195 } else {
196 /* Otherwise, let them float to the pull-ups */
197 if (sfp->gpio[GPIO_TX_DISABLE])
198 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
199 if (state & SFP_F_RATE_SELECT)
200 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
201 }
202}
203
204static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700205 void *buf, size_t len)
Russell King73970052017-07-25 15:03:39 +0100206{
207 struct i2c_msg msgs[2];
208 int ret;
209
210 msgs[0].addr = bus_addr;
211 msgs[0].flags = 0;
212 msgs[0].len = 1;
213 msgs[0].buf = &dev_addr;
214 msgs[1].addr = bus_addr;
215 msgs[1].flags = I2C_M_RD;
216 msgs[1].len = len;
217 msgs[1].buf = buf;
218
219 ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
220 if (ret < 0)
221 return ret;
222
223 return ret == ARRAY_SIZE(msgs) ? len : 0;
224}
225
226static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700227 size_t len)
Russell King73970052017-07-25 15:03:39 +0100228{
229 return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
230}
231
232static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
233{
234 struct mii_bus *i2c_mii;
235 int ret;
236
237 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
238 return -EINVAL;
239
240 sfp->i2c = i2c;
241 sfp->read = sfp_i2c_read;
242
243 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
244 if (IS_ERR(i2c_mii))
245 return PTR_ERR(i2c_mii);
246
247 i2c_mii->name = "SFP I2C Bus";
248 i2c_mii->phy_mask = ~0;
249
250 ret = mdiobus_register(i2c_mii);
251 if (ret < 0) {
252 mdiobus_free(i2c_mii);
253 return ret;
254 }
255
256 sfp->i2c_mii = i2c_mii;
257
258 return 0;
259}
260
Russell King73970052017-07-25 15:03:39 +0100261/* Interface */
262static unsigned int sfp_get_state(struct sfp *sfp)
263{
264 return sfp->get_state(sfp);
265}
266
267static void sfp_set_state(struct sfp *sfp, unsigned int state)
268{
269 sfp->set_state(sfp, state);
270}
271
272static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
273{
274 return sfp->read(sfp, a2, addr, buf, len);
275}
276
277static unsigned int sfp_check(void *buf, size_t len)
278{
279 u8 *p, check;
280
281 for (p = buf, check = 0; len; p++, len--)
282 check += *p;
283
284 return check;
285}
286
287/* Helpers */
288static void sfp_module_tx_disable(struct sfp *sfp)
289{
290 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
291 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
292 sfp->state |= SFP_F_TX_DISABLE;
293 sfp_set_state(sfp, sfp->state);
294}
295
296static void sfp_module_tx_enable(struct sfp *sfp)
297{
298 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
299 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
300 sfp->state &= ~SFP_F_TX_DISABLE;
301 sfp_set_state(sfp, sfp->state);
302}
303
304static void sfp_module_tx_fault_reset(struct sfp *sfp)
305{
306 unsigned int state = sfp->state;
307
308 if (state & SFP_F_TX_DISABLE)
309 return;
310
311 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
312
313 udelay(T_RESET_US);
314
315 sfp_set_state(sfp, state);
316}
317
318/* SFP state machine */
319static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
320{
321 if (timeout)
322 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
323 timeout);
324 else
325 cancel_delayed_work(&sfp->timeout);
326}
327
328static void sfp_sm_next(struct sfp *sfp, unsigned int state,
329 unsigned int timeout)
330{
331 sfp->sm_state = state;
332 sfp_sm_set_timer(sfp, timeout);
333}
334
Florian Fainelli516b29e2017-10-30 21:42:57 -0700335static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
336 unsigned int timeout)
Russell King73970052017-07-25 15:03:39 +0100337{
338 sfp->sm_mod_state = state;
339 sfp_sm_set_timer(sfp, timeout);
340}
341
342static void sfp_sm_phy_detach(struct sfp *sfp)
343{
344 phy_stop(sfp->mod_phy);
345 sfp_remove_phy(sfp->sfp_bus);
346 phy_device_remove(sfp->mod_phy);
347 phy_device_free(sfp->mod_phy);
348 sfp->mod_phy = NULL;
349}
350
351static void sfp_sm_probe_phy(struct sfp *sfp)
352{
353 struct phy_device *phy;
354 int err;
355
356 msleep(T_PHY_RESET_MS);
357
358 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
359 if (IS_ERR(phy)) {
360 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
361 return;
362 }
363 if (!phy) {
364 dev_info(sfp->dev, "no PHY detected\n");
365 return;
366 }
367
368 err = sfp_add_phy(sfp->sfp_bus, phy);
369 if (err) {
370 phy_device_remove(phy);
371 phy_device_free(phy);
372 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
373 return;
374 }
375
376 sfp->mod_phy = phy;
377 phy_start(phy);
378}
379
380static void sfp_sm_link_up(struct sfp *sfp)
381{
382 sfp_link_up(sfp->sfp_bus);
383 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
384}
385
386static void sfp_sm_link_down(struct sfp *sfp)
387{
388 sfp_link_down(sfp->sfp_bus);
389}
390
391static void sfp_sm_link_check_los(struct sfp *sfp)
392{
393 unsigned int los = sfp->state & SFP_F_LOS;
394
Russell King710dfbb2017-11-30 13:59:16 +0000395 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
396 * are set, we assume that no LOS signal is available.
Russell King73970052017-07-25 15:03:39 +0100397 */
Russell Kingacf1c022017-11-30 13:59:11 +0000398 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
Russell King73970052017-07-25 15:03:39 +0100399 los ^= SFP_F_LOS;
Russell King710dfbb2017-11-30 13:59:16 +0000400 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
401 los = 0;
Russell King73970052017-07-25 15:03:39 +0100402
403 if (los)
404 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
405 else
406 sfp_sm_link_up(sfp);
407}
408
Russell King710dfbb2017-11-30 13:59:16 +0000409static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
410{
411 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
412 event == SFP_E_LOS_LOW) ||
413 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
414 event == SFP_E_LOS_HIGH);
415}
416
417static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
418{
419 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
420 event == SFP_E_LOS_HIGH) ||
421 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
422 event == SFP_E_LOS_LOW);
423}
424
Russell King73970052017-07-25 15:03:39 +0100425static void sfp_sm_fault(struct sfp *sfp, bool warn)
426{
427 if (sfp->sm_retries && !--sfp->sm_retries) {
Florian Fainelli516b29e2017-10-30 21:42:57 -0700428 dev_err(sfp->dev,
429 "module persistently indicates fault, disabling\n");
Russell King73970052017-07-25 15:03:39 +0100430 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
431 } else {
432 if (warn)
433 dev_err(sfp->dev, "module transmit fault indicated\n");
434
435 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
436 }
437}
438
439static void sfp_sm_mod_init(struct sfp *sfp)
440{
441 sfp_module_tx_enable(sfp);
442
443 /* Wait t_init before indicating that the link is up, provided the
444 * current state indicates no TX_FAULT. If TX_FAULT clears before
445 * this time, that's fine too.
446 */
447 sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
448 sfp->sm_retries = 5;
449
450 /* Setting the serdes link mode is guesswork: there's no
451 * field in the EEPROM which indicates what mode should
452 * be used.
453 *
454 * If it's a gigabit-only fiber module, it probably does
455 * not have a PHY, so switch to 802.3z negotiation mode.
456 * Otherwise, switch to SGMII mode (which is required to
457 * support non-gigabit speeds) and probe for a PHY.
458 */
459 if (sfp->id.base.e1000_base_t ||
460 sfp->id.base.e100_base_lx ||
461 sfp->id.base.e100_base_fx)
462 sfp_sm_probe_phy(sfp);
463}
464
465static int sfp_sm_mod_probe(struct sfp *sfp)
466{
467 /* SFP module inserted - read I2C data */
468 struct sfp_eeprom_id id;
469 char vendor[17];
470 char part[17];
471 char sn[17];
472 char date[9];
473 char rev[5];
474 u8 check;
475 int err;
476
477 err = sfp_read(sfp, false, 0, &id, sizeof(id));
478 if (err < 0) {
479 dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
480 return -EAGAIN;
481 }
482
483 if (err != sizeof(id)) {
484 dev_err(sfp->dev, "EEPROM short read: %d\n", err);
485 return -EAGAIN;
486 }
487
488 /* Validate the checksum over the base structure */
489 check = sfp_check(&id.base, sizeof(id.base) - 1);
490 if (check != id.base.cc_base) {
491 dev_err(sfp->dev,
492 "EEPROM base structure checksum failure: 0x%02x\n",
493 check);
494 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
495 16, 1, &id, sizeof(id.base) - 1, true);
496 return -EINVAL;
497 }
498
499 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
500 if (check != id.ext.cc_ext) {
501 dev_err(sfp->dev,
502 "EEPROM extended structure checksum failure: 0x%02x\n",
503 check);
504 memset(&id.ext, 0, sizeof(id.ext));
505 }
506
507 sfp->id = id;
508
509 memcpy(vendor, sfp->id.base.vendor_name, 16);
510 vendor[16] = '\0';
511 memcpy(part, sfp->id.base.vendor_pn, 16);
512 part[16] = '\0';
513 memcpy(rev, sfp->id.base.vendor_rev, 4);
514 rev[4] = '\0';
515 memcpy(sn, sfp->id.ext.vendor_sn, 16);
516 sn[16] = '\0';
517 memcpy(date, sfp->id.ext.datecode, 8);
518 date[8] = '\0';
519
Florian Fainelli516b29e2017-10-30 21:42:57 -0700520 dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n",
521 vendor, part, rev, sn, date);
Russell King73970052017-07-25 15:03:39 +0100522
Russell King259c8612017-12-14 10:27:47 +0000523 /* Check whether we support this module */
524 if (!sfp->type->module_supported(&sfp->id)) {
525 dev_err(sfp->dev,
526 "module is not supported - phys id 0x%02x 0x%02x\n",
Russell King73970052017-07-25 15:03:39 +0100527 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
528 return -EINVAL;
529 }
530
Russell Kingec7681b2017-11-30 13:59:21 +0000531 /* If the module requires address swap mode, warn about it */
532 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
533 dev_warn(sfp->dev,
534 "module address swap to access page 0xA2 is not supported.\n");
535
Russell King73970052017-07-25 15:03:39 +0100536 return sfp_module_insert(sfp->sfp_bus, &sfp->id);
537}
538
539static void sfp_sm_mod_remove(struct sfp *sfp)
540{
541 sfp_module_remove(sfp->sfp_bus);
542
543 if (sfp->mod_phy)
544 sfp_sm_phy_detach(sfp);
545
546 sfp_module_tx_disable(sfp);
547
548 memset(&sfp->id, 0, sizeof(sfp->id));
549
550 dev_info(sfp->dev, "module removed\n");
551}
552
553static void sfp_sm_event(struct sfp *sfp, unsigned int event)
554{
555 mutex_lock(&sfp->sm_mutex);
556
557 dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
558 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
559
560 /* This state machine tracks the insert/remove state of
561 * the module, and handles probing the on-board EEPROM.
562 */
563 switch (sfp->sm_mod_state) {
564 default:
565 if (event == SFP_E_INSERT) {
566 sfp_module_tx_disable(sfp);
567 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
568 }
569 break;
570
571 case SFP_MOD_PROBE:
572 if (event == SFP_E_REMOVE) {
573 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
574 } else if (event == SFP_E_TIMEOUT) {
575 int err = sfp_sm_mod_probe(sfp);
576
577 if (err == 0)
578 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
579 else if (err == -EAGAIN)
580 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
581 else
582 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
583 }
584 break;
585
586 case SFP_MOD_PRESENT:
587 case SFP_MOD_ERROR:
588 if (event == SFP_E_REMOVE) {
589 sfp_sm_mod_remove(sfp);
590 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
591 }
592 break;
593 }
594
595 /* This state machine tracks the netdev up/down state */
596 switch (sfp->sm_dev_state) {
597 default:
598 if (event == SFP_E_DEV_UP)
599 sfp->sm_dev_state = SFP_DEV_UP;
600 break;
601
602 case SFP_DEV_UP:
603 if (event == SFP_E_DEV_DOWN) {
604 /* If the module has a PHY, avoid raising TX disable
605 * as this resets the PHY. Otherwise, raise it to
606 * turn the laser off.
607 */
608 if (!sfp->mod_phy)
609 sfp_module_tx_disable(sfp);
610 sfp->sm_dev_state = SFP_DEV_DOWN;
611 }
612 break;
613 }
614
615 /* Some events are global */
616 if (sfp->sm_state != SFP_S_DOWN &&
617 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
618 sfp->sm_dev_state != SFP_DEV_UP)) {
619 if (sfp->sm_state == SFP_S_LINK_UP &&
620 sfp->sm_dev_state == SFP_DEV_UP)
621 sfp_sm_link_down(sfp);
622 if (sfp->mod_phy)
623 sfp_sm_phy_detach(sfp);
624 sfp_sm_next(sfp, SFP_S_DOWN, 0);
625 mutex_unlock(&sfp->sm_mutex);
626 return;
627 }
628
629 /* The main state machine */
630 switch (sfp->sm_state) {
631 case SFP_S_DOWN:
632 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
633 sfp->sm_dev_state == SFP_DEV_UP)
634 sfp_sm_mod_init(sfp);
635 break;
636
637 case SFP_S_INIT:
638 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
639 sfp_sm_fault(sfp, true);
640 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
641 sfp_sm_link_check_los(sfp);
642 break;
643
644 case SFP_S_WAIT_LOS:
645 if (event == SFP_E_TX_FAULT)
646 sfp_sm_fault(sfp, true);
Russell King710dfbb2017-11-30 13:59:16 +0000647 else if (sfp_los_event_inactive(sfp, event))
Russell King73970052017-07-25 15:03:39 +0100648 sfp_sm_link_up(sfp);
649 break;
650
651 case SFP_S_LINK_UP:
652 if (event == SFP_E_TX_FAULT) {
653 sfp_sm_link_down(sfp);
654 sfp_sm_fault(sfp, true);
Russell King710dfbb2017-11-30 13:59:16 +0000655 } else if (sfp_los_event_active(sfp, event)) {
Russell King73970052017-07-25 15:03:39 +0100656 sfp_sm_link_down(sfp);
657 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
658 }
659 break;
660
661 case SFP_S_TX_FAULT:
662 if (event == SFP_E_TIMEOUT) {
663 sfp_module_tx_fault_reset(sfp);
664 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
665 }
666 break;
667
668 case SFP_S_REINIT:
669 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
670 sfp_sm_fault(sfp, false);
671 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
672 dev_info(sfp->dev, "module transmit fault recovered\n");
673 sfp_sm_link_check_los(sfp);
674 }
675 break;
676
677 case SFP_S_TX_DISABLE:
678 break;
679 }
680
681 dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
682 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
683
684 mutex_unlock(&sfp->sm_mutex);
685}
686
687static void sfp_start(struct sfp *sfp)
688{
689 sfp_sm_event(sfp, SFP_E_DEV_UP);
690}
691
692static void sfp_stop(struct sfp *sfp)
693{
694 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
695}
696
697static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
698{
699 /* locking... and check module is present */
700
Russell Kingec7681b2017-11-30 13:59:21 +0000701 if (sfp->id.ext.sff8472_compliance &&
702 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
Russell King73970052017-07-25 15:03:39 +0100703 modinfo->type = ETH_MODULE_SFF_8472;
704 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
705 } else {
706 modinfo->type = ETH_MODULE_SFF_8079;
707 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
708 }
709 return 0;
710}
711
712static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
Florian Fainelli516b29e2017-10-30 21:42:57 -0700713 u8 *data)
Russell King73970052017-07-25 15:03:39 +0100714{
715 unsigned int first, last, len;
716 int ret;
717
718 if (ee->len == 0)
719 return -EINVAL;
720
721 first = ee->offset;
722 last = ee->offset + ee->len;
723 if (first < ETH_MODULE_SFF_8079_LEN) {
724 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
725 len -= first;
726
727 ret = sfp->read(sfp, false, first, data, len);
728 if (ret < 0)
729 return ret;
730
731 first += len;
732 data += len;
733 }
734 if (first >= ETH_MODULE_SFF_8079_LEN &&
735 first < ETH_MODULE_SFF_8472_LEN) {
736 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
737 len -= first;
738 first -= ETH_MODULE_SFF_8079_LEN;
739
740 ret = sfp->read(sfp, true, first, data, len);
741 if (ret < 0)
742 return ret;
743 }
744 return 0;
745}
746
747static const struct sfp_socket_ops sfp_module_ops = {
748 .start = sfp_start,
749 .stop = sfp_stop,
750 .module_info = sfp_module_info,
751 .module_eeprom = sfp_module_eeprom,
752};
753
754static void sfp_timeout(struct work_struct *work)
755{
756 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
757
758 rtnl_lock();
759 sfp_sm_event(sfp, SFP_E_TIMEOUT);
760 rtnl_unlock();
761}
762
763static void sfp_check_state(struct sfp *sfp)
764{
765 unsigned int state, i, changed;
766
767 state = sfp_get_state(sfp);
768 changed = state ^ sfp->state;
769 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
770
771 for (i = 0; i < GPIO_MAX; i++)
772 if (changed & BIT(i))
773 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
774 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
775
776 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
777 sfp->state = state;
778
779 rtnl_lock();
780 if (changed & SFP_F_PRESENT)
781 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
782 SFP_E_INSERT : SFP_E_REMOVE);
783
784 if (changed & SFP_F_TX_FAULT)
785 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
786 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
787
788 if (changed & SFP_F_LOS)
789 sfp_sm_event(sfp, state & SFP_F_LOS ?
790 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
791 rtnl_unlock();
792}
793
794static irqreturn_t sfp_irq(int irq, void *data)
795{
796 struct sfp *sfp = data;
797
798 sfp_check_state(sfp);
799
800 return IRQ_HANDLED;
801}
802
803static void sfp_poll(struct work_struct *work)
804{
805 struct sfp *sfp = container_of(work, struct sfp, poll.work);
806
807 sfp_check_state(sfp);
808 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
809}
810
811static struct sfp *sfp_alloc(struct device *dev)
812{
813 struct sfp *sfp;
814
815 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
816 if (!sfp)
817 return ERR_PTR(-ENOMEM);
818
819 sfp->dev = dev;
820
821 mutex_init(&sfp->sm_mutex);
822 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
823 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
824
825 return sfp;
826}
827
828static void sfp_cleanup(void *data)
829{
830 struct sfp *sfp = data;
831
832 cancel_delayed_work_sync(&sfp->poll);
833 cancel_delayed_work_sync(&sfp->timeout);
834 if (sfp->i2c_mii) {
835 mdiobus_unregister(sfp->i2c_mii);
836 mdiobus_free(sfp->i2c_mii);
837 }
838 if (sfp->i2c)
839 i2c_put_adapter(sfp->i2c);
840 kfree(sfp);
841}
842
843static int sfp_probe(struct platform_device *pdev)
844{
Russell King259c8612017-12-14 10:27:47 +0000845 const struct sff_data *sff;
Russell King73970052017-07-25 15:03:39 +0100846 struct sfp *sfp;
847 bool poll = false;
848 int irq, err, i;
849
850 sfp = sfp_alloc(&pdev->dev);
851 if (IS_ERR(sfp))
852 return PTR_ERR(sfp);
853
854 platform_set_drvdata(pdev, sfp);
855
856 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
857 if (err < 0)
858 return err;
859
Russell King259c8612017-12-14 10:27:47 +0000860 sff = sfp->type = &sfp_data;
861
Russell King73970052017-07-25 15:03:39 +0100862 if (pdev->dev.of_node) {
863 struct device_node *node = pdev->dev.of_node;
Russell King259c8612017-12-14 10:27:47 +0000864 const struct of_device_id *id;
Russell King73970052017-07-25 15:03:39 +0100865 struct device_node *np;
866
Russell King259c8612017-12-14 10:27:47 +0000867 id = of_match_node(sfp_of_match, node);
868 if (WARN_ON(!id))
869 return -EINVAL;
870
871 sff = sfp->type = id->data;
872
Russell King73970052017-07-25 15:03:39 +0100873 np = of_parse_phandle(node, "i2c-bus", 0);
874 if (np) {
875 struct i2c_adapter *i2c;
876
877 i2c = of_find_i2c_adapter_by_node(np);
878 of_node_put(np);
879 if (!i2c)
880 return -EPROBE_DEFER;
881
882 err = sfp_i2c_configure(sfp, i2c);
883 if (err < 0) {
884 i2c_put_adapter(i2c);
885 return err;
886 }
887 }
Russell King259c8612017-12-14 10:27:47 +0000888 }
Russell King73970052017-07-25 15:03:39 +0100889
Russell King259c8612017-12-14 10:27:47 +0000890 for (i = 0; i < GPIO_MAX; i++)
891 if (sff->gpios & BIT(i)) {
Russell King73970052017-07-25 15:03:39 +0100892 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
893 gpio_of_names[i], gpio_flags[i]);
894 if (IS_ERR(sfp->gpio[i]))
895 return PTR_ERR(sfp->gpio[i]);
896 }
897
Russell King259c8612017-12-14 10:27:47 +0000898 sfp->get_state = sfp_gpio_get_state;
899 sfp->set_state = sfp_gpio_set_state;
900
901 /* Modules that have no detect signal are always present */
902 if (!(sfp->gpio[GPIO_MODDEF0]))
903 sfp->get_state = sff_gpio_get_state;
Russell King73970052017-07-25 15:03:39 +0100904
905 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
906 if (!sfp->sfp_bus)
907 return -ENOMEM;
908
909 /* Get the initial state, and always signal TX disable,
910 * since the network interface will not be up.
911 */
912 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
913
914 if (sfp->gpio[GPIO_RATE_SELECT] &&
915 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
916 sfp->state |= SFP_F_RATE_SELECT;
917 sfp_set_state(sfp, sfp->state);
918 sfp_module_tx_disable(sfp);
919 rtnl_lock();
920 if (sfp->state & SFP_F_PRESENT)
921 sfp_sm_event(sfp, SFP_E_INSERT);
922 rtnl_unlock();
923
924 for (i = 0; i < GPIO_MAX; i++) {
925 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
926 continue;
927
928 irq = gpiod_to_irq(sfp->gpio[i]);
929 if (!irq) {
930 poll = true;
931 continue;
932 }
933
934 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
935 IRQF_ONESHOT |
936 IRQF_TRIGGER_RISING |
937 IRQF_TRIGGER_FALLING,
938 dev_name(sfp->dev), sfp);
939 if (err)
940 poll = true;
941 }
942
943 if (poll)
944 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
945
946 return 0;
947}
948
949static int sfp_remove(struct platform_device *pdev)
950{
951 struct sfp *sfp = platform_get_drvdata(pdev);
952
953 sfp_unregister_socket(sfp->sfp_bus);
954
955 return 0;
956}
957
Russell King73970052017-07-25 15:03:39 +0100958static struct platform_driver sfp_driver = {
959 .probe = sfp_probe,
960 .remove = sfp_remove,
961 .driver = {
962 .name = "sfp",
963 .of_match_table = sfp_of_match,
964 },
965};
966
967static int sfp_init(void)
968{
969 poll_jiffies = msecs_to_jiffies(100);
970
971 return platform_driver_register(&sfp_driver);
972}
973module_init(sfp_init);
974
975static void sfp_exit(void)
976{
977 platform_driver_unregister(&sfp_driver);
978}
979module_exit(sfp_exit);
980
981MODULE_ALIAS("platform:sfp");
982MODULE_AUTHOR("Russell King");
983MODULE_LICENSE("GPL v2");