FreeBSD/sys/dev/nxge/xgehal/xgehal-device.c
/*
 * __hal_device_handle_link_state_change
 * @hldev: HAL device handle.
 *
 * Link state change handler. The function is invoked by HAL when
 * Xframe indicates link state change condition. The code here makes sure to
 * 1) ignore redundant state change indications;
 * 2) execute link-up sequence, and handle the failure to bring the link up;
 * 3) generate XGE_HAL_LINK_UP/DOWN event for the subsequent handling by
 *    upper-layer driver (ULD).
 */
hal_device_handle_link_state_change
@hldev: HAL device handle.

Link state change handler. The function is invoked by HAL when
Xframe indicates link state change condition. The code here makes sure to
1) ignore redundant state change indications;
2) execute link-up sequence, and handle the failure to bring the link up;
3) generate XGE_HAL_LINK_UP/DOWN event for the subsequent handling by
upper-layer driver (ULD).
hal_device_handle_link_state_change @hldev : HAL device handle . Link state change handler . The function is invoked by HAL when Xframe indicates link state change condition . The code here makes sure to 1 ) ignore redundant state change indications ; 2 ) execute link - up sequence and handle the failure to bring the link up ; 3 ) generate XGE_HAL_LINK_UP / DOWN event for the subsequent handling by upper - layer driver ( ULD ) .
static int
__hal_device_handle_link_state_change(xge_hal_device_t *hldev)
{
	u64 hw_status;
	int hw_link_state;
	int retcode;
	xge_hal_pci_bar0_t *bar0 = (xge_hal_pci_bar0_t *)(void *)hldev->bar0;
	u64 val64;
	int i = 0;
	val64 = xge_os_pio_mem_read64(hldev->pdev, hldev->regh0,
	                &bar0->adapter_control);
	if ( !(val64 & XGE_HAL_ADAPTER_CNTL_EN) &&
	     (hldev->link_state == XGE_HAL_LINK_UP) ) {
	    return(__hal_device_handle_link_down_ind(hldev));
	}
	do {
	    xge_os_mdelay(1);
	    (void) xge_hal_device_status(hldev, &hw_status);
	    hw_link_state = (hw_status &
	        (XGE_HAL_ADAPTER_STATUS_RMAC_REMOTE_FAULT |
	            XGE_HAL_ADAPTER_STATUS_RMAC_LOCAL_FAULT)) ?
	            XGE_HAL_LINK_DOWN : XGE_HAL_LINK_UP;
	    if (hldev->link_state == hw_link_state)
	        break;
	} while (i++ < hldev->config.link_valid_cnt);
	if (hldev->link_state == hw_link_state)
	    retcode = 0;
	else if (hw_link_state == XGE_HAL_LINK_UP)
	    retcode = __hal_device_handle_link_up_ind(hldev);
	else
	    retcode = __hal_device_handle_link_down_ind(hldev);
	return retcode;
}
static int __hal_device_handle_link_state_change ( xge_hal_device_t * hldev ) { u64 hw_status ; int hw_link_state ; int retcode ; xge_hal_pci_bar0_t * bar0 = ( xge_hal_pci_bar0_t * ) ( void * ) hldev -> bar0 ; u64 val64 ; int i = 0 ; val64 = xge_os_pio_mem_read64 ( hldev -> pdev , hldev -> regh0 , & bar0 -> adapter_control ) ; if ( ! ( val64 & XGE_HAL_ADAPTER_CNTL_EN ) && ( hldev -> link_state == XGE_HAL_LINK_UP ) ) { return ( __hal_device_handle_link_down_ind ( hldev ) ) ; } do { xge_os_mdelay ( 1 ) ; ( void ) xge_hal_device_status ( hldev , & hw_status ) ; hw_link_state = ( hw_status & ( XGE_HAL_ADAPTER_STATUS_RMAC_REMOTE_FAULT | XGE_HAL_ADAPTER_STATUS_RMAC_LOCAL_FAULT ) ) ? XGE_HAL_LINK_DOWN : XGE_HAL_LINK_UP ; if ( hldev -> link_state == hw_link_state ) break ; } while ( i ++ < hldev -> config . link_valid_cnt ) ; if ( hldev -> link_state == hw_link_state ) retcode = 0 ; else if ( hw_link_state == XGE_HAL_LINK_UP ) retcode = __hal_device_handle_link_up_ind ( hldev ) ; else retcode = __hal_device_handle_link_down_ind ( hldev ) ; return retcode ; }
__hal_device_handle_link_state_change
 @hldev: HAL device handle.
__hal_device_handle_link_state_change @hldev : HAL device handle .
/* If the adapter is not enabled but the hal thinks we are in the up
	 * state then transition to the down state.
	 */ /* check if the current link state is still considered
	     * to be changed. This way we will make sure that this is
	     * not a noise which needs to be filtered out */ /* If the current link state is same as previous, just return */ /* detected state change */