18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/dim.h> 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_cibool dim_on_top(struct dim *dim) 98c2ecf20Sopenharmony_ci{ 108c2ecf20Sopenharmony_ci switch (dim->tune_state) { 118c2ecf20Sopenharmony_ci case DIM_PARKING_ON_TOP: 128c2ecf20Sopenharmony_ci case DIM_PARKING_TIRED: 138c2ecf20Sopenharmony_ci return true; 148c2ecf20Sopenharmony_ci case DIM_GOING_RIGHT: 158c2ecf20Sopenharmony_ci return (dim->steps_left > 1) && (dim->steps_right == 1); 168c2ecf20Sopenharmony_ci default: /* DIM_GOING_LEFT */ 178c2ecf20Sopenharmony_ci return (dim->steps_right > 1) && (dim->steps_left == 1); 188c2ecf20Sopenharmony_ci } 198c2ecf20Sopenharmony_ci} 208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dim_on_top); 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_civoid dim_turn(struct dim *dim) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci switch (dim->tune_state) { 258c2ecf20Sopenharmony_ci case DIM_PARKING_ON_TOP: 268c2ecf20Sopenharmony_ci case DIM_PARKING_TIRED: 278c2ecf20Sopenharmony_ci break; 288c2ecf20Sopenharmony_ci case DIM_GOING_RIGHT: 298c2ecf20Sopenharmony_ci dim->tune_state = DIM_GOING_LEFT; 308c2ecf20Sopenharmony_ci dim->steps_left = 0; 318c2ecf20Sopenharmony_ci break; 328c2ecf20Sopenharmony_ci case DIM_GOING_LEFT: 338c2ecf20Sopenharmony_ci dim->tune_state = DIM_GOING_RIGHT; 348c2ecf20Sopenharmony_ci dim->steps_right = 0; 358c2ecf20Sopenharmony_ci break; 368c2ecf20Sopenharmony_ci } 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dim_turn); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_civoid dim_park_on_top(struct dim *dim) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci dim->steps_right = 0; 438c2ecf20Sopenharmony_ci dim->steps_left = 0; 448c2ecf20Sopenharmony_ci dim->tired = 0; 458c2ecf20Sopenharmony_ci dim->tune_state = DIM_PARKING_ON_TOP; 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dim_park_on_top); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_civoid dim_park_tired(struct dim *dim) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci dim->steps_right = 0; 528c2ecf20Sopenharmony_ci dim->steps_left = 0; 538c2ecf20Sopenharmony_ci dim->tune_state = DIM_PARKING_TIRED; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dim_park_tired); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cibool dim_calc_stats(struct dim_sample *start, struct dim_sample *end, 588c2ecf20Sopenharmony_ci struct dim_stats *curr_stats) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci /* u32 holds up to 71 minutes, should be enough */ 618c2ecf20Sopenharmony_ci u32 delta_us = ktime_us_delta(end->time, start->time); 628c2ecf20Sopenharmony_ci u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr); 638c2ecf20Sopenharmony_ci u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr, 648c2ecf20Sopenharmony_ci start->byte_ctr); 658c2ecf20Sopenharmony_ci u32 ncomps = BIT_GAP(BITS_PER_TYPE(u32), end->comp_ctr, 668c2ecf20Sopenharmony_ci start->comp_ctr); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (!delta_us) 698c2ecf20Sopenharmony_ci return false; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us); 728c2ecf20Sopenharmony_ci curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us); 738c2ecf20Sopenharmony_ci curr_stats->epms = DIV_ROUND_UP(DIM_NEVENTS * USEC_PER_MSEC, 748c2ecf20Sopenharmony_ci delta_us); 758c2ecf20Sopenharmony_ci curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us); 768c2ecf20Sopenharmony_ci if (curr_stats->epms != 0) 778c2ecf20Sopenharmony_ci curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL( 788c2ecf20Sopenharmony_ci curr_stats->cpms * 100, curr_stats->epms); 798c2ecf20Sopenharmony_ci else 808c2ecf20Sopenharmony_ci curr_stats->cpe_ratio = 0; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci return true; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dim_calc_stats); 85