| 1 | /* |
| 2 | * Copyright 2010-2017 Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * Disclaimer: The codes contained in these modules may be specific to |
| 14 | * the Intel Software Development Platform codenamed Knights Ferry, |
| 15 | * and the Intel product codenamed Knights Corner, and are not backward |
| 16 | * compatible with other Intel products. Additionally, Intel will NOT |
| 17 | * support the codes or instruction set in future products. |
| 18 | * |
| 19 | * Intel offers no warranty of any kind regarding the code. This code is |
| 20 | * licensed on an "AS IS" basis and Intel is not obligated to provide |
| 21 | * any support, assistance, installation, training, or other services |
| 22 | * of any kind. Intel is also not obligated to provide any updates, |
| 23 | * enhancements or extensions. Intel specifically disclaims any warranty |
| 24 | * of merchantability, non-infringement, fitness for any particular |
| 25 | * purpose, and any other warranty. |
| 26 | * |
| 27 | * Further, Intel disclaims all liability of any kind, including but |
| 28 | * not limited to liability for infringement of any proprietary rights, |
| 29 | * relating to the use of the code, even if Intel is notified of the |
| 30 | * possibility of such liability. Except as expressly stated in an Intel |
| 31 | * license agreement provided with this code and agreed upon with Intel, |
| 32 | * no license, express or implied, by estoppel or otherwise, to any |
| 33 | * intellectual property rights is granted herein. |
| 34 | */ |
| 35 | |
| 36 | #ifndef MICVETH_DMA_H |
| 37 | #define MICVETH_DMA_H |
| 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include "micint.h" |
| 41 | |
| 42 | #include "mic_common.h" |
| 43 | #include "mic_dma_lib.h" |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/hardirq.h> |
| 46 | #include <linux/types.h> |
| 47 | #include <linux/capability.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/string.h> |
| 50 | #include <linux/gfp.h> |
| 51 | #include <linux/vmalloc.h> |
| 52 | #include <asm/io.h> |
| 53 | #include <linux/kernel.h> |
| 54 | #include <linux/mm_types.h> |
| 55 | #include <linux/jiffies.h> |
| 56 | #include <linux/timer.h> |
| 57 | #include <linux/irqflags.h> |
| 58 | #include <linux/time.h> |
| 59 | #include <linux/spinlock.h> |
| 60 | #include <linux/mutex.h> |
| 61 | #include <linux/semaphore.h> |
| 62 | #include <linux/kthread.h> |
| 63 | #include <linux/sched.h> |
| 64 | #include <linux/delay.h> |
| 65 | #include <linux/wait.h> |
| 66 | #include <asm/bug.h> |
| 67 | #include <linux/pci.h> |
| 68 | #include <linux/device.h> |
| 69 | #include <linux/fs.h> |
| 70 | #include <linux/list.h> |
| 71 | #include <linux/workqueue.h> |
| 72 | #include <linux/interrupt.h> |
| 73 | #include <asm/atomic.h> |
| 74 | #include <linux/netdevice.h> |
| 75 | #include <linux/debugfs.h> |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | Define this if only DMA mode is supported without legacy POLL/INTR modes |
| 80 | (i.e if only micveth_dma.c is included in the host/card side drivers, i.e |
| 81 | when linvnet.c is excluded from host side driver and micveth.c from card |
| 82 | side driver). This will ensure that other global symbols which are at |
| 83 | present common with legacy modes (in linvnet.c/micveth.c) are all included |
| 84 | in micveth_dma.c. |
| 85 | */ |
| 86 | #undef STANDALONE_VNET_DMA |
| 87 | |
| 88 | /*******************************************************/ |
| 89 | #define MICVNET_MSG_RB_SIZE 128 |
| 90 | #define DMA_ALIGNMENT L1_CACHE_BYTES |
| 91 | #define VNET_MAX_SKBS 62 |
| 92 | |
| 93 | /* The maximum total number of outstanding messages possible in the current |
| 94 | implementation is 2 * VNET_MAX_SKBS + 1. */ |
| 95 | #if (MICVNET_MSG_RB_SIZE < 2 * VNET_MAX_SKBS + 2) |
| 96 | #error "MICVNET_MSG_RB_SIZE should be at least (2 * VNET_MAX_SKBS + 2)" |
| 97 | #endif |
| 98 | |
| 99 | #if (MICVNET_MSG_RB_SIZE & (MICVNET_MSG_RB_SIZE - 1)) |
| 100 | #error "MICVNET_MSG_RB_SIZE should be power of 2" |
| 101 | #endif |
| 102 | |
| 103 | enum micvnet_msg_id { |
| 104 | MICVNET_MSG_ADD_DMA_BUFFER, |
| 105 | MICVNET_MSG_DMA_COMPLETE, |
| 106 | MICVNET_MSG_LINK_DOWN, |
| 107 | MICVNET_MSG_LINK_UP, |
| 108 | }; |
| 109 | |
| 110 | struct micvnet_msg_add_dma_buffer { |
| 111 | uint64_t buf_phys; |
| 112 | uint64_t buf_size; |
| 113 | }; |
| 114 | |
| 115 | struct micvnet_msg_dma_complete { |
| 116 | uint64_t dst_phys; |
| 117 | uint64_t size; |
| 118 | uint64_t dma_offset; |
| 119 | }; |
| 120 | |
| 121 | #define VNET_DRIVER_VERSION 1 |
| 122 | struct micvnet_msg_link_up { |
| 123 | uint64_t vnet_driver_version; |
| 124 | }; |
| 125 | |
| 126 | union micvnet_msg_body { |
| 127 | struct micvnet_msg_add_dma_buffer micvnet_msg_add_dma_buffer; |
| 128 | struct micvnet_msg_dma_complete micvnet_msg_dma_complete; |
| 129 | struct micvnet_msg_link_up micvnet_msg_link_up; |
| 130 | }; |
| 131 | |
| 132 | struct micvnet_msg { |
| 133 | uint64_t msg_id; |
| 134 | union micvnet_msg_body body; |
| 135 | }; |
| 136 | |
| 137 | struct micvnet_msg_rb { |
| 138 | struct micvnet_msg buf[MICVNET_MSG_RB_SIZE]; |
| 139 | volatile uint32_t head; |
| 140 | volatile uint32_t tail; |
| 141 | uint32_t size; |
| 142 | volatile uint32_t prev_head; |
| 143 | volatile uint32_t prev_tail; |
| 144 | }; |
| 145 | |
| 146 | struct micvnet_msg_ring_pair { |
| 147 | struct micvnet_msg_rb rb_tx; |
| 148 | struct micvnet_msg_rb rb_rx; |
| 149 | }; |
| 150 | |
| 151 | struct micvnet_msg_qp { |
| 152 | struct micvnet_msg_rb *tx; |
| 153 | struct micvnet_msg_rb *rx; |
| 154 | }; |
| 155 | |
| 156 | /*******************************************************/ |
| 157 | |
| 158 | /* Restict micvnet mtu to 63K because ping does not work on RHEL 6.3 with 64K |
| 159 | MTU - HSD [4118026] */ |
| 160 | #define MICVNET_MAX_MTU (63 * 1024) |
| 161 | #define MICVNET_CARD_UP_MAGIC 0x1A77BBEE |
| 162 | |
| 163 | struct rx_node { |
| 164 | struct list_head list; |
| 165 | struct sk_buff *skb; |
| 166 | uint64_t phys; |
| 167 | uint64_t size; |
| 168 | }; |
| 169 | |
| 170 | struct dma_node { |
| 171 | struct list_head list; |
| 172 | uint64_t phys; |
| 173 | uint64_t size; |
| 174 | }; |
| 175 | |
| 176 | struct tx_node { |
| 177 | struct list_head list; |
| 178 | struct sk_buff *skb; |
| 179 | }; |
| 180 | |
| 181 | struct sched_node { |
| 182 | struct list_head list; |
| 183 | struct sk_buff *skb; |
| 184 | unsigned char *skb_data_aligned; |
| 185 | uint64_t dma_src_phys; |
| 186 | uint64_t dma_size; |
| 187 | uint64_t dma_offset; |
| 188 | uint64_t dst_phys; |
| 189 | }; |
| 190 | |
| 191 | struct obj_list { |
| 192 | char *buf; |
| 193 | int size; |
| 194 | size_t obj_size; |
| 195 | volatile uint32_t head; |
| 196 | volatile uint32_t tail; |
| 197 | }; |
| 198 | |
| 199 | struct micvnet_info { |
| 200 | struct pci_dev *vi_pdev; |
| 201 | struct net_device *vi_netdev; |
| 202 | uint8_t *vi_sbox; |
| 203 | uint8_t *vi_dbox; |
| 204 | uint32_t *vi_scratch14; |
| 205 | mic_ctx_t *mic_ctx; |
| 206 | atomic_t vi_state; |
| 207 | |
| 208 | struct workqueue_struct *vi_wq; |
| 209 | char vi_wqname[16]; |
| 210 | struct work_struct vi_ws_bh; |
| 211 | struct work_struct vi_ws_tx; |
| 212 | struct work_struct vi_ws_dmacb; |
| 213 | struct work_struct vi_ws_link_down; |
| 214 | struct work_struct vi_ws_stop; |
| 215 | struct work_struct vi_ws_start; |
| 216 | |
| 217 | spinlock_t vi_rxlock; |
| 218 | spinlock_t vi_txlock; |
| 219 | |
| 220 | #ifdef HOST |
| 221 | struct micvnet_msg_ring_pair vi_rp; |
| 222 | #else |
| 223 | struct micvnet_msg_ring_pair *ring_ptr; |
| 224 | #endif |
| 225 | uint64_t vi_rp_phys; |
| 226 | struct micvnet_msg_qp vi_qp; |
| 227 | |
| 228 | struct obj_list dnode_list; |
| 229 | |
| 230 | struct list_head vi_rx_skb; |
| 231 | struct list_head vi_dma_buf; |
| 232 | struct list_head vi_tx_skb; |
| 233 | struct list_head vi_sched_skb; |
| 234 | |
| 235 | mic_dma_handle_t dma_handle; |
| 236 | struct dma_channel *dma_chan; |
| 237 | struct dma_completion_cb dma_cb; |
| 238 | atomic_t cnt_dma_complete; |
| 239 | |
| 240 | atomic_t cnt_dma_buf_avail; |
| 241 | bool link_down_initiator; |
| 242 | atomic_t cnt_tx_pending; |
| 243 | wait_queue_head_t stop_waitq; |
| 244 | }; |
| 245 | |
| 246 | |
| 247 | struct micvnet { |
| 248 | atomic_t lv_active_clients; |
| 249 | int created; |
| 250 | }; |
| 251 | |
| 252 | int micvnet_init(struct device *dev); |
| 253 | void micvnet_exit(void); |
| 254 | int micvnet_probe(mic_ctx_t *mic_ctx); |
| 255 | void micvnet_remove(mic_ctx_t *mic_ctx); |
| 256 | int micvnet_xmit(struct sk_buff *skb, struct net_device *dev); |
| 257 | |
| 258 | int micvnet_start(mic_ctx_t *mic_ctx); |
| 259 | void micvnet_stop(mic_ctx_t *mic_ctx); |
| 260 | |
| 261 | #ifndef HOST |
| 262 | int __init micvnet_module_init(void); |
| 263 | void __exit micvnet_module_exit(void); |
| 264 | #endif |
| 265 | |
| 266 | #ifdef STANDALONE_VNET_DMA |
| 267 | #define micveth_init micvnet_init |
| 268 | #define micveth_exit micvnet_exit |
| 269 | #define micveth_probe micvnet_probe |
| 270 | #define micveth_remove micvnet_remove |
| 271 | #define micveth_start micvnet_start |
| 272 | #define micveth_stop micvnet_stop |
| 273 | #endif |
| 274 | |
| 275 | extern int vnet_num_buffers; |
| 276 | #ifndef HOST |
| 277 | extern ulong vnet_addr; |
| 278 | #endif |
| 279 | #endif // MICVETH_DMA_H |