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