0023-v6.15-net-phy-move-PHY-package-code-from-phy_device..patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. From 986ec7ee75f2f1f7f93eb0e05f61f297f6123fce Mon Sep 17 00:00:00 2001
  2. From: Heiner Kallweit <[email protected]>
  3. Date: Mon, 3 Mar 2025 21:14:02 +0100
  4. Subject: [PATCH] v6.15: net: phy: move PHY package code from phy_device.c to
  5. own source file
  6. This patch is the first step in moving the PHY package related code
  7. to its own source file. No functional change intended.
  8. Signed-off-by: Heiner Kallweit <[email protected]>
  9. Link: https://patch.msgid.link/[email protected]
  10. Signed-off-by: Jakub Kicinski <[email protected]>
  11. ---
  12. drivers/net/phy/Makefile | 3 +-
  13. drivers/net/phy/phy_device.c | 237 ---------------------------------
  14. drivers/net/phy/phy_package.c | 244 ++++++++++++++++++++++++++++++++++
  15. 3 files changed, 246 insertions(+), 238 deletions(-)
  16. create mode 100644 drivers/net/phy/phy_package.c
  17. --- a/drivers/net/phy/Makefile
  18. +++ b/drivers/net/phy/Makefile
  19. @@ -2,7 +2,8 @@
  20. # Makefile for Linux PHY drivers
  21. libphy-y := phy.o phy-c45.o phy-core.o phy_device.o \
  22. - linkmode.o phy_link_topology.o
  23. + linkmode.o phy_link_topology.o \
  24. + phy_package.o
  25. mdio-bus-y += mdio_bus.o mdio_device.o
  26. ifdef CONFIG_MDIO_DEVICE
  27. --- a/drivers/net/phy/phy_device.c
  28. +++ b/drivers/net/phy/phy_device.c
  29. @@ -1783,243 +1783,6 @@ bool phy_driver_is_genphy_10g(struct phy
  30. EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
  31. /**
  32. - * phy_package_join - join a common PHY group
  33. - * @phydev: target phy_device struct
  34. - * @base_addr: cookie and base PHY address of PHY package for offset
  35. - * calculation of global register access
  36. - * @priv_size: if non-zero allocate this amount of bytes for private data
  37. - *
  38. - * This joins a PHY group and provides a shared storage for all phydevs in
  39. - * this group. This is intended to be used for packages which contain
  40. - * more than one PHY, for example a quad PHY transceiver.
  41. - *
  42. - * The base_addr parameter serves as cookie which has to have the same values
  43. - * for all members of one group and as the base PHY address of the PHY package
  44. - * for offset calculation to access generic registers of a PHY package.
  45. - * Usually, one of the PHY addresses of the different PHYs in the package
  46. - * provides access to these global registers.
  47. - * The address which is given here, will be used in the phy_package_read()
  48. - * and phy_package_write() convenience functions as base and added to the
  49. - * passed offset in those functions.
  50. - *
  51. - * This will set the shared pointer of the phydev to the shared storage.
  52. - * If this is the first call for a this cookie the shared storage will be
  53. - * allocated. If priv_size is non-zero, the given amount of bytes are
  54. - * allocated for the priv member.
  55. - *
  56. - * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
  57. - * with the same cookie but a different priv_size is an error.
  58. - */
  59. -int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
  60. -{
  61. - struct mii_bus *bus = phydev->mdio.bus;
  62. - struct phy_package_shared *shared;
  63. - int ret;
  64. -
  65. - if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
  66. - return -EINVAL;
  67. -
  68. - mutex_lock(&bus->shared_lock);
  69. - shared = bus->shared[base_addr];
  70. - if (!shared) {
  71. - ret = -ENOMEM;
  72. - shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  73. - if (!shared)
  74. - goto err_unlock;
  75. - if (priv_size) {
  76. - shared->priv = kzalloc(priv_size, GFP_KERNEL);
  77. - if (!shared->priv)
  78. - goto err_free;
  79. - shared->priv_size = priv_size;
  80. - }
  81. - shared->base_addr = base_addr;
  82. - shared->np = NULL;
  83. - refcount_set(&shared->refcnt, 1);
  84. - bus->shared[base_addr] = shared;
  85. - } else {
  86. - ret = -EINVAL;
  87. - if (priv_size && priv_size != shared->priv_size)
  88. - goto err_unlock;
  89. - refcount_inc(&shared->refcnt);
  90. - }
  91. - mutex_unlock(&bus->shared_lock);
  92. -
  93. - phydev->shared = shared;
  94. -
  95. - return 0;
  96. -
  97. -err_free:
  98. - kfree(shared);
  99. -err_unlock:
  100. - mutex_unlock(&bus->shared_lock);
  101. - return ret;
  102. -}
  103. -EXPORT_SYMBOL_GPL(phy_package_join);
  104. -
  105. -/**
  106. - * of_phy_package_join - join a common PHY group in PHY package
  107. - * @phydev: target phy_device struct
  108. - * @priv_size: if non-zero allocate this amount of bytes for private data
  109. - *
  110. - * This is a variant of phy_package_join for PHY package defined in DT.
  111. - *
  112. - * The parent node of the @phydev is checked as a valid PHY package node
  113. - * structure (by matching the node name "ethernet-phy-package") and the
  114. - * base_addr for the PHY package is passed to phy_package_join.
  115. - *
  116. - * With this configuration the shared struct will also have the np value
  117. - * filled to use additional DT defined properties in PHY specific
  118. - * probe_once and config_init_once PHY package OPs.
  119. - *
  120. - * Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
  121. - * with the same cookie but a different priv_size is an error. Or a parent
  122. - * node is not detected or is not valid or doesn't match the expected node
  123. - * name for PHY package.
  124. - */
  125. -int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
  126. -{
  127. - struct device_node *node = phydev->mdio.dev.of_node;
  128. - struct device_node *package_node;
  129. - u32 base_addr;
  130. - int ret;
  131. -
  132. - if (!node)
  133. - return -EINVAL;
  134. -
  135. - package_node = of_get_parent(node);
  136. - if (!package_node)
  137. - return -EINVAL;
  138. -
  139. - if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
  140. - ret = -EINVAL;
  141. - goto exit;
  142. - }
  143. -
  144. - if (of_property_read_u32(package_node, "reg", &base_addr)) {
  145. - ret = -EINVAL;
  146. - goto exit;
  147. - }
  148. -
  149. - ret = phy_package_join(phydev, base_addr, priv_size);
  150. - if (ret)
  151. - goto exit;
  152. -
  153. - phydev->shared->np = package_node;
  154. -
  155. - return 0;
  156. -exit:
  157. - of_node_put(package_node);
  158. - return ret;
  159. -}
  160. -EXPORT_SYMBOL_GPL(of_phy_package_join);
  161. -
  162. -/**
  163. - * phy_package_leave - leave a common PHY group
  164. - * @phydev: target phy_device struct
  165. - *
  166. - * This leaves a PHY group created by phy_package_join(). If this phydev
  167. - * was the last user of the shared data between the group, this data is
  168. - * freed. Resets the phydev->shared pointer to NULL.
  169. - */
  170. -void phy_package_leave(struct phy_device *phydev)
  171. -{
  172. - struct phy_package_shared *shared = phydev->shared;
  173. - struct mii_bus *bus = phydev->mdio.bus;
  174. -
  175. - if (!shared)
  176. - return;
  177. -
  178. - /* Decrease the node refcount on leave if present */
  179. - if (shared->np)
  180. - of_node_put(shared->np);
  181. -
  182. - if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
  183. - bus->shared[shared->base_addr] = NULL;
  184. - mutex_unlock(&bus->shared_lock);
  185. - kfree(shared->priv);
  186. - kfree(shared);
  187. - }
  188. -
  189. - phydev->shared = NULL;
  190. -}
  191. -EXPORT_SYMBOL_GPL(phy_package_leave);
  192. -
  193. -static void devm_phy_package_leave(struct device *dev, void *res)
  194. -{
  195. - phy_package_leave(*(struct phy_device **)res);
  196. -}
  197. -
  198. -/**
  199. - * devm_phy_package_join - resource managed phy_package_join()
  200. - * @dev: device that is registering this PHY package
  201. - * @phydev: target phy_device struct
  202. - * @base_addr: cookie and base PHY address of PHY package for offset
  203. - * calculation of global register access
  204. - * @priv_size: if non-zero allocate this amount of bytes for private data
  205. - *
  206. - * Managed phy_package_join(). Shared storage fetched by this function,
  207. - * phy_package_leave() is automatically called on driver detach. See
  208. - * phy_package_join() for more information.
  209. - */
  210. -int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  211. - int base_addr, size_t priv_size)
  212. -{
  213. - struct phy_device **ptr;
  214. - int ret;
  215. -
  216. - ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  217. - GFP_KERNEL);
  218. - if (!ptr)
  219. - return -ENOMEM;
  220. -
  221. - ret = phy_package_join(phydev, base_addr, priv_size);
  222. -
  223. - if (!ret) {
  224. - *ptr = phydev;
  225. - devres_add(dev, ptr);
  226. - } else {
  227. - devres_free(ptr);
  228. - }
  229. -
  230. - return ret;
  231. -}
  232. -EXPORT_SYMBOL_GPL(devm_phy_package_join);
  233. -
  234. -/**
  235. - * devm_of_phy_package_join - resource managed of_phy_package_join()
  236. - * @dev: device that is registering this PHY package
  237. - * @phydev: target phy_device struct
  238. - * @priv_size: if non-zero allocate this amount of bytes for private data
  239. - *
  240. - * Managed of_phy_package_join(). Shared storage fetched by this function,
  241. - * phy_package_leave() is automatically called on driver detach. See
  242. - * of_phy_package_join() for more information.
  243. - */
  244. -int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
  245. - size_t priv_size)
  246. -{
  247. - struct phy_device **ptr;
  248. - int ret;
  249. -
  250. - ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  251. - GFP_KERNEL);
  252. - if (!ptr)
  253. - return -ENOMEM;
  254. -
  255. - ret = of_phy_package_join(phydev, priv_size);
  256. -
  257. - if (!ret) {
  258. - *ptr = phydev;
  259. - devres_add(dev, ptr);
  260. - } else {
  261. - devres_free(ptr);
  262. - }
  263. -
  264. - return ret;
  265. -}
  266. -EXPORT_SYMBOL_GPL(devm_of_phy_package_join);
  267. -
  268. -/**
  269. * phy_detach - detach a PHY device from its network device
  270. * @phydev: target phy_device struct
  271. *
  272. --- /dev/null
  273. +++ b/drivers/net/phy/phy_package.c
  274. @@ -0,0 +1,244 @@
  275. +// SPDX-License-Identifier: GPL-2.0-or-later
  276. +/*
  277. + * PHY package support
  278. + */
  279. +
  280. +#include <linux/of.h>
  281. +#include <linux/phy.h>
  282. +
  283. +/**
  284. + * phy_package_join - join a common PHY group
  285. + * @phydev: target phy_device struct
  286. + * @base_addr: cookie and base PHY address of PHY package for offset
  287. + * calculation of global register access
  288. + * @priv_size: if non-zero allocate this amount of bytes for private data
  289. + *
  290. + * This joins a PHY group and provides a shared storage for all phydevs in
  291. + * this group. This is intended to be used for packages which contain
  292. + * more than one PHY, for example a quad PHY transceiver.
  293. + *
  294. + * The base_addr parameter serves as cookie which has to have the same values
  295. + * for all members of one group and as the base PHY address of the PHY package
  296. + * for offset calculation to access generic registers of a PHY package.
  297. + * Usually, one of the PHY addresses of the different PHYs in the package
  298. + * provides access to these global registers.
  299. + * The address which is given here, will be used in the phy_package_read()
  300. + * and phy_package_write() convenience functions as base and added to the
  301. + * passed offset in those functions.
  302. + *
  303. + * This will set the shared pointer of the phydev to the shared storage.
  304. + * If this is the first call for a this cookie the shared storage will be
  305. + * allocated. If priv_size is non-zero, the given amount of bytes are
  306. + * allocated for the priv member.
  307. + *
  308. + * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
  309. + * with the same cookie but a different priv_size is an error.
  310. + */
  311. +int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
  312. +{
  313. + struct mii_bus *bus = phydev->mdio.bus;
  314. + struct phy_package_shared *shared;
  315. + int ret;
  316. +
  317. + if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
  318. + return -EINVAL;
  319. +
  320. + mutex_lock(&bus->shared_lock);
  321. + shared = bus->shared[base_addr];
  322. + if (!shared) {
  323. + ret = -ENOMEM;
  324. + shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  325. + if (!shared)
  326. + goto err_unlock;
  327. + if (priv_size) {
  328. + shared->priv = kzalloc(priv_size, GFP_KERNEL);
  329. + if (!shared->priv)
  330. + goto err_free;
  331. + shared->priv_size = priv_size;
  332. + }
  333. + shared->base_addr = base_addr;
  334. + shared->np = NULL;
  335. + refcount_set(&shared->refcnt, 1);
  336. + bus->shared[base_addr] = shared;
  337. + } else {
  338. + ret = -EINVAL;
  339. + if (priv_size && priv_size != shared->priv_size)
  340. + goto err_unlock;
  341. + refcount_inc(&shared->refcnt);
  342. + }
  343. + mutex_unlock(&bus->shared_lock);
  344. +
  345. + phydev->shared = shared;
  346. +
  347. + return 0;
  348. +
  349. +err_free:
  350. + kfree(shared);
  351. +err_unlock:
  352. + mutex_unlock(&bus->shared_lock);
  353. + return ret;
  354. +}
  355. +EXPORT_SYMBOL_GPL(phy_package_join);
  356. +
  357. +/**
  358. + * of_phy_package_join - join a common PHY group in PHY package
  359. + * @phydev: target phy_device struct
  360. + * @priv_size: if non-zero allocate this amount of bytes for private data
  361. + *
  362. + * This is a variant of phy_package_join for PHY package defined in DT.
  363. + *
  364. + * The parent node of the @phydev is checked as a valid PHY package node
  365. + * structure (by matching the node name "ethernet-phy-package") and the
  366. + * base_addr for the PHY package is passed to phy_package_join.
  367. + *
  368. + * With this configuration the shared struct will also have the np value
  369. + * filled to use additional DT defined properties in PHY specific
  370. + * probe_once and config_init_once PHY package OPs.
  371. + *
  372. + * Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
  373. + * with the same cookie but a different priv_size is an error. Or a parent
  374. + * node is not detected or is not valid or doesn't match the expected node
  375. + * name for PHY package.
  376. + */
  377. +int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
  378. +{
  379. + struct device_node *node = phydev->mdio.dev.of_node;
  380. + struct device_node *package_node;
  381. + u32 base_addr;
  382. + int ret;
  383. +
  384. + if (!node)
  385. + return -EINVAL;
  386. +
  387. + package_node = of_get_parent(node);
  388. + if (!package_node)
  389. + return -EINVAL;
  390. +
  391. + if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
  392. + ret = -EINVAL;
  393. + goto exit;
  394. + }
  395. +
  396. + if (of_property_read_u32(package_node, "reg", &base_addr)) {
  397. + ret = -EINVAL;
  398. + goto exit;
  399. + }
  400. +
  401. + ret = phy_package_join(phydev, base_addr, priv_size);
  402. + if (ret)
  403. + goto exit;
  404. +
  405. + phydev->shared->np = package_node;
  406. +
  407. + return 0;
  408. +exit:
  409. + of_node_put(package_node);
  410. + return ret;
  411. +}
  412. +EXPORT_SYMBOL_GPL(of_phy_package_join);
  413. +
  414. +/**
  415. + * phy_package_leave - leave a common PHY group
  416. + * @phydev: target phy_device struct
  417. + *
  418. + * This leaves a PHY group created by phy_package_join(). If this phydev
  419. + * was the last user of the shared data between the group, this data is
  420. + * freed. Resets the phydev->shared pointer to NULL.
  421. + */
  422. +void phy_package_leave(struct phy_device *phydev)
  423. +{
  424. + struct phy_package_shared *shared = phydev->shared;
  425. + struct mii_bus *bus = phydev->mdio.bus;
  426. +
  427. + if (!shared)
  428. + return;
  429. +
  430. + /* Decrease the node refcount on leave if present */
  431. + if (shared->np)
  432. + of_node_put(shared->np);
  433. +
  434. + if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
  435. + bus->shared[shared->base_addr] = NULL;
  436. + mutex_unlock(&bus->shared_lock);
  437. + kfree(shared->priv);
  438. + kfree(shared);
  439. + }
  440. +
  441. + phydev->shared = NULL;
  442. +}
  443. +EXPORT_SYMBOL_GPL(phy_package_leave);
  444. +
  445. +static void devm_phy_package_leave(struct device *dev, void *res)
  446. +{
  447. + phy_package_leave(*(struct phy_device **)res);
  448. +}
  449. +
  450. +/**
  451. + * devm_phy_package_join - resource managed phy_package_join()
  452. + * @dev: device that is registering this PHY package
  453. + * @phydev: target phy_device struct
  454. + * @base_addr: cookie and base PHY address of PHY package for offset
  455. + * calculation of global register access
  456. + * @priv_size: if non-zero allocate this amount of bytes for private data
  457. + *
  458. + * Managed phy_package_join(). Shared storage fetched by this function,
  459. + * phy_package_leave() is automatically called on driver detach. See
  460. + * phy_package_join() for more information.
  461. + */
  462. +int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  463. + int base_addr, size_t priv_size)
  464. +{
  465. + struct phy_device **ptr;
  466. + int ret;
  467. +
  468. + ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  469. + GFP_KERNEL);
  470. + if (!ptr)
  471. + return -ENOMEM;
  472. +
  473. + ret = phy_package_join(phydev, base_addr, priv_size);
  474. +
  475. + if (!ret) {
  476. + *ptr = phydev;
  477. + devres_add(dev, ptr);
  478. + } else {
  479. + devres_free(ptr);
  480. + }
  481. +
  482. + return ret;
  483. +}
  484. +EXPORT_SYMBOL_GPL(devm_phy_package_join);
  485. +
  486. +/**
  487. + * devm_of_phy_package_join - resource managed of_phy_package_join()
  488. + * @dev: device that is registering this PHY package
  489. + * @phydev: target phy_device struct
  490. + * @priv_size: if non-zero allocate this amount of bytes for private data
  491. + *
  492. + * Managed of_phy_package_join(). Shared storage fetched by this function,
  493. + * phy_package_leave() is automatically called on driver detach. See
  494. + * of_phy_package_join() for more information.
  495. + */
  496. +int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
  497. + size_t priv_size)
  498. +{
  499. + struct phy_device **ptr;
  500. + int ret;
  501. +
  502. + ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
  503. + GFP_KERNEL);
  504. + if (!ptr)
  505. + return -ENOMEM;
  506. +
  507. + ret = of_phy_package_join(phydev, priv_size);
  508. +
  509. + if (!ret) {
  510. + *ptr = phydev;
  511. + devres_add(dev, ptr);
  512. + } else {
  513. + devres_free(ptr);
  514. + }
  515. +
  516. + return ret;
  517. +}
  518. +EXPORT_SYMBOL_GPL(devm_of_phy_package_join);