octeon_droq.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 Output Receive Header. */
  49. union octeon_rh rh;
  50. /** The Length of the packet. */
  51. u64 length;
  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. #define POLL_EVENT_INTR_ARRIVED 1
  100. #define POLL_EVENT_PROCESS_PKTS 2
  101. #define POLL_EVENT_PENDING_PKTS 3
  102. #define POLL_EVENT_ENABLE_INTR 4
  103. /* The maximum number of buffers that can be dispatched from the
  104. * output/dma queue. Set to 64 assuming 1K buffers in DROQ and the fact that
  105. * max packet size from DROQ is 64K.
  106. */
  107. #define MAX_RECV_BUFS 64
  108. /** Receive Packet format used when dispatching output queue packets
  109. * with non-raw opcodes.
  110. * The received packet will be sent to the upper layers using this
  111. * structure which is passed as a parameter to the dispatch function
  112. */
  113. struct octeon_recv_pkt {
  114. /** Number of buffers in this received packet */
  115. u16 buffer_count;
  116. /** Id of the device that is sending the packet up */
  117. u16 octeon_id;
  118. /** Length of data in the packet buffer */
  119. u32 length;
  120. /** The receive header */
  121. union octeon_rh rh;
  122. /** Pointer to the OS-specific packet buffer */
  123. void *buffer_ptr[MAX_RECV_BUFS];
  124. /** Size of the buffers pointed to by ptr's in buffer_ptr */
  125. u32 buffer_size[MAX_RECV_BUFS];
  126. };
  127. #define OCT_RECV_PKT_SIZE (sizeof(struct octeon_recv_pkt))
  128. /** The first parameter of a dispatch function.
  129. * For a raw mode opcode, the driver dispatches with the device
  130. * pointer in this structure.
  131. * For non-raw mode opcode, the driver dispatches the recv_pkt
  132. * created to contain the buffers with data received from Octeon.
  133. * ---------------------
  134. * | *recv_pkt ----|---
  135. * |-------------------| |
  136. * | 0 or more bytes | |
  137. * | reserved by driver| |
  138. * |-------------------|<-/
  139. * | octeon_recv_pkt |
  140. * | |
  141. * |___________________|
  142. */
  143. struct octeon_recv_info {
  144. void *rsvd;
  145. struct octeon_recv_pkt *recv_pkt;
  146. };
  147. #define OCT_RECV_INFO_SIZE (sizeof(struct octeon_recv_info))
  148. /** Allocate a recv_info structure. The recv_pkt pointer in the recv_info
  149. * structure is filled in before this call returns.
  150. * @param extra_bytes - extra bytes to be allocated at the end of the recv info
  151. * structure.
  152. * @return - pointer to a newly allocated recv_info structure.
  153. */
  154. static inline struct octeon_recv_info *octeon_alloc_recv_info(int extra_bytes)
  155. {
  156. struct octeon_recv_info *recv_info;
  157. u8 *buf;
  158. buf = kmalloc(OCT_RECV_PKT_SIZE + OCT_RECV_INFO_SIZE +
  159. extra_bytes, GFP_ATOMIC);
  160. if (!buf)
  161. return NULL;
  162. recv_info = (struct octeon_recv_info *)buf;
  163. recv_info->recv_pkt =
  164. (struct octeon_recv_pkt *)(buf + OCT_RECV_INFO_SIZE);
  165. recv_info->rsvd = NULL;
  166. if (extra_bytes)
  167. recv_info->rsvd = buf + OCT_RECV_INFO_SIZE + OCT_RECV_PKT_SIZE;
  168. return recv_info;
  169. }
  170. /** Free a recv_info structure.
  171. * @param recv_info - Pointer to receive_info to be freed
  172. */
  173. static inline void octeon_free_recv_info(struct octeon_recv_info *recv_info)
  174. {
  175. kfree(recv_info);
  176. }
  177. typedef int (*octeon_dispatch_fn_t)(struct octeon_recv_info *, void *);
  178. /** Used by NIC module to register packet handler and to get device
  179. * information for each octeon device.
  180. */
  181. struct octeon_droq_ops {
  182. /** This registered function will be called by the driver with
  183. * the octeon id, pointer to buffer from droq and length of
  184. * data in the buffer. The receive header gives the port
  185. * number to the caller. Function pointer is set by caller.
  186. */
  187. void (*fptr)(u32, void *, u32, union octeon_rh *, void *, void *);
  188. void *farg;
  189. /* This function will be called by the driver for all NAPI related
  190. * events. The first param is the octeon id. The second param is the
  191. * output queue number. The third is the NAPI event that occurred.
  192. */
  193. void (*napi_fn)(void *);
  194. u32 poll_mode;
  195. /** Flag indicating if the DROQ handler should drop packets that
  196. * it cannot handle in one iteration. Set by caller.
  197. */
  198. u32 drop_on_max;
  199. };
  200. /** The Descriptor Ring Output Queue structure.
  201. * This structure has all the information required to implement a
  202. * Octeon DROQ.
  203. */
  204. struct octeon_droq {
  205. /** A spinlock to protect access to this ring. */
  206. spinlock_t lock;
  207. u32 q_no;
  208. u32 pkt_count;
  209. struct octeon_droq_ops ops;
  210. struct octeon_device *oct_dev;
  211. /** The 8B aligned descriptor ring starts at this address. */
  212. struct octeon_droq_desc *desc_ring;
  213. /** Index in the ring where the driver should read the next packet */
  214. u32 read_idx;
  215. /** Index in the ring where Octeon will write the next packet */
  216. u32 write_idx;
  217. /** Index in the ring where the driver will refill the descriptor's
  218. * buffer
  219. */
  220. u32 refill_idx;
  221. /** Packets pending to be processed */
  222. atomic_t pkts_pending;
  223. /** Number of descriptors in this ring. */
  224. u32 max_count;
  225. /** The number of descriptors pending refill. */
  226. u32 refill_count;
  227. u32 pkts_per_intr;
  228. u32 refill_threshold;
  229. /** The max number of descriptors in DROQ without a buffer.
  230. * This field is used to keep track of empty space threshold. If the
  231. * refill_count reaches this value, the DROQ cannot accept a max-sized
  232. * (64K) packet.
  233. */
  234. u32 max_empty_descs;
  235. /** The 8B aligned info ptrs begin from this address. */
  236. struct octeon_droq_info *info_list;
  237. /** The receive buffer list. This list has the virtual addresses of the
  238. * buffers.
  239. */
  240. struct octeon_recv_buffer *recv_buf_list;
  241. /** The size of each buffer pointed by the buffer pointer. */
  242. u32 buffer_size;
  243. /** Pointer to the mapped packet credit register.
  244. * Host writes number of info/buffer ptrs available to this register
  245. */
  246. void __iomem *pkts_credit_reg;
  247. /** Pointer to the mapped packet sent register.
  248. * Octeon writes the number of packets DMA'ed to host memory
  249. * in this register.
  250. */
  251. void __iomem *pkts_sent_reg;
  252. struct list_head dispatch_list;
  253. /** Statistics for this DROQ. */
  254. struct oct_droq_stats stats;
  255. /** DMA mapped address of the DROQ descriptor ring. */
  256. size_t desc_ring_dma;
  257. /** Info ptr list are allocated at this virtual address. */
  258. size_t info_base_addr;
  259. /** DMA mapped address of the info list */
  260. size_t info_list_dma;
  261. /** Allocated size of info list. */
  262. u32 info_alloc_size;
  263. /** application context */
  264. void *app_ctx;
  265. struct napi_struct napi;
  266. u32 cpu_id;
  267. struct call_single_data csd;
  268. };
  269. #define OCT_DROQ_SIZE (sizeof(struct octeon_droq))
  270. /**
  271. * Allocates space for the descriptor ring for the droq and sets the
  272. * base addr, num desc etc in Octeon registers.
  273. *
  274. * @param oct_dev - pointer to the octeon device structure
  275. * @param q_no - droq no. ranges from 0 - 3.
  276. * @param app_ctx - pointer to application context
  277. * @return Success: 0 Failure: 1
  278. */
  279. int octeon_init_droq(struct octeon_device *oct_dev,
  280. u32 q_no,
  281. u32 num_descs,
  282. u32 desc_size,
  283. void *app_ctx);
  284. /**
  285. * Frees the space for descriptor ring for the droq.
  286. *
  287. * @param oct_dev - pointer to the octeon device structure
  288. * @param q_no - droq no. ranges from 0 - 3.
  289. * @return: Success: 0 Failure: 1
  290. */
  291. int octeon_delete_droq(struct octeon_device *oct_dev, u32 q_no);
  292. /** Register a change in droq operations. The ops field has a pointer to a
  293. * function which will called by the DROQ handler for all packets arriving
  294. * on output queues given by q_no irrespective of the type of packet.
  295. * The ops field also has a flag which if set tells the DROQ handler to
  296. * drop packets if it receives more than what it can process in one
  297. * invocation of the handler.
  298. * @param oct - octeon device
  299. * @param q_no - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
  300. * @param ops - the droq_ops settings for this queue
  301. * @return - 0 on success, -ENODEV or -EINVAL on error.
  302. */
  303. int
  304. octeon_register_droq_ops(struct octeon_device *oct,
  305. u32 q_no,
  306. struct octeon_droq_ops *ops);
  307. /** Resets the function pointer and flag settings made by
  308. * octeon_register_droq_ops(). After this routine is called, the DROQ handler
  309. * will lookup dispatch function for each arriving packet on the output queue
  310. * given by q_no.
  311. * @param oct - octeon device
  312. * @param q_no - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
  313. * @return - 0 on success, -ENODEV or -EINVAL on error.
  314. */
  315. int octeon_unregister_droq_ops(struct octeon_device *oct, u32 q_no);
  316. /** Register a dispatch function for a opcode/subcode. The driver will call
  317. * this dispatch function when it receives a packet with the given
  318. * opcode/subcode in its output queues along with the user specified
  319. * argument.
  320. * @param oct - the octeon device to register with.
  321. * @param opcode - the opcode for which the dispatch will be registered.
  322. * @param subcode - the subcode for which the dispatch will be registered
  323. * @param fn - the dispatch function.
  324. * @param fn_arg - user specified that will be passed along with the
  325. * dispatch function by the driver.
  326. * @return Success: 0; Failure: 1
  327. */
  328. int octeon_register_dispatch_fn(struct octeon_device *oct,
  329. u16 opcode,
  330. u16 subcode,
  331. octeon_dispatch_fn_t fn, void *fn_arg);
  332. void octeon_droq_print_stats(void);
  333. u32 octeon_droq_check_hw_for_pkts(struct octeon_droq *droq);
  334. int octeon_create_droq(struct octeon_device *oct, u32 q_no,
  335. u32 num_descs, u32 desc_size, void *app_ctx);
  336. int octeon_droq_process_packets(struct octeon_device *oct,
  337. struct octeon_droq *droq,
  338. u32 budget);
  339. int octeon_process_droq_poll_cmd(struct octeon_device *oct, u32 q_no,
  340. int cmd, u32 arg);
  341. #endif /*__OCTEON_DROQ_H__ */