742-v5.5-net-sfp-add-support-for-module-quirks.patch 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. From 8df5dd55cef48c0769379e04dbc085a899b106d4 Mon Sep 17 00:00:00 2001
  2. From: Russell King <[email protected]>
  3. Date: Fri, 8 Mar 2019 14:02:25 +0000
  4. Subject: [PATCH 640/660] net: sfp: add support for module quirks
  5. Add support for applying module quirks to the list of supported
  6. ethtool link modes.
  7. Signed-off-by: Russell King <[email protected]>
  8. ---
  9. drivers/net/phy/sfp-bus.c | 54 +++++++++++++++++++++++++++++++++++++++
  10. 1 file changed, 54 insertions(+)
  11. --- a/drivers/net/phy/sfp-bus.c
  12. +++ b/drivers/net/phy/sfp-bus.c
  13. @@ -9,6 +9,12 @@
  14. #include "sfp.h"
  15. +struct sfp_quirk {
  16. + const char *vendor;
  17. + const char *part;
  18. + void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
  19. +};
  20. +
  21. /**
  22. * struct sfp_bus - internal representation of a sfp bus
  23. */
  24. @@ -21,6 +27,7 @@ struct sfp_bus {
  25. const struct sfp_socket_ops *socket_ops;
  26. struct device *sfp_dev;
  27. struct sfp *sfp;
  28. + const struct sfp_quirk *sfp_quirk;
  29. const struct sfp_upstream_ops *upstream_ops;
  30. void *upstream;
  31. @@ -30,6 +37,46 @@ struct sfp_bus {
  32. bool started;
  33. };
  34. +static const struct sfp_quirk sfp_quirks[] = {
  35. +};
  36. +
  37. +static size_t sfp_strlen(const char *str, size_t maxlen)
  38. +{
  39. + size_t size, i;
  40. +
  41. + /* Trailing characters should be filled with space chars */
  42. + for (i = 0, size = 0; i < maxlen; i++)
  43. + if (str[i] != ' ')
  44. + size = i + 1;
  45. +
  46. + return size;
  47. +}
  48. +
  49. +static bool sfp_match(const char *qs, const char *str, size_t len)
  50. +{
  51. + if (!qs)
  52. + return true;
  53. + if (strlen(qs) != len)
  54. + return false;
  55. + return !strncmp(qs, str, len);
  56. +}
  57. +
  58. +static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
  59. +{
  60. + const struct sfp_quirk *q;
  61. + unsigned int i;
  62. + size_t vs, ps;
  63. +
  64. + vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
  65. + ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
  66. +
  67. + for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
  68. + if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
  69. + sfp_match(q->part, id->base.vendor_pn, ps))
  70. + return q;
  71. +
  72. + return NULL;
  73. +}
  74. /**
  75. * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
  76. * @bus: a pointer to the &struct sfp_bus structure for the sfp module
  77. @@ -233,6 +280,9 @@ void sfp_parse_support(struct sfp_bus *b
  78. phylink_set(modes, 1000baseX_Full);
  79. }
  80. + if (bus->sfp_quirk)
  81. + bus->sfp_quirk->modes(id, modes);
  82. +
  83. bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
  84. phylink_set(support, Autoneg);
  85. @@ -609,6 +659,8 @@ int sfp_module_insert(struct sfp_bus *bu
  86. const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
  87. int ret = 0;
  88. + bus->sfp_quirk = sfp_lookup_quirk(id);
  89. +
  90. if (ops && ops->module_insert)
  91. ret = ops->module_insert(bus->upstream, id);
  92. @@ -622,6 +674,8 @@ void sfp_module_remove(struct sfp_bus *b
  93. if (ops && ops->module_remove)
  94. ops->module_remove(bus->upstream);
  95. +
  96. + bus->sfp_quirk = NULL;
  97. }
  98. EXPORT_SYMBOL_GPL(sfp_module_remove);