aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Nettleton <jon@solid-run.com>2018-02-27 10:53:12 -0500
committerDavid S. Miller <davem@davemloft.net>2018-02-28 11:07:11 -0500
commit3bb35261c74e394aa42d0c636d2608093a1e3309 (patch)
treeb16ee97e936e7db44364d28641871f31db2088ce
parent66f5325ce93a2e6fd726fca31fea91baf36a392e (diff)
sfp: add high power module support
This patch is the result of work by both Jon Nettleton and Russell King. Jon wrote the original patch, adding support for SFP modules which require a power level greater than '1'. Russell's changes: - Fix the power levels for big-endian, and make the code flow better. - Convert to use device_property_read_u8() - Warn for power levels exceeding host level SFF-8431 says: "To avoid exceeding system power supply limits and cooling capacity, all modules at power up by default shall operate with up to 1.0 W. Hosts supporting Power Level II or III operation may enable a Power Level II or III module through the 2-wire interface. Power Level II or III modules shall assert the power level declaration bit of SFF-8472." Print a warning for modules that exceed the host power level, and leave them operating in power level 1. - Fix i2c write The first byte of any write after the bus address is always the device address. In order to write a value to device D, address I, value V, we need to generate on the bus: S DDDDDDDD A IIIIIIII A VVVVVVVV A P where S = start, R = restart, A = ack, P = stop. Splitting this as two: S DDDDDDDD A IIIIIIII A R DDDDDDDD A VVVVVVVV A P results in the device's address register being written first by I and then by V - the addressed register within the device is not written. - Avoid power mode switching if 0xa2 is not implemented Some modules indicate that they support power level II or power level III, but do not implement address 0xa2, meaning that the bit to set them to high power mode is not accessible. These modules appear to have the sff8472_compliance field set to zero, and also do not implement diagnostics. Detect this, but also ensure that the module does not require the address switching mode, which we do not implement. - Use mW for power level rather than power level number. - Fix high power mode transition We must not switch to SFP_MOD_PRESENT state until we have finished initialising, because the remaining state machines check for that state. Add SFP_MOD_HPOWER as an intermediate state. - Use definition for I2C register address rather than constant. Signed-off-by: Jon Nettleton <jon@solid-run.com> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/phy/sfp.c150
1 files changed, 132 insertions, 18 deletions
diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
index 6c7d9289078d..83bf4959b043 100644
--- a/drivers/net/phy/sfp.c
+++ b/drivers/net/phy/sfp.c
@@ -42,6 +42,7 @@ enum {
42 42
43 SFP_MOD_EMPTY = 0, 43 SFP_MOD_EMPTY = 0,
44 SFP_MOD_PROBE, 44 SFP_MOD_PROBE,
45 SFP_MOD_HPOWER,
45 SFP_MOD_PRESENT, 46 SFP_MOD_PRESENT,
46 SFP_MOD_ERROR, 47 SFP_MOD_ERROR,
47 48
@@ -86,6 +87,7 @@ static const enum gpiod_flags gpio_flags[] = {
86 * access the I2C EEPROM. However, Avago modules require 300ms. 87 * access the I2C EEPROM. However, Avago modules require 300ms.
87 */ 88 */
88#define T_PROBE_INIT msecs_to_jiffies(300) 89#define T_PROBE_INIT msecs_to_jiffies(300)
90#define T_HPOWER_LEVEL msecs_to_jiffies(300)
89#define T_PROBE_RETRY msecs_to_jiffies(100) 91#define T_PROBE_RETRY msecs_to_jiffies(100)
90 92
91/* SFP modules appear to always have their PHY configured for bus address 93/* SFP modules appear to always have their PHY configured for bus address
@@ -110,10 +112,12 @@ struct sfp {
110 struct sfp_bus *sfp_bus; 112 struct sfp_bus *sfp_bus;
111 struct phy_device *mod_phy; 113 struct phy_device *mod_phy;
112 const struct sff_data *type; 114 const struct sff_data *type;
115 u32 max_power_mW;
113 116
114 unsigned int (*get_state)(struct sfp *); 117 unsigned int (*get_state)(struct sfp *);
115 void (*set_state)(struct sfp *, unsigned int); 118 void (*set_state)(struct sfp *, unsigned int);
116 int (*read)(struct sfp *, bool, u8, void *, size_t); 119 int (*read)(struct sfp *, bool, u8, void *, size_t);
120 int (*write)(struct sfp *, bool, u8, void *, size_t);
117 121
118 struct gpio_desc *gpio[GPIO_MAX]; 122 struct gpio_desc *gpio[GPIO_MAX];
119 123
@@ -201,10 +205,11 @@ static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
201 } 205 }
202} 206}
203 207
204static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr, 208static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
205 void *buf, size_t len) 209 size_t len)
206{ 210{
207 struct i2c_msg msgs[2]; 211 struct i2c_msg msgs[2];
212 u8 bus_addr = a2 ? 0x51 : 0x50;
208 int ret; 213 int ret;
209 214
210 msgs[0].addr = bus_addr; 215 msgs[0].addr = bus_addr;
@@ -216,17 +221,38 @@ static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
216 msgs[1].len = len; 221 msgs[1].len = len;
217 msgs[1].buf = buf; 222 msgs[1].buf = buf;
218 223
219 ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs)); 224 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
220 if (ret < 0) 225 if (ret < 0)
221 return ret; 226 return ret;
222 227
223 return ret == ARRAY_SIZE(msgs) ? len : 0; 228 return ret == ARRAY_SIZE(msgs) ? len : 0;
224} 229}
225 230
226static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf, 231static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
227 size_t len) 232 size_t len)
228{ 233{
229 return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len); 234 struct i2c_msg msgs[1];
235 u8 bus_addr = a2 ? 0x51 : 0x50;
236 int ret;
237
238 msgs[0].addr = bus_addr;
239 msgs[0].flags = 0;
240 msgs[0].len = 1 + len;
241 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
242 if (!msgs[0].buf)
243 return -ENOMEM;
244
245 msgs[0].buf[0] = dev_addr;
246 memcpy(&msgs[0].buf[1], buf, len);
247
248 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
249
250 kfree(msgs[0].buf);
251
252 if (ret < 0)
253 return ret;
254
255 return ret == ARRAY_SIZE(msgs) ? len : 0;
230} 256}
231 257
232static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) 258static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
@@ -239,6 +265,7 @@ static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
239 265
240 sfp->i2c = i2c; 266 sfp->i2c = i2c;
241 sfp->read = sfp_i2c_read; 267 sfp->read = sfp_i2c_read;
268 sfp->write = sfp_i2c_write;
242 269
243 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c); 270 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
244 if (IS_ERR(i2c_mii)) 271 if (IS_ERR(i2c_mii))
@@ -274,6 +301,11 @@ static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
274 return sfp->read(sfp, a2, addr, buf, len); 301 return sfp->read(sfp, a2, addr, buf, len);
275} 302}
276 303
304static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
305{
306 return sfp->write(sfp, a2, addr, buf, len);
307}
308
277static unsigned int sfp_check(void *buf, size_t len) 309static unsigned int sfp_check(void *buf, size_t len)
278{ 310{
279 u8 *p, check; 311 u8 *p, check;
@@ -462,21 +494,83 @@ static void sfp_sm_mod_init(struct sfp *sfp)
462 sfp_sm_probe_phy(sfp); 494 sfp_sm_probe_phy(sfp);
463} 495}
464 496
497static int sfp_sm_mod_hpower(struct sfp *sfp)
498{
499 u32 power;
500 u8 val;
501 int err;
502
503 power = 1000;
504 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
505 power = 1500;
506 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
507 power = 2000;
508
509 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
510 (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
511 SFP_DIAGMON_DDM) {
512 /* The module appears not to implement bus address 0xa2,
513 * or requires an address change sequence, so assume that
514 * the module powers up in the indicated power mode.
515 */
516 if (power > sfp->max_power_mW) {
517 dev_err(sfp->dev,
518 "Host does not support %u.%uW modules\n",
519 power / 1000, (power / 100) % 10);
520 return -EINVAL;
521 }
522 return 0;
523 }
524
525 if (power > sfp->max_power_mW) {
526 dev_warn(sfp->dev,
527 "Host does not support %u.%uW modules, module left in power mode 1\n",
528 power / 1000, (power / 100) % 10);
529 return 0;
530 }
531
532 if (power <= 1000)
533 return 0;
534
535 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
536 if (err != sizeof(val)) {
537 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
538 err = -EAGAIN;
539 goto err;
540 }
541
542 val |= BIT(0);
543
544 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
545 if (err != sizeof(val)) {
546 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
547 err = -EAGAIN;
548 goto err;
549 }
550
551 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
552 power / 1000, (power / 100) % 10);
553 return T_HPOWER_LEVEL;
554
555err:
556 return err;
557}
558
465static int sfp_sm_mod_probe(struct sfp *sfp) 559static int sfp_sm_mod_probe(struct sfp *sfp)
466{ 560{
467 /* SFP module inserted - read I2C data */ 561 /* SFP module inserted - read I2C data */
468 struct sfp_eeprom_id id; 562 struct sfp_eeprom_id id;
469 u8 check; 563 u8 check;
470 int err; 564 int ret;
471 565
472 err = sfp_read(sfp, false, 0, &id, sizeof(id)); 566 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
473 if (err < 0) { 567 if (ret < 0) {
474 dev_err(sfp->dev, "failed to read EEPROM: %d\n", err); 568 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
475 return -EAGAIN; 569 return -EAGAIN;
476 } 570 }
477 571
478 if (err != sizeof(id)) { 572 if (ret != sizeof(id)) {
479 dev_err(sfp->dev, "EEPROM short read: %d\n", err); 573 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
480 return -EAGAIN; 574 return -EAGAIN;
481 } 575 }
482 576
@@ -521,7 +615,11 @@ static int sfp_sm_mod_probe(struct sfp *sfp)
521 dev_warn(sfp->dev, 615 dev_warn(sfp->dev,
522 "module address swap to access page 0xA2 is not supported.\n"); 616 "module address swap to access page 0xA2 is not supported.\n");
523 617
524 return sfp_module_insert(sfp->sfp_bus, &sfp->id); 618 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
619 if (ret < 0)
620 return ret;
621
622 return sfp_sm_mod_hpower(sfp);
525} 623}
526 624
527static void sfp_sm_mod_remove(struct sfp *sfp) 625static void sfp_sm_mod_remove(struct sfp *sfp)
@@ -560,17 +658,25 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
560 if (event == SFP_E_REMOVE) { 658 if (event == SFP_E_REMOVE) {
561 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0); 659 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
562 } else if (event == SFP_E_TIMEOUT) { 660 } else if (event == SFP_E_TIMEOUT) {
563 int err = sfp_sm_mod_probe(sfp); 661 int val = sfp_sm_mod_probe(sfp);
564 662
565 if (err == 0) 663 if (val == 0)
566 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0); 664 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
567 else if (err == -EAGAIN) 665 else if (val > 0)
568 sfp_sm_set_timer(sfp, T_PROBE_RETRY); 666 sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
569 else 667 else if (val != -EAGAIN)
570 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0); 668 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
669 else
670 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
571 } 671 }
572 break; 672 break;
573 673
674 case SFP_MOD_HPOWER:
675 if (event == SFP_E_TIMEOUT) {
676 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
677 break;
678 }
679 /* fallthrough */
574 case SFP_MOD_PRESENT: 680 case SFP_MOD_PRESENT:
575 case SFP_MOD_ERROR: 681 case SFP_MOD_ERROR:
576 if (event == SFP_E_REMOVE) { 682 if (event == SFP_E_REMOVE) {
@@ -889,6 +995,14 @@ static int sfp_probe(struct platform_device *pdev)
889 if (!(sfp->gpio[GPIO_MODDEF0])) 995 if (!(sfp->gpio[GPIO_MODDEF0]))
890 sfp->get_state = sff_gpio_get_state; 996 sfp->get_state = sff_gpio_get_state;
891 997
998 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
999 &sfp->max_power_mW);
1000 if (!sfp->max_power_mW)
1001 sfp->max_power_mW = 1000;
1002
1003 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1004 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1005
892 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops); 1006 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
893 if (!sfp->sfp_bus) 1007 if (!sfp->sfp_bus)
894 return -ENOMEM; 1008 return -ENOMEM;