ena_eth_com.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2015 Amazon.com, Inc. or its affiliates.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #ifndef ENA_ETH_COM_H_
  33. #define ENA_ETH_COM_H_
  34. #include "ena_com.h"
  35. /* head update threshold in units of (queue size / ENA_COMP_HEAD_THRESH) */
  36. #define ENA_COMP_HEAD_THRESH 4
  37. struct ena_com_tx_ctx {
  38. struct ena_com_tx_meta ena_meta;
  39. struct ena_com_buf *ena_bufs;
  40. /* For LLQ, header buffer - pushed to the device mem space */
  41. void *push_header;
  42. enum ena_eth_io_l3_proto_index l3_proto;
  43. enum ena_eth_io_l4_proto_index l4_proto;
  44. u16 num_bufs;
  45. u16 req_id;
  46. /* For regular queue, indicate the size of the header
  47. * For LLQ, indicate the size of the pushed buffer
  48. */
  49. u16 header_len;
  50. u8 meta_valid;
  51. u8 tso_enable;
  52. u8 l3_csum_enable;
  53. u8 l4_csum_enable;
  54. u8 l4_csum_partial;
  55. u8 df; /* Don't fragment */
  56. };
  57. struct ena_com_rx_ctx {
  58. struct ena_com_rx_buf_info *ena_bufs;
  59. enum ena_eth_io_l3_proto_index l3_proto;
  60. enum ena_eth_io_l4_proto_index l4_proto;
  61. bool l3_csum_err;
  62. bool l4_csum_err;
  63. u8 l4_csum_checked;
  64. /* fragmented packet */
  65. bool frag;
  66. u32 hash;
  67. u16 descs;
  68. int max_bufs;
  69. };
  70. int ena_com_prepare_tx(struct ena_com_io_sq *io_sq,
  71. struct ena_com_tx_ctx *ena_tx_ctx,
  72. int *nb_hw_desc);
  73. int ena_com_rx_pkt(struct ena_com_io_cq *io_cq,
  74. struct ena_com_io_sq *io_sq,
  75. struct ena_com_rx_ctx *ena_rx_ctx);
  76. int ena_com_add_single_rx_desc(struct ena_com_io_sq *io_sq,
  77. struct ena_com_buf *ena_buf,
  78. u16 req_id);
  79. bool ena_com_cq_empty(struct ena_com_io_cq *io_cq);
  80. static inline void ena_com_unmask_intr(struct ena_com_io_cq *io_cq,
  81. struct ena_eth_io_intr_reg *intr_reg)
  82. {
  83. writel(intr_reg->intr_control, io_cq->unmask_reg);
  84. }
  85. static inline int ena_com_free_desc(struct ena_com_io_sq *io_sq)
  86. {
  87. u16 tail, next_to_comp, cnt;
  88. next_to_comp = io_sq->next_to_comp;
  89. tail = io_sq->tail;
  90. cnt = tail - next_to_comp;
  91. return io_sq->q_depth - 1 - cnt;
  92. }
  93. /* Check if the submission queue has enough space to hold required_buffers */
  94. static inline bool ena_com_sq_have_enough_space(struct ena_com_io_sq *io_sq,
  95. u16 required_buffers)
  96. {
  97. int temp;
  98. if (io_sq->mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
  99. return ena_com_free_desc(io_sq) >= required_buffers;
  100. /* This calculation doesn't need to be 100% accurate. So to reduce
  101. * the calculation overhead just Subtract 2 lines from the free descs
  102. * (one for the header line and one to compensate the devision
  103. * down calculation.
  104. */
  105. temp = required_buffers / io_sq->llq_info.descs_per_entry + 2;
  106. return ena_com_free_desc(io_sq) > temp;
  107. }
  108. static inline bool ena_com_meta_desc_changed(struct ena_com_io_sq *io_sq,
  109. struct ena_com_tx_ctx *ena_tx_ctx)
  110. {
  111. if (!ena_tx_ctx->meta_valid)
  112. return false;
  113. return !!memcmp(&io_sq->cached_tx_meta,
  114. &ena_tx_ctx->ena_meta,
  115. sizeof(struct ena_com_tx_meta));
  116. }
  117. static inline bool is_llq_max_tx_burst_exists(struct ena_com_io_sq *io_sq)
  118. {
  119. return (io_sq->mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) &&
  120. io_sq->llq_info.max_entries_in_tx_burst > 0;
  121. }
  122. static inline bool ena_com_is_doorbell_needed(struct ena_com_io_sq *io_sq,
  123. struct ena_com_tx_ctx *ena_tx_ctx)
  124. {
  125. struct ena_com_llq_info *llq_info;
  126. int descs_after_first_entry;
  127. int num_entries_needed = 1;
  128. u16 num_descs;
  129. if (!is_llq_max_tx_burst_exists(io_sq))
  130. return false;
  131. llq_info = &io_sq->llq_info;
  132. num_descs = ena_tx_ctx->num_bufs;
  133. if (unlikely(ena_com_meta_desc_changed(io_sq, ena_tx_ctx)))
  134. ++num_descs;
  135. if (num_descs > llq_info->descs_num_before_header) {
  136. descs_after_first_entry = num_descs - llq_info->descs_num_before_header;
  137. num_entries_needed += DIV_ROUND_UP(descs_after_first_entry,
  138. llq_info->descs_per_entry);
  139. }
  140. pr_debug("queue: %d num_descs: %d num_entries_needed: %d\n", io_sq->qid,
  141. num_descs, num_entries_needed);
  142. return num_entries_needed > io_sq->entries_in_tx_burst_left;
  143. }
  144. static inline int ena_com_write_sq_doorbell(struct ena_com_io_sq *io_sq)
  145. {
  146. u16 max_entries_in_tx_burst = io_sq->llq_info.max_entries_in_tx_burst;
  147. u16 tail = io_sq->tail;
  148. pr_debug("write submission queue doorbell for queue: %d tail: %d\n",
  149. io_sq->qid, tail);
  150. writel(tail, io_sq->db_addr);
  151. if (is_llq_max_tx_burst_exists(io_sq)) {
  152. pr_debug("reset available entries in tx burst for queue %d to %d\n",
  153. io_sq->qid, max_entries_in_tx_burst);
  154. io_sq->entries_in_tx_burst_left = max_entries_in_tx_burst;
  155. }
  156. return 0;
  157. }
  158. static inline int ena_com_update_dev_comp_head(struct ena_com_io_cq *io_cq)
  159. {
  160. u16 unreported_comp, head;
  161. bool need_update;
  162. if (unlikely(io_cq->cq_head_db_reg)) {
  163. head = io_cq->head;
  164. unreported_comp = head - io_cq->last_head_update;
  165. need_update = unreported_comp > (io_cq->q_depth / ENA_COMP_HEAD_THRESH);
  166. if (unlikely(need_update)) {
  167. pr_debug("Write completion queue doorbell for queue %d: head: %d\n",
  168. io_cq->qid, head);
  169. writel(head, io_cq->cq_head_db_reg);
  170. io_cq->last_head_update = head;
  171. }
  172. }
  173. return 0;
  174. }
  175. static inline void ena_com_update_numa_node(struct ena_com_io_cq *io_cq,
  176. u8 numa_node)
  177. {
  178. struct ena_eth_io_numa_node_cfg_reg numa_cfg;
  179. if (!io_cq->numa_node_cfg_reg)
  180. return;
  181. numa_cfg.numa_cfg = (numa_node & ENA_ETH_IO_NUMA_NODE_CFG_REG_NUMA_MASK)
  182. | ENA_ETH_IO_NUMA_NODE_CFG_REG_ENABLED_MASK;
  183. writel(numa_cfg.numa_cfg, io_cq->numa_node_cfg_reg);
  184. }
  185. static inline void ena_com_comp_ack(struct ena_com_io_sq *io_sq, u16 elem)
  186. {
  187. io_sq->next_to_comp += elem;
  188. }
  189. static inline void ena_com_cq_inc_head(struct ena_com_io_cq *io_cq)
  190. {
  191. io_cq->head++;
  192. /* Switch phase bit in case of wrap around */
  193. if (unlikely((io_cq->head & (io_cq->q_depth - 1)) == 0))
  194. io_cq->phase ^= 1;
  195. }
  196. static inline int ena_com_tx_comp_req_id_get(struct ena_com_io_cq *io_cq,
  197. u16 *req_id)
  198. {
  199. u8 expected_phase, cdesc_phase;
  200. struct ena_eth_io_tx_cdesc *cdesc;
  201. u16 masked_head;
  202. masked_head = io_cq->head & (io_cq->q_depth - 1);
  203. expected_phase = io_cq->phase;
  204. cdesc = (struct ena_eth_io_tx_cdesc *)
  205. ((uintptr_t)io_cq->cdesc_addr.virt_addr +
  206. (masked_head * io_cq->cdesc_entry_size_in_bytes));
  207. /* When the current completion descriptor phase isn't the same as the
  208. * expected, it mean that the device still didn't update
  209. * this completion.
  210. */
  211. cdesc_phase = READ_ONCE(cdesc->flags) & ENA_ETH_IO_TX_CDESC_PHASE_MASK;
  212. if (cdesc_phase != expected_phase)
  213. return -EAGAIN;
  214. dma_rmb();
  215. *req_id = READ_ONCE(cdesc->req_id);
  216. if (unlikely(*req_id >= io_cq->q_depth)) {
  217. pr_err("Invalid req id %d\n", cdesc->req_id);
  218. return -EINVAL;
  219. }
  220. ena_com_cq_inc_head(io_cq);
  221. return 0;
  222. }
  223. #endif /* ENA_ETH_COM_H_ */