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