pio.h 13 KB

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