1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for finding and configuring PHYs.
3 * Also contains generic PHY driver
4 *
5 * Author: Andy Fleming
6 *
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/bitmap.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/etherdevice.h>
16 #include <linux/ethtool.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/mdio.h>
22 #include <linux/mii.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/phy.h>
27 #include <linux/phy_led_triggers.h>
28 #include <linux/property.h>
29 #include <linux/sfp.h>
30 #include <linux/skbuff.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/uaccess.h>
34 #include <linux/unistd.h>
35
36 MODULE_DESCRIPTION("PHY library");
37 MODULE_AUTHOR("Andy Fleming");
38 MODULE_LICENSE("GPL");
39
40 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
41 EXPORT_SYMBOL_GPL(phy_basic_features);
42
43 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
44 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
45
46 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
47 EXPORT_SYMBOL_GPL(phy_gbit_features);
48
49 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
50 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
51
52 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
53 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
54
55 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
56 EXPORT_SYMBOL_GPL(phy_10gbit_features);
57
58 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
59 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
60
61 const int phy_basic_ports_array[3] = {
62 ETHTOOL_LINK_MODE_Autoneg_BIT,
63 ETHTOOL_LINK_MODE_TP_BIT,
64 ETHTOOL_LINK_MODE_MII_BIT,
65 };
66 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
67
68 const int phy_fibre_port_array[1] = {
69 ETHTOOL_LINK_MODE_FIBRE_BIT,
70 };
71 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
72
73 const int phy_all_ports_features_array[7] = {
74 ETHTOOL_LINK_MODE_Autoneg_BIT,
75 ETHTOOL_LINK_MODE_TP_BIT,
76 ETHTOOL_LINK_MODE_MII_BIT,
77 ETHTOOL_LINK_MODE_FIBRE_BIT,
78 ETHTOOL_LINK_MODE_AUI_BIT,
79 ETHTOOL_LINK_MODE_BNC_BIT,
80 ETHTOOL_LINK_MODE_Backplane_BIT,
81 };
82 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
83
84 const int phy_10_100_features_array[4] = {
85 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
86 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
87 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
88 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
89 };
90 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
91
92 const int phy_basic_t1_features_array[2] = {
93 ETHTOOL_LINK_MODE_TP_BIT,
94 ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
95 };
96 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
97
98 const int phy_gbit_features_array[2] = {
99 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
100 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
101 };
102 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
103
104 const int phy_10gbit_features_array[1] = {
105 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
106 };
107 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
108
109 static const int phy_10gbit_fec_features_array[1] = {
110 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
111 };
112
113 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
114 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
115
116 static const int phy_10gbit_full_features_array[] = {
117 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
118 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
119 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
120 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
121 };
122
features_init(void)123 static void features_init(void)
124 {
125 /* 10/100 half/full*/
126 linkmode_set_bit_array(phy_basic_ports_array,
127 ARRAY_SIZE(phy_basic_ports_array),
128 phy_basic_features);
129 linkmode_set_bit_array(phy_10_100_features_array,
130 ARRAY_SIZE(phy_10_100_features_array),
131 phy_basic_features);
132
133 /* 100 full, TP */
134 linkmode_set_bit_array(phy_basic_t1_features_array,
135 ARRAY_SIZE(phy_basic_t1_features_array),
136 phy_basic_t1_features);
137
138 /* 10/100 half/full + 1000 half/full */
139 linkmode_set_bit_array(phy_basic_ports_array,
140 ARRAY_SIZE(phy_basic_ports_array),
141 phy_gbit_features);
142 linkmode_set_bit_array(phy_10_100_features_array,
143 ARRAY_SIZE(phy_10_100_features_array),
144 phy_gbit_features);
145 linkmode_set_bit_array(phy_gbit_features_array,
146 ARRAY_SIZE(phy_gbit_features_array),
147 phy_gbit_features);
148
149 /* 10/100 half/full + 1000 half/full + fibre*/
150 linkmode_set_bit_array(phy_basic_ports_array,
151 ARRAY_SIZE(phy_basic_ports_array),
152 phy_gbit_fibre_features);
153 linkmode_set_bit_array(phy_10_100_features_array,
154 ARRAY_SIZE(phy_10_100_features_array),
155 phy_gbit_fibre_features);
156 linkmode_set_bit_array(phy_gbit_features_array,
157 ARRAY_SIZE(phy_gbit_features_array),
158 phy_gbit_fibre_features);
159 linkmode_set_bit_array(phy_fibre_port_array,
160 ARRAY_SIZE(phy_fibre_port_array),
161 phy_gbit_fibre_features);
162
163 /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
164 linkmode_set_bit_array(phy_all_ports_features_array,
165 ARRAY_SIZE(phy_all_ports_features_array),
166 phy_gbit_all_ports_features);
167 linkmode_set_bit_array(phy_10_100_features_array,
168 ARRAY_SIZE(phy_10_100_features_array),
169 phy_gbit_all_ports_features);
170 linkmode_set_bit_array(phy_gbit_features_array,
171 ARRAY_SIZE(phy_gbit_features_array),
172 phy_gbit_all_ports_features);
173
174 /* 10/100 half/full + 1000 half/full + 10G full*/
175 linkmode_set_bit_array(phy_all_ports_features_array,
176 ARRAY_SIZE(phy_all_ports_features_array),
177 phy_10gbit_features);
178 linkmode_set_bit_array(phy_10_100_features_array,
179 ARRAY_SIZE(phy_10_100_features_array),
180 phy_10gbit_features);
181 linkmode_set_bit_array(phy_gbit_features_array,
182 ARRAY_SIZE(phy_gbit_features_array),
183 phy_10gbit_features);
184 linkmode_set_bit_array(phy_10gbit_features_array,
185 ARRAY_SIZE(phy_10gbit_features_array),
186 phy_10gbit_features);
187
188 /* 10/100/1000/10G full */
189 linkmode_set_bit_array(phy_all_ports_features_array,
190 ARRAY_SIZE(phy_all_ports_features_array),
191 phy_10gbit_full_features);
192 linkmode_set_bit_array(phy_10gbit_full_features_array,
193 ARRAY_SIZE(phy_10gbit_full_features_array),
194 phy_10gbit_full_features);
195 /* 10G FEC only */
196 linkmode_set_bit_array(phy_10gbit_fec_features_array,
197 ARRAY_SIZE(phy_10gbit_fec_features_array),
198 phy_10gbit_fec_features);
199 }
200
phy_device_free(struct phy_device * phydev)201 void phy_device_free(struct phy_device *phydev)
202 {
203 put_device(&phydev->mdio.dev);
204 }
205 EXPORT_SYMBOL(phy_device_free);
206
phy_mdio_device_free(struct mdio_device * mdiodev)207 static void phy_mdio_device_free(struct mdio_device *mdiodev)
208 {
209 struct phy_device *phydev;
210
211 phydev = container_of(mdiodev, struct phy_device, mdio);
212 phy_device_free(phydev);
213 }
214
phy_device_release(struct device * dev)215 static void phy_device_release(struct device *dev)
216 {
217 kfree(to_phy_device(dev));
218 }
219
phy_mdio_device_remove(struct mdio_device * mdiodev)220 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
221 {
222 struct phy_device *phydev;
223
224 phydev = container_of(mdiodev, struct phy_device, mdio);
225 phy_device_remove(phydev);
226 }
227
228 static struct phy_driver genphy_driver;
229
230 static LIST_HEAD(phy_fixup_list);
231 static DEFINE_MUTEX(phy_fixup_lock);
232
mdio_bus_phy_may_suspend(struct phy_device * phydev)233 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
234 {
235 struct device_driver *drv = phydev->mdio.dev.driver;
236 struct phy_driver *phydrv = to_phy_driver(drv);
237 struct net_device *netdev = phydev->attached_dev;
238
239 if (!drv || !phydrv->suspend)
240 return false;
241
242 /* PHY not attached? May suspend if the PHY has not already been
243 * suspended as part of a prior call to phy_disconnect() ->
244 * phy_detach() -> phy_suspend() because the parent netdev might be the
245 * MDIO bus driver and clock gated at this point.
246 */
247 if (!netdev)
248 goto out;
249
250 if (netdev->wol_enabled)
251 return false;
252
253 /* As long as not all affected network drivers support the
254 * wol_enabled flag, let's check for hints that WoL is enabled.
255 * Don't suspend PHY if the attached netdev parent may wake up.
256 * The parent may point to a PCI device, as in tg3 driver.
257 */
258 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
259 return false;
260
261 /* Also don't suspend PHY if the netdev itself may wakeup. This
262 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
263 * e.g. SoC devices.
264 */
265 if (device_may_wakeup(&netdev->dev))
266 return false;
267
268 out:
269 return !phydev->suspended;
270 }
271
mdio_bus_phy_suspend(struct device * dev)272 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
273 {
274 struct phy_device *phydev = to_phy_device(dev);
275
276 /* We must stop the state machine manually, otherwise it stops out of
277 * control, possibly with the phydev->lock held. Upon resume, netdev
278 * may call phy routines that try to grab the same lock, and that may
279 * lead to a deadlock.
280 */
281 if (phydev->attached_dev && phydev->adjust_link)
282 phy_stop_machine(phydev);
283
284 if (!mdio_bus_phy_may_suspend(phydev))
285 return 0;
286
287 phydev->suspended_by_mdio_bus = 1;
288
289 return phy_suspend(phydev);
290 }
291
mdio_bus_phy_resume(struct device * dev)292 static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
293 {
294 struct phy_device *phydev = to_phy_device(dev);
295 int ret;
296
297 if (!phydev->suspended_by_mdio_bus)
298 goto no_resume;
299
300 phydev->suspended_by_mdio_bus = 0;
301
302 ret = phy_init_hw(phydev);
303 if (ret < 0)
304 return ret;
305
306 ret = phy_resume(phydev);
307 if (ret < 0)
308 return ret;
309 no_resume:
310 if (phydev->attached_dev && phydev->adjust_link)
311 phy_start_machine(phydev);
312
313 return 0;
314 }
315
316 static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
317 mdio_bus_phy_resume);
318
319 /**
320 * phy_register_fixup - creates a new phy_fixup and adds it to the list
321 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
322 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
323 * It can also be PHY_ANY_UID
324 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
325 * comparison
326 * @run: The actual code to be run when a matching PHY is found
327 */
phy_register_fixup(const char * bus_id,u32 phy_uid,u32 phy_uid_mask,int (* run)(struct phy_device *))328 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
329 int (*run)(struct phy_device *))
330 {
331 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
332
333 if (!fixup)
334 return -ENOMEM;
335
336 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
337 fixup->phy_uid = phy_uid;
338 fixup->phy_uid_mask = phy_uid_mask;
339 fixup->run = run;
340
341 mutex_lock(&phy_fixup_lock);
342 list_add_tail(&fixup->list, &phy_fixup_list);
343 mutex_unlock(&phy_fixup_lock);
344
345 return 0;
346 }
347 EXPORT_SYMBOL(phy_register_fixup);
348
349 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
phy_register_fixup_for_uid(u32 phy_uid,u32 phy_uid_mask,int (* run)(struct phy_device *))350 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
351 int (*run)(struct phy_device *))
352 {
353 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
354 }
355 EXPORT_SYMBOL(phy_register_fixup_for_uid);
356
357 /* Registers a fixup to be run on the PHY with id string bus_id */
phy_register_fixup_for_id(const char * bus_id,int (* run)(struct phy_device *))358 int phy_register_fixup_for_id(const char *bus_id,
359 int (*run)(struct phy_device *))
360 {
361 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
362 }
363 EXPORT_SYMBOL(phy_register_fixup_for_id);
364
365 /**
366 * phy_unregister_fixup - remove a phy_fixup from the list
367 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
368 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
369 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
370 */
phy_unregister_fixup(const char * bus_id,u32 phy_uid,u32 phy_uid_mask)371 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
372 {
373 struct list_head *pos, *n;
374 struct phy_fixup *fixup;
375 int ret;
376
377 ret = -ENODEV;
378
379 mutex_lock(&phy_fixup_lock);
380 list_for_each_safe(pos, n, &phy_fixup_list) {
381 fixup = list_entry(pos, struct phy_fixup, list);
382
383 if ((!strcmp(fixup->bus_id, bus_id)) &&
384 ((fixup->phy_uid & phy_uid_mask) ==
385 (phy_uid & phy_uid_mask))) {
386 list_del(&fixup->list);
387 kfree(fixup);
388 ret = 0;
389 break;
390 }
391 }
392 mutex_unlock(&phy_fixup_lock);
393
394 return ret;
395 }
396 EXPORT_SYMBOL(phy_unregister_fixup);
397
398 /* Unregisters a fixup of any PHY with the UID in phy_uid */
phy_unregister_fixup_for_uid(u32 phy_uid,u32 phy_uid_mask)399 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
400 {
401 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
402 }
403 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
404
405 /* Unregisters a fixup of the PHY with id string bus_id */
phy_unregister_fixup_for_id(const char * bus_id)406 int phy_unregister_fixup_for_id(const char *bus_id)
407 {
408 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
409 }
410 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
411
412 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
413 * Fixups can be set to match any in one or more fields.
414 */
phy_needs_fixup(struct phy_device * phydev,struct phy_fixup * fixup)415 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
416 {
417 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
418 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
419 return 0;
420
421 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
422 (phydev->phy_id & fixup->phy_uid_mask))
423 if (fixup->phy_uid != PHY_ANY_UID)
424 return 0;
425
426 return 1;
427 }
428
429 /* Runs any matching fixups for this phydev */
phy_scan_fixups(struct phy_device * phydev)430 static int phy_scan_fixups(struct phy_device *phydev)
431 {
432 struct phy_fixup *fixup;
433
434 mutex_lock(&phy_fixup_lock);
435 list_for_each_entry(fixup, &phy_fixup_list, list) {
436 if (phy_needs_fixup(phydev, fixup)) {
437 int err = fixup->run(phydev);
438
439 if (err < 0) {
440 mutex_unlock(&phy_fixup_lock);
441 return err;
442 }
443 phydev->has_fixups = true;
444 }
445 }
446 mutex_unlock(&phy_fixup_lock);
447
448 return 0;
449 }
450
phy_bus_match(struct device * dev,struct device_driver * drv)451 static int phy_bus_match(struct device *dev, struct device_driver *drv)
452 {
453 struct phy_device *phydev = to_phy_device(dev);
454 struct phy_driver *phydrv = to_phy_driver(drv);
455 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
456 int i;
457
458 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
459 return 0;
460
461 if (phydrv->match_phy_device)
462 return phydrv->match_phy_device(phydev);
463
464 if (phydev->is_c45) {
465 for (i = 1; i < num_ids; i++) {
466 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
467 continue;
468
469 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
470 (phydev->c45_ids.device_ids[i] &
471 phydrv->phy_id_mask))
472 return 1;
473 }
474 return 0;
475 } else {
476 return (phydrv->phy_id & phydrv->phy_id_mask) ==
477 (phydev->phy_id & phydrv->phy_id_mask);
478 }
479 }
480
481 static ssize_t
phy_id_show(struct device * dev,struct device_attribute * attr,char * buf)482 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
483 {
484 struct phy_device *phydev = to_phy_device(dev);
485
486 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
487 }
488 static DEVICE_ATTR_RO(phy_id);
489
490 static ssize_t
phy_interface_show(struct device * dev,struct device_attribute * attr,char * buf)491 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
492 {
493 struct phy_device *phydev = to_phy_device(dev);
494 const char *mode = NULL;
495
496 if (phy_is_internal(phydev))
497 mode = "internal";
498 else
499 mode = phy_modes(phydev->interface);
500
501 return sprintf(buf, "%s\n", mode);
502 }
503 static DEVICE_ATTR_RO(phy_interface);
504
505 static ssize_t
phy_has_fixups_show(struct device * dev,struct device_attribute * attr,char * buf)506 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
507 char *buf)
508 {
509 struct phy_device *phydev = to_phy_device(dev);
510
511 return sprintf(buf, "%d\n", phydev->has_fixups);
512 }
513 static DEVICE_ATTR_RO(phy_has_fixups);
514
515 static ssize_t
phy_registers_show(struct device * dev,struct device_attribute * attr,char * buf)516 phy_registers_show(struct device *dev, struct device_attribute *attr, char *buf)
517 {
518 struct phy_device *phydev = to_phy_device(dev);
519 int index;
520
521 for (index = 0; index < 32; index++)
522 sprintf(buf, "%s%2d: 0x%x\n", buf, index,
523 phy_read(phydev, index));
524
525 return strlen(buf);
526 }
527
528 static ssize_t
phy_registers_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)529 phy_registers_store(struct device *dev,
530 struct device_attribute *attr,
531 const char *buf, size_t count)
532 {
533 struct phy_device *phydev = to_phy_device(dev);
534 int index = 0, val = 0;
535 char tmp[32];
536 char *data;
537
538 if (count >= sizeof(tmp))
539 goto out;
540
541 memset(tmp, 0, sizeof(tmp));
542 memcpy(tmp, buf, count);
543
544 data = tmp;
545 data = strstr(data, " ");
546 if (!data)
547 goto out;
548 *data = 0;
549 data++;
550
551 if (kstrtoint(tmp, 0, &index) || index >= 32)
552 goto out;
553
554 if (kstrtoint(data, 0, &val) || val > 0xffff)
555 goto out;
556
557 pr_info("Set Ethernet PHY register %d to 0x%x\n", (int)index, (int)val);
558
559 phy_write(phydev, index, val);
560
561 return count;
562
563 out:
564 pr_err("wrong register value input\n");
565 pr_err("usage: <reg index> <value>\n");
566
567 return count;
568 }
569
570 static DEVICE_ATTR_RW(phy_registers);
571
572 static struct attribute *phy_dev_attrs[] = {
573 &dev_attr_phy_id.attr,
574 &dev_attr_phy_interface.attr,
575 &dev_attr_phy_has_fixups.attr,
576 &dev_attr_phy_registers.attr,
577 NULL,
578 };
579 ATTRIBUTE_GROUPS(phy_dev);
580
581 static const struct device_type mdio_bus_phy_type = {
582 .name = "PHY",
583 .groups = phy_dev_groups,
584 .release = phy_device_release,
585 .pm = pm_ptr(&mdio_bus_phy_pm_ops),
586 };
587
phy_request_driver_module(struct phy_device * dev,u32 phy_id)588 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
589 {
590 int ret;
591
592 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
593 MDIO_ID_ARGS(phy_id));
594 /* We only check for failures in executing the usermode binary,
595 * not whether a PHY driver module exists for the PHY ID.
596 * Accept -ENOENT because this may occur in case no initramfs exists,
597 * then modprobe isn't available.
598 */
599 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
600 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
601 ret, (unsigned long)phy_id);
602 return ret;
603 }
604
605 return 0;
606 }
607
phy_device_create(struct mii_bus * bus,int addr,u32 phy_id,bool is_c45,struct phy_c45_device_ids * c45_ids)608 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
609 bool is_c45,
610 struct phy_c45_device_ids *c45_ids)
611 {
612 struct phy_device *dev;
613 struct mdio_device *mdiodev;
614 int ret = 0;
615
616 /* We allocate the device, and initialize the default values */
617 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
618 if (!dev)
619 return ERR_PTR(-ENOMEM);
620
621 mdiodev = &dev->mdio;
622 mdiodev->dev.parent = &bus->dev;
623 mdiodev->dev.bus = &mdio_bus_type;
624 mdiodev->dev.type = &mdio_bus_phy_type;
625 mdiodev->bus = bus;
626 mdiodev->bus_match = phy_bus_match;
627 mdiodev->addr = addr;
628 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
629 mdiodev->device_free = phy_mdio_device_free;
630 mdiodev->device_remove = phy_mdio_device_remove;
631
632 dev->speed = SPEED_UNKNOWN;
633 dev->duplex = DUPLEX_UNKNOWN;
634 dev->pause = 0;
635 dev->asym_pause = 0;
636 dev->link = 0;
637 dev->port = PORT_TP;
638 dev->interface = PHY_INTERFACE_MODE_GMII;
639
640 dev->autoneg = AUTONEG_ENABLE;
641
642 dev->is_c45 = is_c45;
643 dev->phy_id = phy_id;
644 if (c45_ids)
645 dev->c45_ids = *c45_ids;
646 dev->irq = bus->irq[addr];
647
648 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
649 device_initialize(&mdiodev->dev);
650
651 dev->state = PHY_DOWN;
652
653 mutex_init(&dev->lock);
654 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
655
656 /* Request the appropriate module unconditionally; don't
657 * bother trying to do so only if it isn't already loaded,
658 * because that gets complicated. A hotplug event would have
659 * done an unconditional modprobe anyway.
660 * We don't do normal hotplug because it won't work for MDIO
661 * -- because it relies on the device staying around for long
662 * enough for the driver to get loaded. With MDIO, the NIC
663 * driver will get bored and give up as soon as it finds that
664 * there's no driver _already_ loaded.
665 */
666 if (is_c45 && c45_ids) {
667 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
668 int i;
669
670 for (i = 1; i < num_ids; i++) {
671 if (c45_ids->device_ids[i] == 0xffffffff)
672 continue;
673
674 ret = phy_request_driver_module(dev,
675 c45_ids->device_ids[i]);
676 if (ret)
677 break;
678 }
679 } else {
680 ret = phy_request_driver_module(dev, phy_id);
681 }
682
683 if (ret) {
684 put_device(&mdiodev->dev);
685 dev = ERR_PTR(ret);
686 }
687
688 return dev;
689 }
690 EXPORT_SYMBOL(phy_device_create);
691
692 /* phy_c45_probe_present - checks to see if a MMD is present in the package
693 * @bus: the target MII bus
694 * @prtad: PHY package address on the MII bus
695 * @devad: PHY device (MMD) address
696 *
697 * Read the MDIO_STAT2 register, and check whether a device is responding
698 * at this address.
699 *
700 * Returns: negative error number on bus access error, zero if no device
701 * is responding, or positive if a device is present.
702 */
phy_c45_probe_present(struct mii_bus * bus,int prtad,int devad)703 static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
704 {
705 int stat2;
706
707 stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
708 if (stat2 < 0)
709 return stat2;
710
711 return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
712 }
713
714 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
715 * @bus: the target MII bus
716 * @addr: PHY address on the MII bus
717 * @dev_addr: MMD address in the PHY.
718 * @devices_in_package: where to store the devices in package information.
719 *
720 * Description: reads devices in package registers of a MMD at @dev_addr
721 * from PHY at @addr on @bus.
722 *
723 * Returns: 0 on success, -EIO on failure.
724 */
get_phy_c45_devs_in_pkg(struct mii_bus * bus,int addr,int dev_addr,u32 * devices_in_package)725 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
726 u32 *devices_in_package)
727 {
728 int phy_reg;
729
730 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
731 if (phy_reg < 0)
732 return -EIO;
733 *devices_in_package = phy_reg << 16;
734
735 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
736 if (phy_reg < 0)
737 return -EIO;
738 *devices_in_package |= phy_reg;
739
740 return 0;
741 }
742
743 /**
744 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
745 * @bus: the target MII bus
746 * @addr: PHY address on the MII bus
747 * @c45_ids: where to store the c45 ID information.
748 *
749 * Read the PHY "devices in package". If this appears to be valid, read
750 * the PHY identifiers for each device. Return the "devices in package"
751 * and identifiers in @c45_ids.
752 *
753 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
754 * the "devices in package" is invalid.
755 */
get_phy_c45_ids(struct mii_bus * bus,int addr,struct phy_c45_device_ids * c45_ids)756 static int get_phy_c45_ids(struct mii_bus *bus, int addr,
757 struct phy_c45_device_ids *c45_ids)
758 {
759 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
760 u32 devs_in_pkg = 0;
761 int i, ret, phy_reg;
762
763 /* Find first non-zero Devices In package. Device zero is reserved
764 * for 802.3 c45 complied PHYs, so don't probe it at first.
765 */
766 for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
767 (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
768 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
769 /* Check that there is a device present at this
770 * address before reading the devices-in-package
771 * register to avoid reading garbage from the PHY.
772 * Some PHYs (88x3310) vendor space is not IEEE802.3
773 * compliant.
774 */
775 ret = phy_c45_probe_present(bus, addr, i);
776 if (ret < 0)
777 return -EIO;
778
779 if (!ret)
780 continue;
781 }
782 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
783 if (phy_reg < 0)
784 return -EIO;
785 }
786
787 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
788 /* If mostly Fs, there is no device there, then let's probe
789 * MMD 0, as some 10G PHYs have zero Devices In package,
790 * e.g. Cortina CS4315/CS4340 PHY.
791 */
792 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
793 if (phy_reg < 0)
794 return -EIO;
795
796 /* no device there, let's get out of here */
797 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
798 return -ENODEV;
799 }
800
801 /* Now probe Device Identifiers for each device present. */
802 for (i = 1; i < num_ids; i++) {
803 if (!(devs_in_pkg & (1 << i)))
804 continue;
805
806 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
807 /* Probe the "Device Present" bits for the vendor MMDs
808 * to ignore these if they do not contain IEEE 802.3
809 * registers.
810 */
811 ret = phy_c45_probe_present(bus, addr, i);
812 if (ret < 0)
813 return ret;
814
815 if (!ret)
816 continue;
817 }
818
819 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
820 if (phy_reg < 0)
821 return -EIO;
822 c45_ids->device_ids[i] = phy_reg << 16;
823
824 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
825 if (phy_reg < 0)
826 return -EIO;
827 c45_ids->device_ids[i] |= phy_reg;
828 }
829
830 c45_ids->devices_in_package = devs_in_pkg;
831 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
832 c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
833
834 return 0;
835 }
836
837 /**
838 * get_phy_c22_id - reads the specified addr for its clause 22 ID.
839 * @bus: the target MII bus
840 * @addr: PHY address on the MII bus
841 * @phy_id: where to store the ID retrieved.
842 *
843 * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
844 * placing it in @phy_id. Return zero on successful read and the ID is
845 * valid, %-EIO on bus access error, or %-ENODEV if no device responds
846 * or invalid ID.
847 */
get_phy_c22_id(struct mii_bus * bus,int addr,u32 * phy_id)848 static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
849 {
850 int phy_reg;
851
852 /* Grab the bits from PHYIR1, and put them in the upper half */
853 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
854 if (phy_reg < 0) {
855 /* returning -ENODEV doesn't stop bus scanning */
856 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
857 }
858
859 *phy_id = phy_reg << 16;
860
861 /* Grab the bits from PHYIR2, and put them in the lower half */
862 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
863 if (phy_reg < 0) {
864 /* returning -ENODEV doesn't stop bus scanning */
865 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
866 }
867
868 *phy_id |= phy_reg;
869
870 /* If the phy_id is mostly Fs, there is no device there */
871 if ((*phy_id & 0x1fffffff) == 0x1fffffff)
872 return -ENODEV;
873
874 return 0;
875 }
876
877 /**
878 * get_phy_device - reads the specified PHY device and returns its @phy_device
879 * struct
880 * @bus: the target MII bus
881 * @addr: PHY address on the MII bus
882 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
883 *
884 * Probe for a PHY at @addr on @bus.
885 *
886 * When probing for a clause 22 PHY, then read the ID registers. If we find
887 * a valid ID, allocate and return a &struct phy_device.
888 *
889 * When probing for a clause 45 PHY, read the "devices in package" registers.
890 * If the "devices in package" appears valid, read the ID registers for each
891 * MMD, allocate and return a &struct phy_device.
892 *
893 * Returns an allocated &struct phy_device on success, %-ENODEV if there is
894 * no PHY present, or %-EIO on bus access error.
895 */
get_phy_device(struct mii_bus * bus,int addr,bool is_c45)896 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
897 {
898 struct phy_c45_device_ids c45_ids;
899 u32 phy_id = 0;
900 int r;
901
902 c45_ids.devices_in_package = 0;
903 c45_ids.mmds_present = 0;
904 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
905
906 if (is_c45)
907 r = get_phy_c45_ids(bus, addr, &c45_ids);
908 else
909 r = get_phy_c22_id(bus, addr, &phy_id);
910
911 if (r)
912 return ERR_PTR(r);
913
914 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
915 }
916 EXPORT_SYMBOL(get_phy_device);
917
918 /**
919 * phy_device_register - Register the phy device on the MDIO bus
920 * @phydev: phy_device structure to be added to the MDIO bus
921 */
phy_device_register(struct phy_device * phydev)922 int phy_device_register(struct phy_device *phydev)
923 {
924 int err;
925
926 err = mdiobus_register_device(&phydev->mdio);
927 if (err)
928 return err;
929
930 /* Deassert the reset signal */
931 phy_device_reset(phydev, 0);
932
933 /* Run all of the fixups for this PHY */
934 err = phy_scan_fixups(phydev);
935 if (err) {
936 phydev_err(phydev, "failed to initialize\n");
937 goto out;
938 }
939
940 err = device_add(&phydev->mdio.dev);
941 if (err) {
942 phydev_err(phydev, "failed to add\n");
943 goto out;
944 }
945
946 return 0;
947
948 out:
949 /* Assert the reset signal */
950 phy_device_reset(phydev, 1);
951
952 mdiobus_unregister_device(&phydev->mdio);
953 return err;
954 }
955 EXPORT_SYMBOL(phy_device_register);
956
957 /**
958 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
959 * @phydev: phy_device structure to remove
960 *
961 * This doesn't free the phy_device itself, it merely reverses the effects
962 * of phy_device_register(). Use phy_device_free() to free the device
963 * after calling this function.
964 */
phy_device_remove(struct phy_device * phydev)965 void phy_device_remove(struct phy_device *phydev)
966 {
967 if (phydev->mii_ts)
968 unregister_mii_timestamper(phydev->mii_ts);
969
970 device_del(&phydev->mdio.dev);
971
972 /* Assert the reset signal */
973 phy_device_reset(phydev, 1);
974
975 mdiobus_unregister_device(&phydev->mdio);
976 }
977 EXPORT_SYMBOL(phy_device_remove);
978
979 /**
980 * phy_find_first - finds the first PHY device on the bus
981 * @bus: the target MII bus
982 */
phy_find_first(struct mii_bus * bus)983 struct phy_device *phy_find_first(struct mii_bus *bus)
984 {
985 struct phy_device *phydev;
986 int addr;
987
988 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
989 phydev = mdiobus_get_phy(bus, addr);
990 if (phydev)
991 return phydev;
992 }
993 return NULL;
994 }
995 EXPORT_SYMBOL(phy_find_first);
996
phy_link_change(struct phy_device * phydev,bool up)997 static void phy_link_change(struct phy_device *phydev, bool up)
998 {
999 struct net_device *netdev = phydev->attached_dev;
1000
1001 if (up)
1002 netif_carrier_on(netdev);
1003 else
1004 netif_carrier_off(netdev);
1005 phydev->adjust_link(netdev);
1006 if (phydev->mii_ts && phydev->mii_ts->link_state)
1007 phydev->mii_ts->link_state(phydev->mii_ts, phydev);
1008 }
1009
1010 /**
1011 * phy_prepare_link - prepares the PHY layer to monitor link status
1012 * @phydev: target phy_device struct
1013 * @handler: callback function for link status change notifications
1014 *
1015 * Description: Tells the PHY infrastructure to handle the
1016 * gory details on monitoring link status (whether through
1017 * polling or an interrupt), and to call back to the
1018 * connected device driver when the link status changes.
1019 * If you want to monitor your own link state, don't call
1020 * this function.
1021 */
phy_prepare_link(struct phy_device * phydev,void (* handler)(struct net_device *))1022 static void phy_prepare_link(struct phy_device *phydev,
1023 void (*handler)(struct net_device *))
1024 {
1025 phydev->adjust_link = handler;
1026 }
1027
1028 /**
1029 * phy_connect_direct - connect an ethernet device to a specific phy_device
1030 * @dev: the network device to connect
1031 * @phydev: the pointer to the phy device
1032 * @handler: callback function for state change notifications
1033 * @interface: PHY device's interface
1034 */
phy_connect_direct(struct net_device * dev,struct phy_device * phydev,void (* handler)(struct net_device *),phy_interface_t interface)1035 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1036 void (*handler)(struct net_device *),
1037 phy_interface_t interface)
1038 {
1039 int rc;
1040
1041 if (!dev)
1042 return -EINVAL;
1043
1044 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1045 if (rc)
1046 return rc;
1047
1048 phy_prepare_link(phydev, handler);
1049 if (phy_interrupt_is_valid(phydev))
1050 phy_request_interrupt(phydev);
1051
1052 return 0;
1053 }
1054 EXPORT_SYMBOL(phy_connect_direct);
1055
1056 /**
1057 * phy_connect - connect an ethernet device to a PHY device
1058 * @dev: the network device to connect
1059 * @bus_id: the id string of the PHY device to connect
1060 * @handler: callback function for state change notifications
1061 * @interface: PHY device's interface
1062 *
1063 * Description: Convenience function for connecting ethernet
1064 * devices to PHY devices. The default behavior is for
1065 * the PHY infrastructure to handle everything, and only notify
1066 * the connected driver when the link status changes. If you
1067 * don't want, or can't use the provided functionality, you may
1068 * choose to call only the subset of functions which provide
1069 * the desired functionality.
1070 */
phy_connect(struct net_device * dev,const char * bus_id,void (* handler)(struct net_device *),phy_interface_t interface)1071 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1072 void (*handler)(struct net_device *),
1073 phy_interface_t interface)
1074 {
1075 struct phy_device *phydev;
1076 struct device *d;
1077 int rc;
1078
1079 /* Search the list of PHY devices on the mdio bus for the
1080 * PHY with the requested name
1081 */
1082 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1083 if (!d) {
1084 pr_err("PHY %s not found\n", bus_id);
1085 return ERR_PTR(-ENODEV);
1086 }
1087 phydev = to_phy_device(d);
1088
1089 rc = phy_connect_direct(dev, phydev, handler, interface);
1090 put_device(d);
1091 if (rc)
1092 return ERR_PTR(rc);
1093
1094 return phydev;
1095 }
1096 EXPORT_SYMBOL(phy_connect);
1097
1098 /**
1099 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1100 * device
1101 * @phydev: target phy_device struct
1102 */
phy_disconnect(struct phy_device * phydev)1103 void phy_disconnect(struct phy_device *phydev)
1104 {
1105 if (phy_is_started(phydev))
1106 phy_stop(phydev);
1107
1108 if (phy_interrupt_is_valid(phydev))
1109 phy_free_interrupt(phydev);
1110
1111 phydev->adjust_link = NULL;
1112
1113 phy_detach(phydev);
1114 }
1115 EXPORT_SYMBOL(phy_disconnect);
1116
1117 /**
1118 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1119 * @phydev: The PHY device to poll
1120 *
1121 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1122 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1123 * register must be polled until the BMCR_RESET bit clears.
1124 *
1125 * Furthermore, any attempts to write to PHY registers may have no effect
1126 * or even generate MDIO bus errors until this is complete.
1127 *
1128 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1129 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1130 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1131 * effort to support such broken PHYs, this function is separate from the
1132 * standard phy_init_hw() which will zero all the other bits in the BMCR
1133 * and reapply all driver-specific and board-specific fixups.
1134 */
phy_poll_reset(struct phy_device * phydev)1135 static int phy_poll_reset(struct phy_device *phydev)
1136 {
1137 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1138 int ret, val;
1139
1140 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1141 50000, 600000, true);
1142 if (ret)
1143 return ret;
1144 /* Some chips (smsc911x) may still need up to another 1ms after the
1145 * BMCR_RESET bit is cleared before they are usable.
1146 */
1147 msleep(1);
1148 return 0;
1149 }
1150
phy_init_hw(struct phy_device * phydev)1151 int phy_init_hw(struct phy_device *phydev)
1152 {
1153 int ret = 0;
1154
1155 /* Deassert the reset signal */
1156 phy_device_reset(phydev, 0);
1157
1158 if (!phydev->drv)
1159 return 0;
1160
1161 if (phydev->drv->soft_reset) {
1162 ret = phydev->drv->soft_reset(phydev);
1163 /* see comment in genphy_soft_reset for an explanation */
1164 if (!ret)
1165 phydev->suspended = 0;
1166 }
1167
1168 if (ret < 0)
1169 return ret;
1170
1171 ret = phy_scan_fixups(phydev);
1172 if (ret < 0)
1173 return ret;
1174
1175 if (phydev->drv->config_init) {
1176 ret = phydev->drv->config_init(phydev);
1177 if (ret < 0)
1178 return ret;
1179 }
1180
1181 if (phydev->drv->config_intr) {
1182 ret = phydev->drv->config_intr(phydev);
1183 if (ret < 0)
1184 return ret;
1185 }
1186
1187 return 0;
1188 }
1189 EXPORT_SYMBOL(phy_init_hw);
1190
phy_attached_info(struct phy_device * phydev)1191 void phy_attached_info(struct phy_device *phydev)
1192 {
1193 phy_attached_print(phydev, NULL);
1194 }
1195 EXPORT_SYMBOL(phy_attached_info);
1196
1197 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
phy_attached_info_irq(struct phy_device * phydev)1198 char *phy_attached_info_irq(struct phy_device *phydev)
1199 {
1200 char *irq_str;
1201 char irq_num[8];
1202
1203 switch(phydev->irq) {
1204 case PHY_POLL:
1205 irq_str = "POLL";
1206 break;
1207 case PHY_IGNORE_INTERRUPT:
1208 irq_str = "IGNORE";
1209 break;
1210 default:
1211 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1212 irq_str = irq_num;
1213 break;
1214 }
1215
1216 return kasprintf(GFP_KERNEL, "%s", irq_str);
1217 }
1218 EXPORT_SYMBOL(phy_attached_info_irq);
1219
phy_attached_print(struct phy_device * phydev,const char * fmt,...)1220 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1221 {
1222 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1223 char *irq_str = phy_attached_info_irq(phydev);
1224
1225 if (!fmt) {
1226 phydev_info(phydev, ATTACHED_FMT "\n",
1227 drv_name, phydev_name(phydev),
1228 irq_str);
1229 } else {
1230 va_list ap;
1231
1232 phydev_info(phydev, ATTACHED_FMT,
1233 drv_name, phydev_name(phydev),
1234 irq_str);
1235
1236 va_start(ap, fmt);
1237 vprintk(fmt, ap);
1238 va_end(ap);
1239 }
1240 kfree(irq_str);
1241 }
1242 EXPORT_SYMBOL(phy_attached_print);
1243
phy_sysfs_create_links(struct phy_device * phydev)1244 static void phy_sysfs_create_links(struct phy_device *phydev)
1245 {
1246 struct net_device *dev = phydev->attached_dev;
1247 int err;
1248
1249 if (!dev)
1250 return;
1251
1252 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1253 "attached_dev");
1254 if (err)
1255 return;
1256
1257 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1258 &phydev->mdio.dev.kobj,
1259 "phydev");
1260 if (err) {
1261 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1262 kobject_name(&phydev->mdio.dev.kobj),
1263 err);
1264 /* non-fatal - some net drivers can use one netdevice
1265 * with more then one phy
1266 */
1267 }
1268
1269 phydev->sysfs_links = true;
1270 }
1271
1272 static ssize_t
phy_standalone_show(struct device * dev,struct device_attribute * attr,char * buf)1273 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1274 char *buf)
1275 {
1276 struct phy_device *phydev = to_phy_device(dev);
1277
1278 return sprintf(buf, "%d\n", !phydev->attached_dev);
1279 }
1280 static DEVICE_ATTR_RO(phy_standalone);
1281
1282 /**
1283 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1284 * @upstream: pointer to the phy device
1285 * @bus: sfp bus representing cage being attached
1286 *
1287 * This is used to fill in the sfp_upstream_ops .attach member.
1288 */
phy_sfp_attach(void * upstream,struct sfp_bus * bus)1289 void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1290 {
1291 struct phy_device *phydev = upstream;
1292
1293 if (phydev->attached_dev)
1294 phydev->attached_dev->sfp_bus = bus;
1295 phydev->sfp_bus_attached = true;
1296 }
1297 EXPORT_SYMBOL(phy_sfp_attach);
1298
1299 /**
1300 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1301 * @upstream: pointer to the phy device
1302 * @bus: sfp bus representing cage being attached
1303 *
1304 * This is used to fill in the sfp_upstream_ops .detach member.
1305 */
phy_sfp_detach(void * upstream,struct sfp_bus * bus)1306 void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1307 {
1308 struct phy_device *phydev = upstream;
1309
1310 if (phydev->attached_dev)
1311 phydev->attached_dev->sfp_bus = NULL;
1312 phydev->sfp_bus_attached = false;
1313 }
1314 EXPORT_SYMBOL(phy_sfp_detach);
1315
1316 /**
1317 * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1318 * @phydev: Pointer to phy_device
1319 * @ops: SFP's upstream operations
1320 */
phy_sfp_probe(struct phy_device * phydev,const struct sfp_upstream_ops * ops)1321 int phy_sfp_probe(struct phy_device *phydev,
1322 const struct sfp_upstream_ops *ops)
1323 {
1324 struct sfp_bus *bus;
1325 int ret = 0;
1326
1327 if (phydev->mdio.dev.fwnode) {
1328 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1329 if (IS_ERR(bus))
1330 return PTR_ERR(bus);
1331
1332 phydev->sfp_bus = bus;
1333
1334 ret = sfp_bus_add_upstream(bus, phydev, ops);
1335 sfp_bus_put(bus);
1336 }
1337 return ret;
1338 }
1339 EXPORT_SYMBOL(phy_sfp_probe);
1340
1341 /**
1342 * phy_attach_direct - attach a network device to a given PHY device pointer
1343 * @dev: network device to attach
1344 * @phydev: Pointer to phy_device to attach
1345 * @flags: PHY device's dev_flags
1346 * @interface: PHY device's interface
1347 *
1348 * Description: Called by drivers to attach to a particular PHY
1349 * device. The phy_device is found, and properly hooked up
1350 * to the phy_driver. If no driver is attached, then a
1351 * generic driver is used. The phy_device is given a ptr to
1352 * the attaching device, and given a callback for link status
1353 * change. The phy_device is returned to the attaching driver.
1354 * This function takes a reference on the phy device.
1355 */
phy_attach_direct(struct net_device * dev,struct phy_device * phydev,u32 flags,phy_interface_t interface)1356 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1357 u32 flags, phy_interface_t interface)
1358 {
1359 struct mii_bus *bus = phydev->mdio.bus;
1360 struct device *d = &phydev->mdio.dev;
1361 struct module *ndev_owner = NULL;
1362 bool using_genphy = false;
1363 int err;
1364
1365 /* For Ethernet device drivers that register their own MDIO bus, we
1366 * will have bus->owner match ndev_mod, so we do not want to increment
1367 * our own module->refcnt here, otherwise we would not be able to
1368 * unload later on.
1369 */
1370 if (dev)
1371 ndev_owner = dev->dev.parent->driver->owner;
1372 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1373 phydev_err(phydev, "failed to get the bus module\n");
1374 return -EIO;
1375 }
1376
1377 get_device(d);
1378
1379 /* Assume that if there is no driver, that it doesn't
1380 * exist, and we should use the genphy driver.
1381 */
1382 if (!d->driver) {
1383 if (phydev->is_c45)
1384 d->driver = &genphy_c45_driver.mdiodrv.driver;
1385 else
1386 d->driver = &genphy_driver.mdiodrv.driver;
1387
1388 using_genphy = true;
1389 }
1390
1391 if (!try_module_get(d->driver->owner)) {
1392 phydev_err(phydev, "failed to get the device driver module\n");
1393 err = -EIO;
1394 goto error_put_device;
1395 }
1396
1397 if (using_genphy) {
1398 err = d->driver->probe(d);
1399 if (err >= 0)
1400 err = device_bind_driver(d);
1401
1402 if (err)
1403 goto error_module_put;
1404 }
1405
1406 if (phydev->attached_dev) {
1407 dev_err(&dev->dev, "PHY already attached\n");
1408 err = -EBUSY;
1409 goto error;
1410 }
1411
1412 phydev->phy_link_change = phy_link_change;
1413 if (dev) {
1414 phydev->attached_dev = dev;
1415 dev->phydev = phydev;
1416
1417 if (phydev->sfp_bus_attached)
1418 dev->sfp_bus = phydev->sfp_bus;
1419 }
1420
1421 /* Some Ethernet drivers try to connect to a PHY device before
1422 * calling register_netdevice() -> netdev_register_kobject() and
1423 * does the dev->dev.kobj initialization. Here we only check for
1424 * success which indicates that the network device kobject is
1425 * ready. Once we do that we still need to keep track of whether
1426 * links were successfully set up or not for phy_detach() to
1427 * remove them accordingly.
1428 */
1429 phydev->sysfs_links = false;
1430
1431 phy_sysfs_create_links(phydev);
1432
1433 if (!phydev->attached_dev) {
1434 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1435 &dev_attr_phy_standalone.attr);
1436 if (err)
1437 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1438 }
1439
1440 phydev->dev_flags |= flags;
1441
1442 phydev->interface = interface;
1443
1444 phydev->state = PHY_READY;
1445
1446 /* Port is set to PORT_TP by default and the actual PHY driver will set
1447 * it to different value depending on the PHY configuration. If we have
1448 * the generic PHY driver we can't figure it out, thus set the old
1449 * legacy PORT_MII value.
1450 */
1451 if (using_genphy)
1452 phydev->port = PORT_MII;
1453
1454 /* Initial carrier state is off as the phy is about to be
1455 * (re)initialized.
1456 */
1457 if (dev)
1458 netif_carrier_off(phydev->attached_dev);
1459
1460 /* Do initial configuration here, now that
1461 * we have certain key parameters
1462 * (dev_flags and interface)
1463 */
1464 err = phy_init_hw(phydev);
1465 if (err)
1466 goto error;
1467
1468 err = phy_disable_interrupts(phydev);
1469 if (err)
1470 return err;
1471
1472 phy_resume(phydev);
1473 phy_led_triggers_register(phydev);
1474
1475 return err;
1476
1477 error:
1478 /* phy_detach() does all of the cleanup below */
1479 phy_detach(phydev);
1480 return err;
1481
1482 error_module_put:
1483 module_put(d->driver->owner);
1484 d->driver = NULL;
1485 error_put_device:
1486 put_device(d);
1487 if (ndev_owner != bus->owner)
1488 module_put(bus->owner);
1489 return err;
1490 }
1491 EXPORT_SYMBOL(phy_attach_direct);
1492
1493 /**
1494 * phy_attach - attach a network device to a particular PHY device
1495 * @dev: network device to attach
1496 * @bus_id: Bus ID of PHY device to attach
1497 * @interface: PHY device's interface
1498 *
1499 * Description: Same as phy_attach_direct() except that a PHY bus_id
1500 * string is passed instead of a pointer to a struct phy_device.
1501 */
phy_attach(struct net_device * dev,const char * bus_id,phy_interface_t interface)1502 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1503 phy_interface_t interface)
1504 {
1505 struct bus_type *bus = &mdio_bus_type;
1506 struct phy_device *phydev;
1507 struct device *d;
1508 int rc;
1509
1510 if (!dev)
1511 return ERR_PTR(-EINVAL);
1512
1513 /* Search the list of PHY devices on the mdio bus for the
1514 * PHY with the requested name
1515 */
1516 d = bus_find_device_by_name(bus, NULL, bus_id);
1517 if (!d) {
1518 pr_err("PHY %s not found\n", bus_id);
1519 return ERR_PTR(-ENODEV);
1520 }
1521 phydev = to_phy_device(d);
1522
1523 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1524 put_device(d);
1525 if (rc)
1526 return ERR_PTR(rc);
1527
1528 return phydev;
1529 }
1530 EXPORT_SYMBOL(phy_attach);
1531
phy_driver_is_genphy_kind(struct phy_device * phydev,struct device_driver * driver)1532 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1533 struct device_driver *driver)
1534 {
1535 struct device *d = &phydev->mdio.dev;
1536 bool ret = false;
1537
1538 if (!phydev->drv)
1539 return ret;
1540
1541 get_device(d);
1542 ret = d->driver == driver;
1543 put_device(d);
1544
1545 return ret;
1546 }
1547
phy_driver_is_genphy(struct phy_device * phydev)1548 bool phy_driver_is_genphy(struct phy_device *phydev)
1549 {
1550 return phy_driver_is_genphy_kind(phydev,
1551 &genphy_driver.mdiodrv.driver);
1552 }
1553 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1554
phy_driver_is_genphy_10g(struct phy_device * phydev)1555 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1556 {
1557 return phy_driver_is_genphy_kind(phydev,
1558 &genphy_c45_driver.mdiodrv.driver);
1559 }
1560 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1561
1562 /**
1563 * phy_package_join - join a common PHY group
1564 * @phydev: target phy_device struct
1565 * @addr: cookie and PHY address for global register access
1566 * @priv_size: if non-zero allocate this amount of bytes for private data
1567 *
1568 * This joins a PHY group and provides a shared storage for all phydevs in
1569 * this group. This is intended to be used for packages which contain
1570 * more than one PHY, for example a quad PHY transceiver.
1571 *
1572 * The addr parameter serves as a cookie which has to have the same value
1573 * for all members of one group and as a PHY address to access generic
1574 * registers of a PHY package. Usually, one of the PHY addresses of the
1575 * different PHYs in the package provides access to these global registers.
1576 * The address which is given here, will be used in the phy_package_read()
1577 * and phy_package_write() convenience functions. If your PHY doesn't have
1578 * global registers you can just pick any of the PHY addresses.
1579 *
1580 * This will set the shared pointer of the phydev to the shared storage.
1581 * If this is the first call for a this cookie the shared storage will be
1582 * allocated. If priv_size is non-zero, the given amount of bytes are
1583 * allocated for the priv member.
1584 *
1585 * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
1586 * with the same cookie but a different priv_size is an error.
1587 */
phy_package_join(struct phy_device * phydev,int addr,size_t priv_size)1588 int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
1589 {
1590 struct mii_bus *bus = phydev->mdio.bus;
1591 struct phy_package_shared *shared;
1592 int ret;
1593
1594 if (addr < 0 || addr >= PHY_MAX_ADDR)
1595 return -EINVAL;
1596
1597 mutex_lock(&bus->shared_lock);
1598 shared = bus->shared[addr];
1599 if (!shared) {
1600 ret = -ENOMEM;
1601 shared = kzalloc(sizeof(*shared), GFP_KERNEL);
1602 if (!shared)
1603 goto err_unlock;
1604 if (priv_size) {
1605 shared->priv = kzalloc(priv_size, GFP_KERNEL);
1606 if (!shared->priv)
1607 goto err_free;
1608 shared->priv_size = priv_size;
1609 }
1610 shared->addr = addr;
1611 refcount_set(&shared->refcnt, 1);
1612 bus->shared[addr] = shared;
1613 } else {
1614 ret = -EINVAL;
1615 if (priv_size && priv_size != shared->priv_size)
1616 goto err_unlock;
1617 refcount_inc(&shared->refcnt);
1618 }
1619 mutex_unlock(&bus->shared_lock);
1620
1621 phydev->shared = shared;
1622
1623 return 0;
1624
1625 err_free:
1626 kfree(shared);
1627 err_unlock:
1628 mutex_unlock(&bus->shared_lock);
1629 return ret;
1630 }
1631 EXPORT_SYMBOL_GPL(phy_package_join);
1632
1633 /**
1634 * phy_package_leave - leave a common PHY group
1635 * @phydev: target phy_device struct
1636 *
1637 * This leaves a PHY group created by phy_package_join(). If this phydev
1638 * was the last user of the shared data between the group, this data is
1639 * freed. Resets the phydev->shared pointer to NULL.
1640 */
phy_package_leave(struct phy_device * phydev)1641 void phy_package_leave(struct phy_device *phydev)
1642 {
1643 struct phy_package_shared *shared = phydev->shared;
1644 struct mii_bus *bus = phydev->mdio.bus;
1645
1646 if (!shared)
1647 return;
1648
1649 if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
1650 bus->shared[shared->addr] = NULL;
1651 mutex_unlock(&bus->shared_lock);
1652 kfree(shared->priv);
1653 kfree(shared);
1654 }
1655
1656 phydev->shared = NULL;
1657 }
1658 EXPORT_SYMBOL_GPL(phy_package_leave);
1659
devm_phy_package_leave(struct device * dev,void * res)1660 static void devm_phy_package_leave(struct device *dev, void *res)
1661 {
1662 phy_package_leave(*(struct phy_device **)res);
1663 }
1664
1665 /**
1666 * devm_phy_package_join - resource managed phy_package_join()
1667 * @dev: device that is registering this PHY package
1668 * @phydev: target phy_device struct
1669 * @addr: cookie and PHY address for global register access
1670 * @priv_size: if non-zero allocate this amount of bytes for private data
1671 *
1672 * Managed phy_package_join(). Shared storage fetched by this function,
1673 * phy_package_leave() is automatically called on driver detach. See
1674 * phy_package_join() for more information.
1675 */
devm_phy_package_join(struct device * dev,struct phy_device * phydev,int addr,size_t priv_size)1676 int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1677 int addr, size_t priv_size)
1678 {
1679 struct phy_device **ptr;
1680 int ret;
1681
1682 ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
1683 GFP_KERNEL);
1684 if (!ptr)
1685 return -ENOMEM;
1686
1687 ret = phy_package_join(phydev, addr, priv_size);
1688
1689 if (!ret) {
1690 *ptr = phydev;
1691 devres_add(dev, ptr);
1692 } else {
1693 devres_free(ptr);
1694 }
1695
1696 return ret;
1697 }
1698 EXPORT_SYMBOL_GPL(devm_phy_package_join);
1699
1700 /**
1701 * phy_detach - detach a PHY device from its network device
1702 * @phydev: target phy_device struct
1703 *
1704 * This detaches the phy device from its network device and the phy
1705 * driver, and drops the reference count taken in phy_attach_direct().
1706 */
phy_detach(struct phy_device * phydev)1707 void phy_detach(struct phy_device *phydev)
1708 {
1709 struct net_device *dev = phydev->attached_dev;
1710 struct module *ndev_owner = NULL;
1711 struct mii_bus *bus;
1712
1713 if (phydev->sysfs_links) {
1714 if (dev)
1715 sysfs_remove_link(&dev->dev.kobj, "phydev");
1716 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1717 }
1718
1719 if (!phydev->attached_dev)
1720 sysfs_remove_file(&phydev->mdio.dev.kobj,
1721 &dev_attr_phy_standalone.attr);
1722
1723 phy_suspend(phydev);
1724 if (dev) {
1725 phydev->attached_dev->phydev = NULL;
1726 phydev->attached_dev = NULL;
1727 }
1728 phydev->phylink = NULL;
1729
1730 phy_led_triggers_unregister(phydev);
1731
1732 if (phydev->mdio.dev.driver)
1733 module_put(phydev->mdio.dev.driver->owner);
1734
1735 /* If the device had no specific driver before (i.e. - it
1736 * was using the generic driver), we unbind the device
1737 * from the generic driver so that there's a chance a
1738 * real driver could be loaded
1739 */
1740 if (phy_driver_is_genphy(phydev) ||
1741 phy_driver_is_genphy_10g(phydev))
1742 device_release_driver(&phydev->mdio.dev);
1743
1744 /* Assert the reset signal */
1745 phy_device_reset(phydev, 1);
1746
1747 /*
1748 * The phydev might go away on the put_device() below, so avoid
1749 * a use-after-free bug by reading the underlying bus first.
1750 */
1751 bus = phydev->mdio.bus;
1752
1753 put_device(&phydev->mdio.dev);
1754 if (dev)
1755 ndev_owner = dev->dev.parent->driver->owner;
1756 if (ndev_owner != bus->owner)
1757 module_put(bus->owner);
1758 }
1759 EXPORT_SYMBOL(phy_detach);
1760
phy_suspend(struct phy_device * phydev)1761 int phy_suspend(struct phy_device *phydev)
1762 {
1763 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1764 struct net_device *netdev = phydev->attached_dev;
1765 struct phy_driver *phydrv = phydev->drv;
1766 int ret;
1767
1768 if (phydev->suspended)
1769 return 0;
1770
1771 /* If the device has WOL enabled, we cannot suspend the PHY */
1772 phy_ethtool_get_wol(phydev, &wol);
1773 if (wol.wolopts || (netdev && netdev->wol_enabled))
1774 return -EBUSY;
1775
1776 if (!phydrv || !phydrv->suspend)
1777 return 0;
1778
1779 ret = phydrv->suspend(phydev);
1780 if (!ret)
1781 phydev->suspended = true;
1782
1783 return ret;
1784 }
1785 EXPORT_SYMBOL(phy_suspend);
1786
__phy_resume(struct phy_device * phydev)1787 int __phy_resume(struct phy_device *phydev)
1788 {
1789 struct phy_driver *phydrv = phydev->drv;
1790 int ret;
1791
1792 WARN_ON(!mutex_is_locked(&phydev->lock));
1793
1794 if (!phydrv || !phydrv->resume)
1795 return 0;
1796
1797 ret = phydrv->resume(phydev);
1798 if (!ret)
1799 phydev->suspended = false;
1800
1801 return ret;
1802 }
1803 EXPORT_SYMBOL(__phy_resume);
1804
phy_resume(struct phy_device * phydev)1805 int phy_resume(struct phy_device *phydev)
1806 {
1807 int ret;
1808
1809 mutex_lock(&phydev->lock);
1810 ret = __phy_resume(phydev);
1811 mutex_unlock(&phydev->lock);
1812
1813 return ret;
1814 }
1815 EXPORT_SYMBOL(phy_resume);
1816
phy_loopback(struct phy_device * phydev,bool enable)1817 int phy_loopback(struct phy_device *phydev, bool enable)
1818 {
1819 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1820 int ret = 0;
1821
1822 mutex_lock(&phydev->lock);
1823
1824 if (enable && phydev->loopback_enabled) {
1825 ret = -EBUSY;
1826 goto out;
1827 }
1828
1829 if (!enable && !phydev->loopback_enabled) {
1830 ret = -EINVAL;
1831 goto out;
1832 }
1833
1834 if (phydev->drv && phydrv->set_loopback)
1835 ret = phydrv->set_loopback(phydev, enable);
1836 else
1837 ret = -EOPNOTSUPP;
1838
1839 if (ret)
1840 goto out;
1841
1842 phydev->loopback_enabled = enable;
1843
1844 out:
1845 mutex_unlock(&phydev->lock);
1846 return ret;
1847 }
1848 EXPORT_SYMBOL(phy_loopback);
1849
1850 /**
1851 * phy_reset_after_clk_enable - perform a PHY reset if needed
1852 * @phydev: target phy_device struct
1853 *
1854 * Description: Some PHYs are known to need a reset after their refclk was
1855 * enabled. This function evaluates the flags and perform the reset if it's
1856 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1857 * was reset.
1858 */
phy_reset_after_clk_enable(struct phy_device * phydev)1859 int phy_reset_after_clk_enable(struct phy_device *phydev)
1860 {
1861 if (!phydev || !phydev->drv)
1862 return -ENODEV;
1863
1864 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1865 phy_device_reset(phydev, 1);
1866 phy_device_reset(phydev, 0);
1867 return 1;
1868 }
1869
1870 return 0;
1871 }
1872 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1873
1874 /* Generic PHY support and helper functions */
1875
1876 /**
1877 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1878 * @phydev: target phy_device struct
1879 *
1880 * Description: Writes MII_ADVERTISE with the appropriate values,
1881 * after sanitizing the values to make sure we only advertise
1882 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1883 * hasn't changed, and > 0 if it has changed.
1884 */
genphy_config_advert(struct phy_device * phydev)1885 static int genphy_config_advert(struct phy_device *phydev)
1886 {
1887 int err, bmsr, changed = 0;
1888 u32 adv;
1889
1890 /* Only allow advertising what this PHY supports */
1891 linkmode_and(phydev->advertising, phydev->advertising,
1892 phydev->supported);
1893
1894 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1895
1896 /* Setup standard advertisement */
1897 err = phy_modify_changed(phydev, MII_ADVERTISE,
1898 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1899 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1900 adv);
1901 if (err < 0)
1902 return err;
1903 if (err > 0)
1904 changed = 1;
1905
1906 bmsr = phy_read(phydev, MII_BMSR);
1907 if (bmsr < 0)
1908 return bmsr;
1909
1910 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1911 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1912 * logical 1.
1913 */
1914 if (!(bmsr & BMSR_ESTATEN))
1915 return changed;
1916
1917 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1918
1919 err = phy_modify_changed(phydev, MII_CTRL1000,
1920 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1921 adv);
1922 if (err < 0)
1923 return err;
1924 if (err > 0)
1925 changed = 1;
1926
1927 return changed;
1928 }
1929
1930 /**
1931 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1932 * @phydev: target phy_device struct
1933 *
1934 * Description: Writes MII_ADVERTISE with the appropriate values,
1935 * after sanitizing the values to make sure we only advertise
1936 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1937 * hasn't changed, and > 0 if it has changed. This function is intended
1938 * for Clause 37 1000Base-X mode.
1939 */
genphy_c37_config_advert(struct phy_device * phydev)1940 static int genphy_c37_config_advert(struct phy_device *phydev)
1941 {
1942 u16 adv = 0;
1943
1944 /* Only allow advertising what this PHY supports */
1945 linkmode_and(phydev->advertising, phydev->advertising,
1946 phydev->supported);
1947
1948 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1949 phydev->advertising))
1950 adv |= ADVERTISE_1000XFULL;
1951 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1952 phydev->advertising))
1953 adv |= ADVERTISE_1000XPAUSE;
1954 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1955 phydev->advertising))
1956 adv |= ADVERTISE_1000XPSE_ASYM;
1957
1958 return phy_modify_changed(phydev, MII_ADVERTISE,
1959 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1960 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1961 adv);
1962 }
1963
1964 /**
1965 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1966 * @phydev: target phy_device struct
1967 *
1968 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1969 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1970 * changed, and 1 if it has changed.
1971 */
genphy_config_eee_advert(struct phy_device * phydev)1972 int genphy_config_eee_advert(struct phy_device *phydev)
1973 {
1974 int err;
1975
1976 /* Nothing to disable */
1977 if (!phydev->eee_broken_modes)
1978 return 0;
1979
1980 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1981 phydev->eee_broken_modes, 0);
1982 /* If the call failed, we assume that EEE is not supported */
1983 return err < 0 ? 0 : err;
1984 }
1985 EXPORT_SYMBOL(genphy_config_eee_advert);
1986
1987 /**
1988 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1989 * @phydev: target phy_device struct
1990 *
1991 * Description: Configures MII_BMCR to force speed/duplex
1992 * to the values in phydev. Assumes that the values are valid.
1993 * Please see phy_sanitize_settings().
1994 */
genphy_setup_forced(struct phy_device * phydev)1995 int genphy_setup_forced(struct phy_device *phydev)
1996 {
1997 u16 ctl = 0;
1998
1999 phydev->pause = 0;
2000 phydev->asym_pause = 0;
2001
2002 if (SPEED_1000 == phydev->speed)
2003 ctl |= BMCR_SPEED1000;
2004 else if (SPEED_100 == phydev->speed)
2005 ctl |= BMCR_SPEED100;
2006
2007 if (DUPLEX_FULL == phydev->duplex)
2008 ctl |= BMCR_FULLDPLX;
2009
2010 return phy_modify(phydev, MII_BMCR,
2011 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
2012 }
2013 EXPORT_SYMBOL(genphy_setup_forced);
2014
genphy_setup_master_slave(struct phy_device * phydev)2015 static int genphy_setup_master_slave(struct phy_device *phydev)
2016 {
2017 u16 ctl = 0;
2018
2019 if (!phydev->is_gigabit_capable)
2020 return 0;
2021
2022 switch (phydev->master_slave_set) {
2023 case MASTER_SLAVE_CFG_MASTER_PREFERRED:
2024 ctl |= CTL1000_PREFER_MASTER;
2025 break;
2026 case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2027 break;
2028 case MASTER_SLAVE_CFG_MASTER_FORCE:
2029 ctl |= CTL1000_AS_MASTER;
2030 fallthrough;
2031 case MASTER_SLAVE_CFG_SLAVE_FORCE:
2032 ctl |= CTL1000_ENABLE_MASTER;
2033 break;
2034 case MASTER_SLAVE_CFG_UNKNOWN:
2035 case MASTER_SLAVE_CFG_UNSUPPORTED:
2036 return 0;
2037 default:
2038 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2039 return -EOPNOTSUPP;
2040 }
2041
2042 return phy_modify_changed(phydev, MII_CTRL1000,
2043 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2044 CTL1000_PREFER_MASTER), ctl);
2045 }
2046
genphy_read_master_slave(struct phy_device * phydev)2047 static int genphy_read_master_slave(struct phy_device *phydev)
2048 {
2049 int cfg, state;
2050 int val;
2051
2052 if (!phydev->is_gigabit_capable) {
2053 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2054 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2055 return 0;
2056 }
2057
2058 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2059 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2060
2061 val = phy_read(phydev, MII_CTRL1000);
2062 if (val < 0)
2063 return val;
2064
2065 if (val & CTL1000_ENABLE_MASTER) {
2066 if (val & CTL1000_AS_MASTER)
2067 cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2068 else
2069 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2070 } else {
2071 if (val & CTL1000_PREFER_MASTER)
2072 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2073 else
2074 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2075 }
2076
2077 val = phy_read(phydev, MII_STAT1000);
2078 if (val < 0)
2079 return val;
2080
2081 if (val & LPA_1000MSFAIL) {
2082 state = MASTER_SLAVE_STATE_ERR;
2083 } else if (phydev->link) {
2084 /* this bits are valid only for active link */
2085 if (val & LPA_1000MSRES)
2086 state = MASTER_SLAVE_STATE_MASTER;
2087 else
2088 state = MASTER_SLAVE_STATE_SLAVE;
2089 } else {
2090 state = MASTER_SLAVE_STATE_UNKNOWN;
2091 }
2092
2093 phydev->master_slave_get = cfg;
2094 phydev->master_slave_state = state;
2095
2096 return 0;
2097 }
2098
2099 /**
2100 * genphy_restart_aneg - Enable and Restart Autonegotiation
2101 * @phydev: target phy_device struct
2102 */
genphy_restart_aneg(struct phy_device * phydev)2103 int genphy_restart_aneg(struct phy_device *phydev)
2104 {
2105 /* Don't isolate the PHY if we're negotiating */
2106 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2107 BMCR_ANENABLE | BMCR_ANRESTART);
2108 }
2109 EXPORT_SYMBOL(genphy_restart_aneg);
2110
2111 /**
2112 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2113 * @phydev: target phy_device struct
2114 * @restart: whether aneg restart is requested
2115 *
2116 * Check, and restart auto-negotiation if needed.
2117 */
genphy_check_and_restart_aneg(struct phy_device * phydev,bool restart)2118 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2119 {
2120 int ret;
2121
2122 if (!restart) {
2123 /* Advertisement hasn't changed, but maybe aneg was never on to
2124 * begin with? Or maybe phy was isolated?
2125 */
2126 ret = phy_read(phydev, MII_BMCR);
2127 if (ret < 0)
2128 return ret;
2129
2130 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2131 restart = true;
2132 }
2133
2134 if (restart)
2135 return genphy_restart_aneg(phydev);
2136
2137 return 0;
2138 }
2139 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2140
2141 /**
2142 * __genphy_config_aneg - restart auto-negotiation or write BMCR
2143 * @phydev: target phy_device struct
2144 * @changed: whether autoneg is requested
2145 *
2146 * Description: If auto-negotiation is enabled, we configure the
2147 * advertising, and then restart auto-negotiation. If it is not
2148 * enabled, then we write the BMCR.
2149 */
__genphy_config_aneg(struct phy_device * phydev,bool changed)2150 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2151 {
2152 int err;
2153
2154 if (genphy_config_eee_advert(phydev))
2155 changed = true;
2156
2157 err = genphy_setup_master_slave(phydev);
2158 if (err < 0)
2159 return err;
2160 else if (err)
2161 changed = true;
2162
2163 if (AUTONEG_ENABLE != phydev->autoneg)
2164 return genphy_setup_forced(phydev);
2165
2166 err = genphy_config_advert(phydev);
2167 if (err < 0) /* error */
2168 return err;
2169 else if (err)
2170 changed = true;
2171
2172 return genphy_check_and_restart_aneg(phydev, changed);
2173 }
2174 EXPORT_SYMBOL(__genphy_config_aneg);
2175
2176 /**
2177 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2178 * @phydev: target phy_device struct
2179 *
2180 * Description: If auto-negotiation is enabled, we configure the
2181 * advertising, and then restart auto-negotiation. If it is not
2182 * enabled, then we write the BMCR. This function is intended
2183 * for use with Clause 37 1000Base-X mode.
2184 */
genphy_c37_config_aneg(struct phy_device * phydev)2185 int genphy_c37_config_aneg(struct phy_device *phydev)
2186 {
2187 int err, changed;
2188
2189 if (phydev->autoneg != AUTONEG_ENABLE)
2190 return genphy_setup_forced(phydev);
2191
2192 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2193 BMCR_SPEED1000);
2194 if (err)
2195 return err;
2196
2197 changed = genphy_c37_config_advert(phydev);
2198 if (changed < 0) /* error */
2199 return changed;
2200
2201 if (!changed) {
2202 /* Advertisement hasn't changed, but maybe aneg was never on to
2203 * begin with? Or maybe phy was isolated?
2204 */
2205 int ctl = phy_read(phydev, MII_BMCR);
2206
2207 if (ctl < 0)
2208 return ctl;
2209
2210 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2211 changed = 1; /* do restart aneg */
2212 }
2213
2214 /* Only restart aneg if we are advertising something different
2215 * than we were before.
2216 */
2217 if (changed > 0)
2218 return genphy_restart_aneg(phydev);
2219
2220 return 0;
2221 }
2222 EXPORT_SYMBOL(genphy_c37_config_aneg);
2223
2224 /**
2225 * genphy_aneg_done - return auto-negotiation status
2226 * @phydev: target phy_device struct
2227 *
2228 * Description: Reads the status register and returns 0 either if
2229 * auto-negotiation is incomplete, or if there was an error.
2230 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2231 */
genphy_aneg_done(struct phy_device * phydev)2232 int genphy_aneg_done(struct phy_device *phydev)
2233 {
2234 int retval = phy_read(phydev, MII_BMSR);
2235
2236 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2237 }
2238 EXPORT_SYMBOL(genphy_aneg_done);
2239
2240 /**
2241 * genphy_update_link - update link status in @phydev
2242 * @phydev: target phy_device struct
2243 *
2244 * Description: Update the value in phydev->link to reflect the
2245 * current link value. In order to do this, we need to read
2246 * the status register twice, keeping the second value.
2247 */
genphy_update_link(struct phy_device * phydev)2248 int genphy_update_link(struct phy_device *phydev)
2249 {
2250 int status = 0, bmcr;
2251
2252 bmcr = phy_read(phydev, MII_BMCR);
2253 if (bmcr < 0)
2254 return bmcr;
2255
2256 /* Autoneg is being started, therefore disregard BMSR value and
2257 * report link as down.
2258 */
2259 if (bmcr & BMCR_ANRESTART)
2260 goto done;
2261
2262 /* The link state is latched low so that momentary link
2263 * drops can be detected. Do not double-read the status
2264 * in polling mode to detect such short link drops except
2265 * the link was already down.
2266 */
2267 if (!phy_polling_mode(phydev) || !phydev->link) {
2268 status = phy_read(phydev, MII_BMSR);
2269 if (status < 0)
2270 return status;
2271 else if (status & BMSR_LSTATUS)
2272 goto done;
2273 }
2274
2275 /* Read link and autonegotiation status */
2276 status = phy_read(phydev, MII_BMSR);
2277 if (status < 0)
2278 return status;
2279 done:
2280 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2281 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2282
2283 /* Consider the case that autoneg was started and "aneg complete"
2284 * bit has been reset, but "link up" bit not yet.
2285 */
2286 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2287 phydev->link = 0;
2288
2289 return 0;
2290 }
2291 EXPORT_SYMBOL(genphy_update_link);
2292
genphy_read_lpa(struct phy_device * phydev)2293 int genphy_read_lpa(struct phy_device *phydev)
2294 {
2295 int lpa, lpagb;
2296
2297 if (phydev->autoneg == AUTONEG_ENABLE) {
2298 if (!phydev->autoneg_complete) {
2299 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2300 0);
2301 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2302 return 0;
2303 }
2304
2305 if (phydev->is_gigabit_capable) {
2306 lpagb = phy_read(phydev, MII_STAT1000);
2307 if (lpagb < 0)
2308 return lpagb;
2309
2310 if (lpagb & LPA_1000MSFAIL) {
2311 int adv = phy_read(phydev, MII_CTRL1000);
2312
2313 if (adv < 0)
2314 return adv;
2315
2316 if (adv & CTL1000_ENABLE_MASTER)
2317 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2318 else
2319 phydev_err(phydev, "Master/Slave resolution failed\n");
2320 return -ENOLINK;
2321 }
2322
2323 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2324 lpagb);
2325 }
2326
2327 lpa = phy_read(phydev, MII_LPA);
2328 if (lpa < 0)
2329 return lpa;
2330
2331 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2332 } else {
2333 linkmode_zero(phydev->lp_advertising);
2334 }
2335
2336 return 0;
2337 }
2338 EXPORT_SYMBOL(genphy_read_lpa);
2339
2340 /**
2341 * genphy_read_status_fixed - read the link parameters for !aneg mode
2342 * @phydev: target phy_device struct
2343 *
2344 * Read the current duplex and speed state for a PHY operating with
2345 * autonegotiation disabled.
2346 */
genphy_read_status_fixed(struct phy_device * phydev)2347 int genphy_read_status_fixed(struct phy_device *phydev)
2348 {
2349 int bmcr = phy_read(phydev, MII_BMCR);
2350
2351 if (bmcr < 0)
2352 return bmcr;
2353
2354 if (bmcr & BMCR_FULLDPLX)
2355 phydev->duplex = DUPLEX_FULL;
2356 else
2357 phydev->duplex = DUPLEX_HALF;
2358
2359 if (bmcr & BMCR_SPEED1000)
2360 phydev->speed = SPEED_1000;
2361 else if (bmcr & BMCR_SPEED100)
2362 phydev->speed = SPEED_100;
2363 else
2364 phydev->speed = SPEED_10;
2365
2366 return 0;
2367 }
2368 EXPORT_SYMBOL(genphy_read_status_fixed);
2369
2370 /**
2371 * genphy_read_status - check the link status and update current link state
2372 * @phydev: target phy_device struct
2373 *
2374 * Description: Check the link, then figure out the current state
2375 * by comparing what we advertise with what the link partner
2376 * advertises. Start by checking the gigabit possibilities,
2377 * then move on to 10/100.
2378 */
genphy_read_status(struct phy_device * phydev)2379 int genphy_read_status(struct phy_device *phydev)
2380 {
2381 int err, old_link = phydev->link;
2382
2383 /* Update the link, but return if there was an error */
2384 err = genphy_update_link(phydev);
2385 if (err)
2386 return err;
2387
2388 /* why bother the PHY if nothing can have changed */
2389 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2390 return 0;
2391
2392 phydev->speed = SPEED_UNKNOWN;
2393 phydev->duplex = DUPLEX_UNKNOWN;
2394 phydev->pause = 0;
2395 phydev->asym_pause = 0;
2396
2397 err = genphy_read_master_slave(phydev);
2398 if (err < 0)
2399 return err;
2400
2401 err = genphy_read_lpa(phydev);
2402 if (err < 0)
2403 return err;
2404
2405 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2406 phy_resolve_aneg_linkmode(phydev);
2407 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2408 err = genphy_read_status_fixed(phydev);
2409 if (err < 0)
2410 return err;
2411 }
2412
2413 return 0;
2414 }
2415 EXPORT_SYMBOL(genphy_read_status);
2416
2417 /**
2418 * genphy_c37_read_status - check the link status and update current link state
2419 * @phydev: target phy_device struct
2420 *
2421 * Description: Check the link, then figure out the current state
2422 * by comparing what we advertise with what the link partner
2423 * advertises. This function is for Clause 37 1000Base-X mode.
2424 */
genphy_c37_read_status(struct phy_device * phydev)2425 int genphy_c37_read_status(struct phy_device *phydev)
2426 {
2427 int lpa, err, old_link = phydev->link;
2428
2429 /* Update the link, but return if there was an error */
2430 err = genphy_update_link(phydev);
2431 if (err)
2432 return err;
2433
2434 /* why bother the PHY if nothing can have changed */
2435 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2436 return 0;
2437
2438 phydev->duplex = DUPLEX_UNKNOWN;
2439 phydev->pause = 0;
2440 phydev->asym_pause = 0;
2441
2442 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2443 lpa = phy_read(phydev, MII_LPA);
2444 if (lpa < 0)
2445 return lpa;
2446
2447 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2448 phydev->lp_advertising, lpa & LPA_LPACK);
2449 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2450 phydev->lp_advertising, lpa & LPA_1000XFULL);
2451 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2452 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2453 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2454 phydev->lp_advertising,
2455 lpa & LPA_1000XPAUSE_ASYM);
2456
2457 phy_resolve_aneg_linkmode(phydev);
2458 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2459 int bmcr = phy_read(phydev, MII_BMCR);
2460
2461 if (bmcr < 0)
2462 return bmcr;
2463
2464 if (bmcr & BMCR_FULLDPLX)
2465 phydev->duplex = DUPLEX_FULL;
2466 else
2467 phydev->duplex = DUPLEX_HALF;
2468 }
2469
2470 return 0;
2471 }
2472 EXPORT_SYMBOL(genphy_c37_read_status);
2473
2474 /**
2475 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2476 * @phydev: target phy_device struct
2477 *
2478 * Description: Perform a software PHY reset using the standard
2479 * BMCR_RESET bit and poll for the reset bit to be cleared.
2480 *
2481 * Returns: 0 on success, < 0 on failure
2482 */
genphy_soft_reset(struct phy_device * phydev)2483 int genphy_soft_reset(struct phy_device *phydev)
2484 {
2485 u16 res = BMCR_RESET;
2486 int ret;
2487
2488 if (phydev->autoneg == AUTONEG_ENABLE)
2489 res |= BMCR_ANRESTART;
2490
2491 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2492 if (ret < 0)
2493 return ret;
2494
2495 /* Clause 22 states that setting bit BMCR_RESET sets control registers
2496 * to their default value. Therefore the POWER DOWN bit is supposed to
2497 * be cleared after soft reset.
2498 */
2499 phydev->suspended = 0;
2500
2501 ret = phy_poll_reset(phydev);
2502 if (ret)
2503 return ret;
2504
2505 /* BMCR may be reset to defaults */
2506 if (phydev->autoneg == AUTONEG_DISABLE)
2507 ret = genphy_setup_forced(phydev);
2508
2509 return ret;
2510 }
2511 EXPORT_SYMBOL(genphy_soft_reset);
2512
2513 /**
2514 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2515 * @phydev: target phy_device struct
2516 *
2517 * Description: Reads the PHY's abilities and populates
2518 * phydev->supported accordingly.
2519 *
2520 * Returns: 0 on success, < 0 on failure
2521 */
genphy_read_abilities(struct phy_device * phydev)2522 int genphy_read_abilities(struct phy_device *phydev)
2523 {
2524 int val;
2525
2526 linkmode_set_bit_array(phy_basic_ports_array,
2527 ARRAY_SIZE(phy_basic_ports_array),
2528 phydev->supported);
2529
2530 val = phy_read(phydev, MII_BMSR);
2531 if (val < 0)
2532 return val;
2533
2534 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2535 val & BMSR_ANEGCAPABLE);
2536
2537 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2538 val & BMSR_100FULL);
2539 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2540 val & BMSR_100HALF);
2541 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2542 val & BMSR_10FULL);
2543 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2544 val & BMSR_10HALF);
2545
2546 if (val & BMSR_ESTATEN) {
2547 val = phy_read(phydev, MII_ESTATUS);
2548 if (val < 0)
2549 return val;
2550
2551 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2552 phydev->supported, val & ESTATUS_1000_TFULL);
2553 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2554 phydev->supported, val & ESTATUS_1000_THALF);
2555 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2556 phydev->supported, val & ESTATUS_1000_XFULL);
2557 }
2558
2559 return 0;
2560 }
2561 EXPORT_SYMBOL(genphy_read_abilities);
2562
2563 /* This is used for the phy device which doesn't support the MMD extended
2564 * register access, but it does have side effect when we are trying to access
2565 * the MMD register via indirect method.
2566 */
genphy_read_mmd_unsupported(struct phy_device * phdev,int devad,u16 regnum)2567 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2568 {
2569 return -EOPNOTSUPP;
2570 }
2571 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2572
genphy_write_mmd_unsupported(struct phy_device * phdev,int devnum,u16 regnum,u16 val)2573 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2574 u16 regnum, u16 val)
2575 {
2576 return -EOPNOTSUPP;
2577 }
2578 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2579
genphy_suspend(struct phy_device * phydev)2580 int genphy_suspend(struct phy_device *phydev)
2581 {
2582 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2583 }
2584 EXPORT_SYMBOL(genphy_suspend);
2585
genphy_resume(struct phy_device * phydev)2586 int genphy_resume(struct phy_device *phydev)
2587 {
2588 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2589 }
2590 EXPORT_SYMBOL(genphy_resume);
2591
genphy_loopback(struct phy_device * phydev,bool enable)2592 int genphy_loopback(struct phy_device *phydev, bool enable)
2593 {
2594 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2595 enable ? BMCR_LOOPBACK : 0);
2596 }
2597 EXPORT_SYMBOL(genphy_loopback);
2598
2599 /**
2600 * phy_remove_link_mode - Remove a supported link mode
2601 * @phydev: phy_device structure to remove link mode from
2602 * @link_mode: Link mode to be removed
2603 *
2604 * Description: Some MACs don't support all link modes which the PHY
2605 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2606 * to remove a link mode.
2607 */
phy_remove_link_mode(struct phy_device * phydev,u32 link_mode)2608 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2609 {
2610 linkmode_clear_bit(link_mode, phydev->supported);
2611 phy_advertise_supported(phydev);
2612 }
2613 EXPORT_SYMBOL(phy_remove_link_mode);
2614
phy_copy_pause_bits(unsigned long * dst,unsigned long * src)2615 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2616 {
2617 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2618 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2619 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2620 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2621 }
2622
2623 /**
2624 * phy_advertise_supported - Advertise all supported modes
2625 * @phydev: target phy_device struct
2626 *
2627 * Description: Called to advertise all supported modes, doesn't touch
2628 * pause mode advertising.
2629 */
phy_advertise_supported(struct phy_device * phydev)2630 void phy_advertise_supported(struct phy_device *phydev)
2631 {
2632 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2633
2634 linkmode_copy(new, phydev->supported);
2635 phy_copy_pause_bits(new, phydev->advertising);
2636 linkmode_copy(phydev->advertising, new);
2637 }
2638 EXPORT_SYMBOL(phy_advertise_supported);
2639
2640 /**
2641 * phy_support_sym_pause - Enable support of symmetrical pause
2642 * @phydev: target phy_device struct
2643 *
2644 * Description: Called by the MAC to indicate is supports symmetrical
2645 * Pause, but not asym pause.
2646 */
phy_support_sym_pause(struct phy_device * phydev)2647 void phy_support_sym_pause(struct phy_device *phydev)
2648 {
2649 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2650 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2651 }
2652 EXPORT_SYMBOL(phy_support_sym_pause);
2653
2654 /**
2655 * phy_support_asym_pause - Enable support of asym pause
2656 * @phydev: target phy_device struct
2657 *
2658 * Description: Called by the MAC to indicate is supports Asym Pause.
2659 */
phy_support_asym_pause(struct phy_device * phydev)2660 void phy_support_asym_pause(struct phy_device *phydev)
2661 {
2662 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2663 }
2664 EXPORT_SYMBOL(phy_support_asym_pause);
2665
2666 /**
2667 * phy_set_sym_pause - Configure symmetric Pause
2668 * @phydev: target phy_device struct
2669 * @rx: Receiver Pause is supported
2670 * @tx: Transmit Pause is supported
2671 * @autoneg: Auto neg should be used
2672 *
2673 * Description: Configure advertised Pause support depending on if
2674 * receiver pause and pause auto neg is supported. Generally called
2675 * from the set_pauseparam .ndo.
2676 */
phy_set_sym_pause(struct phy_device * phydev,bool rx,bool tx,bool autoneg)2677 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2678 bool autoneg)
2679 {
2680 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2681
2682 if (rx && tx && autoneg)
2683 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2684 phydev->supported);
2685
2686 linkmode_copy(phydev->advertising, phydev->supported);
2687 }
2688 EXPORT_SYMBOL(phy_set_sym_pause);
2689
2690 /**
2691 * phy_set_asym_pause - Configure Pause and Asym Pause
2692 * @phydev: target phy_device struct
2693 * @rx: Receiver Pause is supported
2694 * @tx: Transmit Pause is supported
2695 *
2696 * Description: Configure advertised Pause support depending on if
2697 * transmit and receiver pause is supported. If there has been a
2698 * change in adverting, trigger a new autoneg. Generally called from
2699 * the set_pauseparam .ndo.
2700 */
phy_set_asym_pause(struct phy_device * phydev,bool rx,bool tx)2701 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2702 {
2703 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2704
2705 linkmode_copy(oldadv, phydev->advertising);
2706 linkmode_set_pause(phydev->advertising, tx, rx);
2707
2708 if (!linkmode_equal(oldadv, phydev->advertising) &&
2709 phydev->autoneg)
2710 phy_start_aneg(phydev);
2711 }
2712 EXPORT_SYMBOL(phy_set_asym_pause);
2713
2714 /**
2715 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2716 * @phydev: phy_device struct
2717 * @pp: requested pause configuration
2718 *
2719 * Description: Test if the PHY/MAC combination supports the Pause
2720 * configuration the user is requesting. Returns True if it is
2721 * supported, false otherwise.
2722 */
phy_validate_pause(struct phy_device * phydev,struct ethtool_pauseparam * pp)2723 bool phy_validate_pause(struct phy_device *phydev,
2724 struct ethtool_pauseparam *pp)
2725 {
2726 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2727 phydev->supported) && pp->rx_pause)
2728 return false;
2729
2730 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2731 phydev->supported) &&
2732 pp->rx_pause != pp->tx_pause)
2733 return false;
2734
2735 return true;
2736 }
2737 EXPORT_SYMBOL(phy_validate_pause);
2738
2739 /**
2740 * phy_get_pause - resolve negotiated pause modes
2741 * @phydev: phy_device struct
2742 * @tx_pause: pointer to bool to indicate whether transmit pause should be
2743 * enabled.
2744 * @rx_pause: pointer to bool to indicate whether receive pause should be
2745 * enabled.
2746 *
2747 * Resolve and return the flow control modes according to the negotiation
2748 * result. This includes checking that we are operating in full duplex mode.
2749 * See linkmode_resolve_pause() for further details.
2750 */
phy_get_pause(struct phy_device * phydev,bool * tx_pause,bool * rx_pause)2751 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
2752 {
2753 if (phydev->duplex != DUPLEX_FULL) {
2754 *tx_pause = false;
2755 *rx_pause = false;
2756 return;
2757 }
2758
2759 return linkmode_resolve_pause(phydev->advertising,
2760 phydev->lp_advertising,
2761 tx_pause, rx_pause);
2762 }
2763 EXPORT_SYMBOL(phy_get_pause);
2764
2765 #if IS_ENABLED(CONFIG_OF_MDIO)
phy_get_int_delay_property(struct device * dev,const char * name)2766 static int phy_get_int_delay_property(struct device *dev, const char *name)
2767 {
2768 s32 int_delay;
2769 int ret;
2770
2771 ret = device_property_read_u32(dev, name, &int_delay);
2772 if (ret)
2773 return ret;
2774
2775 return int_delay;
2776 }
2777 #else
phy_get_int_delay_property(struct device * dev,const char * name)2778 static int phy_get_int_delay_property(struct device *dev, const char *name)
2779 {
2780 return -EINVAL;
2781 }
2782 #endif
2783
2784 /**
2785 * phy_get_delay_index - returns the index of the internal delay
2786 * @phydev: phy_device struct
2787 * @dev: pointer to the devices device struct
2788 * @delay_values: array of delays the PHY supports
2789 * @size: the size of the delay array
2790 * @is_rx: boolean to indicate to get the rx internal delay
2791 *
2792 * Returns the index within the array of internal delay passed in.
2793 * If the device property is not present then the interface type is checked
2794 * if the interface defines use of internal delay then a 1 is returned otherwise
2795 * a 0 is returned.
2796 * The array must be in ascending order. If PHY does not have an ascending order
2797 * array then size = 0 and the value of the delay property is returned.
2798 * Return -EINVAL if the delay is invalid or cannot be found.
2799 */
phy_get_internal_delay(struct phy_device * phydev,struct device * dev,const int * delay_values,int size,bool is_rx)2800 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
2801 const int *delay_values, int size, bool is_rx)
2802 {
2803 s32 delay;
2804 int i;
2805
2806 if (is_rx) {
2807 delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
2808 if (delay < 0 && size == 0) {
2809 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2810 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
2811 return 1;
2812 else
2813 return 0;
2814 }
2815
2816 } else {
2817 delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
2818 if (delay < 0 && size == 0) {
2819 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2820 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
2821 return 1;
2822 else
2823 return 0;
2824 }
2825 }
2826
2827 if (delay < 0)
2828 return delay;
2829
2830 if (delay && size == 0)
2831 return delay;
2832
2833 if (delay < delay_values[0] || delay > delay_values[size - 1]) {
2834 phydev_err(phydev, "Delay %d is out of range\n", delay);
2835 return -EINVAL;
2836 }
2837
2838 if (delay == delay_values[0])
2839 return 0;
2840
2841 for (i = 1; i < size; i++) {
2842 if (delay == delay_values[i])
2843 return i;
2844
2845 /* Find an approximate index by looking up the table */
2846 if (delay > delay_values[i - 1] &&
2847 delay < delay_values[i]) {
2848 if (delay - delay_values[i - 1] <
2849 delay_values[i] - delay)
2850 return i - 1;
2851 else
2852 return i;
2853 }
2854 }
2855
2856 phydev_err(phydev, "error finding internal delay index for %d\n",
2857 delay);
2858
2859 return -EINVAL;
2860 }
2861 EXPORT_SYMBOL(phy_get_internal_delay);
2862
phy_drv_supports_irq(struct phy_driver * phydrv)2863 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2864 {
2865 return phydrv->config_intr && phydrv->ack_interrupt;
2866 }
2867
2868 /**
2869 * phy_probe - probe and init a PHY device
2870 * @dev: device to probe and init
2871 *
2872 * Description: Take care of setting up the phy_device structure,
2873 * set the state to READY (the driver's init function should
2874 * set it to STARTING if needed).
2875 */
phy_probe(struct device * dev)2876 static int phy_probe(struct device *dev)
2877 {
2878 struct phy_device *phydev = to_phy_device(dev);
2879 struct device_driver *drv = phydev->mdio.dev.driver;
2880 struct phy_driver *phydrv = to_phy_driver(drv);
2881 int err = 0;
2882
2883 phydev->drv = phydrv;
2884
2885 /* Disable the interrupt if the PHY doesn't support it
2886 * but the interrupt is still a valid one
2887 */
2888 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2889 phydev->irq = PHY_POLL;
2890
2891 if (phydrv->flags & PHY_IS_INTERNAL)
2892 phydev->is_internal = true;
2893
2894 mutex_lock(&phydev->lock);
2895
2896 /* Deassert the reset signal */
2897 phy_device_reset(phydev, 0);
2898
2899 if (phydev->drv->probe) {
2900 err = phydev->drv->probe(phydev);
2901 if (err)
2902 goto out;
2903 }
2904
2905 /* Start out supporting everything. Eventually,
2906 * a controller will attach, and may modify one
2907 * or both of these values
2908 */
2909 if (phydrv->features) {
2910 linkmode_copy(phydev->supported, phydrv->features);
2911 } else if (phydrv->get_features) {
2912 err = phydrv->get_features(phydev);
2913 } else if (phydev->is_c45) {
2914 err = genphy_c45_pma_read_abilities(phydev);
2915 } else {
2916 err = genphy_read_abilities(phydev);
2917 }
2918
2919 if (err)
2920 goto out;
2921
2922 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2923 phydev->supported))
2924 phydev->autoneg = 0;
2925
2926 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2927 phydev->supported))
2928 phydev->is_gigabit_capable = 1;
2929 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2930 phydev->supported))
2931 phydev->is_gigabit_capable = 1;
2932
2933 of_set_phy_supported(phydev);
2934 phy_advertise_supported(phydev);
2935
2936 /* Get the EEE modes we want to prohibit. We will ask
2937 * the PHY stop advertising these mode later on
2938 */
2939 of_set_phy_eee_broken(phydev);
2940
2941 /* The Pause Frame bits indicate that the PHY can support passing
2942 * pause frames. During autonegotiation, the PHYs will determine if
2943 * they should allow pause frames to pass. The MAC driver should then
2944 * use that result to determine whether to enable flow control via
2945 * pause frames.
2946 *
2947 * Normally, PHY drivers should not set the Pause bits, and instead
2948 * allow phylib to do that. However, there may be some situations
2949 * (e.g. hardware erratum) where the driver wants to set only one
2950 * of these bits.
2951 */
2952 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2953 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2954 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2955 phydev->supported);
2956 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2957 phydev->supported);
2958 }
2959
2960 /* Set the state to READY by default */
2961 phydev->state = PHY_READY;
2962
2963 out:
2964 /* Assert the reset signal */
2965 if (err)
2966 phy_device_reset(phydev, 1);
2967
2968 mutex_unlock(&phydev->lock);
2969
2970 return err;
2971 }
2972
phy_remove(struct device * dev)2973 static int phy_remove(struct device *dev)
2974 {
2975 struct phy_device *phydev = to_phy_device(dev);
2976
2977 cancel_delayed_work_sync(&phydev->state_queue);
2978
2979 mutex_lock(&phydev->lock);
2980 phydev->state = PHY_DOWN;
2981 mutex_unlock(&phydev->lock);
2982
2983 sfp_bus_del_upstream(phydev->sfp_bus);
2984 phydev->sfp_bus = NULL;
2985
2986 if (phydev->drv && phydev->drv->remove)
2987 phydev->drv->remove(phydev);
2988
2989 /* Assert the reset signal */
2990 phy_device_reset(phydev, 1);
2991
2992 phydev->drv = NULL;
2993
2994 return 0;
2995 }
2996
2997 /**
2998 * phy_driver_register - register a phy_driver with the PHY layer
2999 * @new_driver: new phy_driver to register
3000 * @owner: module owning this PHY
3001 */
phy_driver_register(struct phy_driver * new_driver,struct module * owner)3002 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
3003 {
3004 int retval;
3005
3006 /* Either the features are hard coded, or dynamically
3007 * determined. It cannot be both.
3008 */
3009 if (WARN_ON(new_driver->features && new_driver->get_features)) {
3010 pr_err("%s: features and get_features must not both be set\n",
3011 new_driver->name);
3012 return -EINVAL;
3013 }
3014
3015 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3016 new_driver->mdiodrv.driver.name = new_driver->name;
3017 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3018 new_driver->mdiodrv.driver.probe = phy_probe;
3019 new_driver->mdiodrv.driver.remove = phy_remove;
3020 new_driver->mdiodrv.driver.owner = owner;
3021 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3022
3023 retval = driver_register(&new_driver->mdiodrv.driver);
3024 if (retval) {
3025 pr_err("%s: Error %d in registering driver\n",
3026 new_driver->name, retval);
3027
3028 return retval;
3029 }
3030
3031 pr_debug("%s: Registered new driver\n", new_driver->name);
3032
3033 return 0;
3034 }
3035 EXPORT_SYMBOL(phy_driver_register);
3036
phy_drivers_register(struct phy_driver * new_driver,int n,struct module * owner)3037 int phy_drivers_register(struct phy_driver *new_driver, int n,
3038 struct module *owner)
3039 {
3040 int i, ret = 0;
3041
3042 for (i = 0; i < n; i++) {
3043 ret = phy_driver_register(new_driver + i, owner);
3044 if (ret) {
3045 while (i-- > 0)
3046 phy_driver_unregister(new_driver + i);
3047 break;
3048 }
3049 }
3050 return ret;
3051 }
3052 EXPORT_SYMBOL(phy_drivers_register);
3053
phy_driver_unregister(struct phy_driver * drv)3054 void phy_driver_unregister(struct phy_driver *drv)
3055 {
3056 driver_unregister(&drv->mdiodrv.driver);
3057 }
3058 EXPORT_SYMBOL(phy_driver_unregister);
3059
phy_drivers_unregister(struct phy_driver * drv,int n)3060 void phy_drivers_unregister(struct phy_driver *drv, int n)
3061 {
3062 int i;
3063
3064 for (i = 0; i < n; i++)
3065 phy_driver_unregister(drv + i);
3066 }
3067 EXPORT_SYMBOL(phy_drivers_unregister);
3068
3069 static struct phy_driver genphy_driver = {
3070 .phy_id = 0xffffffff,
3071 .phy_id_mask = 0xffffffff,
3072 .name = "Generic PHY",
3073 .get_features = genphy_read_abilities,
3074 .suspend = genphy_suspend,
3075 .resume = genphy_resume,
3076 .set_loopback = genphy_loopback,
3077 };
3078
3079 static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
3080 .get_sset_count = phy_ethtool_get_sset_count,
3081 .get_strings = phy_ethtool_get_strings,
3082 .get_stats = phy_ethtool_get_stats,
3083 .start_cable_test = phy_start_cable_test,
3084 .start_cable_test_tdr = phy_start_cable_test_tdr,
3085 };
3086
phy_init(void)3087 static int __init phy_init(void)
3088 {
3089 int rc;
3090
3091 rc = mdio_bus_init();
3092 if (rc)
3093 return rc;
3094
3095 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
3096 features_init();
3097
3098 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
3099 if (rc)
3100 goto err_c45;
3101
3102 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3103 if (rc) {
3104 phy_driver_unregister(&genphy_c45_driver);
3105 err_c45:
3106 mdio_bus_exit();
3107 }
3108
3109 return rc;
3110 }
3111
phy_exit(void)3112 static void __exit phy_exit(void)
3113 {
3114 phy_driver_unregister(&genphy_c45_driver);
3115 phy_driver_unregister(&genphy_driver);
3116 mdio_bus_exit();
3117 ethtool_set_ethtool_phy_ops(NULL);
3118 }
3119
3120 subsys_initcall(phy_init);
3121 module_exit(phy_exit);
3122