pio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. /* invalid send context index */
  56. #define INVALID_SCI 0xff
  57. /* PIO buffer release callback function */
  58. typedef void (*pio_release_cb)(void *arg, int code);
  59. /* PIO release codes - in bits, as there could more than one that apply */
  60. #define PRC_OK 0 /* no known error */
  61. #define PRC_STATUS_ERR 0x01 /* credit return due to status error */
  62. #define PRC_PBC 0x02 /* credit return due to PBC */
  63. #define PRC_THRESHOLD 0x04 /* credit return due to threshold */
  64. #define PRC_FILL_ERR 0x08 /* credit return due fill error */
  65. #define PRC_FORCE 0x10 /* credit return due credit force */
  66. #define PRC_SC_DISABLE 0x20 /* clean-up after a context disable */
  67. /* byte helper */
  68. union mix {
  69. u64 val64;
  70. u32 val32[2];
  71. u8 val8[8];
  72. };
  73. /* an allocated PIO buffer */
  74. struct pio_buf {
  75. struct send_context *sc;/* back pointer to owning send context */
  76. pio_release_cb cb; /* called when the buffer is released */
  77. void *arg; /* argument for cb */
  78. void __iomem *start; /* buffer start address */
  79. void __iomem *end; /* context end address */
  80. unsigned long sent_at; /* buffer is sent when <= free */
  81. union mix carry; /* pending unwritten bytes */
  82. u16 qw_written; /* QW written so far */
  83. u8 carry_bytes; /* number of valid bytes in carry */
  84. };
  85. /* cache line aligned pio buffer array */
  86. union pio_shadow_ring {
  87. struct pio_buf pbuf;
  88. } ____cacheline_aligned;
  89. /* per-NUMA send context */
  90. struct send_context {
  91. /* read-only after init */
  92. struct hfi1_devdata *dd; /* device */
  93. union pio_shadow_ring *sr; /* shadow ring */
  94. void __iomem *base_addr; /* start of PIO memory */
  95. u32 __percpu *buffers_allocated;/* count of buffers allocated */
  96. u32 size; /* context size, in bytes */
  97. int node; /* context home node */
  98. u32 sr_size; /* size of the shadow ring */
  99. u16 flags; /* flags */
  100. u8 type; /* context type */
  101. u8 sw_index; /* software index number */
  102. u8 hw_context; /* hardware context number */
  103. u8 group; /* credit return group */
  104. /* allocator fields */
  105. spinlock_t alloc_lock ____cacheline_aligned_in_smp;
  106. u32 sr_head; /* shadow ring head */
  107. unsigned long fill; /* official alloc count */
  108. unsigned long alloc_free; /* copy of free (less cache thrash) */
  109. u32 fill_wrap; /* tracks fill within ring */
  110. u32 credits; /* number of blocks in context */
  111. /* adding a new field here would make it part of this cacheline */
  112. /* releaser fields */
  113. spinlock_t release_lock ____cacheline_aligned_in_smp;
  114. u32 sr_tail; /* shadow ring tail */
  115. unsigned long free; /* official free count */
  116. volatile __le64 *hw_free; /* HW free counter */
  117. /* list for PIO waiters */
  118. struct list_head piowait ____cacheline_aligned_in_smp;
  119. spinlock_t credit_ctrl_lock ____cacheline_aligned_in_smp;
  120. u32 credit_intr_count; /* count of credit intr users */
  121. u64 credit_ctrl; /* cache for credit control */
  122. wait_queue_head_t halt_wait; /* wait until kernel sees interrupt */
  123. struct work_struct halt_work; /* halted context work queue entry */
  124. };
  125. /* send context flags */
  126. #define SCF_ENABLED 0x01
  127. #define SCF_IN_FREE 0x02
  128. #define SCF_HALTED 0x04
  129. #define SCF_FROZEN 0x08
  130. #define SCF_LINK_DOWN 0x10
  131. struct send_context_info {
  132. struct send_context *sc; /* allocated working context */
  133. u16 allocated; /* has this been allocated? */
  134. u16 type; /* context type */
  135. u16 base; /* base in PIO array */
  136. u16 credits; /* size in PIO array */
  137. };
  138. /* DMA credit return, index is always (context & 0x7) */
  139. struct credit_return {
  140. volatile __le64 cr[8];
  141. };
  142. /* NUMA indexed credit return array */
  143. struct credit_return_base {
  144. struct credit_return *va;
  145. dma_addr_t dma;
  146. };
  147. /* send context configuration sizes (one per type) */
  148. struct sc_config_sizes {
  149. short int size;
  150. short int count;
  151. };
  152. /*
  153. * The diagram below details the relationship of the mapping structures
  154. *
  155. * Since the mapping now allows for non-uniform send contexts per vl, the
  156. * number of send contexts for a vl is either the vl_scontexts[vl] or
  157. * a computation based on num_kernel_send_contexts/num_vls:
  158. *
  159. * For example:
  160. * nactual = vl_scontexts ? vl_scontexts[vl] : num_kernel_send_contexts/num_vls
  161. *
  162. * n = roundup to next highest power of 2 using nactual
  163. *
  164. * In the case where there are num_kernel_send_contexts/num_vls doesn't divide
  165. * evenly, the extras are added from the last vl downward.
  166. *
  167. * For the case where n > nactual, the send contexts are assigned
  168. * in a round robin fashion wrapping back to the first send context
  169. * for a particular vl.
  170. *
  171. * dd->pio_map
  172. * | pio_map_elem[0]
  173. * | +--------------------+
  174. * v | mask |
  175. * pio_vl_map |--------------------|
  176. * +--------------------------+ | ksc[0] -> sc 1 |
  177. * | list (RCU) | |--------------------|
  178. * |--------------------------| ->| ksc[1] -> sc 2 |
  179. * | mask | --/ |--------------------|
  180. * |--------------------------| -/ | * |
  181. * | actual_vls (max 8) | -/ |--------------------|
  182. * |--------------------------| --/ | ksc[n-1] -> sc n |
  183. * | vls (max 8) | -/ +--------------------+
  184. * |--------------------------| --/
  185. * | map[0] |-/
  186. * |--------------------------| +--------------------+
  187. * | map[1] |--- | mask |
  188. * |--------------------------| \---- |--------------------|
  189. * | * | \-- | ksc[0] -> sc 1+n |
  190. * | * | \---- |--------------------|
  191. * | * | \->| ksc[1] -> sc 2+n |
  192. * |--------------------------| |--------------------|
  193. * | map[vls - 1] |- | * |
  194. * +--------------------------+ \- |--------------------|
  195. * \- | ksc[m-1] -> sc m+n |
  196. * \ +--------------------+
  197. * \-
  198. * \
  199. * \- +----------------------+
  200. * \- | mask |
  201. * \ |----------------------|
  202. * \- | ksc[0] -> sc 1+m+n |
  203. * \- |----------------------|
  204. * >| ksc[1] -> sc 2+m+n |
  205. * |----------------------|
  206. * | * |
  207. * |----------------------|
  208. * | ksc[o-1] -> sc o+m+n |
  209. * +----------------------+
  210. *
  211. */
  212. /* Initial number of send contexts per VL */
  213. #define INIT_SC_PER_VL 2
  214. /*
  215. * struct pio_map_elem - mapping for a vl
  216. * @mask - selector mask
  217. * @ksc - array of kernel send contexts for this vl
  218. *
  219. * The mask is used to "mod" the selector to
  220. * produce index into the trailing array of
  221. * kscs
  222. */
  223. struct pio_map_elem {
  224. u32 mask;
  225. struct send_context *ksc[0];
  226. };
  227. /*
  228. * struct pio_vl_map - mapping for a vl
  229. * @list - rcu head for free callback
  230. * @mask - vl mask to "mod" the vl to produce an index to map array
  231. * @actual_vls - number of vls
  232. * @vls - numbers of vls rounded to next power of 2
  233. * @map - array of pio_map_elem entries
  234. *
  235. * This is the parent mapping structure. The trailing members of the
  236. * struct point to pio_map_elem entries, which in turn point to an
  237. * array of kscs for that vl.
  238. */
  239. struct pio_vl_map {
  240. struct rcu_head list;
  241. u32 mask;
  242. u8 actual_vls;
  243. u8 vls;
  244. struct pio_map_elem *map[0];
  245. };
  246. int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls,
  247. u8 *vl_scontexts);
  248. void free_pio_map(struct hfi1_devdata *dd);
  249. struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
  250. u32 selector, u8 vl);
  251. struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
  252. u32 selector, u8 sc5);
  253. /* send context functions */
  254. int init_credit_return(struct hfi1_devdata *dd);
  255. void free_credit_return(struct hfi1_devdata *dd);
  256. int init_sc_pools_and_sizes(struct hfi1_devdata *dd);
  257. int init_send_contexts(struct hfi1_devdata *dd);
  258. int init_credit_return(struct hfi1_devdata *dd);
  259. int init_pervl_scs(struct hfi1_devdata *dd);
  260. struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
  261. uint hdrqentsize, int numa);
  262. void sc_free(struct send_context *sc);
  263. int sc_enable(struct send_context *sc);
  264. void sc_disable(struct send_context *sc);
  265. int sc_restart(struct send_context *sc);
  266. void sc_return_credits(struct send_context *sc);
  267. void sc_flush(struct send_context *sc);
  268. void sc_drop(struct send_context *sc);
  269. void sc_stop(struct send_context *sc, int bit);
  270. struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
  271. pio_release_cb cb, void *arg);
  272. void sc_release_update(struct send_context *sc);
  273. void sc_return_credits(struct send_context *sc);
  274. void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context);
  275. void sc_add_credit_return_intr(struct send_context *sc);
  276. void sc_del_credit_return_intr(struct send_context *sc);
  277. void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold);
  278. u32 sc_percent_to_threshold(struct send_context *sc, u32 percent);
  279. u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize);
  280. void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint);
  281. void sc_wait(struct hfi1_devdata *dd);
  282. void set_pio_integrity(struct send_context *sc);
  283. /* support functions */
  284. void pio_reset_all(struct hfi1_devdata *dd);
  285. void pio_freeze(struct hfi1_devdata *dd);
  286. void pio_kernel_unfreeze(struct hfi1_devdata *dd);
  287. void pio_kernel_linkup(struct hfi1_devdata *dd);
  288. /* global PIO send control operations */
  289. #define PSC_GLOBAL_ENABLE 0
  290. #define PSC_GLOBAL_DISABLE 1
  291. #define PSC_GLOBAL_VLARB_ENABLE 2
  292. #define PSC_GLOBAL_VLARB_DISABLE 3
  293. #define PSC_CM_RESET 4
  294. #define PSC_DATA_VL_ENABLE 5
  295. #define PSC_DATA_VL_DISABLE 6
  296. void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl);
  297. void pio_send_control(struct hfi1_devdata *dd, int op);
  298. /* PIO copy routines */
  299. void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc,
  300. const void *from, size_t count);
  301. void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc,
  302. const void *from, size_t nbytes);
  303. void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes);
  304. void seg_pio_copy_end(struct pio_buf *pbuf);
  305. #endif /* _PIO_H */