1cdcd52d4SBartosz Sobczak /*- 2cdcd52d4SBartosz Sobczak * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB 3cdcd52d4SBartosz Sobczak * 4*01fbb869SBartosz Sobczak * Copyright (c) 2021 - 2023 Intel Corporation 5cdcd52d4SBartosz Sobczak * 6cdcd52d4SBartosz Sobczak * This software is available to you under a choice of one of two 7cdcd52d4SBartosz Sobczak * licenses. You may choose to be licensed under the terms of the GNU 8cdcd52d4SBartosz Sobczak * General Public License (GPL) Version 2, available from the file 9cdcd52d4SBartosz Sobczak * COPYING in the main directory of this source tree, or the 10cdcd52d4SBartosz Sobczak * OpenFabrics.org BSD license below: 11cdcd52d4SBartosz Sobczak * 12cdcd52d4SBartosz Sobczak * Redistribution and use in source and binary forms, with or 13cdcd52d4SBartosz Sobczak * without modification, are permitted provided that the following 14cdcd52d4SBartosz Sobczak * conditions are met: 15cdcd52d4SBartosz Sobczak * 16cdcd52d4SBartosz Sobczak * - Redistributions of source code must retain the above 17cdcd52d4SBartosz Sobczak * copyright notice, this list of conditions and the following 18cdcd52d4SBartosz Sobczak * disclaimer. 19cdcd52d4SBartosz Sobczak * 20cdcd52d4SBartosz Sobczak * - Redistributions in binary form must reproduce the above 21cdcd52d4SBartosz Sobczak * copyright notice, this list of conditions and the following 22cdcd52d4SBartosz Sobczak * disclaimer in the documentation and/or other materials 23cdcd52d4SBartosz Sobczak * provided with the distribution. 24cdcd52d4SBartosz Sobczak * 25cdcd52d4SBartosz Sobczak * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26cdcd52d4SBartosz Sobczak * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27cdcd52d4SBartosz Sobczak * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28cdcd52d4SBartosz Sobczak * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29cdcd52d4SBartosz Sobczak * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30cdcd52d4SBartosz Sobczak * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31cdcd52d4SBartosz Sobczak * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32cdcd52d4SBartosz Sobczak * SOFTWARE. 33cdcd52d4SBartosz Sobczak */ 34cdcd52d4SBartosz Sobczak 35cdcd52d4SBartosz Sobczak #ifndef _ICRDMA_OSDEP_H_ 36cdcd52d4SBartosz Sobczak #define _ICRDMA_OSDEP_H_ 37cdcd52d4SBartosz Sobczak 38cdcd52d4SBartosz Sobczak #include <linux/dma-mapping.h> 39cdcd52d4SBartosz Sobczak #include <linux/etherdevice.h> 40cdcd52d4SBartosz Sobczak #include <linux/fs.h> 41cdcd52d4SBartosz Sobczak #include <linux/if_ether.h> 42cdcd52d4SBartosz Sobczak #include <linux/interrupt.h> 43cdcd52d4SBartosz Sobczak #include <linux/io.h> 44cdcd52d4SBartosz Sobczak #include <linux/jhash.h> 45cdcd52d4SBartosz Sobczak #include <linux/list.h> 46cdcd52d4SBartosz Sobczak #include <linux/log2.h> 47cdcd52d4SBartosz Sobczak #include <linux/mutex.h> 48cdcd52d4SBartosz Sobczak #include <linux/pci.h> 49cdcd52d4SBartosz Sobczak #include <linux/random.h> 50cdcd52d4SBartosz Sobczak #include <linux/spinlock.h> 51cdcd52d4SBartosz Sobczak #include <linux/timer.h> 52cdcd52d4SBartosz Sobczak #include <linux/workqueue.h> 53*01fbb869SBartosz Sobczak #include <linux/atomic.h> 54cdcd52d4SBartosz Sobczak 55cdcd52d4SBartosz Sobczak #include <sys/bus.h> 56cdcd52d4SBartosz Sobczak #include <machine/bus.h> 57cdcd52d4SBartosz Sobczak 58cdcd52d4SBartosz Sobczak #define IOMEM 59cdcd52d4SBartosz Sobczak #define IRDMA_NTOHS(a) ntohs(a) 60cdcd52d4SBartosz Sobczak #define MAKEMASK(m, s) ((m) << (s)) 61cdcd52d4SBartosz Sobczak #define OS_TIMER timer_list 62cdcd52d4SBartosz Sobczak #define DECLARE_HASHTABLE(n, b) struct hlist_head (n)[1 << (b)] 63cdcd52d4SBartosz Sobczak #define HASH_MIN(v, b) (sizeof(v) <= 4 ? hash_32(v, b) : hash_long(v, b)) 64cdcd52d4SBartosz Sobczak #define HASH_FOR_EACH_RCU(n, b, o, m) for ((b) = 0, o = NULL; o == NULL && (b) < ARRAY_SIZE(n);\ 65cdcd52d4SBartosz Sobczak (b)++)\ 66cdcd52d4SBartosz Sobczak hlist_for_each_entry_rcu(o, &n[(b)], m) 67cdcd52d4SBartosz Sobczak #define HASH_FOR_EACH_POSSIBLE_RCU(n, o, m, k) \ 68cdcd52d4SBartosz Sobczak hlist_for_each_entry_rcu(o, &n[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(n)))],\ 69cdcd52d4SBartosz Sobczak m) 70cdcd52d4SBartosz Sobczak #define HASH_FOR_EACH_POSSIBLE(n, o, m, k) \ 71cdcd52d4SBartosz Sobczak hlist_for_each_entry(o, &n[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(n)))],\ 72cdcd52d4SBartosz Sobczak m) 73cdcd52d4SBartosz Sobczak #define HASH_ADD_RCU(h, n, k) \ 74cdcd52d4SBartosz Sobczak hlist_add_head_rcu(n, &h[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(h)))]) 75cdcd52d4SBartosz Sobczak #define HASH_DEL_RCU(tbl, node) hlist_del_rcu(node) 76cdcd52d4SBartosz Sobczak #define HASH_ADD(h, n, k) \ 77cdcd52d4SBartosz Sobczak hlist_add_head(n, &h[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(h)))]) 78cdcd52d4SBartosz Sobczak #define HASH_DEL(tbl, node) hlist_del(node) 79cdcd52d4SBartosz Sobczak 80cdcd52d4SBartosz Sobczak #define WQ_UNBOUND_MAX_ACTIVE max_t(int, 512, num_possible_cpus() * 4) 81cdcd52d4SBartosz Sobczak #define if_addr_rlock(x) 82cdcd52d4SBartosz Sobczak #define if_addr_runlock(x) 83cdcd52d4SBartosz Sobczak 84cdcd52d4SBartosz Sobczak /* constants */ 85cdcd52d4SBartosz Sobczak #define STATS_TIMER_DELAY 60000 86cdcd52d4SBartosz Sobczak 87cdcd52d4SBartosz Sobczak /* a couple of linux size defines */ 88cdcd52d4SBartosz Sobczak #define SZ_128 128 89cdcd52d4SBartosz Sobczak #define SPEED_1000 1000 90cdcd52d4SBartosz Sobczak #define SPEED_10000 10000 91cdcd52d4SBartosz Sobczak #define SPEED_20000 20000 92cdcd52d4SBartosz Sobczak #define SPEED_25000 25000 93cdcd52d4SBartosz Sobczak #define SPEED_40000 40000 94cdcd52d4SBartosz Sobczak #define SPEED_100000 100000 95cdcd52d4SBartosz Sobczak 96cdcd52d4SBartosz Sobczak #define irdma_mb() mb() 97cdcd52d4SBartosz Sobczak #define irdma_wmb() wmb() 98cdcd52d4SBartosz Sobczak #define irdma_get_virt_to_phy vtophys 99cdcd52d4SBartosz Sobczak 100cdcd52d4SBartosz Sobczak #define __aligned_u64 uint64_t __aligned(8) 101cdcd52d4SBartosz Sobczak 102cdcd52d4SBartosz Sobczak #define VLAN_PRIO_SHIFT 13 103cdcd52d4SBartosz Sobczak 104cdcd52d4SBartosz Sobczak /* 105cdcd52d4SBartosz Sobczak * debug definition section 106cdcd52d4SBartosz Sobczak */ 107cdcd52d4SBartosz Sobczak #define irdma_print(S, ...) printf("%s:%d "S, __FUNCTION__, __LINE__, ##__VA_ARGS__) 108cdcd52d4SBartosz Sobczak #define irdma_debug_buf(dev, mask, desc, buf, size) \ 109cdcd52d4SBartosz Sobczak do { \ 110cdcd52d4SBartosz Sobczak u32 i; \ 111cdcd52d4SBartosz Sobczak if (!((mask) & (dev)->debug_mask)) { \ 112cdcd52d4SBartosz Sobczak break; \ 113cdcd52d4SBartosz Sobczak } \ 114cdcd52d4SBartosz Sobczak irdma_debug(dev, mask, "%s\n", desc); \ 115cdcd52d4SBartosz Sobczak irdma_debug(dev, mask, "starting address virt=%p phy=%lxh\n", buf, irdma_get_virt_to_phy(buf)); \ 116cdcd52d4SBartosz Sobczak for (i = 0; i < size ; i += 8) \ 11735105900SBartosz Sobczak irdma_debug(dev, mask, "index %03d val: %016lx\n", i, ((unsigned long *)(buf))[i / 8]); \ 118cdcd52d4SBartosz Sobczak } while(0) 119cdcd52d4SBartosz Sobczak 120cdcd52d4SBartosz Sobczak #define irdma_debug(h, m, s, ...) \ 121cdcd52d4SBartosz Sobczak do { \ 122cdcd52d4SBartosz Sobczak if (!(h)) { \ 123cdcd52d4SBartosz Sobczak if ((m) == IRDMA_DEBUG_INIT) \ 124cdcd52d4SBartosz Sobczak printf("irdma INIT " s, ##__VA_ARGS__); \ 125cdcd52d4SBartosz Sobczak } else if (((m) & (h)->debug_mask)) { \ 126cdcd52d4SBartosz Sobczak printf("irdma " s, ##__VA_ARGS__); \ 127cdcd52d4SBartosz Sobczak } \ 128cdcd52d4SBartosz Sobczak } while (0) 12935105900SBartosz Sobczak #define irdma_dev_err(ibdev, fmt, ...) \ 13035105900SBartosz Sobczak pr_err("%s:%s:%d ERR "fmt, (ibdev)->name, __func__, __LINE__, ##__VA_ARGS__) 13135105900SBartosz Sobczak #define irdma_dev_warn(ibdev, fmt, ...) \ 13235105900SBartosz Sobczak pr_warn("%s:%s:%d WARN "fmt, (ibdev)->name, __func__, __LINE__, ##__VA_ARGS__) 133cdcd52d4SBartosz Sobczak #define irdma_dev_info(a, b, ...) printf(b, ##__VA_ARGS__) 134cdcd52d4SBartosz Sobczak #define irdma_pr_warn printf 135cdcd52d4SBartosz Sobczak 136*01fbb869SBartosz Sobczak #define IRDMA_PRINT_IP6(ip6) \ 137*01fbb869SBartosz Sobczak ((u32*)ip6)[0], ((u32*)ip6)[1], ((u32*)ip6)[2], ((u32*)ip6)[3] 138cdcd52d4SBartosz Sobczak 139cdcd52d4SBartosz Sobczak /* 140cdcd52d4SBartosz Sobczak * debug definition end 141cdcd52d4SBartosz Sobczak */ 142cdcd52d4SBartosz Sobczak 143cdcd52d4SBartosz Sobczak typedef __be16 BE16; 144cdcd52d4SBartosz Sobczak typedef __be32 BE32; 145cdcd52d4SBartosz Sobczak typedef uintptr_t irdma_uintptr; 146cdcd52d4SBartosz Sobczak 147cdcd52d4SBartosz Sobczak struct irdma_hw; 148cdcd52d4SBartosz Sobczak struct irdma_pci_f; 149cdcd52d4SBartosz Sobczak struct irdma_sc_dev; 150cdcd52d4SBartosz Sobczak struct irdma_sc_qp; 151cdcd52d4SBartosz Sobczak struct irdma_sc_vsi; 152cdcd52d4SBartosz Sobczak 153cdcd52d4SBartosz Sobczak struct irdma_task_arg { 154cdcd52d4SBartosz Sobczak struct irdma_device *iwdev; 155cdcd52d4SBartosz Sobczak struct ice_rdma_peer *peer; 156cdcd52d4SBartosz Sobczak atomic_t open_ongoing; 157cdcd52d4SBartosz Sobczak atomic_t close_ongoing; 158cdcd52d4SBartosz Sobczak }; 159cdcd52d4SBartosz Sobczak 160cdcd52d4SBartosz Sobczak struct irdma_dev_ctx { 161cdcd52d4SBartosz Sobczak bus_space_tag_t mem_bus_space_tag; 162cdcd52d4SBartosz Sobczak bus_space_handle_t mem_bus_space_handle; 163cdcd52d4SBartosz Sobczak bus_size_t mem_bus_space_size; 164cdcd52d4SBartosz Sobczak void *dev; 165cdcd52d4SBartosz Sobczak struct irdma_task_arg task_arg; 166*01fbb869SBartosz Sobczak atomic_t event_rfcnt; 167cdcd52d4SBartosz Sobczak }; 168cdcd52d4SBartosz Sobczak 169cdcd52d4SBartosz Sobczak #define irdma_pr_info(fmt, args ...) printf("%s: WARN "fmt, __func__, ## args) 170cdcd52d4SBartosz Sobczak #define irdma_pr_err(fmt, args ...) printf("%s: ERR "fmt, __func__, ## args) 171cdcd52d4SBartosz Sobczak #define irdma_memcpy(a, b, c) memcpy((a), (b), (c)) 172cdcd52d4SBartosz Sobczak #define irdma_memset(a, b, c) memset((a), (b), (c)) 173cdcd52d4SBartosz Sobczak #define irdma_usec_delay(x) DELAY(x) 174cdcd52d4SBartosz Sobczak #define mdelay(x) DELAY((x) * 1000) 175cdcd52d4SBartosz Sobczak 176777e472cSBartosz Sobczak #define rt_tos2priority(tos) (tos >> 5) 177cdcd52d4SBartosz Sobczak #define ah_attr_to_dmac(attr) ((attr).dmac) 178cdcd52d4SBartosz Sobczak #define irdma_del_timer_compat(tt) del_timer((tt)) 179cdcd52d4SBartosz Sobczak #define IRDMA_TAILQ_FOREACH CK_STAILQ_FOREACH 180cdcd52d4SBartosz Sobczak #define IRDMA_TAILQ_FOREACH_SAFE CK_STAILQ_FOREACH_SAFE 181cdcd52d4SBartosz Sobczak #define between(a, b, c) (bool)(c-a >= b-a) 182cdcd52d4SBartosz Sobczak 183cdcd52d4SBartosz Sobczak #define rd32(a, reg) irdma_rd32((a)->dev_context, (reg)) 184cdcd52d4SBartosz Sobczak #define wr32(a, reg, value) irdma_wr32((a)->dev_context, (reg), (value)) 185cdcd52d4SBartosz Sobczak 186cdcd52d4SBartosz Sobczak #define rd64(a, reg) irdma_rd64((a)->dev_context, (reg)) 187cdcd52d4SBartosz Sobczak #define wr64(a, reg, value) irdma_wr64((a)->dev_context, (reg), (value)) 188cdcd52d4SBartosz Sobczak #define db_wr32(value, a) writel((value), (a)) 189cdcd52d4SBartosz Sobczak 190cdcd52d4SBartosz Sobczak void *hw_to_dev(struct irdma_hw *hw); 191cdcd52d4SBartosz Sobczak 192cdcd52d4SBartosz Sobczak struct irdma_dma_mem { 193cdcd52d4SBartosz Sobczak void *va; 194cdcd52d4SBartosz Sobczak u64 pa; 195cdcd52d4SBartosz Sobczak bus_dma_tag_t tag; 196cdcd52d4SBartosz Sobczak bus_dmamap_t map; 197cdcd52d4SBartosz Sobczak bus_dma_segment_t seg; 198cdcd52d4SBartosz Sobczak bus_size_t size; 199cdcd52d4SBartosz Sobczak int nseg; 200cdcd52d4SBartosz Sobczak int flags; 201cdcd52d4SBartosz Sobczak }; 202cdcd52d4SBartosz Sobczak 203cdcd52d4SBartosz Sobczak struct irdma_virt_mem { 204cdcd52d4SBartosz Sobczak void *va; 205cdcd52d4SBartosz Sobczak u32 size; 206cdcd52d4SBartosz Sobczak }; 207cdcd52d4SBartosz Sobczak 208cdcd52d4SBartosz Sobczak struct irdma_dma_info { 209cdcd52d4SBartosz Sobczak dma_addr_t *dmaaddrs; 210cdcd52d4SBartosz Sobczak }; 211cdcd52d4SBartosz Sobczak 212cdcd52d4SBartosz Sobczak struct list_head; 213cdcd52d4SBartosz Sobczak u32 irdma_rd32(struct irdma_dev_ctx *dev_ctx, u32 reg); 214cdcd52d4SBartosz Sobczak void irdma_wr32(struct irdma_dev_ctx *dev_ctx, u32 reg, u32 value); 215cdcd52d4SBartosz Sobczak u64 irdma_rd64(struct irdma_dev_ctx *dev_ctx, u32 reg); 216cdcd52d4SBartosz Sobczak void irdma_wr64(struct irdma_dev_ctx *dev_ctx, u32 reg, u64 value); 217cdcd52d4SBartosz Sobczak 218cdcd52d4SBartosz Sobczak void irdma_term_modify_qp(struct irdma_sc_qp *qp, u8 next_state, u8 term, u8 term_len); 219cdcd52d4SBartosz Sobczak void irdma_terminate_done(struct irdma_sc_qp *qp, int timeout_occurred); 220cdcd52d4SBartosz Sobczak void irdma_terminate_start_timer(struct irdma_sc_qp *qp); 221cdcd52d4SBartosz Sobczak void irdma_terminate_del_timer(struct irdma_sc_qp *qp); 222cdcd52d4SBartosz Sobczak 223cdcd52d4SBartosz Sobczak void irdma_hw_stats_start_timer(struct irdma_sc_vsi *vsi); 224cdcd52d4SBartosz Sobczak void irdma_hw_stats_stop_timer(struct irdma_sc_vsi *vsi); 225cdcd52d4SBartosz Sobczak void irdma_send_ieq_ack(struct irdma_sc_qp *qp); 226cdcd52d4SBartosz Sobczak 227cdcd52d4SBartosz Sobczak u8* irdma_get_hw_addr(void *par); 228cdcd52d4SBartosz Sobczak 229cdcd52d4SBartosz Sobczak void irdma_unmap_vm_page_list(struct irdma_hw *hw, u64 *pg_arr, u32 pg_cnt); 230cdcd52d4SBartosz Sobczak int irdma_map_vm_page_list(struct irdma_hw *hw, void *va, 231cdcd52d4SBartosz Sobczak u64 *pg_arr, u32 pg_cnt); 232cdcd52d4SBartosz Sobczak 23335105900SBartosz Sobczak struct ib_device *to_ibdev(struct irdma_sc_dev *dev); 234cdcd52d4SBartosz Sobczak 235cdcd52d4SBartosz Sobczak #endif /* _ICRDMA_OSDEP_H_ */ 236