phy-core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Core PHY library, taken from phy.c
  4. */
  5. #include <linux/export.h>
  6. #include <linux/phy.h>
  7. #include <linux/of.h>
  8. /**
  9. * phy_speed_to_str - Return a string representing the PHY link speed
  10. *
  11. * @speed: Speed of the link
  12. */
  13. const char *phy_speed_to_str(int speed)
  14. {
  15. BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 103,
  16. "Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
  17. "If a speed or mode has been added please update phy_speed_to_str "
  18. "and the PHY settings array.\n");
  19. switch (speed) {
  20. case SPEED_10:
  21. return "10Mbps";
  22. case SPEED_100:
  23. return "100Mbps";
  24. case SPEED_1000:
  25. return "1Gbps";
  26. case SPEED_2500:
  27. return "2.5Gbps";
  28. case SPEED_5000:
  29. return "5Gbps";
  30. case SPEED_10000:
  31. return "10Gbps";
  32. case SPEED_14000:
  33. return "14Gbps";
  34. case SPEED_20000:
  35. return "20Gbps";
  36. case SPEED_25000:
  37. return "25Gbps";
  38. case SPEED_40000:
  39. return "40Gbps";
  40. case SPEED_50000:
  41. return "50Gbps";
  42. case SPEED_56000:
  43. return "56Gbps";
  44. case SPEED_100000:
  45. return "100Gbps";
  46. case SPEED_200000:
  47. return "200Gbps";
  48. case SPEED_400000:
  49. return "400Gbps";
  50. case SPEED_800000:
  51. return "800Gbps";
  52. case SPEED_UNKNOWN:
  53. return "Unknown";
  54. default:
  55. return "Unsupported (update phy-core.c)";
  56. }
  57. }
  58. EXPORT_SYMBOL_GPL(phy_speed_to_str);
  59. /**
  60. * phy_duplex_to_str - Return string describing the duplex
  61. *
  62. * @duplex: Duplex setting to describe
  63. */
  64. const char *phy_duplex_to_str(unsigned int duplex)
  65. {
  66. if (duplex == DUPLEX_HALF)
  67. return "Half";
  68. if (duplex == DUPLEX_FULL)
  69. return "Full";
  70. if (duplex == DUPLEX_UNKNOWN)
  71. return "Unknown";
  72. return "Unsupported (update phy-core.c)";
  73. }
  74. EXPORT_SYMBOL_GPL(phy_duplex_to_str);
  75. /**
  76. * phy_rate_matching_to_str - Return a string describing the rate matching
  77. *
  78. * @rate_matching: Type of rate matching to describe
  79. */
  80. const char *phy_rate_matching_to_str(int rate_matching)
  81. {
  82. switch (rate_matching) {
  83. case RATE_MATCH_NONE:
  84. return "none";
  85. case RATE_MATCH_PAUSE:
  86. return "pause";
  87. case RATE_MATCH_CRS:
  88. return "crs";
  89. case RATE_MATCH_OPEN_LOOP:
  90. return "open-loop";
  91. }
  92. return "Unsupported (update phy-core.c)";
  93. }
  94. EXPORT_SYMBOL_GPL(phy_rate_matching_to_str);
  95. /**
  96. * phy_interface_num_ports - Return the number of links that can be carried by
  97. * a given MAC-PHY physical link. Returns 0 if this is
  98. * unknown, the number of links else.
  99. *
  100. * @interface: The interface mode we want to get the number of ports
  101. */
  102. int phy_interface_num_ports(phy_interface_t interface)
  103. {
  104. switch (interface) {
  105. case PHY_INTERFACE_MODE_NA:
  106. return 0;
  107. case PHY_INTERFACE_MODE_INTERNAL:
  108. case PHY_INTERFACE_MODE_MII:
  109. case PHY_INTERFACE_MODE_GMII:
  110. case PHY_INTERFACE_MODE_TBI:
  111. case PHY_INTERFACE_MODE_REVMII:
  112. case PHY_INTERFACE_MODE_RMII:
  113. case PHY_INTERFACE_MODE_REVRMII:
  114. case PHY_INTERFACE_MODE_RGMII:
  115. case PHY_INTERFACE_MODE_RGMII_ID:
  116. case PHY_INTERFACE_MODE_RGMII_RXID:
  117. case PHY_INTERFACE_MODE_RGMII_TXID:
  118. case PHY_INTERFACE_MODE_RTBI:
  119. case PHY_INTERFACE_MODE_XGMII:
  120. case PHY_INTERFACE_MODE_XLGMII:
  121. case PHY_INTERFACE_MODE_MOCA:
  122. case PHY_INTERFACE_MODE_TRGMII:
  123. case PHY_INTERFACE_MODE_USXGMII:
  124. case PHY_INTERFACE_MODE_SGMII:
  125. case PHY_INTERFACE_MODE_SMII:
  126. case PHY_INTERFACE_MODE_1000BASEX:
  127. case PHY_INTERFACE_MODE_2500BASEX:
  128. case PHY_INTERFACE_MODE_5GBASER:
  129. case PHY_INTERFACE_MODE_10GBASER:
  130. case PHY_INTERFACE_MODE_25GBASER:
  131. case PHY_INTERFACE_MODE_10GKR:
  132. case PHY_INTERFACE_MODE_100BASEX:
  133. case PHY_INTERFACE_MODE_RXAUI:
  134. case PHY_INTERFACE_MODE_XAUI:
  135. case PHY_INTERFACE_MODE_1000BASEKX:
  136. return 1;
  137. case PHY_INTERFACE_MODE_QSGMII:
  138. case PHY_INTERFACE_MODE_QUSGMII:
  139. case PHY_INTERFACE_MODE_10G_QXGMII:
  140. return 4;
  141. case PHY_INTERFACE_MODE_PSGMII:
  142. return 5;
  143. case PHY_INTERFACE_MODE_MAX:
  144. WARN_ONCE(1, "PHY_INTERFACE_MODE_MAX isn't a valid interface mode");
  145. return 0;
  146. }
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(phy_interface_num_ports);
  150. /* A mapping of all SUPPORTED settings to speed/duplex. This table
  151. * must be grouped by speed and sorted in descending match priority
  152. * - iow, descending speed.
  153. */
  154. #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
  155. .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
  156. static const struct phy_setting settings[] = {
  157. /* 800G */
  158. PHY_SETTING( 800000, FULL, 800000baseCR8_Full ),
  159. PHY_SETTING( 800000, FULL, 800000baseKR8_Full ),
  160. PHY_SETTING( 800000, FULL, 800000baseDR8_Full ),
  161. PHY_SETTING( 800000, FULL, 800000baseDR8_2_Full ),
  162. PHY_SETTING( 800000, FULL, 800000baseSR8_Full ),
  163. PHY_SETTING( 800000, FULL, 800000baseVR8_Full ),
  164. /* 400G */
  165. PHY_SETTING( 400000, FULL, 400000baseCR8_Full ),
  166. PHY_SETTING( 400000, FULL, 400000baseKR8_Full ),
  167. PHY_SETTING( 400000, FULL, 400000baseLR8_ER8_FR8_Full ),
  168. PHY_SETTING( 400000, FULL, 400000baseDR8_Full ),
  169. PHY_SETTING( 400000, FULL, 400000baseSR8_Full ),
  170. PHY_SETTING( 400000, FULL, 400000baseCR4_Full ),
  171. PHY_SETTING( 400000, FULL, 400000baseKR4_Full ),
  172. PHY_SETTING( 400000, FULL, 400000baseLR4_ER4_FR4_Full ),
  173. PHY_SETTING( 400000, FULL, 400000baseDR4_Full ),
  174. PHY_SETTING( 400000, FULL, 400000baseSR4_Full ),
  175. /* 200G */
  176. PHY_SETTING( 200000, FULL, 200000baseCR4_Full ),
  177. PHY_SETTING( 200000, FULL, 200000baseKR4_Full ),
  178. PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full ),
  179. PHY_SETTING( 200000, FULL, 200000baseDR4_Full ),
  180. PHY_SETTING( 200000, FULL, 200000baseSR4_Full ),
  181. PHY_SETTING( 200000, FULL, 200000baseCR2_Full ),
  182. PHY_SETTING( 200000, FULL, 200000baseKR2_Full ),
  183. PHY_SETTING( 200000, FULL, 200000baseLR2_ER2_FR2_Full ),
  184. PHY_SETTING( 200000, FULL, 200000baseDR2_Full ),
  185. PHY_SETTING( 200000, FULL, 200000baseSR2_Full ),
  186. /* 100G */
  187. PHY_SETTING( 100000, FULL, 100000baseCR4_Full ),
  188. PHY_SETTING( 100000, FULL, 100000baseKR4_Full ),
  189. PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full ),
  190. PHY_SETTING( 100000, FULL, 100000baseSR4_Full ),
  191. PHY_SETTING( 100000, FULL, 100000baseCR2_Full ),
  192. PHY_SETTING( 100000, FULL, 100000baseKR2_Full ),
  193. PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full ),
  194. PHY_SETTING( 100000, FULL, 100000baseDR2_Full ),
  195. PHY_SETTING( 100000, FULL, 100000baseSR2_Full ),
  196. PHY_SETTING( 100000, FULL, 100000baseCR_Full ),
  197. PHY_SETTING( 100000, FULL, 100000baseKR_Full ),
  198. PHY_SETTING( 100000, FULL, 100000baseLR_ER_FR_Full ),
  199. PHY_SETTING( 100000, FULL, 100000baseDR_Full ),
  200. PHY_SETTING( 100000, FULL, 100000baseSR_Full ),
  201. /* 56G */
  202. PHY_SETTING( 56000, FULL, 56000baseCR4_Full ),
  203. PHY_SETTING( 56000, FULL, 56000baseKR4_Full ),
  204. PHY_SETTING( 56000, FULL, 56000baseLR4_Full ),
  205. PHY_SETTING( 56000, FULL, 56000baseSR4_Full ),
  206. /* 50G */
  207. PHY_SETTING( 50000, FULL, 50000baseCR2_Full ),
  208. PHY_SETTING( 50000, FULL, 50000baseKR2_Full ),
  209. PHY_SETTING( 50000, FULL, 50000baseSR2_Full ),
  210. PHY_SETTING( 50000, FULL, 50000baseCR_Full ),
  211. PHY_SETTING( 50000, FULL, 50000baseKR_Full ),
  212. PHY_SETTING( 50000, FULL, 50000baseLR_ER_FR_Full ),
  213. PHY_SETTING( 50000, FULL, 50000baseDR_Full ),
  214. PHY_SETTING( 50000, FULL, 50000baseSR_Full ),
  215. /* 40G */
  216. PHY_SETTING( 40000, FULL, 40000baseCR4_Full ),
  217. PHY_SETTING( 40000, FULL, 40000baseKR4_Full ),
  218. PHY_SETTING( 40000, FULL, 40000baseLR4_Full ),
  219. PHY_SETTING( 40000, FULL, 40000baseSR4_Full ),
  220. /* 25G */
  221. PHY_SETTING( 25000, FULL, 25000baseCR_Full ),
  222. PHY_SETTING( 25000, FULL, 25000baseKR_Full ),
  223. PHY_SETTING( 25000, FULL, 25000baseSR_Full ),
  224. /* 20G */
  225. PHY_SETTING( 20000, FULL, 20000baseKR2_Full ),
  226. PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ),
  227. /* 10G */
  228. PHY_SETTING( 10000, FULL, 10000baseCR_Full ),
  229. PHY_SETTING( 10000, FULL, 10000baseER_Full ),
  230. PHY_SETTING( 10000, FULL, 10000baseKR_Full ),
  231. PHY_SETTING( 10000, FULL, 10000baseKX4_Full ),
  232. PHY_SETTING( 10000, FULL, 10000baseLR_Full ),
  233. PHY_SETTING( 10000, FULL, 10000baseLRM_Full ),
  234. PHY_SETTING( 10000, FULL, 10000baseR_FEC ),
  235. PHY_SETTING( 10000, FULL, 10000baseSR_Full ),
  236. PHY_SETTING( 10000, FULL, 10000baseT_Full ),
  237. /* 5G */
  238. PHY_SETTING( 5000, FULL, 5000baseT_Full ),
  239. /* 2.5G */
  240. PHY_SETTING( 2500, FULL, 2500baseT_Full ),
  241. PHY_SETTING( 2500, FULL, 2500baseX_Full ),
  242. /* 1G */
  243. PHY_SETTING( 1000, FULL, 1000baseT_Full ),
  244. PHY_SETTING( 1000, HALF, 1000baseT_Half ),
  245. PHY_SETTING( 1000, FULL, 1000baseT1_Full ),
  246. PHY_SETTING( 1000, FULL, 1000baseX_Full ),
  247. PHY_SETTING( 1000, FULL, 1000baseKX_Full ),
  248. /* 100M */
  249. PHY_SETTING( 100, FULL, 100baseT_Full ),
  250. PHY_SETTING( 100, FULL, 100baseT1_Full ),
  251. PHY_SETTING( 100, HALF, 100baseT_Half ),
  252. PHY_SETTING( 100, HALF, 100baseFX_Half ),
  253. PHY_SETTING( 100, FULL, 100baseFX_Full ),
  254. /* 10M */
  255. PHY_SETTING( 10, FULL, 10baseT_Full ),
  256. PHY_SETTING( 10, HALF, 10baseT_Half ),
  257. PHY_SETTING( 10, FULL, 10baseT1L_Full ),
  258. PHY_SETTING( 10, FULL, 10baseT1S_Full ),
  259. PHY_SETTING( 10, HALF, 10baseT1S_Half ),
  260. PHY_SETTING( 10, HALF, 10baseT1S_P2MP_Half ),
  261. PHY_SETTING( 10, FULL, 10baseT1BRR_Full ),
  262. };
  263. #undef PHY_SETTING
  264. /**
  265. * phy_lookup_setting - lookup a PHY setting
  266. * @speed: speed to match
  267. * @duplex: duplex to match
  268. * @mask: allowed link modes
  269. * @exact: an exact match is required
  270. *
  271. * Search the settings array for a setting that matches the speed and
  272. * duplex, and which is supported.
  273. *
  274. * If @exact is unset, either an exact match or %NULL for no match will
  275. * be returned.
  276. *
  277. * If @exact is set, an exact match, the fastest supported setting at
  278. * or below the specified speed, the slowest supported setting, or if
  279. * they all fail, %NULL will be returned.
  280. */
  281. const struct phy_setting *
  282. phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
  283. {
  284. const struct phy_setting *p, *match = NULL, *last = NULL;
  285. int i;
  286. for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
  287. if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
  288. test_bit(p->bit, mask)) {
  289. last = p;
  290. if (p->speed == speed && p->duplex == duplex) {
  291. /* Exact match for speed and duplex */
  292. match = p;
  293. break;
  294. } else if (!exact) {
  295. if (!match && p->speed <= speed)
  296. /* Candidate */
  297. match = p;
  298. if (p->speed < speed)
  299. break;
  300. }
  301. }
  302. }
  303. if (!match && !exact)
  304. match = last;
  305. return match;
  306. }
  307. EXPORT_SYMBOL_GPL(phy_lookup_setting);
  308. size_t phy_speeds(unsigned int *speeds, size_t size,
  309. unsigned long *mask)
  310. {
  311. size_t count;
  312. int i;
  313. for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
  314. if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
  315. test_bit(settings[i].bit, mask) &&
  316. (count == 0 || speeds[count - 1] != settings[i].speed))
  317. speeds[count++] = settings[i].speed;
  318. return count;
  319. }
  320. static void __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
  321. {
  322. const struct phy_setting *p;
  323. int i;
  324. for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
  325. if (p->speed > max_speed)
  326. linkmode_clear_bit(p->bit, addr);
  327. else
  328. break;
  329. }
  330. }
  331. static void __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  332. {
  333. __set_linkmode_max_speed(max_speed, phydev->supported);
  334. }
  335. /**
  336. * phy_set_max_speed - Set the maximum speed the PHY should support
  337. *
  338. * @phydev: The phy_device struct
  339. * @max_speed: Maximum speed
  340. *
  341. * The PHY might be more capable than the MAC. For example a Fast Ethernet
  342. * is connected to a 1G PHY. This function allows the MAC to indicate its
  343. * maximum speed, and so limit what the PHY will advertise.
  344. */
  345. void phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  346. {
  347. __set_phy_supported(phydev, max_speed);
  348. phy_advertise_supported(phydev);
  349. }
  350. EXPORT_SYMBOL(phy_set_max_speed);
  351. void of_set_phy_supported(struct phy_device *phydev)
  352. {
  353. struct device_node *node = phydev->mdio.dev.of_node;
  354. u32 max_speed;
  355. if (!IS_ENABLED(CONFIG_OF_MDIO))
  356. return;
  357. if (!node)
  358. return;
  359. if (!of_property_read_u32(node, "max-speed", &max_speed))
  360. __set_phy_supported(phydev, max_speed);
  361. }
  362. void of_set_phy_eee_broken(struct phy_device *phydev)
  363. {
  364. struct device_node *node = phydev->mdio.dev.of_node;
  365. u32 broken = 0;
  366. if (!IS_ENABLED(CONFIG_OF_MDIO))
  367. return;
  368. if (!node)
  369. return;
  370. if (of_property_read_bool(node, "eee-broken-100tx"))
  371. broken |= MDIO_EEE_100TX;
  372. if (of_property_read_bool(node, "eee-broken-1000t"))
  373. broken |= MDIO_EEE_1000T;
  374. if (of_property_read_bool(node, "eee-broken-10gt"))
  375. broken |= MDIO_EEE_10GT;
  376. if (of_property_read_bool(node, "eee-broken-1000kx"))
  377. broken |= MDIO_EEE_1000KX;
  378. if (of_property_read_bool(node, "eee-broken-10gkx4"))
  379. broken |= MDIO_EEE_10GKX4;
  380. if (of_property_read_bool(node, "eee-broken-10gkr"))
  381. broken |= MDIO_EEE_10GKR;
  382. phydev->eee_broken_modes = broken;
  383. }
  384. /**
  385. * phy_resolve_aneg_pause - Determine pause autoneg results
  386. *
  387. * @phydev: The phy_device struct
  388. *
  389. * Once autoneg has completed the local pause settings can be
  390. * resolved. Determine if pause and asymmetric pause should be used
  391. * by the MAC.
  392. */
  393. void phy_resolve_aneg_pause(struct phy_device *phydev)
  394. {
  395. if (phydev->duplex == DUPLEX_FULL) {
  396. phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  397. phydev->lp_advertising);
  398. phydev->asym_pause = linkmode_test_bit(
  399. ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  400. phydev->lp_advertising);
  401. }
  402. }
  403. EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
  404. /**
  405. * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings
  406. * @phydev: The phy_device struct
  407. *
  408. * Resolve our and the link partner advertisements into their corresponding
  409. * speed and duplex. If full duplex was negotiated, extract the pause mode
  410. * from the link partner mask.
  411. */
  412. void phy_resolve_aneg_linkmode(struct phy_device *phydev)
  413. {
  414. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  415. int i;
  416. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  417. for (i = 0; i < ARRAY_SIZE(settings); i++)
  418. if (test_bit(settings[i].bit, common)) {
  419. phydev->speed = settings[i].speed;
  420. phydev->duplex = settings[i].duplex;
  421. break;
  422. }
  423. phy_resolve_aneg_pause(phydev);
  424. }
  425. EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
  426. /**
  427. * phy_check_downshift - check whether downshift occurred
  428. * @phydev: The phy_device struct
  429. *
  430. * Check whether a downshift to a lower speed occurred. If this should be the
  431. * case warn the user.
  432. * Prerequisite for detecting downshift is that PHY driver implements the
  433. * read_status callback and sets phydev->speed to the actual link speed.
  434. */
  435. void phy_check_downshift(struct phy_device *phydev)
  436. {
  437. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  438. int i, speed = SPEED_UNKNOWN;
  439. phydev->downshifted_rate = 0;
  440. if (phydev->autoneg == AUTONEG_DISABLE ||
  441. phydev->speed == SPEED_UNKNOWN)
  442. return;
  443. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  444. for (i = 0; i < ARRAY_SIZE(settings); i++)
  445. if (test_bit(settings[i].bit, common)) {
  446. speed = settings[i].speed;
  447. break;
  448. }
  449. if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
  450. return;
  451. phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
  452. phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
  453. phydev->downshifted_rate = 1;
  454. }
  455. EXPORT_SYMBOL_GPL(phy_check_downshift);
  456. static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
  457. {
  458. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  459. int i = ARRAY_SIZE(settings);
  460. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  461. while (--i >= 0) {
  462. if (test_bit(settings[i].bit, common)) {
  463. if (fdx_only && settings[i].duplex != DUPLEX_FULL)
  464. continue;
  465. return settings[i].speed;
  466. }
  467. }
  468. return SPEED_UNKNOWN;
  469. }
  470. int phy_speed_down_core(struct phy_device *phydev)
  471. {
  472. int min_common_speed = phy_resolve_min_speed(phydev, true);
  473. if (min_common_speed == SPEED_UNKNOWN)
  474. return -EINVAL;
  475. __set_linkmode_max_speed(min_common_speed, phydev->advertising);
  476. return 0;
  477. }
  478. static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
  479. u16 regnum)
  480. {
  481. /* Write the desired MMD Devad */
  482. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
  483. /* Write the desired MMD register address */
  484. __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
  485. /* Select the Function : DATA with no post increment */
  486. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
  487. devad | MII_MMD_CTRL_NOINCR);
  488. }
  489. static int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45,
  490. int devad, u32 regnum)
  491. {
  492. if (is_c45)
  493. return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
  494. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  495. /* Read the content of the MMD's selected register */
  496. return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
  497. }
  498. static int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45,
  499. int devad, u32 regnum, u16 val)
  500. {
  501. if (is_c45)
  502. return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
  503. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  504. /* Write the data into MMD's selected register */
  505. return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
  506. }
  507. /**
  508. * __phy_read_mmd - Convenience function for reading a register
  509. * from an MMD on a given PHY.
  510. * @phydev: The phy_device struct
  511. * @devad: The MMD to read from (0..31)
  512. * @regnum: The register on the MMD to read (0..65535)
  513. *
  514. * Same rules as for __phy_read();
  515. */
  516. int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  517. {
  518. if (regnum > (u16)~0 || devad > 32)
  519. return -EINVAL;
  520. if (phydev->drv && phydev->drv->read_mmd)
  521. return phydev->drv->read_mmd(phydev, devad, regnum);
  522. return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr,
  523. phydev->is_c45, devad, regnum);
  524. }
  525. EXPORT_SYMBOL(__phy_read_mmd);
  526. /**
  527. * phy_read_mmd - Convenience function for reading a register
  528. * from an MMD on a given PHY.
  529. * @phydev: The phy_device struct
  530. * @devad: The MMD to read from
  531. * @regnum: The register on the MMD to read
  532. *
  533. * Same rules as for phy_read();
  534. */
  535. int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  536. {
  537. int ret;
  538. phy_lock_mdio_bus(phydev);
  539. ret = __phy_read_mmd(phydev, devad, regnum);
  540. phy_unlock_mdio_bus(phydev);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL(phy_read_mmd);
  544. /**
  545. * __phy_write_mmd - Convenience function for writing a register
  546. * on an MMD on a given PHY.
  547. * @phydev: The phy_device struct
  548. * @devad: The MMD to read from
  549. * @regnum: The register on the MMD to read
  550. * @val: value to write to @regnum
  551. *
  552. * Same rules as for __phy_write();
  553. */
  554. int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  555. {
  556. if (regnum > (u16)~0 || devad > 32)
  557. return -EINVAL;
  558. if (phydev->drv && phydev->drv->write_mmd)
  559. return phydev->drv->write_mmd(phydev, devad, regnum, val);
  560. return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr,
  561. phydev->is_c45, devad, regnum, val);
  562. }
  563. EXPORT_SYMBOL(__phy_write_mmd);
  564. /**
  565. * phy_write_mmd - Convenience function for writing a register
  566. * on an MMD on a given PHY.
  567. * @phydev: The phy_device struct
  568. * @devad: The MMD to read from
  569. * @regnum: The register on the MMD to read
  570. * @val: value to write to @regnum
  571. *
  572. * Same rules as for phy_write();
  573. */
  574. int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  575. {
  576. int ret;
  577. phy_lock_mdio_bus(phydev);
  578. ret = __phy_write_mmd(phydev, devad, regnum, val);
  579. phy_unlock_mdio_bus(phydev);
  580. return ret;
  581. }
  582. EXPORT_SYMBOL(phy_write_mmd);
  583. /**
  584. * __phy_package_read_mmd - read MMD reg relative to PHY package base addr
  585. * @phydev: The phy_device struct
  586. * @addr_offset: The offset to be added to PHY package base_addr
  587. * @devad: The MMD to read from
  588. * @regnum: The register on the MMD to read
  589. *
  590. * Convenience helper for reading a register of an MMD on a given PHY
  591. * using the PHY package base address. The base address is added to
  592. * the addr_offset value.
  593. *
  594. * Same calling rules as for __phy_read();
  595. *
  596. * NOTE: It's assumed that the entire PHY package is either C22 or C45.
  597. */
  598. int __phy_package_read_mmd(struct phy_device *phydev,
  599. unsigned int addr_offset, int devad,
  600. u32 regnum)
  601. {
  602. int addr = phy_package_address(phydev, addr_offset);
  603. if (addr < 0)
  604. return addr;
  605. if (regnum > (u16)~0 || devad > 32)
  606. return -EINVAL;
  607. return mmd_phy_read(phydev->mdio.bus, addr, phydev->is_c45, devad,
  608. regnum);
  609. }
  610. EXPORT_SYMBOL(__phy_package_read_mmd);
  611. /**
  612. * phy_package_read_mmd - read MMD reg relative to PHY package base addr
  613. * @phydev: The phy_device struct
  614. * @addr_offset: The offset to be added to PHY package base_addr
  615. * @devad: The MMD to read from
  616. * @regnum: The register on the MMD to read
  617. *
  618. * Convenience helper for reading a register of an MMD on a given PHY
  619. * using the PHY package base address. The base address is added to
  620. * the addr_offset value.
  621. *
  622. * Same calling rules as for phy_read();
  623. *
  624. * NOTE: It's assumed that the entire PHY package is either C22 or C45.
  625. */
  626. int phy_package_read_mmd(struct phy_device *phydev,
  627. unsigned int addr_offset, int devad,
  628. u32 regnum)
  629. {
  630. int addr = phy_package_address(phydev, addr_offset);
  631. int val;
  632. if (addr < 0)
  633. return addr;
  634. if (regnum > (u16)~0 || devad > 32)
  635. return -EINVAL;
  636. phy_lock_mdio_bus(phydev);
  637. val = mmd_phy_read(phydev->mdio.bus, addr, phydev->is_c45, devad,
  638. regnum);
  639. phy_unlock_mdio_bus(phydev);
  640. return val;
  641. }
  642. EXPORT_SYMBOL(phy_package_read_mmd);
  643. /**
  644. * __phy_package_write_mmd - write MMD reg relative to PHY package base addr
  645. * @phydev: The phy_device struct
  646. * @addr_offset: The offset to be added to PHY package base_addr
  647. * @devad: The MMD to write to
  648. * @regnum: The register on the MMD to write
  649. * @val: value to write to @regnum
  650. *
  651. * Convenience helper for writing a register of an MMD on a given PHY
  652. * using the PHY package base address. The base address is added to
  653. * the addr_offset value.
  654. *
  655. * Same calling rules as for __phy_write();
  656. *
  657. * NOTE: It's assumed that the entire PHY package is either C22 or C45.
  658. */
  659. int __phy_package_write_mmd(struct phy_device *phydev,
  660. unsigned int addr_offset, int devad,
  661. u32 regnum, u16 val)
  662. {
  663. int addr = phy_package_address(phydev, addr_offset);
  664. if (addr < 0)
  665. return addr;
  666. if (regnum > (u16)~0 || devad > 32)
  667. return -EINVAL;
  668. return mmd_phy_write(phydev->mdio.bus, addr, phydev->is_c45, devad,
  669. regnum, val);
  670. }
  671. EXPORT_SYMBOL(__phy_package_write_mmd);
  672. /**
  673. * phy_package_write_mmd - write MMD reg relative to PHY package base addr
  674. * @phydev: The phy_device struct
  675. * @addr_offset: The offset to be added to PHY package base_addr
  676. * @devad: The MMD to write to
  677. * @regnum: The register on the MMD to write
  678. * @val: value to write to @regnum
  679. *
  680. * Convenience helper for writing a register of an MMD on a given PHY
  681. * using the PHY package base address. The base address is added to
  682. * the addr_offset value.
  683. *
  684. * Same calling rules as for phy_write();
  685. *
  686. * NOTE: It's assumed that the entire PHY package is either C22 or C45.
  687. */
  688. int phy_package_write_mmd(struct phy_device *phydev,
  689. unsigned int addr_offset, int devad,
  690. u32 regnum, u16 val)
  691. {
  692. int addr = phy_package_address(phydev, addr_offset);
  693. int ret;
  694. if (addr < 0)
  695. return addr;
  696. if (regnum > (u16)~0 || devad > 32)
  697. return -EINVAL;
  698. phy_lock_mdio_bus(phydev);
  699. ret = mmd_phy_write(phydev->mdio.bus, addr, phydev->is_c45, devad,
  700. regnum, val);
  701. phy_unlock_mdio_bus(phydev);
  702. return ret;
  703. }
  704. EXPORT_SYMBOL(phy_package_write_mmd);
  705. /**
  706. * phy_modify_changed - Function for modifying a PHY register
  707. * @phydev: the phy_device struct
  708. * @regnum: register number to modify
  709. * @mask: bit mask of bits to clear
  710. * @set: new value of bits set in mask to write to @regnum
  711. *
  712. * NOTE: MUST NOT be called from interrupt context,
  713. * because the bus read/write functions may wait for an interrupt
  714. * to conclude the operation.
  715. *
  716. * Returns negative errno, 0 if there was no change, and 1 in case of change
  717. */
  718. int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  719. {
  720. int ret;
  721. phy_lock_mdio_bus(phydev);
  722. ret = __phy_modify_changed(phydev, regnum, mask, set);
  723. phy_unlock_mdio_bus(phydev);
  724. return ret;
  725. }
  726. EXPORT_SYMBOL_GPL(phy_modify_changed);
  727. /**
  728. * __phy_modify - Convenience function for modifying a PHY register
  729. * @phydev: the phy_device struct
  730. * @regnum: register number to modify
  731. * @mask: bit mask of bits to clear
  732. * @set: new value of bits set in mask to write to @regnum
  733. *
  734. * NOTE: MUST NOT be called from interrupt context,
  735. * because the bus read/write functions may wait for an interrupt
  736. * to conclude the operation.
  737. */
  738. int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  739. {
  740. int ret;
  741. ret = __phy_modify_changed(phydev, regnum, mask, set);
  742. return ret < 0 ? ret : 0;
  743. }
  744. EXPORT_SYMBOL_GPL(__phy_modify);
  745. /**
  746. * phy_modify - Convenience function for modifying a given PHY register
  747. * @phydev: the phy_device struct
  748. * @regnum: register number to write
  749. * @mask: bit mask of bits to clear
  750. * @set: new value of bits set in mask to write to @regnum
  751. *
  752. * NOTE: MUST NOT be called from interrupt context,
  753. * because the bus read/write functions may wait for an interrupt
  754. * to conclude the operation.
  755. */
  756. int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  757. {
  758. int ret;
  759. phy_lock_mdio_bus(phydev);
  760. ret = __phy_modify(phydev, regnum, mask, set);
  761. phy_unlock_mdio_bus(phydev);
  762. return ret;
  763. }
  764. EXPORT_SYMBOL_GPL(phy_modify);
  765. /**
  766. * __phy_modify_mmd_changed - Function for modifying a register on MMD
  767. * @phydev: the phy_device struct
  768. * @devad: the MMD containing register to modify
  769. * @regnum: register number to modify
  770. * @mask: bit mask of bits to clear
  771. * @set: new value of bits set in mask to write to @regnum
  772. *
  773. * Unlocked helper function which allows a MMD register to be modified as
  774. * new register value = (old register value & ~mask) | set
  775. *
  776. * Returns negative errno, 0 if there was no change, and 1 in case of change
  777. */
  778. int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  779. u16 mask, u16 set)
  780. {
  781. int new, ret;
  782. ret = __phy_read_mmd(phydev, devad, regnum);
  783. if (ret < 0)
  784. return ret;
  785. new = (ret & ~mask) | set;
  786. if (new == ret)
  787. return 0;
  788. ret = __phy_write_mmd(phydev, devad, regnum, new);
  789. return ret < 0 ? ret : 1;
  790. }
  791. EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
  792. /**
  793. * phy_modify_mmd_changed - Function for modifying a register on MMD
  794. * @phydev: the phy_device struct
  795. * @devad: the MMD containing register to modify
  796. * @regnum: register number to modify
  797. * @mask: bit mask of bits to clear
  798. * @set: new value of bits set in mask to write to @regnum
  799. *
  800. * NOTE: MUST NOT be called from interrupt context,
  801. * because the bus read/write functions may wait for an interrupt
  802. * to conclude the operation.
  803. *
  804. * Returns negative errno, 0 if there was no change, and 1 in case of change
  805. */
  806. int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  807. u16 mask, u16 set)
  808. {
  809. int ret;
  810. phy_lock_mdio_bus(phydev);
  811. ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  812. phy_unlock_mdio_bus(phydev);
  813. return ret;
  814. }
  815. EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
  816. /**
  817. * __phy_modify_mmd - Convenience function for modifying a register on MMD
  818. * @phydev: the phy_device struct
  819. * @devad: the MMD containing register to modify
  820. * @regnum: register number to modify
  821. * @mask: bit mask of bits to clear
  822. * @set: new value of bits set in mask to write to @regnum
  823. *
  824. * NOTE: MUST NOT be called from interrupt context,
  825. * because the bus read/write functions may wait for an interrupt
  826. * to conclude the operation.
  827. */
  828. int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  829. u16 mask, u16 set)
  830. {
  831. int ret;
  832. ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  833. return ret < 0 ? ret : 0;
  834. }
  835. EXPORT_SYMBOL_GPL(__phy_modify_mmd);
  836. /**
  837. * phy_modify_mmd - Convenience function for modifying a register on MMD
  838. * @phydev: the phy_device struct
  839. * @devad: the MMD containing register to modify
  840. * @regnum: register number to modify
  841. * @mask: bit mask of bits to clear
  842. * @set: new value of bits set in mask to write to @regnum
  843. *
  844. * NOTE: MUST NOT be called from interrupt context,
  845. * because the bus read/write functions may wait for an interrupt
  846. * to conclude the operation.
  847. */
  848. int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  849. u16 mask, u16 set)
  850. {
  851. int ret;
  852. phy_lock_mdio_bus(phydev);
  853. ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
  854. phy_unlock_mdio_bus(phydev);
  855. return ret;
  856. }
  857. EXPORT_SYMBOL_GPL(phy_modify_mmd);
  858. static int __phy_read_page(struct phy_device *phydev)
  859. {
  860. if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
  861. return -EOPNOTSUPP;
  862. return phydev->drv->read_page(phydev);
  863. }
  864. static int __phy_write_page(struct phy_device *phydev, int page)
  865. {
  866. if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
  867. return -EOPNOTSUPP;
  868. return phydev->drv->write_page(phydev, page);
  869. }
  870. /**
  871. * phy_save_page() - take the bus lock and save the current page
  872. * @phydev: a pointer to a &struct phy_device
  873. *
  874. * Take the MDIO bus lock, and return the current page number. On error,
  875. * returns a negative errno. phy_restore_page() must always be called
  876. * after this, irrespective of success or failure of this call.
  877. */
  878. int phy_save_page(struct phy_device *phydev)
  879. {
  880. phy_lock_mdio_bus(phydev);
  881. return __phy_read_page(phydev);
  882. }
  883. EXPORT_SYMBOL_GPL(phy_save_page);
  884. /**
  885. * phy_select_page() - take the bus lock, save the current page, and set a page
  886. * @phydev: a pointer to a &struct phy_device
  887. * @page: desired page
  888. *
  889. * Take the MDIO bus lock to protect against concurrent access, save the
  890. * current PHY page, and set the current page. On error, returns a
  891. * negative errno, otherwise returns the previous page number.
  892. * phy_restore_page() must always be called after this, irrespective
  893. * of success or failure of this call.
  894. */
  895. int phy_select_page(struct phy_device *phydev, int page)
  896. {
  897. int ret, oldpage;
  898. oldpage = ret = phy_save_page(phydev);
  899. if (ret < 0)
  900. return ret;
  901. if (oldpage != page) {
  902. ret = __phy_write_page(phydev, page);
  903. if (ret < 0)
  904. return ret;
  905. }
  906. return oldpage;
  907. }
  908. EXPORT_SYMBOL_GPL(phy_select_page);
  909. /**
  910. * phy_restore_page() - restore the page register and release the bus lock
  911. * @phydev: a pointer to a &struct phy_device
  912. * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
  913. * @ret: operation's return code
  914. *
  915. * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
  916. * This function propagates the earliest error code from the group of
  917. * operations.
  918. *
  919. * Returns:
  920. * @oldpage if it was a negative value, otherwise
  921. * @ret if it was a negative errno value, otherwise
  922. * phy_write_page()'s negative value if it were in error, otherwise
  923. * @ret.
  924. */
  925. int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
  926. {
  927. int r;
  928. if (oldpage >= 0) {
  929. r = __phy_write_page(phydev, oldpage);
  930. /* Propagate the operation return code if the page write
  931. * was successful.
  932. */
  933. if (ret >= 0 && r < 0)
  934. ret = r;
  935. } else {
  936. /* Propagate the phy page selection error code */
  937. ret = oldpage;
  938. }
  939. phy_unlock_mdio_bus(phydev);
  940. return ret;
  941. }
  942. EXPORT_SYMBOL_GPL(phy_restore_page);
  943. /**
  944. * phy_read_paged() - Convenience function for reading a paged register
  945. * @phydev: a pointer to a &struct phy_device
  946. * @page: the page for the phy
  947. * @regnum: register number
  948. *
  949. * Same rules as for phy_read().
  950. */
  951. int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
  952. {
  953. int ret = 0, oldpage;
  954. oldpage = phy_select_page(phydev, page);
  955. if (oldpage >= 0)
  956. ret = __phy_read(phydev, regnum);
  957. return phy_restore_page(phydev, oldpage, ret);
  958. }
  959. EXPORT_SYMBOL(phy_read_paged);
  960. /**
  961. * phy_write_paged() - Convenience function for writing a paged register
  962. * @phydev: a pointer to a &struct phy_device
  963. * @page: the page for the phy
  964. * @regnum: register number
  965. * @val: value to write
  966. *
  967. * Same rules as for phy_write().
  968. */
  969. int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
  970. {
  971. int ret = 0, oldpage;
  972. oldpage = phy_select_page(phydev, page);
  973. if (oldpage >= 0)
  974. ret = __phy_write(phydev, regnum, val);
  975. return phy_restore_page(phydev, oldpage, ret);
  976. }
  977. EXPORT_SYMBOL(phy_write_paged);
  978. /**
  979. * phy_modify_paged_changed() - Function for modifying a paged register
  980. * @phydev: a pointer to a &struct phy_device
  981. * @page: the page for the phy
  982. * @regnum: register number
  983. * @mask: bit mask of bits to clear
  984. * @set: bit mask of bits to set
  985. *
  986. * Returns negative errno, 0 if there was no change, and 1 in case of change
  987. */
  988. int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
  989. u16 mask, u16 set)
  990. {
  991. int ret = 0, oldpage;
  992. oldpage = phy_select_page(phydev, page);
  993. if (oldpage >= 0)
  994. ret = __phy_modify_changed(phydev, regnum, mask, set);
  995. return phy_restore_page(phydev, oldpage, ret);
  996. }
  997. EXPORT_SYMBOL(phy_modify_paged_changed);
  998. /**
  999. * phy_modify_paged() - Convenience function for modifying a paged register
  1000. * @phydev: a pointer to a &struct phy_device
  1001. * @page: the page for the phy
  1002. * @regnum: register number
  1003. * @mask: bit mask of bits to clear
  1004. * @set: bit mask of bits to set
  1005. *
  1006. * Same rules as for phy_read() and phy_write().
  1007. */
  1008. int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
  1009. u16 mask, u16 set)
  1010. {
  1011. int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
  1012. return ret < 0 ? ret : 0;
  1013. }
  1014. EXPORT_SYMBOL(phy_modify_paged);