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