1 /* 2 * BSD LICENSE 3 * 4 * Copyright(c) 2017 Cavium, Inc.. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Cavium, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* \file lio_iq.h 35 * \brief Host Driver: Implementation of Octeon input queues. "Input" is 36 * with respect to the Octeon device on the NIC. From this driver's 37 * point of view they are egress queues. 38 */ 39 40 #ifndef __LIO_IQ_H__ 41 #define __LIO_IQ_H__ 42 43 #define LIO_IQ_SEND_OK 0 44 #define LIO_IQ_SEND_STOP 1 45 #define LIO_IQ_SEND_FAILED -1 46 47 /*------------------------- INSTRUCTION QUEUE --------------------------*/ 48 49 #define LIO_REQTYPE_NONE 0 50 #define LIO_REQTYPE_NORESP_NET 1 51 #define LIO_REQTYPE_NORESP_NET_SG 2 52 #define LIO_REQTYPE_RESP_NET 3 53 #define LIO_REQTYPE_SOFT_COMMAND 4 54 55 /* 56 * This structure is used by NIC driver to store information required 57 * to free the mbuf when the packet has been fetched by Octeon. 58 * Bytes offset below assume worst-case of a 64-bit system. 59 */ 60 struct lio_mbuf_free_info { 61 /* Pointer to mbuf. */ 62 struct mbuf *mb; 63 64 /* Pointer to gather list. */ 65 struct lio_gather *g; 66 67 bus_dmamap_t map; 68 }; 69 70 struct lio_request_list { 71 uint32_t reqtype; 72 void *buf; 73 bus_dmamap_t map; 74 struct lio_mbuf_free_info finfo; 75 }; 76 77 /* Input Queue statistics. Each input queue has four stats fields. */ 78 struct lio_iq_stats { 79 uint64_t instr_posted; /**< Instructions posted to this queue. */ 80 uint64_t instr_processed; /**< Instructions processed in this queue. */ 81 uint64_t instr_dropped; /**< Instructions that could not be processed */ 82 uint64_t bytes_sent; /**< Bytes sent through this queue. */ 83 uint64_t sgentry_sent; /**< Gather entries sent through this queue. */ 84 uint64_t tx_done; /**< Num of packets sent to network. */ 85 uint64_t tx_iq_busy; /**< Numof times this iq was found to be full. */ 86 uint64_t tx_dropped; /**< Numof pkts dropped dueto xmitpath errors. */ 87 uint64_t tx_tot_bytes; /**< Total count of bytes sento to network. */ 88 uint64_t tx_gso; /* count of tso */ 89 uint64_t tx_vxlan; /* tunnel */ 90 uint64_t tx_dmamap_fail; 91 uint64_t tx_restart; 92 uint64_t mbuf_defrag_failed; 93 }; 94 95 /* 96 * The instruction (input) queue. 97 * The input queue is used to post raw (instruction) mode data or packet 98 * data to Octeon device from the host. Each input queue for 99 * a Octeon device has one such structure to represent it. 100 */ 101 struct lio_instr_queue { 102 struct octeon_device *oct_dev; 103 104 /* A lock to protect access to the input ring. */ 105 struct mtx lock; 106 107 /* A lock to protect while enqueue to the input ring. */ 108 struct mtx enq_lock; 109 110 /* A lock to protect while posting on the ring. */ 111 struct mtx post_lock; 112 113 uint32_t pkt_in_done; 114 115 /* A lock to protect access to the input ring. */ 116 struct mtx iq_flush_running_lock; 117 118 /* Flag that indicates if the queue uses 64 byte commands. */ 119 uint32_t iqcmd_64B:1; 120 121 /* Queue info. */ 122 union octeon_txpciq txpciq; 123 124 uint32_t rsvd:17; 125 126 uint32_t status:8; 127 128 /* Maximum no. of instructions in this queue. */ 129 uint32_t max_count; 130 131 /* Index in input ring where the driver should write the next packet */ 132 uint32_t host_write_index; 133 134 /* 135 * Index in input ring where Octeon is expected to read the next 136 * packet. 137 */ 138 uint32_t octeon_read_index; 139 140 /* 141 * This index aids in finding the window in the queue where Octeon 142 * has read the commands. 143 */ 144 uint32_t flush_index; 145 146 /* This field keeps track of the instructions pending in this queue. */ 147 volatile int instr_pending; 148 149 uint32_t reset_instr_cnt; 150 151 /* Pointer to the Virtual Base addr of the input ring. */ 152 uint8_t *base_addr; 153 bus_dma_tag_t txtag; 154 155 struct lio_request_list *request_list; 156 157 struct buf_ring *br; 158 159 /* Octeon doorbell register for the ring. */ 160 uint32_t doorbell_reg; 161 162 /* Octeon instruction count register for this ring. */ 163 uint32_t inst_cnt_reg; 164 165 /* Number of instructions pending to be posted to Octeon. */ 166 uint32_t fill_cnt; 167 168 /* The last time that the doorbell was rung. */ 169 uint64_t last_db_time; 170 171 /* 172 * The doorbell timeout. If the doorbell was not rung for this time 173 * and fill_cnt is non-zero, ring the doorbell again. 174 */ 175 uint32_t db_timeout; 176 177 /* Statistics for this input queue. */ 178 struct lio_iq_stats stats; 179 180 /* DMA mapped base address of the input descriptor ring. */ 181 uint64_t base_addr_dma; 182 183 /* Application context */ 184 void *app_ctx; 185 186 /* network stack queue index */ 187 int q_index; 188 189 /* os ifidx associated with this queue */ 190 int ifidx; 191 192 }; 193 194 /*---------------------- INSTRUCTION FORMAT ----------------------------*/ 195 196 struct lio_instr3_64B { 197 /* Pointer where the input data is available. */ 198 uint64_t dptr; 199 200 /* Instruction Header. */ 201 uint64_t ih3; 202 203 /* Instruction Header. */ 204 uint64_t pki_ih3; 205 206 /* Input Request Header. */ 207 uint64_t irh; 208 209 /* opcode/subcode specific parameters */ 210 uint64_t ossp[2]; 211 212 /* Return Data Parameters */ 213 uint64_t rdp; 214 215 /* 216 * Pointer where the response for a RAW mode packet will be written 217 * by Octeon. 218 */ 219 uint64_t rptr; 220 221 }; 222 223 union lio_instr_64B { 224 struct lio_instr3_64B cmd3; 225 }; 226 227 /* The size of each buffer in soft command buffer pool */ 228 #define LIO_SOFT_COMMAND_BUFFER_SIZE 2048 229 230 struct lio_soft_command { 231 /* Soft command buffer info. */ 232 struct lio_stailq_node node; 233 uint64_t dma_addr; 234 uint32_t size; 235 236 /* Command and return status */ 237 union lio_instr_64B cmd; 238 239 #define COMPLETION_WORD_INIT 0xffffffffffffffffULL 240 uint64_t *status_word; 241 242 /* Data buffer info */ 243 void *virtdptr; 244 uint64_t dmadptr; 245 uint32_t datasize; 246 247 /* Return buffer info */ 248 void *virtrptr; 249 uint64_t dmarptr; 250 uint32_t rdatasize; 251 252 /* Context buffer info */ 253 void *ctxptr; 254 uint32_t ctxsize; 255 256 /* Time out and callback */ 257 int wait_time; 258 int timeout; 259 uint32_t iq_no; 260 void (*callback) (struct octeon_device *, uint32_t, 261 void *); 262 void *callback_arg; 263 }; 264 265 /* Maximum number of buffers to allocate into soft command buffer pool */ 266 #define LIO_MAX_SOFT_COMMAND_BUFFERS 256 267 268 /* Head of a soft command buffer pool. */ 269 struct lio_sc_buffer_pool { 270 /* List structure to add delete pending entries to */ 271 struct lio_stailq_head head; 272 273 /* A lock for this response list */ 274 struct mtx lock; 275 276 volatile uint32_t alloc_buf_count; 277 }; 278 279 #define LIO_INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \ 280 (((octeon_dev_ptr)->instr_queue[iq_no]->stats.field) += count) 281 282 int lio_setup_sc_buffer_pool(struct octeon_device *oct); 283 int lio_free_sc_buffer_pool(struct octeon_device *oct); 284 struct lio_soft_command *lio_alloc_soft_command(struct octeon_device *oct, 285 uint32_t datasize, 286 uint32_t rdatasize, 287 uint32_t ctxsize); 288 void lio_free_soft_command(struct octeon_device *oct, 289 struct lio_soft_command *sc); 290 291 /* 292 * lio_init_instr_queue() 293 * @param octeon_dev - pointer to the octeon device structure. 294 * @param txpciq - queue to be initialized (0 <= q_no <= 3). 295 * 296 * Called at driver init time for each input queue. iq_conf has the 297 * configuration parameters for the queue. 298 * 299 * @return Success: 0 Failure: 1 300 */ 301 int lio_init_instr_queue(struct octeon_device *octeon_dev, 302 union octeon_txpciq txpciq, uint32_t num_descs); 303 304 /* 305 * lio_delete_instr_queue() 306 * @param octeon_dev - pointer to the octeon device structure. 307 * @param iq_no - queue to be deleted 308 * 309 * Called at driver unload time for each input queue. Deletes all 310 * allocated resources for the input queue. 311 * 312 * @return Success: 0 Failure: 1 313 */ 314 int lio_delete_instr_queue(struct octeon_device *octeon_dev, 315 uint32_t iq_no); 316 317 int lio_wait_for_instr_fetch(struct octeon_device *oct); 318 319 int lio_process_iq_request_list(struct octeon_device *oct, 320 struct lio_instr_queue *iq, 321 uint32_t budget); 322 323 int lio_send_command(struct octeon_device *oct, uint32_t iq_no, 324 uint32_t force_db, void *cmd, void *buf, 325 uint32_t datasize, uint32_t reqtype); 326 327 void lio_prepare_soft_command(struct octeon_device *oct, 328 struct lio_soft_command *sc, 329 uint8_t opcode, uint8_t subcode, 330 uint32_t irh_ossp, uint64_t ossp0, 331 uint64_t ossp1); 332 333 int lio_send_soft_command(struct octeon_device *oct, 334 struct lio_soft_command *sc); 335 336 int lio_setup_iq(struct octeon_device *oct, int ifidx, 337 int q_index, union octeon_txpciq iq_no, 338 uint32_t num_descs); 339 int lio_flush_iq(struct octeon_device *oct, struct lio_instr_queue *iq, 340 uint32_t budget); 341 #endif /* __LIO_IQ_H__ */ 342