1*145eba1aSCai Huoqing /* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ 2f48ad614SDennis Dalessandro /* 32280740fSVishwanathapura, Niranjana * Copyright(c) 2015-2017 Intel Corporation. 4f48ad614SDennis Dalessandro */ 5f48ad614SDennis Dalessandro 6*145eba1aSCai Huoqing #ifndef _PIO_H 7*145eba1aSCai Huoqing #define _PIO_H 8f48ad614SDennis Dalessandro /* send context types */ 9f48ad614SDennis Dalessandro #define SC_KERNEL 0 10f48ad614SDennis Dalessandro #define SC_VL15 1 11f48ad614SDennis Dalessandro #define SC_ACK 2 12f48ad614SDennis Dalessandro #define SC_USER 3 /* must be the last one: it may take all left */ 13f48ad614SDennis Dalessandro #define SC_MAX 4 /* count of send context types */ 14f48ad614SDennis Dalessandro 15f48ad614SDennis Dalessandro /* invalid send context index */ 16f48ad614SDennis Dalessandro #define INVALID_SCI 0xff 17f48ad614SDennis Dalessandro 18f48ad614SDennis Dalessandro /* PIO buffer release callback function */ 19f48ad614SDennis Dalessandro typedef void (*pio_release_cb)(void *arg, int code); 20f48ad614SDennis Dalessandro 21f48ad614SDennis Dalessandro /* PIO release codes - in bits, as there could more than one that apply */ 22f48ad614SDennis Dalessandro #define PRC_OK 0 /* no known error */ 23f48ad614SDennis Dalessandro #define PRC_STATUS_ERR 0x01 /* credit return due to status error */ 24f48ad614SDennis Dalessandro #define PRC_PBC 0x02 /* credit return due to PBC */ 25f48ad614SDennis Dalessandro #define PRC_THRESHOLD 0x04 /* credit return due to threshold */ 26f48ad614SDennis Dalessandro #define PRC_FILL_ERR 0x08 /* credit return due fill error */ 27f48ad614SDennis Dalessandro #define PRC_FORCE 0x10 /* credit return due credit force */ 28f48ad614SDennis Dalessandro #define PRC_SC_DISABLE 0x20 /* clean-up after a context disable */ 29f48ad614SDennis Dalessandro 30f48ad614SDennis Dalessandro /* byte helper */ 31f48ad614SDennis Dalessandro union mix { 32f48ad614SDennis Dalessandro u64 val64; 33f48ad614SDennis Dalessandro u32 val32[2]; 34f48ad614SDennis Dalessandro u8 val8[8]; 35f48ad614SDennis Dalessandro }; 36f48ad614SDennis Dalessandro 37f48ad614SDennis Dalessandro /* an allocated PIO buffer */ 38f48ad614SDennis Dalessandro struct pio_buf { 39f48ad614SDennis Dalessandro struct send_context *sc;/* back pointer to owning send context */ 40f48ad614SDennis Dalessandro pio_release_cb cb; /* called when the buffer is released */ 41f48ad614SDennis Dalessandro void *arg; /* argument for cb */ 42f48ad614SDennis Dalessandro void __iomem *start; /* buffer start address */ 43f48ad614SDennis Dalessandro void __iomem *end; /* context end address */ 44f48ad614SDennis Dalessandro unsigned long sent_at; /* buffer is sent when <= free */ 45f48ad614SDennis Dalessandro union mix carry; /* pending unwritten bytes */ 468af8d297SSebastian Sanchez u16 qw_written; /* QW written so far */ 478af8d297SSebastian Sanchez u8 carry_bytes; /* number of valid bytes in carry */ 48f48ad614SDennis Dalessandro }; 49f48ad614SDennis Dalessandro 50f48ad614SDennis Dalessandro /* cache line aligned pio buffer array */ 51f48ad614SDennis Dalessandro union pio_shadow_ring { 52f48ad614SDennis Dalessandro struct pio_buf pbuf; 53f48ad614SDennis Dalessandro } ____cacheline_aligned; 54f48ad614SDennis Dalessandro 55f48ad614SDennis Dalessandro /* per-NUMA send context */ 56f48ad614SDennis Dalessandro struct send_context { 57f48ad614SDennis Dalessandro /* read-only after init */ 58f48ad614SDennis Dalessandro struct hfi1_devdata *dd; /* device */ 59f48ad614SDennis Dalessandro union pio_shadow_ring *sr; /* shadow ring */ 608af8d297SSebastian Sanchez void __iomem *base_addr; /* start of PIO memory */ 618af8d297SSebastian Sanchez u32 __percpu *buffers_allocated;/* count of buffers allocated */ 628af8d297SSebastian Sanchez u32 size; /* context size, in bytes */ 63f48ad614SDennis Dalessandro 64f48ad614SDennis Dalessandro int node; /* context home node */ 65f48ad614SDennis Dalessandro u32 sr_size; /* size of the shadow ring */ 668af8d297SSebastian Sanchez u16 flags; /* flags */ 678af8d297SSebastian Sanchez u8 type; /* context type */ 688af8d297SSebastian Sanchez u8 sw_index; /* software index number */ 698af8d297SSebastian Sanchez u8 hw_context; /* hardware context number */ 708af8d297SSebastian Sanchez u8 group; /* credit return group */ 718af8d297SSebastian Sanchez 72f48ad614SDennis Dalessandro /* allocator fields */ 73f48ad614SDennis Dalessandro spinlock_t alloc_lock ____cacheline_aligned_in_smp; 7499c7abfbSMike Marciniszyn u32 sr_head; /* shadow ring head */ 75f48ad614SDennis Dalessandro unsigned long fill; /* official alloc count */ 76f48ad614SDennis Dalessandro unsigned long alloc_free; /* copy of free (less cache thrash) */ 772474d775SSebastian Sanchez u32 fill_wrap; /* tracks fill within ring */ 788af8d297SSebastian Sanchez u32 credits; /* number of blocks in context */ 798af8d297SSebastian Sanchez /* adding a new field here would make it part of this cacheline */ 808af8d297SSebastian Sanchez 81f48ad614SDennis Dalessandro /* releaser fields */ 82f48ad614SDennis Dalessandro spinlock_t release_lock ____cacheline_aligned_in_smp; 83f48ad614SDennis Dalessandro u32 sr_tail; /* shadow ring tail */ 8499c7abfbSMike Marciniszyn unsigned long free; /* official free count */ 8599c7abfbSMike Marciniszyn volatile __le64 *hw_free; /* HW free counter */ 86f48ad614SDennis Dalessandro /* list for PIO waiters */ 87f48ad614SDennis Dalessandro struct list_head piowait ____cacheline_aligned_in_smp; 889aefcabeSMike Marciniszyn seqlock_t waitlock; 899aefcabeSMike Marciniszyn 90f48ad614SDennis Dalessandro spinlock_t credit_ctrl_lock ____cacheline_aligned_in_smp; 91f48ad614SDennis Dalessandro u32 credit_intr_count; /* count of credit intr users */ 9299c7abfbSMike Marciniszyn u64 credit_ctrl; /* cache for credit control */ 93f48ad614SDennis Dalessandro wait_queue_head_t halt_wait; /* wait until kernel sees interrupt */ 948af8d297SSebastian Sanchez struct work_struct halt_work; /* halted context work queue entry */ 95f48ad614SDennis Dalessandro }; 96f48ad614SDennis Dalessandro 97f48ad614SDennis Dalessandro /* send context flags */ 98f48ad614SDennis Dalessandro #define SCF_ENABLED 0x01 99f48ad614SDennis Dalessandro #define SCF_IN_FREE 0x02 100f48ad614SDennis Dalessandro #define SCF_HALTED 0x04 101f48ad614SDennis Dalessandro #define SCF_FROZEN 0x08 102b4a4957dSMichael J. Ruhl #define SCF_LINK_DOWN 0x10 103f48ad614SDennis Dalessandro 104f48ad614SDennis Dalessandro struct send_context_info { 105f48ad614SDennis Dalessandro struct send_context *sc; /* allocated working context */ 106f48ad614SDennis Dalessandro u16 allocated; /* has this been allocated? */ 107f48ad614SDennis Dalessandro u16 type; /* context type */ 108f48ad614SDennis Dalessandro u16 base; /* base in PIO array */ 109f48ad614SDennis Dalessandro u16 credits; /* size in PIO array */ 110f48ad614SDennis Dalessandro }; 111f48ad614SDennis Dalessandro 112f48ad614SDennis Dalessandro /* DMA credit return, index is always (context & 0x7) */ 113f48ad614SDennis Dalessandro struct credit_return { 114f48ad614SDennis Dalessandro volatile __le64 cr[8]; 115f48ad614SDennis Dalessandro }; 116f48ad614SDennis Dalessandro 117f48ad614SDennis Dalessandro /* NUMA indexed credit return array */ 118f48ad614SDennis Dalessandro struct credit_return_base { 119f48ad614SDennis Dalessandro struct credit_return *va; 12060368186STymoteusz Kielan dma_addr_t dma; 121f48ad614SDennis Dalessandro }; 122f48ad614SDennis Dalessandro 123f48ad614SDennis Dalessandro /* send context configuration sizes (one per type) */ 124f48ad614SDennis Dalessandro struct sc_config_sizes { 125f48ad614SDennis Dalessandro short int size; 126f48ad614SDennis Dalessandro short int count; 127f48ad614SDennis Dalessandro }; 128f48ad614SDennis Dalessandro 129f48ad614SDennis Dalessandro /* 130f48ad614SDennis Dalessandro * The diagram below details the relationship of the mapping structures 131f48ad614SDennis Dalessandro * 132f48ad614SDennis Dalessandro * Since the mapping now allows for non-uniform send contexts per vl, the 133f48ad614SDennis Dalessandro * number of send contexts for a vl is either the vl_scontexts[vl] or 134f48ad614SDennis Dalessandro * a computation based on num_kernel_send_contexts/num_vls: 135f48ad614SDennis Dalessandro * 136f48ad614SDennis Dalessandro * For example: 137f48ad614SDennis Dalessandro * nactual = vl_scontexts ? vl_scontexts[vl] : num_kernel_send_contexts/num_vls 138f48ad614SDennis Dalessandro * 139f48ad614SDennis Dalessandro * n = roundup to next highest power of 2 using nactual 140f48ad614SDennis Dalessandro * 141f48ad614SDennis Dalessandro * In the case where there are num_kernel_send_contexts/num_vls doesn't divide 142f48ad614SDennis Dalessandro * evenly, the extras are added from the last vl downward. 143f48ad614SDennis Dalessandro * 144f48ad614SDennis Dalessandro * For the case where n > nactual, the send contexts are assigned 145f48ad614SDennis Dalessandro * in a round robin fashion wrapping back to the first send context 146f48ad614SDennis Dalessandro * for a particular vl. 147f48ad614SDennis Dalessandro * 148f48ad614SDennis Dalessandro * dd->pio_map 149f48ad614SDennis Dalessandro * | pio_map_elem[0] 150f48ad614SDennis Dalessandro * | +--------------------+ 151f48ad614SDennis Dalessandro * v | mask | 152f48ad614SDennis Dalessandro * pio_vl_map |--------------------| 153f48ad614SDennis Dalessandro * +--------------------------+ | ksc[0] -> sc 1 | 154f48ad614SDennis Dalessandro * | list (RCU) | |--------------------| 155f48ad614SDennis Dalessandro * |--------------------------| ->| ksc[1] -> sc 2 | 156f48ad614SDennis Dalessandro * | mask | --/ |--------------------| 157f48ad614SDennis Dalessandro * |--------------------------| -/ | * | 158f48ad614SDennis Dalessandro * | actual_vls (max 8) | -/ |--------------------| 159e8ea95afSIra Weiny * |--------------------------| --/ | ksc[n-1] -> sc n | 160f48ad614SDennis Dalessandro * | vls (max 8) | -/ +--------------------+ 161f48ad614SDennis Dalessandro * |--------------------------| --/ 162f48ad614SDennis Dalessandro * | map[0] |-/ 163f48ad614SDennis Dalessandro * |--------------------------| +--------------------+ 164f48ad614SDennis Dalessandro * | map[1] |--- | mask | 165f48ad614SDennis Dalessandro * |--------------------------| \---- |--------------------| 166f48ad614SDennis Dalessandro * | * | \-- | ksc[0] -> sc 1+n | 167f48ad614SDennis Dalessandro * | * | \---- |--------------------| 168f48ad614SDennis Dalessandro * | * | \->| ksc[1] -> sc 2+n | 169f48ad614SDennis Dalessandro * |--------------------------| |--------------------| 170f48ad614SDennis Dalessandro * | map[vls - 1] |- | * | 171f48ad614SDennis Dalessandro * +--------------------------+ \- |--------------------| 172e8ea95afSIra Weiny * \- | ksc[m-1] -> sc m+n | 173f48ad614SDennis Dalessandro * \ +--------------------+ 174f48ad614SDennis Dalessandro * \- 175f48ad614SDennis Dalessandro * \ 176e8ea95afSIra Weiny * \- +----------------------+ 177f48ad614SDennis Dalessandro * \- | mask | 178e8ea95afSIra Weiny * \ |----------------------| 179f48ad614SDennis Dalessandro * \- | ksc[0] -> sc 1+m+n | 180e8ea95afSIra Weiny * \- |----------------------| 181f48ad614SDennis Dalessandro * >| ksc[1] -> sc 2+m+n | 182e8ea95afSIra Weiny * |----------------------| 183f48ad614SDennis Dalessandro * | * | 184e8ea95afSIra Weiny * |----------------------| 185e8ea95afSIra Weiny * | ksc[o-1] -> sc o+m+n | 186e8ea95afSIra Weiny * +----------------------+ 187f48ad614SDennis Dalessandro * 188f48ad614SDennis Dalessandro */ 189f48ad614SDennis Dalessandro 190f48ad614SDennis Dalessandro /* Initial number of send contexts per VL */ 191f48ad614SDennis Dalessandro #define INIT_SC_PER_VL 2 192f48ad614SDennis Dalessandro 193f48ad614SDennis Dalessandro /* 194f48ad614SDennis Dalessandro * struct pio_map_elem - mapping for a vl 195f48ad614SDennis Dalessandro * @mask - selector mask 196f48ad614SDennis Dalessandro * @ksc - array of kernel send contexts for this vl 197f48ad614SDennis Dalessandro * 198f48ad614SDennis Dalessandro * The mask is used to "mod" the selector to 199f48ad614SDennis Dalessandro * produce index into the trailing array of 200f48ad614SDennis Dalessandro * kscs 201f48ad614SDennis Dalessandro */ 202f48ad614SDennis Dalessandro struct pio_map_elem { 203f48ad614SDennis Dalessandro u32 mask; 2045b361328SGustavo A. R. Silva struct send_context *ksc[]; 205f48ad614SDennis Dalessandro }; 206f48ad614SDennis Dalessandro 207f48ad614SDennis Dalessandro /* 208f48ad614SDennis Dalessandro * struct pio_vl_map - mapping for a vl 209f48ad614SDennis Dalessandro * @list - rcu head for free callback 210f48ad614SDennis Dalessandro * @mask - vl mask to "mod" the vl to produce an index to map array 211f48ad614SDennis Dalessandro * @actual_vls - number of vls 212f48ad614SDennis Dalessandro * @vls - numbers of vls rounded to next power of 2 213f48ad614SDennis Dalessandro * @map - array of pio_map_elem entries 214f48ad614SDennis Dalessandro * 215f48ad614SDennis Dalessandro * This is the parent mapping structure. The trailing members of the 216f48ad614SDennis Dalessandro * struct point to pio_map_elem entries, which in turn point to an 217f48ad614SDennis Dalessandro * array of kscs for that vl. 218f48ad614SDennis Dalessandro */ 219f48ad614SDennis Dalessandro struct pio_vl_map { 220f48ad614SDennis Dalessandro struct rcu_head list; 221f48ad614SDennis Dalessandro u32 mask; 222f48ad614SDennis Dalessandro u8 actual_vls; 223f48ad614SDennis Dalessandro u8 vls; 2245b361328SGustavo A. R. Silva struct pio_map_elem *map[]; 225f48ad614SDennis Dalessandro }; 226f48ad614SDennis Dalessandro 227f48ad614SDennis Dalessandro int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, 228f48ad614SDennis Dalessandro u8 *vl_scontexts); 229f48ad614SDennis Dalessandro void free_pio_map(struct hfi1_devdata *dd); 230f48ad614SDennis Dalessandro struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd, 231f48ad614SDennis Dalessandro u32 selector, u8 vl); 232f48ad614SDennis Dalessandro struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd, 233f48ad614SDennis Dalessandro u32 selector, u8 sc5); 234f48ad614SDennis Dalessandro 235f48ad614SDennis Dalessandro /* send context functions */ 236f48ad614SDennis Dalessandro int init_credit_return(struct hfi1_devdata *dd); 237f48ad614SDennis Dalessandro void free_credit_return(struct hfi1_devdata *dd); 238f48ad614SDennis Dalessandro int init_sc_pools_and_sizes(struct hfi1_devdata *dd); 239f48ad614SDennis Dalessandro int init_send_contexts(struct hfi1_devdata *dd); 240f48ad614SDennis Dalessandro int init_pervl_scs(struct hfi1_devdata *dd); 241f48ad614SDennis Dalessandro struct send_context *sc_alloc(struct hfi1_devdata *dd, int type, 242f48ad614SDennis Dalessandro uint hdrqentsize, int numa); 243f48ad614SDennis Dalessandro void sc_free(struct send_context *sc); 244f48ad614SDennis Dalessandro int sc_enable(struct send_context *sc); 245f48ad614SDennis Dalessandro void sc_disable(struct send_context *sc); 246f48ad614SDennis Dalessandro int sc_restart(struct send_context *sc); 247f48ad614SDennis Dalessandro void sc_return_credits(struct send_context *sc); 248f48ad614SDennis Dalessandro void sc_flush(struct send_context *sc); 249f48ad614SDennis Dalessandro void sc_drop(struct send_context *sc); 250f48ad614SDennis Dalessandro void sc_stop(struct send_context *sc, int bit); 251f48ad614SDennis Dalessandro struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len, 252f48ad614SDennis Dalessandro pio_release_cb cb, void *arg); 253f48ad614SDennis Dalessandro void sc_release_update(struct send_context *sc); 254f48ad614SDennis Dalessandro void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context); 255f48ad614SDennis Dalessandro void sc_add_credit_return_intr(struct send_context *sc); 256f48ad614SDennis Dalessandro void sc_del_credit_return_intr(struct send_context *sc); 257f48ad614SDennis Dalessandro void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold); 258f48ad614SDennis Dalessandro u32 sc_percent_to_threshold(struct send_context *sc, u32 percent); 259f48ad614SDennis Dalessandro u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize); 260f48ad614SDennis Dalessandro void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint); 261f48ad614SDennis Dalessandro void sc_wait(struct hfi1_devdata *dd); 262f48ad614SDennis Dalessandro void set_pio_integrity(struct send_context *sc); 263f48ad614SDennis Dalessandro 264f48ad614SDennis Dalessandro /* support functions */ 265f48ad614SDennis Dalessandro void pio_reset_all(struct hfi1_devdata *dd); 266f48ad614SDennis Dalessandro void pio_freeze(struct hfi1_devdata *dd); 267f48ad614SDennis Dalessandro void pio_kernel_unfreeze(struct hfi1_devdata *dd); 268b4a4957dSMichael J. Ruhl void pio_kernel_linkup(struct hfi1_devdata *dd); 269f48ad614SDennis Dalessandro 270f48ad614SDennis Dalessandro /* global PIO send control operations */ 271f48ad614SDennis Dalessandro #define PSC_GLOBAL_ENABLE 0 272f48ad614SDennis Dalessandro #define PSC_GLOBAL_DISABLE 1 273f48ad614SDennis Dalessandro #define PSC_GLOBAL_VLARB_ENABLE 2 274f48ad614SDennis Dalessandro #define PSC_GLOBAL_VLARB_DISABLE 3 275f48ad614SDennis Dalessandro #define PSC_CM_RESET 4 276f48ad614SDennis Dalessandro #define PSC_DATA_VL_ENABLE 5 277f48ad614SDennis Dalessandro #define PSC_DATA_VL_DISABLE 6 278f48ad614SDennis Dalessandro 279f48ad614SDennis Dalessandro void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl); 280f48ad614SDennis Dalessandro void pio_send_control(struct hfi1_devdata *dd, int op); 281f48ad614SDennis Dalessandro 282f48ad614SDennis Dalessandro /* PIO copy routines */ 283f48ad614SDennis Dalessandro void pio_copy(struct hfi1_devdata *dd, struct pio_buf *pbuf, u64 pbc, 284f48ad614SDennis Dalessandro const void *from, size_t count); 285f48ad614SDennis Dalessandro void seg_pio_copy_start(struct pio_buf *pbuf, u64 pbc, 286f48ad614SDennis Dalessandro const void *from, size_t nbytes); 287f48ad614SDennis Dalessandro void seg_pio_copy_mid(struct pio_buf *pbuf, const void *from, size_t nbytes); 288f48ad614SDennis Dalessandro void seg_pio_copy_end(struct pio_buf *pbuf); 289f48ad614SDennis Dalessandro 290937488a8SKaike Wan void seqfile_dump_sci(struct seq_file *s, u32 i, 291937488a8SKaike Wan struct send_context_info *sci); 292937488a8SKaike Wan 293f48ad614SDennis Dalessandro #endif /* _PIO_H */ 294