1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/ctype.h>
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/hwmon.h>
7 #include <linux/i2c.h>
8 #include <linux/interrupt.h>
9 #include <linux/jiffies.h>
10 #include <linux/mdio/mdio-i2c.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/of.h>
14 #include <linux/phy.h>
15 #include <linux/platform_device.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19
20 #include "sfp.h"
21 #include "swphy.h"
22
23 enum {
24 GPIO_MODDEF0,
25 GPIO_LOS,
26 GPIO_TX_FAULT,
27 GPIO_TX_DISABLE,
28 GPIO_RATE_SELECT,
29 GPIO_MAX,
30
31 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
32 SFP_F_LOS = BIT(GPIO_LOS),
33 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
34 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
35 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
36
37 SFP_E_INSERT = 0,
38 SFP_E_REMOVE,
39 SFP_E_DEV_ATTACH,
40 SFP_E_DEV_DETACH,
41 SFP_E_DEV_DOWN,
42 SFP_E_DEV_UP,
43 SFP_E_TX_FAULT,
44 SFP_E_TX_CLEAR,
45 SFP_E_LOS_HIGH,
46 SFP_E_LOS_LOW,
47 SFP_E_TIMEOUT,
48
49 SFP_MOD_EMPTY = 0,
50 SFP_MOD_ERROR,
51 SFP_MOD_PROBE,
52 SFP_MOD_WAITDEV,
53 SFP_MOD_HPOWER,
54 SFP_MOD_WAITPWR,
55 SFP_MOD_PRESENT,
56
57 SFP_DEV_DETACHED = 0,
58 SFP_DEV_DOWN,
59 SFP_DEV_UP,
60
61 SFP_S_DOWN = 0,
62 SFP_S_FAIL,
63 SFP_S_WAIT,
64 SFP_S_INIT,
65 SFP_S_INIT_PHY,
66 SFP_S_INIT_TX_FAULT,
67 SFP_S_WAIT_LOS,
68 SFP_S_LINK_UP,
69 SFP_S_TX_FAULT,
70 SFP_S_REINIT,
71 SFP_S_TX_DISABLE,
72 };
73
74 static const char * const mod_state_strings[] = {
75 [SFP_MOD_EMPTY] = "empty",
76 [SFP_MOD_ERROR] = "error",
77 [SFP_MOD_PROBE] = "probe",
78 [SFP_MOD_WAITDEV] = "waitdev",
79 [SFP_MOD_HPOWER] = "hpower",
80 [SFP_MOD_WAITPWR] = "waitpwr",
81 [SFP_MOD_PRESENT] = "present",
82 };
83
mod_state_to_str(unsigned short mod_state)84 static const char *mod_state_to_str(unsigned short mod_state)
85 {
86 if (mod_state >= ARRAY_SIZE(mod_state_strings))
87 return "Unknown module state";
88 return mod_state_strings[mod_state];
89 }
90
91 static const char * const dev_state_strings[] = {
92 [SFP_DEV_DETACHED] = "detached",
93 [SFP_DEV_DOWN] = "down",
94 [SFP_DEV_UP] = "up",
95 };
96
dev_state_to_str(unsigned short dev_state)97 static const char *dev_state_to_str(unsigned short dev_state)
98 {
99 if (dev_state >= ARRAY_SIZE(dev_state_strings))
100 return "Unknown device state";
101 return dev_state_strings[dev_state];
102 }
103
104 static const char * const event_strings[] = {
105 [SFP_E_INSERT] = "insert",
106 [SFP_E_REMOVE] = "remove",
107 [SFP_E_DEV_ATTACH] = "dev_attach",
108 [SFP_E_DEV_DETACH] = "dev_detach",
109 [SFP_E_DEV_DOWN] = "dev_down",
110 [SFP_E_DEV_UP] = "dev_up",
111 [SFP_E_TX_FAULT] = "tx_fault",
112 [SFP_E_TX_CLEAR] = "tx_clear",
113 [SFP_E_LOS_HIGH] = "los_high",
114 [SFP_E_LOS_LOW] = "los_low",
115 [SFP_E_TIMEOUT] = "timeout",
116 };
117
event_to_str(unsigned short event)118 static const char *event_to_str(unsigned short event)
119 {
120 if (event >= ARRAY_SIZE(event_strings))
121 return "Unknown event";
122 return event_strings[event];
123 }
124
125 static const char * const sm_state_strings[] = {
126 [SFP_S_DOWN] = "down",
127 [SFP_S_FAIL] = "fail",
128 [SFP_S_WAIT] = "wait",
129 [SFP_S_INIT] = "init",
130 [SFP_S_INIT_PHY] = "init_phy",
131 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
132 [SFP_S_WAIT_LOS] = "wait_los",
133 [SFP_S_LINK_UP] = "link_up",
134 [SFP_S_TX_FAULT] = "tx_fault",
135 [SFP_S_REINIT] = "reinit",
136 [SFP_S_TX_DISABLE] = "tx_disable",
137 };
138
sm_state_to_str(unsigned short sm_state)139 static const char *sm_state_to_str(unsigned short sm_state)
140 {
141 if (sm_state >= ARRAY_SIZE(sm_state_strings))
142 return "Unknown state";
143 return sm_state_strings[sm_state];
144 }
145
146 static const char *gpio_of_names[] = {
147 "mod-def0",
148 "los",
149 "tx-fault",
150 "tx-disable",
151 "rate-select0",
152 };
153
154 static const enum gpiod_flags gpio_flags[] = {
155 GPIOD_IN,
156 GPIOD_IN,
157 GPIOD_IN,
158 GPIOD_ASIS,
159 GPIOD_ASIS,
160 };
161
162 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a
163 * non-cooled module to initialise its laser safety circuitry. We wait
164 * an initial T_WAIT period before we check the tx fault to give any PHY
165 * on board (for a copper SFP) time to initialise.
166 */
167 #define T_WAIT msecs_to_jiffies(50)
168 #define T_START_UP msecs_to_jiffies(300)
169 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
170
171 /* t_reset is the time required to assert the TX_DISABLE signal to reset
172 * an indicated TX_FAULT.
173 */
174 #define T_RESET_US 10
175 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
176
177 /* N_FAULT_INIT is the number of recovery attempts at module initialisation
178 * time. If the TX_FAULT signal is not deasserted after this number of
179 * attempts at clearing it, we decide that the module is faulty.
180 * N_FAULT is the same but after the module has initialised.
181 */
182 #define N_FAULT_INIT 5
183 #define N_FAULT 5
184
185 /* T_PHY_RETRY is the time interval between attempts to probe the PHY.
186 * R_PHY_RETRY is the number of attempts.
187 */
188 #define T_PHY_RETRY msecs_to_jiffies(50)
189 #define R_PHY_RETRY 12
190
191 /* SFP module presence detection is poor: the three MOD DEF signals are
192 * the same length on the PCB, which means it's possible for MOD DEF 0 to
193 * connect before the I2C bus on MOD DEF 1/2.
194 *
195 * The SFF-8472 specifies t_serial ("Time from power on until module is
196 * ready for data transmission over the two wire serial bus.") as 300ms.
197 */
198 #define T_SERIAL msecs_to_jiffies(300)
199 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
200 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
201 #define R_PROBE_RETRY_INIT 10
202 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
203 #define R_PROBE_RETRY_SLOW 12
204
205 /* SFP modules appear to always have their PHY configured for bus address
206 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
207 */
208 #define SFP_PHY_ADDR 22
209
210 struct sff_data {
211 unsigned int gpios;
212 bool (*module_supported)(const struct sfp_eeprom_id *id);
213 };
214
215 struct sfp {
216 struct device *dev;
217 struct i2c_adapter *i2c;
218 struct mii_bus *i2c_mii;
219 struct sfp_bus *sfp_bus;
220 struct phy_device *mod_phy;
221 const struct sff_data *type;
222 size_t i2c_block_size;
223 u32 max_power_mW;
224
225 unsigned int (*get_state)(struct sfp *);
226 void (*set_state)(struct sfp *, unsigned int);
227 int (*read)(struct sfp *, bool, u8, void *, size_t);
228 int (*write)(struct sfp *, bool, u8, void *, size_t);
229
230 struct gpio_desc *gpio[GPIO_MAX];
231 int gpio_irq[GPIO_MAX];
232
233 bool need_poll;
234
235 struct mutex st_mutex; /* Protects state */
236 unsigned int state_soft_mask;
237 unsigned int state;
238 struct delayed_work poll;
239 struct delayed_work timeout;
240 struct mutex sm_mutex; /* Protects state machine */
241 unsigned char sm_mod_state;
242 unsigned char sm_mod_tries_init;
243 unsigned char sm_mod_tries;
244 unsigned char sm_dev_state;
245 unsigned short sm_state;
246 unsigned char sm_fault_retries;
247 unsigned char sm_phy_retries;
248
249 struct sfp_eeprom_id id;
250 unsigned int module_power_mW;
251 unsigned int module_t_start_up;
252
253 #if IS_ENABLED(CONFIG_HWMON)
254 struct sfp_diag diag;
255 struct delayed_work hwmon_probe;
256 unsigned int hwmon_tries;
257 struct device *hwmon_dev;
258 char *hwmon_name;
259 #endif
260
261 };
262
sff_module_supported(const struct sfp_eeprom_id * id)263 static bool sff_module_supported(const struct sfp_eeprom_id *id)
264 {
265 return id->base.phys_id == SFF8024_ID_SFF_8472 &&
266 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
267 }
268
269 static const struct sff_data sff_data = {
270 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
271 .module_supported = sff_module_supported,
272 };
273
sfp_module_supported(const struct sfp_eeprom_id * id)274 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
275 {
276 if (id->base.phys_id == SFF8024_ID_SFP &&
277 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP)
278 return true;
279
280 /* SFP GPON module Ubiquiti U-Fiber Instant has in its EEPROM stored
281 * phys id SFF instead of SFP. Therefore mark this module explicitly
282 * as supported based on vendor name and pn match.
283 */
284 if (id->base.phys_id == SFF8024_ID_SFF_8472 &&
285 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP &&
286 !memcmp(id->base.vendor_name, "UBNT ", 16) &&
287 !memcmp(id->base.vendor_pn, "UF-INSTANT ", 16))
288 return true;
289
290 return false;
291 }
292
293 static const struct sff_data sfp_data = {
294 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
295 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
296 .module_supported = sfp_module_supported,
297 };
298
299 static const struct of_device_id sfp_of_match[] = {
300 { .compatible = "sff,sff", .data = &sff_data, },
301 { .compatible = "sff,sfp", .data = &sfp_data, },
302 { },
303 };
304 MODULE_DEVICE_TABLE(of, sfp_of_match);
305
306 static unsigned long poll_jiffies;
307
sfp_gpio_get_state(struct sfp * sfp)308 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
309 {
310 unsigned int i, state, v;
311
312 for (i = state = 0; i < GPIO_MAX; i++) {
313 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
314 continue;
315
316 v = gpiod_get_value_cansleep(sfp->gpio[i]);
317 if (v)
318 state |= BIT(i);
319 }
320
321 return state;
322 }
323
sff_gpio_get_state(struct sfp * sfp)324 static unsigned int sff_gpio_get_state(struct sfp *sfp)
325 {
326 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
327 }
328
sfp_gpio_set_state(struct sfp * sfp,unsigned int state)329 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
330 {
331 if (state & SFP_F_PRESENT) {
332 /* If the module is present, drive the signals */
333 if (sfp->gpio[GPIO_TX_DISABLE])
334 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
335 state & SFP_F_TX_DISABLE);
336 if (state & SFP_F_RATE_SELECT)
337 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
338 state & SFP_F_RATE_SELECT);
339 } else {
340 /* Otherwise, let them float to the pull-ups */
341 if (sfp->gpio[GPIO_TX_DISABLE])
342 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
343 if (state & SFP_F_RATE_SELECT)
344 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
345 }
346 }
347
sfp_i2c_read(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)348 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
349 size_t len)
350 {
351 struct i2c_msg msgs[2];
352 u8 bus_addr = a2 ? 0x51 : 0x50;
353 size_t block_size = sfp->i2c_block_size;
354 size_t this_len;
355 int ret;
356
357 msgs[0].addr = bus_addr;
358 msgs[0].flags = 0;
359 msgs[0].len = 1;
360 msgs[0].buf = &dev_addr;
361 msgs[1].addr = bus_addr;
362 msgs[1].flags = I2C_M_RD;
363 msgs[1].len = len;
364 msgs[1].buf = buf;
365
366 while (len) {
367 this_len = len;
368 if (this_len > block_size)
369 this_len = block_size;
370
371 msgs[1].len = this_len;
372
373 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
374 if (ret < 0)
375 return ret;
376
377 if (ret != ARRAY_SIZE(msgs))
378 break;
379
380 msgs[1].buf += this_len;
381 dev_addr += this_len;
382 len -= this_len;
383 }
384
385 return msgs[1].buf - (u8 *)buf;
386 }
387
sfp_i2c_write(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)388 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
389 size_t len)
390 {
391 struct i2c_msg msgs[1];
392 u8 bus_addr = a2 ? 0x51 : 0x50;
393 int ret;
394
395 msgs[0].addr = bus_addr;
396 msgs[0].flags = 0;
397 msgs[0].len = 1 + len;
398 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
399 if (!msgs[0].buf)
400 return -ENOMEM;
401
402 msgs[0].buf[0] = dev_addr;
403 memcpy(&msgs[0].buf[1], buf, len);
404
405 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
406
407 kfree(msgs[0].buf);
408
409 if (ret < 0)
410 return ret;
411
412 return ret == ARRAY_SIZE(msgs) ? len : 0;
413 }
414
sfp_i2c_configure(struct sfp * sfp,struct i2c_adapter * i2c)415 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
416 {
417 struct mii_bus *i2c_mii;
418 int ret;
419
420 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
421 return -EINVAL;
422
423 sfp->i2c = i2c;
424 sfp->read = sfp_i2c_read;
425 sfp->write = sfp_i2c_write;
426
427 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
428 if (IS_ERR(i2c_mii))
429 return PTR_ERR(i2c_mii);
430
431 i2c_mii->name = "SFP I2C Bus";
432 i2c_mii->phy_mask = ~0;
433
434 ret = mdiobus_register(i2c_mii);
435 if (ret < 0) {
436 mdiobus_free(i2c_mii);
437 return ret;
438 }
439
440 sfp->i2c_mii = i2c_mii;
441
442 return 0;
443 }
444
445 /* Interface */
sfp_read(struct sfp * sfp,bool a2,u8 addr,void * buf,size_t len)446 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
447 {
448 return sfp->read(sfp, a2, addr, buf, len);
449 }
450
sfp_write(struct sfp * sfp,bool a2,u8 addr,void * buf,size_t len)451 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
452 {
453 return sfp->write(sfp, a2, addr, buf, len);
454 }
455
sfp_soft_get_state(struct sfp * sfp)456 static unsigned int sfp_soft_get_state(struct sfp *sfp)
457 {
458 unsigned int state = 0;
459 u8 status;
460 int ret;
461
462 ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status));
463 if (ret == sizeof(status)) {
464 if (status & SFP_STATUS_RX_LOS)
465 state |= SFP_F_LOS;
466 if (status & SFP_STATUS_TX_FAULT)
467 state |= SFP_F_TX_FAULT;
468 } else {
469 dev_err_ratelimited(sfp->dev,
470 "failed to read SFP soft status: %d\n",
471 ret);
472 /* Preserve the current state */
473 state = sfp->state;
474 }
475
476 return state & sfp->state_soft_mask;
477 }
478
sfp_soft_set_state(struct sfp * sfp,unsigned int state)479 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state)
480 {
481 u8 status;
482
483 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
484 sizeof(status)) {
485 if (state & SFP_F_TX_DISABLE)
486 status |= SFP_STATUS_TX_DISABLE_FORCE;
487 else
488 status &= ~SFP_STATUS_TX_DISABLE_FORCE;
489
490 sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status));
491 }
492 }
493
sfp_soft_start_poll(struct sfp * sfp)494 static void sfp_soft_start_poll(struct sfp *sfp)
495 {
496 const struct sfp_eeprom_id *id = &sfp->id;
497
498 sfp->state_soft_mask = 0;
499 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
500 !sfp->gpio[GPIO_TX_DISABLE])
501 sfp->state_soft_mask |= SFP_F_TX_DISABLE;
502 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
503 !sfp->gpio[GPIO_TX_FAULT])
504 sfp->state_soft_mask |= SFP_F_TX_FAULT;
505 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
506 !sfp->gpio[GPIO_LOS])
507 sfp->state_soft_mask |= SFP_F_LOS;
508
509 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
510 !sfp->need_poll)
511 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
512 }
513
sfp_soft_stop_poll(struct sfp * sfp)514 static void sfp_soft_stop_poll(struct sfp *sfp)
515 {
516 sfp->state_soft_mask = 0;
517 }
518
sfp_get_state(struct sfp * sfp)519 static unsigned int sfp_get_state(struct sfp *sfp)
520 {
521 unsigned int state = sfp->get_state(sfp);
522
523 if (state & SFP_F_PRESENT &&
524 sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
525 state |= sfp_soft_get_state(sfp);
526
527 return state;
528 }
529
sfp_set_state(struct sfp * sfp,unsigned int state)530 static void sfp_set_state(struct sfp *sfp, unsigned int state)
531 {
532 sfp->set_state(sfp, state);
533
534 if (state & SFP_F_PRESENT &&
535 sfp->state_soft_mask & SFP_F_TX_DISABLE)
536 sfp_soft_set_state(sfp, state);
537 }
538
sfp_check(void * buf,size_t len)539 static unsigned int sfp_check(void *buf, size_t len)
540 {
541 u8 *p, check;
542
543 for (p = buf, check = 0; len; p++, len--)
544 check += *p;
545
546 return check;
547 }
548
549 /* hwmon */
550 #if IS_ENABLED(CONFIG_HWMON)
sfp_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)551 static umode_t sfp_hwmon_is_visible(const void *data,
552 enum hwmon_sensor_types type,
553 u32 attr, int channel)
554 {
555 const struct sfp *sfp = data;
556
557 switch (type) {
558 case hwmon_temp:
559 switch (attr) {
560 case hwmon_temp_min_alarm:
561 case hwmon_temp_max_alarm:
562 case hwmon_temp_lcrit_alarm:
563 case hwmon_temp_crit_alarm:
564 case hwmon_temp_min:
565 case hwmon_temp_max:
566 case hwmon_temp_lcrit:
567 case hwmon_temp_crit:
568 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
569 return 0;
570 fallthrough;
571 case hwmon_temp_input:
572 case hwmon_temp_label:
573 return 0444;
574 default:
575 return 0;
576 }
577 case hwmon_in:
578 switch (attr) {
579 case hwmon_in_min_alarm:
580 case hwmon_in_max_alarm:
581 case hwmon_in_lcrit_alarm:
582 case hwmon_in_crit_alarm:
583 case hwmon_in_min:
584 case hwmon_in_max:
585 case hwmon_in_lcrit:
586 case hwmon_in_crit:
587 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
588 return 0;
589 fallthrough;
590 case hwmon_in_input:
591 case hwmon_in_label:
592 return 0444;
593 default:
594 return 0;
595 }
596 case hwmon_curr:
597 switch (attr) {
598 case hwmon_curr_min_alarm:
599 case hwmon_curr_max_alarm:
600 case hwmon_curr_lcrit_alarm:
601 case hwmon_curr_crit_alarm:
602 case hwmon_curr_min:
603 case hwmon_curr_max:
604 case hwmon_curr_lcrit:
605 case hwmon_curr_crit:
606 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
607 return 0;
608 fallthrough;
609 case hwmon_curr_input:
610 case hwmon_curr_label:
611 return 0444;
612 default:
613 return 0;
614 }
615 case hwmon_power:
616 /* External calibration of receive power requires
617 * floating point arithmetic. Doing that in the kernel
618 * is not easy, so just skip it. If the module does
619 * not require external calibration, we can however
620 * show receiver power, since FP is then not needed.
621 */
622 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
623 channel == 1)
624 return 0;
625 switch (attr) {
626 case hwmon_power_min_alarm:
627 case hwmon_power_max_alarm:
628 case hwmon_power_lcrit_alarm:
629 case hwmon_power_crit_alarm:
630 case hwmon_power_min:
631 case hwmon_power_max:
632 case hwmon_power_lcrit:
633 case hwmon_power_crit:
634 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
635 return 0;
636 fallthrough;
637 case hwmon_power_input:
638 case hwmon_power_label:
639 return 0444;
640 default:
641 return 0;
642 }
643 default:
644 return 0;
645 }
646 }
647
sfp_hwmon_read_sensor(struct sfp * sfp,int reg,long * value)648 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
649 {
650 __be16 val;
651 int err;
652
653 err = sfp_read(sfp, true, reg, &val, sizeof(val));
654 if (err < 0)
655 return err;
656
657 *value = be16_to_cpu(val);
658
659 return 0;
660 }
661
sfp_hwmon_to_rx_power(long * value)662 static void sfp_hwmon_to_rx_power(long *value)
663 {
664 *value = DIV_ROUND_CLOSEST(*value, 10);
665 }
666
sfp_hwmon_calibrate(struct sfp * sfp,unsigned int slope,int offset,long * value)667 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
668 long *value)
669 {
670 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
671 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
672 }
673
sfp_hwmon_calibrate_temp(struct sfp * sfp,long * value)674 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
675 {
676 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
677 be16_to_cpu(sfp->diag.cal_t_offset), value);
678
679 if (*value >= 0x8000)
680 *value -= 0x10000;
681
682 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
683 }
684
sfp_hwmon_calibrate_vcc(struct sfp * sfp,long * value)685 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
686 {
687 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
688 be16_to_cpu(sfp->diag.cal_v_offset), value);
689
690 *value = DIV_ROUND_CLOSEST(*value, 10);
691 }
692
sfp_hwmon_calibrate_bias(struct sfp * sfp,long * value)693 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
694 {
695 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
696 be16_to_cpu(sfp->diag.cal_txi_offset), value);
697
698 *value = DIV_ROUND_CLOSEST(*value, 500);
699 }
700
sfp_hwmon_calibrate_tx_power(struct sfp * sfp,long * value)701 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
702 {
703 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
704 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
705
706 *value = DIV_ROUND_CLOSEST(*value, 10);
707 }
708
sfp_hwmon_read_temp(struct sfp * sfp,int reg,long * value)709 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
710 {
711 int err;
712
713 err = sfp_hwmon_read_sensor(sfp, reg, value);
714 if (err < 0)
715 return err;
716
717 sfp_hwmon_calibrate_temp(sfp, value);
718
719 return 0;
720 }
721
sfp_hwmon_read_vcc(struct sfp * sfp,int reg,long * value)722 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
723 {
724 int err;
725
726 err = sfp_hwmon_read_sensor(sfp, reg, value);
727 if (err < 0)
728 return err;
729
730 sfp_hwmon_calibrate_vcc(sfp, value);
731
732 return 0;
733 }
734
sfp_hwmon_read_bias(struct sfp * sfp,int reg,long * value)735 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
736 {
737 int err;
738
739 err = sfp_hwmon_read_sensor(sfp, reg, value);
740 if (err < 0)
741 return err;
742
743 sfp_hwmon_calibrate_bias(sfp, value);
744
745 return 0;
746 }
747
sfp_hwmon_read_tx_power(struct sfp * sfp,int reg,long * value)748 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
749 {
750 int err;
751
752 err = sfp_hwmon_read_sensor(sfp, reg, value);
753 if (err < 0)
754 return err;
755
756 sfp_hwmon_calibrate_tx_power(sfp, value);
757
758 return 0;
759 }
760
sfp_hwmon_read_rx_power(struct sfp * sfp,int reg,long * value)761 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
762 {
763 int err;
764
765 err = sfp_hwmon_read_sensor(sfp, reg, value);
766 if (err < 0)
767 return err;
768
769 sfp_hwmon_to_rx_power(value);
770
771 return 0;
772 }
773
sfp_hwmon_temp(struct sfp * sfp,u32 attr,long * value)774 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
775 {
776 u8 status;
777 int err;
778
779 switch (attr) {
780 case hwmon_temp_input:
781 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
782
783 case hwmon_temp_lcrit:
784 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
785 sfp_hwmon_calibrate_temp(sfp, value);
786 return 0;
787
788 case hwmon_temp_min:
789 *value = be16_to_cpu(sfp->diag.temp_low_warn);
790 sfp_hwmon_calibrate_temp(sfp, value);
791 return 0;
792 case hwmon_temp_max:
793 *value = be16_to_cpu(sfp->diag.temp_high_warn);
794 sfp_hwmon_calibrate_temp(sfp, value);
795 return 0;
796
797 case hwmon_temp_crit:
798 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
799 sfp_hwmon_calibrate_temp(sfp, value);
800 return 0;
801
802 case hwmon_temp_lcrit_alarm:
803 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
804 if (err < 0)
805 return err;
806
807 *value = !!(status & SFP_ALARM0_TEMP_LOW);
808 return 0;
809
810 case hwmon_temp_min_alarm:
811 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
812 if (err < 0)
813 return err;
814
815 *value = !!(status & SFP_WARN0_TEMP_LOW);
816 return 0;
817
818 case hwmon_temp_max_alarm:
819 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
820 if (err < 0)
821 return err;
822
823 *value = !!(status & SFP_WARN0_TEMP_HIGH);
824 return 0;
825
826 case hwmon_temp_crit_alarm:
827 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
828 if (err < 0)
829 return err;
830
831 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
832 return 0;
833 default:
834 return -EOPNOTSUPP;
835 }
836
837 return -EOPNOTSUPP;
838 }
839
sfp_hwmon_vcc(struct sfp * sfp,u32 attr,long * value)840 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
841 {
842 u8 status;
843 int err;
844
845 switch (attr) {
846 case hwmon_in_input:
847 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
848
849 case hwmon_in_lcrit:
850 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
851 sfp_hwmon_calibrate_vcc(sfp, value);
852 return 0;
853
854 case hwmon_in_min:
855 *value = be16_to_cpu(sfp->diag.volt_low_warn);
856 sfp_hwmon_calibrate_vcc(sfp, value);
857 return 0;
858
859 case hwmon_in_max:
860 *value = be16_to_cpu(sfp->diag.volt_high_warn);
861 sfp_hwmon_calibrate_vcc(sfp, value);
862 return 0;
863
864 case hwmon_in_crit:
865 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
866 sfp_hwmon_calibrate_vcc(sfp, value);
867 return 0;
868
869 case hwmon_in_lcrit_alarm:
870 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
871 if (err < 0)
872 return err;
873
874 *value = !!(status & SFP_ALARM0_VCC_LOW);
875 return 0;
876
877 case hwmon_in_min_alarm:
878 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
879 if (err < 0)
880 return err;
881
882 *value = !!(status & SFP_WARN0_VCC_LOW);
883 return 0;
884
885 case hwmon_in_max_alarm:
886 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
887 if (err < 0)
888 return err;
889
890 *value = !!(status & SFP_WARN0_VCC_HIGH);
891 return 0;
892
893 case hwmon_in_crit_alarm:
894 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
895 if (err < 0)
896 return err;
897
898 *value = !!(status & SFP_ALARM0_VCC_HIGH);
899 return 0;
900 default:
901 return -EOPNOTSUPP;
902 }
903
904 return -EOPNOTSUPP;
905 }
906
sfp_hwmon_bias(struct sfp * sfp,u32 attr,long * value)907 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
908 {
909 u8 status;
910 int err;
911
912 switch (attr) {
913 case hwmon_curr_input:
914 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
915
916 case hwmon_curr_lcrit:
917 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
918 sfp_hwmon_calibrate_bias(sfp, value);
919 return 0;
920
921 case hwmon_curr_min:
922 *value = be16_to_cpu(sfp->diag.bias_low_warn);
923 sfp_hwmon_calibrate_bias(sfp, value);
924 return 0;
925
926 case hwmon_curr_max:
927 *value = be16_to_cpu(sfp->diag.bias_high_warn);
928 sfp_hwmon_calibrate_bias(sfp, value);
929 return 0;
930
931 case hwmon_curr_crit:
932 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
933 sfp_hwmon_calibrate_bias(sfp, value);
934 return 0;
935
936 case hwmon_curr_lcrit_alarm:
937 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
938 if (err < 0)
939 return err;
940
941 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
942 return 0;
943
944 case hwmon_curr_min_alarm:
945 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
946 if (err < 0)
947 return err;
948
949 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
950 return 0;
951
952 case hwmon_curr_max_alarm:
953 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
954 if (err < 0)
955 return err;
956
957 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
958 return 0;
959
960 case hwmon_curr_crit_alarm:
961 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
962 if (err < 0)
963 return err;
964
965 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
966 return 0;
967 default:
968 return -EOPNOTSUPP;
969 }
970
971 return -EOPNOTSUPP;
972 }
973
sfp_hwmon_tx_power(struct sfp * sfp,u32 attr,long * value)974 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
975 {
976 u8 status;
977 int err;
978
979 switch (attr) {
980 case hwmon_power_input:
981 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
982
983 case hwmon_power_lcrit:
984 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
985 sfp_hwmon_calibrate_tx_power(sfp, value);
986 return 0;
987
988 case hwmon_power_min:
989 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
990 sfp_hwmon_calibrate_tx_power(sfp, value);
991 return 0;
992
993 case hwmon_power_max:
994 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
995 sfp_hwmon_calibrate_tx_power(sfp, value);
996 return 0;
997
998 case hwmon_power_crit:
999 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
1000 sfp_hwmon_calibrate_tx_power(sfp, value);
1001 return 0;
1002
1003 case hwmon_power_lcrit_alarm:
1004 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1005 if (err < 0)
1006 return err;
1007
1008 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
1009 return 0;
1010
1011 case hwmon_power_min_alarm:
1012 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1013 if (err < 0)
1014 return err;
1015
1016 *value = !!(status & SFP_WARN0_TXPWR_LOW);
1017 return 0;
1018
1019 case hwmon_power_max_alarm:
1020 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1021 if (err < 0)
1022 return err;
1023
1024 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
1025 return 0;
1026
1027 case hwmon_power_crit_alarm:
1028 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1029 if (err < 0)
1030 return err;
1031
1032 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
1033 return 0;
1034 default:
1035 return -EOPNOTSUPP;
1036 }
1037
1038 return -EOPNOTSUPP;
1039 }
1040
sfp_hwmon_rx_power(struct sfp * sfp,u32 attr,long * value)1041 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
1042 {
1043 u8 status;
1044 int err;
1045
1046 switch (attr) {
1047 case hwmon_power_input:
1048 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
1049
1050 case hwmon_power_lcrit:
1051 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1052 sfp_hwmon_to_rx_power(value);
1053 return 0;
1054
1055 case hwmon_power_min:
1056 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1057 sfp_hwmon_to_rx_power(value);
1058 return 0;
1059
1060 case hwmon_power_max:
1061 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1062 sfp_hwmon_to_rx_power(value);
1063 return 0;
1064
1065 case hwmon_power_crit:
1066 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1067 sfp_hwmon_to_rx_power(value);
1068 return 0;
1069
1070 case hwmon_power_lcrit_alarm:
1071 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1072 if (err < 0)
1073 return err;
1074
1075 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
1076 return 0;
1077
1078 case hwmon_power_min_alarm:
1079 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1080 if (err < 0)
1081 return err;
1082
1083 *value = !!(status & SFP_WARN1_RXPWR_LOW);
1084 return 0;
1085
1086 case hwmon_power_max_alarm:
1087 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1088 if (err < 0)
1089 return err;
1090
1091 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
1092 return 0;
1093
1094 case hwmon_power_crit_alarm:
1095 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1096 if (err < 0)
1097 return err;
1098
1099 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1100 return 0;
1101 default:
1102 return -EOPNOTSUPP;
1103 }
1104
1105 return -EOPNOTSUPP;
1106 }
1107
sfp_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * value)1108 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1109 u32 attr, int channel, long *value)
1110 {
1111 struct sfp *sfp = dev_get_drvdata(dev);
1112
1113 switch (type) {
1114 case hwmon_temp:
1115 return sfp_hwmon_temp(sfp, attr, value);
1116 case hwmon_in:
1117 return sfp_hwmon_vcc(sfp, attr, value);
1118 case hwmon_curr:
1119 return sfp_hwmon_bias(sfp, attr, value);
1120 case hwmon_power:
1121 switch (channel) {
1122 case 0:
1123 return sfp_hwmon_tx_power(sfp, attr, value);
1124 case 1:
1125 return sfp_hwmon_rx_power(sfp, attr, value);
1126 default:
1127 return -EOPNOTSUPP;
1128 }
1129 default:
1130 return -EOPNOTSUPP;
1131 }
1132 }
1133
1134 static const char *const sfp_hwmon_power_labels[] = {
1135 "TX_power",
1136 "RX_power",
1137 };
1138
sfp_hwmon_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)1139 static int sfp_hwmon_read_string(struct device *dev,
1140 enum hwmon_sensor_types type,
1141 u32 attr, int channel, const char **str)
1142 {
1143 switch (type) {
1144 case hwmon_curr:
1145 switch (attr) {
1146 case hwmon_curr_label:
1147 *str = "bias";
1148 return 0;
1149 default:
1150 return -EOPNOTSUPP;
1151 }
1152 break;
1153 case hwmon_temp:
1154 switch (attr) {
1155 case hwmon_temp_label:
1156 *str = "temperature";
1157 return 0;
1158 default:
1159 return -EOPNOTSUPP;
1160 }
1161 break;
1162 case hwmon_in:
1163 switch (attr) {
1164 case hwmon_in_label:
1165 *str = "VCC";
1166 return 0;
1167 default:
1168 return -EOPNOTSUPP;
1169 }
1170 break;
1171 case hwmon_power:
1172 switch (attr) {
1173 case hwmon_power_label:
1174 *str = sfp_hwmon_power_labels[channel];
1175 return 0;
1176 default:
1177 return -EOPNOTSUPP;
1178 }
1179 break;
1180 default:
1181 return -EOPNOTSUPP;
1182 }
1183
1184 return -EOPNOTSUPP;
1185 }
1186
1187 static const struct hwmon_ops sfp_hwmon_ops = {
1188 .is_visible = sfp_hwmon_is_visible,
1189 .read = sfp_hwmon_read,
1190 .read_string = sfp_hwmon_read_string,
1191 };
1192
1193 static u32 sfp_hwmon_chip_config[] = {
1194 HWMON_C_REGISTER_TZ,
1195 0,
1196 };
1197
1198 static const struct hwmon_channel_info sfp_hwmon_chip = {
1199 .type = hwmon_chip,
1200 .config = sfp_hwmon_chip_config,
1201 };
1202
1203 static u32 sfp_hwmon_temp_config[] = {
1204 HWMON_T_INPUT |
1205 HWMON_T_MAX | HWMON_T_MIN |
1206 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1207 HWMON_T_CRIT | HWMON_T_LCRIT |
1208 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1209 HWMON_T_LABEL,
1210 0,
1211 };
1212
1213 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1214 .type = hwmon_temp,
1215 .config = sfp_hwmon_temp_config,
1216 };
1217
1218 static u32 sfp_hwmon_vcc_config[] = {
1219 HWMON_I_INPUT |
1220 HWMON_I_MAX | HWMON_I_MIN |
1221 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1222 HWMON_I_CRIT | HWMON_I_LCRIT |
1223 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1224 HWMON_I_LABEL,
1225 0,
1226 };
1227
1228 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1229 .type = hwmon_in,
1230 .config = sfp_hwmon_vcc_config,
1231 };
1232
1233 static u32 sfp_hwmon_bias_config[] = {
1234 HWMON_C_INPUT |
1235 HWMON_C_MAX | HWMON_C_MIN |
1236 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1237 HWMON_C_CRIT | HWMON_C_LCRIT |
1238 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1239 HWMON_C_LABEL,
1240 0,
1241 };
1242
1243 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1244 .type = hwmon_curr,
1245 .config = sfp_hwmon_bias_config,
1246 };
1247
1248 static u32 sfp_hwmon_power_config[] = {
1249 /* Transmit power */
1250 HWMON_P_INPUT |
1251 HWMON_P_MAX | HWMON_P_MIN |
1252 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1253 HWMON_P_CRIT | HWMON_P_LCRIT |
1254 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1255 HWMON_P_LABEL,
1256 /* Receive power */
1257 HWMON_P_INPUT |
1258 HWMON_P_MAX | HWMON_P_MIN |
1259 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1260 HWMON_P_CRIT | HWMON_P_LCRIT |
1261 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1262 HWMON_P_LABEL,
1263 0,
1264 };
1265
1266 static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1267 .type = hwmon_power,
1268 .config = sfp_hwmon_power_config,
1269 };
1270
1271 static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1272 &sfp_hwmon_chip,
1273 &sfp_hwmon_vcc_channel_info,
1274 &sfp_hwmon_temp_channel_info,
1275 &sfp_hwmon_bias_channel_info,
1276 &sfp_hwmon_power_channel_info,
1277 NULL,
1278 };
1279
1280 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1281 .ops = &sfp_hwmon_ops,
1282 .info = sfp_hwmon_info,
1283 };
1284
sfp_hwmon_probe(struct work_struct * work)1285 static void sfp_hwmon_probe(struct work_struct *work)
1286 {
1287 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
1288 int err, i;
1289
1290 /* hwmon interface needs to access 16bit registers in atomic way to
1291 * guarantee coherency of the diagnostic monitoring data. If it is not
1292 * possible to guarantee coherency because EEPROM is broken in such way
1293 * that does not support atomic 16bit read operation then we have to
1294 * skip registration of hwmon device.
1295 */
1296 if (sfp->i2c_block_size < 2) {
1297 dev_info(sfp->dev,
1298 "skipping hwmon device registration due to broken EEPROM\n");
1299 dev_info(sfp->dev,
1300 "diagnostic EEPROM area cannot be read atomically to guarantee data coherency\n");
1301 return;
1302 }
1303
1304 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1305 if (err < 0) {
1306 if (sfp->hwmon_tries--) {
1307 mod_delayed_work(system_wq, &sfp->hwmon_probe,
1308 T_PROBE_RETRY_SLOW);
1309 } else {
1310 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1311 }
1312 return;
1313 }
1314
1315 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1316 if (!sfp->hwmon_name) {
1317 dev_err(sfp->dev, "out of memory for hwmon name\n");
1318 return;
1319 }
1320
1321 for (i = 0; sfp->hwmon_name[i]; i++)
1322 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1323 sfp->hwmon_name[i] = '_';
1324
1325 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1326 sfp->hwmon_name, sfp,
1327 &sfp_hwmon_chip_info,
1328 NULL);
1329 if (IS_ERR(sfp->hwmon_dev))
1330 dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1331 PTR_ERR(sfp->hwmon_dev));
1332 }
1333
sfp_hwmon_insert(struct sfp * sfp)1334 static int sfp_hwmon_insert(struct sfp *sfp)
1335 {
1336 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1337 return 0;
1338
1339 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1340 return 0;
1341
1342 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1343 /* This driver in general does not support address
1344 * change.
1345 */
1346 return 0;
1347
1348 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
1349 sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
1350
1351 return 0;
1352 }
1353
sfp_hwmon_remove(struct sfp * sfp)1354 static void sfp_hwmon_remove(struct sfp *sfp)
1355 {
1356 cancel_delayed_work_sync(&sfp->hwmon_probe);
1357 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1358 hwmon_device_unregister(sfp->hwmon_dev);
1359 sfp->hwmon_dev = NULL;
1360 kfree(sfp->hwmon_name);
1361 }
1362 }
1363
sfp_hwmon_init(struct sfp * sfp)1364 static int sfp_hwmon_init(struct sfp *sfp)
1365 {
1366 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1367
1368 return 0;
1369 }
1370
sfp_hwmon_exit(struct sfp * sfp)1371 static void sfp_hwmon_exit(struct sfp *sfp)
1372 {
1373 cancel_delayed_work_sync(&sfp->hwmon_probe);
1374 }
1375 #else
sfp_hwmon_insert(struct sfp * sfp)1376 static int sfp_hwmon_insert(struct sfp *sfp)
1377 {
1378 return 0;
1379 }
1380
sfp_hwmon_remove(struct sfp * sfp)1381 static void sfp_hwmon_remove(struct sfp *sfp)
1382 {
1383 }
1384
sfp_hwmon_init(struct sfp * sfp)1385 static int sfp_hwmon_init(struct sfp *sfp)
1386 {
1387 return 0;
1388 }
1389
sfp_hwmon_exit(struct sfp * sfp)1390 static void sfp_hwmon_exit(struct sfp *sfp)
1391 {
1392 }
1393 #endif
1394
1395 /* Helpers */
sfp_module_tx_disable(struct sfp * sfp)1396 static void sfp_module_tx_disable(struct sfp *sfp)
1397 {
1398 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1399 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1400 sfp->state |= SFP_F_TX_DISABLE;
1401 sfp_set_state(sfp, sfp->state);
1402 }
1403
sfp_module_tx_enable(struct sfp * sfp)1404 static void sfp_module_tx_enable(struct sfp *sfp)
1405 {
1406 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1407 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1408 sfp->state &= ~SFP_F_TX_DISABLE;
1409 sfp_set_state(sfp, sfp->state);
1410 }
1411
sfp_module_tx_fault_reset(struct sfp * sfp)1412 static void sfp_module_tx_fault_reset(struct sfp *sfp)
1413 {
1414 unsigned int state = sfp->state;
1415
1416 if (state & SFP_F_TX_DISABLE)
1417 return;
1418
1419 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1420
1421 udelay(T_RESET_US);
1422
1423 sfp_set_state(sfp, state);
1424 }
1425
1426 /* SFP state machine */
sfp_sm_set_timer(struct sfp * sfp,unsigned int timeout)1427 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1428 {
1429 if (timeout)
1430 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1431 timeout);
1432 else
1433 cancel_delayed_work(&sfp->timeout);
1434 }
1435
sfp_sm_next(struct sfp * sfp,unsigned int state,unsigned int timeout)1436 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1437 unsigned int timeout)
1438 {
1439 sfp->sm_state = state;
1440 sfp_sm_set_timer(sfp, timeout);
1441 }
1442
sfp_sm_mod_next(struct sfp * sfp,unsigned int state,unsigned int timeout)1443 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
1444 unsigned int timeout)
1445 {
1446 sfp->sm_mod_state = state;
1447 sfp_sm_set_timer(sfp, timeout);
1448 }
1449
sfp_sm_phy_detach(struct sfp * sfp)1450 static void sfp_sm_phy_detach(struct sfp *sfp)
1451 {
1452 sfp_remove_phy(sfp->sfp_bus);
1453 phy_device_remove(sfp->mod_phy);
1454 phy_device_free(sfp->mod_phy);
1455 sfp->mod_phy = NULL;
1456 }
1457
sfp_sm_probe_phy(struct sfp * sfp,bool is_c45)1458 static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45)
1459 {
1460 struct phy_device *phy;
1461 int err;
1462
1463 phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45);
1464 if (phy == ERR_PTR(-ENODEV))
1465 return PTR_ERR(phy);
1466 if (IS_ERR(phy)) {
1467 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1468 return PTR_ERR(phy);
1469 }
1470
1471 err = phy_device_register(phy);
1472 if (err) {
1473 phy_device_free(phy);
1474 dev_err(sfp->dev, "phy_device_register failed: %d\n", err);
1475 return err;
1476 }
1477
1478 err = sfp_add_phy(sfp->sfp_bus, phy);
1479 if (err) {
1480 phy_device_remove(phy);
1481 phy_device_free(phy);
1482 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1483 return err;
1484 }
1485
1486 sfp->mod_phy = phy;
1487
1488 return 0;
1489 }
1490
sfp_sm_link_up(struct sfp * sfp)1491 static void sfp_sm_link_up(struct sfp *sfp)
1492 {
1493 sfp_link_up(sfp->sfp_bus);
1494 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1495 }
1496
sfp_sm_link_down(struct sfp * sfp)1497 static void sfp_sm_link_down(struct sfp *sfp)
1498 {
1499 sfp_link_down(sfp->sfp_bus);
1500 }
1501
sfp_sm_link_check_los(struct sfp * sfp)1502 static void sfp_sm_link_check_los(struct sfp *sfp)
1503 {
1504 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
1505 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
1506 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
1507 bool los = false;
1508
1509 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1510 * are set, we assume that no LOS signal is available. If both are
1511 * set, we assume LOS is not implemented (and is meaningless.)
1512 */
1513 if (los_options == los_inverted)
1514 los = !(sfp->state & SFP_F_LOS);
1515 else if (los_options == los_normal)
1516 los = !!(sfp->state & SFP_F_LOS);
1517
1518 if (los)
1519 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1520 else
1521 sfp_sm_link_up(sfp);
1522 }
1523
sfp_los_event_active(struct sfp * sfp,unsigned int event)1524 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1525 {
1526 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
1527 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
1528 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
1529
1530 return (los_options == los_inverted && event == SFP_E_LOS_LOW) ||
1531 (los_options == los_normal && event == SFP_E_LOS_HIGH);
1532 }
1533
sfp_los_event_inactive(struct sfp * sfp,unsigned int event)1534 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1535 {
1536 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
1537 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
1538 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
1539
1540 return (los_options == los_inverted && event == SFP_E_LOS_HIGH) ||
1541 (los_options == los_normal && event == SFP_E_LOS_LOW);
1542 }
1543
sfp_sm_fault(struct sfp * sfp,unsigned int next_state,bool warn)1544 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
1545 {
1546 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) {
1547 dev_err(sfp->dev,
1548 "module persistently indicates fault, disabling\n");
1549 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1550 } else {
1551 if (warn)
1552 dev_err(sfp->dev, "module transmit fault indicated\n");
1553
1554 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
1555 }
1556 }
1557
1558 /* Probe a SFP for a PHY device if the module supports copper - the PHY
1559 * normally sits at I2C bus address 0x56, and may either be a clause 22
1560 * or clause 45 PHY.
1561 *
1562 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with
1563 * negotiation enabled, but some may be in 1000base-X - which is for the
1564 * PHY driver to determine.
1565 *
1566 * Clause 45 copper SFP+ modules (10G) appear to switch their interface
1567 * mode according to the negotiated line speed.
1568 */
sfp_sm_probe_for_phy(struct sfp * sfp)1569 static int sfp_sm_probe_for_phy(struct sfp *sfp)
1570 {
1571 int err = 0;
1572
1573 switch (sfp->id.base.extended_cc) {
1574 case SFF8024_ECC_10GBASE_T_SFI:
1575 case SFF8024_ECC_10GBASE_T_SR:
1576 case SFF8024_ECC_5GBASE_T:
1577 case SFF8024_ECC_2_5GBASE_T:
1578 err = sfp_sm_probe_phy(sfp, true);
1579 break;
1580
1581 default:
1582 if (sfp->id.base.e1000_base_t)
1583 err = sfp_sm_probe_phy(sfp, false);
1584 break;
1585 }
1586 return err;
1587 }
1588
sfp_module_parse_power(struct sfp * sfp)1589 static int sfp_module_parse_power(struct sfp *sfp)
1590 {
1591 u32 power_mW = 1000;
1592
1593 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1594 power_mW = 1500;
1595 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1596 power_mW = 2000;
1597
1598 if (power_mW > sfp->max_power_mW) {
1599 /* Module power specification exceeds the allowed maximum. */
1600 if (sfp->id.ext.sff8472_compliance ==
1601 SFP_SFF8472_COMPLIANCE_NONE &&
1602 !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1603 /* The module appears not to implement bus address
1604 * 0xa2, so assume that the module powers up in the
1605 * indicated mode.
1606 */
1607 dev_err(sfp->dev,
1608 "Host does not support %u.%uW modules\n",
1609 power_mW / 1000, (power_mW / 100) % 10);
1610 return -EINVAL;
1611 } else {
1612 dev_warn(sfp->dev,
1613 "Host does not support %u.%uW modules, module left in power mode 1\n",
1614 power_mW / 1000, (power_mW / 100) % 10);
1615 return 0;
1616 }
1617 }
1618
1619 /* If the module requires a higher power mode, but also requires
1620 * an address change sequence, warn the user that the module may
1621 * not be functional.
1622 */
1623 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
1624 dev_warn(sfp->dev,
1625 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
1626 power_mW / 1000, (power_mW / 100) % 10);
1627 return 0;
1628 }
1629
1630 sfp->module_power_mW = power_mW;
1631
1632 return 0;
1633 }
1634
sfp_sm_mod_hpower(struct sfp * sfp,bool enable)1635 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
1636 {
1637 u8 val;
1638 int err;
1639
1640 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1641 if (err != sizeof(val)) {
1642 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1643 return -EAGAIN;
1644 }
1645
1646 /* DM7052 reports as a high power module, responds to reads (with
1647 * all bytes 0xff) at 0x51 but does not accept writes. In any case,
1648 * if the bit is already set, we're already in high power mode.
1649 */
1650 if (!!(val & BIT(0)) == enable)
1651 return 0;
1652
1653 if (enable)
1654 val |= BIT(0);
1655 else
1656 val &= ~BIT(0);
1657
1658 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1659 if (err != sizeof(val)) {
1660 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1661 return -EAGAIN;
1662 }
1663
1664 if (enable)
1665 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1666 sfp->module_power_mW / 1000,
1667 (sfp->module_power_mW / 100) % 10);
1668
1669 return 0;
1670 }
1671
1672 /* GPON modules based on Realtek RTL8672 and RTL9601C chips (e.g. V-SOL
1673 * V2801F, CarlitoxxPro CPGOS03-0490, Ubiquiti U-Fiber Instant, ...) do
1674 * not support multibyte reads from the EEPROM. Each multi-byte read
1675 * operation returns just one byte of EEPROM followed by zeros. There is
1676 * no way to identify which modules are using Realtek RTL8672 and RTL9601C
1677 * chips. Moreover every OEM of V-SOL V2801F module puts its own vendor
1678 * name and vendor id into EEPROM, so there is even no way to detect if
1679 * module is V-SOL V2801F. Therefore check for those zeros in the read
1680 * data and then based on check switch to reading EEPROM to one byte
1681 * at a time.
1682 */
sfp_id_needs_byte_io(struct sfp * sfp,void * buf,size_t len)1683 static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len)
1684 {
1685 size_t i, block_size = sfp->i2c_block_size;
1686
1687 /* Already using byte IO */
1688 if (block_size == 1)
1689 return false;
1690
1691 for (i = 1; i < len; i += block_size) {
1692 if (memchr_inv(buf + i, '\0', min(block_size - 1, len - i)))
1693 return false;
1694 }
1695 return true;
1696 }
1697
sfp_cotsworks_fixup_check(struct sfp * sfp,struct sfp_eeprom_id * id)1698 static int sfp_cotsworks_fixup_check(struct sfp *sfp, struct sfp_eeprom_id *id)
1699 {
1700 u8 check;
1701 int err;
1702
1703 if (id->base.phys_id != SFF8024_ID_SFF_8472 ||
1704 id->base.phys_ext_id != SFP_PHYS_EXT_ID_SFP ||
1705 id->base.connector != SFF8024_CONNECTOR_LC) {
1706 dev_warn(sfp->dev, "Rewriting fiber module EEPROM with corrected values\n");
1707 id->base.phys_id = SFF8024_ID_SFF_8472;
1708 id->base.phys_ext_id = SFP_PHYS_EXT_ID_SFP;
1709 id->base.connector = SFF8024_CONNECTOR_LC;
1710 err = sfp_write(sfp, false, SFP_PHYS_ID, &id->base, 3);
1711 if (err != 3) {
1712 dev_err(sfp->dev, "Failed to rewrite module EEPROM: %d\n", err);
1713 return err;
1714 }
1715
1716 /* Cotsworks modules have been found to require a delay between write operations. */
1717 mdelay(50);
1718
1719 /* Update base structure checksum */
1720 check = sfp_check(&id->base, sizeof(id->base) - 1);
1721 err = sfp_write(sfp, false, SFP_CC_BASE, &check, 1);
1722 if (err != 1) {
1723 dev_err(sfp->dev, "Failed to update base structure checksum in fiber module EEPROM: %d\n", err);
1724 return err;
1725 }
1726 }
1727 return 0;
1728 }
1729
sfp_sm_mod_probe(struct sfp * sfp,bool report)1730 static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
1731 {
1732 /* SFP module inserted - read I2C data */
1733 struct sfp_eeprom_id id;
1734 bool cotsworks_sfbg;
1735 bool cotsworks;
1736 u8 check;
1737 int ret;
1738
1739 /* Some SFP modules and also some Linux I2C drivers do not like reads
1740 * longer than 16 bytes, so read the EEPROM in chunks of 16 bytes at
1741 * a time.
1742 */
1743 sfp->i2c_block_size = 16;
1744
1745 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
1746 if (ret < 0) {
1747 if (report)
1748 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1749 return -EAGAIN;
1750 }
1751
1752 if (ret != sizeof(id.base)) {
1753 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1754 return -EAGAIN;
1755 }
1756
1757 /* Some SFP modules (e.g. Nokia 3FE46541AA) lock up if read from
1758 * address 0x51 is just one byte at a time. Also SFF-8472 requires
1759 * that EEPROM supports atomic 16bit read operation for diagnostic
1760 * fields, so do not switch to one byte reading at a time unless it
1761 * is really required and we have no other option.
1762 */
1763 if (sfp_id_needs_byte_io(sfp, &id.base, sizeof(id.base))) {
1764 dev_info(sfp->dev,
1765 "Detected broken RTL8672/RTL9601C emulated EEPROM\n");
1766 dev_info(sfp->dev,
1767 "Switching to reading EEPROM to one byte at a time\n");
1768 sfp->i2c_block_size = 1;
1769
1770 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
1771 if (ret < 0) {
1772 if (report)
1773 dev_err(sfp->dev, "failed to read EEPROM: %d\n",
1774 ret);
1775 return -EAGAIN;
1776 }
1777
1778 if (ret != sizeof(id.base)) {
1779 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1780 return -EAGAIN;
1781 }
1782 }
1783
1784 /* Cotsworks do not seem to update the checksums when they
1785 * do the final programming with the final module part number,
1786 * serial number and date code.
1787 */
1788 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1789 cotsworks_sfbg = !memcmp(id.base.vendor_pn, "SFBG", 4);
1790
1791 /* Cotsworks SFF module EEPROM do not always have valid phys_id,
1792 * phys_ext_id, and connector bytes. Rewrite SFF EEPROM bytes if
1793 * Cotsworks PN matches and bytes are not correct.
1794 */
1795 if (cotsworks && cotsworks_sfbg) {
1796 ret = sfp_cotsworks_fixup_check(sfp, &id);
1797 if (ret < 0)
1798 return ret;
1799 }
1800
1801 /* Validate the checksum over the base structure */
1802 check = sfp_check(&id.base, sizeof(id.base) - 1);
1803 if (check != id.base.cc_base) {
1804 if (cotsworks) {
1805 dev_warn(sfp->dev,
1806 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1807 check, id.base.cc_base);
1808 } else {
1809 dev_err(sfp->dev,
1810 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1811 check, id.base.cc_base);
1812 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1813 16, 1, &id, sizeof(id), true);
1814 return -EINVAL;
1815 }
1816 }
1817
1818 ret = sfp_read(sfp, false, SFP_CC_BASE + 1, &id.ext, sizeof(id.ext));
1819 if (ret < 0) {
1820 if (report)
1821 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1822 return -EAGAIN;
1823 }
1824
1825 if (ret != sizeof(id.ext)) {
1826 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1827 return -EAGAIN;
1828 }
1829
1830 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1831 if (check != id.ext.cc_ext) {
1832 if (cotsworks) {
1833 dev_warn(sfp->dev,
1834 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1835 check, id.ext.cc_ext);
1836 } else {
1837 dev_err(sfp->dev,
1838 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1839 check, id.ext.cc_ext);
1840 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1841 16, 1, &id, sizeof(id), true);
1842 memset(&id.ext, 0, sizeof(id.ext));
1843 }
1844 }
1845
1846 sfp->id = id;
1847
1848 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1849 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1850 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1851 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1852 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1853 (int)sizeof(id.ext.datecode), id.ext.datecode);
1854
1855 /* Check whether we support this module */
1856 if (!sfp->type->module_supported(&id)) {
1857 dev_err(sfp->dev,
1858 "module is not supported - phys id 0x%02x 0x%02x\n",
1859 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1860 return -EINVAL;
1861 }
1862
1863 /* If the module requires address swap mode, warn about it */
1864 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1865 dev_warn(sfp->dev,
1866 "module address swap to access page 0xA2 is not supported.\n");
1867
1868 /* Parse the module power requirement */
1869 ret = sfp_module_parse_power(sfp);
1870 if (ret < 0)
1871 return ret;
1872
1873 if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) &&
1874 !memcmp(id.base.vendor_pn, "3FE46541AA ", 16))
1875 sfp->module_t_start_up = T_START_UP_BAD_GPON;
1876 else
1877 sfp->module_t_start_up = T_START_UP;
1878
1879 return 0;
1880 }
1881
sfp_sm_mod_remove(struct sfp * sfp)1882 static void sfp_sm_mod_remove(struct sfp *sfp)
1883 {
1884 if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
1885 sfp_module_remove(sfp->sfp_bus);
1886
1887 sfp_hwmon_remove(sfp);
1888
1889 memset(&sfp->id, 0, sizeof(sfp->id));
1890 sfp->module_power_mW = 0;
1891
1892 dev_info(sfp->dev, "module removed\n");
1893 }
1894
1895 /* This state machine tracks the upstream's state */
sfp_sm_device(struct sfp * sfp,unsigned int event)1896 static void sfp_sm_device(struct sfp *sfp, unsigned int event)
1897 {
1898 switch (sfp->sm_dev_state) {
1899 default:
1900 if (event == SFP_E_DEV_ATTACH)
1901 sfp->sm_dev_state = SFP_DEV_DOWN;
1902 break;
1903
1904 case SFP_DEV_DOWN:
1905 if (event == SFP_E_DEV_DETACH)
1906 sfp->sm_dev_state = SFP_DEV_DETACHED;
1907 else if (event == SFP_E_DEV_UP)
1908 sfp->sm_dev_state = SFP_DEV_UP;
1909 break;
1910
1911 case SFP_DEV_UP:
1912 if (event == SFP_E_DEV_DETACH)
1913 sfp->sm_dev_state = SFP_DEV_DETACHED;
1914 else if (event == SFP_E_DEV_DOWN)
1915 sfp->sm_dev_state = SFP_DEV_DOWN;
1916 break;
1917 }
1918 }
1919
1920 /* This state machine tracks the insert/remove state of the module, probes
1921 * the on-board EEPROM, and sets up the power level.
1922 */
sfp_sm_module(struct sfp * sfp,unsigned int event)1923 static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1924 {
1925 int err;
1926
1927 /* Handle remove event globally, it resets this state machine */
1928 if (event == SFP_E_REMOVE) {
1929 if (sfp->sm_mod_state > SFP_MOD_PROBE)
1930 sfp_sm_mod_remove(sfp);
1931 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
1932 return;
1933 }
1934
1935 /* Handle device detach globally */
1936 if (sfp->sm_dev_state < SFP_DEV_DOWN &&
1937 sfp->sm_mod_state > SFP_MOD_WAITDEV) {
1938 if (sfp->module_power_mW > 1000 &&
1939 sfp->sm_mod_state > SFP_MOD_HPOWER)
1940 sfp_sm_mod_hpower(sfp, false);
1941 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1942 return;
1943 }
1944
1945 switch (sfp->sm_mod_state) {
1946 default:
1947 if (event == SFP_E_INSERT) {
1948 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
1949 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
1950 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
1951 }
1952 break;
1953
1954 case SFP_MOD_PROBE:
1955 /* Wait for T_PROBE_INIT to time out */
1956 if (event != SFP_E_TIMEOUT)
1957 break;
1958
1959 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
1960 if (err == -EAGAIN) {
1961 if (sfp->sm_mod_tries_init &&
1962 --sfp->sm_mod_tries_init) {
1963 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1964 break;
1965 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
1966 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
1967 dev_warn(sfp->dev,
1968 "please wait, module slow to respond\n");
1969 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
1970 break;
1971 }
1972 }
1973 if (err < 0) {
1974 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1975 break;
1976 }
1977
1978 err = sfp_hwmon_insert(sfp);
1979 if (err)
1980 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1981
1982 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1983 fallthrough;
1984 case SFP_MOD_WAITDEV:
1985 /* Ensure that the device is attached before proceeding */
1986 if (sfp->sm_dev_state < SFP_DEV_DOWN)
1987 break;
1988
1989 /* Report the module insertion to the upstream device */
1990 err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1991 if (err < 0) {
1992 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1993 break;
1994 }
1995
1996 /* If this is a power level 1 module, we are done */
1997 if (sfp->module_power_mW <= 1000)
1998 goto insert;
1999
2000 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
2001 fallthrough;
2002 case SFP_MOD_HPOWER:
2003 /* Enable high power mode */
2004 err = sfp_sm_mod_hpower(sfp, true);
2005 if (err < 0) {
2006 if (err != -EAGAIN) {
2007 sfp_module_remove(sfp->sfp_bus);
2008 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2009 } else {
2010 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
2011 }
2012 break;
2013 }
2014
2015 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
2016 break;
2017
2018 case SFP_MOD_WAITPWR:
2019 /* Wait for T_HPOWER_LEVEL to time out */
2020 if (event != SFP_E_TIMEOUT)
2021 break;
2022
2023 insert:
2024 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
2025 break;
2026
2027 case SFP_MOD_PRESENT:
2028 case SFP_MOD_ERROR:
2029 break;
2030 }
2031 }
2032
sfp_sm_main(struct sfp * sfp,unsigned int event)2033 static void sfp_sm_main(struct sfp *sfp, unsigned int event)
2034 {
2035 unsigned long timeout;
2036 int ret;
2037
2038 /* Some events are global */
2039 if (sfp->sm_state != SFP_S_DOWN &&
2040 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
2041 sfp->sm_dev_state != SFP_DEV_UP)) {
2042 if (sfp->sm_state == SFP_S_LINK_UP &&
2043 sfp->sm_dev_state == SFP_DEV_UP)
2044 sfp_sm_link_down(sfp);
2045 if (sfp->sm_state > SFP_S_INIT)
2046 sfp_module_stop(sfp->sfp_bus);
2047 if (sfp->mod_phy)
2048 sfp_sm_phy_detach(sfp);
2049 sfp_module_tx_disable(sfp);
2050 sfp_soft_stop_poll(sfp);
2051 sfp_sm_next(sfp, SFP_S_DOWN, 0);
2052 return;
2053 }
2054
2055 /* The main state machine */
2056 switch (sfp->sm_state) {
2057 case SFP_S_DOWN:
2058 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
2059 sfp->sm_dev_state != SFP_DEV_UP)
2060 break;
2061
2062 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE))
2063 sfp_soft_start_poll(sfp);
2064
2065 sfp_module_tx_enable(sfp);
2066
2067 /* Initialise the fault clearance retries */
2068 sfp->sm_fault_retries = N_FAULT_INIT;
2069
2070 /* We need to check the TX_FAULT state, which is not defined
2071 * while TX_DISABLE is asserted. The earliest we want to do
2072 * anything (such as probe for a PHY) is 50ms.
2073 */
2074 sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
2075 break;
2076
2077 case SFP_S_WAIT:
2078 if (event != SFP_E_TIMEOUT)
2079 break;
2080
2081 if (sfp->state & SFP_F_TX_FAULT) {
2082 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
2083 * from the TX_DISABLE deassertion for the module to
2084 * initialise, which is indicated by TX_FAULT
2085 * deasserting.
2086 */
2087 timeout = sfp->module_t_start_up;
2088 if (timeout > T_WAIT)
2089 timeout -= T_WAIT;
2090 else
2091 timeout = 1;
2092
2093 sfp_sm_next(sfp, SFP_S_INIT, timeout);
2094 } else {
2095 /* TX_FAULT is not asserted, assume the module has
2096 * finished initialising.
2097 */
2098 goto init_done;
2099 }
2100 break;
2101
2102 case SFP_S_INIT:
2103 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
2104 /* TX_FAULT is still asserted after t_init or
2105 * or t_start_up, so assume there is a fault.
2106 */
2107 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
2108 sfp->sm_fault_retries == N_FAULT_INIT);
2109 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2110 init_done:
2111 sfp->sm_phy_retries = R_PHY_RETRY;
2112 goto phy_probe;
2113 }
2114 break;
2115
2116 case SFP_S_INIT_PHY:
2117 if (event != SFP_E_TIMEOUT)
2118 break;
2119 phy_probe:
2120 /* TX_FAULT deasserted or we timed out with TX_FAULT
2121 * clear. Probe for the PHY and check the LOS state.
2122 */
2123 ret = sfp_sm_probe_for_phy(sfp);
2124 if (ret == -ENODEV) {
2125 if (--sfp->sm_phy_retries) {
2126 sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY);
2127 break;
2128 } else {
2129 dev_info(sfp->dev, "no PHY detected\n");
2130 }
2131 } else if (ret) {
2132 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2133 break;
2134 }
2135 if (sfp_module_start(sfp->sfp_bus)) {
2136 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2137 break;
2138 }
2139 sfp_sm_link_check_los(sfp);
2140
2141 /* Reset the fault retry count */
2142 sfp->sm_fault_retries = N_FAULT;
2143 break;
2144
2145 case SFP_S_INIT_TX_FAULT:
2146 if (event == SFP_E_TIMEOUT) {
2147 sfp_module_tx_fault_reset(sfp);
2148 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
2149 }
2150 break;
2151
2152 case SFP_S_WAIT_LOS:
2153 if (event == SFP_E_TX_FAULT)
2154 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
2155 else if (sfp_los_event_inactive(sfp, event))
2156 sfp_sm_link_up(sfp);
2157 break;
2158
2159 case SFP_S_LINK_UP:
2160 if (event == SFP_E_TX_FAULT) {
2161 sfp_sm_link_down(sfp);
2162 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
2163 } else if (sfp_los_event_active(sfp, event)) {
2164 sfp_sm_link_down(sfp);
2165 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
2166 }
2167 break;
2168
2169 case SFP_S_TX_FAULT:
2170 if (event == SFP_E_TIMEOUT) {
2171 sfp_module_tx_fault_reset(sfp);
2172 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
2173 }
2174 break;
2175
2176 case SFP_S_REINIT:
2177 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
2178 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
2179 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2180 dev_info(sfp->dev, "module transmit fault recovered\n");
2181 sfp_sm_link_check_los(sfp);
2182 }
2183 break;
2184
2185 case SFP_S_TX_DISABLE:
2186 break;
2187 }
2188 }
2189
sfp_sm_event(struct sfp * sfp,unsigned int event)2190 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
2191 {
2192 mutex_lock(&sfp->sm_mutex);
2193
2194 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
2195 mod_state_to_str(sfp->sm_mod_state),
2196 dev_state_to_str(sfp->sm_dev_state),
2197 sm_state_to_str(sfp->sm_state),
2198 event_to_str(event));
2199
2200 sfp_sm_device(sfp, event);
2201 sfp_sm_module(sfp, event);
2202 sfp_sm_main(sfp, event);
2203
2204 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
2205 mod_state_to_str(sfp->sm_mod_state),
2206 dev_state_to_str(sfp->sm_dev_state),
2207 sm_state_to_str(sfp->sm_state));
2208
2209 mutex_unlock(&sfp->sm_mutex);
2210 }
2211
sfp_attach(struct sfp * sfp)2212 static void sfp_attach(struct sfp *sfp)
2213 {
2214 sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
2215 }
2216
sfp_detach(struct sfp * sfp)2217 static void sfp_detach(struct sfp *sfp)
2218 {
2219 sfp_sm_event(sfp, SFP_E_DEV_DETACH);
2220 }
2221
sfp_start(struct sfp * sfp)2222 static void sfp_start(struct sfp *sfp)
2223 {
2224 sfp_sm_event(sfp, SFP_E_DEV_UP);
2225 }
2226
sfp_stop(struct sfp * sfp)2227 static void sfp_stop(struct sfp *sfp)
2228 {
2229 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
2230 }
2231
sfp_module_info(struct sfp * sfp,struct ethtool_modinfo * modinfo)2232 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
2233 {
2234 /* locking... and check module is present */
2235
2236 if (sfp->id.ext.sff8472_compliance &&
2237 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
2238 modinfo->type = ETH_MODULE_SFF_8472;
2239 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2240 } else {
2241 modinfo->type = ETH_MODULE_SFF_8079;
2242 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
2243 }
2244 return 0;
2245 }
2246
sfp_module_eeprom(struct sfp * sfp,struct ethtool_eeprom * ee,u8 * data)2247 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
2248 u8 *data)
2249 {
2250 unsigned int first, last, len;
2251 int ret;
2252
2253 if (ee->len == 0)
2254 return -EINVAL;
2255
2256 first = ee->offset;
2257 last = ee->offset + ee->len;
2258 if (first < ETH_MODULE_SFF_8079_LEN) {
2259 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
2260 len -= first;
2261
2262 ret = sfp_read(sfp, false, first, data, len);
2263 if (ret < 0)
2264 return ret;
2265
2266 first += len;
2267 data += len;
2268 }
2269 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
2270 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
2271 len -= first;
2272 first -= ETH_MODULE_SFF_8079_LEN;
2273
2274 ret = sfp_read(sfp, true, first, data, len);
2275 if (ret < 0)
2276 return ret;
2277 }
2278 return 0;
2279 }
2280
2281 static const struct sfp_socket_ops sfp_module_ops = {
2282 .attach = sfp_attach,
2283 .detach = sfp_detach,
2284 .start = sfp_start,
2285 .stop = sfp_stop,
2286 .module_info = sfp_module_info,
2287 .module_eeprom = sfp_module_eeprom,
2288 };
2289
sfp_timeout(struct work_struct * work)2290 static void sfp_timeout(struct work_struct *work)
2291 {
2292 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
2293
2294 rtnl_lock();
2295 sfp_sm_event(sfp, SFP_E_TIMEOUT);
2296 rtnl_unlock();
2297 }
2298
sfp_check_state(struct sfp * sfp)2299 static void sfp_check_state(struct sfp *sfp)
2300 {
2301 unsigned int state, i, changed;
2302
2303 mutex_lock(&sfp->st_mutex);
2304 state = sfp_get_state(sfp);
2305 changed = state ^ sfp->state;
2306 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
2307
2308 for (i = 0; i < GPIO_MAX; i++)
2309 if (changed & BIT(i))
2310 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
2311 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
2312
2313 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
2314 sfp->state = state;
2315
2316 rtnl_lock();
2317 if (changed & SFP_F_PRESENT)
2318 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
2319 SFP_E_INSERT : SFP_E_REMOVE);
2320
2321 if (changed & SFP_F_TX_FAULT)
2322 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
2323 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
2324
2325 if (changed & SFP_F_LOS)
2326 sfp_sm_event(sfp, state & SFP_F_LOS ?
2327 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
2328 rtnl_unlock();
2329 mutex_unlock(&sfp->st_mutex);
2330 }
2331
sfp_irq(int irq,void * data)2332 static irqreturn_t sfp_irq(int irq, void *data)
2333 {
2334 struct sfp *sfp = data;
2335
2336 sfp_check_state(sfp);
2337
2338 return IRQ_HANDLED;
2339 }
2340
sfp_poll(struct work_struct * work)2341 static void sfp_poll(struct work_struct *work)
2342 {
2343 struct sfp *sfp = container_of(work, struct sfp, poll.work);
2344
2345 sfp_check_state(sfp);
2346
2347 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
2348 sfp->need_poll)
2349 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2350 }
2351
sfp_alloc(struct device * dev)2352 static struct sfp *sfp_alloc(struct device *dev)
2353 {
2354 struct sfp *sfp;
2355
2356 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
2357 if (!sfp)
2358 return ERR_PTR(-ENOMEM);
2359
2360 sfp->dev = dev;
2361
2362 mutex_init(&sfp->sm_mutex);
2363 mutex_init(&sfp->st_mutex);
2364 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
2365 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
2366
2367 sfp_hwmon_init(sfp);
2368
2369 return sfp;
2370 }
2371
sfp_cleanup(void * data)2372 static void sfp_cleanup(void *data)
2373 {
2374 struct sfp *sfp = data;
2375
2376 sfp_hwmon_exit(sfp);
2377
2378 cancel_delayed_work_sync(&sfp->poll);
2379 cancel_delayed_work_sync(&sfp->timeout);
2380 if (sfp->i2c_mii) {
2381 mdiobus_unregister(sfp->i2c_mii);
2382 mdiobus_free(sfp->i2c_mii);
2383 }
2384 if (sfp->i2c)
2385 i2c_put_adapter(sfp->i2c);
2386 kfree(sfp);
2387 }
2388
sfp_probe(struct platform_device * pdev)2389 static int sfp_probe(struct platform_device *pdev)
2390 {
2391 const struct sff_data *sff;
2392 struct i2c_adapter *i2c;
2393 char *sfp_irq_name;
2394 struct sfp *sfp;
2395 int err, i;
2396
2397 sfp = sfp_alloc(&pdev->dev);
2398 if (IS_ERR(sfp))
2399 return PTR_ERR(sfp);
2400
2401 platform_set_drvdata(pdev, sfp);
2402
2403 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
2404 if (err < 0)
2405 return err;
2406
2407 sff = sfp->type = &sfp_data;
2408
2409 if (pdev->dev.of_node) {
2410 struct device_node *node = pdev->dev.of_node;
2411 const struct of_device_id *id;
2412 struct device_node *np;
2413
2414 id = of_match_node(sfp_of_match, node);
2415 if (WARN_ON(!id))
2416 return -EINVAL;
2417
2418 sff = sfp->type = id->data;
2419
2420 np = of_parse_phandle(node, "i2c-bus", 0);
2421 if (!np) {
2422 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
2423 return -ENODEV;
2424 }
2425
2426 i2c = of_find_i2c_adapter_by_node(np);
2427 of_node_put(np);
2428 } else if (has_acpi_companion(&pdev->dev)) {
2429 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2430 struct fwnode_handle *fw = acpi_fwnode_handle(adev);
2431 struct fwnode_reference_args args;
2432 struct acpi_handle *acpi_handle;
2433 int ret;
2434
2435 ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args);
2436 if (ret || !is_acpi_device_node(args.fwnode)) {
2437 dev_err(&pdev->dev, "missing 'i2c-bus' property\n");
2438 return -ENODEV;
2439 }
2440
2441 acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode);
2442 i2c = i2c_acpi_find_adapter_by_handle(acpi_handle);
2443 } else {
2444 return -EINVAL;
2445 }
2446
2447 if (!i2c)
2448 return -EPROBE_DEFER;
2449
2450 err = sfp_i2c_configure(sfp, i2c);
2451 if (err < 0) {
2452 i2c_put_adapter(i2c);
2453 return err;
2454 }
2455
2456 for (i = 0; i < GPIO_MAX; i++)
2457 if (sff->gpios & BIT(i)) {
2458 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
2459 gpio_of_names[i], gpio_flags[i]);
2460 if (IS_ERR(sfp->gpio[i]))
2461 return PTR_ERR(sfp->gpio[i]);
2462 }
2463
2464 sfp->get_state = sfp_gpio_get_state;
2465 sfp->set_state = sfp_gpio_set_state;
2466
2467 /* Modules that have no detect signal are always present */
2468 if (!(sfp->gpio[GPIO_MODDEF0]))
2469 sfp->get_state = sff_gpio_get_state;
2470
2471 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
2472 &sfp->max_power_mW);
2473 if (!sfp->max_power_mW)
2474 sfp->max_power_mW = 1000;
2475
2476 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
2477 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
2478
2479 /* Get the initial state, and always signal TX disable,
2480 * since the network interface will not be up.
2481 */
2482 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
2483
2484 if (sfp->gpio[GPIO_RATE_SELECT] &&
2485 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
2486 sfp->state |= SFP_F_RATE_SELECT;
2487 sfp_set_state(sfp, sfp->state);
2488 sfp_module_tx_disable(sfp);
2489 if (sfp->state & SFP_F_PRESENT) {
2490 rtnl_lock();
2491 sfp_sm_event(sfp, SFP_E_INSERT);
2492 rtnl_unlock();
2493 }
2494
2495 for (i = 0; i < GPIO_MAX; i++) {
2496 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
2497 continue;
2498
2499 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
2500 if (sfp->gpio_irq[i] < 0) {
2501 sfp->gpio_irq[i] = 0;
2502 sfp->need_poll = true;
2503 continue;
2504 }
2505
2506 sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL,
2507 "%s-%s", dev_name(sfp->dev),
2508 gpio_of_names[i]);
2509
2510 if (!sfp_irq_name)
2511 return -ENOMEM;
2512
2513 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
2514 NULL, sfp_irq,
2515 IRQF_ONESHOT |
2516 IRQF_TRIGGER_RISING |
2517 IRQF_TRIGGER_FALLING,
2518 sfp_irq_name, sfp);
2519 if (err) {
2520 sfp->gpio_irq[i] = 0;
2521 sfp->need_poll = true;
2522 }
2523 }
2524
2525 if (sfp->need_poll)
2526 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2527
2528 /* We could have an issue in cases no Tx disable pin is available or
2529 * wired as modules using a laser as their light source will continue to
2530 * be active when the fiber is removed. This could be a safety issue and
2531 * we should at least warn the user about that.
2532 */
2533 if (!sfp->gpio[GPIO_TX_DISABLE])
2534 dev_warn(sfp->dev,
2535 "No tx_disable pin: SFP modules will always be emitting.\n");
2536
2537 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
2538 if (!sfp->sfp_bus)
2539 return -ENOMEM;
2540
2541 return 0;
2542 }
2543
sfp_remove(struct platform_device * pdev)2544 static int sfp_remove(struct platform_device *pdev)
2545 {
2546 struct sfp *sfp = platform_get_drvdata(pdev);
2547
2548 sfp_unregister_socket(sfp->sfp_bus);
2549
2550 rtnl_lock();
2551 sfp_sm_event(sfp, SFP_E_REMOVE);
2552 rtnl_unlock();
2553
2554 return 0;
2555 }
2556
sfp_shutdown(struct platform_device * pdev)2557 static void sfp_shutdown(struct platform_device *pdev)
2558 {
2559 struct sfp *sfp = platform_get_drvdata(pdev);
2560 int i;
2561
2562 for (i = 0; i < GPIO_MAX; i++) {
2563 if (!sfp->gpio_irq[i])
2564 continue;
2565
2566 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
2567 }
2568
2569 cancel_delayed_work_sync(&sfp->poll);
2570 cancel_delayed_work_sync(&sfp->timeout);
2571 }
2572
2573 static struct platform_driver sfp_driver = {
2574 .probe = sfp_probe,
2575 .remove = sfp_remove,
2576 .shutdown = sfp_shutdown,
2577 .driver = {
2578 .name = "sfp",
2579 .of_match_table = sfp_of_match,
2580 },
2581 };
2582
sfp_init(void)2583 static int sfp_init(void)
2584 {
2585 poll_jiffies = msecs_to_jiffies(100);
2586
2587 return platform_driver_register(&sfp_driver);
2588 }
2589 module_init(sfp_init);
2590
sfp_exit(void)2591 static void sfp_exit(void)
2592 {
2593 platform_driver_unregister(&sfp_driver);
2594 }
2595 module_exit(sfp_exit);
2596
2597 MODULE_ALIAS("platform:sfp");
2598 MODULE_AUTHOR("Russell King");
2599 MODULE_LICENSE("GPL v2");
2600