phy_caps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/ethtool.h>
  3. #include <linux/linkmode.h>
  4. #include <linux/phy.h>
  5. #include "phy-caps.h"
  6. static struct link_capabilities link_caps[__LINK_CAPA_MAX] __ro_after_init = {
  7. { SPEED_10, DUPLEX_HALF, {0} }, /* LINK_CAPA_10HD */
  8. { SPEED_10, DUPLEX_FULL, {0} }, /* LINK_CAPA_10FD */
  9. { SPEED_100, DUPLEX_HALF, {0} }, /* LINK_CAPA_100HD */
  10. { SPEED_100, DUPLEX_FULL, {0} }, /* LINK_CAPA_100FD */
  11. { SPEED_1000, DUPLEX_HALF, {0} }, /* LINK_CAPA_1000HD */
  12. { SPEED_1000, DUPLEX_FULL, {0} }, /* LINK_CAPA_1000FD */
  13. { SPEED_2500, DUPLEX_FULL, {0} }, /* LINK_CAPA_2500FD */
  14. { SPEED_5000, DUPLEX_FULL, {0} }, /* LINK_CAPA_5000FD */
  15. { SPEED_10000, DUPLEX_FULL, {0} }, /* LINK_CAPA_10000FD */
  16. { SPEED_20000, DUPLEX_FULL, {0} }, /* LINK_CAPA_20000FD */
  17. { SPEED_25000, DUPLEX_FULL, {0} }, /* LINK_CAPA_25000FD */
  18. { SPEED_40000, DUPLEX_FULL, {0} }, /* LINK_CAPA_40000FD */
  19. { SPEED_50000, DUPLEX_FULL, {0} }, /* LINK_CAPA_50000FD */
  20. { SPEED_56000, DUPLEX_FULL, {0} }, /* LINK_CAPA_56000FD */
  21. { SPEED_80000, DUPLEX_FULL, {0} }, /* LINK_CAPA_80000FD */
  22. { SPEED_100000, DUPLEX_FULL, {0} }, /* LINK_CAPA_100000FD */
  23. { SPEED_200000, DUPLEX_FULL, {0} }, /* LINK_CAPA_200000FD */
  24. { SPEED_400000, DUPLEX_FULL, {0} }, /* LINK_CAPA_400000FD */
  25. { SPEED_800000, DUPLEX_FULL, {0} }, /* LINK_CAPA_800000FD */
  26. { SPEED_1600000, DUPLEX_FULL, {0} }, /* LINK_CAPA_1600000FD */
  27. };
  28. static int speed_duplex_to_capa(int speed, unsigned int duplex)
  29. {
  30. if (duplex == DUPLEX_UNKNOWN ||
  31. (speed > SPEED_1000 && duplex != DUPLEX_FULL))
  32. return -EINVAL;
  33. switch (speed) {
  34. case SPEED_10: return duplex == DUPLEX_FULL ?
  35. LINK_CAPA_10FD : LINK_CAPA_10HD;
  36. case SPEED_100: return duplex == DUPLEX_FULL ?
  37. LINK_CAPA_100FD : LINK_CAPA_100HD;
  38. case SPEED_1000: return duplex == DUPLEX_FULL ?
  39. LINK_CAPA_1000FD : LINK_CAPA_1000HD;
  40. case SPEED_2500: return LINK_CAPA_2500FD;
  41. case SPEED_5000: return LINK_CAPA_5000FD;
  42. case SPEED_10000: return LINK_CAPA_10000FD;
  43. case SPEED_20000: return LINK_CAPA_20000FD;
  44. case SPEED_25000: return LINK_CAPA_25000FD;
  45. case SPEED_40000: return LINK_CAPA_40000FD;
  46. case SPEED_50000: return LINK_CAPA_50000FD;
  47. case SPEED_56000: return LINK_CAPA_56000FD;
  48. case SPEED_80000: return LINK_CAPA_80000FD;
  49. case SPEED_100000: return LINK_CAPA_100000FD;
  50. case SPEED_200000: return LINK_CAPA_200000FD;
  51. case SPEED_400000: return LINK_CAPA_400000FD;
  52. case SPEED_800000: return LINK_CAPA_800000FD;
  53. case SPEED_1600000: return LINK_CAPA_1600000FD;
  54. }
  55. return -EINVAL;
  56. }
  57. #define for_each_link_caps_asc_speed(cap) \
  58. for (cap = link_caps; cap < &link_caps[__LINK_CAPA_MAX]; cap++)
  59. #define for_each_link_caps_desc_speed(cap) \
  60. for (cap = &link_caps[__LINK_CAPA_MAX - 1]; cap >= link_caps; cap--)
  61. /**
  62. * phy_caps_init() - Initializes the link_caps array from the link_mode_params.
  63. *
  64. * Returns: 0 if phy caps init was successful, -EINVAL if we found an
  65. * unexpected linkmode setting that requires LINK_CAPS update.
  66. *
  67. */
  68. int __init phy_caps_init(void)
  69. {
  70. const struct link_mode_info *linkmode;
  71. int i, capa;
  72. /* Fill the caps array from net/ethtool/common.c */
  73. for (i = 0; i < __ETHTOOL_LINK_MODE_MASK_NBITS; i++) {
  74. linkmode = &link_mode_params[i];
  75. /* Sanity check the linkmodes array for number of pairs */
  76. if (linkmode->pairs < linkmode->min_pairs) {
  77. pr_err("Pairs count must not be under min_pairs for linkmode %d\n",
  78. i);
  79. return -EINVAL;
  80. }
  81. capa = speed_duplex_to_capa(linkmode->speed, linkmode->duplex);
  82. if (capa < 0) {
  83. if (linkmode->speed != SPEED_UNKNOWN) {
  84. pr_err("Unknown speed %d, please update LINK_CAPS\n",
  85. linkmode->speed);
  86. return -EINVAL;
  87. }
  88. continue;
  89. }
  90. __set_bit(i, link_caps[capa].linkmodes);
  91. }
  92. return 0;
  93. }
  94. /**
  95. * phy_caps_speeds() - Fill an array of supported SPEED_* values for given modes
  96. * @speeds: Output array to store the speeds list into
  97. * @size: Size of the output array
  98. * @linkmodes: Linkmodes to get the speeds from
  99. *
  100. * Fills the speeds array with all possible speeds that can be achieved with
  101. * the specified linkmodes.
  102. *
  103. * Returns: The number of speeds filled into the array. If the input array isn't
  104. * big enough to store all speeds, fill it as much as possible.
  105. */
  106. size_t phy_caps_speeds(unsigned int *speeds, size_t size,
  107. unsigned long *linkmodes)
  108. {
  109. struct link_capabilities *lcap;
  110. size_t count = 0;
  111. for_each_link_caps_asc_speed(lcap) {
  112. if (linkmode_intersects(lcap->linkmodes, linkmodes) &&
  113. (count == 0 || speeds[count - 1] != lcap->speed)) {
  114. speeds[count++] = lcap->speed;
  115. if (count >= size)
  116. break;
  117. }
  118. }
  119. return count;
  120. }
  121. /**
  122. * phy_caps_lookup_by_linkmode() - Lookup the fastest matching link_capabilities
  123. * @linkmodes: Linkmodes to match against
  124. *
  125. * Returns: The highest-speed link_capabilities that intersects the given
  126. * linkmodes. In case several DUPLEX_ options exist at that speed,
  127. * DUPLEX_FULL is matched first. NULL is returned if no match.
  128. */
  129. const struct link_capabilities *
  130. phy_caps_lookup_by_linkmode(const unsigned long *linkmodes)
  131. {
  132. struct link_capabilities *lcap;
  133. for_each_link_caps_desc_speed(lcap)
  134. if (linkmode_intersects(lcap->linkmodes, linkmodes))
  135. return lcap;
  136. return NULL;
  137. }
  138. /**
  139. * phy_caps_lookup_by_linkmode_rev() - Lookup the slowest matching link_capabilities
  140. * @linkmodes: Linkmodes to match against
  141. * @fdx_only: Full duplex match only when set
  142. *
  143. * Returns: The lowest-speed link_capabilities that intersects the given
  144. * linkmodes. When set, fdx_only will ignore half-duplex matches.
  145. * NULL is returned if no match.
  146. */
  147. const struct link_capabilities *
  148. phy_caps_lookup_by_linkmode_rev(const unsigned long *linkmodes, bool fdx_only)
  149. {
  150. struct link_capabilities *lcap;
  151. for_each_link_caps_asc_speed(lcap) {
  152. if (fdx_only && lcap->duplex != DUPLEX_FULL)
  153. continue;
  154. if (linkmode_intersects(lcap->linkmodes, linkmodes))
  155. return lcap;
  156. }
  157. return NULL;
  158. }
  159. /**
  160. * phy_caps_lookup() - Lookup capabilities by speed/duplex that matches a mask
  161. * @speed: Speed to match
  162. * @duplex: Duplex to match
  163. * @supported: Mask of linkmodes to match
  164. * @exact: Perform an exact match or not.
  165. *
  166. * Lookup a link_capabilities entry that intersect the supported linkmodes mask,
  167. * and that matches the passed speed and duplex.
  168. *
  169. * When @exact is set, an exact match is performed on speed and duplex, meaning
  170. * that if the linkmodes for the given speed and duplex intersect the supported
  171. * mask, this capability is returned, otherwise we don't have a match and return
  172. * NULL.
  173. *
  174. * When @exact is not set, we return either an exact match, or matching capabilities
  175. * at lower speed, or the lowest matching speed, or NULL.
  176. *
  177. * Non-exact matches will try to return an exact speed and duplex match, but may
  178. * return matching capabilities with same speed but a different duplex.
  179. *
  180. * Returns: a matched link_capabilities according to the above process, NULL
  181. * otherwise.
  182. */
  183. const struct link_capabilities *
  184. phy_caps_lookup(int speed, unsigned int duplex, const unsigned long *supported,
  185. bool exact)
  186. {
  187. const struct link_capabilities *lcap, *match = NULL, *last = NULL;
  188. for_each_link_caps_desc_speed(lcap) {
  189. if (linkmode_intersects(lcap->linkmodes, supported)) {
  190. last = lcap;
  191. /* exact match on speed and duplex*/
  192. if (lcap->speed == speed && lcap->duplex == duplex) {
  193. return lcap;
  194. } else if (!exact) {
  195. if (!match && lcap->speed <= speed)
  196. match = lcap;
  197. if (lcap->speed < speed)
  198. break;
  199. }
  200. }
  201. }
  202. if (!match && !exact)
  203. match = last;
  204. return match;
  205. }
  206. EXPORT_SYMBOL_GPL(phy_caps_lookup);
  207. /**
  208. * phy_caps_linkmode_max_speed() - Clamp a linkmodes set to a max speed
  209. * @max_speed: Speed limit for the linkmode set
  210. * @linkmodes: Linkmodes to limit
  211. */
  212. void phy_caps_linkmode_max_speed(u32 max_speed, unsigned long *linkmodes)
  213. {
  214. struct link_capabilities *lcap;
  215. for_each_link_caps_desc_speed(lcap)
  216. if (lcap->speed > max_speed)
  217. linkmode_andnot(linkmodes, linkmodes, lcap->linkmodes);
  218. else
  219. break;
  220. }
  221. /**
  222. * phy_caps_valid() - Validate a linkmodes set agains given speed and duplex
  223. * @speed: input speed to validate
  224. * @duplex: input duplex to validate. Passing DUPLEX_UNKNOWN is always not valid
  225. * @linkmodes: The linkmodes to validate
  226. *
  227. * Returns: True if at least one of the linkmodes in @linkmodes can function at
  228. * the given speed and duplex, false otherwise.
  229. */
  230. bool phy_caps_valid(int speed, int duplex, const unsigned long *linkmodes)
  231. {
  232. int capa = speed_duplex_to_capa(speed, duplex);
  233. if (capa < 0)
  234. return false;
  235. return linkmode_intersects(link_caps[capa].linkmodes, linkmodes);
  236. }
  237. /**
  238. * phy_caps_linkmodes() - Convert a bitfield of capabilities into linkmodes
  239. * @caps: The list of caps, each bit corresponding to a LINK_CAPA value
  240. * @linkmodes: The set of linkmodes to fill. Must be previously initialized.
  241. */
  242. void phy_caps_linkmodes(unsigned long caps, unsigned long *linkmodes)
  243. {
  244. unsigned long capa;
  245. for_each_set_bit(capa, &caps, __LINK_CAPA_MAX)
  246. linkmode_or(linkmodes, linkmodes, link_caps[capa].linkmodes);
  247. }
  248. EXPORT_SYMBOL_GPL(phy_caps_linkmodes);
  249. /**
  250. * phy_caps_from_interface() - Get the link capa from a given PHY interface
  251. * @interface: The PHY interface we want to get the possible Speed/Duplex from
  252. *
  253. * Returns: A bitmask of LINK_CAPA_xxx values that can be achieved with the
  254. * provided interface.
  255. */
  256. unsigned long phy_caps_from_interface(phy_interface_t interface)
  257. {
  258. unsigned long link_caps = 0;
  259. switch (interface) {
  260. case PHY_INTERFACE_MODE_USXGMII:
  261. link_caps |= BIT(LINK_CAPA_10000FD) | BIT(LINK_CAPA_5000FD);
  262. fallthrough;
  263. case PHY_INTERFACE_MODE_10G_QXGMII:
  264. link_caps |= BIT(LINK_CAPA_2500FD);
  265. fallthrough;
  266. case PHY_INTERFACE_MODE_RGMII_TXID:
  267. case PHY_INTERFACE_MODE_RGMII_RXID:
  268. case PHY_INTERFACE_MODE_RGMII_ID:
  269. case PHY_INTERFACE_MODE_RGMII:
  270. case PHY_INTERFACE_MODE_PSGMII:
  271. case PHY_INTERFACE_MODE_QSGMII:
  272. case PHY_INTERFACE_MODE_QUSGMII:
  273. case PHY_INTERFACE_MODE_SGMII:
  274. case PHY_INTERFACE_MODE_GMII:
  275. link_caps |= BIT(LINK_CAPA_1000HD) | BIT(LINK_CAPA_1000FD);
  276. fallthrough;
  277. case PHY_INTERFACE_MODE_REVRMII:
  278. case PHY_INTERFACE_MODE_RMII:
  279. case PHY_INTERFACE_MODE_SMII:
  280. case PHY_INTERFACE_MODE_REVMII:
  281. case PHY_INTERFACE_MODE_MII:
  282. link_caps |= BIT(LINK_CAPA_10HD) | BIT(LINK_CAPA_10FD);
  283. fallthrough;
  284. case PHY_INTERFACE_MODE_100BASEX:
  285. link_caps |= BIT(LINK_CAPA_100HD) | BIT(LINK_CAPA_100FD);
  286. break;
  287. case PHY_INTERFACE_MODE_MIILITE:
  288. link_caps |= BIT(LINK_CAPA_10FD) | BIT(LINK_CAPA_100FD);
  289. break;
  290. case PHY_INTERFACE_MODE_TBI:
  291. case PHY_INTERFACE_MODE_MOCA:
  292. case PHY_INTERFACE_MODE_RTBI:
  293. case PHY_INTERFACE_MODE_1000BASEX:
  294. link_caps |= BIT(LINK_CAPA_1000HD);
  295. fallthrough;
  296. case PHY_INTERFACE_MODE_1000BASEKX:
  297. case PHY_INTERFACE_MODE_TRGMII:
  298. link_caps |= BIT(LINK_CAPA_1000FD);
  299. break;
  300. case PHY_INTERFACE_MODE_2500BASEX:
  301. link_caps |= BIT(LINK_CAPA_2500FD);
  302. break;
  303. case PHY_INTERFACE_MODE_5GBASER:
  304. link_caps |= BIT(LINK_CAPA_5000FD);
  305. break;
  306. case PHY_INTERFACE_MODE_XGMII:
  307. case PHY_INTERFACE_MODE_RXAUI:
  308. case PHY_INTERFACE_MODE_XAUI:
  309. case PHY_INTERFACE_MODE_10GBASER:
  310. case PHY_INTERFACE_MODE_10GKR:
  311. link_caps |= BIT(LINK_CAPA_10000FD);
  312. break;
  313. case PHY_INTERFACE_MODE_25GBASER:
  314. link_caps |= BIT(LINK_CAPA_25000FD);
  315. break;
  316. case PHY_INTERFACE_MODE_XLGMII:
  317. link_caps |= BIT(LINK_CAPA_40000FD);
  318. break;
  319. case PHY_INTERFACE_MODE_50GBASER:
  320. case PHY_INTERFACE_MODE_LAUI:
  321. link_caps |= BIT(LINK_CAPA_50000FD);
  322. break;
  323. case PHY_INTERFACE_MODE_100GBASEP:
  324. link_caps |= BIT(LINK_CAPA_100000FD);
  325. break;
  326. case PHY_INTERFACE_MODE_INTERNAL:
  327. link_caps |= LINK_CAPA_ALL;
  328. break;
  329. case PHY_INTERFACE_MODE_NA:
  330. case PHY_INTERFACE_MODE_MAX:
  331. break;
  332. }
  333. return link_caps;
  334. }
  335. EXPORT_SYMBOL_GPL(phy_caps_from_interface);
  336. /**
  337. * phy_caps_medium_get_supported() - Returns linkmodes supported on a given medium
  338. * @supported: After this call, contains all possible linkmodes on a given medium,
  339. * and with the given number of pairs, or less.
  340. * @medium: The medium to get the support from
  341. * @pairs: The number of pairs used on the given medium. Only relevant for modes
  342. * that support this notion, such as BaseT. Pass 0 if not applicable.
  343. *
  344. * If no match exists, the supported field is left untouched.
  345. */
  346. void phy_caps_medium_get_supported(unsigned long *supported,
  347. enum ethtool_link_medium medium,
  348. int pairs)
  349. {
  350. int i;
  351. for (i = 0; i < __ETHTOOL_LINK_MODE_MASK_NBITS; i++) {
  352. /* Special bits such as Autoneg, Pause, Asym_pause, etc. are
  353. * set and will be masked away by the port parent.
  354. */
  355. if (link_mode_params[i].mediums == BIT(ETHTOOL_LINK_MEDIUM_NONE)) {
  356. linkmode_set_bit(i, supported);
  357. continue;
  358. }
  359. /* If this medium matches, and had a non-zero min-pairs */
  360. if (link_mode_params[i].mediums & BIT(medium) &&
  361. (!link_mode_params[i].min_pairs ||
  362. (link_mode_params[i].min_pairs <= pairs &&
  363. link_mode_params[i].pairs >= pairs)))
  364. linkmode_set_bit(i, supported);
  365. }
  366. }
  367. EXPORT_SYMBOL_GPL(phy_caps_medium_get_supported);
  368. /**
  369. * phy_caps_mediums_from_linkmodes() - Get all mediums from a linkmodes list
  370. * @linkmodes: A bitset of linkmodes to get the mediums from
  371. *
  372. * Returns: A bitset of ETHTOOL_MEDIUM_XXX values corresponding to all medium
  373. * types in the linkmodes list
  374. */
  375. u32 phy_caps_mediums_from_linkmodes(unsigned long *linkmodes)
  376. {
  377. const struct link_mode_info *linkmode;
  378. u32 mediums = 0;
  379. int i;
  380. for_each_set_bit(i, linkmodes, __ETHTOOL_LINK_MODE_MASK_NBITS) {
  381. linkmode = &link_mode_params[i];
  382. mediums |= linkmode->mediums;
  383. }
  384. return mediums;
  385. }
  386. EXPORT_SYMBOL_GPL(phy_caps_mediums_from_linkmodes);