phy.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Framework for configuring and reading PHY devices
  3. * Based on code in sungem_phy.c and gianfar_phy.c
  4. *
  5. * Author: Andy Fleming
  6. *
  7. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  8. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/unistd.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/netlink.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/mii.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/ethtool_netlink.h>
  25. #include <linux/phy.h>
  26. #include <linux/phy_led_triggers.h>
  27. #include <linux/sfp.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/mdio.h>
  30. #include <linux/io.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/atomic.h>
  33. #include <linux/suspend.h>
  34. #include <net/netlink.h>
  35. #include <net/genetlink.h>
  36. #include <net/sock.h>
  37. #define PHY_STATE_TIME HZ
  38. #define PHY_STATE_STR(_state) \
  39. case PHY_##_state: \
  40. return __stringify(_state); \
  41. static const char *phy_state_to_str(enum phy_state st)
  42. {
  43. switch (st) {
  44. PHY_STATE_STR(DOWN)
  45. PHY_STATE_STR(READY)
  46. PHY_STATE_STR(UP)
  47. PHY_STATE_STR(RUNNING)
  48. PHY_STATE_STR(NOLINK)
  49. PHY_STATE_STR(CABLETEST)
  50. PHY_STATE_STR(HALTED)
  51. PHY_STATE_STR(ERROR)
  52. }
  53. return NULL;
  54. }
  55. static void phy_process_state_change(struct phy_device *phydev,
  56. enum phy_state old_state)
  57. {
  58. if (old_state != phydev->state) {
  59. phydev_dbg(phydev, "PHY state change %s -> %s\n",
  60. phy_state_to_str(old_state),
  61. phy_state_to_str(phydev->state));
  62. if (phydev->drv && phydev->drv->link_change_notify)
  63. phydev->drv->link_change_notify(phydev);
  64. }
  65. }
  66. static void phy_link_up(struct phy_device *phydev)
  67. {
  68. phydev->phy_link_change(phydev, true);
  69. phy_led_trigger_change_speed(phydev);
  70. }
  71. static void phy_link_down(struct phy_device *phydev)
  72. {
  73. phydev->phy_link_change(phydev, false);
  74. phy_led_trigger_change_speed(phydev);
  75. WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1);
  76. }
  77. static const char *phy_pause_str(struct phy_device *phydev)
  78. {
  79. bool local_pause, local_asym_pause;
  80. if (phydev->autoneg == AUTONEG_DISABLE)
  81. goto no_pause;
  82. local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  83. phydev->advertising);
  84. local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  85. phydev->advertising);
  86. if (local_pause && phydev->pause)
  87. return "rx/tx";
  88. if (local_asym_pause && phydev->asym_pause) {
  89. if (local_pause)
  90. return "rx";
  91. if (phydev->pause)
  92. return "tx";
  93. }
  94. no_pause:
  95. return "off";
  96. }
  97. /**
  98. * phy_print_status - Convenience function to print out the current phy status
  99. * @phydev: the phy_device struct
  100. */
  101. void phy_print_status(struct phy_device *phydev)
  102. {
  103. if (phydev->link) {
  104. netdev_info(phydev->attached_dev,
  105. "Link is Up - %s/%s %s- flow control %s\n",
  106. phy_speed_to_str(phydev->speed),
  107. phy_duplex_to_str(phydev->duplex),
  108. phydev->downshifted_rate ? "(downshifted) " : "",
  109. phy_pause_str(phydev));
  110. } else {
  111. netdev_info(phydev->attached_dev, "Link is Down\n");
  112. }
  113. }
  114. EXPORT_SYMBOL(phy_print_status);
  115. /**
  116. * phy_get_rate_matching - determine if rate matching is supported
  117. * @phydev: The phy device to return rate matching for
  118. * @iface: The interface mode to use
  119. *
  120. * This determines the type of rate matching (if any) that @phy supports
  121. * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any
  122. * interface supports rate matching.
  123. *
  124. * Return: The type of rate matching @phy supports for @iface, or
  125. * %RATE_MATCH_NONE.
  126. */
  127. int phy_get_rate_matching(struct phy_device *phydev,
  128. phy_interface_t iface)
  129. {
  130. int ret = RATE_MATCH_NONE;
  131. if (phydev->drv->get_rate_matching) {
  132. mutex_lock(&phydev->lock);
  133. ret = phydev->drv->get_rate_matching(phydev, iface);
  134. mutex_unlock(&phydev->lock);
  135. }
  136. return ret;
  137. }
  138. EXPORT_SYMBOL_GPL(phy_get_rate_matching);
  139. /**
  140. * phy_config_interrupt - configure the PHY device for the requested interrupts
  141. * @phydev: the phy_device struct
  142. * @interrupts: interrupt flags to configure for this @phydev
  143. *
  144. * Returns 0 on success or < 0 on error.
  145. */
  146. static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
  147. {
  148. phydev->interrupts = interrupts ? 1 : 0;
  149. if (phydev->drv->config_intr)
  150. return phydev->drv->config_intr(phydev);
  151. return 0;
  152. }
  153. /**
  154. * phy_restart_aneg - restart auto-negotiation
  155. * @phydev: target phy_device struct
  156. *
  157. * Restart the autonegotiation on @phydev. Returns >= 0 on success or
  158. * negative errno on error.
  159. */
  160. int phy_restart_aneg(struct phy_device *phydev)
  161. {
  162. int ret;
  163. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  164. ret = genphy_c45_restart_aneg(phydev);
  165. else
  166. ret = genphy_restart_aneg(phydev);
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(phy_restart_aneg);
  170. /**
  171. * phy_aneg_done - return auto-negotiation status
  172. * @phydev: target phy_device struct
  173. *
  174. * Description: Return the auto-negotiation status from this @phydev
  175. * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
  176. * is still pending.
  177. */
  178. int phy_aneg_done(struct phy_device *phydev)
  179. {
  180. if (phydev->drv && phydev->drv->aneg_done)
  181. return phydev->drv->aneg_done(phydev);
  182. else if (phydev->is_c45)
  183. return genphy_c45_aneg_done(phydev);
  184. else
  185. return genphy_aneg_done(phydev);
  186. }
  187. EXPORT_SYMBOL(phy_aneg_done);
  188. /**
  189. * phy_find_valid - find a PHY setting that matches the requested parameters
  190. * @speed: desired speed
  191. * @duplex: desired duplex
  192. * @supported: mask of supported link modes
  193. *
  194. * Locate a supported phy setting that is, in priority order:
  195. * - an exact match for the specified speed and duplex mode
  196. * - a match for the specified speed, or slower speed
  197. * - the slowest supported speed
  198. * Returns the matched phy_setting entry, or %NULL if no supported phy
  199. * settings were found.
  200. */
  201. static const struct phy_setting *
  202. phy_find_valid(int speed, int duplex, unsigned long *supported)
  203. {
  204. return phy_lookup_setting(speed, duplex, supported, false);
  205. }
  206. /**
  207. * phy_supported_speeds - return all speeds currently supported by a phy device
  208. * @phy: The phy device to return supported speeds of.
  209. * @speeds: buffer to store supported speeds in.
  210. * @size: size of speeds buffer.
  211. *
  212. * Description: Returns the number of supported speeds, and fills the speeds
  213. * buffer with the supported speeds. If speeds buffer is too small to contain
  214. * all currently supported speeds, will return as many speeds as can fit.
  215. */
  216. unsigned int phy_supported_speeds(struct phy_device *phy,
  217. unsigned int *speeds,
  218. unsigned int size)
  219. {
  220. return phy_speeds(speeds, size, phy->supported);
  221. }
  222. /**
  223. * phy_check_valid - check if there is a valid PHY setting which matches
  224. * speed, duplex, and feature mask
  225. * @speed: speed to match
  226. * @duplex: duplex to match
  227. * @features: A mask of the valid settings
  228. *
  229. * Description: Returns true if there is a valid setting, false otherwise.
  230. */
  231. bool phy_check_valid(int speed, int duplex, unsigned long *features)
  232. {
  233. return !!phy_lookup_setting(speed, duplex, features, true);
  234. }
  235. EXPORT_SYMBOL(phy_check_valid);
  236. /**
  237. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  238. * @phydev: the target phy_device struct
  239. *
  240. * Description: Make sure the PHY is set to supported speeds and
  241. * duplexes. Drop down by one in this order: 1000/FULL,
  242. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  243. */
  244. static void phy_sanitize_settings(struct phy_device *phydev)
  245. {
  246. const struct phy_setting *setting;
  247. setting = phy_find_valid(phydev->speed, phydev->duplex,
  248. phydev->supported);
  249. if (setting) {
  250. phydev->speed = setting->speed;
  251. phydev->duplex = setting->duplex;
  252. } else {
  253. /* We failed to find anything (no supported speeds?) */
  254. phydev->speed = SPEED_UNKNOWN;
  255. phydev->duplex = DUPLEX_UNKNOWN;
  256. }
  257. }
  258. void phy_ethtool_ksettings_get(struct phy_device *phydev,
  259. struct ethtool_link_ksettings *cmd)
  260. {
  261. mutex_lock(&phydev->lock);
  262. linkmode_copy(cmd->link_modes.supported, phydev->supported);
  263. linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
  264. linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
  265. cmd->base.speed = phydev->speed;
  266. cmd->base.duplex = phydev->duplex;
  267. cmd->base.master_slave_cfg = phydev->master_slave_get;
  268. cmd->base.master_slave_state = phydev->master_slave_state;
  269. cmd->base.rate_matching = phydev->rate_matching;
  270. if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
  271. cmd->base.port = PORT_BNC;
  272. else
  273. cmd->base.port = phydev->port;
  274. cmd->base.transceiver = phy_is_internal(phydev) ?
  275. XCVR_INTERNAL : XCVR_EXTERNAL;
  276. cmd->base.phy_address = phydev->mdio.addr;
  277. cmd->base.autoneg = phydev->autoneg;
  278. cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
  279. cmd->base.eth_tp_mdix = phydev->mdix;
  280. mutex_unlock(&phydev->lock);
  281. }
  282. EXPORT_SYMBOL(phy_ethtool_ksettings_get);
  283. /**
  284. * phy_mii_ioctl - generic PHY MII ioctl interface
  285. * @phydev: the phy_device struct
  286. * @ifr: &struct ifreq for socket ioctl's
  287. * @cmd: ioctl cmd to execute
  288. *
  289. * Note that this function is currently incompatible with the
  290. * PHYCONTROL layer. It changes registers without regard to
  291. * current state. Use at own risk.
  292. */
  293. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  294. {
  295. struct mii_ioctl_data *mii_data = if_mii(ifr);
  296. struct kernel_hwtstamp_config kernel_cfg;
  297. struct netlink_ext_ack extack = {};
  298. u16 val = mii_data->val_in;
  299. bool change_autoneg = false;
  300. struct hwtstamp_config cfg;
  301. int prtad, devad;
  302. int ret;
  303. switch (cmd) {
  304. case SIOCGMIIPHY:
  305. mii_data->phy_id = phydev->mdio.addr;
  306. fallthrough;
  307. case SIOCGMIIREG:
  308. if (mdio_phy_id_is_c45(mii_data->phy_id)) {
  309. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  310. devad = mdio_phy_id_devad(mii_data->phy_id);
  311. ret = mdiobus_c45_read(phydev->mdio.bus, prtad, devad,
  312. mii_data->reg_num);
  313. } else {
  314. ret = mdiobus_read(phydev->mdio.bus, mii_data->phy_id,
  315. mii_data->reg_num);
  316. }
  317. if (ret < 0)
  318. return ret;
  319. mii_data->val_out = ret;
  320. return 0;
  321. case SIOCSMIIREG:
  322. if (mdio_phy_id_is_c45(mii_data->phy_id)) {
  323. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  324. devad = mdio_phy_id_devad(mii_data->phy_id);
  325. } else {
  326. prtad = mii_data->phy_id;
  327. devad = mii_data->reg_num;
  328. }
  329. if (prtad == phydev->mdio.addr) {
  330. switch (devad) {
  331. case MII_BMCR:
  332. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
  333. if (phydev->autoneg == AUTONEG_ENABLE)
  334. change_autoneg = true;
  335. phydev->autoneg = AUTONEG_DISABLE;
  336. if (val & BMCR_FULLDPLX)
  337. phydev->duplex = DUPLEX_FULL;
  338. else
  339. phydev->duplex = DUPLEX_HALF;
  340. if (val & BMCR_SPEED1000)
  341. phydev->speed = SPEED_1000;
  342. else if (val & BMCR_SPEED100)
  343. phydev->speed = SPEED_100;
  344. else phydev->speed = SPEED_10;
  345. } else {
  346. if (phydev->autoneg == AUTONEG_DISABLE)
  347. change_autoneg = true;
  348. phydev->autoneg = AUTONEG_ENABLE;
  349. }
  350. break;
  351. case MII_ADVERTISE:
  352. mii_adv_mod_linkmode_adv_t(phydev->advertising,
  353. val);
  354. change_autoneg = true;
  355. break;
  356. case MII_CTRL1000:
  357. mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
  358. val);
  359. change_autoneg = true;
  360. break;
  361. default:
  362. /* do nothing */
  363. break;
  364. }
  365. }
  366. if (mdio_phy_id_is_c45(mii_data->phy_id))
  367. mdiobus_c45_write(phydev->mdio.bus, prtad, devad,
  368. mii_data->reg_num, val);
  369. else
  370. mdiobus_write(phydev->mdio.bus, prtad, devad, val);
  371. if (prtad == phydev->mdio.addr &&
  372. devad == MII_BMCR &&
  373. val & BMCR_RESET)
  374. return phy_init_hw(phydev);
  375. if (change_autoneg)
  376. return phy_start_aneg(phydev);
  377. return 0;
  378. case SIOCSHWTSTAMP:
  379. if (phydev->mii_ts && phydev->mii_ts->hwtstamp) {
  380. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  381. return -EFAULT;
  382. hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
  383. ret = phydev->mii_ts->hwtstamp(phydev->mii_ts, &kernel_cfg, &extack);
  384. if (ret)
  385. return ret;
  386. hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
  387. if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
  388. return -EFAULT;
  389. return 0;
  390. }
  391. fallthrough;
  392. default:
  393. return -EOPNOTSUPP;
  394. }
  395. }
  396. EXPORT_SYMBOL(phy_mii_ioctl);
  397. /**
  398. * phy_do_ioctl - generic ndo_eth_ioctl implementation
  399. * @dev: the net_device struct
  400. * @ifr: &struct ifreq for socket ioctl's
  401. * @cmd: ioctl cmd to execute
  402. */
  403. int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  404. {
  405. if (!dev->phydev)
  406. return -ENODEV;
  407. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  408. }
  409. EXPORT_SYMBOL(phy_do_ioctl);
  410. /**
  411. * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
  412. *
  413. * @dev: the net_device struct
  414. * @ifr: &struct ifreq for socket ioctl's
  415. * @cmd: ioctl cmd to execute
  416. *
  417. * Same as phy_do_ioctl, but ensures that net_device is running before
  418. * handling the ioctl.
  419. */
  420. int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
  421. {
  422. if (!netif_running(dev))
  423. return -ENODEV;
  424. return phy_do_ioctl(dev, ifr, cmd);
  425. }
  426. EXPORT_SYMBOL(phy_do_ioctl_running);
  427. /**
  428. * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY
  429. *
  430. * @phydev: the PHY device structure
  431. * @config: structure holding the timestamping configuration
  432. *
  433. * Query the PHY device for its current hardware timestamping configuration.
  434. */
  435. int __phy_hwtstamp_get(struct phy_device *phydev,
  436. struct kernel_hwtstamp_config *config)
  437. {
  438. if (!phydev)
  439. return -ENODEV;
  440. return -EOPNOTSUPP;
  441. }
  442. /**
  443. * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration
  444. *
  445. * @phydev: the PHY device structure
  446. * @config: structure holding the timestamping configuration
  447. * @extack: netlink extended ack structure, for error reporting
  448. */
  449. int __phy_hwtstamp_set(struct phy_device *phydev,
  450. struct kernel_hwtstamp_config *config,
  451. struct netlink_ext_ack *extack)
  452. {
  453. if (!phydev)
  454. return -ENODEV;
  455. if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
  456. return phydev->mii_ts->hwtstamp(phydev->mii_ts, config, extack);
  457. return -EOPNOTSUPP;
  458. }
  459. /**
  460. * phy_queue_state_machine - Trigger the state machine to run soon
  461. *
  462. * @phydev: the phy_device struct
  463. * @jiffies: Run the state machine after these jiffies
  464. */
  465. void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
  466. {
  467. mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  468. jiffies);
  469. }
  470. EXPORT_SYMBOL(phy_queue_state_machine);
  471. /**
  472. * phy_trigger_machine - Trigger the state machine to run now
  473. *
  474. * @phydev: the phy_device struct
  475. */
  476. void phy_trigger_machine(struct phy_device *phydev)
  477. {
  478. phy_queue_state_machine(phydev, 0);
  479. }
  480. EXPORT_SYMBOL(phy_trigger_machine);
  481. static void phy_abort_cable_test(struct phy_device *phydev)
  482. {
  483. int err;
  484. ethnl_cable_test_finished(phydev);
  485. err = phy_init_hw(phydev);
  486. if (err)
  487. phydev_err(phydev, "Error while aborting cable test");
  488. }
  489. /**
  490. * phy_ethtool_get_strings - Get the statistic counter names
  491. *
  492. * @phydev: the phy_device struct
  493. * @data: Where to put the strings
  494. */
  495. int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
  496. {
  497. if (!phydev->drv)
  498. return -EIO;
  499. mutex_lock(&phydev->lock);
  500. phydev->drv->get_strings(phydev, data);
  501. mutex_unlock(&phydev->lock);
  502. return 0;
  503. }
  504. EXPORT_SYMBOL(phy_ethtool_get_strings);
  505. /**
  506. * phy_ethtool_get_sset_count - Get the number of statistic counters
  507. *
  508. * @phydev: the phy_device struct
  509. */
  510. int phy_ethtool_get_sset_count(struct phy_device *phydev)
  511. {
  512. int ret;
  513. if (!phydev->drv)
  514. return -EIO;
  515. if (phydev->drv->get_sset_count &&
  516. phydev->drv->get_strings &&
  517. phydev->drv->get_stats) {
  518. mutex_lock(&phydev->lock);
  519. ret = phydev->drv->get_sset_count(phydev);
  520. mutex_unlock(&phydev->lock);
  521. return ret;
  522. }
  523. return -EOPNOTSUPP;
  524. }
  525. EXPORT_SYMBOL(phy_ethtool_get_sset_count);
  526. /**
  527. * phy_ethtool_get_stats - Get the statistic counters
  528. *
  529. * @phydev: the phy_device struct
  530. * @stats: What counters to get
  531. * @data: Where to store the counters
  532. */
  533. int phy_ethtool_get_stats(struct phy_device *phydev,
  534. struct ethtool_stats *stats, u64 *data)
  535. {
  536. if (!phydev->drv)
  537. return -EIO;
  538. mutex_lock(&phydev->lock);
  539. phydev->drv->get_stats(phydev, stats, data);
  540. mutex_unlock(&phydev->lock);
  541. return 0;
  542. }
  543. EXPORT_SYMBOL(phy_ethtool_get_stats);
  544. /**
  545. * __phy_ethtool_get_phy_stats - Retrieve standardized PHY statistics
  546. * @phydev: Pointer to the PHY device
  547. * @phy_stats: Pointer to ethtool_eth_phy_stats structure
  548. * @phydev_stats: Pointer to ethtool_phy_stats structure
  549. *
  550. * Fetches PHY statistics using a kernel-defined interface for consistent
  551. * diagnostics. Unlike phy_ethtool_get_stats(), which allows custom stats,
  552. * this function enforces a standardized format for better interoperability.
  553. */
  554. void __phy_ethtool_get_phy_stats(struct phy_device *phydev,
  555. struct ethtool_eth_phy_stats *phy_stats,
  556. struct ethtool_phy_stats *phydev_stats)
  557. {
  558. if (!phydev->drv || !phydev->drv->get_phy_stats)
  559. return;
  560. mutex_lock(&phydev->lock);
  561. phydev->drv->get_phy_stats(phydev, phy_stats, phydev_stats);
  562. mutex_unlock(&phydev->lock);
  563. }
  564. /**
  565. * __phy_ethtool_get_link_ext_stats - Retrieve extended link statistics for a PHY
  566. * @phydev: Pointer to the PHY device
  567. * @link_stats: Pointer to the structure to store extended link statistics
  568. *
  569. * Populates the ethtool_link_ext_stats structure with link down event counts
  570. * and additional driver-specific link statistics, if available.
  571. */
  572. void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev,
  573. struct ethtool_link_ext_stats *link_stats)
  574. {
  575. link_stats->link_down_events = READ_ONCE(phydev->link_down_events);
  576. if (!phydev->drv || !phydev->drv->get_link_stats)
  577. return;
  578. mutex_lock(&phydev->lock);
  579. phydev->drv->get_link_stats(phydev, link_stats);
  580. mutex_unlock(&phydev->lock);
  581. }
  582. /**
  583. * phy_ethtool_get_plca_cfg - Get PLCA RS configuration
  584. * @phydev: the phy_device struct
  585. * @plca_cfg: where to store the retrieved configuration
  586. *
  587. * Retrieve the PLCA configuration from the PHY. Return 0 on success or a
  588. * negative value if an error occurred.
  589. */
  590. int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
  591. struct phy_plca_cfg *plca_cfg)
  592. {
  593. int ret;
  594. if (!phydev->drv) {
  595. ret = -EIO;
  596. goto out;
  597. }
  598. if (!phydev->drv->get_plca_cfg) {
  599. ret = -EOPNOTSUPP;
  600. goto out;
  601. }
  602. mutex_lock(&phydev->lock);
  603. ret = phydev->drv->get_plca_cfg(phydev, plca_cfg);
  604. mutex_unlock(&phydev->lock);
  605. out:
  606. return ret;
  607. }
  608. /**
  609. * plca_check_valid - Check PLCA configuration before enabling
  610. * @phydev: the phy_device struct
  611. * @plca_cfg: current PLCA configuration
  612. * @extack: extack for reporting useful error messages
  613. *
  614. * Checks whether the PLCA and PHY configuration are consistent and it is safe
  615. * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY
  616. * configuration is not consistent.
  617. */
  618. static int plca_check_valid(struct phy_device *phydev,
  619. const struct phy_plca_cfg *plca_cfg,
  620. struct netlink_ext_ack *extack)
  621. {
  622. int ret = 0;
  623. if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
  624. phydev->advertising)) {
  625. ret = -EOPNOTSUPP;
  626. NL_SET_ERR_MSG(extack,
  627. "Point to Multi-Point mode is not enabled");
  628. } else if (plca_cfg->node_id >= 255) {
  629. NL_SET_ERR_MSG(extack, "PLCA node ID is not set");
  630. ret = -EINVAL;
  631. }
  632. return ret;
  633. }
  634. /**
  635. * phy_ethtool_set_plca_cfg - Set PLCA RS configuration
  636. * @phydev: the phy_device struct
  637. * @plca_cfg: new PLCA configuration to apply
  638. * @extack: extack for reporting useful error messages
  639. *
  640. * Sets the PLCA configuration in the PHY. Return 0 on success or a
  641. * negative value if an error occurred.
  642. */
  643. int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
  644. const struct phy_plca_cfg *plca_cfg,
  645. struct netlink_ext_ack *extack)
  646. {
  647. struct phy_plca_cfg *curr_plca_cfg;
  648. int ret;
  649. if (!phydev->drv) {
  650. ret = -EIO;
  651. goto out;
  652. }
  653. if (!phydev->drv->set_plca_cfg ||
  654. !phydev->drv->get_plca_cfg) {
  655. ret = -EOPNOTSUPP;
  656. goto out;
  657. }
  658. curr_plca_cfg = kmalloc(sizeof(*curr_plca_cfg), GFP_KERNEL);
  659. if (!curr_plca_cfg) {
  660. ret = -ENOMEM;
  661. goto out;
  662. }
  663. mutex_lock(&phydev->lock);
  664. ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg);
  665. if (ret)
  666. goto out_drv;
  667. if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) {
  668. NL_SET_ERR_MSG(extack,
  669. "PHY does not support changing the PLCA 'enable' attribute");
  670. ret = -EINVAL;
  671. goto out_drv;
  672. }
  673. if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) {
  674. NL_SET_ERR_MSG(extack,
  675. "PHY does not support changing the PLCA 'local node ID' attribute");
  676. ret = -EINVAL;
  677. goto out_drv;
  678. }
  679. if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) {
  680. NL_SET_ERR_MSG(extack,
  681. "PHY does not support changing the PLCA 'node count' attribute");
  682. ret = -EINVAL;
  683. goto out_drv;
  684. }
  685. if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) {
  686. NL_SET_ERR_MSG(extack,
  687. "PHY does not support changing the PLCA 'TO timer' attribute");
  688. ret = -EINVAL;
  689. goto out_drv;
  690. }
  691. if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) {
  692. NL_SET_ERR_MSG(extack,
  693. "PHY does not support changing the PLCA 'burst count' attribute");
  694. ret = -EINVAL;
  695. goto out_drv;
  696. }
  697. if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) {
  698. NL_SET_ERR_MSG(extack,
  699. "PHY does not support changing the PLCA 'burst timer' attribute");
  700. ret = -EINVAL;
  701. goto out_drv;
  702. }
  703. // if enabling PLCA, perform a few sanity checks
  704. if (plca_cfg->enabled > 0) {
  705. // allow setting node_id concurrently with enabled
  706. if (plca_cfg->node_id >= 0)
  707. curr_plca_cfg->node_id = plca_cfg->node_id;
  708. ret = plca_check_valid(phydev, curr_plca_cfg, extack);
  709. if (ret)
  710. goto out_drv;
  711. }
  712. ret = phydev->drv->set_plca_cfg(phydev, plca_cfg);
  713. out_drv:
  714. kfree(curr_plca_cfg);
  715. mutex_unlock(&phydev->lock);
  716. out:
  717. return ret;
  718. }
  719. /**
  720. * phy_ethtool_get_plca_status - Get PLCA RS status information
  721. * @phydev: the phy_device struct
  722. * @plca_st: where to store the retrieved status information
  723. *
  724. * Retrieve the PLCA status information from the PHY. Return 0 on success or a
  725. * negative value if an error occurred.
  726. */
  727. int phy_ethtool_get_plca_status(struct phy_device *phydev,
  728. struct phy_plca_status *plca_st)
  729. {
  730. int ret;
  731. if (!phydev->drv) {
  732. ret = -EIO;
  733. goto out;
  734. }
  735. if (!phydev->drv->get_plca_status) {
  736. ret = -EOPNOTSUPP;
  737. goto out;
  738. }
  739. mutex_lock(&phydev->lock);
  740. ret = phydev->drv->get_plca_status(phydev, plca_st);
  741. mutex_unlock(&phydev->lock);
  742. out:
  743. return ret;
  744. }
  745. /**
  746. * phy_start_cable_test - Start a cable test
  747. *
  748. * @phydev: the phy_device struct
  749. * @extack: extack for reporting useful error messages
  750. */
  751. int phy_start_cable_test(struct phy_device *phydev,
  752. struct netlink_ext_ack *extack)
  753. {
  754. struct net_device *dev = phydev->attached_dev;
  755. int err = -ENOMEM;
  756. if (!(phydev->drv &&
  757. phydev->drv->cable_test_start &&
  758. phydev->drv->cable_test_get_status)) {
  759. NL_SET_ERR_MSG(extack,
  760. "PHY driver does not support cable testing");
  761. return -EOPNOTSUPP;
  762. }
  763. mutex_lock(&phydev->lock);
  764. if (phydev->state == PHY_CABLETEST) {
  765. NL_SET_ERR_MSG(extack,
  766. "PHY already performing a test");
  767. err = -EBUSY;
  768. goto out;
  769. }
  770. if (phydev->state < PHY_UP ||
  771. phydev->state > PHY_CABLETEST) {
  772. NL_SET_ERR_MSG(extack,
  773. "PHY not configured. Try setting interface up");
  774. err = -EBUSY;
  775. goto out;
  776. }
  777. err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
  778. if (err)
  779. goto out;
  780. /* Mark the carrier down until the test is complete */
  781. phy_link_down(phydev);
  782. netif_testing_on(dev);
  783. err = phydev->drv->cable_test_start(phydev);
  784. if (err) {
  785. netif_testing_off(dev);
  786. phy_link_up(phydev);
  787. goto out_free;
  788. }
  789. phydev->state = PHY_CABLETEST;
  790. if (phy_polling_mode(phydev))
  791. phy_trigger_machine(phydev);
  792. mutex_unlock(&phydev->lock);
  793. return 0;
  794. out_free:
  795. ethnl_cable_test_free(phydev);
  796. out:
  797. mutex_unlock(&phydev->lock);
  798. return err;
  799. }
  800. EXPORT_SYMBOL(phy_start_cable_test);
  801. /**
  802. * phy_start_cable_test_tdr - Start a raw TDR cable test
  803. *
  804. * @phydev: the phy_device struct
  805. * @extack: extack for reporting useful error messages
  806. * @config: Configuration of the test to run
  807. */
  808. int phy_start_cable_test_tdr(struct phy_device *phydev,
  809. struct netlink_ext_ack *extack,
  810. const struct phy_tdr_config *config)
  811. {
  812. struct net_device *dev = phydev->attached_dev;
  813. int err = -ENOMEM;
  814. if (!(phydev->drv &&
  815. phydev->drv->cable_test_tdr_start &&
  816. phydev->drv->cable_test_get_status)) {
  817. NL_SET_ERR_MSG(extack,
  818. "PHY driver does not support cable test TDR");
  819. return -EOPNOTSUPP;
  820. }
  821. mutex_lock(&phydev->lock);
  822. if (phydev->state == PHY_CABLETEST) {
  823. NL_SET_ERR_MSG(extack,
  824. "PHY already performing a test");
  825. err = -EBUSY;
  826. goto out;
  827. }
  828. if (phydev->state < PHY_UP ||
  829. phydev->state > PHY_CABLETEST) {
  830. NL_SET_ERR_MSG(extack,
  831. "PHY not configured. Try setting interface up");
  832. err = -EBUSY;
  833. goto out;
  834. }
  835. err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
  836. if (err)
  837. goto out;
  838. /* Mark the carrier down until the test is complete */
  839. phy_link_down(phydev);
  840. netif_testing_on(dev);
  841. err = phydev->drv->cable_test_tdr_start(phydev, config);
  842. if (err) {
  843. netif_testing_off(dev);
  844. phy_link_up(phydev);
  845. goto out_free;
  846. }
  847. phydev->state = PHY_CABLETEST;
  848. if (phy_polling_mode(phydev))
  849. phy_trigger_machine(phydev);
  850. mutex_unlock(&phydev->lock);
  851. return 0;
  852. out_free:
  853. ethnl_cable_test_free(phydev);
  854. out:
  855. mutex_unlock(&phydev->lock);
  856. return err;
  857. }
  858. EXPORT_SYMBOL(phy_start_cable_test_tdr);
  859. int phy_config_aneg(struct phy_device *phydev)
  860. {
  861. if (phydev->drv->config_aneg)
  862. return phydev->drv->config_aneg(phydev);
  863. /* Clause 45 PHYs that don't implement Clause 22 registers are not
  864. * allowed to call genphy_config_aneg()
  865. */
  866. if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
  867. return genphy_c45_config_aneg(phydev);
  868. return genphy_config_aneg(phydev);
  869. }
  870. EXPORT_SYMBOL(phy_config_aneg);
  871. /**
  872. * phy_check_link_status - check link status and set state accordingly
  873. * @phydev: the phy_device struct
  874. *
  875. * Description: Check for link and whether autoneg was triggered / is running
  876. * and set state accordingly
  877. */
  878. static int phy_check_link_status(struct phy_device *phydev)
  879. {
  880. int err;
  881. lockdep_assert_held(&phydev->lock);
  882. /* Keep previous state if loopback is enabled because some PHYs
  883. * report that Link is Down when loopback is enabled.
  884. */
  885. if (phydev->loopback_enabled)
  886. return 0;
  887. err = phy_read_status(phydev);
  888. if (err)
  889. return err;
  890. if (phydev->link && phydev->state != PHY_RUNNING) {
  891. phy_check_downshift(phydev);
  892. phydev->state = PHY_RUNNING;
  893. err = genphy_c45_eee_is_active(phydev,
  894. NULL, NULL, NULL);
  895. if (err <= 0)
  896. phydev->enable_tx_lpi = false;
  897. else
  898. phydev->enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled;
  899. phy_link_up(phydev);
  900. } else if (!phydev->link && phydev->state != PHY_NOLINK) {
  901. phydev->state = PHY_NOLINK;
  902. phydev->enable_tx_lpi = false;
  903. phy_link_down(phydev);
  904. }
  905. return 0;
  906. }
  907. /**
  908. * _phy_start_aneg - start auto-negotiation for this PHY device
  909. * @phydev: the phy_device struct
  910. *
  911. * Description: Sanitizes the settings (if we're not autonegotiating
  912. * them), and then calls the driver's config_aneg function.
  913. * If the PHYCONTROL Layer is operating, we change the state to
  914. * reflect the beginning of Auto-negotiation or forcing.
  915. */
  916. int _phy_start_aneg(struct phy_device *phydev)
  917. {
  918. int err;
  919. lockdep_assert_held(&phydev->lock);
  920. if (!phydev->drv)
  921. return -EIO;
  922. if (AUTONEG_DISABLE == phydev->autoneg)
  923. phy_sanitize_settings(phydev);
  924. err = phy_config_aneg(phydev);
  925. if (err < 0)
  926. return err;
  927. if (phy_is_started(phydev))
  928. err = phy_check_link_status(phydev);
  929. return err;
  930. }
  931. EXPORT_SYMBOL(_phy_start_aneg);
  932. /**
  933. * phy_start_aneg - start auto-negotiation for this PHY device
  934. * @phydev: the phy_device struct
  935. *
  936. * Description: Sanitizes the settings (if we're not autonegotiating
  937. * them), and then calls the driver's config_aneg function.
  938. * If the PHYCONTROL Layer is operating, we change the state to
  939. * reflect the beginning of Auto-negotiation or forcing.
  940. */
  941. int phy_start_aneg(struct phy_device *phydev)
  942. {
  943. int err;
  944. mutex_lock(&phydev->lock);
  945. err = _phy_start_aneg(phydev);
  946. mutex_unlock(&phydev->lock);
  947. return err;
  948. }
  949. EXPORT_SYMBOL(phy_start_aneg);
  950. static int phy_poll_aneg_done(struct phy_device *phydev)
  951. {
  952. unsigned int retries = 100;
  953. int ret;
  954. do {
  955. msleep(100);
  956. ret = phy_aneg_done(phydev);
  957. } while (!ret && --retries);
  958. if (!ret)
  959. return -ETIMEDOUT;
  960. return ret < 0 ? ret : 0;
  961. }
  962. int phy_ethtool_ksettings_set(struct phy_device *phydev,
  963. const struct ethtool_link_ksettings *cmd)
  964. {
  965. __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
  966. u8 autoneg = cmd->base.autoneg;
  967. u8 duplex = cmd->base.duplex;
  968. u32 speed = cmd->base.speed;
  969. if (cmd->base.phy_address != phydev->mdio.addr)
  970. return -EINVAL;
  971. linkmode_copy(advertising, cmd->link_modes.advertising);
  972. /* We make sure that we don't pass unsupported values in to the PHY */
  973. linkmode_and(advertising, advertising, phydev->supported);
  974. /* Verify the settings we care about. */
  975. if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
  976. return -EINVAL;
  977. if (autoneg == AUTONEG_ENABLE &&
  978. (linkmode_empty(advertising) ||
  979. !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  980. phydev->supported)))
  981. return -EINVAL;
  982. if (autoneg == AUTONEG_DISABLE &&
  983. ((speed != SPEED_1000 &&
  984. speed != SPEED_100 &&
  985. speed != SPEED_10) ||
  986. (duplex != DUPLEX_HALF &&
  987. duplex != DUPLEX_FULL)))
  988. return -EINVAL;
  989. mutex_lock(&phydev->lock);
  990. phydev->autoneg = autoneg;
  991. if (autoneg == AUTONEG_DISABLE) {
  992. phydev->speed = speed;
  993. phydev->duplex = duplex;
  994. }
  995. linkmode_copy(phydev->advertising, advertising);
  996. linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  997. phydev->advertising, autoneg == AUTONEG_ENABLE);
  998. phydev->master_slave_set = cmd->base.master_slave_cfg;
  999. phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
  1000. /* Restart the PHY */
  1001. if (phy_is_started(phydev)) {
  1002. phydev->state = PHY_UP;
  1003. phy_trigger_machine(phydev);
  1004. } else {
  1005. _phy_start_aneg(phydev);
  1006. }
  1007. mutex_unlock(&phydev->lock);
  1008. return 0;
  1009. }
  1010. EXPORT_SYMBOL(phy_ethtool_ksettings_set);
  1011. /**
  1012. * phy_speed_down - set speed to lowest speed supported by both link partners
  1013. * @phydev: the phy_device struct
  1014. * @sync: perform action synchronously
  1015. *
  1016. * Description: Typically used to save energy when waiting for a WoL packet
  1017. *
  1018. * WARNING: Setting sync to false may cause the system being unable to suspend
  1019. * in case the PHY generates an interrupt when finishing the autonegotiation.
  1020. * This interrupt may wake up the system immediately after suspend.
  1021. * Therefore use sync = false only if you're sure it's safe with the respective
  1022. * network chip.
  1023. */
  1024. int phy_speed_down(struct phy_device *phydev, bool sync)
  1025. {
  1026. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
  1027. int ret = 0;
  1028. mutex_lock(&phydev->lock);
  1029. if (phydev->autoneg != AUTONEG_ENABLE)
  1030. goto out;
  1031. linkmode_copy(adv_tmp, phydev->advertising);
  1032. ret = phy_speed_down_core(phydev);
  1033. if (ret)
  1034. goto out;
  1035. linkmode_copy(phydev->adv_old, adv_tmp);
  1036. if (linkmode_equal(phydev->advertising, adv_tmp)) {
  1037. ret = 0;
  1038. goto out;
  1039. }
  1040. ret = phy_config_aneg(phydev);
  1041. if (ret)
  1042. goto out;
  1043. ret = sync ? phy_poll_aneg_done(phydev) : 0;
  1044. out:
  1045. mutex_unlock(&phydev->lock);
  1046. return ret;
  1047. }
  1048. EXPORT_SYMBOL_GPL(phy_speed_down);
  1049. /**
  1050. * phy_speed_up - (re)set advertised speeds to all supported speeds
  1051. * @phydev: the phy_device struct
  1052. *
  1053. * Description: Used to revert the effect of phy_speed_down
  1054. */
  1055. int phy_speed_up(struct phy_device *phydev)
  1056. {
  1057. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
  1058. int ret = 0;
  1059. mutex_lock(&phydev->lock);
  1060. if (phydev->autoneg != AUTONEG_ENABLE)
  1061. goto out;
  1062. if (linkmode_empty(phydev->adv_old))
  1063. goto out;
  1064. linkmode_copy(adv_tmp, phydev->advertising);
  1065. linkmode_copy(phydev->advertising, phydev->adv_old);
  1066. linkmode_zero(phydev->adv_old);
  1067. if (linkmode_equal(phydev->advertising, adv_tmp))
  1068. goto out;
  1069. ret = phy_config_aneg(phydev);
  1070. out:
  1071. mutex_unlock(&phydev->lock);
  1072. return ret;
  1073. }
  1074. EXPORT_SYMBOL_GPL(phy_speed_up);
  1075. /**
  1076. * phy_start_machine - start PHY state machine tracking
  1077. * @phydev: the phy_device struct
  1078. *
  1079. * Description: The PHY infrastructure can run a state machine
  1080. * which tracks whether the PHY is starting up, negotiating,
  1081. * etc. This function starts the delayed workqueue which tracks
  1082. * the state of the PHY. If you want to maintain your own state machine,
  1083. * do not call this function.
  1084. */
  1085. void phy_start_machine(struct phy_device *phydev)
  1086. {
  1087. phy_trigger_machine(phydev);
  1088. }
  1089. EXPORT_SYMBOL_GPL(phy_start_machine);
  1090. /**
  1091. * phy_stop_machine - stop the PHY state machine tracking
  1092. * @phydev: target phy_device struct
  1093. *
  1094. * Description: Stops the state machine delayed workqueue, sets the
  1095. * state to UP (unless it wasn't up yet). This function must be
  1096. * called BEFORE phy_detach.
  1097. */
  1098. void phy_stop_machine(struct phy_device *phydev)
  1099. {
  1100. cancel_delayed_work_sync(&phydev->state_queue);
  1101. mutex_lock(&phydev->lock);
  1102. if (phy_is_started(phydev))
  1103. phydev->state = PHY_UP;
  1104. mutex_unlock(&phydev->lock);
  1105. }
  1106. static void phy_process_error(struct phy_device *phydev)
  1107. {
  1108. /* phydev->lock must be held for the state change to be safe */
  1109. if (!mutex_is_locked(&phydev->lock))
  1110. phydev_err(phydev, "PHY-device data unsafe context\n");
  1111. phydev->state = PHY_ERROR;
  1112. phy_trigger_machine(phydev);
  1113. }
  1114. static void phy_error_precise(struct phy_device *phydev,
  1115. const void *func, int err)
  1116. {
  1117. WARN(1, "%pS: returned: %d\n", func, err);
  1118. phy_process_error(phydev);
  1119. }
  1120. /**
  1121. * phy_error - enter ERROR state for this PHY device
  1122. * @phydev: target phy_device struct
  1123. *
  1124. * Moves the PHY to the ERROR state in response to a read
  1125. * or write error, and tells the controller the link is down.
  1126. * Must be called with phydev->lock held.
  1127. */
  1128. void phy_error(struct phy_device *phydev)
  1129. {
  1130. WARN_ON(1);
  1131. phy_process_error(phydev);
  1132. }
  1133. EXPORT_SYMBOL(phy_error);
  1134. /**
  1135. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  1136. * @phydev: target phy_device struct
  1137. */
  1138. int phy_disable_interrupts(struct phy_device *phydev)
  1139. {
  1140. /* Disable PHY interrupts */
  1141. return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  1142. }
  1143. /**
  1144. * phy_interrupt - PHY interrupt handler
  1145. * @irq: interrupt line
  1146. * @phy_dat: phy_device pointer
  1147. *
  1148. * Description: Handle PHY interrupt
  1149. */
  1150. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  1151. {
  1152. struct phy_device *phydev = phy_dat;
  1153. irqreturn_t ret;
  1154. /* Wakeup interrupts may occur during a system sleep transition.
  1155. * Postpone handling until the PHY has resumed.
  1156. */
  1157. if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
  1158. struct net_device *netdev = phydev->attached_dev;
  1159. if (netdev) {
  1160. struct device *parent = netdev->dev.parent;
  1161. if (netdev->ethtool->wol_enabled)
  1162. pm_system_wakeup();
  1163. else if (device_may_wakeup(&netdev->dev))
  1164. pm_wakeup_dev_event(&netdev->dev, 0, true);
  1165. else if (parent && device_may_wakeup(parent))
  1166. pm_wakeup_dev_event(parent, 0, true);
  1167. }
  1168. phydev->irq_rerun = 1;
  1169. disable_irq_nosync(irq);
  1170. return IRQ_HANDLED;
  1171. }
  1172. mutex_lock(&phydev->lock);
  1173. ret = phydev->drv->handle_interrupt(phydev);
  1174. mutex_unlock(&phydev->lock);
  1175. return ret;
  1176. }
  1177. /**
  1178. * phy_enable_interrupts - Enable the interrupts from the PHY side
  1179. * @phydev: target phy_device struct
  1180. */
  1181. static int phy_enable_interrupts(struct phy_device *phydev)
  1182. {
  1183. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  1184. }
  1185. /**
  1186. * phy_request_interrupt - request and enable interrupt for a PHY device
  1187. * @phydev: target phy_device struct
  1188. *
  1189. * Description: Request and enable the interrupt for the given PHY.
  1190. * If this fails, then we set irq to PHY_POLL.
  1191. * This should only be called with a valid IRQ number.
  1192. */
  1193. void phy_request_interrupt(struct phy_device *phydev)
  1194. {
  1195. int err;
  1196. err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
  1197. IRQF_ONESHOT | IRQF_SHARED,
  1198. phydev_name(phydev), phydev);
  1199. if (err) {
  1200. phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
  1201. err, phydev->irq);
  1202. phydev->irq = PHY_POLL;
  1203. } else {
  1204. if (phy_enable_interrupts(phydev)) {
  1205. phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
  1206. phy_free_interrupt(phydev);
  1207. phydev->irq = PHY_POLL;
  1208. }
  1209. }
  1210. }
  1211. EXPORT_SYMBOL(phy_request_interrupt);
  1212. /**
  1213. * phy_free_interrupt - disable and free interrupt for a PHY device
  1214. * @phydev: target phy_device struct
  1215. *
  1216. * Description: Disable and free the interrupt for the given PHY.
  1217. * This should only be called with a valid IRQ number.
  1218. */
  1219. void phy_free_interrupt(struct phy_device *phydev)
  1220. {
  1221. phy_disable_interrupts(phydev);
  1222. free_irq(phydev->irq, phydev);
  1223. }
  1224. EXPORT_SYMBOL(phy_free_interrupt);
  1225. enum phy_state_work {
  1226. PHY_STATE_WORK_NONE,
  1227. PHY_STATE_WORK_ANEG,
  1228. PHY_STATE_WORK_SUSPEND,
  1229. };
  1230. static enum phy_state_work _phy_state_machine(struct phy_device *phydev)
  1231. {
  1232. enum phy_state_work state_work = PHY_STATE_WORK_NONE;
  1233. struct net_device *dev = phydev->attached_dev;
  1234. enum phy_state old_state = phydev->state;
  1235. const void *func = NULL;
  1236. bool finished = false;
  1237. int err = 0;
  1238. switch (phydev->state) {
  1239. case PHY_DOWN:
  1240. case PHY_READY:
  1241. break;
  1242. case PHY_UP:
  1243. state_work = PHY_STATE_WORK_ANEG;
  1244. break;
  1245. case PHY_NOLINK:
  1246. case PHY_RUNNING:
  1247. err = phy_check_link_status(phydev);
  1248. func = &phy_check_link_status;
  1249. break;
  1250. case PHY_CABLETEST:
  1251. err = phydev->drv->cable_test_get_status(phydev, &finished);
  1252. if (err) {
  1253. phy_abort_cable_test(phydev);
  1254. netif_testing_off(dev);
  1255. state_work = PHY_STATE_WORK_ANEG;
  1256. phydev->state = PHY_UP;
  1257. break;
  1258. }
  1259. if (finished) {
  1260. ethnl_cable_test_finished(phydev);
  1261. netif_testing_off(dev);
  1262. state_work = PHY_STATE_WORK_ANEG;
  1263. phydev->state = PHY_UP;
  1264. }
  1265. break;
  1266. case PHY_HALTED:
  1267. case PHY_ERROR:
  1268. if (phydev->link) {
  1269. phydev->link = 0;
  1270. phy_link_down(phydev);
  1271. }
  1272. state_work = PHY_STATE_WORK_SUSPEND;
  1273. break;
  1274. }
  1275. if (state_work == PHY_STATE_WORK_ANEG) {
  1276. err = _phy_start_aneg(phydev);
  1277. func = &_phy_start_aneg;
  1278. }
  1279. if (err == -ENODEV)
  1280. return state_work;
  1281. if (err < 0)
  1282. phy_error_precise(phydev, func, err);
  1283. phy_process_state_change(phydev, old_state);
  1284. /* Only re-schedule a PHY state machine change if we are polling the
  1285. * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
  1286. * between states from phy_mac_interrupt().
  1287. *
  1288. * In state PHY_HALTED the PHY gets suspended, so rescheduling the
  1289. * state machine would be pointless and possibly error prone when
  1290. * called from phy_disconnect() synchronously.
  1291. */
  1292. if (phy_polling_mode(phydev) && phy_is_started(phydev))
  1293. phy_queue_state_machine(phydev, PHY_STATE_TIME);
  1294. return state_work;
  1295. }
  1296. /* unlocked part of the PHY state machine */
  1297. static void _phy_state_machine_post_work(struct phy_device *phydev,
  1298. enum phy_state_work state_work)
  1299. {
  1300. if (state_work == PHY_STATE_WORK_SUSPEND)
  1301. phy_suspend(phydev);
  1302. }
  1303. /**
  1304. * phy_state_machine - Handle the state machine
  1305. * @work: work_struct that describes the work to be done
  1306. */
  1307. void phy_state_machine(struct work_struct *work)
  1308. {
  1309. struct delayed_work *dwork = to_delayed_work(work);
  1310. struct phy_device *phydev =
  1311. container_of(dwork, struct phy_device, state_queue);
  1312. enum phy_state_work state_work;
  1313. mutex_lock(&phydev->lock);
  1314. state_work = _phy_state_machine(phydev);
  1315. mutex_unlock(&phydev->lock);
  1316. _phy_state_machine_post_work(phydev, state_work);
  1317. }
  1318. /**
  1319. * phy_stop - Bring down the PHY link, and stop checking the status
  1320. * @phydev: target phy_device struct
  1321. */
  1322. void phy_stop(struct phy_device *phydev)
  1323. {
  1324. struct net_device *dev = phydev->attached_dev;
  1325. enum phy_state_work state_work;
  1326. enum phy_state old_state;
  1327. if (!phy_is_started(phydev) && phydev->state != PHY_DOWN &&
  1328. phydev->state != PHY_ERROR) {
  1329. WARN(1, "called from state %s\n",
  1330. phy_state_to_str(phydev->state));
  1331. return;
  1332. }
  1333. mutex_lock(&phydev->lock);
  1334. old_state = phydev->state;
  1335. if (phydev->state == PHY_CABLETEST) {
  1336. phy_abort_cable_test(phydev);
  1337. netif_testing_off(dev);
  1338. }
  1339. if (phydev->sfp_bus)
  1340. sfp_upstream_stop(phydev->sfp_bus);
  1341. phydev->state = PHY_HALTED;
  1342. phy_process_state_change(phydev, old_state);
  1343. state_work = _phy_state_machine(phydev);
  1344. mutex_unlock(&phydev->lock);
  1345. _phy_state_machine_post_work(phydev, state_work);
  1346. phy_stop_machine(phydev);
  1347. /* Cannot call flush_scheduled_work() here as desired because
  1348. * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
  1349. * will not reenable interrupts.
  1350. */
  1351. }
  1352. EXPORT_SYMBOL(phy_stop);
  1353. /**
  1354. * phy_start - start or restart a PHY device
  1355. * @phydev: target phy_device struct
  1356. *
  1357. * Description: Indicates the attached device's readiness to
  1358. * handle PHY-related work. Used during startup to start the
  1359. * PHY, and after a call to phy_stop() to resume operation.
  1360. * Also used to indicate the MDIO bus has cleared an error
  1361. * condition.
  1362. */
  1363. void phy_start(struct phy_device *phydev)
  1364. {
  1365. mutex_lock(&phydev->lock);
  1366. if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
  1367. WARN(1, "called from state %s\n",
  1368. phy_state_to_str(phydev->state));
  1369. goto out;
  1370. }
  1371. if (phydev->sfp_bus)
  1372. sfp_upstream_start(phydev->sfp_bus);
  1373. /* if phy was suspended, bring the physical link up again */
  1374. __phy_resume(phydev);
  1375. phydev->state = PHY_UP;
  1376. phy_start_machine(phydev);
  1377. out:
  1378. mutex_unlock(&phydev->lock);
  1379. }
  1380. EXPORT_SYMBOL(phy_start);
  1381. /**
  1382. * phy_mac_interrupt - MAC says the link has changed
  1383. * @phydev: phy_device struct with changed link
  1384. *
  1385. * The MAC layer is able to indicate there has been a change in the PHY link
  1386. * status. Trigger the state machine and work a work queue.
  1387. */
  1388. void phy_mac_interrupt(struct phy_device *phydev)
  1389. {
  1390. /* Trigger a state machine change */
  1391. phy_trigger_machine(phydev);
  1392. }
  1393. EXPORT_SYMBOL(phy_mac_interrupt);
  1394. /**
  1395. * phy_init_eee - init and check the EEE feature
  1396. * @phydev: target phy_device struct
  1397. * @clk_stop_enable: PHY may stop the clock during LPI
  1398. *
  1399. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  1400. * is supported by looking at the MMD registers 3.20 and 7.60/61
  1401. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  1402. * bit if required.
  1403. */
  1404. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  1405. {
  1406. int ret;
  1407. if (!phydev->drv)
  1408. return -EIO;
  1409. ret = genphy_c45_eee_is_active(phydev, NULL, NULL, NULL);
  1410. if (ret < 0)
  1411. return ret;
  1412. if (!ret)
  1413. return -EPROTONOSUPPORT;
  1414. if (clk_stop_enable)
  1415. /* Configure the PHY to stop receiving xMII
  1416. * clock while it is signaling LPI.
  1417. */
  1418. ret = phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
  1419. MDIO_PCS_CTRL1_CLKSTOP_EN);
  1420. return ret < 0 ? ret : 0;
  1421. }
  1422. EXPORT_SYMBOL(phy_init_eee);
  1423. /**
  1424. * phy_get_eee_err - report the EEE wake error count
  1425. * @phydev: target phy_device struct
  1426. *
  1427. * Description: it is to report the number of time where the PHY
  1428. * failed to complete its normal wake sequence.
  1429. */
  1430. int phy_get_eee_err(struct phy_device *phydev)
  1431. {
  1432. int ret;
  1433. if (!phydev->drv)
  1434. return -EIO;
  1435. mutex_lock(&phydev->lock);
  1436. ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
  1437. mutex_unlock(&phydev->lock);
  1438. return ret;
  1439. }
  1440. EXPORT_SYMBOL(phy_get_eee_err);
  1441. /**
  1442. * phy_ethtool_get_eee - get EEE supported and status
  1443. * @phydev: target phy_device struct
  1444. * @data: ethtool_keee data
  1445. *
  1446. * Description: reports the Supported/Advertisement/LP Advertisement
  1447. * capabilities, etc.
  1448. */
  1449. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data)
  1450. {
  1451. int ret;
  1452. if (!phydev->drv)
  1453. return -EIO;
  1454. mutex_lock(&phydev->lock);
  1455. ret = genphy_c45_ethtool_get_eee(phydev, data);
  1456. eeecfg_to_eee(data, &phydev->eee_cfg);
  1457. mutex_unlock(&phydev->lock);
  1458. return ret;
  1459. }
  1460. EXPORT_SYMBOL(phy_ethtool_get_eee);
  1461. /**
  1462. * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY
  1463. * renegotiation
  1464. * @phydev: pointer to the target PHY device structure
  1465. * @data: pointer to the ethtool_keee structure containing the new EEE settings
  1466. *
  1467. * This function updates the Energy Efficient Ethernet (EEE) configuration
  1468. * for cases where only the MAC's Low Power Idle (LPI) configuration changes,
  1469. * without triggering PHY renegotiation. It ensures that the MAC is properly
  1470. * informed of the new LPI settings by cycling the link down and up, which
  1471. * is necessary for the MAC to adopt the new configuration. This adjustment
  1472. * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer
  1473. * configuration.
  1474. */
  1475. static void phy_ethtool_set_eee_noneg(struct phy_device *phydev,
  1476. struct ethtool_keee *data)
  1477. {
  1478. if (phydev->eee_cfg.tx_lpi_enabled != data->tx_lpi_enabled ||
  1479. phydev->eee_cfg.tx_lpi_timer != data->tx_lpi_timer) {
  1480. eee_to_eeecfg(&phydev->eee_cfg, data);
  1481. phydev->enable_tx_lpi = eeecfg_mac_can_tx_lpi(&phydev->eee_cfg);
  1482. if (phydev->link) {
  1483. phydev->link = false;
  1484. phy_link_down(phydev);
  1485. phydev->link = true;
  1486. phy_link_up(phydev);
  1487. }
  1488. }
  1489. }
  1490. /**
  1491. * phy_ethtool_set_eee - set EEE supported and status
  1492. * @phydev: target phy_device struct
  1493. * @data: ethtool_keee data
  1494. *
  1495. * Description: it is to program the Advertisement EEE register.
  1496. */
  1497. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data)
  1498. {
  1499. int ret;
  1500. if (!phydev->drv)
  1501. return -EIO;
  1502. mutex_lock(&phydev->lock);
  1503. ret = genphy_c45_ethtool_set_eee(phydev, data);
  1504. if (ret >= 0) {
  1505. if (ret == 0)
  1506. phy_ethtool_set_eee_noneg(phydev, data);
  1507. eee_to_eeecfg(&phydev->eee_cfg, data);
  1508. }
  1509. mutex_unlock(&phydev->lock);
  1510. return ret < 0 ? ret : 0;
  1511. }
  1512. EXPORT_SYMBOL(phy_ethtool_set_eee);
  1513. /**
  1514. * phy_ethtool_set_wol - Configure Wake On LAN
  1515. *
  1516. * @phydev: target phy_device struct
  1517. * @wol: Configuration requested
  1518. */
  1519. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1520. {
  1521. int ret;
  1522. if (phydev->drv && phydev->drv->set_wol) {
  1523. mutex_lock(&phydev->lock);
  1524. ret = phydev->drv->set_wol(phydev, wol);
  1525. mutex_unlock(&phydev->lock);
  1526. return ret;
  1527. }
  1528. return -EOPNOTSUPP;
  1529. }
  1530. EXPORT_SYMBOL(phy_ethtool_set_wol);
  1531. /**
  1532. * phy_ethtool_get_wol - Get the current Wake On LAN configuration
  1533. *
  1534. * @phydev: target phy_device struct
  1535. * @wol: Store the current configuration here
  1536. */
  1537. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1538. {
  1539. if (phydev->drv && phydev->drv->get_wol) {
  1540. mutex_lock(&phydev->lock);
  1541. phydev->drv->get_wol(phydev, wol);
  1542. mutex_unlock(&phydev->lock);
  1543. }
  1544. }
  1545. EXPORT_SYMBOL(phy_ethtool_get_wol);
  1546. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1547. struct ethtool_link_ksettings *cmd)
  1548. {
  1549. struct phy_device *phydev = ndev->phydev;
  1550. if (!phydev)
  1551. return -ENODEV;
  1552. phy_ethtool_ksettings_get(phydev, cmd);
  1553. return 0;
  1554. }
  1555. EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
  1556. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1557. const struct ethtool_link_ksettings *cmd)
  1558. {
  1559. struct phy_device *phydev = ndev->phydev;
  1560. if (!phydev)
  1561. return -ENODEV;
  1562. return phy_ethtool_ksettings_set(phydev, cmd);
  1563. }
  1564. EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
  1565. /**
  1566. * phy_ethtool_nway_reset - Restart auto negotiation
  1567. * @ndev: Network device to restart autoneg for
  1568. */
  1569. int phy_ethtool_nway_reset(struct net_device *ndev)
  1570. {
  1571. struct phy_device *phydev = ndev->phydev;
  1572. int ret;
  1573. if (!phydev)
  1574. return -ENODEV;
  1575. if (!phydev->drv)
  1576. return -EIO;
  1577. mutex_lock(&phydev->lock);
  1578. ret = phy_restart_aneg(phydev);
  1579. mutex_unlock(&phydev->lock);
  1580. return ret;
  1581. }
  1582. EXPORT_SYMBOL(phy_ethtool_nway_reset);