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