]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/net/phy/sfp.c
mac80211: don't WARN on bad WMM parameters from buggy APs
[mirror_ubuntu-eoan-kernel.git] / drivers / net / phy / sfp.c
1 #include <linux/delay.h>
2 #include <linux/gpio/consumer.h>
3 #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
19 enum {
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_HPOWER,
46 SFP_MOD_PRESENT,
47 SFP_MOD_ERROR,
48
49 SFP_DEV_DOWN = 0,
50 SFP_DEV_UP,
51
52 SFP_S_DOWN = 0,
53 SFP_S_INIT,
54 SFP_S_WAIT_LOS,
55 SFP_S_LINK_UP,
56 SFP_S_TX_FAULT,
57 SFP_S_REINIT,
58 SFP_S_TX_DISABLE,
59 };
60
61 static const char *gpio_of_names[] = {
62 "mod-def0",
63 "los",
64 "tx-fault",
65 "tx-disable",
66 "rate-select0",
67 };
68
69 static const enum gpiod_flags gpio_flags[] = {
70 GPIOD_IN,
71 GPIOD_IN,
72 GPIOD_IN,
73 GPIOD_ASIS,
74 GPIOD_ASIS,
75 };
76
77 #define T_INIT_JIFFIES msecs_to_jiffies(300)
78 #define T_RESET_US 10
79 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
80
81 /* SFP module presence detection is poor: the three MOD DEF signals are
82 * the same length on the PCB, which means it's possible for MOD DEF 0 to
83 * connect before the I2C bus on MOD DEF 1/2.
84 *
85 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
86 * be deasserted) but makes no mention of the earliest time before we can
87 * access the I2C EEPROM. However, Avago modules require 300ms.
88 */
89 #define T_PROBE_INIT msecs_to_jiffies(300)
90 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
91 #define T_PROBE_RETRY msecs_to_jiffies(100)
92
93 /* SFP modules appear to always have their PHY configured for bus address
94 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
95 */
96 #define SFP_PHY_ADDR 22
97
98 /* Give this long for the PHY to reset. */
99 #define T_PHY_RESET_MS 50
100
101 static DEFINE_MUTEX(sfp_mutex);
102
103 struct sff_data {
104 unsigned int gpios;
105 bool (*module_supported)(const struct sfp_eeprom_id *id);
106 };
107
108 struct sfp {
109 struct device *dev;
110 struct i2c_adapter *i2c;
111 struct mii_bus *i2c_mii;
112 struct sfp_bus *sfp_bus;
113 struct phy_device *mod_phy;
114 const struct sff_data *type;
115 u32 max_power_mW;
116
117 unsigned int (*get_state)(struct sfp *);
118 void (*set_state)(struct sfp *, unsigned int);
119 int (*read)(struct sfp *, bool, u8, void *, size_t);
120 int (*write)(struct sfp *, bool, u8, void *, size_t);
121
122 struct gpio_desc *gpio[GPIO_MAX];
123
124 unsigned int state;
125 struct delayed_work poll;
126 struct delayed_work timeout;
127 struct mutex sm_mutex;
128 unsigned char sm_mod_state;
129 unsigned char sm_dev_state;
130 unsigned short sm_state;
131 unsigned int sm_retries;
132
133 struct sfp_eeprom_id id;
134 };
135
136 static bool sff_module_supported(const struct sfp_eeprom_id *id)
137 {
138 return id->base.phys_id == SFP_PHYS_ID_SFF &&
139 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
140 }
141
142 static const struct sff_data sff_data = {
143 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
144 .module_supported = sff_module_supported,
145 };
146
147 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
148 {
149 return id->base.phys_id == SFP_PHYS_ID_SFP &&
150 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
151 }
152
153 static const struct sff_data sfp_data = {
154 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
155 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
156 .module_supported = sfp_module_supported,
157 };
158
159 static const struct of_device_id sfp_of_match[] = {
160 { .compatible = "sff,sff", .data = &sff_data, },
161 { .compatible = "sff,sfp", .data = &sfp_data, },
162 { },
163 };
164 MODULE_DEVICE_TABLE(of, sfp_of_match);
165
166 static unsigned long poll_jiffies;
167
168 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
169 {
170 unsigned int i, state, v;
171
172 for (i = state = 0; i < GPIO_MAX; i++) {
173 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
174 continue;
175
176 v = gpiod_get_value_cansleep(sfp->gpio[i]);
177 if (v)
178 state |= BIT(i);
179 }
180
181 return state;
182 }
183
184 static unsigned int sff_gpio_get_state(struct sfp *sfp)
185 {
186 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
187 }
188
189 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
190 {
191 if (state & SFP_F_PRESENT) {
192 /* If the module is present, drive the signals */
193 if (sfp->gpio[GPIO_TX_DISABLE])
194 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
195 state & SFP_F_TX_DISABLE);
196 if (state & SFP_F_RATE_SELECT)
197 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
198 state & SFP_F_RATE_SELECT);
199 } else {
200 /* Otherwise, let them float to the pull-ups */
201 if (sfp->gpio[GPIO_TX_DISABLE])
202 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
203 if (state & SFP_F_RATE_SELECT)
204 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
205 }
206 }
207
208 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
209 size_t len)
210 {
211 struct i2c_msg msgs[2];
212 u8 bus_addr = a2 ? 0x51 : 0x50;
213 int ret;
214
215 msgs[0].addr = bus_addr;
216 msgs[0].flags = 0;
217 msgs[0].len = 1;
218 msgs[0].buf = &dev_addr;
219 msgs[1].addr = bus_addr;
220 msgs[1].flags = I2C_M_RD;
221 msgs[1].len = len;
222 msgs[1].buf = buf;
223
224 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
225 if (ret < 0)
226 return ret;
227
228 return ret == ARRAY_SIZE(msgs) ? len : 0;
229 }
230
231 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
232 size_t len)
233 {
234 struct i2c_msg msgs[1];
235 u8 bus_addr = a2 ? 0x51 : 0x50;
236 int ret;
237
238 msgs[0].addr = bus_addr;
239 msgs[0].flags = 0;
240 msgs[0].len = 1 + len;
241 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
242 if (!msgs[0].buf)
243 return -ENOMEM;
244
245 msgs[0].buf[0] = dev_addr;
246 memcpy(&msgs[0].buf[1], buf, len);
247
248 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
249
250 kfree(msgs[0].buf);
251
252 if (ret < 0)
253 return ret;
254
255 return ret == ARRAY_SIZE(msgs) ? len : 0;
256 }
257
258 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
259 {
260 struct mii_bus *i2c_mii;
261 int ret;
262
263 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
264 return -EINVAL;
265
266 sfp->i2c = i2c;
267 sfp->read = sfp_i2c_read;
268 sfp->write = sfp_i2c_write;
269
270 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
271 if (IS_ERR(i2c_mii))
272 return PTR_ERR(i2c_mii);
273
274 i2c_mii->name = "SFP I2C Bus";
275 i2c_mii->phy_mask = ~0;
276
277 ret = mdiobus_register(i2c_mii);
278 if (ret < 0) {
279 mdiobus_free(i2c_mii);
280 return ret;
281 }
282
283 sfp->i2c_mii = i2c_mii;
284
285 return 0;
286 }
287
288 /* Interface */
289 static unsigned int sfp_get_state(struct sfp *sfp)
290 {
291 return sfp->get_state(sfp);
292 }
293
294 static void sfp_set_state(struct sfp *sfp, unsigned int state)
295 {
296 sfp->set_state(sfp, state);
297 }
298
299 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
300 {
301 return sfp->read(sfp, a2, addr, buf, len);
302 }
303
304 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
305 {
306 return sfp->write(sfp, a2, addr, buf, len);
307 }
308
309 static unsigned int sfp_check(void *buf, size_t len)
310 {
311 u8 *p, check;
312
313 for (p = buf, check = 0; len; p++, len--)
314 check += *p;
315
316 return check;
317 }
318
319 /* Helpers */
320 static void sfp_module_tx_disable(struct sfp *sfp)
321 {
322 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
323 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
324 sfp->state |= SFP_F_TX_DISABLE;
325 sfp_set_state(sfp, sfp->state);
326 }
327
328 static void sfp_module_tx_enable(struct sfp *sfp)
329 {
330 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
331 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
332 sfp->state &= ~SFP_F_TX_DISABLE;
333 sfp_set_state(sfp, sfp->state);
334 }
335
336 static void sfp_module_tx_fault_reset(struct sfp *sfp)
337 {
338 unsigned int state = sfp->state;
339
340 if (state & SFP_F_TX_DISABLE)
341 return;
342
343 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
344
345 udelay(T_RESET_US);
346
347 sfp_set_state(sfp, state);
348 }
349
350 /* SFP state machine */
351 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
352 {
353 if (timeout)
354 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
355 timeout);
356 else
357 cancel_delayed_work(&sfp->timeout);
358 }
359
360 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
361 unsigned int timeout)
362 {
363 sfp->sm_state = state;
364 sfp_sm_set_timer(sfp, timeout);
365 }
366
367 static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
368 unsigned int timeout)
369 {
370 sfp->sm_mod_state = state;
371 sfp_sm_set_timer(sfp, timeout);
372 }
373
374 static void sfp_sm_phy_detach(struct sfp *sfp)
375 {
376 phy_stop(sfp->mod_phy);
377 sfp_remove_phy(sfp->sfp_bus);
378 phy_device_remove(sfp->mod_phy);
379 phy_device_free(sfp->mod_phy);
380 sfp->mod_phy = NULL;
381 }
382
383 static void sfp_sm_probe_phy(struct sfp *sfp)
384 {
385 struct phy_device *phy;
386 int err;
387
388 msleep(T_PHY_RESET_MS);
389
390 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
391 if (phy == ERR_PTR(-ENODEV)) {
392 dev_info(sfp->dev, "no PHY detected\n");
393 return;
394 }
395 if (IS_ERR(phy)) {
396 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
397 return;
398 }
399
400 err = sfp_add_phy(sfp->sfp_bus, phy);
401 if (err) {
402 phy_device_remove(phy);
403 phy_device_free(phy);
404 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
405 return;
406 }
407
408 sfp->mod_phy = phy;
409 phy_start(phy);
410 }
411
412 static void sfp_sm_link_up(struct sfp *sfp)
413 {
414 sfp_link_up(sfp->sfp_bus);
415 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
416 }
417
418 static void sfp_sm_link_down(struct sfp *sfp)
419 {
420 sfp_link_down(sfp->sfp_bus);
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 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
428 * are set, we assume that no LOS signal is available.
429 */
430 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
431 los ^= SFP_F_LOS;
432 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
433 los = 0;
434
435 if (los)
436 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
437 else
438 sfp_sm_link_up(sfp);
439 }
440
441 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
442 {
443 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
444 event == SFP_E_LOS_LOW) ||
445 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
446 event == SFP_E_LOS_HIGH);
447 }
448
449 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
450 {
451 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
452 event == SFP_E_LOS_HIGH) ||
453 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
454 event == SFP_E_LOS_LOW);
455 }
456
457 static void sfp_sm_fault(struct sfp *sfp, bool warn)
458 {
459 if (sfp->sm_retries && !--sfp->sm_retries) {
460 dev_err(sfp->dev,
461 "module persistently indicates fault, disabling\n");
462 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
463 } else {
464 if (warn)
465 dev_err(sfp->dev, "module transmit fault indicated\n");
466
467 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
468 }
469 }
470
471 static void sfp_sm_mod_init(struct sfp *sfp)
472 {
473 sfp_module_tx_enable(sfp);
474
475 /* Wait t_init before indicating that the link is up, provided the
476 * current state indicates no TX_FAULT. If TX_FAULT clears before
477 * this time, that's fine too.
478 */
479 sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
480 sfp->sm_retries = 5;
481
482 /* Setting the serdes link mode is guesswork: there's no
483 * field in the EEPROM which indicates what mode should
484 * be used.
485 *
486 * If it's a gigabit-only fiber module, it probably does
487 * not have a PHY, so switch to 802.3z negotiation mode.
488 * Otherwise, switch to SGMII mode (which is required to
489 * support non-gigabit speeds) and probe for a PHY.
490 */
491 if (sfp->id.base.e1000_base_t ||
492 sfp->id.base.e100_base_lx ||
493 sfp->id.base.e100_base_fx)
494 sfp_sm_probe_phy(sfp);
495 }
496
497 static int sfp_sm_mod_hpower(struct sfp *sfp)
498 {
499 u32 power;
500 u8 val;
501 int err;
502
503 power = 1000;
504 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
505 power = 1500;
506 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
507 power = 2000;
508
509 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
510 (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
511 SFP_DIAGMON_DDM) {
512 /* The module appears not to implement bus address 0xa2,
513 * or requires an address change sequence, so assume that
514 * the module powers up in the indicated power mode.
515 */
516 if (power > sfp->max_power_mW) {
517 dev_err(sfp->dev,
518 "Host does not support %u.%uW modules\n",
519 power / 1000, (power / 100) % 10);
520 return -EINVAL;
521 }
522 return 0;
523 }
524
525 if (power > sfp->max_power_mW) {
526 dev_warn(sfp->dev,
527 "Host does not support %u.%uW modules, module left in power mode 1\n",
528 power / 1000, (power / 100) % 10);
529 return 0;
530 }
531
532 if (power <= 1000)
533 return 0;
534
535 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
536 if (err != sizeof(val)) {
537 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
538 err = -EAGAIN;
539 goto err;
540 }
541
542 val |= BIT(0);
543
544 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
545 if (err != sizeof(val)) {
546 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
547 err = -EAGAIN;
548 goto err;
549 }
550
551 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
552 power / 1000, (power / 100) % 10);
553 return T_HPOWER_LEVEL;
554
555 err:
556 return err;
557 }
558
559 static int sfp_sm_mod_probe(struct sfp *sfp)
560 {
561 /* SFP module inserted - read I2C data */
562 struct sfp_eeprom_id id;
563 u8 check;
564 int ret;
565
566 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
567 if (ret < 0) {
568 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
569 return -EAGAIN;
570 }
571
572 if (ret != sizeof(id)) {
573 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
574 return -EAGAIN;
575 }
576
577 /* Validate the checksum over the base structure */
578 check = sfp_check(&id.base, sizeof(id.base) - 1);
579 if (check != id.base.cc_base) {
580 dev_err(sfp->dev,
581 "EEPROM base structure checksum failure: 0x%02x\n",
582 check);
583 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
584 16, 1, &id, sizeof(id.base) - 1, true);
585 return -EINVAL;
586 }
587
588 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
589 if (check != id.ext.cc_ext) {
590 dev_err(sfp->dev,
591 "EEPROM extended structure checksum failure: 0x%02x\n",
592 check);
593 memset(&id.ext, 0, sizeof(id.ext));
594 }
595
596 sfp->id = id;
597
598 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
599 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
600 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
601 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
602 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
603 (int)sizeof(id.ext.datecode), id.ext.datecode);
604
605 /* Check whether we support this module */
606 if (!sfp->type->module_supported(&sfp->id)) {
607 dev_err(sfp->dev,
608 "module is not supported - phys id 0x%02x 0x%02x\n",
609 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
610 return -EINVAL;
611 }
612
613 /* If the module requires address swap mode, warn about it */
614 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
615 dev_warn(sfp->dev,
616 "module address swap to access page 0xA2 is not supported.\n");
617
618 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
619 if (ret < 0)
620 return ret;
621
622 return sfp_sm_mod_hpower(sfp);
623 }
624
625 static void sfp_sm_mod_remove(struct sfp *sfp)
626 {
627 sfp_module_remove(sfp->sfp_bus);
628
629 if (sfp->mod_phy)
630 sfp_sm_phy_detach(sfp);
631
632 sfp_module_tx_disable(sfp);
633
634 memset(&sfp->id, 0, sizeof(sfp->id));
635
636 dev_info(sfp->dev, "module removed\n");
637 }
638
639 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
640 {
641 mutex_lock(&sfp->sm_mutex);
642
643 dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
644 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
645
646 /* This state machine tracks the insert/remove state of
647 * the module, and handles probing the on-board EEPROM.
648 */
649 switch (sfp->sm_mod_state) {
650 default:
651 if (event == SFP_E_INSERT) {
652 sfp_module_tx_disable(sfp);
653 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
654 }
655 break;
656
657 case SFP_MOD_PROBE:
658 if (event == SFP_E_REMOVE) {
659 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
660 } else if (event == SFP_E_TIMEOUT) {
661 int val = sfp_sm_mod_probe(sfp);
662
663 if (val == 0)
664 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
665 else if (val > 0)
666 sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
667 else if (val != -EAGAIN)
668 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
669 else
670 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
671 }
672 break;
673
674 case SFP_MOD_HPOWER:
675 if (event == SFP_E_TIMEOUT) {
676 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
677 break;
678 }
679 /* fallthrough */
680 case SFP_MOD_PRESENT:
681 case SFP_MOD_ERROR:
682 if (event == SFP_E_REMOVE) {
683 sfp_sm_mod_remove(sfp);
684 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
685 }
686 break;
687 }
688
689 /* This state machine tracks the netdev up/down state */
690 switch (sfp->sm_dev_state) {
691 default:
692 if (event == SFP_E_DEV_UP)
693 sfp->sm_dev_state = SFP_DEV_UP;
694 break;
695
696 case SFP_DEV_UP:
697 if (event == SFP_E_DEV_DOWN) {
698 /* If the module has a PHY, avoid raising TX disable
699 * as this resets the PHY. Otherwise, raise it to
700 * turn the laser off.
701 */
702 if (!sfp->mod_phy)
703 sfp_module_tx_disable(sfp);
704 sfp->sm_dev_state = SFP_DEV_DOWN;
705 }
706 break;
707 }
708
709 /* Some events are global */
710 if (sfp->sm_state != SFP_S_DOWN &&
711 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
712 sfp->sm_dev_state != SFP_DEV_UP)) {
713 if (sfp->sm_state == SFP_S_LINK_UP &&
714 sfp->sm_dev_state == SFP_DEV_UP)
715 sfp_sm_link_down(sfp);
716 if (sfp->mod_phy)
717 sfp_sm_phy_detach(sfp);
718 sfp_sm_next(sfp, SFP_S_DOWN, 0);
719 mutex_unlock(&sfp->sm_mutex);
720 return;
721 }
722
723 /* The main state machine */
724 switch (sfp->sm_state) {
725 case SFP_S_DOWN:
726 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
727 sfp->sm_dev_state == SFP_DEV_UP)
728 sfp_sm_mod_init(sfp);
729 break;
730
731 case SFP_S_INIT:
732 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
733 sfp_sm_fault(sfp, true);
734 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
735 sfp_sm_link_check_los(sfp);
736 break;
737
738 case SFP_S_WAIT_LOS:
739 if (event == SFP_E_TX_FAULT)
740 sfp_sm_fault(sfp, true);
741 else if (sfp_los_event_inactive(sfp, event))
742 sfp_sm_link_up(sfp);
743 break;
744
745 case SFP_S_LINK_UP:
746 if (event == SFP_E_TX_FAULT) {
747 sfp_sm_link_down(sfp);
748 sfp_sm_fault(sfp, true);
749 } else if (sfp_los_event_active(sfp, event)) {
750 sfp_sm_link_down(sfp);
751 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
752 }
753 break;
754
755 case SFP_S_TX_FAULT:
756 if (event == SFP_E_TIMEOUT) {
757 sfp_module_tx_fault_reset(sfp);
758 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
759 }
760 break;
761
762 case SFP_S_REINIT:
763 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
764 sfp_sm_fault(sfp, false);
765 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
766 dev_info(sfp->dev, "module transmit fault recovered\n");
767 sfp_sm_link_check_los(sfp);
768 }
769 break;
770
771 case SFP_S_TX_DISABLE:
772 break;
773 }
774
775 dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
776 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
777
778 mutex_unlock(&sfp->sm_mutex);
779 }
780
781 static void sfp_start(struct sfp *sfp)
782 {
783 sfp_sm_event(sfp, SFP_E_DEV_UP);
784 }
785
786 static void sfp_stop(struct sfp *sfp)
787 {
788 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
789 }
790
791 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
792 {
793 /* locking... and check module is present */
794
795 if (sfp->id.ext.sff8472_compliance &&
796 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
797 modinfo->type = ETH_MODULE_SFF_8472;
798 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
799 } else {
800 modinfo->type = ETH_MODULE_SFF_8079;
801 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
802 }
803 return 0;
804 }
805
806 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
807 u8 *data)
808 {
809 unsigned int first, last, len;
810 int ret;
811
812 if (ee->len == 0)
813 return -EINVAL;
814
815 first = ee->offset;
816 last = ee->offset + ee->len;
817 if (first < ETH_MODULE_SFF_8079_LEN) {
818 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
819 len -= first;
820
821 ret = sfp_read(sfp, false, first, data, len);
822 if (ret < 0)
823 return ret;
824
825 first += len;
826 data += len;
827 }
828 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
829 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
830 len -= first;
831 first -= ETH_MODULE_SFF_8079_LEN;
832
833 ret = sfp_read(sfp, true, first, data, len);
834 if (ret < 0)
835 return ret;
836 }
837 return 0;
838 }
839
840 static const struct sfp_socket_ops sfp_module_ops = {
841 .start = sfp_start,
842 .stop = sfp_stop,
843 .module_info = sfp_module_info,
844 .module_eeprom = sfp_module_eeprom,
845 };
846
847 static void sfp_timeout(struct work_struct *work)
848 {
849 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
850
851 rtnl_lock();
852 sfp_sm_event(sfp, SFP_E_TIMEOUT);
853 rtnl_unlock();
854 }
855
856 static void sfp_check_state(struct sfp *sfp)
857 {
858 unsigned int state, i, changed;
859
860 state = sfp_get_state(sfp);
861 changed = state ^ sfp->state;
862 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
863
864 for (i = 0; i < GPIO_MAX; i++)
865 if (changed & BIT(i))
866 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
867 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
868
869 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
870 sfp->state = state;
871
872 rtnl_lock();
873 if (changed & SFP_F_PRESENT)
874 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
875 SFP_E_INSERT : SFP_E_REMOVE);
876
877 if (changed & SFP_F_TX_FAULT)
878 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
879 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
880
881 if (changed & SFP_F_LOS)
882 sfp_sm_event(sfp, state & SFP_F_LOS ?
883 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
884 rtnl_unlock();
885 }
886
887 static irqreturn_t sfp_irq(int irq, void *data)
888 {
889 struct sfp *sfp = data;
890
891 sfp_check_state(sfp);
892
893 return IRQ_HANDLED;
894 }
895
896 static void sfp_poll(struct work_struct *work)
897 {
898 struct sfp *sfp = container_of(work, struct sfp, poll.work);
899
900 sfp_check_state(sfp);
901 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
902 }
903
904 static struct sfp *sfp_alloc(struct device *dev)
905 {
906 struct sfp *sfp;
907
908 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
909 if (!sfp)
910 return ERR_PTR(-ENOMEM);
911
912 sfp->dev = dev;
913
914 mutex_init(&sfp->sm_mutex);
915 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
916 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
917
918 return sfp;
919 }
920
921 static void sfp_cleanup(void *data)
922 {
923 struct sfp *sfp = data;
924
925 cancel_delayed_work_sync(&sfp->poll);
926 cancel_delayed_work_sync(&sfp->timeout);
927 if (sfp->i2c_mii) {
928 mdiobus_unregister(sfp->i2c_mii);
929 mdiobus_free(sfp->i2c_mii);
930 }
931 if (sfp->i2c)
932 i2c_put_adapter(sfp->i2c);
933 kfree(sfp);
934 }
935
936 static int sfp_probe(struct platform_device *pdev)
937 {
938 const struct sff_data *sff;
939 struct sfp *sfp;
940 bool poll = false;
941 int irq, err, i;
942
943 sfp = sfp_alloc(&pdev->dev);
944 if (IS_ERR(sfp))
945 return PTR_ERR(sfp);
946
947 platform_set_drvdata(pdev, sfp);
948
949 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
950 if (err < 0)
951 return err;
952
953 sff = sfp->type = &sfp_data;
954
955 if (pdev->dev.of_node) {
956 struct device_node *node = pdev->dev.of_node;
957 const struct of_device_id *id;
958 struct device_node *np;
959
960 id = of_match_node(sfp_of_match, node);
961 if (WARN_ON(!id))
962 return -EINVAL;
963
964 sff = sfp->type = id->data;
965
966 np = of_parse_phandle(node, "i2c-bus", 0);
967 if (np) {
968 struct i2c_adapter *i2c;
969
970 i2c = of_find_i2c_adapter_by_node(np);
971 of_node_put(np);
972 if (!i2c)
973 return -EPROBE_DEFER;
974
975 err = sfp_i2c_configure(sfp, i2c);
976 if (err < 0) {
977 i2c_put_adapter(i2c);
978 return err;
979 }
980 }
981 }
982
983 for (i = 0; i < GPIO_MAX; i++)
984 if (sff->gpios & BIT(i)) {
985 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
986 gpio_of_names[i], gpio_flags[i]);
987 if (IS_ERR(sfp->gpio[i]))
988 return PTR_ERR(sfp->gpio[i]);
989 }
990
991 sfp->get_state = sfp_gpio_get_state;
992 sfp->set_state = sfp_gpio_set_state;
993
994 /* Modules that have no detect signal are always present */
995 if (!(sfp->gpio[GPIO_MODDEF0]))
996 sfp->get_state = sff_gpio_get_state;
997
998 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
999 &sfp->max_power_mW);
1000 if (!sfp->max_power_mW)
1001 sfp->max_power_mW = 1000;
1002
1003 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1004 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1005
1006 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
1007 if (!sfp->sfp_bus)
1008 return -ENOMEM;
1009
1010 /* Get the initial state, and always signal TX disable,
1011 * since the network interface will not be up.
1012 */
1013 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
1014
1015 if (sfp->gpio[GPIO_RATE_SELECT] &&
1016 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1017 sfp->state |= SFP_F_RATE_SELECT;
1018 sfp_set_state(sfp, sfp->state);
1019 sfp_module_tx_disable(sfp);
1020 rtnl_lock();
1021 if (sfp->state & SFP_F_PRESENT)
1022 sfp_sm_event(sfp, SFP_E_INSERT);
1023 rtnl_unlock();
1024
1025 for (i = 0; i < GPIO_MAX; i++) {
1026 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1027 continue;
1028
1029 irq = gpiod_to_irq(sfp->gpio[i]);
1030 if (!irq) {
1031 poll = true;
1032 continue;
1033 }
1034
1035 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1036 IRQF_ONESHOT |
1037 IRQF_TRIGGER_RISING |
1038 IRQF_TRIGGER_FALLING,
1039 dev_name(sfp->dev), sfp);
1040 if (err)
1041 poll = true;
1042 }
1043
1044 if (poll)
1045 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1046
1047 return 0;
1048 }
1049
1050 static int sfp_remove(struct platform_device *pdev)
1051 {
1052 struct sfp *sfp = platform_get_drvdata(pdev);
1053
1054 sfp_unregister_socket(sfp->sfp_bus);
1055
1056 return 0;
1057 }
1058
1059 static struct platform_driver sfp_driver = {
1060 .probe = sfp_probe,
1061 .remove = sfp_remove,
1062 .driver = {
1063 .name = "sfp",
1064 .of_match_table = sfp_of_match,
1065 },
1066 };
1067
1068 static int sfp_init(void)
1069 {
1070 poll_jiffies = msecs_to_jiffies(100);
1071
1072 return platform_driver_register(&sfp_driver);
1073 }
1074 module_init(sfp_init);
1075
1076 static void sfp_exit(void)
1077 {
1078 platform_driver_unregister(&sfp_driver);
1079 }
1080 module_exit(sfp_exit);
1081
1082 MODULE_ALIAS("platform:sfp");
1083 MODULE_AUTHOR("Russell King");
1084 MODULE_LICENSE("GPL v2");