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