aq_vec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * aQuantia Corporation Network Driver
  3. * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. /* File aq_vec.c: Definition of common structure for vector of Rx and Tx rings.
  10. * Definition of functions for Rx and Tx rings. Friendly module for aq_nic.
  11. */
  12. #include "aq_vec.h"
  13. #include "aq_nic.h"
  14. #include "aq_ring.h"
  15. #include "aq_hw.h"
  16. #include <linux/netdevice.h>
  17. struct aq_vec_s {
  18. const struct aq_hw_ops *aq_hw_ops;
  19. struct aq_hw_s *aq_hw;
  20. struct aq_nic_s *aq_nic;
  21. unsigned int tx_rings;
  22. unsigned int rx_rings;
  23. struct aq_ring_param_s aq_ring_param;
  24. struct napi_struct napi;
  25. struct aq_ring_s ring[AQ_CFG_TCS_MAX][2];
  26. };
  27. #define AQ_VEC_TX_ID 0
  28. #define AQ_VEC_RX_ID 1
  29. static int aq_vec_poll(struct napi_struct *napi, int budget)
  30. {
  31. struct aq_vec_s *self = container_of(napi, struct aq_vec_s, napi);
  32. unsigned int sw_tail_old = 0U;
  33. struct aq_ring_s *ring = NULL;
  34. bool was_tx_cleaned = true;
  35. unsigned int i = 0U;
  36. int work_done = 0;
  37. int err = 0;
  38. if (!self) {
  39. err = -EINVAL;
  40. } else {
  41. for (i = 0U, ring = self->ring[0];
  42. self->tx_rings > i; ++i, ring = self->ring[i]) {
  43. if (self->aq_hw_ops->hw_ring_tx_head_update) {
  44. err = self->aq_hw_ops->hw_ring_tx_head_update(
  45. self->aq_hw,
  46. &ring[AQ_VEC_TX_ID]);
  47. if (err < 0)
  48. goto err_exit;
  49. }
  50. if (ring[AQ_VEC_TX_ID].sw_head !=
  51. ring[AQ_VEC_TX_ID].hw_head) {
  52. was_tx_cleaned = aq_ring_tx_clean(&ring[AQ_VEC_TX_ID]);
  53. aq_ring_update_queue_state(&ring[AQ_VEC_TX_ID]);
  54. }
  55. err = self->aq_hw_ops->hw_ring_rx_receive(self->aq_hw,
  56. &ring[AQ_VEC_RX_ID]);
  57. if (err < 0)
  58. goto err_exit;
  59. if (ring[AQ_VEC_RX_ID].sw_head !=
  60. ring[AQ_VEC_RX_ID].hw_head) {
  61. err = aq_ring_rx_clean(&ring[AQ_VEC_RX_ID],
  62. napi,
  63. &work_done,
  64. budget - work_done);
  65. if (err < 0)
  66. goto err_exit;
  67. sw_tail_old = ring[AQ_VEC_RX_ID].sw_tail;
  68. err = aq_ring_rx_fill(&ring[AQ_VEC_RX_ID]);
  69. if (err < 0)
  70. goto err_exit;
  71. err = self->aq_hw_ops->hw_ring_rx_fill(
  72. self->aq_hw,
  73. &ring[AQ_VEC_RX_ID], sw_tail_old);
  74. if (err < 0)
  75. goto err_exit;
  76. }
  77. }
  78. err_exit:
  79. if (!was_tx_cleaned)
  80. work_done = budget;
  81. if (work_done < budget) {
  82. napi_complete_done(napi, work_done);
  83. self->aq_hw_ops->hw_irq_enable(self->aq_hw,
  84. 1U << self->aq_ring_param.vec_idx);
  85. }
  86. }
  87. return work_done;
  88. }
  89. struct aq_vec_s *aq_vec_alloc(struct aq_nic_s *aq_nic, unsigned int idx,
  90. struct aq_nic_cfg_s *aq_nic_cfg)
  91. {
  92. struct aq_vec_s *self = NULL;
  93. struct aq_ring_s *ring = NULL;
  94. unsigned int i = 0U;
  95. int err = 0;
  96. self = kzalloc(sizeof(*self), GFP_KERNEL);
  97. if (!self) {
  98. err = -ENOMEM;
  99. goto err_exit;
  100. }
  101. self->aq_nic = aq_nic;
  102. self->aq_ring_param.vec_idx = idx;
  103. self->aq_ring_param.cpu =
  104. idx + aq_nic_cfg->aq_rss.base_cpu_number;
  105. cpumask_set_cpu(self->aq_ring_param.cpu,
  106. &self->aq_ring_param.affinity_mask);
  107. self->tx_rings = 0;
  108. self->rx_rings = 0;
  109. netif_napi_add(aq_nic_get_ndev(aq_nic), &self->napi,
  110. aq_vec_poll, AQ_CFG_NAPI_WEIGHT);
  111. for (i = 0; i < aq_nic_cfg->tcs; ++i) {
  112. unsigned int idx_ring = AQ_NIC_TCVEC2RING(self->nic,
  113. self->tx_rings,
  114. self->aq_ring_param.vec_idx);
  115. ring = aq_ring_tx_alloc(&self->ring[i][AQ_VEC_TX_ID], aq_nic,
  116. idx_ring, aq_nic_cfg);
  117. if (!ring) {
  118. err = -ENOMEM;
  119. goto err_exit;
  120. }
  121. ++self->tx_rings;
  122. aq_nic_set_tx_ring(aq_nic, idx_ring, ring);
  123. ring = aq_ring_rx_alloc(&self->ring[i][AQ_VEC_RX_ID], aq_nic,
  124. idx_ring, aq_nic_cfg);
  125. if (!ring) {
  126. err = -ENOMEM;
  127. goto err_exit;
  128. }
  129. ++self->rx_rings;
  130. }
  131. err_exit:
  132. if (err < 0) {
  133. aq_vec_free(self);
  134. self = NULL;
  135. }
  136. return self;
  137. }
  138. int aq_vec_init(struct aq_vec_s *self, const struct aq_hw_ops *aq_hw_ops,
  139. struct aq_hw_s *aq_hw)
  140. {
  141. struct aq_ring_s *ring = NULL;
  142. unsigned int i = 0U;
  143. int err = 0;
  144. self->aq_hw_ops = aq_hw_ops;
  145. self->aq_hw = aq_hw;
  146. for (i = 0U, ring = self->ring[0];
  147. self->tx_rings > i; ++i, ring = self->ring[i]) {
  148. err = aq_ring_init(&ring[AQ_VEC_TX_ID]);
  149. if (err < 0)
  150. goto err_exit;
  151. err = self->aq_hw_ops->hw_ring_tx_init(self->aq_hw,
  152. &ring[AQ_VEC_TX_ID],
  153. &self->aq_ring_param);
  154. if (err < 0)
  155. goto err_exit;
  156. err = aq_ring_init(&ring[AQ_VEC_RX_ID]);
  157. if (err < 0)
  158. goto err_exit;
  159. err = self->aq_hw_ops->hw_ring_rx_init(self->aq_hw,
  160. &ring[AQ_VEC_RX_ID],
  161. &self->aq_ring_param);
  162. if (err < 0)
  163. goto err_exit;
  164. err = aq_ring_rx_fill(&ring[AQ_VEC_RX_ID]);
  165. if (err < 0)
  166. goto err_exit;
  167. err = self->aq_hw_ops->hw_ring_rx_fill(self->aq_hw,
  168. &ring[AQ_VEC_RX_ID], 0U);
  169. if (err < 0)
  170. goto err_exit;
  171. }
  172. err_exit:
  173. return err;
  174. }
  175. int aq_vec_start(struct aq_vec_s *self)
  176. {
  177. struct aq_ring_s *ring = NULL;
  178. unsigned int i = 0U;
  179. int err = 0;
  180. for (i = 0U, ring = self->ring[0];
  181. self->tx_rings > i; ++i, ring = self->ring[i]) {
  182. err = self->aq_hw_ops->hw_ring_tx_start(self->aq_hw,
  183. &ring[AQ_VEC_TX_ID]);
  184. if (err < 0)
  185. goto err_exit;
  186. err = self->aq_hw_ops->hw_ring_rx_start(self->aq_hw,
  187. &ring[AQ_VEC_RX_ID]);
  188. if (err < 0)
  189. goto err_exit;
  190. }
  191. napi_enable(&self->napi);
  192. err_exit:
  193. return err;
  194. }
  195. void aq_vec_stop(struct aq_vec_s *self)
  196. {
  197. struct aq_ring_s *ring = NULL;
  198. unsigned int i = 0U;
  199. for (i = 0U, ring = self->ring[0];
  200. self->tx_rings > i; ++i, ring = self->ring[i]) {
  201. self->aq_hw_ops->hw_ring_tx_stop(self->aq_hw,
  202. &ring[AQ_VEC_TX_ID]);
  203. self->aq_hw_ops->hw_ring_rx_stop(self->aq_hw,
  204. &ring[AQ_VEC_RX_ID]);
  205. }
  206. napi_disable(&self->napi);
  207. }
  208. void aq_vec_deinit(struct aq_vec_s *self)
  209. {
  210. struct aq_ring_s *ring = NULL;
  211. unsigned int i = 0U;
  212. if (!self)
  213. goto err_exit;
  214. for (i = 0U, ring = self->ring[0];
  215. self->tx_rings > i; ++i, ring = self->ring[i]) {
  216. aq_ring_tx_clean(&ring[AQ_VEC_TX_ID]);
  217. aq_ring_rx_deinit(&ring[AQ_VEC_RX_ID]);
  218. }
  219. err_exit:;
  220. }
  221. void aq_vec_free(struct aq_vec_s *self)
  222. {
  223. struct aq_ring_s *ring = NULL;
  224. unsigned int i = 0U;
  225. if (!self)
  226. goto err_exit;
  227. for (i = 0U, ring = self->ring[0];
  228. self->tx_rings > i; ++i, ring = self->ring[i]) {
  229. aq_ring_free(&ring[AQ_VEC_TX_ID]);
  230. aq_ring_free(&ring[AQ_VEC_RX_ID]);
  231. }
  232. netif_napi_del(&self->napi);
  233. kfree(self);
  234. err_exit:;
  235. }
  236. irqreturn_t aq_vec_isr(int irq, void *private)
  237. {
  238. struct aq_vec_s *self = private;
  239. int err = 0;
  240. if (!self) {
  241. err = -EINVAL;
  242. goto err_exit;
  243. }
  244. napi_schedule(&self->napi);
  245. err_exit:
  246. return err >= 0 ? IRQ_HANDLED : IRQ_NONE;
  247. }
  248. irqreturn_t aq_vec_isr_legacy(int irq, void *private)
  249. {
  250. struct aq_vec_s *self = private;
  251. u64 irq_mask = 0U;
  252. int err;
  253. if (!self)
  254. return IRQ_NONE;
  255. err = self->aq_hw_ops->hw_irq_read(self->aq_hw, &irq_mask);
  256. if (err < 0)
  257. return IRQ_NONE;
  258. if (irq_mask) {
  259. self->aq_hw_ops->hw_irq_disable(self->aq_hw,
  260. 1U << self->aq_ring_param.vec_idx);
  261. napi_schedule(&self->napi);
  262. } else {
  263. self->aq_hw_ops->hw_irq_enable(self->aq_hw, 1U);
  264. return IRQ_NONE;
  265. }
  266. return IRQ_HANDLED;
  267. }
  268. cpumask_t *aq_vec_get_affinity_mask(struct aq_vec_s *self)
  269. {
  270. return &self->aq_ring_param.affinity_mask;
  271. }
  272. void aq_vec_add_stats(struct aq_vec_s *self,
  273. struct aq_ring_stats_rx_s *stats_rx,
  274. struct aq_ring_stats_tx_s *stats_tx)
  275. {
  276. struct aq_ring_s *ring = NULL;
  277. unsigned int r = 0U;
  278. for (r = 0U, ring = self->ring[0];
  279. self->tx_rings > r; ++r, ring = self->ring[r]) {
  280. struct aq_ring_stats_tx_s *tx = &ring[AQ_VEC_TX_ID].stats.tx;
  281. struct aq_ring_stats_rx_s *rx = &ring[AQ_VEC_RX_ID].stats.rx;
  282. stats_rx->packets += rx->packets;
  283. stats_rx->bytes += rx->bytes;
  284. stats_rx->errors += rx->errors;
  285. stats_rx->jumbo_packets += rx->jumbo_packets;
  286. stats_rx->lro_packets += rx->lro_packets;
  287. stats_tx->packets += tx->packets;
  288. stats_tx->bytes += tx->bytes;
  289. stats_tx->errors += tx->errors;
  290. stats_tx->queue_restarts += tx->queue_restarts;
  291. }
  292. }
  293. int aq_vec_get_sw_stats(struct aq_vec_s *self, u64 *data, unsigned int *p_count)
  294. {
  295. unsigned int count = 0U;
  296. struct aq_ring_stats_rx_s stats_rx;
  297. struct aq_ring_stats_tx_s stats_tx;
  298. memset(&stats_rx, 0U, sizeof(struct aq_ring_stats_rx_s));
  299. memset(&stats_tx, 0U, sizeof(struct aq_ring_stats_tx_s));
  300. aq_vec_add_stats(self, &stats_rx, &stats_tx);
  301. /* This data should mimic aq_ethtool_queue_stat_names structure
  302. */
  303. data[count] += stats_rx.packets;
  304. data[++count] += stats_tx.packets;
  305. data[++count] += stats_tx.queue_restarts;
  306. data[++count] += stats_rx.jumbo_packets;
  307. data[++count] += stats_rx.lro_packets;
  308. data[++count] += stats_rx.errors;
  309. if (p_count)
  310. *p_count = ++count;
  311. return 0;
  312. }