blob: 7bdfcde98266c258d34a89ff8065132dca14ae85 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002#include <linux/acpi.h>
Andrew Lunn13230612018-07-17 21:48:13 +02003#include <linux/ctype.h>
Russell King73970052017-07-25 15:03:39 +01004#include <linux/delay.h>
Florian Fainelli54a2fc62017-10-30 21:42:58 -07005#include <linux/gpio/consumer.h>
Andrew Lunn13230612018-07-17 21:48:13 +02006#include <linux/hwmon.h>
Russell King73970052017-07-25 15:03:39 +01007#include <linux/i2c.h>
8#include <linux/interrupt.h>
9#include <linux/jiffies.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 "mdio-i2c.h"
20#include "sfp.h"
21#include "swphy.h"
22
23enum {
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,
Russell King6b0da5c2019-11-10 14:07:14 +000039 SFP_E_DEV_ATTACH,
40 SFP_E_DEV_DETACH,
Russell King73970052017-07-25 15:03:39 +010041 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,
Russell King73f5e842019-11-10 14:07:25 +000050 SFP_MOD_ERROR,
Russell King73970052017-07-25 15:03:39 +010051 SFP_MOD_PROBE,
Russell King73f5e842019-11-10 14:07:25 +000052 SFP_MOD_WAITDEV,
Jon Nettleton3bb35262018-02-27 15:53:12 +000053 SFP_MOD_HPOWER,
Russell Kingb036a552019-11-10 14:07:20 +000054 SFP_MOD_WAITPWR,
Russell King73970052017-07-25 15:03:39 +010055 SFP_MOD_PRESENT,
Russell King73970052017-07-25 15:03:39 +010056
Russell King6b0da5c2019-11-10 14:07:14 +000057 SFP_DEV_DETACHED = 0,
58 SFP_DEV_DOWN,
Russell King73970052017-07-25 15:03:39 +010059 SFP_DEV_UP,
60
61 SFP_S_DOWN = 0,
Russell King74c551c2019-12-11 10:56:09 +000062 SFP_S_FAIL,
Russell Kingeefa6f12019-11-10 14:06:59 +000063 SFP_S_WAIT,
Russell King73970052017-07-25 15:03:39 +010064 SFP_S_INIT,
Russell King1cb89a12019-12-09 14:16:11 +000065 SFP_S_INIT_PHY,
Russell Kingd23751a2019-11-10 14:07:09 +000066 SFP_S_INIT_TX_FAULT,
Russell King73970052017-07-25 15:03:39 +010067 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
Andrew Lunn4005a7c2018-08-08 20:54:12 +020074static const char * const mod_state_strings[] = {
75 [SFP_MOD_EMPTY] = "empty",
Russell King73f5e842019-11-10 14:07:25 +000076 [SFP_MOD_ERROR] = "error",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020077 [SFP_MOD_PROBE] = "probe",
Russell King73f5e842019-11-10 14:07:25 +000078 [SFP_MOD_WAITDEV] = "waitdev",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020079 [SFP_MOD_HPOWER] = "hpower",
Russell Kingb036a552019-11-10 14:07:20 +000080 [SFP_MOD_WAITPWR] = "waitpwr",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020081 [SFP_MOD_PRESENT] = "present",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020082};
83
84static 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
91static const char * const dev_state_strings[] = {
Russell King6b0da5c2019-11-10 14:07:14 +000092 [SFP_DEV_DETACHED] = "detached",
Andrew Lunn4005a7c2018-08-08 20:54:12 +020093 [SFP_DEV_DOWN] = "down",
94 [SFP_DEV_UP] = "up",
95};
96
97static 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
104static const char * const event_strings[] = {
105 [SFP_E_INSERT] = "insert",
106 [SFP_E_REMOVE] = "remove",
Russell King6b0da5c2019-11-10 14:07:14 +0000107 [SFP_E_DEV_ATTACH] = "dev_attach",
108 [SFP_E_DEV_DETACH] = "dev_detach",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200109 [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
118static 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
125static const char * const sm_state_strings[] = {
126 [SFP_S_DOWN] = "down",
Russell King74c551c2019-12-11 10:56:09 +0000127 [SFP_S_FAIL] = "fail",
Russell Kingeefa6f12019-11-10 14:06:59 +0000128 [SFP_S_WAIT] = "wait",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200129 [SFP_S_INIT] = "init",
Russell King1cb89a12019-12-09 14:16:11 +0000130 [SFP_S_INIT_PHY] = "init_phy",
Russell Kingd23751a2019-11-10 14:07:09 +0000131 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
Andrew Lunn4005a7c2018-08-08 20:54:12 +0200132 [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] = "rx_disable",
137};
138
139static 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
Russell King73970052017-07-25 15:03:39 +0100146static const char *gpio_of_names[] = {
Baruch Siach25ee0792017-09-07 12:25:50 +0300147 "mod-def0",
Russell King73970052017-07-25 15:03:39 +0100148 "los",
149 "tx-fault",
150 "tx-disable",
Baruch Siach25ee0792017-09-07 12:25:50 +0300151 "rate-select0",
Russell King73970052017-07-25 15:03:39 +0100152};
153
154static const enum gpiod_flags gpio_flags[] = {
155 GPIOD_IN,
156 GPIOD_IN,
157 GPIOD_IN,
158 GPIOD_ASIS,
159 GPIOD_ASIS,
160};
161
Russell King26c97a22019-12-09 13:40:23 +0000162/* 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)
Russell King73970052017-07-25 15:03:39 +0100176
Russell King65ef2d52019-12-09 14:15:55 +0000177/* 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
Russell King1cb89a12019-12-09 14:16:11 +0000185/* 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
Russell King73970052017-07-25 15:03:39 +0100191/* 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 *
Russell Kingd9009542019-11-10 14:06:33 +0000195 * 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.
Russell King73970052017-07-25 15:03:39 +0100197 */
Russell Kinge117be72019-11-10 14:07:30 +0000198#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
Russell King73970052017-07-25 15:03:39 +0100204
Florian Fainelli516b29e2017-10-30 21:42:57 -0700205/* SFP modules appear to always have their PHY configured for bus address
Russell King73970052017-07-25 15:03:39 +0100206 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
207 */
208#define SFP_PHY_ADDR 22
209
Russell King259c8612017-12-14 10:27:47 +0000210struct sff_data {
211 unsigned int gpios;
212 bool (*module_supported)(const struct sfp_eeprom_id *id);
213};
214
Russell King73970052017-07-25 15:03:39 +0100215struct 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;
Russell King259c8612017-12-14 10:27:47 +0000221 const struct sff_data *type;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000222 u32 max_power_mW;
Russell King73970052017-07-25 15:03:39 +0100223
224 unsigned int (*get_state)(struct sfp *);
225 void (*set_state)(struct sfp *, unsigned int);
226 int (*read)(struct sfp *, bool, u8, void *, size_t);
Jon Nettleton3bb35262018-02-27 15:53:12 +0000227 int (*write)(struct sfp *, bool, u8, void *, size_t);
Russell King73970052017-07-25 15:03:39 +0100228
229 struct gpio_desc *gpio[GPIO_MAX];
Robert Hancock257c2552019-06-07 10:42:35 -0600230 int gpio_irq[GPIO_MAX];
Russell King73970052017-07-25 15:03:39 +0100231
Russell Kingf3c9a662019-11-20 12:29:59 +0000232 bool need_poll;
233
Robert Hancock2158e852019-06-07 10:42:36 -0600234 struct mutex st_mutex; /* Protects state */
Russell Kingf3c9a662019-11-20 12:29:59 +0000235 unsigned int state_soft_mask;
Russell King73970052017-07-25 15:03:39 +0100236 unsigned int state;
237 struct delayed_work poll;
238 struct delayed_work timeout;
Robert Hancock2158e852019-06-07 10:42:36 -0600239 struct mutex sm_mutex; /* Protects state machine */
Russell King73970052017-07-25 15:03:39 +0100240 unsigned char sm_mod_state;
Russell Kinge117be72019-11-10 14:07:30 +0000241 unsigned char sm_mod_tries_init;
242 unsigned char sm_mod_tries;
Russell King73970052017-07-25 15:03:39 +0100243 unsigned char sm_dev_state;
244 unsigned short sm_state;
Russell King281e4ea2019-12-09 14:16:00 +0000245 unsigned char sm_fault_retries;
Russell King1cb89a12019-12-09 14:16:11 +0000246 unsigned char sm_phy_retries;
Russell King73970052017-07-25 15:03:39 +0100247
248 struct sfp_eeprom_id id;
Russell Kinged32abb2019-11-10 14:06:39 +0000249 unsigned int module_power_mW;
Russell King26c97a22019-12-09 13:40:23 +0000250 unsigned int module_t_start_up;
Russell Kinged32abb2019-11-10 14:06:39 +0000251
Andrew Lunn13230612018-07-17 21:48:13 +0200252#if IS_ENABLED(CONFIG_HWMON)
253 struct sfp_diag diag;
Russell King139d3a22019-11-10 14:07:35 +0000254 struct delayed_work hwmon_probe;
255 unsigned int hwmon_tries;
Andrew Lunn13230612018-07-17 21:48:13 +0200256 struct device *hwmon_dev;
257 char *hwmon_name;
258#endif
259
Russell King73970052017-07-25 15:03:39 +0100260};
261
Russell King259c8612017-12-14 10:27:47 +0000262static bool sff_module_supported(const struct sfp_eeprom_id *id)
263{
Russell King0fbd26a2019-12-11 10:56:04 +0000264 return id->base.phys_id == SFF8024_ID_SFF_8472 &&
Russell King259c8612017-12-14 10:27:47 +0000265 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
266}
267
268static const struct sff_data sff_data = {
269 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
270 .module_supported = sff_module_supported,
271};
272
273static bool sfp_module_supported(const struct sfp_eeprom_id *id)
274{
Russell King0fbd26a2019-12-11 10:56:04 +0000275 return id->base.phys_id == SFF8024_ID_SFP &&
Russell King259c8612017-12-14 10:27:47 +0000276 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
277}
278
279static const struct sff_data sfp_data = {
280 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
281 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
282 .module_supported = sfp_module_supported,
283};
284
285static const struct of_device_id sfp_of_match[] = {
286 { .compatible = "sff,sff", .data = &sff_data, },
287 { .compatible = "sff,sfp", .data = &sfp_data, },
288 { },
289};
290MODULE_DEVICE_TABLE(of, sfp_of_match);
291
Russell King73970052017-07-25 15:03:39 +0100292static unsigned long poll_jiffies;
293
294static unsigned int sfp_gpio_get_state(struct sfp *sfp)
295{
296 unsigned int i, state, v;
297
298 for (i = state = 0; i < GPIO_MAX; i++) {
299 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
300 continue;
301
302 v = gpiod_get_value_cansleep(sfp->gpio[i]);
303 if (v)
304 state |= BIT(i);
305 }
306
307 return state;
308}
309
Russell King259c8612017-12-14 10:27:47 +0000310static unsigned int sff_gpio_get_state(struct sfp *sfp)
311{
312 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
313}
314
Russell King73970052017-07-25 15:03:39 +0100315static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
316{
317 if (state & SFP_F_PRESENT) {
318 /* If the module is present, drive the signals */
319 if (sfp->gpio[GPIO_TX_DISABLE])
320 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700321 state & SFP_F_TX_DISABLE);
Russell King73970052017-07-25 15:03:39 +0100322 if (state & SFP_F_RATE_SELECT)
323 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700324 state & SFP_F_RATE_SELECT);
Russell King73970052017-07-25 15:03:39 +0100325 } else {
326 /* Otherwise, let them float to the pull-ups */
327 if (sfp->gpio[GPIO_TX_DISABLE])
328 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
329 if (state & SFP_F_RATE_SELECT)
330 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
331 }
332}
333
Jon Nettleton3bb35262018-02-27 15:53:12 +0000334static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
335 size_t len)
Russell King73970052017-07-25 15:03:39 +0100336{
337 struct i2c_msg msgs[2];
Jon Nettleton3bb35262018-02-27 15:53:12 +0000338 u8 bus_addr = a2 ? 0x51 : 0x50;
Russell King28e74a72019-06-02 15:13:00 +0100339 size_t this_len;
Russell King73970052017-07-25 15:03:39 +0100340 int ret;
341
342 msgs[0].addr = bus_addr;
343 msgs[0].flags = 0;
344 msgs[0].len = 1;
345 msgs[0].buf = &dev_addr;
346 msgs[1].addr = bus_addr;
347 msgs[1].flags = I2C_M_RD;
348 msgs[1].len = len;
349 msgs[1].buf = buf;
350
Russell King28e74a72019-06-02 15:13:00 +0100351 while (len) {
352 this_len = len;
353 if (this_len > 16)
354 this_len = 16;
Russell King73970052017-07-25 15:03:39 +0100355
Russell King28e74a72019-06-02 15:13:00 +0100356 msgs[1].len = this_len;
357
358 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
359 if (ret < 0)
360 return ret;
361
362 if (ret != ARRAY_SIZE(msgs))
363 break;
364
365 msgs[1].buf += this_len;
366 dev_addr += this_len;
367 len -= this_len;
368 }
369
370 return msgs[1].buf - (u8 *)buf;
Russell King73970052017-07-25 15:03:39 +0100371}
372
Jon Nettleton3bb35262018-02-27 15:53:12 +0000373static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
374 size_t len)
Russell King73970052017-07-25 15:03:39 +0100375{
Jon Nettleton3bb35262018-02-27 15:53:12 +0000376 struct i2c_msg msgs[1];
377 u8 bus_addr = a2 ? 0x51 : 0x50;
378 int ret;
379
380 msgs[0].addr = bus_addr;
381 msgs[0].flags = 0;
382 msgs[0].len = 1 + len;
383 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
384 if (!msgs[0].buf)
385 return -ENOMEM;
386
387 msgs[0].buf[0] = dev_addr;
388 memcpy(&msgs[0].buf[1], buf, len);
389
390 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
391
392 kfree(msgs[0].buf);
393
394 if (ret < 0)
395 return ret;
396
397 return ret == ARRAY_SIZE(msgs) ? len : 0;
Russell King73970052017-07-25 15:03:39 +0100398}
399
400static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
401{
402 struct mii_bus *i2c_mii;
403 int ret;
404
405 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
406 return -EINVAL;
407
408 sfp->i2c = i2c;
409 sfp->read = sfp_i2c_read;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000410 sfp->write = sfp_i2c_write;
Russell King73970052017-07-25 15:03:39 +0100411
412 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
413 if (IS_ERR(i2c_mii))
414 return PTR_ERR(i2c_mii);
415
416 i2c_mii->name = "SFP I2C Bus";
417 i2c_mii->phy_mask = ~0;
418
419 ret = mdiobus_register(i2c_mii);
420 if (ret < 0) {
421 mdiobus_free(i2c_mii);
422 return ret;
423 }
424
425 sfp->i2c_mii = i2c_mii;
426
427 return 0;
428}
429
Russell King73970052017-07-25 15:03:39 +0100430/* Interface */
Russell King73970052017-07-25 15:03:39 +0100431static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
432{
433 return sfp->read(sfp, a2, addr, buf, len);
434}
435
Jon Nettleton3bb35262018-02-27 15:53:12 +0000436static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
437{
438 return sfp->write(sfp, a2, addr, buf, len);
439}
440
Russell Kingf3c9a662019-11-20 12:29:59 +0000441static unsigned int sfp_soft_get_state(struct sfp *sfp)
442{
443 unsigned int state = 0;
444 u8 status;
Russell King0dea4d02019-12-17 13:50:29 +0000445 int ret;
Russell Kingf3c9a662019-11-20 12:29:59 +0000446
Russell King0dea4d02019-12-17 13:50:29 +0000447 ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status));
448 if (ret == sizeof(status)) {
Russell Kingf3c9a662019-11-20 12:29:59 +0000449 if (status & SFP_STATUS_RX_LOS)
450 state |= SFP_F_LOS;
451 if (status & SFP_STATUS_TX_FAULT)
452 state |= SFP_F_TX_FAULT;
Russell King0dea4d02019-12-17 13:50:29 +0000453 } else {
454 dev_err_ratelimited(sfp->dev,
455 "failed to read SFP soft status: %d\n",
456 ret);
457 /* Preserve the current state */
458 state = sfp->state;
Russell Kingf3c9a662019-11-20 12:29:59 +0000459 }
460
461 return state & sfp->state_soft_mask;
462}
463
464static void sfp_soft_set_state(struct sfp *sfp, unsigned int state)
465{
466 u8 status;
467
468 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
469 sizeof(status)) {
470 if (state & SFP_F_TX_DISABLE)
471 status |= SFP_STATUS_TX_DISABLE_FORCE;
472 else
473 status &= ~SFP_STATUS_TX_DISABLE_FORCE;
474
475 sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status));
476 }
477}
478
479static void sfp_soft_start_poll(struct sfp *sfp)
480{
481 const struct sfp_eeprom_id *id = &sfp->id;
482
483 sfp->state_soft_mask = 0;
484 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
485 !sfp->gpio[GPIO_TX_DISABLE])
486 sfp->state_soft_mask |= SFP_F_TX_DISABLE;
487 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
488 !sfp->gpio[GPIO_TX_FAULT])
489 sfp->state_soft_mask |= SFP_F_TX_FAULT;
490 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
491 !sfp->gpio[GPIO_LOS])
492 sfp->state_soft_mask |= SFP_F_LOS;
493
494 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
495 !sfp->need_poll)
496 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
497}
498
499static void sfp_soft_stop_poll(struct sfp *sfp)
500{
501 sfp->state_soft_mask = 0;
502}
503
504static unsigned int sfp_get_state(struct sfp *sfp)
505{
506 unsigned int state = sfp->get_state(sfp);
507
508 if (state & SFP_F_PRESENT &&
509 sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
510 state |= sfp_soft_get_state(sfp);
511
512 return state;
513}
514
515static void sfp_set_state(struct sfp *sfp, unsigned int state)
516{
517 sfp->set_state(sfp, state);
518
519 if (state & SFP_F_PRESENT &&
520 sfp->state_soft_mask & SFP_F_TX_DISABLE)
521 sfp_soft_set_state(sfp, state);
522}
523
Russell King73970052017-07-25 15:03:39 +0100524static unsigned int sfp_check(void *buf, size_t len)
525{
526 u8 *p, check;
527
528 for (p = buf, check = 0; len; p++, len--)
529 check += *p;
530
531 return check;
532}
533
Andrew Lunn13230612018-07-17 21:48:13 +0200534/* hwmon */
535#if IS_ENABLED(CONFIG_HWMON)
536static umode_t sfp_hwmon_is_visible(const void *data,
537 enum hwmon_sensor_types type,
538 u32 attr, int channel)
539{
540 const struct sfp *sfp = data;
541
542 switch (type) {
543 case hwmon_temp:
544 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200545 case hwmon_temp_min_alarm:
546 case hwmon_temp_max_alarm:
547 case hwmon_temp_lcrit_alarm:
548 case hwmon_temp_crit_alarm:
549 case hwmon_temp_min:
550 case hwmon_temp_max:
551 case hwmon_temp_lcrit:
552 case hwmon_temp_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200553 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
554 return 0;
555 /* fall through */
556 case hwmon_temp_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200557 case hwmon_temp_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200558 return 0444;
559 default:
560 return 0;
561 }
562 case hwmon_in:
563 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200564 case hwmon_in_min_alarm:
565 case hwmon_in_max_alarm:
566 case hwmon_in_lcrit_alarm:
567 case hwmon_in_crit_alarm:
568 case hwmon_in_min:
569 case hwmon_in_max:
570 case hwmon_in_lcrit:
571 case hwmon_in_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200572 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
573 return 0;
574 /* fall through */
575 case hwmon_in_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200576 case hwmon_in_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200577 return 0444;
578 default:
579 return 0;
580 }
581 case hwmon_curr:
582 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200583 case hwmon_curr_min_alarm:
584 case hwmon_curr_max_alarm:
585 case hwmon_curr_lcrit_alarm:
586 case hwmon_curr_crit_alarm:
587 case hwmon_curr_min:
588 case hwmon_curr_max:
589 case hwmon_curr_lcrit:
590 case hwmon_curr_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200591 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
592 return 0;
593 /* fall through */
594 case hwmon_curr_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200595 case hwmon_curr_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200596 return 0444;
597 default:
598 return 0;
599 }
600 case hwmon_power:
601 /* External calibration of receive power requires
602 * floating point arithmetic. Doing that in the kernel
603 * is not easy, so just skip it. If the module does
604 * not require external calibration, we can however
605 * show receiver power, since FP is then not needed.
606 */
607 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
608 channel == 1)
609 return 0;
610 switch (attr) {
Andrew Lunn13230612018-07-17 21:48:13 +0200611 case hwmon_power_min_alarm:
612 case hwmon_power_max_alarm:
613 case hwmon_power_lcrit_alarm:
614 case hwmon_power_crit_alarm:
615 case hwmon_power_min:
616 case hwmon_power_max:
617 case hwmon_power_lcrit:
618 case hwmon_power_crit:
Andrew Lunna33710b2018-09-04 04:23:56 +0200619 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
620 return 0;
621 /* fall through */
622 case hwmon_power_input:
Andrew Lunnc1236972019-08-25 01:04:17 +0200623 case hwmon_power_label:
Andrew Lunn13230612018-07-17 21:48:13 +0200624 return 0444;
625 default:
626 return 0;
627 }
628 default:
629 return 0;
630 }
631}
632
633static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
634{
635 __be16 val;
636 int err;
637
638 err = sfp_read(sfp, true, reg, &val, sizeof(val));
639 if (err < 0)
640 return err;
641
642 *value = be16_to_cpu(val);
643
644 return 0;
645}
646
647static void sfp_hwmon_to_rx_power(long *value)
648{
Andrew Lunn0cea0e12019-07-21 18:50:08 +0200649 *value = DIV_ROUND_CLOSEST(*value, 10);
Andrew Lunn13230612018-07-17 21:48:13 +0200650}
651
652static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
653 long *value)
654{
655 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
656 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
657}
658
659static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
660{
661 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
662 be16_to_cpu(sfp->diag.cal_t_offset), value);
663
664 if (*value >= 0x8000)
665 *value -= 0x10000;
666
667 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
668}
669
670static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
671{
672 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
673 be16_to_cpu(sfp->diag.cal_v_offset), value);
674
675 *value = DIV_ROUND_CLOSEST(*value, 10);
676}
677
678static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
679{
680 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
681 be16_to_cpu(sfp->diag.cal_txi_offset), value);
682
683 *value = DIV_ROUND_CLOSEST(*value, 500);
684}
685
686static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
687{
688 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
689 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
690
691 *value = DIV_ROUND_CLOSEST(*value, 10);
692}
693
694static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
695{
696 int err;
697
698 err = sfp_hwmon_read_sensor(sfp, reg, value);
699 if (err < 0)
700 return err;
701
702 sfp_hwmon_calibrate_temp(sfp, value);
703
704 return 0;
705}
706
707static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
708{
709 int err;
710
711 err = sfp_hwmon_read_sensor(sfp, reg, value);
712 if (err < 0)
713 return err;
714
715 sfp_hwmon_calibrate_vcc(sfp, value);
716
717 return 0;
718}
719
720static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
721{
722 int err;
723
724 err = sfp_hwmon_read_sensor(sfp, reg, value);
725 if (err < 0)
726 return err;
727
728 sfp_hwmon_calibrate_bias(sfp, value);
729
730 return 0;
731}
732
733static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
734{
735 int err;
736
737 err = sfp_hwmon_read_sensor(sfp, reg, value);
738 if (err < 0)
739 return err;
740
741 sfp_hwmon_calibrate_tx_power(sfp, value);
742
743 return 0;
744}
745
746static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
747{
748 int err;
749
750 err = sfp_hwmon_read_sensor(sfp, reg, value);
751 if (err < 0)
752 return err;
753
754 sfp_hwmon_to_rx_power(value);
755
756 return 0;
757}
758
759static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
760{
761 u8 status;
762 int err;
763
764 switch (attr) {
765 case hwmon_temp_input:
766 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
767
768 case hwmon_temp_lcrit:
769 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
770 sfp_hwmon_calibrate_temp(sfp, value);
771 return 0;
772
773 case hwmon_temp_min:
774 *value = be16_to_cpu(sfp->diag.temp_low_warn);
775 sfp_hwmon_calibrate_temp(sfp, value);
776 return 0;
777 case hwmon_temp_max:
778 *value = be16_to_cpu(sfp->diag.temp_high_warn);
779 sfp_hwmon_calibrate_temp(sfp, value);
780 return 0;
781
782 case hwmon_temp_crit:
783 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
784 sfp_hwmon_calibrate_temp(sfp, value);
785 return 0;
786
787 case hwmon_temp_lcrit_alarm:
788 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
789 if (err < 0)
790 return err;
791
792 *value = !!(status & SFP_ALARM0_TEMP_LOW);
793 return 0;
794
795 case hwmon_temp_min_alarm:
796 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
797 if (err < 0)
798 return err;
799
800 *value = !!(status & SFP_WARN0_TEMP_LOW);
801 return 0;
802
803 case hwmon_temp_max_alarm:
804 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
805 if (err < 0)
806 return err;
807
808 *value = !!(status & SFP_WARN0_TEMP_HIGH);
809 return 0;
810
811 case hwmon_temp_crit_alarm:
812 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
813 if (err < 0)
814 return err;
815
816 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
817 return 0;
818 default:
819 return -EOPNOTSUPP;
820 }
821
822 return -EOPNOTSUPP;
823}
824
825static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
826{
827 u8 status;
828 int err;
829
830 switch (attr) {
831 case hwmon_in_input:
832 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
833
834 case hwmon_in_lcrit:
835 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
836 sfp_hwmon_calibrate_vcc(sfp, value);
837 return 0;
838
839 case hwmon_in_min:
840 *value = be16_to_cpu(sfp->diag.volt_low_warn);
841 sfp_hwmon_calibrate_vcc(sfp, value);
842 return 0;
843
844 case hwmon_in_max:
845 *value = be16_to_cpu(sfp->diag.volt_high_warn);
846 sfp_hwmon_calibrate_vcc(sfp, value);
847 return 0;
848
849 case hwmon_in_crit:
850 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
851 sfp_hwmon_calibrate_vcc(sfp, value);
852 return 0;
853
854 case hwmon_in_lcrit_alarm:
855 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
856 if (err < 0)
857 return err;
858
859 *value = !!(status & SFP_ALARM0_VCC_LOW);
860 return 0;
861
862 case hwmon_in_min_alarm:
863 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
864 if (err < 0)
865 return err;
866
867 *value = !!(status & SFP_WARN0_VCC_LOW);
868 return 0;
869
870 case hwmon_in_max_alarm:
871 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
872 if (err < 0)
873 return err;
874
875 *value = !!(status & SFP_WARN0_VCC_HIGH);
876 return 0;
877
878 case hwmon_in_crit_alarm:
879 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
880 if (err < 0)
881 return err;
882
883 *value = !!(status & SFP_ALARM0_VCC_HIGH);
884 return 0;
885 default:
886 return -EOPNOTSUPP;
887 }
888
889 return -EOPNOTSUPP;
890}
891
892static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
893{
894 u8 status;
895 int err;
896
897 switch (attr) {
898 case hwmon_curr_input:
899 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
900
901 case hwmon_curr_lcrit:
902 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
903 sfp_hwmon_calibrate_bias(sfp, value);
904 return 0;
905
906 case hwmon_curr_min:
907 *value = be16_to_cpu(sfp->diag.bias_low_warn);
908 sfp_hwmon_calibrate_bias(sfp, value);
909 return 0;
910
911 case hwmon_curr_max:
912 *value = be16_to_cpu(sfp->diag.bias_high_warn);
913 sfp_hwmon_calibrate_bias(sfp, value);
914 return 0;
915
916 case hwmon_curr_crit:
917 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
918 sfp_hwmon_calibrate_bias(sfp, value);
919 return 0;
920
921 case hwmon_curr_lcrit_alarm:
922 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
923 if (err < 0)
924 return err;
925
926 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
927 return 0;
928
929 case hwmon_curr_min_alarm:
930 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
931 if (err < 0)
932 return err;
933
934 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
935 return 0;
936
937 case hwmon_curr_max_alarm:
938 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
939 if (err < 0)
940 return err;
941
942 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
943 return 0;
944
945 case hwmon_curr_crit_alarm:
946 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
947 if (err < 0)
948 return err;
949
950 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
951 return 0;
952 default:
953 return -EOPNOTSUPP;
954 }
955
956 return -EOPNOTSUPP;
957}
958
959static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
960{
961 u8 status;
962 int err;
963
964 switch (attr) {
965 case hwmon_power_input:
966 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
967
968 case hwmon_power_lcrit:
969 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
970 sfp_hwmon_calibrate_tx_power(sfp, value);
971 return 0;
972
973 case hwmon_power_min:
974 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
975 sfp_hwmon_calibrate_tx_power(sfp, value);
976 return 0;
977
978 case hwmon_power_max:
979 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
980 sfp_hwmon_calibrate_tx_power(sfp, value);
981 return 0;
982
983 case hwmon_power_crit:
984 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
985 sfp_hwmon_calibrate_tx_power(sfp, value);
986 return 0;
987
988 case hwmon_power_lcrit_alarm:
989 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
990 if (err < 0)
991 return err;
992
993 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
994 return 0;
995
996 case hwmon_power_min_alarm:
997 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
998 if (err < 0)
999 return err;
1000
1001 *value = !!(status & SFP_WARN0_TXPWR_LOW);
1002 return 0;
1003
1004 case hwmon_power_max_alarm:
1005 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1006 if (err < 0)
1007 return err;
1008
1009 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
1010 return 0;
1011
1012 case hwmon_power_crit_alarm:
1013 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1014 if (err < 0)
1015 return err;
1016
1017 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
1018 return 0;
1019 default:
1020 return -EOPNOTSUPP;
1021 }
1022
1023 return -EOPNOTSUPP;
1024}
1025
1026static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
1027{
1028 u8 status;
1029 int err;
1030
1031 switch (attr) {
1032 case hwmon_power_input:
1033 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
1034
1035 case hwmon_power_lcrit:
1036 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1037 sfp_hwmon_to_rx_power(value);
1038 return 0;
1039
1040 case hwmon_power_min:
1041 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1042 sfp_hwmon_to_rx_power(value);
1043 return 0;
1044
1045 case hwmon_power_max:
1046 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1047 sfp_hwmon_to_rx_power(value);
1048 return 0;
1049
1050 case hwmon_power_crit:
1051 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1052 sfp_hwmon_to_rx_power(value);
1053 return 0;
1054
1055 case hwmon_power_lcrit_alarm:
1056 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1057 if (err < 0)
1058 return err;
1059
1060 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
1061 return 0;
1062
1063 case hwmon_power_min_alarm:
1064 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1065 if (err < 0)
1066 return err;
1067
1068 *value = !!(status & SFP_WARN1_RXPWR_LOW);
1069 return 0;
1070
1071 case hwmon_power_max_alarm:
1072 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1073 if (err < 0)
1074 return err;
1075
1076 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
1077 return 0;
1078
1079 case hwmon_power_crit_alarm:
1080 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1081 if (err < 0)
1082 return err;
1083
1084 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1085 return 0;
1086 default:
1087 return -EOPNOTSUPP;
1088 }
1089
1090 return -EOPNOTSUPP;
1091}
1092
1093static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1094 u32 attr, int channel, long *value)
1095{
1096 struct sfp *sfp = dev_get_drvdata(dev);
1097
1098 switch (type) {
1099 case hwmon_temp:
1100 return sfp_hwmon_temp(sfp, attr, value);
1101 case hwmon_in:
1102 return sfp_hwmon_vcc(sfp, attr, value);
1103 case hwmon_curr:
1104 return sfp_hwmon_bias(sfp, attr, value);
1105 case hwmon_power:
1106 switch (channel) {
1107 case 0:
1108 return sfp_hwmon_tx_power(sfp, attr, value);
1109 case 1:
1110 return sfp_hwmon_rx_power(sfp, attr, value);
1111 default:
1112 return -EOPNOTSUPP;
1113 }
1114 default:
1115 return -EOPNOTSUPP;
1116 }
1117}
1118
Andrew Lunnc1236972019-08-25 01:04:17 +02001119static const char *const sfp_hwmon_power_labels[] = {
1120 "TX_power",
1121 "RX_power",
1122};
1123
1124static int sfp_hwmon_read_string(struct device *dev,
1125 enum hwmon_sensor_types type,
1126 u32 attr, int channel, const char **str)
1127{
1128 switch (type) {
1129 case hwmon_curr:
1130 switch (attr) {
1131 case hwmon_curr_label:
1132 *str = "bias";
1133 return 0;
1134 default:
1135 return -EOPNOTSUPP;
1136 }
1137 break;
1138 case hwmon_temp:
1139 switch (attr) {
1140 case hwmon_temp_label:
1141 *str = "temperature";
1142 return 0;
1143 default:
1144 return -EOPNOTSUPP;
1145 }
1146 break;
1147 case hwmon_in:
1148 switch (attr) {
1149 case hwmon_in_label:
1150 *str = "VCC";
1151 return 0;
1152 default:
1153 return -EOPNOTSUPP;
1154 }
1155 break;
1156 case hwmon_power:
1157 switch (attr) {
1158 case hwmon_power_label:
1159 *str = sfp_hwmon_power_labels[channel];
1160 return 0;
1161 default:
1162 return -EOPNOTSUPP;
1163 }
1164 break;
1165 default:
1166 return -EOPNOTSUPP;
1167 }
1168
1169 return -EOPNOTSUPP;
1170}
1171
Andrew Lunn13230612018-07-17 21:48:13 +02001172static const struct hwmon_ops sfp_hwmon_ops = {
1173 .is_visible = sfp_hwmon_is_visible,
1174 .read = sfp_hwmon_read,
Andrew Lunnc1236972019-08-25 01:04:17 +02001175 .read_string = sfp_hwmon_read_string,
Andrew Lunn13230612018-07-17 21:48:13 +02001176};
1177
1178static u32 sfp_hwmon_chip_config[] = {
1179 HWMON_C_REGISTER_TZ,
1180 0,
1181};
1182
1183static const struct hwmon_channel_info sfp_hwmon_chip = {
1184 .type = hwmon_chip,
1185 .config = sfp_hwmon_chip_config,
1186};
1187
1188static u32 sfp_hwmon_temp_config[] = {
1189 HWMON_T_INPUT |
1190 HWMON_T_MAX | HWMON_T_MIN |
1191 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1192 HWMON_T_CRIT | HWMON_T_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001193 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1194 HWMON_T_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001195 0,
1196};
1197
1198static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1199 .type = hwmon_temp,
1200 .config = sfp_hwmon_temp_config,
1201};
1202
1203static u32 sfp_hwmon_vcc_config[] = {
1204 HWMON_I_INPUT |
1205 HWMON_I_MAX | HWMON_I_MIN |
1206 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1207 HWMON_I_CRIT | HWMON_I_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001208 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1209 HWMON_I_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001210 0,
1211};
1212
1213static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1214 .type = hwmon_in,
1215 .config = sfp_hwmon_vcc_config,
1216};
1217
1218static u32 sfp_hwmon_bias_config[] = {
1219 HWMON_C_INPUT |
1220 HWMON_C_MAX | HWMON_C_MIN |
1221 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1222 HWMON_C_CRIT | HWMON_C_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001223 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1224 HWMON_C_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001225 0,
1226};
1227
1228static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1229 .type = hwmon_curr,
1230 .config = sfp_hwmon_bias_config,
1231};
1232
1233static u32 sfp_hwmon_power_config[] = {
1234 /* Transmit power */
1235 HWMON_P_INPUT |
1236 HWMON_P_MAX | HWMON_P_MIN |
1237 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1238 HWMON_P_CRIT | HWMON_P_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001239 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1240 HWMON_P_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001241 /* Receive power */
1242 HWMON_P_INPUT |
1243 HWMON_P_MAX | HWMON_P_MIN |
1244 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1245 HWMON_P_CRIT | HWMON_P_LCRIT |
Andrew Lunnc1236972019-08-25 01:04:17 +02001246 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1247 HWMON_P_LABEL,
Andrew Lunn13230612018-07-17 21:48:13 +02001248 0,
1249};
1250
1251static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1252 .type = hwmon_power,
1253 .config = sfp_hwmon_power_config,
1254};
1255
1256static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1257 &sfp_hwmon_chip,
1258 &sfp_hwmon_vcc_channel_info,
1259 &sfp_hwmon_temp_channel_info,
1260 &sfp_hwmon_bias_channel_info,
1261 &sfp_hwmon_power_channel_info,
1262 NULL,
1263};
1264
1265static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1266 .ops = &sfp_hwmon_ops,
1267 .info = sfp_hwmon_info,
1268};
1269
Russell King139d3a22019-11-10 14:07:35 +00001270static void sfp_hwmon_probe(struct work_struct *work)
Andrew Lunn13230612018-07-17 21:48:13 +02001271{
Russell King139d3a22019-11-10 14:07:35 +00001272 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
Andrew Lunn13230612018-07-17 21:48:13 +02001273 int err, i;
1274
Russell King139d3a22019-11-10 14:07:35 +00001275 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1276 if (err < 0) {
1277 if (sfp->hwmon_tries--) {
1278 mod_delayed_work(system_wq, &sfp->hwmon_probe,
1279 T_PROBE_RETRY_SLOW);
1280 } else {
1281 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1282 }
1283 return;
1284 }
1285
1286 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1287 if (!sfp->hwmon_name) {
1288 dev_err(sfp->dev, "out of memory for hwmon name\n");
1289 return;
1290 }
1291
1292 for (i = 0; sfp->hwmon_name[i]; i++)
1293 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1294 sfp->hwmon_name[i] = '_';
1295
1296 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1297 sfp->hwmon_name, sfp,
1298 &sfp_hwmon_chip_info,
1299 NULL);
1300 if (IS_ERR(sfp->hwmon_dev))
1301 dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1302 PTR_ERR(sfp->hwmon_dev));
1303}
1304
1305static int sfp_hwmon_insert(struct sfp *sfp)
1306{
Andrew Lunn13230612018-07-17 21:48:13 +02001307 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1308 return 0;
1309
1310 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1311 return 0;
1312
1313 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1314 /* This driver in general does not support address
1315 * change.
1316 */
1317 return 0;
1318
Russell King139d3a22019-11-10 14:07:35 +00001319 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
1320 sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
Andrew Lunn13230612018-07-17 21:48:13 +02001321
Russell King139d3a22019-11-10 14:07:35 +00001322 return 0;
Andrew Lunn13230612018-07-17 21:48:13 +02001323}
1324
1325static void sfp_hwmon_remove(struct sfp *sfp)
1326{
Russell King139d3a22019-11-10 14:07:35 +00001327 cancel_delayed_work_sync(&sfp->hwmon_probe);
Andrew Lunn3e322472018-09-25 01:50:00 +02001328 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1329 hwmon_device_unregister(sfp->hwmon_dev);
1330 sfp->hwmon_dev = NULL;
1331 kfree(sfp->hwmon_name);
1332 }
Andrew Lunn13230612018-07-17 21:48:13 +02001333}
Russell King139d3a22019-11-10 14:07:35 +00001334
1335static int sfp_hwmon_init(struct sfp *sfp)
1336{
1337 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1338
1339 return 0;
1340}
1341
1342static void sfp_hwmon_exit(struct sfp *sfp)
1343{
1344 cancel_delayed_work_sync(&sfp->hwmon_probe);
1345}
Andrew Lunn13230612018-07-17 21:48:13 +02001346#else
1347static int sfp_hwmon_insert(struct sfp *sfp)
1348{
1349 return 0;
1350}
1351
1352static void sfp_hwmon_remove(struct sfp *sfp)
1353{
1354}
Russell King139d3a22019-11-10 14:07:35 +00001355
1356static int sfp_hwmon_init(struct sfp *sfp)
1357{
1358 return 0;
1359}
1360
1361static void sfp_hwmon_exit(struct sfp *sfp)
1362{
1363}
Andrew Lunn13230612018-07-17 21:48:13 +02001364#endif
1365
Russell King73970052017-07-25 15:03:39 +01001366/* Helpers */
1367static void sfp_module_tx_disable(struct sfp *sfp)
1368{
1369 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1370 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1371 sfp->state |= SFP_F_TX_DISABLE;
1372 sfp_set_state(sfp, sfp->state);
1373}
1374
1375static void sfp_module_tx_enable(struct sfp *sfp)
1376{
1377 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1378 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1379 sfp->state &= ~SFP_F_TX_DISABLE;
1380 sfp_set_state(sfp, sfp->state);
1381}
1382
1383static void sfp_module_tx_fault_reset(struct sfp *sfp)
1384{
1385 unsigned int state = sfp->state;
1386
1387 if (state & SFP_F_TX_DISABLE)
1388 return;
1389
1390 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1391
1392 udelay(T_RESET_US);
1393
1394 sfp_set_state(sfp, state);
1395}
1396
1397/* SFP state machine */
1398static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1399{
1400 if (timeout)
1401 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1402 timeout);
1403 else
1404 cancel_delayed_work(&sfp->timeout);
1405}
1406
1407static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1408 unsigned int timeout)
1409{
1410 sfp->sm_state = state;
1411 sfp_sm_set_timer(sfp, timeout);
1412}
1413
Russell King0936ebc2019-11-10 14:06:23 +00001414static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001415 unsigned int timeout)
Russell King73970052017-07-25 15:03:39 +01001416{
1417 sfp->sm_mod_state = state;
1418 sfp_sm_set_timer(sfp, timeout);
1419}
1420
1421static void sfp_sm_phy_detach(struct sfp *sfp)
1422{
Russell King73970052017-07-25 15:03:39 +01001423 sfp_remove_phy(sfp->sfp_bus);
1424 phy_device_remove(sfp->mod_phy);
1425 phy_device_free(sfp->mod_phy);
1426 sfp->mod_phy = NULL;
1427}
1428
Russell King256e43c2019-12-09 14:16:05 +00001429static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45)
Russell King73970052017-07-25 15:03:39 +01001430{
1431 struct phy_device *phy;
1432 int err;
1433
Russell King9a484622019-12-11 10:57:01 +00001434 phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45);
Russell King1cb89a12019-12-09 14:16:11 +00001435 if (phy == ERR_PTR(-ENODEV))
1436 return PTR_ERR(phy);
Russell King20b56ed2017-12-15 16:09:36 +00001437 if (IS_ERR(phy)) {
1438 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
Russell King256e43c2019-12-09 14:16:05 +00001439 return PTR_ERR(phy);
Russell King73970052017-07-25 15:03:39 +01001440 }
1441
Russell King9a484622019-12-11 10:57:01 +00001442 err = phy_device_register(phy);
1443 if (err) {
1444 phy_device_free(phy);
1445 dev_err(sfp->dev, "phy_device_register failed: %d\n", err);
Russell King256e43c2019-12-09 14:16:05 +00001446 return err;
Russell King9a484622019-12-11 10:57:01 +00001447 }
1448
Russell King73970052017-07-25 15:03:39 +01001449 err = sfp_add_phy(sfp->sfp_bus, phy);
1450 if (err) {
1451 phy_device_remove(phy);
1452 phy_device_free(phy);
1453 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
Russell King256e43c2019-12-09 14:16:05 +00001454 return err;
Russell King73970052017-07-25 15:03:39 +01001455 }
1456
1457 sfp->mod_phy = phy;
Russell King256e43c2019-12-09 14:16:05 +00001458
1459 return 0;
Russell King73970052017-07-25 15:03:39 +01001460}
1461
1462static void sfp_sm_link_up(struct sfp *sfp)
1463{
1464 sfp_link_up(sfp->sfp_bus);
1465 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1466}
1467
1468static void sfp_sm_link_down(struct sfp *sfp)
1469{
1470 sfp_link_down(sfp->sfp_bus);
1471}
1472
1473static void sfp_sm_link_check_los(struct sfp *sfp)
1474{
1475 unsigned int los = sfp->state & SFP_F_LOS;
1476
Russell King710dfbb2017-11-30 13:59:16 +00001477 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1478 * are set, we assume that no LOS signal is available.
Russell King73970052017-07-25 15:03:39 +01001479 */
Russell Kingacf1c022017-11-30 13:59:11 +00001480 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
Russell King73970052017-07-25 15:03:39 +01001481 los ^= SFP_F_LOS;
Russell King710dfbb2017-11-30 13:59:16 +00001482 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1483 los = 0;
Russell King73970052017-07-25 15:03:39 +01001484
1485 if (los)
1486 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1487 else
1488 sfp_sm_link_up(sfp);
1489}
1490
Russell King710dfbb2017-11-30 13:59:16 +00001491static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1492{
1493 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1494 event == SFP_E_LOS_LOW) ||
1495 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1496 event == SFP_E_LOS_HIGH);
1497}
1498
1499static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1500{
1501 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1502 event == SFP_E_LOS_HIGH) ||
1503 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1504 event == SFP_E_LOS_LOW);
1505}
1506
Russell King63ec1c72019-11-10 14:07:04 +00001507static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
Russell King73970052017-07-25 15:03:39 +01001508{
Russell King281e4ea2019-12-09 14:16:00 +00001509 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) {
Florian Fainelli516b29e2017-10-30 21:42:57 -07001510 dev_err(sfp->dev,
1511 "module persistently indicates fault, disabling\n");
Russell King73970052017-07-25 15:03:39 +01001512 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1513 } else {
1514 if (warn)
1515 dev_err(sfp->dev, "module transmit fault indicated\n");
1516
Russell King63ec1c72019-11-10 14:07:04 +00001517 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
Russell King73970052017-07-25 15:03:39 +01001518 }
1519}
1520
Russell King9a484622019-12-11 10:57:01 +00001521/* Probe a SFP for a PHY device if the module supports copper - the PHY
1522 * normally sits at I2C bus address 0x56, and may either be a clause 22
1523 * or clause 45 PHY.
1524 *
1525 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with
1526 * negotiation enabled, but some may be in 1000base-X - which is for the
1527 * PHY driver to determine.
1528 *
1529 * Clause 45 copper SFP+ modules (10G) appear to switch their interface
1530 * mode according to the negotiated line speed.
1531 */
Russell King256e43c2019-12-09 14:16:05 +00001532static int sfp_sm_probe_for_phy(struct sfp *sfp)
Russell King181f29d2019-11-10 14:06:54 +00001533{
Russell King256e43c2019-12-09 14:16:05 +00001534 int err = 0;
1535
Russell King9a484622019-12-11 10:57:01 +00001536 switch (sfp->id.base.extended_cc) {
1537 case SFF8024_ECC_10GBASE_T_SFI:
1538 case SFF8024_ECC_10GBASE_T_SR:
1539 case SFF8024_ECC_5GBASE_T:
1540 case SFF8024_ECC_2_5GBASE_T:
Russell King256e43c2019-12-09 14:16:05 +00001541 err = sfp_sm_probe_phy(sfp, true);
Russell King9a484622019-12-11 10:57:01 +00001542 break;
1543
1544 default:
1545 if (sfp->id.base.e1000_base_t)
Russell King256e43c2019-12-09 14:16:05 +00001546 err = sfp_sm_probe_phy(sfp, false);
Russell King9a484622019-12-11 10:57:01 +00001547 break;
1548 }
Russell King256e43c2019-12-09 14:16:05 +00001549 return err;
Russell King73970052017-07-25 15:03:39 +01001550}
1551
Russell Kinged32abb2019-11-10 14:06:39 +00001552static int sfp_module_parse_power(struct sfp *sfp)
Jon Nettleton3bb35262018-02-27 15:53:12 +00001553{
Russell Kinged32abb2019-11-10 14:06:39 +00001554 u32 power_mW = 1000;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001555
Jon Nettleton3bb35262018-02-27 15:53:12 +00001556 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
Russell Kinged32abb2019-11-10 14:06:39 +00001557 power_mW = 1500;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001558 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
Russell Kinged32abb2019-11-10 14:06:39 +00001559 power_mW = 2000;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001560
Russell King7cfa9c92019-11-10 14:06:44 +00001561 if (power_mW > sfp->max_power_mW) {
1562 /* Module power specification exceeds the allowed maximum. */
1563 if (sfp->id.ext.sff8472_compliance ==
1564 SFP_SFF8472_COMPLIANCE_NONE &&
1565 !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1566 /* The module appears not to implement bus address
1567 * 0xa2, so assume that the module powers up in the
1568 * indicated mode.
1569 */
Jon Nettleton3bb35262018-02-27 15:53:12 +00001570 dev_err(sfp->dev,
1571 "Host does not support %u.%uW modules\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001572 power_mW / 1000, (power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001573 return -EINVAL;
Russell King7cfa9c92019-11-10 14:06:44 +00001574 } else {
1575 dev_warn(sfp->dev,
1576 "Host does not support %u.%uW modules, module left in power mode 1\n",
1577 power_mW / 1000, (power_mW / 100) % 10);
1578 return 0;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001579 }
Jon Nettleton3bb35262018-02-27 15:53:12 +00001580 }
1581
Russell King7cfa9c92019-11-10 14:06:44 +00001582 /* If the module requires a higher power mode, but also requires
1583 * an address change sequence, warn the user that the module may
1584 * not be functional.
1585 */
1586 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
Jon Nettleton3bb35262018-02-27 15:53:12 +00001587 dev_warn(sfp->dev,
Colin Ian King07f23d92019-11-13 09:55:48 +00001588 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
Russell Kinged32abb2019-11-10 14:06:39 +00001589 power_mW / 1000, (power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001590 return 0;
1591 }
1592
Russell Kinged32abb2019-11-10 14:06:39 +00001593 sfp->module_power_mW = power_mW;
1594
1595 return 0;
1596}
1597
Russell Kingb036a552019-11-10 14:07:20 +00001598static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
Russell Kinged32abb2019-11-10 14:06:39 +00001599{
1600 u8 val;
1601 int err;
1602
Jon Nettleton3bb35262018-02-27 15:53:12 +00001603 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1604 if (err != sizeof(val)) {
1605 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
Russell Kingb036a552019-11-10 14:07:20 +00001606 return -EAGAIN;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001607 }
1608
Russell King9a484622019-12-11 10:57:01 +00001609 /* DM7052 reports as a high power module, responds to reads (with
1610 * all bytes 0xff) at 0x51 but does not accept writes. In any case,
1611 * if the bit is already set, we're already in high power mode.
1612 */
1613 if (!!(val & BIT(0)) == enable)
1614 return 0;
1615
Russell Kingb036a552019-11-10 14:07:20 +00001616 if (enable)
1617 val |= BIT(0);
1618 else
1619 val &= ~BIT(0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001620
1621 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1622 if (err != sizeof(val)) {
1623 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
Russell Kingb036a552019-11-10 14:07:20 +00001624 return -EAGAIN;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001625 }
1626
Russell Kingb036a552019-11-10 14:07:20 +00001627 if (enable)
1628 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1629 sfp->module_power_mW / 1000,
1630 (sfp->module_power_mW / 100) % 10);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001631
Russell Kingb036a552019-11-10 14:07:20 +00001632 return 0;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001633}
1634
Russell Kinge117be72019-11-10 14:07:30 +00001635static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
Russell King73970052017-07-25 15:03:39 +01001636{
1637 /* SFP module inserted - read I2C data */
1638 struct sfp_eeprom_id id;
Russell King981f1f82018-03-28 11:18:25 +01001639 bool cotsworks;
Russell King73970052017-07-25 15:03:39 +01001640 u8 check;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001641 int ret;
Russell King73970052017-07-25 15:03:39 +01001642
Jon Nettleton3bb35262018-02-27 15:53:12 +00001643 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1644 if (ret < 0) {
Russell Kinge117be72019-11-10 14:07:30 +00001645 if (report)
1646 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001647 return -EAGAIN;
1648 }
1649
Jon Nettleton3bb35262018-02-27 15:53:12 +00001650 if (ret != sizeof(id)) {
1651 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001652 return -EAGAIN;
1653 }
1654
Russell King981f1f82018-03-28 11:18:25 +01001655 /* Cotsworks do not seem to update the checksums when they
1656 * do the final programming with the final module part number,
1657 * serial number and date code.
1658 */
1659 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1660
Russell King73970052017-07-25 15:03:39 +01001661 /* Validate the checksum over the base structure */
1662 check = sfp_check(&id.base, sizeof(id.base) - 1);
1663 if (check != id.base.cc_base) {
Russell King981f1f82018-03-28 11:18:25 +01001664 if (cotsworks) {
1665 dev_warn(sfp->dev,
1666 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1667 check, id.base.cc_base);
1668 } else {
1669 dev_err(sfp->dev,
1670 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1671 check, id.base.cc_base);
1672 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1673 16, 1, &id, sizeof(id), true);
1674 return -EINVAL;
1675 }
Russell King73970052017-07-25 15:03:39 +01001676 }
1677
1678 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1679 if (check != id.ext.cc_ext) {
Russell King981f1f82018-03-28 11:18:25 +01001680 if (cotsworks) {
1681 dev_warn(sfp->dev,
1682 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1683 check, id.ext.cc_ext);
1684 } else {
1685 dev_err(sfp->dev,
1686 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1687 check, id.ext.cc_ext);
1688 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1689 16, 1, &id, sizeof(id), true);
1690 memset(&id.ext, 0, sizeof(id.ext));
1691 }
Russell King73970052017-07-25 15:03:39 +01001692 }
1693
1694 sfp->id = id;
1695
Russell Kinga2f247e2017-12-29 12:15:12 +00001696 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1697 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1698 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1699 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1700 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1701 (int)sizeof(id.ext.datecode), id.ext.datecode);
Russell King73970052017-07-25 15:03:39 +01001702
Russell King259c8612017-12-14 10:27:47 +00001703 /* Check whether we support this module */
Russell Kingf3c9a662019-11-20 12:29:59 +00001704 if (!sfp->type->module_supported(&id)) {
Russell King259c8612017-12-14 10:27:47 +00001705 dev_err(sfp->dev,
1706 "module is not supported - phys id 0x%02x 0x%02x\n",
Russell King73970052017-07-25 15:03:39 +01001707 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1708 return -EINVAL;
1709 }
1710
Russell Kingec7681b2017-11-30 13:59:21 +00001711 /* If the module requires address swap mode, warn about it */
1712 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1713 dev_warn(sfp->dev,
1714 "module address swap to access page 0xA2 is not supported.\n");
1715
Russell Kinged32abb2019-11-10 14:06:39 +00001716 /* Parse the module power requirement */
1717 ret = sfp_module_parse_power(sfp);
1718 if (ret < 0)
1719 return ret;
1720
Russell King26c97a22019-12-09 13:40:23 +00001721 if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) &&
1722 !memcmp(id.base.vendor_pn, "3FE46541AA ", 16))
1723 sfp->module_t_start_up = T_START_UP_BAD_GPON;
1724 else
1725 sfp->module_t_start_up = T_START_UP;
1726
Russell Kingb036a552019-11-10 14:07:20 +00001727 return 0;
Russell King73970052017-07-25 15:03:39 +01001728}
1729
1730static void sfp_sm_mod_remove(struct sfp *sfp)
1731{
Russell King73f5e842019-11-10 14:07:25 +00001732 if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
1733 sfp_module_remove(sfp->sfp_bus);
Russell King73970052017-07-25 15:03:39 +01001734
Andrew Lunn13230612018-07-17 21:48:13 +02001735 sfp_hwmon_remove(sfp);
1736
Russell King73970052017-07-25 15:03:39 +01001737 memset(&sfp->id, 0, sizeof(sfp->id));
Russell Kinged32abb2019-11-10 14:06:39 +00001738 sfp->module_power_mW = 0;
Russell King73970052017-07-25 15:03:39 +01001739
1740 dev_info(sfp->dev, "module removed\n");
1741}
1742
Russell King6b0da5c2019-11-10 14:07:14 +00001743/* This state machine tracks the upstream's state */
Russell Kinge85d81a2019-11-10 14:06:13 +00001744static void sfp_sm_device(struct sfp *sfp, unsigned int event)
Russell King73970052017-07-25 15:03:39 +01001745{
Russell Kinge85d81a2019-11-10 14:06:13 +00001746 switch (sfp->sm_dev_state) {
1747 default:
Russell King6b0da5c2019-11-10 14:07:14 +00001748 if (event == SFP_E_DEV_ATTACH)
1749 sfp->sm_dev_state = SFP_DEV_DOWN;
1750 break;
1751
1752 case SFP_DEV_DOWN:
1753 if (event == SFP_E_DEV_DETACH)
1754 sfp->sm_dev_state = SFP_DEV_DETACHED;
1755 else if (event == SFP_E_DEV_UP)
Russell Kinge85d81a2019-11-10 14:06:13 +00001756 sfp->sm_dev_state = SFP_DEV_UP;
1757 break;
Russell King73970052017-07-25 15:03:39 +01001758
Russell Kinge85d81a2019-11-10 14:06:13 +00001759 case SFP_DEV_UP:
Russell King6b0da5c2019-11-10 14:07:14 +00001760 if (event == SFP_E_DEV_DETACH)
1761 sfp->sm_dev_state = SFP_DEV_DETACHED;
1762 else if (event == SFP_E_DEV_DOWN)
Russell Kinge85d81a2019-11-10 14:06:13 +00001763 sfp->sm_dev_state = SFP_DEV_DOWN;
Russell Kinge85d81a2019-11-10 14:06:13 +00001764 break;
1765 }
1766}
Russell King73970052017-07-25 15:03:39 +01001767
Russell Kingd9009542019-11-10 14:06:33 +00001768/* This state machine tracks the insert/remove state of the module, probes
1769 * the on-board EEPROM, and sets up the power level.
Russell Kinge85d81a2019-11-10 14:06:13 +00001770 */
1771static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1772{
Russell Kingb036a552019-11-10 14:07:20 +00001773 int err;
1774
1775 /* Handle remove event globally, it resets this state machine */
1776 if (event == SFP_E_REMOVE) {
Russell Kingd2e816c2019-11-10 14:06:28 +00001777 if (sfp->sm_mod_state > SFP_MOD_PROBE)
1778 sfp_sm_mod_remove(sfp);
Russell Kingb036a552019-11-10 14:07:20 +00001779 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
1780 return;
1781 }
1782
1783 /* Handle device detach globally */
Russell King73f5e842019-11-10 14:07:25 +00001784 if (sfp->sm_dev_state < SFP_DEV_DOWN &&
1785 sfp->sm_mod_state > SFP_MOD_WAITDEV) {
Russell Kingb036a552019-11-10 14:07:20 +00001786 if (sfp->module_power_mW > 1000 &&
1787 sfp->sm_mod_state > SFP_MOD_HPOWER)
1788 sfp_sm_mod_hpower(sfp, false);
Russell King73f5e842019-11-10 14:07:25 +00001789 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
Russell Kingd2e816c2019-11-10 14:06:28 +00001790 return;
1791 }
1792
Russell King73970052017-07-25 15:03:39 +01001793 switch (sfp->sm_mod_state) {
1794 default:
Russell Kinge117be72019-11-10 14:07:30 +00001795 if (event == SFP_E_INSERT) {
Russell Kingd9009542019-11-10 14:06:33 +00001796 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
Russell Kinge117be72019-11-10 14:07:30 +00001797 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
1798 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
1799 }
Russell King73970052017-07-25 15:03:39 +01001800 break;
1801
1802 case SFP_MOD_PROBE:
Russell King73f5e842019-11-10 14:07:25 +00001803 /* Wait for T_PROBE_INIT to time out */
Russell Kingb036a552019-11-10 14:07:20 +00001804 if (event != SFP_E_TIMEOUT)
1805 break;
Russell King73970052017-07-25 15:03:39 +01001806
Russell Kinge117be72019-11-10 14:07:30 +00001807 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
Russell Kingb036a552019-11-10 14:07:20 +00001808 if (err == -EAGAIN) {
Russell Kinge117be72019-11-10 14:07:30 +00001809 if (sfp->sm_mod_tries_init &&
1810 --sfp->sm_mod_tries_init) {
1811 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1812 break;
1813 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
1814 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
1815 dev_warn(sfp->dev,
1816 "please wait, module slow to respond\n");
1817 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
1818 break;
1819 }
Jon Nettleton3bb35262018-02-27 15:53:12 +00001820 }
Russell Kingb036a552019-11-10 14:07:20 +00001821 if (err < 0) {
1822 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1823 break;
1824 }
1825
Russell King38ecd702019-12-03 23:51:28 +00001826 err = sfp_hwmon_insert(sfp);
1827 if (err)
1828 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1829
Russell King73f5e842019-11-10 14:07:25 +00001830 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1831 /* fall through */
1832 case SFP_MOD_WAITDEV:
1833 /* Ensure that the device is attached before proceeding */
1834 if (sfp->sm_dev_state < SFP_DEV_DOWN)
1835 break;
1836
1837 /* Report the module insertion to the upstream device */
1838 err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1839 if (err < 0) {
1840 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1841 break;
1842 }
1843
Russell Kingb036a552019-11-10 14:07:20 +00001844 /* If this is a power level 1 module, we are done */
1845 if (sfp->module_power_mW <= 1000)
1846 goto insert;
1847
1848 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
1849 /* fall through */
1850 case SFP_MOD_HPOWER:
1851 /* Enable high power mode */
1852 err = sfp_sm_mod_hpower(sfp, true);
Russell King73f5e842019-11-10 14:07:25 +00001853 if (err < 0) {
1854 if (err != -EAGAIN) {
1855 sfp_module_remove(sfp->sfp_bus);
1856 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1857 } else {
Russell Kinge117be72019-11-10 14:07:30 +00001858 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
Russell King73f5e842019-11-10 14:07:25 +00001859 }
1860 break;
1861 }
1862
1863 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
Russell Kingb036a552019-11-10 14:07:20 +00001864 break;
1865
1866 case SFP_MOD_WAITPWR:
1867 /* Wait for T_HPOWER_LEVEL to time out */
1868 if (event != SFP_E_TIMEOUT)
1869 break;
1870
1871 insert:
1872 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
1873 break;
1874
Russell King73970052017-07-25 15:03:39 +01001875 case SFP_MOD_PRESENT:
1876 case SFP_MOD_ERROR:
Russell King73970052017-07-25 15:03:39 +01001877 break;
1878 }
Russell Kinge85d81a2019-11-10 14:06:13 +00001879}
Russell King73970052017-07-25 15:03:39 +01001880
Russell Kinge85d81a2019-11-10 14:06:13 +00001881static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1882{
Russell Kingeefa6f12019-11-10 14:06:59 +00001883 unsigned long timeout;
Russell King1cb89a12019-12-09 14:16:11 +00001884 int ret;
Russell Kingeefa6f12019-11-10 14:06:59 +00001885
Russell King73970052017-07-25 15:03:39 +01001886 /* Some events are global */
1887 if (sfp->sm_state != SFP_S_DOWN &&
1888 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1889 sfp->sm_dev_state != SFP_DEV_UP)) {
1890 if (sfp->sm_state == SFP_S_LINK_UP &&
1891 sfp->sm_dev_state == SFP_DEV_UP)
1892 sfp_sm_link_down(sfp);
Russell King74c551c2019-12-11 10:56:09 +00001893 if (sfp->sm_state > SFP_S_INIT)
1894 sfp_module_stop(sfp->sfp_bus);
Russell King73970052017-07-25 15:03:39 +01001895 if (sfp->mod_phy)
1896 sfp_sm_phy_detach(sfp);
Russell King1539e0d2019-11-10 14:06:18 +00001897 sfp_module_tx_disable(sfp);
Russell Kingf3c9a662019-11-20 12:29:59 +00001898 sfp_soft_stop_poll(sfp);
Russell King73970052017-07-25 15:03:39 +01001899 sfp_sm_next(sfp, SFP_S_DOWN, 0);
Russell King73970052017-07-25 15:03:39 +01001900 return;
1901 }
1902
1903 /* The main state machine */
1904 switch (sfp->sm_state) {
1905 case SFP_S_DOWN:
Russell Kingeefa6f12019-11-10 14:06:59 +00001906 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1907 sfp->sm_dev_state != SFP_DEV_UP)
1908 break;
Russell King181f29d2019-11-10 14:06:54 +00001909
Russell Kingf3c9a662019-11-20 12:29:59 +00001910 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE))
1911 sfp_soft_start_poll(sfp);
1912
1913 sfp_module_tx_enable(sfp);
Russell Kingeefa6f12019-11-10 14:06:59 +00001914
1915 /* Initialise the fault clearance retries */
Russell King281e4ea2019-12-09 14:16:00 +00001916 sfp->sm_fault_retries = N_FAULT_INIT;
Russell Kingeefa6f12019-11-10 14:06:59 +00001917
1918 /* We need to check the TX_FAULT state, which is not defined
1919 * while TX_DISABLE is asserted. The earliest we want to do
1920 * anything (such as probe for a PHY) is 50ms.
1921 */
1922 sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1923 break;
1924
1925 case SFP_S_WAIT:
1926 if (event != SFP_E_TIMEOUT)
1927 break;
1928
Russell Kingeefa6f12019-11-10 14:06:59 +00001929 if (sfp->state & SFP_F_TX_FAULT) {
Russell King26c97a22019-12-09 13:40:23 +00001930 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
1931 * from the TX_DISABLE deassertion for the module to
1932 * initialise, which is indicated by TX_FAULT
1933 * deasserting.
Russell King181f29d2019-11-10 14:06:54 +00001934 */
Russell King26c97a22019-12-09 13:40:23 +00001935 timeout = sfp->module_t_start_up;
Russell Kingeefa6f12019-11-10 14:06:59 +00001936 if (timeout > T_WAIT)
1937 timeout -= T_WAIT;
1938 else
1939 timeout = 1;
1940
1941 sfp_sm_next(sfp, SFP_S_INIT, timeout);
1942 } else {
1943 /* TX_FAULT is not asserted, assume the module has
1944 * finished initialising.
1945 */
1946 goto init_done;
Russell King181f29d2019-11-10 14:06:54 +00001947 }
Russell King73970052017-07-25 15:03:39 +01001948 break;
1949
1950 case SFP_S_INIT:
Russell Kingd23751a2019-11-10 14:07:09 +00001951 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
Russell King26c97a22019-12-09 13:40:23 +00001952 /* TX_FAULT is still asserted after t_init or
1953 * or t_start_up, so assume there is a fault.
Russell Kingd23751a2019-11-10 14:07:09 +00001954 */
1955 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
Russell King281e4ea2019-12-09 14:16:00 +00001956 sfp->sm_fault_retries == N_FAULT_INIT);
Russell Kingd23751a2019-11-10 14:07:09 +00001957 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
Russell King1cb89a12019-12-09 14:16:11 +00001958 init_done:
1959 sfp->sm_phy_retries = R_PHY_RETRY;
1960 goto phy_probe;
Russell Kingd23751a2019-11-10 14:07:09 +00001961 }
1962 break;
1963
Russell King1cb89a12019-12-09 14:16:11 +00001964 case SFP_S_INIT_PHY:
1965 if (event != SFP_E_TIMEOUT)
1966 break;
1967 phy_probe:
1968 /* TX_FAULT deasserted or we timed out with TX_FAULT
1969 * clear. Probe for the PHY and check the LOS state.
1970 */
1971 ret = sfp_sm_probe_for_phy(sfp);
1972 if (ret == -ENODEV) {
1973 if (--sfp->sm_phy_retries) {
1974 sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY);
1975 break;
1976 } else {
1977 dev_info(sfp->dev, "no PHY detected\n");
1978 }
1979 } else if (ret) {
1980 sfp_sm_next(sfp, SFP_S_FAIL, 0);
1981 break;
1982 }
1983 if (sfp_module_start(sfp->sfp_bus)) {
1984 sfp_sm_next(sfp, SFP_S_FAIL, 0);
1985 break;
1986 }
1987 sfp_sm_link_check_los(sfp);
1988
1989 /* Reset the fault retry count */
1990 sfp->sm_fault_retries = N_FAULT;
1991 break;
1992
Russell Kingd23751a2019-11-10 14:07:09 +00001993 case SFP_S_INIT_TX_FAULT:
1994 if (event == SFP_E_TIMEOUT) {
1995 sfp_module_tx_fault_reset(sfp);
Russell King26c97a22019-12-09 13:40:23 +00001996 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
Russell Kingd23751a2019-11-10 14:07:09 +00001997 }
Russell King73970052017-07-25 15:03:39 +01001998 break;
1999
2000 case SFP_S_WAIT_LOS:
2001 if (event == SFP_E_TX_FAULT)
Russell King63ec1c72019-11-10 14:07:04 +00002002 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
Russell King710dfbb2017-11-30 13:59:16 +00002003 else if (sfp_los_event_inactive(sfp, event))
Russell King73970052017-07-25 15:03:39 +01002004 sfp_sm_link_up(sfp);
2005 break;
2006
2007 case SFP_S_LINK_UP:
2008 if (event == SFP_E_TX_FAULT) {
2009 sfp_sm_link_down(sfp);
Russell King63ec1c72019-11-10 14:07:04 +00002010 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
Russell King710dfbb2017-11-30 13:59:16 +00002011 } else if (sfp_los_event_active(sfp, event)) {
Russell King73970052017-07-25 15:03:39 +01002012 sfp_sm_link_down(sfp);
2013 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
2014 }
2015 break;
2016
2017 case SFP_S_TX_FAULT:
2018 if (event == SFP_E_TIMEOUT) {
2019 sfp_module_tx_fault_reset(sfp);
Russell King26c97a22019-12-09 13:40:23 +00002020 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
Russell King73970052017-07-25 15:03:39 +01002021 }
2022 break;
2023
2024 case SFP_S_REINIT:
2025 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
Russell King63ec1c72019-11-10 14:07:04 +00002026 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
Russell King73970052017-07-25 15:03:39 +01002027 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2028 dev_info(sfp->dev, "module transmit fault recovered\n");
2029 sfp_sm_link_check_los(sfp);
2030 }
2031 break;
2032
2033 case SFP_S_TX_DISABLE:
2034 break;
2035 }
Russell Kinge85d81a2019-11-10 14:06:13 +00002036}
2037
2038static void sfp_sm_event(struct sfp *sfp, unsigned int event)
2039{
2040 mutex_lock(&sfp->sm_mutex);
2041
2042 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
2043 mod_state_to_str(sfp->sm_mod_state),
2044 dev_state_to_str(sfp->sm_dev_state),
2045 sm_state_to_str(sfp->sm_state),
2046 event_to_str(event));
2047
Russell Kinge85d81a2019-11-10 14:06:13 +00002048 sfp_sm_device(sfp, event);
Russell King6b0da5c2019-11-10 14:07:14 +00002049 sfp_sm_module(sfp, event);
Russell Kinge85d81a2019-11-10 14:06:13 +00002050 sfp_sm_main(sfp, event);
Russell King73970052017-07-25 15:03:39 +01002051
Andrew Lunn4005a7c2018-08-08 20:54:12 +02002052 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
2053 mod_state_to_str(sfp->sm_mod_state),
2054 dev_state_to_str(sfp->sm_dev_state),
2055 sm_state_to_str(sfp->sm_state));
Russell King73970052017-07-25 15:03:39 +01002056
2057 mutex_unlock(&sfp->sm_mutex);
2058}
2059
Russell Kingb5bfc212019-02-06 10:52:30 +00002060static void sfp_attach(struct sfp *sfp)
2061{
Russell King6b0da5c2019-11-10 14:07:14 +00002062 sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
Russell Kingb5bfc212019-02-06 10:52:30 +00002063}
2064
2065static void sfp_detach(struct sfp *sfp)
2066{
Russell King6b0da5c2019-11-10 14:07:14 +00002067 sfp_sm_event(sfp, SFP_E_DEV_DETACH);
Russell Kingb5bfc212019-02-06 10:52:30 +00002068}
2069
Russell King73970052017-07-25 15:03:39 +01002070static void sfp_start(struct sfp *sfp)
2071{
2072 sfp_sm_event(sfp, SFP_E_DEV_UP);
2073}
2074
2075static void sfp_stop(struct sfp *sfp)
2076{
2077 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
2078}
2079
2080static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
2081{
2082 /* locking... and check module is present */
2083
Russell Kingec7681b2017-11-30 13:59:21 +00002084 if (sfp->id.ext.sff8472_compliance &&
2085 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
Russell King73970052017-07-25 15:03:39 +01002086 modinfo->type = ETH_MODULE_SFF_8472;
2087 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2088 } else {
2089 modinfo->type = ETH_MODULE_SFF_8079;
2090 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
2091 }
2092 return 0;
2093}
2094
2095static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
Florian Fainelli516b29e2017-10-30 21:42:57 -07002096 u8 *data)
Russell King73970052017-07-25 15:03:39 +01002097{
2098 unsigned int first, last, len;
2099 int ret;
2100
2101 if (ee->len == 0)
2102 return -EINVAL;
2103
2104 first = ee->offset;
2105 last = ee->offset + ee->len;
2106 if (first < ETH_MODULE_SFF_8079_LEN) {
2107 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
2108 len -= first;
2109
Russell King2794ffc2017-12-15 16:09:41 +00002110 ret = sfp_read(sfp, false, first, data, len);
Russell King73970052017-07-25 15:03:39 +01002111 if (ret < 0)
2112 return ret;
2113
2114 first += len;
2115 data += len;
2116 }
Russell King2794ffc2017-12-15 16:09:41 +00002117 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
Russell King73970052017-07-25 15:03:39 +01002118 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
2119 len -= first;
2120 first -= ETH_MODULE_SFF_8079_LEN;
2121
Russell King2794ffc2017-12-15 16:09:41 +00002122 ret = sfp_read(sfp, true, first, data, len);
Russell King73970052017-07-25 15:03:39 +01002123 if (ret < 0)
2124 return ret;
2125 }
2126 return 0;
2127}
2128
2129static const struct sfp_socket_ops sfp_module_ops = {
Russell Kingb5bfc212019-02-06 10:52:30 +00002130 .attach = sfp_attach,
2131 .detach = sfp_detach,
Russell King73970052017-07-25 15:03:39 +01002132 .start = sfp_start,
2133 .stop = sfp_stop,
2134 .module_info = sfp_module_info,
2135 .module_eeprom = sfp_module_eeprom,
2136};
2137
2138static void sfp_timeout(struct work_struct *work)
2139{
2140 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
2141
2142 rtnl_lock();
2143 sfp_sm_event(sfp, SFP_E_TIMEOUT);
2144 rtnl_unlock();
2145}
2146
2147static void sfp_check_state(struct sfp *sfp)
2148{
2149 unsigned int state, i, changed;
2150
Robert Hancock2158e852019-06-07 10:42:36 -06002151 mutex_lock(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01002152 state = sfp_get_state(sfp);
2153 changed = state ^ sfp->state;
2154 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
2155
2156 for (i = 0; i < GPIO_MAX; i++)
2157 if (changed & BIT(i))
2158 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
2159 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
2160
2161 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
2162 sfp->state = state;
2163
2164 rtnl_lock();
2165 if (changed & SFP_F_PRESENT)
2166 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
2167 SFP_E_INSERT : SFP_E_REMOVE);
2168
2169 if (changed & SFP_F_TX_FAULT)
2170 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
2171 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
2172
2173 if (changed & SFP_F_LOS)
2174 sfp_sm_event(sfp, state & SFP_F_LOS ?
2175 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
2176 rtnl_unlock();
Robert Hancock2158e852019-06-07 10:42:36 -06002177 mutex_unlock(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01002178}
2179
2180static irqreturn_t sfp_irq(int irq, void *data)
2181{
2182 struct sfp *sfp = data;
2183
2184 sfp_check_state(sfp);
2185
2186 return IRQ_HANDLED;
2187}
2188
2189static void sfp_poll(struct work_struct *work)
2190{
2191 struct sfp *sfp = container_of(work, struct sfp, poll.work);
2192
2193 sfp_check_state(sfp);
Russell Kingf3c9a662019-11-20 12:29:59 +00002194
2195 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
2196 sfp->need_poll)
2197 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
Russell King73970052017-07-25 15:03:39 +01002198}
2199
2200static struct sfp *sfp_alloc(struct device *dev)
2201{
2202 struct sfp *sfp;
2203
2204 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
2205 if (!sfp)
2206 return ERR_PTR(-ENOMEM);
2207
2208 sfp->dev = dev;
2209
2210 mutex_init(&sfp->sm_mutex);
Robert Hancock2158e852019-06-07 10:42:36 -06002211 mutex_init(&sfp->st_mutex);
Russell King73970052017-07-25 15:03:39 +01002212 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
2213 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
2214
Russell King139d3a22019-11-10 14:07:35 +00002215 sfp_hwmon_init(sfp);
2216
Russell King73970052017-07-25 15:03:39 +01002217 return sfp;
2218}
2219
2220static void sfp_cleanup(void *data)
2221{
2222 struct sfp *sfp = data;
2223
Russell King139d3a22019-11-10 14:07:35 +00002224 sfp_hwmon_exit(sfp);
2225
Russell King73970052017-07-25 15:03:39 +01002226 cancel_delayed_work_sync(&sfp->poll);
2227 cancel_delayed_work_sync(&sfp->timeout);
2228 if (sfp->i2c_mii) {
2229 mdiobus_unregister(sfp->i2c_mii);
2230 mdiobus_free(sfp->i2c_mii);
2231 }
2232 if (sfp->i2c)
2233 i2c_put_adapter(sfp->i2c);
2234 kfree(sfp);
2235}
2236
2237static int sfp_probe(struct platform_device *pdev)
2238{
Russell King259c8612017-12-14 10:27:47 +00002239 const struct sff_data *sff;
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002240 struct i2c_adapter *i2c;
Chris Healy5411ca72020-07-06 18:27:07 -07002241 char *sfp_irq_name;
Russell King73970052017-07-25 15:03:39 +01002242 struct sfp *sfp;
Robert Hancock257c2552019-06-07 10:42:35 -06002243 int err, i;
Russell King73970052017-07-25 15:03:39 +01002244
2245 sfp = sfp_alloc(&pdev->dev);
2246 if (IS_ERR(sfp))
2247 return PTR_ERR(sfp);
2248
2249 platform_set_drvdata(pdev, sfp);
2250
2251 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
2252 if (err < 0)
2253 return err;
2254
Russell King259c8612017-12-14 10:27:47 +00002255 sff = sfp->type = &sfp_data;
2256
Russell King73970052017-07-25 15:03:39 +01002257 if (pdev->dev.of_node) {
2258 struct device_node *node = pdev->dev.of_node;
Russell King259c8612017-12-14 10:27:47 +00002259 const struct of_device_id *id;
Russell King73970052017-07-25 15:03:39 +01002260 struct device_node *np;
2261
Russell King259c8612017-12-14 10:27:47 +00002262 id = of_match_node(sfp_of_match, node);
2263 if (WARN_ON(!id))
2264 return -EINVAL;
2265
2266 sff = sfp->type = id->data;
2267
Russell King73970052017-07-25 15:03:39 +01002268 np = of_parse_phandle(node, "i2c-bus", 0);
Antoine Tenart66ede1f2018-05-22 12:18:00 +02002269 if (!np) {
2270 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
2271 return -ENODEV;
2272 }
Russell King73970052017-07-25 15:03:39 +01002273
Antoine Tenart66ede1f2018-05-22 12:18:00 +02002274 i2c = of_find_i2c_adapter_by_node(np);
2275 of_node_put(np);
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002276 } else if (has_acpi_companion(&pdev->dev)) {
2277 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2278 struct fwnode_handle *fw = acpi_fwnode_handle(adev);
2279 struct fwnode_reference_args args;
2280 struct acpi_handle *acpi_handle;
2281 int ret;
Russell King73970052017-07-25 15:03:39 +01002282
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002283 ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args);
Dan Carpenter1086ca32019-06-13 09:51:02 +03002284 if (ret || !is_acpi_device_node(args.fwnode)) {
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002285 dev_err(&pdev->dev, "missing 'i2c-bus' property\n");
2286 return -ENODEV;
Russell King73970052017-07-25 15:03:39 +01002287 }
Ruslan Babayev7ce236fa2019-05-28 16:02:33 -07002288
2289 acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode);
2290 i2c = i2c_acpi_find_adapter_by_handle(acpi_handle);
2291 } else {
2292 return -EINVAL;
2293 }
2294
2295 if (!i2c)
2296 return -EPROBE_DEFER;
2297
2298 err = sfp_i2c_configure(sfp, i2c);
2299 if (err < 0) {
2300 i2c_put_adapter(i2c);
2301 return err;
Russell King259c8612017-12-14 10:27:47 +00002302 }
Russell King73970052017-07-25 15:03:39 +01002303
Russell King259c8612017-12-14 10:27:47 +00002304 for (i = 0; i < GPIO_MAX; i++)
2305 if (sff->gpios & BIT(i)) {
Russell King73970052017-07-25 15:03:39 +01002306 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
2307 gpio_of_names[i], gpio_flags[i]);
2308 if (IS_ERR(sfp->gpio[i]))
2309 return PTR_ERR(sfp->gpio[i]);
2310 }
2311
Russell King259c8612017-12-14 10:27:47 +00002312 sfp->get_state = sfp_gpio_get_state;
2313 sfp->set_state = sfp_gpio_set_state;
2314
2315 /* Modules that have no detect signal are always present */
2316 if (!(sfp->gpio[GPIO_MODDEF0]))
2317 sfp->get_state = sff_gpio_get_state;
Russell King73970052017-07-25 15:03:39 +01002318
Jon Nettleton3bb35262018-02-27 15:53:12 +00002319 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
2320 &sfp->max_power_mW);
2321 if (!sfp->max_power_mW)
2322 sfp->max_power_mW = 1000;
2323
2324 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
2325 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
2326
Russell King73970052017-07-25 15:03:39 +01002327 /* Get the initial state, and always signal TX disable,
2328 * since the network interface will not be up.
2329 */
2330 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
2331
2332 if (sfp->gpio[GPIO_RATE_SELECT] &&
2333 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
2334 sfp->state |= SFP_F_RATE_SELECT;
2335 sfp_set_state(sfp, sfp->state);
2336 sfp_module_tx_disable(sfp);
Russell King73f5e842019-11-10 14:07:25 +00002337 if (sfp->state & SFP_F_PRESENT) {
2338 rtnl_lock();
2339 sfp_sm_event(sfp, SFP_E_INSERT);
2340 rtnl_unlock();
2341 }
Russell King73970052017-07-25 15:03:39 +01002342
2343 for (i = 0; i < GPIO_MAX; i++) {
2344 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
2345 continue;
2346
Robert Hancock257c2552019-06-07 10:42:35 -06002347 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
2348 if (!sfp->gpio_irq[i]) {
Russell Kingf3c9a662019-11-20 12:29:59 +00002349 sfp->need_poll = true;
Russell King73970052017-07-25 15:03:39 +01002350 continue;
2351 }
2352
Chris Healy5411ca72020-07-06 18:27:07 -07002353 sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL,
2354 "%s-%s", dev_name(sfp->dev),
2355 gpio_of_names[i]);
2356
Robert Hancock257c2552019-06-07 10:42:35 -06002357 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
2358 NULL, sfp_irq,
Russell King73970052017-07-25 15:03:39 +01002359 IRQF_ONESHOT |
2360 IRQF_TRIGGER_RISING |
2361 IRQF_TRIGGER_FALLING,
Chris Healy5411ca72020-07-06 18:27:07 -07002362 sfp_irq_name, sfp);
Robert Hancock257c2552019-06-07 10:42:35 -06002363 if (err) {
2364 sfp->gpio_irq[i] = 0;
Russell Kingf3c9a662019-11-20 12:29:59 +00002365 sfp->need_poll = true;
Robert Hancock257c2552019-06-07 10:42:35 -06002366 }
Russell King73970052017-07-25 15:03:39 +01002367 }
2368
Russell Kingf3c9a662019-11-20 12:29:59 +00002369 if (sfp->need_poll)
Russell King73970052017-07-25 15:03:39 +01002370 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2371
Antoine Tenarta1f5d1f2018-05-22 12:17:59 +02002372 /* We could have an issue in cases no Tx disable pin is available or
2373 * wired as modules using a laser as their light source will continue to
2374 * be active when the fiber is removed. This could be a safety issue and
2375 * we should at least warn the user about that.
2376 */
2377 if (!sfp->gpio[GPIO_TX_DISABLE])
2378 dev_warn(sfp->dev,
2379 "No tx_disable pin: SFP modules will always be emitting.\n");
2380
Russell Kingb5bfc212019-02-06 10:52:30 +00002381 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
2382 if (!sfp->sfp_bus)
2383 return -ENOMEM;
2384
Russell King73970052017-07-25 15:03:39 +01002385 return 0;
2386}
2387
2388static int sfp_remove(struct platform_device *pdev)
2389{
2390 struct sfp *sfp = platform_get_drvdata(pdev);
2391
2392 sfp_unregister_socket(sfp->sfp_bus);
2393
Russell King0cb96b52019-12-03 23:51:22 +00002394 rtnl_lock();
2395 sfp_sm_event(sfp, SFP_E_REMOVE);
2396 rtnl_unlock();
2397
Russell King73970052017-07-25 15:03:39 +01002398 return 0;
2399}
2400
Robert Hancock257c2552019-06-07 10:42:35 -06002401static void sfp_shutdown(struct platform_device *pdev)
2402{
2403 struct sfp *sfp = platform_get_drvdata(pdev);
2404 int i;
2405
2406 for (i = 0; i < GPIO_MAX; i++) {
2407 if (!sfp->gpio_irq[i])
2408 continue;
2409
2410 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
2411 }
2412
2413 cancel_delayed_work_sync(&sfp->poll);
2414 cancel_delayed_work_sync(&sfp->timeout);
2415}
2416
Russell King73970052017-07-25 15:03:39 +01002417static struct platform_driver sfp_driver = {
2418 .probe = sfp_probe,
2419 .remove = sfp_remove,
Robert Hancock257c2552019-06-07 10:42:35 -06002420 .shutdown = sfp_shutdown,
Russell King73970052017-07-25 15:03:39 +01002421 .driver = {
2422 .name = "sfp",
2423 .of_match_table = sfp_of_match,
2424 },
2425};
2426
2427static int sfp_init(void)
2428{
2429 poll_jiffies = msecs_to_jiffies(100);
2430
2431 return platform_driver_register(&sfp_driver);
2432}
2433module_init(sfp_init);
2434
2435static void sfp_exit(void)
2436{
2437 platform_driver_unregister(&sfp_driver);
2438}
2439module_exit(sfp_exit);
2440
2441MODULE_ALIAS("platform:sfp");
2442MODULE_AUTHOR("Russell King");
2443MODULE_LICENSE("GPL v2");