zkkkk12 commented on code in PR #21485: URL: https://github.com/apache/nuttx/pull/21485#discussion_r4509281832
########## drivers/net/phy/phy_core.c: ########## @@ -0,0 +1,1002 @@ +/**************************************************************************** + * drivers/net/phy/phy_core.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <debug.h> +#include <inttypes.h> +#include <string.h> +#include <sys/types.h> +#include <syslog.h> + +#include <nuttx/irq.h> +#include <nuttx/arch.h> +#include <nuttx/wqueue.h> +#include <nuttx/net/phy.h> +#include <nuttx/net/mii.h> +#include <nuttx/net/ioctl.h> +#include <nuttx/net/netdev_lowerhalf.h> +#include <nuttx/ethtool.h> + +#include <net/if.h> + +#include "phy.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define PHY_POLL_TICKS MSEC2TICK(CONFIG_NET_PHYLIB_POLL_INTERVAL) + +/* The PHY core currently stores each link mode mask in one 32-bit word. */ + +#define PHY_LINK_MODE_MASK_NWORDS 1 + +/* phy_interrupt_is_valid - true only when phy_device_s.irq is a real, + * attachable interrupt number. Both sentinels (PHY_POLL, PHY_MAC_INTERRUPT) + * are negative, so a non-negative irq is a real, attachable IRQ number. + */ + +#define phy_interrupt_is_valid(p) ((p)->irq >= 0) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: phy_check_link + * + * Description: + * Read the current PHY status and run the link up/down state transitions, + * updating phydev->state. Shared by both the polling work and the + * interrupt work so the two paths behave identically. + * + * Carrier notification (netdev_lower_carrier_on/off) is NOT performed + * here: it takes the netdev d_lock and would invert the lock order used + * by the ioctl path (d_lock then phydev->lock), risking an AB-BA + * deadlock. The caller therefore compares phydev->state before and after + * this call and performs the carrier notification after releasing + * phydev->lock. + * + * Input Parameters: + * phydev - Pointer to the PHY device structure + * + * Returned Value: + * None. The new link state is reflected in phydev->state. + * + ****************************************************************************/ + +static void phy_check_link(FAR struct phy_device_s *phydev) +{ + int ret; + + /* In interrupt mode, service the PHY interrupt source first so INTB + * de-asserts and a fresh edge can be latched. Both the polling and the + * interrupt paths share this single state machine entry point: the + * interrupt path only kicks the state machine, and the driver's + * handle_interrupt() does the actual decode/ack. + * + * handle_interrupt - the driver reads its own vendor interrupt-status + * register, decodes ALL pending sources (link and non-link), services + * non-link diagnostic events internally, and returns: + * >0 : a link-relevant event is pending -> re-read link status below + * 0 : no link-relevant event -> skip the link re-read + * <0 : MDIO/HW failure (PHY unreachable) -> enter PHY_ERROR + */ + + if (phydev->irq != PHY_POLL && phydev->drv->handle_interrupt) + { + ret = phydev->drv->handle_interrupt(phydev); + if (ret < 0) + { + /* The PHY is unreachable (could not even read its interrupt + * status register). Enter the error state: force the carrier + * off and stop rescheduling. Recovery needs an explicit + * stop/start cycle. + */ + + nerr("ERROR: handle_interrupt failed: %d\n", ret); + phydev->state = PHY_ERROR; + return; + } + else if (ret == 0) Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
