octeon_main.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2015 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
  17. * details.
  18. *
  19. * This file may also be available under a different license from Cavium.
  20. * Contact Cavium, Inc. for more information
  21. **********************************************************************/
  22. /*! \file octeon_main.h
  23. * \brief Host Driver: This file is included by all host driver source files
  24. * to include common definitions.
  25. */
  26. #ifndef _OCTEON_MAIN_H_
  27. #define _OCTEON_MAIN_H_
  28. #if BITS_PER_LONG == 32
  29. #define CVM_CAST64(v) ((long long)(v))
  30. #elif BITS_PER_LONG == 64
  31. #define CVM_CAST64(v) ((long long)(long)(v))
  32. #else
  33. #error "Unknown system architecture"
  34. #endif
  35. #define DRV_NAME "LiquidIO"
  36. /** This structure is used by NIC driver to store information required
  37. * to free the sk_buff when the packet has been fetched by Octeon.
  38. * Bytes offset below assume worst-case of a 64-bit system.
  39. */
  40. struct octnet_buf_free_info {
  41. /** Bytes 1-8. Pointer to network device private structure. */
  42. struct lio *lio;
  43. /** Bytes 9-16. Pointer to sk_buff. */
  44. struct sk_buff *skb;
  45. /** Bytes 17-24. Pointer to gather list. */
  46. struct octnic_gather *g;
  47. /** Bytes 25-32. Physical address of skb->data or gather list. */
  48. u64 dptr;
  49. /** Bytes 33-47. Piggybacked soft command, if any */
  50. struct octeon_soft_command *sc;
  51. };
  52. /* BQL-related functions */
  53. void octeon_report_sent_bytes_to_bql(void *buf, int reqtype);
  54. void octeon_update_tx_completion_counters(void *buf, int reqtype,
  55. unsigned int *pkts_compl,
  56. unsigned int *bytes_compl);
  57. void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
  58. unsigned int bytes_compl);
  59. /** Swap 8B blocks */
  60. static inline void octeon_swap_8B_data(u64 *data, u32 blocks)
  61. {
  62. while (blocks) {
  63. cpu_to_be64s(data);
  64. blocks--;
  65. data++;
  66. }
  67. }
  68. /**
  69. * \brief unmaps a PCI BAR
  70. * @param oct Pointer to Octeon device
  71. * @param baridx bar index
  72. */
  73. static inline void octeon_unmap_pci_barx(struct octeon_device *oct, int baridx)
  74. {
  75. dev_dbg(&oct->pci_dev->dev, "Freeing PCI mapped regions for Bar%d\n",
  76. baridx);
  77. if (oct->mmio[baridx].done)
  78. iounmap(oct->mmio[baridx].hw_addr);
  79. if (oct->mmio[baridx].start)
  80. pci_release_region(oct->pci_dev, baridx * 2);
  81. }
  82. /**
  83. * \brief maps a PCI BAR
  84. * @param oct Pointer to Octeon device
  85. * @param baridx bar index
  86. * @param max_map_len maximum length of mapped memory
  87. */
  88. static inline int octeon_map_pci_barx(struct octeon_device *oct,
  89. int baridx, int max_map_len)
  90. {
  91. u32 mapped_len = 0;
  92. if (pci_request_region(oct->pci_dev, baridx * 2, DRV_NAME)) {
  93. dev_err(&oct->pci_dev->dev, "pci_request_region failed for bar %d\n",
  94. baridx);
  95. return 1;
  96. }
  97. oct->mmio[baridx].start = pci_resource_start(oct->pci_dev, baridx * 2);
  98. oct->mmio[baridx].len = pci_resource_len(oct->pci_dev, baridx * 2);
  99. mapped_len = oct->mmio[baridx].len;
  100. if (!mapped_len)
  101. return 1;
  102. if (max_map_len && (mapped_len > max_map_len))
  103. mapped_len = max_map_len;
  104. oct->mmio[baridx].hw_addr =
  105. ioremap(oct->mmio[baridx].start, mapped_len);
  106. oct->mmio[baridx].mapped_len = mapped_len;
  107. dev_dbg(&oct->pci_dev->dev, "BAR%d start: 0x%llx mapped %u of %u bytes\n",
  108. baridx, oct->mmio[baridx].start, mapped_len,
  109. oct->mmio[baridx].len);
  110. if (!oct->mmio[baridx].hw_addr) {
  111. dev_err(&oct->pci_dev->dev, "error ioremap for bar %d\n",
  112. baridx);
  113. return 1;
  114. }
  115. oct->mmio[baridx].done = 1;
  116. return 0;
  117. }
  118. static inline void *
  119. cnnic_numa_alloc_aligned_dma(u32 size,
  120. u32 *alloc_size,
  121. size_t *orig_ptr,
  122. int numa_node)
  123. {
  124. int retries = 0;
  125. void *ptr = NULL;
  126. #define OCTEON_MAX_ALLOC_RETRIES 1
  127. do {
  128. struct page *page = NULL;
  129. page = alloc_pages_node(numa_node,
  130. GFP_KERNEL,
  131. get_order(size));
  132. if (!page)
  133. page = alloc_pages(GFP_KERNEL,
  134. get_order(size));
  135. ptr = (void *)page_address(page);
  136. if ((unsigned long)ptr & 0x07) {
  137. __free_pages(page, get_order(size));
  138. ptr = NULL;
  139. /* Increment the size required if the first
  140. * attempt failed.
  141. */
  142. if (!retries)
  143. size += 7;
  144. }
  145. retries++;
  146. } while ((retries <= OCTEON_MAX_ALLOC_RETRIES) && !ptr);
  147. *alloc_size = size;
  148. *orig_ptr = (unsigned long)ptr;
  149. if ((unsigned long)ptr & 0x07)
  150. ptr = (void *)(((unsigned long)ptr + 7) & ~(7UL));
  151. return ptr;
  152. }
  153. #define cnnic_free_aligned_dma(pci_dev, ptr, size, orig_ptr, dma_addr) \
  154. free_pages(orig_ptr, get_order(size))
  155. static inline int
  156. sleep_cond(wait_queue_head_t *wait_queue, int *condition)
  157. {
  158. int errno = 0;
  159. wait_queue_t we;
  160. init_waitqueue_entry(&we, current);
  161. add_wait_queue(wait_queue, &we);
  162. while (!(READ_ONCE(*condition))) {
  163. set_current_state(TASK_INTERRUPTIBLE);
  164. if (signal_pending(current)) {
  165. errno = -EINTR;
  166. goto out;
  167. }
  168. schedule();
  169. }
  170. out:
  171. set_current_state(TASK_RUNNING);
  172. remove_wait_queue(wait_queue, &we);
  173. return errno;
  174. }
  175. static inline void
  176. sleep_atomic_cond(wait_queue_head_t *waitq, atomic_t *pcond)
  177. {
  178. wait_queue_t we;
  179. init_waitqueue_entry(&we, current);
  180. add_wait_queue(waitq, &we);
  181. while (!atomic_read(pcond)) {
  182. set_current_state(TASK_INTERRUPTIBLE);
  183. if (signal_pending(current))
  184. goto out;
  185. schedule();
  186. }
  187. out:
  188. set_current_state(TASK_RUNNING);
  189. remove_wait_queue(waitq, &we);
  190. }
  191. /* Gives up the CPU for a timeout period.
  192. * Check that the condition is not true before we go to sleep for a
  193. * timeout period.
  194. */
  195. static inline void
  196. sleep_timeout_cond(wait_queue_head_t *wait_queue,
  197. int *condition,
  198. int timeout)
  199. {
  200. wait_queue_t we;
  201. init_waitqueue_entry(&we, current);
  202. add_wait_queue(wait_queue, &we);
  203. set_current_state(TASK_INTERRUPTIBLE);
  204. if (!(*condition))
  205. schedule_timeout(timeout);
  206. set_current_state(TASK_RUNNING);
  207. remove_wait_queue(wait_queue, &we);
  208. }
  209. #ifndef ROUNDUP4
  210. #define ROUNDUP4(val) (((val) + 3) & 0xfffffffc)
  211. #endif
  212. #ifndef ROUNDUP8
  213. #define ROUNDUP8(val) (((val) + 7) & 0xfffffff8)
  214. #endif
  215. #ifndef ROUNDUP16
  216. #define ROUNDUP16(val) (((val) + 15) & 0xfffffff0)
  217. #endif
  218. #ifndef ROUNDUP128
  219. #define ROUNDUP128(val) (((val) + 127) & 0xffffff80)
  220. #endif
  221. #endif /* _OCTEON_MAIN_H_ */