octeon_main.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_main.h
  19. * \brief Host Driver: This file is included by all host driver source files
  20. * to include common definitions.
  21. */
  22. #ifndef _OCTEON_MAIN_H_
  23. #define _OCTEON_MAIN_H_
  24. #include <linux/sched/signal.h>
  25. #if BITS_PER_LONG == 32
  26. #define CVM_CAST64(v) ((long long)(v))
  27. #elif BITS_PER_LONG == 64
  28. #define CVM_CAST64(v) ((long long)(long)(v))
  29. #else
  30. #error "Unknown system architecture"
  31. #endif
  32. #define DRV_NAME "LiquidIO"
  33. struct octeon_device_priv {
  34. /** Tasklet structures for this device. */
  35. struct tasklet_struct droq_tasklet;
  36. unsigned long napi_mask;
  37. };
  38. /** This structure is used by NIC driver to store information required
  39. * to free the sk_buff when the packet has been fetched by Octeon.
  40. * Bytes offset below assume worst-case of a 64-bit system.
  41. */
  42. struct octnet_buf_free_info {
  43. /** Bytes 1-8. Pointer to network device private structure. */
  44. struct lio *lio;
  45. /** Bytes 9-16. Pointer to sk_buff. */
  46. struct sk_buff *skb;
  47. /** Bytes 17-24. Pointer to gather list. */
  48. struct octnic_gather *g;
  49. /** Bytes 25-32. Physical address of skb->data or gather list. */
  50. u64 dptr;
  51. /** Bytes 33-47. Piggybacked soft command, if any */
  52. struct octeon_soft_command *sc;
  53. };
  54. /* BQL-related functions */
  55. int octeon_report_sent_bytes_to_bql(void *buf, int reqtype);
  56. void octeon_update_tx_completion_counters(void *buf, int reqtype,
  57. unsigned int *pkts_compl,
  58. unsigned int *bytes_compl);
  59. void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
  60. unsigned int bytes_compl);
  61. void octeon_pf_changed_vf_macaddr(struct octeon_device *oct, u8 *mac);
  62. /** Swap 8B blocks */
  63. static inline void octeon_swap_8B_data(u64 *data, u32 blocks)
  64. {
  65. while (blocks) {
  66. cpu_to_be64s(data);
  67. blocks--;
  68. data++;
  69. }
  70. }
  71. /**
  72. * \brief unmaps a PCI BAR
  73. * @param oct Pointer to Octeon device
  74. * @param baridx bar index
  75. */
  76. static inline void octeon_unmap_pci_barx(struct octeon_device *oct, int baridx)
  77. {
  78. dev_dbg(&oct->pci_dev->dev, "Freeing PCI mapped regions for Bar%d\n",
  79. baridx);
  80. if (oct->mmio[baridx].done)
  81. iounmap(oct->mmio[baridx].hw_addr);
  82. if (oct->mmio[baridx].start)
  83. pci_release_region(oct->pci_dev, baridx * 2);
  84. }
  85. /**
  86. * \brief maps a PCI BAR
  87. * @param oct Pointer to Octeon device
  88. * @param baridx bar index
  89. * @param max_map_len maximum length of mapped memory
  90. */
  91. static inline int octeon_map_pci_barx(struct octeon_device *oct,
  92. int baridx, int max_map_len)
  93. {
  94. u32 mapped_len = 0;
  95. if (pci_request_region(oct->pci_dev, baridx * 2, DRV_NAME)) {
  96. dev_err(&oct->pci_dev->dev, "pci_request_region failed for bar %d\n",
  97. baridx);
  98. return 1;
  99. }
  100. oct->mmio[baridx].start = pci_resource_start(oct->pci_dev, baridx * 2);
  101. oct->mmio[baridx].len = pci_resource_len(oct->pci_dev, baridx * 2);
  102. mapped_len = oct->mmio[baridx].len;
  103. if (!mapped_len)
  104. goto err_release_region;
  105. if (max_map_len && (mapped_len > max_map_len))
  106. mapped_len = max_map_len;
  107. oct->mmio[baridx].hw_addr =
  108. ioremap(oct->mmio[baridx].start, mapped_len);
  109. oct->mmio[baridx].mapped_len = mapped_len;
  110. dev_dbg(&oct->pci_dev->dev, "BAR%d start: 0x%llx mapped %u of %u bytes\n",
  111. baridx, oct->mmio[baridx].start, mapped_len,
  112. oct->mmio[baridx].len);
  113. if (!oct->mmio[baridx].hw_addr) {
  114. dev_err(&oct->pci_dev->dev, "error ioremap for bar %d\n",
  115. baridx);
  116. goto err_release_region;
  117. }
  118. oct->mmio[baridx].done = 1;
  119. return 0;
  120. err_release_region:
  121. pci_release_region(oct->pci_dev, baridx * 2);
  122. return 1;
  123. }
  124. static inline int
  125. sleep_cond(wait_queue_head_t *wait_queue, int *condition)
  126. {
  127. int errno = 0;
  128. wait_queue_entry_t we;
  129. init_waitqueue_entry(&we, current);
  130. add_wait_queue(wait_queue, &we);
  131. while (!(READ_ONCE(*condition))) {
  132. set_current_state(TASK_INTERRUPTIBLE);
  133. if (signal_pending(current)) {
  134. errno = -EINTR;
  135. goto out;
  136. }
  137. schedule();
  138. }
  139. out:
  140. set_current_state(TASK_RUNNING);
  141. remove_wait_queue(wait_queue, &we);
  142. return errno;
  143. }
  144. /* Gives up the CPU for a timeout period.
  145. * Check that the condition is not true before we go to sleep for a
  146. * timeout period.
  147. */
  148. static inline void
  149. sleep_timeout_cond(wait_queue_head_t *wait_queue,
  150. int *condition,
  151. int timeout)
  152. {
  153. wait_queue_entry_t we;
  154. init_waitqueue_entry(&we, current);
  155. add_wait_queue(wait_queue, &we);
  156. set_current_state(TASK_INTERRUPTIBLE);
  157. if (!(*condition))
  158. schedule_timeout(timeout);
  159. set_current_state(TASK_RUNNING);
  160. remove_wait_queue(wait_queue, &we);
  161. }
  162. #ifndef ROUNDUP4
  163. #define ROUNDUP4(val) (((val) + 3) & 0xfffffffc)
  164. #endif
  165. #ifndef ROUNDUP8
  166. #define ROUNDUP8(val) (((val) + 7) & 0xfffffff8)
  167. #endif
  168. #ifndef ROUNDUP16
  169. #define ROUNDUP16(val) (((val) + 15) & 0xfffffff0)
  170. #endif
  171. #ifndef ROUNDUP128
  172. #define ROUNDUP128(val) (((val) + 127) & 0xffffff80)
  173. #endif
  174. #endif /* _OCTEON_MAIN_H_ */