pio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #ifndef _PIO_H
  2. #define _PIO_H
  3. /*
  4. * Copyright(c) 2015-2017 Intel Corporation.
  5. *
  6. * This file is provided under a dual BSD/GPLv2 license. When using or
  7. * redistributing this file, you may do so under either license.
  8. *
  9. * GPL LICENSE SUMMARY
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * - Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * - Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * - Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. /* send context types */
  50. #define SC_KERNEL 0
  51. #define SC_VL15 1
  52. #define SC_ACK 2
  53. #define SC_USER 3 /* must be the last one: it may take all left */
  54. #define SC_MAX 4 /* count of send context types */
  55. /*
  56. * SC_VNIC types are allocated (dynamically) from the user context pool,
  57. * (SC_USER) and used by kernel driver as kernel contexts (SC_KERNEL).
  58. */
  59. #define SC_VNIC SC_MAX
  60. /* invalid send context index */
  61. #define INVALID_SCI 0xff
  62. /* PIO buffer release callback function */
  63. typedef void (*pio_release_cb)(void *arg, int code);
  64. /* PIO release codes - in bits, as there could more than one that apply */
  65. #define PRC_OK 0 /* no known error */
  66. #define PRC_STATUS_ERR 0x01 /* credit return due to status error */
  67. #define PRC_PBC 0x02 /* credit return due to PBC */
  68. #define PRC_THRESHOLD 0x04 /* credit return due to threshold */
  69. #define PRC_FILL_ERR 0x08 /* credit return due fill error */
  70. #define PRC_FORCE 0x10 /* credit return due credit force */
  71. #define PRC_SC_DISABLE 0x20 /* clean-up after a context disable */
  72. /* byte helper */
  73. union mix {
  74. u64 val64;
  75. u32 val32[2];
  76. u8 val8[8];
  77. };
  78. /* an allocated PIO buffer */
  79. struct pio_buf {
  80. struct send_context *sc;/* back pointer to owning send context */
  81. pio_release_cb cb; /* called when the buffer is released */
  82. void *arg; /* argument for cb */
  83. void __iomem *start; /* buffer start address */
  84. void __iomem *end; /* context end address */
  85. unsigned long sent_at; /* buffer is sent when <= free */
  86. union mix carry; /* pending unwritten bytes */
  87. u16 qw_written; /* QW written so far */
  88. u8 carry_bytes; /* number of valid bytes in carry */
  89. };
  90. /* cache line aligned pio buffer array */
  91. union pio_shadow_ring {
  92. struct pio_buf pbuf;
  93. } ____cacheline_aligned;
  94. /* per-NUMA send context */
  95. struct send_context {
  96. /* read-only after init */
  97. struct hfi1_devdata *dd; /* device */
  98. union pio_shadow_ring *sr; /* shadow ring */
  99. void __iomem *base_addr; /* start of PIO memory */
  100. u32 __percpu *buffers_allocated;/* count of buffers allocated */
  101. u32 size; /* context size, in bytes */
  102. int node; /* context home node */
  103. u32 sr_size; /* size of the shadow ring */
  104. u16 flags; /* flags */
  105. u8 type; /* context type */
  106. u8 sw_index; /* software index number */
  107. u8 hw_context; /* hardware context number */
  108. u8 group; /* credit return group */
  109. /* allocator fields */
  110. spinlock_t alloc_lock ____cacheline_aligned_in_smp;
  111. u32 sr_head; /* shadow ring head */
  112. unsigned long fill; /* official alloc count */
  113. unsigned long alloc_free; /* copy of free (less cache thrash) */
  114. u32 fill_wrap; /* tracks fill within ring */
  115. u32 credits; /* number of blocks in context */
  116. /* adding a new field here would make it part of this cacheline */
  117. /* releaser fields */
  118. spinlock_t release_lock ____cacheline_aligned_in_smp;
  119. u32 sr_tail; /* shadow ring tail */
  120. unsigned long free; /* official free count */
  121. volatile __le64 *hw_free; /* HW free counter */
  122. /* list for PIO waiters */
  123. struct list_head piowait ____cacheline_aligned_in_smp;
  124. spinlock_t credit_ctrl_lock ____cacheline_aligned_in_smp;
  125. u32 credit_intr_count; /* count of credit intr users */
  126. u64 credit_ctrl; /* cache for credit control */
  127. wait_queue_head_t halt_wait; /* wait until kernel sees interrupt */
  128. struct work_struct halt_work; /* halted context work queue entry */
  129. };
  130. /* send context flags */
  131. #define SCF_ENABLED 0x01
  132. #define SCF_IN_FREE 0x02
  133. #define SCF_HALTED 0x04
  134. #define SCF_FROZEN 0x08
  135. struct send_context_info {
  136. struct send_context *sc; /* allocated working context */
  137. u16 allocated; /* has this been allocated? */
  138. u16 type; /* context type */
  139. u16 base; /* base in PIO array */
  140. u16 credits; /* size in PIO array */
  141. };
  142. /* DMA credit return, index is always (context & 0x7) */
  143. struct credit_return {
  144. volatile __le64 cr[8];
  145. };
  146. /* NUMA indexed credit return array */
  147. struct credit_return_base {
  148. struct credit_return *va;
  149. dma_addr_t dma;
  150. };
  151. /* send context configuration sizes (one per type) */
  152. struct sc_config_sizes {
  153. short int size;
  154. short int count;
  155. };
  156. /*
  157. * The diagram below details the relationship of the mapping structures
  158. *
  159. * Since the mapping now allows for non-uniform send contexts per vl, the
  160. * number of send contexts for a vl is either the vl_scontexts[vl] or
  161. * a computation based on num_kernel_send_contexts/num_vls:
  162. *
  163. * For example:
  164. * nactual = vl_scontexts ? vl_scontexts[vl] : num_kernel_send_contexts/num_vls
  165. *
  166. * n = roundup to next highest power of 2 using nactual
  167. *
  168. * In the case where there are num_kernel_send_contexts/num_vls doesn't divide
  169. * evenly, the extras are added from the last vl downward.
  170. *
  171. * For the case where n > nactual, the send contexts are assigned
  172. * in a round robin fashion wrapping back to the first send context
  173. * for a particular vl.
  174. *
  175. * dd->pio_map
  176. * | pio_map_elem[0]
  177. * | +--------------------+
  178. * v | mask |
  179. * pio_vl_map |--------------------|
  180. * +--------------------------+ | ksc[0] -> sc 1 |
  181. * | list (RCU) | |--------------------|
  182. * |--------------------------| ->| ksc[1] -> sc 2 |
  183. * | mask | --/ |--------------------|
  184. * |--------------------------| -/ | * |
  185. * | actual_vls (max 8) | -/ |--------------------|
  186. * |--------------------------| --/ | ksc[n-1] -> sc n |
  187. * | vls (max 8) | -/ +--------------------+
  188. * |--------------------------| --/
  189. * | map[0] |-/
  190. * |--------------------------| +--------------------+
  191. * | map[1] |--- | mask |
  192. * |--------------------------| \---- |--------------------|
  193. * | * | \-- | ksc[0] -> sc 1+n |
  194. * | * | \---- |--------------------|
  195. * | * | \->| ksc[1] -> sc 2+n |
  196. * |--------------------------| |--------------------|
  197. * | map[vls - 1] |- | * |
  198. * +--------------------------+ \- |--------------------|
  199. * \- | ksc[m-1] -> sc m+n |
  200. * \ +--------------------+
  201. * \-
  202. * \
  203. * \- +----------------------+
  204. * \- | mask |
  205. * \ |----------------------|
  206. * \- | ksc[0] -> sc 1+m+n |
  207. * \- |----------------------|
  208. * >| ksc[1] -> sc 2+m+n |
  209. * |----------------------|
  210. * | * |
  211. * |----------------------|
  212. * | ksc[o-1] -> sc o+m+n |
  213. * +----------------------+
  214. *
  215. */
  216. /* Initial number of send contexts per VL */
  217. #define INIT_SC_PER_VL 2
  218. /*
  219. * struct pio_map_elem - mapping for a vl
  220. * @mask - selector mask
  221. * @ksc - array of kernel send contexts for this vl
  222. *
  223. * The mask is used to "mod" the selector to
  224. * produce index into the trailing array of
  225. * kscs
  226. */
  227. struct pio_map_elem {
  228. u32 mask;
  229. struct send_context *ksc[0];
  230. };
  231. /*
  232. * struct pio_vl_map - mapping for a vl
  233. * @list - rcu head for free callback
  234. * @mask - vl mask to "mod" the vl to produce an index to map array
  235. * @actual_vls - number of vls
  236. * @vls - numbers of vls rounded to next power of 2
  237. * @map - array of pio_map_elem entries
  238. *
  239. * This is the parent mapping structure. The trailing members of the
  240. * struct point to pio_map_elem entries, which in turn point to an
  241. * array of kscs for that vl.
  242. */
  243. struct pio_vl_map {
  244. struct rcu_head list;
  245. u32 mask;
  246. u8 actual_vls;
  247. u8 vls;
  248. struct pio_map_elem *map[0];
  249. };
  250. int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls,
  251. u8 *vl_scontexts);
  252. void free_pio_map(struct hfi1_devdata *dd);
  253. struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
  254. u32 selector, u8 vl);
  255. struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
  256. u32 selector, u8 sc5);
  257. /* send context functions */
  258. int init_credit_return(struct hfi1_devdata *dd);
  259. void free_credit_return(struct hfi1_devdata *dd);
  260. int init_sc_pools_and_sizes(struct hfi1_devdata *dd);
  261. int init_send_contexts(struct hfi1_devdata *dd);
  262. int init_credit_return(struct hfi1_devdata *dd);
  263. int init_pervl_scs(struct hfi1_devdata *dd);
  264. struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
  265. uint hdrqentsize, int numa);
  266. void sc_free(struct send_context *sc);
  267. int sc_enable(struct send_context *sc);
  268. void sc_disable(struct send_context *sc);
  269. int sc_restart(struct send_context *sc);
  270. void sc_return_credits(struct send_context *sc);
  271. void sc_flush(struct send_context *sc);
  272. void sc_drop(struct send_context *sc);
  273. void sc_stop(struct send_context *sc, int bit);
  274. struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
  275. pio_release_cb cb, void *arg);
  276. void sc_release_update(struct send_context *sc);
  277. void sc_return_credits(struct send_context *sc);
  278. void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context);
  279. void sc_add_credit_return_intr(struct send_context *sc);
  280. void sc_del_credit_return_intr(struct send_context *sc);
  281. void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold);
  282. u32 sc_percent_to_threshold(struct send_context *sc, u32 percent);
  283. u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize);
  284. void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint);
  285. void sc_wait(struct hfi1_devdata *dd);
  286. void set_pio_integrity(struct send_context *sc);
  287. /* support functions */
  288. void pio_reset_all(struct hfi1_devdata *dd);
  289. void pio_freeze(struct hfi1_devdata *dd);
  290. void pio_kernel_unfreeze(struct hfi1_devdata *dd);
  291. /* global PIO send control operations */
  292. #define PSC_GLOBAL_ENABLE 0
  293. #define PSC_GLOBAL_DISABLE 1
  294. #define PSC_GLOBAL_VLARB_ENABLE 2
  295. #define PSC_GLOBAL_VLARB_DISABLE 3
  296. #define PSC_CM_RESET 4
  297. #define PSC_DATA_VL_ENABLE 5
  298. #define PSC_DATA_VL_DISABLE 6
  299. void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl);
  300. void pio_send_control(struct hfi1_devdata *dd, int op);
  301. /* PIO copy routines */
  302. void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc,
  303. const void *from, size_t count);
  304. void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc,
  305. const void *from, size_t nbytes);
  306. void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes);
  307. void seg_pio_copy_end(struct pio_buf *pbuf);
  308. #endif /* _PIO_H */