1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/debugfs.h>
3 #include <linux/delay.h>
4 #include <linux/gpio/consumer.h>
5 #include <linux/hwmon.h>
6 #include <linux/i2c.h>
7 #include <linux/interrupt.h>
8 #include <linux/jiffies.h>
9 #include <linux/mdio/mdio-i2c.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/of.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/slab.h>
17 #include <linux/unaligned.h>
18 #include <linux/workqueue.h>
19
20 #include "sfp.h"
21
22 enum {
23 GPIO_MODDEF0,
24 GPIO_LOS,
25 GPIO_TX_FAULT,
26 GPIO_TX_DISABLE,
27 GPIO_RS0,
28 GPIO_RS1,
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_RS0 = BIT(GPIO_RS0),
36 SFP_F_RS1 = BIT(GPIO_RS1),
37
38 SFP_F_OUTPUTS = SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1,
39
40 SFP_E_INSERT = 0,
41 SFP_E_REMOVE,
42 SFP_E_DEV_ATTACH,
43 SFP_E_DEV_DETACH,
44 SFP_E_DEV_DOWN,
45 SFP_E_DEV_UP,
46 SFP_E_TX_FAULT,
47 SFP_E_TX_CLEAR,
48 SFP_E_LOS_HIGH,
49 SFP_E_LOS_LOW,
50 SFP_E_TIMEOUT,
51
52 SFP_MOD_EMPTY = 0,
53 SFP_MOD_ERROR,
54 SFP_MOD_PROBE,
55 SFP_MOD_WAITDEV,
56 SFP_MOD_HPOWER,
57 SFP_MOD_WAITPWR,
58 SFP_MOD_PRESENT,
59
60 SFP_DEV_DETACHED = 0,
61 SFP_DEV_DOWN,
62 SFP_DEV_UP,
63
64 SFP_S_DOWN = 0,
65 SFP_S_FAIL,
66 SFP_S_WAIT,
67 SFP_S_INIT,
68 SFP_S_INIT_PHY,
69 SFP_S_INIT_TX_FAULT,
70 SFP_S_WAIT_LOS,
71 SFP_S_LINK_UP,
72 SFP_S_TX_FAULT,
73 SFP_S_REINIT,
74 SFP_S_TX_DISABLE,
75 };
76
77 static const char * const mod_state_strings[] = {
78 [SFP_MOD_EMPTY] = "empty",
79 [SFP_MOD_ERROR] = "error",
80 [SFP_MOD_PROBE] = "probe",
81 [SFP_MOD_WAITDEV] = "waitdev",
82 [SFP_MOD_HPOWER] = "hpower",
83 [SFP_MOD_WAITPWR] = "waitpwr",
84 [SFP_MOD_PRESENT] = "present",
85 };
86
mod_state_to_str(unsigned short mod_state)87 static const char *mod_state_to_str(unsigned short mod_state)
88 {
89 if (mod_state >= ARRAY_SIZE(mod_state_strings))
90 return "Unknown module state";
91 return mod_state_strings[mod_state];
92 }
93
94 static const char * const dev_state_strings[] = {
95 [SFP_DEV_DETACHED] = "detached",
96 [SFP_DEV_DOWN] = "down",
97 [SFP_DEV_UP] = "up",
98 };
99
dev_state_to_str(unsigned short dev_state)100 static const char *dev_state_to_str(unsigned short dev_state)
101 {
102 if (dev_state >= ARRAY_SIZE(dev_state_strings))
103 return "Unknown device state";
104 return dev_state_strings[dev_state];
105 }
106
107 static const char * const event_strings[] = {
108 [SFP_E_INSERT] = "insert",
109 [SFP_E_REMOVE] = "remove",
110 [SFP_E_DEV_ATTACH] = "dev_attach",
111 [SFP_E_DEV_DETACH] = "dev_detach",
112 [SFP_E_DEV_DOWN] = "dev_down",
113 [SFP_E_DEV_UP] = "dev_up",
114 [SFP_E_TX_FAULT] = "tx_fault",
115 [SFP_E_TX_CLEAR] = "tx_clear",
116 [SFP_E_LOS_HIGH] = "los_high",
117 [SFP_E_LOS_LOW] = "los_low",
118 [SFP_E_TIMEOUT] = "timeout",
119 };
120
event_to_str(unsigned short event)121 static const char *event_to_str(unsigned short event)
122 {
123 if (event >= ARRAY_SIZE(event_strings))
124 return "Unknown event";
125 return event_strings[event];
126 }
127
128 static const char * const sm_state_strings[] = {
129 [SFP_S_DOWN] = "down",
130 [SFP_S_FAIL] = "fail",
131 [SFP_S_WAIT] = "wait",
132 [SFP_S_INIT] = "init",
133 [SFP_S_INIT_PHY] = "init_phy",
134 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
135 [SFP_S_WAIT_LOS] = "wait_los",
136 [SFP_S_LINK_UP] = "link_up",
137 [SFP_S_TX_FAULT] = "tx_fault",
138 [SFP_S_REINIT] = "reinit",
139 [SFP_S_TX_DISABLE] = "tx_disable",
140 };
141
sm_state_to_str(unsigned short sm_state)142 static const char *sm_state_to_str(unsigned short sm_state)
143 {
144 if (sm_state >= ARRAY_SIZE(sm_state_strings))
145 return "Unknown state";
146 return sm_state_strings[sm_state];
147 }
148
149 static const char *gpio_names[] = {
150 "mod-def0",
151 "los",
152 "tx-fault",
153 "tx-disable",
154 "rate-select0",
155 "rate-select1",
156 };
157
158 static const enum gpiod_flags gpio_flags[] = {
159 GPIOD_IN,
160 GPIOD_IN,
161 GPIOD_IN,
162 GPIOD_ASIS,
163 GPIOD_ASIS,
164 GPIOD_ASIS,
165 };
166
167 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a
168 * non-cooled module to initialise its laser safety circuitry. We wait
169 * an initial T_WAIT period before we check the tx fault to give any PHY
170 * on board (for a copper SFP) time to initialise.
171 */
172 #define T_WAIT msecs_to_jiffies(50)
173 #define T_START_UP msecs_to_jiffies(300)
174 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
175
176 /* t_reset is the time required to assert the TX_DISABLE signal to reset
177 * an indicated TX_FAULT.
178 */
179 #define T_RESET_US 10
180 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
181
182 /* N_FAULT_INIT is the number of recovery attempts at module initialisation
183 * time. If the TX_FAULT signal is not deasserted after this number of
184 * attempts at clearing it, we decide that the module is faulty.
185 * N_FAULT is the same but after the module has initialised.
186 */
187 #define N_FAULT_INIT 5
188 #define N_FAULT 5
189
190 /* T_PHY_RETRY is the time interval between attempts to probe the PHY.
191 * R_PHY_RETRY is the number of attempts.
192 */
193 #define T_PHY_RETRY msecs_to_jiffies(50)
194 #define R_PHY_RETRY 25
195
196 /* SFP module presence detection is poor: the three MOD DEF signals are
197 * the same length on the PCB, which means it's possible for MOD DEF 0 to
198 * connect before the I2C bus on MOD DEF 1/2.
199 *
200 * The SFF-8472 specifies t_serial ("Time from power on until module is
201 * ready for data transmission over the two wire serial bus.") as 300ms.
202 */
203 #define T_SERIAL msecs_to_jiffies(300)
204 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
205 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
206 #define R_PROBE_RETRY_INIT 10
207 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
208 #define R_PROBE_RETRY_SLOW 12
209
210 /* Polling interval and consecutive-failure threshold for the I2C presence
211 * probe used on boards without a MOD_DEF0 GPIO (see sfp_i2c_get_state()).
212 * A single successful read asserts presence immediately; R_PROBE_ABSENT
213 * consecutive failures are required to declare a live module removed, to ride
214 * out a transient I2C error. Insertion is thus detected within
215 * T_PROBE_PRESENT and removal within T_PROBE_PRESENT * R_PROBE_ABSENT.
216 */
217 #define T_PROBE_PRESENT msecs_to_jiffies(500)
218 #define R_PROBE_ABSENT 3
219
220 /* SFP modules appear to always have their PHY configured for bus address
221 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
222 * RollBall SFPs access phy via SFP Enhanced Digital Diagnostic Interface
223 * via address 0x51 (mdio-i2c will use RollBall protocol on this address).
224 */
225 #define SFP_PHY_ADDR 22
226 #define SFP_PHY_ADDR_ROLLBALL 17
227
228 /* SFP_EEPROM_BLOCK_SIZE is the size of data chunk to read the EEPROM
229 * at a time. Some SFP modules and also some Linux I2C drivers do not like
230 * reads longer than 16 bytes.
231 */
232 #define SFP_EEPROM_BLOCK_SIZE 16
233
234 #define SFP_POLL_INTERVAL msecs_to_jiffies(100)
235
236 struct sff_data {
237 unsigned int gpios;
238 bool (*module_supported)(const struct sfp_eeprom_id *id);
239 };
240
241 struct sfp {
242 struct device *dev;
243 struct i2c_adapter *i2c;
244 struct mii_bus *i2c_mii;
245 struct sfp_bus *sfp_bus;
246 enum mdio_i2c_proto mdio_protocol;
247 struct phy_device *mod_phy;
248 const struct sff_data *type;
249 size_t i2c_max_block_size;
250 size_t i2c_block_size;
251 u32 max_power_mW;
252
253 unsigned int (*get_state)(struct sfp *);
254 void (*set_state)(struct sfp *, unsigned int);
255 int (*read)(struct sfp *, bool, u8, void *, size_t);
256 int (*write)(struct sfp *, bool, u8, void *, size_t);
257
258 struct gpio_desc *gpio[GPIO_MAX];
259 int gpio_irq[GPIO_MAX];
260
261 bool need_poll;
262
263 /* I2C-probed presence, for boards without a MOD_DEF0 GPIO.
264 * Access rules: st_mutex held (updated from the poll/state machine).
265 */
266 bool i2c_present;
267 u8 i2c_present_nak;
268 unsigned long i2c_present_next;
269
270 /* Access rules:
271 * state_hw_drive: st_mutex held
272 * state_hw_mask: st_mutex held
273 * state_soft_mask: st_mutex held
274 * state: st_mutex held unless reading input bits
275 */
276 struct mutex st_mutex; /* Protects state */
277 unsigned int state_hw_drive;
278 unsigned int state_hw_mask;
279 unsigned int state_soft_mask;
280 unsigned int state_ignore_mask;
281 unsigned int state;
282
283 struct delayed_work poll;
284 struct delayed_work timeout;
285 struct mutex sm_mutex; /* Protects state machine */
286 unsigned char sm_mod_state;
287 unsigned char sm_mod_tries_init;
288 unsigned char sm_mod_tries;
289 unsigned char sm_dev_state;
290 unsigned short sm_state;
291 unsigned char sm_fault_retries;
292 unsigned char sm_phy_retries;
293
294 struct sfp_eeprom_id id;
295 unsigned int module_power_mW;
296 unsigned int module_t_start_up;
297 unsigned int module_t_wait;
298 unsigned int phy_t_retry;
299
300 unsigned int rate_kbd;
301 unsigned int rs_threshold_kbd;
302 unsigned int rs_state_mask;
303
304 bool have_a2;
305
306 const struct sfp_quirk *quirk;
307
308 #if IS_ENABLED(CONFIG_HWMON)
309 struct sfp_diag diag;
310 struct delayed_work hwmon_probe;
311 unsigned int hwmon_tries;
312 struct device *hwmon_dev;
313 char *hwmon_name;
314 #endif
315
316 #if IS_ENABLED(CONFIG_DEBUG_FS)
317 struct dentry *debugfs_dir;
318 #endif
319 };
320
sfp_schedule_poll(struct sfp * sfp)321 static void sfp_schedule_poll(struct sfp *sfp)
322 {
323 mod_delayed_work(system_percpu_wq, &sfp->poll, SFP_POLL_INTERVAL);
324 }
325
sff_module_supported(const struct sfp_eeprom_id * id)326 static bool sff_module_supported(const struct sfp_eeprom_id *id)
327 {
328 return id->base.phys_id == SFF8024_ID_SFF_8472 &&
329 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
330 }
331
332 static const struct sff_data sff_data = {
333 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
334 .module_supported = sff_module_supported,
335 };
336
sfp_module_supported(const struct sfp_eeprom_id * id)337 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
338 {
339 if (id->base.phys_id == SFF8024_ID_SFP &&
340 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP)
341 return true;
342
343 /* SFP GPON module Ubiquiti U-Fiber Instant has in its EEPROM stored
344 * phys id SFF instead of SFP. Therefore mark this module explicitly
345 * as supported based on vendor name and pn match.
346 */
347 if (id->base.phys_id == SFF8024_ID_SFF_8472 &&
348 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP &&
349 !memcmp(id->base.vendor_name, "UBNT ", 16) &&
350 !memcmp(id->base.vendor_pn, "UF-INSTANT ", 16))
351 return true;
352
353 return false;
354 }
355
356 static const struct sff_data sfp_data = {
357 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
358 SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1,
359 .module_supported = sfp_module_supported,
360 };
361
362 static const struct of_device_id sfp_of_match[] = {
363 { .compatible = "sff,sff", .data = &sff_data, },
364 { .compatible = "sff,sfp", .data = &sfp_data, },
365 { },
366 };
367 MODULE_DEVICE_TABLE(of, sfp_of_match);
368
sfp_fixup_long_startup(struct sfp * sfp)369 static void sfp_fixup_long_startup(struct sfp *sfp)
370 {
371 sfp->module_t_start_up = T_START_UP_BAD_GPON;
372 }
373
sfp_fixup_ignore_los(struct sfp * sfp)374 static void sfp_fixup_ignore_los(struct sfp *sfp)
375 {
376 /* This forces LOS to zero, so we ignore transitions */
377 sfp->state_ignore_mask |= SFP_F_LOS;
378 /* Make sure that LOS options are clear */
379 sfp->id.ext.options &= ~cpu_to_be16(SFP_OPTIONS_LOS_INVERTED |
380 SFP_OPTIONS_LOS_NORMAL);
381 }
382
sfp_fixup_ignore_tx_fault(struct sfp * sfp)383 static void sfp_fixup_ignore_tx_fault(struct sfp *sfp)
384 {
385 sfp->state_ignore_mask |= SFP_F_TX_FAULT;
386 }
387
sfp_fixup_ignore_tx_fault_and_los(struct sfp * sfp)388 static void sfp_fixup_ignore_tx_fault_and_los(struct sfp *sfp)
389 {
390 sfp_fixup_ignore_tx_fault(sfp);
391 sfp_fixup_ignore_los(sfp);
392 }
393
sfp_fixup_ignore_hw(struct sfp * sfp,unsigned int mask)394 static void sfp_fixup_ignore_hw(struct sfp *sfp, unsigned int mask)
395 {
396 sfp->state_hw_mask &= ~mask;
397 }
398
sfp_fixup_nokia(struct sfp * sfp)399 static void sfp_fixup_nokia(struct sfp *sfp)
400 {
401 sfp_fixup_long_startup(sfp);
402 sfp_fixup_ignore_los(sfp);
403 }
404
405 // For 10GBASE-T short-reach modules
sfp_fixup_10gbaset_30m(struct sfp * sfp)406 static void sfp_fixup_10gbaset_30m(struct sfp *sfp)
407 {
408 sfp->id.base.connector = SFF8024_CONNECTOR_RJ45;
409 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SR;
410 }
411
sfp_fixup_rollball(struct sfp * sfp)412 static void sfp_fixup_rollball(struct sfp *sfp)
413 {
414 sfp->mdio_protocol = MDIO_I2C_ROLLBALL;
415
416 /* RollBall modules may disallow access to PHY registers for up to 25
417 * seconds, and the reads return 0xffff before that. Increase the time
418 * between PHY probe retries from 50ms to 1s so that we will wait for
419 * the PHY for a sufficient amount of time.
420 */
421 sfp->phy_t_retry = msecs_to_jiffies(1000);
422 }
423
sfp_fixup_rollball_wait4s(struct sfp * sfp)424 static void sfp_fixup_rollball_wait4s(struct sfp *sfp)
425 {
426 sfp_fixup_rollball(sfp);
427
428 /* The RollBall fixup is not enough for FS modules, the PHY chip inside
429 * them does not return 0xffff for PHY ID registers in all MMDs for the
430 * while initializing. They need a 4 second wait before accessing PHY.
431 */
432 sfp->module_t_wait = msecs_to_jiffies(4000);
433 }
434
sfp_fixup_fs_10gt(struct sfp * sfp)435 static void sfp_fixup_fs_10gt(struct sfp *sfp)
436 {
437 sfp_fixup_10gbaset_30m(sfp);
438 sfp_fixup_rollball_wait4s(sfp);
439 }
440
sfp_fixup_halny_gsfp(struct sfp * sfp)441 static void sfp_fixup_halny_gsfp(struct sfp *sfp)
442 {
443 /* Ignore the TX_FAULT and LOS signals on this module.
444 * these are possibly used for other purposes on this
445 * module, e.g. a serial port.
446 */
447 sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS);
448 }
449
sfp_fixup_potron(struct sfp * sfp)450 static void sfp_fixup_potron(struct sfp *sfp)
451 {
452 /*
453 * The TX_FAULT and LOS pins on this device are used for serial
454 * communication, so ignore them. Additionally, provide extra
455 * time for this device to fully start up.
456 */
457
458 sfp_fixup_long_startup(sfp);
459 sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS);
460 }
461
sfp_fixup_rollball_cc(struct sfp * sfp)462 static void sfp_fixup_rollball_cc(struct sfp *sfp)
463 {
464 sfp_fixup_rollball(sfp);
465
466 /* Some RollBall SFPs may have wrong (zero) extended compliance code
467 * burned in EEPROM. For PHY probing we need the correct one.
468 */
469 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SFI;
470 }
471
sfp_quirk_2500basex(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)472 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
473 struct sfp_module_caps *caps)
474 {
475 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
476 caps->link_modes);
477 __set_bit(PHY_INTERFACE_MODE_2500BASEX, caps->interfaces);
478 }
479
sfp_quirk_disable_autoneg(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)480 static void sfp_quirk_disable_autoneg(const struct sfp_eeprom_id *id,
481 struct sfp_module_caps *caps)
482 {
483 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, caps->link_modes);
484 }
485
sfp_quirk_oem_2_5g(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)486 static void sfp_quirk_oem_2_5g(const struct sfp_eeprom_id *id,
487 struct sfp_module_caps *caps)
488 {
489 /* Copper 2.5G SFP */
490 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
491 caps->link_modes);
492 __set_bit(PHY_INTERFACE_MODE_2500BASEX, caps->interfaces);
493 sfp_quirk_disable_autoneg(id, caps);
494 }
495
sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id * id,struct sfp_module_caps * caps)496 static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
497 struct sfp_module_caps *caps)
498 {
499 /* Ubiquiti U-Fiber Instant module claims that support all transceiver
500 * types including 10G Ethernet which is not truth. So clear all claimed
501 * modes and set only one mode which module supports: 1000baseX_Full,
502 * along with the Autoneg and pause bits.
503 */
504 linkmode_zero(caps->link_modes);
505 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
506 caps->link_modes);
507 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, caps->link_modes);
508 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, caps->link_modes);
509 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, caps->link_modes);
510
511 phy_interface_zero(caps->interfaces);
512 __set_bit(PHY_INTERFACE_MODE_1000BASEX, caps->interfaces);
513 }
514
515 #define SFP_QUIRK(_v, _p, _s, _f) \
516 { .vendor = _v, .part = _p, .support = _s, .fixup = _f, }
517 #define SFP_QUIRK_S(_v, _p, _s) SFP_QUIRK(_v, _p, _s, NULL)
518 #define SFP_QUIRK_F(_v, _p, _f) SFP_QUIRK(_v, _p, NULL, _f)
519 /* Like SFP_QUIRK_F, but matches the part as a prefix; the vendor name
520 * is still matched exactly. Use for modules whose EEPROM vendor PN
521 * field reads back with garbage past the legitimate characters instead
522 * of the SFF-8472-mandated space padding, so sfp_strlen can't trim the
523 * field down to the legitimate length.
524 */
525 #define SFP_QUIRK_F_PREFIX(_v, _p, _f) \
526 { .vendor = _v, .part = _p, .support = NULL, .fixup = _f, \
527 .part_prefix_match = true }
528
529 static const struct sfp_quirk sfp_quirks[] = {
530 // Alcatel Lucent G-010S-P can operate at 2500base-X, but incorrectly
531 // report 2500MBd NRZ in their EEPROM
532 SFP_QUIRK("ALCATELLUCENT", "G010SP", sfp_quirk_2500basex,
533 sfp_fixup_ignore_tx_fault),
534
535 // Alcatel Lucent G-010S-A can operate at 2500base-X, but report 3.2GBd
536 // NRZ in their EEPROM
537 SFP_QUIRK("ALCATELLUCENT", "3FE46541AA", sfp_quirk_2500basex,
538 sfp_fixup_nokia),
539
540 SFP_QUIRK_F("BIDB", "X-ONU-SFPP", sfp_fixup_potron),
541
542 // FLYPRO SFP-10GT-CS-30M uses Rollball protocol to talk to the PHY.
543 SFP_QUIRK_F("FLYPRO", "SFP-10GT-CS-30M", sfp_fixup_rollball),
544
545 // Fiberstore SFP-10G-T doesn't identify as copper, uses the Rollball
546 // protocol to talk to the PHY and needs 4 sec wait before probing the
547 // PHY.
548 SFP_QUIRK_F("FS", "SFP-10G-T", sfp_fixup_fs_10gt),
549
550 // Fiberstore SFP-2.5G-T and SFP-10GM-T uses Rollball protocol to talk
551 // to the PHY and needs 4 sec wait before probing the PHY.
552 SFP_QUIRK_F("FS", "SFP-2.5G-T", sfp_fixup_rollball_wait4s),
553 SFP_QUIRK_F("FS", "SFP-10GM-T", sfp_fixup_rollball_wait4s),
554
555 // Fiberstore GPON-ONU-34-20BI can operate at 2500base-X, but report 1.2GBd
556 // NRZ in their EEPROM
557 SFP_QUIRK("FS", "GPON-ONU-34-20BI", sfp_quirk_2500basex,
558 sfp_fixup_ignore_tx_fault),
559
560 // Fiberstore XGS-SFP-ONT-MACI is a MAC-mode XGS-PON ONT stick with
561 // ONT-class serial-passthrough TX_FAULT/LOS wiring and slow startup;
562 // mask both signals and extend T_START_UP via the potron fixup. The
563 // PN is the product name (XGS-SFP-ONT-MAC-I) truncated at the 16-byte
564 // field width, so the field is fully occupied and matches exactly.
565 SFP_QUIRK_F("FS", "XGS-SFP-ONT-MACI", sfp_fixup_potron),
566
567 SFP_QUIRK_F("HALNy", "HL-GSFP", sfp_fixup_halny_gsfp),
568
569 SFP_QUIRK_F("H-COM", "SPP425H-GAB4", sfp_fixup_potron),
570
571 // HG MXPD-483II-F 2.5G supports 2500Base-X, but incorrectly reports
572 // 2600MBd in their EERPOM
573 SFP_QUIRK_S("HG GENUINE", "MXPD-483II", sfp_quirk_2500basex),
574
575 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd NRZ in
576 // their EEPROM
577 SFP_QUIRK("HUAWEI", "MA5671A", sfp_quirk_2500basex,
578 sfp_fixup_ignore_tx_fault_and_los),
579
580 // Hisense LXT-010S-H is a GPON ONT SFP (sold as LEOX LXT-010S-H) that
581 // can operate at 2500base-X, but reports 1000BASE-LX / 1300MBd in its
582 // EEPROM
583 SFP_QUIRK("Hisense-Leox", "LXT-010S-H", sfp_quirk_2500basex,
584 sfp_fixup_ignore_tx_fault),
585
586 // Hisense ZNID-GPON-2311NA can operate at 2500base-X, but reports
587 // 1000BASE-LX / 1300MBd in its EEPROM
588 SFP_QUIRK("Hisense", "ZNID-GPON-2311NA", sfp_quirk_2500basex,
589 sfp_fixup_ignore_tx_fault),
590
591 // HSGQ HSGQ-XPON-Stick can operate at 2500base-X, but reports
592 // 1000BASE-LX / 1300MBd in its EEPROM
593 SFP_QUIRK("HSGQ", "HSGQ-XPON-Stick", sfp_quirk_2500basex,
594 sfp_fixup_ignore_tx_fault),
595
596 // Lantech 8330-262D-E and 8330-265D can operate at 2500base-X, but
597 // incorrectly report 2500MBd NRZ in their EEPROM.
598 // Some 8330-265D modules have inverted LOS, while all of them report
599 // normal LOS in EEPROM. Therefore we need to ignore LOS entirely.
600 SFP_QUIRK_S("Lantech", "8330-262D-E", sfp_quirk_2500basex),
601 SFP_QUIRK("Lantech", "8330-265D", sfp_quirk_2500basex,
602 sfp_fixup_ignore_los),
603
604 SFP_QUIRK_S("UBNT", "UF-INSTANT", sfp_quirk_ubnt_uf_instant),
605
606 // Walsun HXSX-ATR[CI]-1 don't identify as copper, and use the
607 // Rollball protocol to talk to the PHY.
608 SFP_QUIRK_F("Walsun", "HXSX-ATRC-1", sfp_fixup_fs_10gt),
609 SFP_QUIRK_F("Walsun", "HXSX-ATRI-1", sfp_fixup_fs_10gt),
610
611 SFP_QUIRK_F("YV", "SFP+ONU-XGSPON", sfp_fixup_potron),
612
613 // HORACO HC-10GE-113C uses Rollball protocol to talk to the PHY.
614 SFP_QUIRK_F("OEM", "HC-10GE-113C", sfp_fixup_rollball),
615
616 // OEM SFP-GE-T is a 1000Base-T module with broken TX_FAULT indicator
617 SFP_QUIRK_F("OEM", "SFP-GE-T", sfp_fixup_ignore_tx_fault),
618
619 SFP_QUIRK_F("OEM", "SFP-10G-T", sfp_fixup_rollball_cc),
620 SFP_QUIRK_S("OEM", "SFP-2.5G-T", sfp_quirk_oem_2_5g),
621 SFP_QUIRK_S("OEM", "SFP-2.5G-BX10-D", sfp_quirk_2500basex),
622 SFP_QUIRK_S("OEM", "SFP-2.5G-BX10-U", sfp_quirk_2500basex),
623 SFP_QUIRK_S("OEM", "SFP-2.5G-LH03-B", sfp_quirk_2500basex),
624 SFP_QUIRK_S("OEM", "SFP-2.5G-LH20-A", sfp_quirk_2500basex),
625 SFP_QUIRK_F("OEM", "RTSFP-10", sfp_fixup_rollball_cc),
626 SFP_QUIRK_F("OEM", "RTSFP-10G", sfp_fixup_rollball_cc),
627
628 // OEM XGSPONST2001 is an XGS-PON ONT stick with broken TX_FAULT and
629 // LOS indicators and slow startup, just like potron. On cold
630 // power-up the EEPROM vendor PN field reads back with non-printable
631 // garbage past the legitimate string instead of space padding, so
632 // match the part as a prefix.
633 SFP_QUIRK_F_PREFIX("OEM", "XGSPONST2001", sfp_fixup_potron),
634
635 SFP_QUIRK_F("Turris", "RTSFP-2.5G", sfp_fixup_rollball),
636 SFP_QUIRK_F("Turris", "RTSFP-10", sfp_fixup_rollball),
637 SFP_QUIRK_F("Turris", "RTSFP-10G", sfp_fixup_rollball),
638
639 SFP_QUIRK_S("ZOERAX", "SFP-2.5G-T", sfp_quirk_oem_2_5g),
640 };
641
sfp_strlen(const char * str,size_t maxlen)642 static size_t sfp_strlen(const char *str, size_t maxlen)
643 {
644 size_t size, i;
645
646 /* Trailing characters should be filled with space chars, but
647 * some manufacturers can't read SFF-8472 and use NUL.
648 */
649 for (i = 0, size = 0; i < maxlen; i++)
650 if (str[i] != ' ' && str[i] != '\0')
651 size = i + 1;
652
653 return size;
654 }
655
sfp_match(const char * qs,const char * str,size_t len,bool prefix)656 static bool sfp_match(const char *qs, const char *str, size_t len, bool prefix)
657 {
658 size_t qs_len;
659
660 if (!qs)
661 return true;
662 qs_len = strlen(qs);
663 if (prefix ? qs_len > len : qs_len != len)
664 return false;
665 return !strncmp(qs, str, qs_len);
666 }
667
sfp_lookup_quirk(const struct sfp_eeprom_id * id)668 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
669 {
670 const struct sfp_quirk *q;
671 unsigned int i;
672 size_t vs, ps;
673
674 vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
675 ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
676
677 for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
678 if (sfp_match(q->vendor, id->base.vendor_name, vs, false) &&
679 sfp_match(q->part, id->base.vendor_pn, ps,
680 q->part_prefix_match))
681 return q;
682
683 return NULL;
684 }
685
sfp_gpio_get_state(struct sfp * sfp)686 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
687 {
688 unsigned int i, state, v;
689
690 for (i = state = 0; i < GPIO_MAX; i++) {
691 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
692 continue;
693
694 v = gpiod_get_value_cansleep(sfp->gpio[i]);
695 if (v)
696 state |= BIT(i);
697 }
698
699 return state;
700 }
701
sff_gpio_get_state(struct sfp * sfp)702 static unsigned int sff_gpio_get_state(struct sfp *sfp)
703 {
704 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
705 }
706
sfp_gpio_set_state(struct sfp * sfp,unsigned int state)707 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
708 {
709 unsigned int drive;
710
711 if (state & SFP_F_PRESENT)
712 /* If the module is present, drive the requested signals */
713 drive = sfp->state_hw_drive;
714 else
715 /* Otherwise, let them float to the pull-ups */
716 drive = 0;
717
718 if (sfp->gpio[GPIO_TX_DISABLE]) {
719 if (drive & SFP_F_TX_DISABLE)
720 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
721 state & SFP_F_TX_DISABLE);
722 else
723 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
724 }
725
726 if (sfp->gpio[GPIO_RS0]) {
727 if (drive & SFP_F_RS0)
728 gpiod_direction_output(sfp->gpio[GPIO_RS0],
729 state & SFP_F_RS0);
730 else
731 gpiod_direction_input(sfp->gpio[GPIO_RS0]);
732 }
733
734 if (sfp->gpio[GPIO_RS1]) {
735 if (drive & SFP_F_RS1)
736 gpiod_direction_output(sfp->gpio[GPIO_RS1],
737 state & SFP_F_RS1);
738 else
739 gpiod_direction_input(sfp->gpio[GPIO_RS1]);
740 }
741 }
742
sfp_i2c_read(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)743 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
744 size_t len)
745 {
746 struct i2c_msg msgs[2];
747 u8 bus_addr = a2 ? 0x51 : 0x50;
748 size_t block_size = sfp->i2c_block_size;
749 size_t this_len;
750 int ret;
751
752 msgs[0].addr = bus_addr;
753 msgs[0].flags = 0;
754 msgs[0].len = 1;
755 msgs[0].buf = &dev_addr;
756 msgs[1].addr = bus_addr;
757 msgs[1].flags = I2C_M_RD;
758 msgs[1].len = len;
759 msgs[1].buf = buf;
760
761 while (len) {
762 this_len = len;
763 if (this_len > block_size)
764 this_len = block_size;
765
766 msgs[1].len = this_len;
767
768 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
769 if (ret < 0)
770 return ret;
771
772 if (ret != ARRAY_SIZE(msgs))
773 break;
774
775 msgs[1].buf += this_len;
776 dev_addr += this_len;
777 len -= this_len;
778 }
779
780 return msgs[1].buf - (u8 *)buf;
781 }
782
sfp_i2c_write(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)783 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
784 size_t len)
785 {
786 struct i2c_msg msgs[1];
787 u8 bus_addr = a2 ? 0x51 : 0x50;
788 int ret;
789
790 msgs[0].addr = bus_addr;
791 msgs[0].flags = 0;
792 msgs[0].len = 1 + len;
793 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
794 if (!msgs[0].buf)
795 return -ENOMEM;
796
797 msgs[0].buf[0] = dev_addr;
798 memcpy(&msgs[0].buf[1], buf, len);
799
800 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
801
802 kfree(msgs[0].buf);
803
804 if (ret < 0)
805 return ret;
806
807 return ret == ARRAY_SIZE(msgs) ? len : 0;
808 }
809
sfp_smbus_read(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)810 static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
811 size_t len)
812 {
813 union i2c_smbus_data smbus_data = {0};
814 u8 bus_addr = a2 ? 0x51 : 0x50;
815 size_t this_len, transferred;
816 u32 functionality;
817 u8 *data = buf;
818 int ret;
819
820 functionality = i2c_get_functionality(sfp->i2c);
821
822 while (len) {
823 this_len = min(len, sfp->i2c_block_size);
824
825 if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
826 smbus_data.block[0] = this_len;
827 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
828 I2C_SMBUS_READ, dev_addr,
829 I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
830 if (ret < 0)
831 return ret;
832
833 transferred = min_t(size_t, smbus_data.block[0], this_len);
834 if (!transferred)
835 return -EIO;
836
837 memcpy(data, &smbus_data.block[1], transferred);
838 } else if (this_len >= 2 &&
839 (functionality & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
840 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
841 I2C_SMBUS_READ, dev_addr,
842 I2C_SMBUS_WORD_DATA, &smbus_data);
843 if (ret < 0)
844 return ret;
845
846 put_unaligned_le16(smbus_data.word, data);
847 transferred = 2;
848 } else {
849 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
850 I2C_SMBUS_READ, dev_addr,
851 I2C_SMBUS_BYTE_DATA, &smbus_data);
852 if (ret < 0)
853 return ret;
854
855 *data = smbus_data.byte;
856 transferred = 1;
857 }
858
859 data += transferred;
860 len -= transferred;
861 dev_addr += transferred;
862 }
863
864 return data - (u8 *)buf;
865 }
866
sfp_smbus_write(struct sfp * sfp,bool a2,u8 dev_addr,void * buf,size_t len)867 static int sfp_smbus_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
868 size_t len)
869 {
870 union i2c_smbus_data smbus_data;
871 u8 bus_addr = a2 ? 0x51 : 0x50;
872 size_t this_len, transferred;
873 u32 functionality;
874 u8 *data = buf;
875 int ret;
876
877 functionality = i2c_get_functionality(sfp->i2c);
878
879 while (len) {
880 this_len = min(len, sfp->i2c_block_size);
881
882 if (functionality & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) {
883 smbus_data.block[0] = this_len;
884 memcpy(&smbus_data.block[1], data, this_len);
885
886 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
887 I2C_SMBUS_WRITE, dev_addr,
888 I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
889 if (ret < 0)
890 return ret;
891
892 transferred = this_len;
893 } else if (this_len >= 2 &&
894 (functionality & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
895 smbus_data.word = get_unaligned_le16(data);
896 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
897 I2C_SMBUS_WRITE, dev_addr,
898 I2C_SMBUS_WORD_DATA, &smbus_data);
899 if (ret < 0)
900 return ret;
901
902 transferred = 2;
903 } else {
904 smbus_data.byte = *data;
905 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
906 I2C_SMBUS_WRITE, dev_addr,
907 I2C_SMBUS_BYTE_DATA, &smbus_data);
908 if (ret < 0)
909 return ret;
910
911 transferred = 1;
912 }
913
914 data += transferred;
915 len -= transferred;
916 dev_addr += transferred;
917 }
918
919 return data - (u8 *)buf;
920 }
921
sfp_i2c_configure(struct sfp * sfp,struct i2c_adapter * i2c)922 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
923 {
924 size_t max_block_size;
925
926 sfp->i2c = i2c;
927
928 if (i2c_check_functionality(i2c, I2C_FUNC_I2C)) {
929 sfp->read = sfp_i2c_read;
930 sfp->write = sfp_i2c_write;
931 max_block_size = SFP_EEPROM_BLOCK_SIZE;
932 } else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) ||
933 i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) {
934 /* Either protocol alone covers any length: I2C-block carries
935 * 1..32 bytes per xfer, byte iterates one byte at a time.
936 */
937 sfp->read = sfp_smbus_read;
938 sfp->write = sfp_smbus_write;
939
940 if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK))
941 max_block_size = SFP_EEPROM_BLOCK_SIZE;
942 else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA))
943 max_block_size = 2;
944 else
945 max_block_size = 1;
946 } else if (WARN_ONCE(i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA),
947 "SMBus word-only adapter; odd-length transfers will fail\n")) {
948 /* Word-only: even-length xfers work; odd-length xfers fall
949 * to BYTE, which the adapter does not advertise and will
950 * likely fail.
951 */
952 sfp->read = sfp_smbus_read;
953 sfp->write = sfp_smbus_write;
954 max_block_size = 2;
955 } else {
956 sfp->i2c = NULL;
957 return -EINVAL;
958 }
959
960 if (i2c->quirks && i2c->quirks->max_read_len)
961 max_block_size = min(max_block_size, i2c->quirks->max_read_len);
962 if (i2c->quirks && i2c->quirks->max_write_len)
963 max_block_size = min(max_block_size, i2c->quirks->max_write_len);
964
965 sfp->i2c_max_block_size = max_block_size;
966 sfp->i2c_block_size = sfp->i2c_max_block_size;
967 return 0;
968 }
969
sfp_i2c_mdiobus_create(struct sfp * sfp)970 static int sfp_i2c_mdiobus_create(struct sfp *sfp)
971 {
972 struct mii_bus *i2c_mii;
973 int ret;
974
975 i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol);
976 if (IS_ERR(i2c_mii))
977 return PTR_ERR(i2c_mii);
978
979 i2c_mii->name = "SFP I2C Bus";
980 i2c_mii->phy_mask = ~0;
981
982 ret = mdiobus_register(i2c_mii);
983 if (ret < 0) {
984 mdiobus_free(i2c_mii);
985 return ret;
986 }
987
988 sfp->i2c_mii = i2c_mii;
989
990 return 0;
991 }
992
sfp_i2c_mdiobus_destroy(struct sfp * sfp)993 static void sfp_i2c_mdiobus_destroy(struct sfp *sfp)
994 {
995 mdiobus_unregister(sfp->i2c_mii);
996 mdiobus_free(sfp->i2c_mii);
997 sfp->i2c_mii = NULL;
998 }
999
1000 /* Interface */
sfp_read(struct sfp * sfp,bool a2,u8 addr,void * buf,size_t len)1001 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
1002 {
1003 return sfp->read(sfp, a2, addr, buf, len);
1004 }
1005
1006 /* Probe whether a module is physically present by attempting a single-byte
1007 * I2C read of the EEPROM identifier (an empty cage NAKs). Used as the presence
1008 * source on boards that do not wire MOD_DEF0 to a GPIO.
1009 */
sfp_module_present_i2c(struct sfp * sfp)1010 static bool sfp_module_present_i2c(struct sfp *sfp)
1011 {
1012 u8 id;
1013
1014 return sfp_read(sfp, false, SFP_PHYS_ID, &id, sizeof(id)) == sizeof(id);
1015 }
1016
1017 /* get_state variant for boards without a MOD_DEF0 GPIO. Instead of assuming
1018 * the module is always present, derive SFP_F_PRESENT from a throttled I2C
1019 * probe so that hot-insertion and removal are detected. A single ACK asserts
1020 * presence; R_PROBE_ABSENT consecutive failures clear it, to ride out a
1021 * transient I2C error on a live module.
1022 */
sfp_i2c_get_state(struct sfp * sfp)1023 static unsigned int sfp_i2c_get_state(struct sfp *sfp)
1024 {
1025 unsigned int state = sfp_gpio_get_state(sfp);
1026
1027 if (time_after_eq(jiffies, sfp->i2c_present_next)) {
1028 if (sfp_module_present_i2c(sfp)) {
1029 sfp->i2c_present = true;
1030 sfp->i2c_present_nak = 0;
1031 } else if (sfp->i2c_present &&
1032 ++sfp->i2c_present_nak >= R_PROBE_ABSENT) {
1033 sfp->i2c_present = false;
1034 sfp->i2c_present_nak = 0;
1035 }
1036 sfp->i2c_present_next = jiffies + T_PROBE_PRESENT;
1037 }
1038
1039 if (sfp->i2c_present)
1040 state |= SFP_F_PRESENT;
1041
1042 return state;
1043 }
1044
sfp_write(struct sfp * sfp,bool a2,u8 addr,void * buf,size_t len)1045 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
1046 {
1047 return sfp->write(sfp, a2, addr, buf, len);
1048 }
1049
sfp_modify_u8(struct sfp * sfp,bool a2,u8 addr,u8 mask,u8 val)1050 static int sfp_modify_u8(struct sfp *sfp, bool a2, u8 addr, u8 mask, u8 val)
1051 {
1052 int ret;
1053 u8 old, v;
1054
1055 ret = sfp_read(sfp, a2, addr, &old, sizeof(old));
1056 if (ret != sizeof(old))
1057 return ret;
1058
1059 v = (old & ~mask) | (val & mask);
1060 if (v == old)
1061 return sizeof(v);
1062
1063 return sfp_write(sfp, a2, addr, &v, sizeof(v));
1064 }
1065
sfp_soft_get_state(struct sfp * sfp)1066 static unsigned int sfp_soft_get_state(struct sfp *sfp)
1067 {
1068 unsigned int state = 0;
1069 u8 status;
1070 int ret;
1071
1072 ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status));
1073 if (ret == sizeof(status)) {
1074 if (status & SFP_STATUS_RX_LOS)
1075 state |= SFP_F_LOS;
1076 if (status & SFP_STATUS_TX_FAULT)
1077 state |= SFP_F_TX_FAULT;
1078 } else {
1079 dev_err_ratelimited(sfp->dev,
1080 "failed to read SFP soft status: %pe\n",
1081 ERR_PTR(ret));
1082 /* Preserve the current state */
1083 state = sfp->state;
1084 }
1085
1086 return state & sfp->state_soft_mask;
1087 }
1088
sfp_soft_set_state(struct sfp * sfp,unsigned int state,unsigned int soft)1089 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state,
1090 unsigned int soft)
1091 {
1092 u8 mask = 0;
1093 u8 val = 0;
1094
1095 if (soft & SFP_F_TX_DISABLE)
1096 mask |= SFP_STATUS_TX_DISABLE_FORCE;
1097 if (state & SFP_F_TX_DISABLE)
1098 val |= SFP_STATUS_TX_DISABLE_FORCE;
1099
1100 if (soft & SFP_F_RS0)
1101 mask |= SFP_STATUS_RS0_SELECT;
1102 if (state & SFP_F_RS0)
1103 val |= SFP_STATUS_RS0_SELECT;
1104
1105 if (mask)
1106 sfp_modify_u8(sfp, true, SFP_STATUS, mask, val);
1107
1108 val = mask = 0;
1109 if (soft & SFP_F_RS1)
1110 mask |= SFP_EXT_STATUS_RS1_SELECT;
1111 if (state & SFP_F_RS1)
1112 val |= SFP_EXT_STATUS_RS1_SELECT;
1113
1114 if (mask)
1115 sfp_modify_u8(sfp, true, SFP_EXT_STATUS, mask, val);
1116 }
1117
sfp_soft_start_poll(struct sfp * sfp)1118 static void sfp_soft_start_poll(struct sfp *sfp)
1119 {
1120 const struct sfp_eeprom_id *id = &sfp->id;
1121 unsigned int mask = 0;
1122
1123 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE)
1124 mask |= SFP_F_TX_DISABLE;
1125 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT)
1126 mask |= SFP_F_TX_FAULT;
1127 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS)
1128 mask |= SFP_F_LOS;
1129 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RATE_SELECT)
1130 mask |= sfp->rs_state_mask;
1131
1132 mutex_lock(&sfp->st_mutex);
1133 // Poll the soft state for hardware pins we want to ignore
1134 sfp->state_soft_mask = ~sfp->state_hw_mask & ~sfp->state_ignore_mask &
1135 mask;
1136
1137 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
1138 !sfp->need_poll)
1139 sfp_schedule_poll(sfp);
1140 mutex_unlock(&sfp->st_mutex);
1141 }
1142
sfp_soft_stop_poll(struct sfp * sfp)1143 static void sfp_soft_stop_poll(struct sfp *sfp)
1144 {
1145 mutex_lock(&sfp->st_mutex);
1146 sfp->state_soft_mask = 0;
1147 mutex_unlock(&sfp->st_mutex);
1148 }
1149
1150 /* sfp_get_state() - must be called with st_mutex held, or in the
1151 * initialisation path.
1152 */
sfp_get_state(struct sfp * sfp)1153 static unsigned int sfp_get_state(struct sfp *sfp)
1154 {
1155 unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT);
1156 unsigned int state;
1157
1158 state = sfp->get_state(sfp) & sfp->state_hw_mask;
1159 if (state & SFP_F_PRESENT && soft)
1160 state |= sfp_soft_get_state(sfp);
1161
1162 return state;
1163 }
1164
1165 /* sfp_set_state() - must be called with st_mutex held, or in the
1166 * initialisation path.
1167 */
sfp_set_state(struct sfp * sfp,unsigned int state)1168 static void sfp_set_state(struct sfp *sfp, unsigned int state)
1169 {
1170 unsigned int soft;
1171
1172 sfp->set_state(sfp, state);
1173
1174 soft = sfp->state_soft_mask & SFP_F_OUTPUTS;
1175 if (state & SFP_F_PRESENT && soft)
1176 sfp_soft_set_state(sfp, state, soft);
1177 }
1178
sfp_mod_state(struct sfp * sfp,unsigned int mask,unsigned int set)1179 static void sfp_mod_state(struct sfp *sfp, unsigned int mask, unsigned int set)
1180 {
1181 mutex_lock(&sfp->st_mutex);
1182 sfp->state = (sfp->state & ~mask) | set;
1183 sfp_set_state(sfp, sfp->state);
1184 mutex_unlock(&sfp->st_mutex);
1185 }
1186
sfp_check(void * buf,size_t len)1187 static unsigned int sfp_check(void *buf, size_t len)
1188 {
1189 u8 *p, check;
1190
1191 for (p = buf, check = 0; len; p++, len--)
1192 check += *p;
1193
1194 return check;
1195 }
1196
1197 /* hwmon */
1198 #if IS_ENABLED(CONFIG_HWMON)
sfp_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)1199 static umode_t sfp_hwmon_is_visible(const void *data,
1200 enum hwmon_sensor_types type,
1201 u32 attr, int channel)
1202 {
1203 const struct sfp *sfp = data;
1204
1205 switch (type) {
1206 case hwmon_temp:
1207 switch (attr) {
1208 case hwmon_temp_min_alarm:
1209 case hwmon_temp_max_alarm:
1210 case hwmon_temp_lcrit_alarm:
1211 case hwmon_temp_crit_alarm:
1212 case hwmon_temp_min:
1213 case hwmon_temp_max:
1214 case hwmon_temp_lcrit:
1215 case hwmon_temp_crit:
1216 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1217 return 0;
1218 fallthrough;
1219 case hwmon_temp_input:
1220 case hwmon_temp_label:
1221 return 0444;
1222 default:
1223 return 0;
1224 }
1225 case hwmon_in:
1226 switch (attr) {
1227 case hwmon_in_min_alarm:
1228 case hwmon_in_max_alarm:
1229 case hwmon_in_lcrit_alarm:
1230 case hwmon_in_crit_alarm:
1231 case hwmon_in_min:
1232 case hwmon_in_max:
1233 case hwmon_in_lcrit:
1234 case hwmon_in_crit:
1235 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1236 return 0;
1237 fallthrough;
1238 case hwmon_in_input:
1239 case hwmon_in_label:
1240 return 0444;
1241 default:
1242 return 0;
1243 }
1244 case hwmon_curr:
1245 switch (attr) {
1246 case hwmon_curr_min_alarm:
1247 case hwmon_curr_max_alarm:
1248 case hwmon_curr_lcrit_alarm:
1249 case hwmon_curr_crit_alarm:
1250 case hwmon_curr_min:
1251 case hwmon_curr_max:
1252 case hwmon_curr_lcrit:
1253 case hwmon_curr_crit:
1254 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1255 return 0;
1256 fallthrough;
1257 case hwmon_curr_input:
1258 case hwmon_curr_label:
1259 return 0444;
1260 default:
1261 return 0;
1262 }
1263 case hwmon_power:
1264 /* External calibration of receive power requires
1265 * floating point arithmetic. Doing that in the kernel
1266 * is not easy, so just skip it. If the module does
1267 * not require external calibration, we can however
1268 * show receiver power, since FP is then not needed.
1269 */
1270 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
1271 channel == 1)
1272 return 0;
1273 switch (attr) {
1274 case hwmon_power_min_alarm:
1275 case hwmon_power_max_alarm:
1276 case hwmon_power_lcrit_alarm:
1277 case hwmon_power_crit_alarm:
1278 case hwmon_power_min:
1279 case hwmon_power_max:
1280 case hwmon_power_lcrit:
1281 case hwmon_power_crit:
1282 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
1283 return 0;
1284 fallthrough;
1285 case hwmon_power_input:
1286 case hwmon_power_label:
1287 return 0444;
1288 default:
1289 return 0;
1290 }
1291 default:
1292 return 0;
1293 }
1294 }
1295
sfp_hwmon_read_sensor(struct sfp * sfp,int reg,long * value)1296 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
1297 {
1298 __be16 val;
1299 int err;
1300
1301 err = sfp_read(sfp, true, reg, &val, sizeof(val));
1302 if (err < 0)
1303 return err;
1304
1305 *value = be16_to_cpu(val);
1306
1307 return 0;
1308 }
1309
sfp_hwmon_to_rx_power(long * value)1310 static void sfp_hwmon_to_rx_power(long *value)
1311 {
1312 *value = DIV_ROUND_CLOSEST(*value, 10);
1313 }
1314
sfp_hwmon_calibrate(struct sfp * sfp,unsigned int slope,int offset,long * value)1315 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
1316 long *value)
1317 {
1318 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
1319 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
1320 }
1321
sfp_hwmon_calibrate_temp(struct sfp * sfp,long * value)1322 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
1323 {
1324 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
1325 be16_to_cpu(sfp->diag.cal_t_offset), value);
1326
1327 if (*value >= 0x8000)
1328 *value -= 0x10000;
1329
1330 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
1331 }
1332
sfp_hwmon_calibrate_vcc(struct sfp * sfp,long * value)1333 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
1334 {
1335 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
1336 be16_to_cpu(sfp->diag.cal_v_offset), value);
1337
1338 *value = DIV_ROUND_CLOSEST(*value, 10);
1339 }
1340
sfp_hwmon_calibrate_bias(struct sfp * sfp,long * value)1341 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
1342 {
1343 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
1344 be16_to_cpu(sfp->diag.cal_txi_offset), value);
1345
1346 *value = DIV_ROUND_CLOSEST(*value, 500);
1347 }
1348
sfp_hwmon_calibrate_tx_power(struct sfp * sfp,long * value)1349 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
1350 {
1351 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
1352 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
1353
1354 *value = DIV_ROUND_CLOSEST(*value, 10);
1355 }
1356
sfp_hwmon_read_temp(struct sfp * sfp,int reg,long * value)1357 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
1358 {
1359 int err;
1360
1361 err = sfp_hwmon_read_sensor(sfp, reg, value);
1362 if (err < 0)
1363 return err;
1364
1365 sfp_hwmon_calibrate_temp(sfp, value);
1366
1367 return 0;
1368 }
1369
sfp_hwmon_read_vcc(struct sfp * sfp,int reg,long * value)1370 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
1371 {
1372 int err;
1373
1374 err = sfp_hwmon_read_sensor(sfp, reg, value);
1375 if (err < 0)
1376 return err;
1377
1378 sfp_hwmon_calibrate_vcc(sfp, value);
1379
1380 return 0;
1381 }
1382
sfp_hwmon_read_bias(struct sfp * sfp,int reg,long * value)1383 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
1384 {
1385 int err;
1386
1387 err = sfp_hwmon_read_sensor(sfp, reg, value);
1388 if (err < 0)
1389 return err;
1390
1391 sfp_hwmon_calibrate_bias(sfp, value);
1392
1393 return 0;
1394 }
1395
sfp_hwmon_read_tx_power(struct sfp * sfp,int reg,long * value)1396 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
1397 {
1398 int err;
1399
1400 err = sfp_hwmon_read_sensor(sfp, reg, value);
1401 if (err < 0)
1402 return err;
1403
1404 sfp_hwmon_calibrate_tx_power(sfp, value);
1405
1406 return 0;
1407 }
1408
sfp_hwmon_read_rx_power(struct sfp * sfp,int reg,long * value)1409 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
1410 {
1411 int err;
1412
1413 err = sfp_hwmon_read_sensor(sfp, reg, value);
1414 if (err < 0)
1415 return err;
1416
1417 sfp_hwmon_to_rx_power(value);
1418
1419 return 0;
1420 }
1421
sfp_hwmon_temp(struct sfp * sfp,u32 attr,long * value)1422 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
1423 {
1424 u8 status;
1425 int err;
1426
1427 switch (attr) {
1428 case hwmon_temp_input:
1429 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
1430
1431 case hwmon_temp_lcrit:
1432 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
1433 sfp_hwmon_calibrate_temp(sfp, value);
1434 return 0;
1435
1436 case hwmon_temp_min:
1437 *value = be16_to_cpu(sfp->diag.temp_low_warn);
1438 sfp_hwmon_calibrate_temp(sfp, value);
1439 return 0;
1440 case hwmon_temp_max:
1441 *value = be16_to_cpu(sfp->diag.temp_high_warn);
1442 sfp_hwmon_calibrate_temp(sfp, value);
1443 return 0;
1444
1445 case hwmon_temp_crit:
1446 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
1447 sfp_hwmon_calibrate_temp(sfp, value);
1448 return 0;
1449
1450 case hwmon_temp_lcrit_alarm:
1451 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1452 if (err < 0)
1453 return err;
1454
1455 *value = !!(status & SFP_ALARM0_TEMP_LOW);
1456 return 0;
1457
1458 case hwmon_temp_min_alarm:
1459 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1460 if (err < 0)
1461 return err;
1462
1463 *value = !!(status & SFP_WARN0_TEMP_LOW);
1464 return 0;
1465
1466 case hwmon_temp_max_alarm:
1467 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1468 if (err < 0)
1469 return err;
1470
1471 *value = !!(status & SFP_WARN0_TEMP_HIGH);
1472 return 0;
1473
1474 case hwmon_temp_crit_alarm:
1475 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1476 if (err < 0)
1477 return err;
1478
1479 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
1480 return 0;
1481 default:
1482 return -EOPNOTSUPP;
1483 }
1484
1485 return -EOPNOTSUPP;
1486 }
1487
sfp_hwmon_vcc(struct sfp * sfp,u32 attr,long * value)1488 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
1489 {
1490 u8 status;
1491 int err;
1492
1493 switch (attr) {
1494 case hwmon_in_input:
1495 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
1496
1497 case hwmon_in_lcrit:
1498 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
1499 sfp_hwmon_calibrate_vcc(sfp, value);
1500 return 0;
1501
1502 case hwmon_in_min:
1503 *value = be16_to_cpu(sfp->diag.volt_low_warn);
1504 sfp_hwmon_calibrate_vcc(sfp, value);
1505 return 0;
1506
1507 case hwmon_in_max:
1508 *value = be16_to_cpu(sfp->diag.volt_high_warn);
1509 sfp_hwmon_calibrate_vcc(sfp, value);
1510 return 0;
1511
1512 case hwmon_in_crit:
1513 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
1514 sfp_hwmon_calibrate_vcc(sfp, value);
1515 return 0;
1516
1517 case hwmon_in_lcrit_alarm:
1518 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1519 if (err < 0)
1520 return err;
1521
1522 *value = !!(status & SFP_ALARM0_VCC_LOW);
1523 return 0;
1524
1525 case hwmon_in_min_alarm:
1526 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1527 if (err < 0)
1528 return err;
1529
1530 *value = !!(status & SFP_WARN0_VCC_LOW);
1531 return 0;
1532
1533 case hwmon_in_max_alarm:
1534 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1535 if (err < 0)
1536 return err;
1537
1538 *value = !!(status & SFP_WARN0_VCC_HIGH);
1539 return 0;
1540
1541 case hwmon_in_crit_alarm:
1542 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1543 if (err < 0)
1544 return err;
1545
1546 *value = !!(status & SFP_ALARM0_VCC_HIGH);
1547 return 0;
1548 default:
1549 return -EOPNOTSUPP;
1550 }
1551
1552 return -EOPNOTSUPP;
1553 }
1554
sfp_hwmon_bias(struct sfp * sfp,u32 attr,long * value)1555 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
1556 {
1557 u8 status;
1558 int err;
1559
1560 switch (attr) {
1561 case hwmon_curr_input:
1562 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
1563
1564 case hwmon_curr_lcrit:
1565 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
1566 sfp_hwmon_calibrate_bias(sfp, value);
1567 return 0;
1568
1569 case hwmon_curr_min:
1570 *value = be16_to_cpu(sfp->diag.bias_low_warn);
1571 sfp_hwmon_calibrate_bias(sfp, value);
1572 return 0;
1573
1574 case hwmon_curr_max:
1575 *value = be16_to_cpu(sfp->diag.bias_high_warn);
1576 sfp_hwmon_calibrate_bias(sfp, value);
1577 return 0;
1578
1579 case hwmon_curr_crit:
1580 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
1581 sfp_hwmon_calibrate_bias(sfp, value);
1582 return 0;
1583
1584 case hwmon_curr_lcrit_alarm:
1585 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1586 if (err < 0)
1587 return err;
1588
1589 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
1590 return 0;
1591
1592 case hwmon_curr_min_alarm:
1593 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1594 if (err < 0)
1595 return err;
1596
1597 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
1598 return 0;
1599
1600 case hwmon_curr_max_alarm:
1601 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1602 if (err < 0)
1603 return err;
1604
1605 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
1606 return 0;
1607
1608 case hwmon_curr_crit_alarm:
1609 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1610 if (err < 0)
1611 return err;
1612
1613 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
1614 return 0;
1615 default:
1616 return -EOPNOTSUPP;
1617 }
1618
1619 return -EOPNOTSUPP;
1620 }
1621
sfp_hwmon_tx_power(struct sfp * sfp,u32 attr,long * value)1622 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
1623 {
1624 u8 status;
1625 int err;
1626
1627 switch (attr) {
1628 case hwmon_power_input:
1629 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
1630
1631 case hwmon_power_lcrit:
1632 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
1633 sfp_hwmon_calibrate_tx_power(sfp, value);
1634 return 0;
1635
1636 case hwmon_power_min:
1637 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
1638 sfp_hwmon_calibrate_tx_power(sfp, value);
1639 return 0;
1640
1641 case hwmon_power_max:
1642 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
1643 sfp_hwmon_calibrate_tx_power(sfp, value);
1644 return 0;
1645
1646 case hwmon_power_crit:
1647 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
1648 sfp_hwmon_calibrate_tx_power(sfp, value);
1649 return 0;
1650
1651 case hwmon_power_lcrit_alarm:
1652 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1653 if (err < 0)
1654 return err;
1655
1656 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
1657 return 0;
1658
1659 case hwmon_power_min_alarm:
1660 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1661 if (err < 0)
1662 return err;
1663
1664 *value = !!(status & SFP_WARN0_TXPWR_LOW);
1665 return 0;
1666
1667 case hwmon_power_max_alarm:
1668 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1669 if (err < 0)
1670 return err;
1671
1672 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
1673 return 0;
1674
1675 case hwmon_power_crit_alarm:
1676 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1677 if (err < 0)
1678 return err;
1679
1680 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
1681 return 0;
1682 default:
1683 return -EOPNOTSUPP;
1684 }
1685
1686 return -EOPNOTSUPP;
1687 }
1688
sfp_hwmon_rx_power(struct sfp * sfp,u32 attr,long * value)1689 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
1690 {
1691 u8 status;
1692 int err;
1693
1694 switch (attr) {
1695 case hwmon_power_input:
1696 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
1697
1698 case hwmon_power_lcrit:
1699 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1700 sfp_hwmon_to_rx_power(value);
1701 return 0;
1702
1703 case hwmon_power_min:
1704 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1705 sfp_hwmon_to_rx_power(value);
1706 return 0;
1707
1708 case hwmon_power_max:
1709 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1710 sfp_hwmon_to_rx_power(value);
1711 return 0;
1712
1713 case hwmon_power_crit:
1714 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1715 sfp_hwmon_to_rx_power(value);
1716 return 0;
1717
1718 case hwmon_power_lcrit_alarm:
1719 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1720 if (err < 0)
1721 return err;
1722
1723 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
1724 return 0;
1725
1726 case hwmon_power_min_alarm:
1727 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1728 if (err < 0)
1729 return err;
1730
1731 *value = !!(status & SFP_WARN1_RXPWR_LOW);
1732 return 0;
1733
1734 case hwmon_power_max_alarm:
1735 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1736 if (err < 0)
1737 return err;
1738
1739 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
1740 return 0;
1741
1742 case hwmon_power_crit_alarm:
1743 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1744 if (err < 0)
1745 return err;
1746
1747 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1748 return 0;
1749 default:
1750 return -EOPNOTSUPP;
1751 }
1752
1753 return -EOPNOTSUPP;
1754 }
1755
sfp_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * value)1756 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1757 u32 attr, int channel, long *value)
1758 {
1759 struct sfp *sfp = dev_get_drvdata(dev);
1760
1761 switch (type) {
1762 case hwmon_temp:
1763 return sfp_hwmon_temp(sfp, attr, value);
1764 case hwmon_in:
1765 return sfp_hwmon_vcc(sfp, attr, value);
1766 case hwmon_curr:
1767 return sfp_hwmon_bias(sfp, attr, value);
1768 case hwmon_power:
1769 switch (channel) {
1770 case 0:
1771 return sfp_hwmon_tx_power(sfp, attr, value);
1772 case 1:
1773 return sfp_hwmon_rx_power(sfp, attr, value);
1774 default:
1775 return -EOPNOTSUPP;
1776 }
1777 default:
1778 return -EOPNOTSUPP;
1779 }
1780 }
1781
1782 static const char *const sfp_hwmon_power_labels[] = {
1783 "TX_power",
1784 "RX_power",
1785 };
1786
sfp_hwmon_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)1787 static int sfp_hwmon_read_string(struct device *dev,
1788 enum hwmon_sensor_types type,
1789 u32 attr, int channel, const char **str)
1790 {
1791 switch (type) {
1792 case hwmon_curr:
1793 switch (attr) {
1794 case hwmon_curr_label:
1795 *str = "bias";
1796 return 0;
1797 default:
1798 return -EOPNOTSUPP;
1799 }
1800 break;
1801 case hwmon_temp:
1802 switch (attr) {
1803 case hwmon_temp_label:
1804 *str = "temperature";
1805 return 0;
1806 default:
1807 return -EOPNOTSUPP;
1808 }
1809 break;
1810 case hwmon_in:
1811 switch (attr) {
1812 case hwmon_in_label:
1813 *str = "VCC";
1814 return 0;
1815 default:
1816 return -EOPNOTSUPP;
1817 }
1818 break;
1819 case hwmon_power:
1820 switch (attr) {
1821 case hwmon_power_label:
1822 *str = sfp_hwmon_power_labels[channel];
1823 return 0;
1824 default:
1825 return -EOPNOTSUPP;
1826 }
1827 break;
1828 default:
1829 return -EOPNOTSUPP;
1830 }
1831
1832 return -EOPNOTSUPP;
1833 }
1834
1835 static const struct hwmon_ops sfp_hwmon_ops = {
1836 .is_visible = sfp_hwmon_is_visible,
1837 .read = sfp_hwmon_read,
1838 .read_string = sfp_hwmon_read_string,
1839 };
1840
1841 static const struct hwmon_channel_info * const sfp_hwmon_info[] = {
1842 HWMON_CHANNEL_INFO(chip,
1843 HWMON_C_REGISTER_TZ),
1844 HWMON_CHANNEL_INFO(in,
1845 HWMON_I_INPUT |
1846 HWMON_I_MAX | HWMON_I_MIN |
1847 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1848 HWMON_I_CRIT | HWMON_I_LCRIT |
1849 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1850 HWMON_I_LABEL),
1851 HWMON_CHANNEL_INFO(temp,
1852 HWMON_T_INPUT |
1853 HWMON_T_MAX | HWMON_T_MIN |
1854 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1855 HWMON_T_CRIT | HWMON_T_LCRIT |
1856 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1857 HWMON_T_LABEL),
1858 HWMON_CHANNEL_INFO(curr,
1859 HWMON_C_INPUT |
1860 HWMON_C_MAX | HWMON_C_MIN |
1861 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1862 HWMON_C_CRIT | HWMON_C_LCRIT |
1863 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1864 HWMON_C_LABEL),
1865 HWMON_CHANNEL_INFO(power,
1866 /* Transmit power */
1867 HWMON_P_INPUT |
1868 HWMON_P_MAX | HWMON_P_MIN |
1869 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1870 HWMON_P_CRIT | HWMON_P_LCRIT |
1871 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1872 HWMON_P_LABEL,
1873 /* Receive power */
1874 HWMON_P_INPUT |
1875 HWMON_P_MAX | HWMON_P_MIN |
1876 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1877 HWMON_P_CRIT | HWMON_P_LCRIT |
1878 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1879 HWMON_P_LABEL),
1880 NULL,
1881 };
1882
1883 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1884 .ops = &sfp_hwmon_ops,
1885 .info = sfp_hwmon_info,
1886 };
1887
sfp_hwmon_probe(struct work_struct * work)1888 static void sfp_hwmon_probe(struct work_struct *work)
1889 {
1890 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
1891 int err;
1892
1893 /* hwmon interface needs to access 16bit registers in atomic way to
1894 * guarantee coherency of the diagnostic monitoring data. If it is not
1895 * possible to guarantee coherency because EEPROM is broken in such way
1896 * that does not support atomic 16bit read operation then we have to
1897 * skip registration of hwmon device.
1898 */
1899 if (sfp->i2c_block_size < 2) {
1900 dev_info(sfp->dev,
1901 "skipping hwmon device registration\n");
1902 dev_info(sfp->dev,
1903 "diagnostic EEPROM area cannot be read atomically to guarantee data coherency\n");
1904 return;
1905 }
1906
1907 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1908 if (err < 0) {
1909 if (sfp->hwmon_tries--) {
1910 mod_delayed_work(system_percpu_wq, &sfp->hwmon_probe,
1911 T_PROBE_RETRY_SLOW);
1912 } else {
1913 dev_warn(sfp->dev, "hwmon probe failed: %pe\n",
1914 ERR_PTR(err));
1915 }
1916 return;
1917 }
1918
1919 sfp->hwmon_name = hwmon_sanitize_name(dev_name(sfp->dev));
1920 if (IS_ERR(sfp->hwmon_name)) {
1921 dev_err(sfp->dev, "out of memory for hwmon name\n");
1922 return;
1923 }
1924
1925 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1926 sfp->hwmon_name, sfp,
1927 &sfp_hwmon_chip_info,
1928 NULL);
1929 if (IS_ERR(sfp->hwmon_dev))
1930 dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1931 PTR_ERR(sfp->hwmon_dev));
1932 }
1933
sfp_hwmon_insert(struct sfp * sfp)1934 static int sfp_hwmon_insert(struct sfp *sfp)
1935 {
1936 if (sfp->have_a2 && sfp->id.ext.diagmon & SFP_DIAGMON_DDM) {
1937 mod_delayed_work(system_percpu_wq, &sfp->hwmon_probe, 1);
1938 sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
1939 }
1940
1941 return 0;
1942 }
1943
sfp_hwmon_remove(struct sfp * sfp)1944 static void sfp_hwmon_remove(struct sfp *sfp)
1945 {
1946 cancel_delayed_work_sync(&sfp->hwmon_probe);
1947 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1948 hwmon_device_unregister(sfp->hwmon_dev);
1949 sfp->hwmon_dev = NULL;
1950 }
1951
1952 if (!IS_ERR_OR_NULL(sfp->hwmon_name)) {
1953 kfree(sfp->hwmon_name);
1954 sfp->hwmon_name = NULL;
1955 }
1956 }
1957
sfp_hwmon_init(struct sfp * sfp)1958 static int sfp_hwmon_init(struct sfp *sfp)
1959 {
1960 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1961
1962 return 0;
1963 }
1964
sfp_hwmon_exit(struct sfp * sfp)1965 static void sfp_hwmon_exit(struct sfp *sfp)
1966 {
1967 cancel_delayed_work_sync(&sfp->hwmon_probe);
1968 }
1969 #else
sfp_hwmon_insert(struct sfp * sfp)1970 static int sfp_hwmon_insert(struct sfp *sfp)
1971 {
1972 return 0;
1973 }
1974
sfp_hwmon_remove(struct sfp * sfp)1975 static void sfp_hwmon_remove(struct sfp *sfp)
1976 {
1977 }
1978
sfp_hwmon_init(struct sfp * sfp)1979 static int sfp_hwmon_init(struct sfp *sfp)
1980 {
1981 return 0;
1982 }
1983
sfp_hwmon_exit(struct sfp * sfp)1984 static void sfp_hwmon_exit(struct sfp *sfp)
1985 {
1986 }
1987 #endif
1988
1989 /* Helpers */
sfp_module_tx_disable(struct sfp * sfp)1990 static void sfp_module_tx_disable(struct sfp *sfp)
1991 {
1992 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1993 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1994 sfp_mod_state(sfp, SFP_F_TX_DISABLE, SFP_F_TX_DISABLE);
1995 }
1996
sfp_module_tx_enable(struct sfp * sfp)1997 static void sfp_module_tx_enable(struct sfp *sfp)
1998 {
1999 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
2000 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
2001 sfp_mod_state(sfp, SFP_F_TX_DISABLE, 0);
2002 }
2003
2004 #if IS_ENABLED(CONFIG_DEBUG_FS)
sfp_debug_state_show(struct seq_file * s,void * data)2005 static int sfp_debug_state_show(struct seq_file *s, void *data)
2006 {
2007 struct sfp *sfp = s->private;
2008
2009 seq_printf(s, "Module state: %s\n",
2010 mod_state_to_str(sfp->sm_mod_state));
2011 seq_printf(s, "Module probe attempts: %d %d\n",
2012 R_PROBE_RETRY_INIT - sfp->sm_mod_tries_init,
2013 R_PROBE_RETRY_SLOW - sfp->sm_mod_tries);
2014 seq_printf(s, "Device state: %s\n",
2015 dev_state_to_str(sfp->sm_dev_state));
2016 seq_printf(s, "Main state: %s\n",
2017 sm_state_to_str(sfp->sm_state));
2018 seq_printf(s, "Fault recovery remaining retries: %d\n",
2019 sfp->sm_fault_retries);
2020 seq_printf(s, "PHY probe remaining retries: %d\n",
2021 sfp->sm_phy_retries);
2022 seq_printf(s, "Signalling rate: %u kBd\n", sfp->rate_kbd);
2023 seq_printf(s, "Rate select threshold: %u kBd\n",
2024 sfp->rs_threshold_kbd);
2025 seq_printf(s, "moddef0: %d\n", !!(sfp->state & SFP_F_PRESENT));
2026 seq_printf(s, "rx_los: %d\n", !!(sfp->state & SFP_F_LOS));
2027 seq_printf(s, "tx_fault: %d\n", !!(sfp->state & SFP_F_TX_FAULT));
2028 seq_printf(s, "tx_disable: %d\n", !!(sfp->state & SFP_F_TX_DISABLE));
2029 seq_printf(s, "rs0: %d\n", !!(sfp->state & SFP_F_RS0));
2030 seq_printf(s, "rs1: %d\n", !!(sfp->state & SFP_F_RS1));
2031 return 0;
2032 }
2033 DEFINE_SHOW_ATTRIBUTE(sfp_debug_state);
2034
sfp_debugfs_init(struct sfp * sfp)2035 static void sfp_debugfs_init(struct sfp *sfp)
2036 {
2037 sfp->debugfs_dir = debugfs_create_dir(dev_name(sfp->dev), NULL);
2038
2039 debugfs_create_file("state", 0600, sfp->debugfs_dir, sfp,
2040 &sfp_debug_state_fops);
2041 }
2042
sfp_debugfs_exit(struct sfp * sfp)2043 static void sfp_debugfs_exit(struct sfp *sfp)
2044 {
2045 debugfs_remove_recursive(sfp->debugfs_dir);
2046 }
2047 #else
sfp_debugfs_init(struct sfp * sfp)2048 static void sfp_debugfs_init(struct sfp *sfp)
2049 {
2050 }
2051
sfp_debugfs_exit(struct sfp * sfp)2052 static void sfp_debugfs_exit(struct sfp *sfp)
2053 {
2054 }
2055 #endif
2056
sfp_module_tx_fault_reset(struct sfp * sfp)2057 static void sfp_module_tx_fault_reset(struct sfp *sfp)
2058 {
2059 unsigned int state;
2060
2061 mutex_lock(&sfp->st_mutex);
2062 state = sfp->state;
2063 if (!(state & SFP_F_TX_DISABLE)) {
2064 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
2065
2066 udelay(T_RESET_US);
2067
2068 sfp_set_state(sfp, state);
2069 }
2070 mutex_unlock(&sfp->st_mutex);
2071 }
2072
2073 /* SFP state machine */
sfp_sm_set_timer(struct sfp * sfp,unsigned int timeout)2074 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
2075 {
2076 if (timeout)
2077 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
2078 timeout);
2079 else
2080 cancel_delayed_work(&sfp->timeout);
2081 }
2082
sfp_sm_next(struct sfp * sfp,unsigned int state,unsigned int timeout)2083 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
2084 unsigned int timeout)
2085 {
2086 sfp->sm_state = state;
2087 sfp_sm_set_timer(sfp, timeout);
2088 }
2089
sfp_sm_mod_next(struct sfp * sfp,unsigned int state,unsigned int timeout)2090 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
2091 unsigned int timeout)
2092 {
2093 sfp->sm_mod_state = state;
2094 sfp_sm_set_timer(sfp, timeout);
2095 }
2096
sfp_sm_phy_detach(struct sfp * sfp)2097 static void sfp_sm_phy_detach(struct sfp *sfp)
2098 {
2099 sfp_remove_phy(sfp->sfp_bus);
2100 phy_device_remove(sfp->mod_phy);
2101 phy_device_free(sfp->mod_phy);
2102 sfp->mod_phy = NULL;
2103 }
2104
sfp_sm_probe_phy(struct sfp * sfp,int addr,bool is_c45)2105 static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45)
2106 {
2107 struct phy_device *phy;
2108 int err;
2109
2110 phy = get_phy_device(sfp->i2c_mii, addr, is_c45);
2111 if (phy == ERR_PTR(-ENODEV))
2112 return PTR_ERR(phy);
2113 if (IS_ERR(phy)) {
2114 dev_err(sfp->dev, "mdiobus scan returned %pe\n", phy);
2115 return PTR_ERR(phy);
2116 }
2117
2118 /* Mark this PHY as being on a SFP module */
2119 phy->is_on_sfp_module = true;
2120
2121 err = phy_device_register(phy);
2122 if (err) {
2123 phy_device_free(phy);
2124 dev_err(sfp->dev, "phy_device_register failed: %pe\n",
2125 ERR_PTR(err));
2126 return err;
2127 }
2128
2129 err = sfp_add_phy(sfp->sfp_bus, phy);
2130 if (err) {
2131 phy_device_remove(phy);
2132 phy_device_free(phy);
2133 dev_err(sfp->dev, "sfp_add_phy failed: %pe\n", ERR_PTR(err));
2134 return err;
2135 }
2136
2137 sfp->mod_phy = phy;
2138
2139 return 0;
2140 }
2141
sfp_sm_link_up(struct sfp * sfp)2142 static void sfp_sm_link_up(struct sfp *sfp)
2143 {
2144 sfp_link_up(sfp->sfp_bus);
2145 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
2146 }
2147
sfp_sm_link_down(struct sfp * sfp)2148 static void sfp_sm_link_down(struct sfp *sfp)
2149 {
2150 sfp_link_down(sfp->sfp_bus);
2151 }
2152
sfp_sm_link_check_los(struct sfp * sfp)2153 static void sfp_sm_link_check_los(struct sfp *sfp)
2154 {
2155 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
2156 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
2157 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
2158 bool los = false;
2159
2160 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
2161 * are set, we assume that no LOS signal is available. If both are
2162 * set, we assume LOS is not implemented (and is meaningless.)
2163 */
2164 if (los_options == los_inverted)
2165 los = !(sfp->state & SFP_F_LOS);
2166 else if (los_options == los_normal)
2167 los = !!(sfp->state & SFP_F_LOS);
2168
2169 if (los)
2170 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
2171 else
2172 sfp_sm_link_up(sfp);
2173 }
2174
sfp_los_event_active(struct sfp * sfp,unsigned int event)2175 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
2176 {
2177 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
2178 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
2179 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
2180
2181 return (los_options == los_inverted && event == SFP_E_LOS_LOW) ||
2182 (los_options == los_normal && event == SFP_E_LOS_HIGH);
2183 }
2184
sfp_los_event_inactive(struct sfp * sfp,unsigned int event)2185 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
2186 {
2187 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED);
2188 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL);
2189 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal);
2190
2191 return (los_options == los_inverted && event == SFP_E_LOS_HIGH) ||
2192 (los_options == los_normal && event == SFP_E_LOS_LOW);
2193 }
2194
sfp_sm_fault(struct sfp * sfp,unsigned int next_state,bool warn)2195 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
2196 {
2197 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) {
2198 dev_err(sfp->dev,
2199 "module persistently indicates fault, disabling\n");
2200 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
2201 } else {
2202 if (warn)
2203 dev_err(sfp->dev, "module transmit fault indicated\n");
2204
2205 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
2206 }
2207 }
2208
sfp_sm_add_mdio_bus(struct sfp * sfp)2209 static int sfp_sm_add_mdio_bus(struct sfp *sfp)
2210 {
2211 if (sfp->mdio_protocol != MDIO_I2C_NONE)
2212 return sfp_i2c_mdiobus_create(sfp);
2213
2214 return 0;
2215 }
2216
2217 /* Probe a SFP for a PHY device if the module supports copper - the PHY
2218 * normally sits at I2C bus address 0x56, and may either be a clause 22
2219 * or clause 45 PHY.
2220 *
2221 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with
2222 * negotiation enabled, but some may be in 1000base-X - which is for the
2223 * PHY driver to determine.
2224 *
2225 * Clause 45 copper SFP+ modules (10G) appear to switch their interface
2226 * mode according to the negotiated line speed.
2227 */
sfp_sm_probe_for_phy(struct sfp * sfp)2228 static int sfp_sm_probe_for_phy(struct sfp *sfp)
2229 {
2230 int err = 0;
2231
2232 switch (sfp->mdio_protocol) {
2233 case MDIO_I2C_NONE:
2234 break;
2235
2236 case MDIO_I2C_MARVELL_C22:
2237 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false);
2238 break;
2239
2240 case MDIO_I2C_C45:
2241 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true);
2242 break;
2243
2244 case MDIO_I2C_ROLLBALL:
2245 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true);
2246 break;
2247 }
2248
2249 return err;
2250 }
2251
sfp_module_parse_power(struct sfp * sfp)2252 static int sfp_module_parse_power(struct sfp *sfp)
2253 {
2254 u32 power_mW = 1000;
2255 bool supports_a2;
2256
2257 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
2258 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
2259 power_mW = 1500;
2260 /* Added in Rev 11.9, but there is no compliance code for this */
2261 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV11_4 &&
2262 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
2263 power_mW = 2000;
2264
2265 /* Power level 1 modules (max. 1W) are always supported. */
2266 if (power_mW <= 1000) {
2267 sfp->module_power_mW = power_mW;
2268 return 0;
2269 }
2270
2271 supports_a2 = sfp->id.ext.sff8472_compliance !=
2272 SFP_SFF8472_COMPLIANCE_NONE ||
2273 sfp->id.ext.diagmon & SFP_DIAGMON_DDM;
2274
2275 if (power_mW > sfp->max_power_mW) {
2276 /* Module power specification exceeds the allowed maximum. */
2277 if (!supports_a2) {
2278 /* The module appears not to implement bus address
2279 * 0xa2, so assume that the module powers up in the
2280 * indicated mode.
2281 */
2282 dev_err(sfp->dev,
2283 "Host does not support %u.%uW modules\n",
2284 power_mW / 1000, (power_mW / 100) % 10);
2285 return -EINVAL;
2286 } else {
2287 dev_warn(sfp->dev,
2288 "Host does not support %u.%uW modules, module left in power mode 1\n",
2289 power_mW / 1000, (power_mW / 100) % 10);
2290 return 0;
2291 }
2292 }
2293
2294 if (!supports_a2) {
2295 /* The module power level is below the host maximum and the
2296 * module appears not to implement bus address 0xa2, so assume
2297 * that the module powers up in the indicated mode.
2298 */
2299 return 0;
2300 }
2301
2302 /* If the module requires a higher power mode, but also requires
2303 * an address change sequence, warn the user that the module may
2304 * not be functional.
2305 */
2306 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) {
2307 dev_warn(sfp->dev,
2308 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
2309 power_mW / 1000, (power_mW / 100) % 10);
2310 return 0;
2311 }
2312
2313 sfp->module_power_mW = power_mW;
2314
2315 return 0;
2316 }
2317
sfp_sm_mod_hpower(struct sfp * sfp,bool enable)2318 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
2319 {
2320 int err;
2321
2322 err = sfp_modify_u8(sfp, true, SFP_EXT_STATUS,
2323 SFP_EXT_STATUS_PWRLVL_SELECT,
2324 enable ? SFP_EXT_STATUS_PWRLVL_SELECT : 0);
2325 if (err != sizeof(u8)) {
2326 dev_err(sfp->dev, "failed to %sable high power: %pe\n",
2327 enable ? "en" : "dis", ERR_PTR(err));
2328 return -EAGAIN;
2329 }
2330
2331 if (enable)
2332 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
2333 sfp->module_power_mW / 1000,
2334 (sfp->module_power_mW / 100) % 10);
2335
2336 return 0;
2337 }
2338
sfp_module_parse_rate_select(struct sfp * sfp)2339 static void sfp_module_parse_rate_select(struct sfp *sfp)
2340 {
2341 u8 rate_id;
2342
2343 sfp->rs_threshold_kbd = 0;
2344 sfp->rs_state_mask = 0;
2345
2346 if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_RATE_SELECT)))
2347 /* No support for RateSelect */
2348 return;
2349
2350 /* Default to INF-8074 RateSelect operation. The signalling threshold
2351 * rate is not well specified, so always select "Full Bandwidth", but
2352 * SFF-8079 reveals that it is understood that RS0 will be low for
2353 * 1.0625Gb/s and high for 2.125Gb/s. Choose a value half-way between.
2354 * This method exists prior to SFF-8472.
2355 */
2356 sfp->rs_state_mask = SFP_F_RS0;
2357 sfp->rs_threshold_kbd = 1594;
2358
2359 /* Parse the rate identifier, which is complicated due to history:
2360 * SFF-8472 rev 9.5 marks this field as reserved.
2361 * SFF-8079 references SFF-8472 rev 9.5 and defines bit 0. SFF-8472
2362 * compliance is not required.
2363 * SFF-8472 rev 10.2 defines this field using values 0..4
2364 * SFF-8472 rev 11.0 redefines this field with bit 0 for SFF-8079
2365 * and even values.
2366 */
2367 rate_id = sfp->id.base.rate_id;
2368 if (rate_id == 0)
2369 /* Unspecified */
2370 return;
2371
2372 /* SFF-8472 rev 10.0..10.4 did not account for SFF-8079 using bit 0,
2373 * and allocated value 3 to SFF-8431 independent tx/rx rate select.
2374 * Convert this to a SFF-8472 rev 11.0 rate identifier.
2375 */
2376 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 &&
2377 sfp->id.ext.sff8472_compliance < SFP_SFF8472_COMPLIANCE_REV11_0 &&
2378 rate_id == 3)
2379 rate_id = SFF_RID_8431;
2380
2381 if (rate_id & SFF_RID_8079) {
2382 /* SFF-8079 RateSelect / Application Select in conjunction with
2383 * SFF-8472 rev 9.5. SFF-8079 defines rate_id as a bitfield
2384 * with only bit 0 used, which takes precedence over SFF-8472.
2385 */
2386 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_APP_SELECT_SFF8079)) {
2387 /* SFF-8079 Part 1 - rate selection between Fibre
2388 * Channel 1.0625/2.125/4.25 Gbd modes. Note that RS0
2389 * is high for 2125, so we have to subtract 1 to
2390 * include it.
2391 */
2392 sfp->rs_threshold_kbd = 2125 - 1;
2393 sfp->rs_state_mask = SFP_F_RS0;
2394 }
2395 return;
2396 }
2397
2398 /* SFF-8472 rev 9.5 does not define the rate identifier */
2399 if (sfp->id.ext.sff8472_compliance <= SFP_SFF8472_COMPLIANCE_REV9_5)
2400 return;
2401
2402 /* SFF-8472 rev 11.0 defines rate_id as a numerical value which will
2403 * always have bit 0 clear due to SFF-8079's bitfield usage of rate_id.
2404 */
2405 switch (rate_id) {
2406 case SFF_RID_8431_RX_ONLY:
2407 sfp->rs_threshold_kbd = 4250;
2408 sfp->rs_state_mask = SFP_F_RS0;
2409 break;
2410
2411 case SFF_RID_8431_TX_ONLY:
2412 sfp->rs_threshold_kbd = 4250;
2413 sfp->rs_state_mask = SFP_F_RS1;
2414 break;
2415
2416 case SFF_RID_8431:
2417 sfp->rs_threshold_kbd = 4250;
2418 sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1;
2419 break;
2420
2421 case SFF_RID_10G8G:
2422 sfp->rs_threshold_kbd = 9000;
2423 sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1;
2424 break;
2425 }
2426 }
2427
2428 /* GPON modules based on Realtek RTL8672 and RTL9601C chips (e.g. V-SOL
2429 * V2801F, CarlitoxxPro CPGOS03-0490, Ubiquiti U-Fiber Instant, ...) do
2430 * not support multibyte reads from the EEPROM. Each multi-byte read
2431 * operation returns just one byte of EEPROM followed by zeros. There is
2432 * no way to identify which modules are using Realtek RTL8672 and RTL9601C
2433 * chips. Moreover every OEM of V-SOL V2801F module puts its own vendor
2434 * name and vendor id into EEPROM, so there is even no way to detect if
2435 * module is V-SOL V2801F. Therefore check for those zeros in the read
2436 * data and then based on check switch to reading EEPROM to one byte
2437 * at a time.
2438 */
sfp_id_needs_byte_io(struct sfp * sfp,void * buf,size_t len)2439 static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len)
2440 {
2441 size_t i, block_size = sfp->i2c_block_size;
2442
2443 /* Already using byte IO */
2444 if (block_size == 1)
2445 return false;
2446
2447 for (i = 1; i < len; i += block_size) {
2448 if (memchr_inv(buf + i, '\0', min(block_size - 1, len - i)))
2449 return false;
2450 }
2451 return true;
2452 }
2453
sfp_cotsworks_fixup_check(struct sfp * sfp,struct sfp_eeprom_id * id)2454 static int sfp_cotsworks_fixup_check(struct sfp *sfp, struct sfp_eeprom_id *id)
2455 {
2456 u8 check;
2457 int err;
2458
2459 if (id->base.phys_id != SFF8024_ID_SFF_8472 ||
2460 id->base.phys_ext_id != SFP_PHYS_EXT_ID_SFP ||
2461 id->base.connector != SFF8024_CONNECTOR_LC) {
2462 dev_warn(sfp->dev, "Rewriting fiber module EEPROM with corrected values\n");
2463 id->base.phys_id = SFF8024_ID_SFF_8472;
2464 id->base.phys_ext_id = SFP_PHYS_EXT_ID_SFP;
2465 id->base.connector = SFF8024_CONNECTOR_LC;
2466 err = sfp_write(sfp, false, SFP_PHYS_ID, &id->base, 3);
2467 if (err != 3) {
2468 dev_err(sfp->dev,
2469 "Failed to rewrite module EEPROM: %pe\n",
2470 ERR_PTR(err));
2471 return err;
2472 }
2473
2474 /* Cotsworks modules have been found to require a delay between write operations. */
2475 mdelay(50);
2476
2477 /* Update base structure checksum */
2478 check = sfp_check(&id->base, sizeof(id->base) - 1);
2479 err = sfp_write(sfp, false, SFP_CC_BASE, &check, 1);
2480 if (err != 1) {
2481 dev_err(sfp->dev,
2482 "Failed to update base structure checksum in fiber module EEPROM: %pe\n",
2483 ERR_PTR(err));
2484 return err;
2485 }
2486 }
2487 return 0;
2488 }
2489
sfp_module_parse_sff8472(struct sfp * sfp)2490 static int sfp_module_parse_sff8472(struct sfp *sfp)
2491 {
2492 /* If the module requires address swap mode, warn about it */
2493 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
2494 dev_warn(sfp->dev,
2495 "module address swap to access page 0xA2 is not supported.\n");
2496 else
2497 sfp->have_a2 = true;
2498
2499 return 0;
2500 }
2501
sfp_sm_mod_probe(struct sfp * sfp,bool report)2502 static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
2503 {
2504 /* SFP module inserted - read I2C data */
2505 struct sfp_eeprom_id id;
2506 bool cotsworks_sfbg;
2507 unsigned int mask;
2508 bool cotsworks;
2509 u8 check;
2510 int ret;
2511
2512 sfp->i2c_block_size = sfp->i2c_max_block_size;
2513
2514 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
2515 if (ret < 0) {
2516 if (report)
2517 dev_err(sfp->dev, "failed to read EEPROM: %pe\n",
2518 ERR_PTR(ret));
2519 return -EAGAIN;
2520 }
2521
2522 if (ret != sizeof(id.base)) {
2523 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret));
2524 return -EAGAIN;
2525 }
2526
2527 /* Some SFP modules (e.g. Nokia 3FE46541AA) lock up if read from
2528 * address 0x51 is just one byte at a time. Also SFF-8472 requires
2529 * that EEPROM supports atomic 16bit read operation for diagnostic
2530 * fields, so do not switch to one byte reading at a time unless it
2531 * is really required and we have no other option.
2532 */
2533 if (sfp_id_needs_byte_io(sfp, &id.base, sizeof(id.base))) {
2534 dev_info(sfp->dev,
2535 "Detected broken RTL8672/RTL9601C emulated EEPROM\n");
2536 dev_info(sfp->dev,
2537 "Switching to reading EEPROM to one byte at a time\n");
2538 sfp->i2c_block_size = 1;
2539
2540 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base));
2541 if (ret < 0) {
2542 if (report)
2543 dev_err(sfp->dev,
2544 "failed to read EEPROM: %pe\n",
2545 ERR_PTR(ret));
2546 return -EAGAIN;
2547 }
2548
2549 if (ret != sizeof(id.base)) {
2550 dev_err(sfp->dev, "EEPROM short read: %pe\n",
2551 ERR_PTR(ret));
2552 return -EAGAIN;
2553 }
2554 }
2555
2556 /* Cotsworks do not seem to update the checksums when they
2557 * do the final programming with the final module part number,
2558 * serial number and date code.
2559 */
2560 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
2561 cotsworks_sfbg = !memcmp(id.base.vendor_pn, "SFBG", 4);
2562
2563 /* Cotsworks SFF module EEPROM do not always have valid phys_id,
2564 * phys_ext_id, and connector bytes. Rewrite SFF EEPROM bytes if
2565 * Cotsworks PN matches and bytes are not correct.
2566 */
2567 if (cotsworks && cotsworks_sfbg) {
2568 ret = sfp_cotsworks_fixup_check(sfp, &id);
2569 if (ret < 0)
2570 return ret;
2571 }
2572
2573 /* Validate the checksum over the base structure */
2574 check = sfp_check(&id.base, sizeof(id.base) - 1);
2575 if (check != id.base.cc_base) {
2576 if (cotsworks) {
2577 dev_warn(sfp->dev,
2578 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
2579 check, id.base.cc_base);
2580 } else {
2581 dev_err(sfp->dev,
2582 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
2583 check, id.base.cc_base);
2584 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
2585 16, 1, &id, sizeof(id), true);
2586 return -EINVAL;
2587 }
2588 }
2589
2590 ret = sfp_read(sfp, false, SFP_CC_BASE + 1, &id.ext, sizeof(id.ext));
2591 if (ret < 0) {
2592 if (report)
2593 dev_err(sfp->dev, "failed to read EEPROM: %pe\n",
2594 ERR_PTR(ret));
2595 return -EAGAIN;
2596 }
2597
2598 if (ret != sizeof(id.ext)) {
2599 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret));
2600 return -EAGAIN;
2601 }
2602
2603 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
2604 if (check != id.ext.cc_ext) {
2605 if (cotsworks) {
2606 dev_warn(sfp->dev,
2607 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
2608 check, id.ext.cc_ext);
2609 } else {
2610 dev_err(sfp->dev,
2611 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
2612 check, id.ext.cc_ext);
2613 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
2614 16, 1, &id, sizeof(id), true);
2615 memset(&id.ext, 0, sizeof(id.ext));
2616 }
2617 }
2618
2619 sfp->id = id;
2620
2621 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
2622 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
2623 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
2624 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
2625 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
2626 (int)sizeof(id.ext.datecode), id.ext.datecode);
2627
2628 /* Check whether we support this module */
2629 if (!sfp->type->module_supported(&id)) {
2630 dev_err(sfp->dev,
2631 "module is not supported - phys id 0x%02x 0x%02x\n",
2632 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
2633 return -EINVAL;
2634 }
2635
2636 if (sfp->id.ext.sff8472_compliance != SFP_SFF8472_COMPLIANCE_NONE) {
2637 ret = sfp_module_parse_sff8472(sfp);
2638 if (ret < 0)
2639 return ret;
2640 }
2641
2642 /* Parse the module power requirement */
2643 ret = sfp_module_parse_power(sfp);
2644 if (ret < 0)
2645 return ret;
2646
2647 sfp_module_parse_rate_select(sfp);
2648
2649 mask = SFP_F_PRESENT;
2650 if (sfp->gpio[GPIO_TX_DISABLE])
2651 mask |= SFP_F_TX_DISABLE;
2652 if (sfp->gpio[GPIO_TX_FAULT])
2653 mask |= SFP_F_TX_FAULT;
2654 if (sfp->gpio[GPIO_LOS])
2655 mask |= SFP_F_LOS;
2656 if (sfp->gpio[GPIO_RS0])
2657 mask |= SFP_F_RS0;
2658 if (sfp->gpio[GPIO_RS1])
2659 mask |= SFP_F_RS1;
2660
2661 sfp->module_t_start_up = T_START_UP;
2662 sfp->module_t_wait = T_WAIT;
2663 sfp->phy_t_retry = T_PHY_RETRY;
2664
2665 sfp->state_ignore_mask = 0;
2666
2667 if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI ||
2668 sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR ||
2669 sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T ||
2670 sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T)
2671 sfp->mdio_protocol = MDIO_I2C_C45;
2672 else if (sfp->id.base.e1000_base_t)
2673 sfp->mdio_protocol = MDIO_I2C_MARVELL_C22;
2674 else
2675 sfp->mdio_protocol = MDIO_I2C_NONE;
2676
2677 sfp->quirk = sfp_lookup_quirk(&id);
2678
2679 mutex_lock(&sfp->st_mutex);
2680 /* Initialise state bits to use from hardware */
2681 sfp->state_hw_mask = mask;
2682
2683 /* We want to drive the rate select pins that the module is using */
2684 sfp->state_hw_drive |= sfp->rs_state_mask;
2685
2686 if (sfp->quirk && sfp->quirk->fixup)
2687 sfp->quirk->fixup(sfp);
2688
2689 sfp->state_hw_mask &= ~sfp->state_ignore_mask;
2690 mutex_unlock(&sfp->st_mutex);
2691
2692 return 0;
2693 }
2694
sfp_sm_mod_remove(struct sfp * sfp)2695 static void sfp_sm_mod_remove(struct sfp *sfp)
2696 {
2697 if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
2698 sfp_module_remove(sfp->sfp_bus);
2699
2700 sfp_hwmon_remove(sfp);
2701
2702 memset(&sfp->id, 0, sizeof(sfp->id));
2703 sfp->module_power_mW = 0;
2704 sfp->state_hw_drive = SFP_F_TX_DISABLE;
2705 sfp->have_a2 = false;
2706
2707 dev_info(sfp->dev, "module removed\n");
2708 }
2709
2710 /* This state machine tracks the upstream's state */
sfp_sm_device(struct sfp * sfp,unsigned int event)2711 static void sfp_sm_device(struct sfp *sfp, unsigned int event)
2712 {
2713 switch (sfp->sm_dev_state) {
2714 default:
2715 if (event == SFP_E_DEV_ATTACH)
2716 sfp->sm_dev_state = SFP_DEV_DOWN;
2717 break;
2718
2719 case SFP_DEV_DOWN:
2720 if (event == SFP_E_DEV_DETACH)
2721 sfp->sm_dev_state = SFP_DEV_DETACHED;
2722 else if (event == SFP_E_DEV_UP)
2723 sfp->sm_dev_state = SFP_DEV_UP;
2724 break;
2725
2726 case SFP_DEV_UP:
2727 if (event == SFP_E_DEV_DETACH)
2728 sfp->sm_dev_state = SFP_DEV_DETACHED;
2729 else if (event == SFP_E_DEV_DOWN)
2730 sfp->sm_dev_state = SFP_DEV_DOWN;
2731 break;
2732 }
2733 }
2734
2735 /* This state machine tracks the insert/remove state of the module, probes
2736 * the on-board EEPROM, and sets up the power level.
2737 */
sfp_sm_module(struct sfp * sfp,unsigned int event)2738 static void sfp_sm_module(struct sfp *sfp, unsigned int event)
2739 {
2740 int err;
2741
2742 /* Handle remove event globally, it resets this state machine */
2743 if (event == SFP_E_REMOVE) {
2744 sfp_sm_mod_remove(sfp);
2745 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
2746 return;
2747 }
2748
2749 /* Handle device detach globally */
2750 if (sfp->sm_dev_state < SFP_DEV_DOWN &&
2751 sfp->sm_mod_state > SFP_MOD_WAITDEV) {
2752 if (sfp->module_power_mW > 1000 &&
2753 sfp->sm_mod_state > SFP_MOD_HPOWER)
2754 sfp_sm_mod_hpower(sfp, false);
2755 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
2756 return;
2757 }
2758
2759 switch (sfp->sm_mod_state) {
2760 default:
2761 if (event == SFP_E_INSERT) {
2762 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
2763 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
2764 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
2765 }
2766 break;
2767
2768 case SFP_MOD_PROBE:
2769 /* Wait for T_PROBE_INIT to time out */
2770 if (event != SFP_E_TIMEOUT)
2771 break;
2772
2773 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
2774 if (err == -EAGAIN) {
2775 if (sfp->sm_mod_tries_init &&
2776 --sfp->sm_mod_tries_init) {
2777 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
2778 break;
2779 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
2780 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
2781 dev_warn(sfp->dev,
2782 "please wait, module slow to respond\n");
2783 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
2784 break;
2785 }
2786 }
2787 if (err < 0) {
2788 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2789 break;
2790 }
2791
2792 /* Force a poll to re-read the hardware signal state after
2793 * sfp_sm_mod_probe() changed state_hw_mask.
2794 */
2795 mod_delayed_work(system_percpu_wq, &sfp->poll, 1);
2796
2797 err = sfp_hwmon_insert(sfp);
2798 if (err)
2799 dev_warn(sfp->dev, "hwmon probe failed: %pe\n",
2800 ERR_PTR(err));
2801
2802 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
2803 fallthrough;
2804 case SFP_MOD_WAITDEV:
2805 /* Ensure that the device is attached before proceeding */
2806 if (sfp->sm_dev_state < SFP_DEV_DOWN)
2807 break;
2808
2809 /* Report the module insertion to the upstream device */
2810 err = sfp_module_insert(sfp->sfp_bus, &sfp->id,
2811 sfp->quirk);
2812 if (err < 0) {
2813 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2814 break;
2815 }
2816
2817 /* If this is a power level 1 module, we are done */
2818 if (sfp->module_power_mW <= 1000)
2819 goto insert;
2820
2821 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
2822 fallthrough;
2823 case SFP_MOD_HPOWER:
2824 /* Enable high power mode */
2825 err = sfp_sm_mod_hpower(sfp, true);
2826 if (err < 0) {
2827 if (err != -EAGAIN) {
2828 sfp_module_remove(sfp->sfp_bus);
2829 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
2830 } else {
2831 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
2832 }
2833 break;
2834 }
2835
2836 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
2837 break;
2838
2839 case SFP_MOD_WAITPWR:
2840 /* Wait for T_HPOWER_LEVEL to time out */
2841 if (event != SFP_E_TIMEOUT)
2842 break;
2843
2844 insert:
2845 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
2846 break;
2847
2848 case SFP_MOD_PRESENT:
2849 case SFP_MOD_ERROR:
2850 break;
2851 }
2852 }
2853
sfp_sm_main(struct sfp * sfp,unsigned int event)2854 static void sfp_sm_main(struct sfp *sfp, unsigned int event)
2855 {
2856 unsigned long timeout;
2857 int ret;
2858
2859 /* Some events are global */
2860 if (sfp->sm_state != SFP_S_DOWN &&
2861 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
2862 sfp->sm_dev_state != SFP_DEV_UP)) {
2863 if (sfp->sm_state == SFP_S_LINK_UP &&
2864 sfp->sm_dev_state == SFP_DEV_UP)
2865 sfp_sm_link_down(sfp);
2866 if (sfp->sm_state > SFP_S_INIT)
2867 sfp_module_stop(sfp->sfp_bus);
2868 if (sfp->mod_phy)
2869 sfp_sm_phy_detach(sfp);
2870 if (sfp->i2c_mii)
2871 sfp_i2c_mdiobus_destroy(sfp);
2872 sfp_module_tx_disable(sfp);
2873 sfp_soft_stop_poll(sfp);
2874 sfp_sm_next(sfp, SFP_S_DOWN, 0);
2875 return;
2876 }
2877
2878 /* The main state machine */
2879 switch (sfp->sm_state) {
2880 case SFP_S_DOWN:
2881 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
2882 sfp->sm_dev_state != SFP_DEV_UP)
2883 break;
2884
2885 /* Only use the soft state bits if we have access to the A2h
2886 * memory, which implies that we have some level of SFF-8472
2887 * compliance.
2888 */
2889 if (sfp->have_a2)
2890 sfp_soft_start_poll(sfp);
2891
2892 sfp_module_tx_enable(sfp);
2893
2894 /* Initialise the fault clearance retries */
2895 sfp->sm_fault_retries = N_FAULT_INIT;
2896
2897 /* We need to check the TX_FAULT state, which is not defined
2898 * while TX_DISABLE is asserted. The earliest we want to do
2899 * anything (such as probe for a PHY) is 50ms (or more on
2900 * specific modules).
2901 */
2902 sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait);
2903 break;
2904
2905 case SFP_S_WAIT:
2906 if (event != SFP_E_TIMEOUT)
2907 break;
2908
2909 if (sfp->state & SFP_F_TX_FAULT) {
2910 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
2911 * from the TX_DISABLE deassertion for the module to
2912 * initialise, which is indicated by TX_FAULT
2913 * deasserting.
2914 */
2915 timeout = sfp->module_t_start_up;
2916 if (timeout > sfp->module_t_wait)
2917 timeout -= sfp->module_t_wait;
2918 else
2919 timeout = 1;
2920
2921 sfp_sm_next(sfp, SFP_S_INIT, timeout);
2922 } else {
2923 /* TX_FAULT is not asserted, assume the module has
2924 * finished initialising.
2925 */
2926 goto init_done;
2927 }
2928 break;
2929
2930 case SFP_S_INIT:
2931 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
2932 /* TX_FAULT is still asserted after t_init
2933 * or t_start_up, so assume there is a fault.
2934 */
2935 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
2936 sfp->sm_fault_retries == N_FAULT_INIT);
2937 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2938 init_done:
2939 /* Create mdiobus and start trying for PHY */
2940 ret = sfp_sm_add_mdio_bus(sfp);
2941 if (ret < 0) {
2942 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2943 break;
2944 }
2945 sfp->sm_phy_retries = R_PHY_RETRY;
2946 goto phy_probe;
2947 }
2948 break;
2949
2950 case SFP_S_INIT_PHY:
2951 if (event != SFP_E_TIMEOUT)
2952 break;
2953 phy_probe:
2954 /* TX_FAULT deasserted or we timed out with TX_FAULT
2955 * clear. Probe for the PHY and check the LOS state.
2956 */
2957 ret = sfp_sm_probe_for_phy(sfp);
2958 if (ret == -ENODEV) {
2959 if (--sfp->sm_phy_retries) {
2960 sfp_sm_next(sfp, SFP_S_INIT_PHY,
2961 sfp->phy_t_retry);
2962 dev_dbg(sfp->dev,
2963 "no PHY detected, %u tries left\n",
2964 sfp->sm_phy_retries);
2965 break;
2966 } else {
2967 dev_info(sfp->dev, "no PHY detected\n");
2968 }
2969 } else if (ret) {
2970 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2971 break;
2972 }
2973 if (sfp_module_start(sfp->sfp_bus)) {
2974 sfp_sm_next(sfp, SFP_S_FAIL, 0);
2975 break;
2976 }
2977 sfp_sm_link_check_los(sfp);
2978
2979 /* Reset the fault retry count */
2980 sfp->sm_fault_retries = N_FAULT;
2981 break;
2982
2983 case SFP_S_INIT_TX_FAULT:
2984 if (event == SFP_E_TIMEOUT) {
2985 sfp_module_tx_fault_reset(sfp);
2986 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
2987 }
2988 break;
2989
2990 case SFP_S_WAIT_LOS:
2991 if (event == SFP_E_TX_FAULT)
2992 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
2993 else if (sfp_los_event_inactive(sfp, event))
2994 sfp_sm_link_up(sfp);
2995 break;
2996
2997 case SFP_S_LINK_UP:
2998 if (event == SFP_E_TX_FAULT) {
2999 sfp_sm_link_down(sfp);
3000 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
3001 } else if (sfp_los_event_active(sfp, event)) {
3002 sfp_sm_link_down(sfp);
3003 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
3004 }
3005 break;
3006
3007 case SFP_S_TX_FAULT:
3008 if (event == SFP_E_TIMEOUT) {
3009 sfp_module_tx_fault_reset(sfp);
3010 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
3011 }
3012 break;
3013
3014 case SFP_S_REINIT:
3015 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
3016 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
3017 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
3018 dev_info(sfp->dev, "module transmit fault recovered\n");
3019 sfp_sm_link_check_los(sfp);
3020 }
3021 break;
3022
3023 case SFP_S_TX_DISABLE:
3024 break;
3025 }
3026 }
3027
__sfp_sm_event(struct sfp * sfp,unsigned int event)3028 static void __sfp_sm_event(struct sfp *sfp, unsigned int event)
3029 {
3030 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
3031 mod_state_to_str(sfp->sm_mod_state),
3032 dev_state_to_str(sfp->sm_dev_state),
3033 sm_state_to_str(sfp->sm_state),
3034 event_to_str(event));
3035
3036 sfp_sm_device(sfp, event);
3037 sfp_sm_module(sfp, event);
3038 sfp_sm_main(sfp, event);
3039
3040 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
3041 mod_state_to_str(sfp->sm_mod_state),
3042 dev_state_to_str(sfp->sm_dev_state),
3043 sm_state_to_str(sfp->sm_state));
3044 }
3045
sfp_sm_event(struct sfp * sfp,unsigned int event)3046 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
3047 {
3048 mutex_lock(&sfp->sm_mutex);
3049 __sfp_sm_event(sfp, event);
3050 mutex_unlock(&sfp->sm_mutex);
3051 }
3052
sfp_attach(struct sfp * sfp)3053 static void sfp_attach(struct sfp *sfp)
3054 {
3055 sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
3056 }
3057
sfp_detach(struct sfp * sfp)3058 static void sfp_detach(struct sfp *sfp)
3059 {
3060 sfp_sm_event(sfp, SFP_E_DEV_DETACH);
3061 }
3062
sfp_start(struct sfp * sfp)3063 static void sfp_start(struct sfp *sfp)
3064 {
3065 sfp_sm_event(sfp, SFP_E_DEV_UP);
3066 }
3067
sfp_stop(struct sfp * sfp)3068 static void sfp_stop(struct sfp *sfp)
3069 {
3070 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
3071 }
3072
sfp_set_signal_rate(struct sfp * sfp,unsigned int rate_kbd)3073 static void sfp_set_signal_rate(struct sfp *sfp, unsigned int rate_kbd)
3074 {
3075 unsigned int set;
3076
3077 sfp->rate_kbd = rate_kbd;
3078
3079 if (rate_kbd > sfp->rs_threshold_kbd)
3080 set = sfp->rs_state_mask;
3081 else
3082 set = 0;
3083
3084 sfp_mod_state(sfp, SFP_F_RS0 | SFP_F_RS1, set);
3085 }
3086
sfp_module_info(struct sfp * sfp,struct ethtool_modinfo * modinfo)3087 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
3088 {
3089 /* locking... and check module is present */
3090
3091 if (sfp->id.ext.sff8472_compliance &&
3092 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
3093 modinfo->type = ETH_MODULE_SFF_8472;
3094 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
3095 } else {
3096 modinfo->type = ETH_MODULE_SFF_8079;
3097 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
3098 }
3099 return 0;
3100 }
3101
sfp_module_eeprom(struct sfp * sfp,struct ethtool_eeprom * ee,u8 * data)3102 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
3103 u8 *data)
3104 {
3105 unsigned int first, last, len;
3106 int ret;
3107
3108 if (!(sfp->state & SFP_F_PRESENT))
3109 return -ENODEV;
3110
3111 if (ee->len == 0)
3112 return -EINVAL;
3113
3114 first = ee->offset;
3115 last = ee->offset + ee->len;
3116 if (first < ETH_MODULE_SFF_8079_LEN) {
3117 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
3118 len -= first;
3119
3120 ret = sfp_read(sfp, false, first, data, len);
3121 if (ret < 0)
3122 return ret;
3123
3124 first += len;
3125 data += len;
3126 }
3127 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
3128 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
3129 len -= first;
3130 first -= ETH_MODULE_SFF_8079_LEN;
3131
3132 ret = sfp_read(sfp, true, first, data, len);
3133 if (ret < 0)
3134 return ret;
3135 }
3136 return 0;
3137 }
3138
sfp_module_eeprom_by_page(struct sfp * sfp,const struct ethtool_module_eeprom * page,struct netlink_ext_ack * extack)3139 static int sfp_module_eeprom_by_page(struct sfp *sfp,
3140 const struct ethtool_module_eeprom *page,
3141 struct netlink_ext_ack *extack)
3142 {
3143 if (!(sfp->state & SFP_F_PRESENT))
3144 return -ENODEV;
3145
3146 if (page->bank) {
3147 NL_SET_ERR_MSG(extack, "Banks not supported");
3148 return -EOPNOTSUPP;
3149 }
3150
3151 if (page->page) {
3152 NL_SET_ERR_MSG(extack, "Only page 0 supported");
3153 return -EOPNOTSUPP;
3154 }
3155
3156 if (page->i2c_address != 0x50 &&
3157 page->i2c_address != 0x51) {
3158 NL_SET_ERR_MSG(extack, "Only address 0x50 and 0x51 supported");
3159 return -EOPNOTSUPP;
3160 }
3161
3162 return sfp_read(sfp, page->i2c_address == 0x51, page->offset,
3163 page->data, page->length);
3164 };
3165
3166 static const struct sfp_socket_ops sfp_module_ops = {
3167 .attach = sfp_attach,
3168 .detach = sfp_detach,
3169 .start = sfp_start,
3170 .stop = sfp_stop,
3171 .set_signal_rate = sfp_set_signal_rate,
3172 .module_info = sfp_module_info,
3173 .module_eeprom = sfp_module_eeprom,
3174 .module_eeprom_by_page = sfp_module_eeprom_by_page,
3175 };
3176
sfp_timeout(struct work_struct * work)3177 static void sfp_timeout(struct work_struct *work)
3178 {
3179 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
3180
3181 rtnl_lock();
3182 sfp_sm_event(sfp, SFP_E_TIMEOUT);
3183 rtnl_unlock();
3184 }
3185
sfp_check_state(struct sfp * sfp)3186 static void sfp_check_state(struct sfp *sfp)
3187 {
3188 unsigned int state, i, changed;
3189
3190 rtnl_lock();
3191 mutex_lock(&sfp->st_mutex);
3192 state = sfp_get_state(sfp);
3193 changed = state ^ sfp->state;
3194 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
3195
3196 for (i = 0; i < GPIO_MAX; i++)
3197 if (changed & BIT(i))
3198 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_names[i],
3199 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
3200
3201 state |= sfp->state & SFP_F_OUTPUTS;
3202 sfp->state = state;
3203 mutex_unlock(&sfp->st_mutex);
3204
3205 mutex_lock(&sfp->sm_mutex);
3206 if (changed & SFP_F_PRESENT)
3207 __sfp_sm_event(sfp, state & SFP_F_PRESENT ?
3208 SFP_E_INSERT : SFP_E_REMOVE);
3209
3210 if (changed & SFP_F_TX_FAULT)
3211 __sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
3212 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
3213
3214 if (changed & SFP_F_LOS)
3215 __sfp_sm_event(sfp, state & SFP_F_LOS ?
3216 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
3217 mutex_unlock(&sfp->sm_mutex);
3218 rtnl_unlock();
3219 }
3220
sfp_irq(int irq,void * data)3221 static irqreturn_t sfp_irq(int irq, void *data)
3222 {
3223 struct sfp *sfp = data;
3224
3225 sfp_check_state(sfp);
3226
3227 return IRQ_HANDLED;
3228 }
3229
sfp_poll(struct work_struct * work)3230 static void sfp_poll(struct work_struct *work)
3231 {
3232 struct sfp *sfp = container_of(work, struct sfp, poll.work);
3233
3234 sfp_check_state(sfp);
3235
3236 // st_mutex doesn't need to be held here for state_soft_mask,
3237 // it's unimportant if we race while reading this.
3238 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
3239 sfp->need_poll)
3240 sfp_schedule_poll(sfp);
3241 }
3242
sfp_alloc(struct device * dev)3243 static struct sfp *sfp_alloc(struct device *dev)
3244 {
3245 struct sfp *sfp;
3246
3247 sfp = kzalloc_obj(*sfp);
3248 if (!sfp)
3249 return ERR_PTR(-ENOMEM);
3250
3251 sfp->dev = dev;
3252
3253 mutex_init(&sfp->sm_mutex);
3254 mutex_init(&sfp->st_mutex);
3255 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
3256 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
3257
3258 sfp_hwmon_init(sfp);
3259
3260 return sfp;
3261 }
3262
sfp_cleanup(void * data)3263 static void sfp_cleanup(void *data)
3264 {
3265 struct sfp *sfp = data;
3266
3267 sfp_hwmon_exit(sfp);
3268
3269 cancel_delayed_work_sync(&sfp->poll);
3270 cancel_delayed_work_sync(&sfp->timeout);
3271 if (sfp->i2c_mii) {
3272 mdiobus_unregister(sfp->i2c_mii);
3273 mdiobus_free(sfp->i2c_mii);
3274 }
3275 if (sfp->i2c)
3276 i2c_put_adapter(sfp->i2c);
3277 kfree(sfp);
3278 }
3279
sfp_i2c_get(struct sfp * sfp)3280 static int sfp_i2c_get(struct sfp *sfp)
3281 {
3282 struct fwnode_handle *h;
3283 struct i2c_adapter *i2c;
3284 int err;
3285
3286 h = fwnode_find_reference(dev_fwnode(sfp->dev), "i2c-bus", 0);
3287 if (IS_ERR(h)) {
3288 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
3289 return -ENODEV;
3290 }
3291
3292 i2c = i2c_get_adapter_by_fwnode(h);
3293 if (!i2c) {
3294 err = -EPROBE_DEFER;
3295 goto put;
3296 }
3297
3298 err = sfp_i2c_configure(sfp, i2c);
3299 if (err)
3300 i2c_put_adapter(i2c);
3301 put:
3302 fwnode_handle_put(h);
3303 return err;
3304 }
3305
sfp_probe(struct platform_device * pdev)3306 static int sfp_probe(struct platform_device *pdev)
3307 {
3308 const struct sff_data *sff;
3309 char *sfp_irq_name;
3310 struct sfp *sfp;
3311 int err, i;
3312
3313 sfp = sfp_alloc(&pdev->dev);
3314 if (IS_ERR(sfp))
3315 return PTR_ERR(sfp);
3316
3317 platform_set_drvdata(pdev, sfp);
3318
3319 err = devm_add_action_or_reset(sfp->dev, sfp_cleanup, sfp);
3320 if (err < 0)
3321 return err;
3322
3323 sff = device_get_match_data(sfp->dev);
3324 if (!sff)
3325 sff = &sfp_data;
3326
3327 sfp->type = sff;
3328
3329 err = sfp_i2c_get(sfp);
3330 if (err)
3331 return err;
3332
3333 for (i = 0; i < GPIO_MAX; i++)
3334 if (sff->gpios & BIT(i)) {
3335 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
3336 gpio_names[i], gpio_flags[i]);
3337 if (IS_ERR(sfp->gpio[i]))
3338 return PTR_ERR(sfp->gpio[i]);
3339 }
3340
3341 sfp->state_hw_mask = SFP_F_PRESENT;
3342 sfp->state_hw_drive = SFP_F_TX_DISABLE;
3343
3344 sfp->get_state = sfp_gpio_get_state;
3345 sfp->set_state = sfp_gpio_set_state;
3346
3347 /* An SFP cage with no MOD_DEF0 GPIO has no hardware presence signal.
3348 * Assuming the module is always present traps an empty cage in
3349 * MOD_ERROR and never detects hot-insertion, so derive presence from a
3350 * throttled I2C probe and poll for changes instead. sfp_i2c_configure()
3351 * has already set i2c_max_block_size; seed i2c_block_size so the
3352 * presence read does not issue a zero-length transfer before the first
3353 * EEPROM read. Seed i2c_present_next to jiffies so the first probe
3354 * happens immediately (a zero value would be in the past relative to
3355 * the negative INITIAL_JIFFIES at boot and delay detection).
3356 *
3357 * A soldered-down module (sff,sff) has no presence signal and is
3358 * genuinely always present, so it keeps the always-present behaviour;
3359 * the I2C probe is gated on the cage type advertising SFP_F_PRESENT.
3360 */
3361 if (!sfp->gpio[GPIO_MODDEF0]) {
3362 if (sff->gpios & SFP_F_PRESENT) {
3363 sfp->get_state = sfp_i2c_get_state;
3364 sfp->i2c_block_size = sfp->i2c_max_block_size;
3365 sfp->i2c_present_next = jiffies;
3366 sfp->need_poll = true;
3367 } else {
3368 sfp->get_state = sff_gpio_get_state;
3369 }
3370 }
3371
3372 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
3373 &sfp->max_power_mW);
3374 if (sfp->max_power_mW < 1000) {
3375 if (sfp->max_power_mW)
3376 dev_warn(sfp->dev,
3377 "Firmware bug: host maximum power should be at least 1W\n");
3378 sfp->max_power_mW = 1000;
3379 }
3380
3381 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
3382 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
3383
3384 /* Get the initial state, and always signal TX disable,
3385 * since the network interface will not be up.
3386 */
3387 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
3388
3389 if (sfp->gpio[GPIO_RS0] &&
3390 gpiod_get_value_cansleep(sfp->gpio[GPIO_RS0]))
3391 sfp->state |= SFP_F_RS0;
3392 sfp_set_state(sfp, sfp->state);
3393 sfp_module_tx_disable(sfp);
3394 if (sfp->state & SFP_F_PRESENT) {
3395 rtnl_lock();
3396 sfp_sm_event(sfp, SFP_E_INSERT);
3397 rtnl_unlock();
3398 }
3399
3400 for (i = 0; i < GPIO_MAX; i++) {
3401 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
3402 continue;
3403
3404 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
3405 if (sfp->gpio_irq[i] < 0) {
3406 sfp->gpio_irq[i] = 0;
3407 sfp->need_poll = true;
3408 continue;
3409 }
3410
3411 sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL,
3412 "%s-%s", dev_name(sfp->dev),
3413 gpio_names[i]);
3414
3415 if (!sfp_irq_name)
3416 return -ENOMEM;
3417
3418 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
3419 NULL, sfp_irq,
3420 IRQF_ONESHOT |
3421 IRQF_TRIGGER_RISING |
3422 IRQF_TRIGGER_FALLING,
3423 sfp_irq_name, sfp);
3424 if (err) {
3425 sfp->gpio_irq[i] = 0;
3426 sfp->need_poll = true;
3427 }
3428 }
3429
3430 if (sfp->need_poll)
3431 sfp_schedule_poll(sfp);
3432
3433 /* We could have an issue in cases no Tx disable pin is available or
3434 * wired as modules using a laser as their light source will continue to
3435 * be active when the fiber is removed. This could be a safety issue and
3436 * we should at least warn the user about that.
3437 */
3438 if (!sfp->gpio[GPIO_TX_DISABLE])
3439 dev_warn(sfp->dev,
3440 "No tx_disable pin: SFP modules will always be emitting.\n");
3441
3442 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
3443 if (!sfp->sfp_bus)
3444 return -ENOMEM;
3445
3446 if (sfp->i2c_max_block_size < 2)
3447 dev_warn(sfp->dev,
3448 "Please note:\n"
3449 "This SFP cage is accessed via an SMBus only capable of single byte\n"
3450 "transactions. Some features are disabled, other may be unreliable or\n"
3451 "sporadically fail. Use with caution. There is nothing that the kernel\n"
3452 "or community can do to fix it, the kernel will try best efforts. Please\n"
3453 "verify any problems on hardware that supports multi-byte I2C transactions.\n");
3454
3455 sfp_debugfs_init(sfp);
3456
3457 return 0;
3458 }
3459
sfp_remove(struct platform_device * pdev)3460 static void sfp_remove(struct platform_device *pdev)
3461 {
3462 struct sfp *sfp = platform_get_drvdata(pdev);
3463
3464 sfp_debugfs_exit(sfp);
3465 sfp_unregister_socket(sfp->sfp_bus);
3466
3467 rtnl_lock();
3468 sfp_sm_event(sfp, SFP_E_REMOVE);
3469 rtnl_unlock();
3470 }
3471
sfp_shutdown(struct platform_device * pdev)3472 static void sfp_shutdown(struct platform_device *pdev)
3473 {
3474 struct sfp *sfp = platform_get_drvdata(pdev);
3475 int i;
3476
3477 for (i = 0; i < GPIO_MAX; i++) {
3478 if (!sfp->gpio_irq[i])
3479 continue;
3480
3481 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
3482 }
3483
3484 cancel_delayed_work_sync(&sfp->poll);
3485 cancel_delayed_work_sync(&sfp->timeout);
3486 }
3487
3488 static struct platform_driver sfp_driver = {
3489 .probe = sfp_probe,
3490 .remove = sfp_remove,
3491 .shutdown = sfp_shutdown,
3492 .driver = {
3493 .name = "sfp",
3494 .of_match_table = sfp_of_match,
3495 },
3496 };
3497
3498 module_platform_driver(sfp_driver);
3499
3500 MODULE_ALIAS("platform:sfp");
3501 MODULE_AUTHOR("Russell King");
3502 MODULE_LICENSE("GPL v2");
3503 MODULE_DESCRIPTION("SFP cage support");
3504