dim.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #include <linux/dim.h>
  6. bool dim_on_top(struct dim *dim)
  7. {
  8. switch (dim->tune_state) {
  9. case DIM_PARKING_ON_TOP:
  10. case DIM_PARKING_TIRED:
  11. return true;
  12. case DIM_GOING_RIGHT:
  13. return (dim->steps_left > 1) && (dim->steps_right == 1);
  14. default: /* DIM_GOING_LEFT */
  15. return (dim->steps_right > 1) && (dim->steps_left == 1);
  16. }
  17. }
  18. EXPORT_SYMBOL(dim_on_top);
  19. void dim_turn(struct dim *dim)
  20. {
  21. switch (dim->tune_state) {
  22. case DIM_PARKING_ON_TOP:
  23. case DIM_PARKING_TIRED:
  24. break;
  25. case DIM_GOING_RIGHT:
  26. dim->tune_state = DIM_GOING_LEFT;
  27. dim->steps_left = 0;
  28. break;
  29. case DIM_GOING_LEFT:
  30. dim->tune_state = DIM_GOING_RIGHT;
  31. dim->steps_right = 0;
  32. break;
  33. }
  34. }
  35. EXPORT_SYMBOL(dim_turn);
  36. void dim_park_on_top(struct dim *dim)
  37. {
  38. dim->steps_right = 0;
  39. dim->steps_left = 0;
  40. dim->tired = 0;
  41. dim->tune_state = DIM_PARKING_ON_TOP;
  42. }
  43. EXPORT_SYMBOL(dim_park_on_top);
  44. void dim_park_tired(struct dim *dim)
  45. {
  46. dim->steps_right = 0;
  47. dim->steps_left = 0;
  48. dim->tune_state = DIM_PARKING_TIRED;
  49. }
  50. EXPORT_SYMBOL(dim_park_tired);
  51. bool dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
  52. struct dim_stats *curr_stats)
  53. {
  54. /* u32 holds up to 71 minutes, should be enough */
  55. u32 delta_us = ktime_us_delta(end->time, start->time);
  56. u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr);
  57. u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr,
  58. start->byte_ctr);
  59. u32 ncomps = BIT_GAP(BITS_PER_TYPE(u32), end->comp_ctr,
  60. start->comp_ctr);
  61. if (!delta_us)
  62. return false;
  63. curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
  64. curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
  65. curr_stats->epms = DIV_ROUND_UP(DIM_NEVENTS * USEC_PER_MSEC,
  66. delta_us);
  67. curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us);
  68. if (curr_stats->epms != 0)
  69. curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL(
  70. curr_stats->cpms * 100, curr_stats->epms);
  71. else
  72. curr_stats->cpe_ratio = 0;
  73. return true;
  74. }
  75. EXPORT_SYMBOL(dim_calc_stats);