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