octeon_droq.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more details.
  17. ***********************************************************************/
  18. /*! \file octeon_droq.h
  19. * \brief Implementation of Octeon Output queues. "Output" is with
  20. * respect to the Octeon device on the NIC. From this driver's point of
  21. * view they are ingress queues.
  22. */
  23. #ifndef __OCTEON_DROQ_H__
  24. #define __OCTEON_DROQ_H__
  25. /* Default number of packets that will be processed in one iteration. */
  26. #define MAX_PACKET_BUDGET 0xFFFFFFFF
  27. /** Octeon descriptor format.
  28. * The descriptor ring is made of descriptors which have 2 64-bit values:
  29. * -# Physical (bus) address of the data buffer.
  30. * -# Physical (bus) address of a octeon_droq_info structure.
  31. * The Octeon device DMA's incoming packets and its information at the address
  32. * given by these descriptor fields.
  33. */
  34. struct octeon_droq_desc {
  35. /** The buffer pointer */
  36. u64 buffer_ptr;
  37. /** The Info pointer */
  38. u64 info_ptr;
  39. };
  40. #define OCT_DROQ_DESC_SIZE (sizeof(struct octeon_droq_desc))
  41. /** Information about packet DMA'ed by Octeon.
  42. * The format of the information available at Info Pointer after Octeon
  43. * has posted a packet. Not all descriptors have valid information. Only
  44. * the Info field of the first descriptor for a packet has information
  45. * about the packet.
  46. */
  47. struct octeon_droq_info {
  48. /** The Length of the packet. */
  49. u64 length;
  50. /** The Output Receive Header. */
  51. union octeon_rh rh;
  52. };
  53. #define OCT_DROQ_INFO_SIZE (sizeof(struct octeon_droq_info))
  54. struct octeon_skb_page_info {
  55. /* DMA address for the page */
  56. dma_addr_t dma;
  57. /* Page for the rx dma **/
  58. struct page *page;
  59. /** which offset into page */
  60. unsigned int page_offset;
  61. };
  62. /** Pointer to data buffer.
  63. * Driver keeps a pointer to the data buffer that it made available to
  64. * the Octeon device. Since the descriptor ring keeps physical (bus)
  65. * addresses, this field is required for the driver to keep track of
  66. * the virtual address pointers.
  67. */
  68. struct octeon_recv_buffer {
  69. /** Packet buffer, including metadata. */
  70. void *buffer;
  71. /** Data in the packet buffer. */
  72. u8 *data;
  73. /** pg_info **/
  74. struct octeon_skb_page_info pg_info;
  75. };
  76. #define OCT_DROQ_RECVBUF_SIZE (sizeof(struct octeon_recv_buffer))
  77. /** Output Queue statistics. Each output queue has four stats fields. */
  78. struct oct_droq_stats {
  79. /** Number of packets received in this queue. */
  80. u64 pkts_received;
  81. /** Bytes received by this queue. */
  82. u64 bytes_received;
  83. /** Packets dropped due to no dispatch function. */
  84. u64 dropped_nodispatch;
  85. /** Packets dropped due to no memory available. */
  86. u64 dropped_nomem;
  87. /** Packets dropped due to large number of pkts to process. */
  88. u64 dropped_toomany;
  89. /** Number of packets sent to stack from this queue. */
  90. u64 rx_pkts_received;
  91. /** Number of Bytes sent to stack from this queue. */
  92. u64 rx_bytes_received;
  93. /** Num of Packets dropped due to receive path failures. */
  94. u64 rx_dropped;
  95. u64 rx_vxlan;
  96. /** Num of failures of recv_buffer_alloc() */
  97. u64 rx_alloc_failure;
  98. };
  99. /* The maximum number of buffers that can be dispatched from the
  100. * output/dma queue. Set to 64 assuming 1K buffers in DROQ and the fact that
  101. * max packet size from DROQ is 64K.
  102. */
  103. #define MAX_RECV_BUFS 64
  104. /** Receive Packet format used when dispatching output queue packets
  105. * with non-raw opcodes.
  106. * The received packet will be sent to the upper layers using this
  107. * structure which is passed as a parameter to the dispatch function
  108. */
  109. struct octeon_recv_pkt {
  110. /** Number of buffers in this received packet */
  111. u16 buffer_count;
  112. /** Id of the device that is sending the packet up */
  113. u16 octeon_id;
  114. /** Length of data in the packet buffer */
  115. u32 length;
  116. /** The receive header */
  117. union octeon_rh rh;
  118. /** Pointer to the OS-specific packet buffer */
  119. void *buffer_ptr[MAX_RECV_BUFS];
  120. /** Size of the buffers pointed to by ptr's in buffer_ptr */
  121. u32 buffer_size[MAX_RECV_BUFS];
  122. };
  123. #define OCT_RECV_PKT_SIZE (sizeof(struct octeon_recv_pkt))
  124. /** The first parameter of a dispatch function.
  125. * For a raw mode opcode, the driver dispatches with the device
  126. * pointer in this structure.
  127. * For non-raw mode opcode, the driver dispatches the recv_pkt
  128. * created to contain the buffers with data received from Octeon.
  129. * ---------------------
  130. * | *recv_pkt ----|---
  131. * |-------------------| |
  132. * | 0 or more bytes | |
  133. * | reserved by driver| |
  134. * |-------------------|<-/
  135. * | octeon_recv_pkt |
  136. * | |
  137. * |___________________|
  138. */
  139. struct octeon_recv_info {
  140. void *rsvd;
  141. struct octeon_recv_pkt *recv_pkt;
  142. };
  143. #define OCT_RECV_INFO_SIZE (sizeof(struct octeon_recv_info))
  144. /** Allocate a recv_info structure. The recv_pkt pointer in the recv_info
  145. * structure is filled in before this call returns.
  146. * @param extra_bytes - extra bytes to be allocated at the end of the recv info
  147. * structure.
  148. * @return - pointer to a newly allocated recv_info structure.
  149. */
  150. static inline struct octeon_recv_info *octeon_alloc_recv_info(int extra_bytes)
  151. {
  152. struct octeon_recv_info *recv_info;
  153. u8 *buf;
  154. buf = kmalloc(OCT_RECV_PKT_SIZE + OCT_RECV_INFO_SIZE +
  155. extra_bytes, GFP_ATOMIC);
  156. if (!buf)
  157. return NULL;
  158. recv_info = (struct octeon_recv_info *)buf;
  159. recv_info->recv_pkt =
  160. (struct octeon_recv_pkt *)(buf + OCT_RECV_INFO_SIZE);
  161. recv_info->rsvd = NULL;
  162. if (extra_bytes)
  163. recv_info->rsvd = buf + OCT_RECV_INFO_SIZE + OCT_RECV_PKT_SIZE;
  164. return recv_info;
  165. }
  166. /** Free a recv_info structure.
  167. * @param recv_info - Pointer to receive_info to be freed
  168. */
  169. static inline void octeon_free_recv_info(struct octeon_recv_info *recv_info)
  170. {
  171. kfree(recv_info);
  172. }
  173. typedef int (*octeon_dispatch_fn_t)(struct octeon_recv_info *, void *);
  174. /** Used by NIC module to register packet handler and to get device
  175. * information for each octeon device.
  176. */
  177. struct octeon_droq_ops {
  178. /** This registered function will be called by the driver with
  179. * the octeon id, pointer to buffer from droq and length of
  180. * data in the buffer. The receive header gives the port
  181. * number to the caller. Function pointer is set by caller.
  182. */
  183. void (*fptr)(u32, void *, u32, union octeon_rh *, void *, void *);
  184. void *farg;
  185. /* This function will be called by the driver for all NAPI related
  186. * events. The first param is the octeon id. The second param is the
  187. * output queue number. The third is the NAPI event that occurred.
  188. */
  189. void (*napi_fn)(void *);
  190. u32 poll_mode;
  191. /** Flag indicating if the DROQ handler should drop packets that
  192. * it cannot handle in one iteration. Set by caller.
  193. */
  194. u32 drop_on_max;
  195. };
  196. /** The Descriptor Ring Output Queue structure.
  197. * This structure has all the information required to implement a
  198. * Octeon DROQ.
  199. */
  200. struct octeon_droq {
  201. /** A spinlock to protect access to this ring. */
  202. spinlock_t lock;
  203. u32 q_no;
  204. u32 pkt_count;
  205. struct octeon_droq_ops ops;
  206. struct octeon_device *oct_dev;
  207. /** The 8B aligned descriptor ring starts at this address. */
  208. struct octeon_droq_desc *desc_ring;
  209. /** Index in the ring where the driver should read the next packet */
  210. u32 read_idx;
  211. /** Index in the ring where Octeon will write the next packet */
  212. u32 write_idx;
  213. /** Index in the ring where the driver will refill the descriptor's
  214. * buffer
  215. */
  216. u32 refill_idx;
  217. /** Packets pending to be processed */
  218. atomic_t pkts_pending;
  219. /** Number of descriptors in this ring. */
  220. u32 max_count;
  221. /** The number of descriptors pending refill. */
  222. u32 refill_count;
  223. u32 pkts_per_intr;
  224. u32 refill_threshold;
  225. /** The max number of descriptors in DROQ without a buffer.
  226. * This field is used to keep track of empty space threshold. If the
  227. * refill_count reaches this value, the DROQ cannot accept a max-sized
  228. * (64K) packet.
  229. */
  230. u32 max_empty_descs;
  231. /** The receive buffer list. This list has the virtual addresses of the
  232. * buffers.
  233. */
  234. struct octeon_recv_buffer *recv_buf_list;
  235. /** The size of each buffer pointed by the buffer pointer. */
  236. u32 buffer_size;
  237. /** Pointer to the mapped packet credit register.
  238. * Host writes number of info/buffer ptrs available to this register
  239. */
  240. void __iomem *pkts_credit_reg;
  241. /** Pointer to the mapped packet sent register.
  242. * Octeon writes the number of packets DMA'ed to host memory
  243. * in this register.
  244. */
  245. void __iomem *pkts_sent_reg;
  246. struct list_head dispatch_list;
  247. /** Statistics for this DROQ. */
  248. struct oct_droq_stats stats;
  249. /** DMA mapped address of the DROQ descriptor ring. */
  250. size_t desc_ring_dma;
  251. /** application context */
  252. void *app_ctx;
  253. struct napi_struct napi;
  254. u32 cpu_id;
  255. call_single_data_t csd;
  256. };
  257. #define OCT_DROQ_SIZE (sizeof(struct octeon_droq))
  258. /**
  259. * Allocates space for the descriptor ring for the droq and sets the
  260. * base addr, num desc etc in Octeon registers.
  261. *
  262. * @param oct_dev - pointer to the octeon device structure
  263. * @param q_no - droq no. ranges from 0 - 3.
  264. * @param app_ctx - pointer to application context
  265. * @return Success: 0 Failure: 1
  266. */
  267. int octeon_init_droq(struct octeon_device *oct_dev,
  268. u32 q_no,
  269. u32 num_descs,
  270. u32 desc_size,
  271. void *app_ctx);
  272. /**
  273. * Frees the space for descriptor ring for the droq.
  274. *
  275. * @param oct_dev - pointer to the octeon device structure
  276. * @param q_no - droq no. ranges from 0 - 3.
  277. * @return: Success: 0 Failure: 1
  278. */
  279. int octeon_delete_droq(struct octeon_device *oct_dev, u32 q_no);
  280. /** Register a change in droq operations. The ops field has a pointer to a
  281. * function which will called by the DROQ handler for all packets arriving
  282. * on output queues given by q_no irrespective of the type of packet.
  283. * The ops field also has a flag which if set tells the DROQ handler to
  284. * drop packets if it receives more than what it can process in one
  285. * invocation of the handler.
  286. * @param oct - octeon device
  287. * @param q_no - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
  288. * @param ops - the droq_ops settings for this queue
  289. * @return - 0 on success, -ENODEV or -EINVAL on error.
  290. */
  291. int
  292. octeon_register_droq_ops(struct octeon_device *oct,
  293. u32 q_no,
  294. struct octeon_droq_ops *ops);
  295. /** Resets the function pointer and flag settings made by
  296. * octeon_register_droq_ops(). After this routine is called, the DROQ handler
  297. * will lookup dispatch function for each arriving packet on the output queue
  298. * given by q_no.
  299. * @param oct - octeon device
  300. * @param q_no - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
  301. * @return - 0 on success, -ENODEV or -EINVAL on error.
  302. */
  303. int octeon_unregister_droq_ops(struct octeon_device *oct, u32 q_no);
  304. /** Register a dispatch function for a opcode/subcode. The driver will call
  305. * this dispatch function when it receives a packet with the given
  306. * opcode/subcode in its output queues along with the user specified
  307. * argument.
  308. * @param oct - the octeon device to register with.
  309. * @param opcode - the opcode for which the dispatch will be registered.
  310. * @param subcode - the subcode for which the dispatch will be registered
  311. * @param fn - the dispatch function.
  312. * @param fn_arg - user specified that will be passed along with the
  313. * dispatch function by the driver.
  314. * @return Success: 0; Failure: 1
  315. */
  316. int octeon_register_dispatch_fn(struct octeon_device *oct,
  317. u16 opcode,
  318. u16 subcode,
  319. octeon_dispatch_fn_t fn, void *fn_arg);
  320. void *octeon_get_dispatch_arg(struct octeon_device *oct,
  321. u16 opcode, u16 subcode);
  322. void octeon_droq_print_stats(void);
  323. u32 octeon_droq_check_hw_for_pkts(struct octeon_droq *droq);
  324. int octeon_create_droq(struct octeon_device *oct, u32 q_no,
  325. u32 num_descs, u32 desc_size, void *app_ctx);
  326. int octeon_droq_process_packets(struct octeon_device *oct,
  327. struct octeon_droq *droq,
  328. u32 budget);
  329. int octeon_droq_process_poll_pkts(struct octeon_device *oct,
  330. struct octeon_droq *droq, u32 budget);
  331. int octeon_enable_irq(struct octeon_device *oct, u32 q_no);
  332. void octeon_droq_check_oom(struct octeon_droq *droq);
  333. #endif /*__OCTEON_DROQ_H__ */