4 * Copyright(c) 2017 Cavium, Inc.. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 * \brief Implementation of Octeon Output queues. "Output" is with
37 * respect to the Octeon device on the NIC. From this driver's point of
38 * view they are ingress queues.
41 #ifndef __LIO_DROQ_H__
42 #define __LIO_DROQ_H__
45 * Octeon descriptor format.
46 * The descriptor ring is made of descriptors which have 2 64-bit values:
47 * -# Physical (bus) address of the data buffer.
48 * -# Physical (bus) address of a lio_droq_info structure.
49 * The Octeon device DMA's incoming packets and its information at the address
50 * given by these descriptor fields.
52 struct lio_droq_desc {
53 /* The buffer pointer */
56 /* The Info pointer */
60 #define LIO_DROQ_DESC_SIZE (sizeof(struct lio_droq_desc))
63 * Information about packet DMA'ed by Octeon.
64 * The format of the information available at Info Pointer after Octeon
65 * has posted a packet. Not all descriptors have valid information. Only
66 * the Info field of the first descriptor for a packet has information
69 struct lio_droq_info {
70 /* The Length of the packet. */
73 /* The Output Receive Header. */
78 #define LIO_DROQ_INFO_SIZE (sizeof(struct lio_droq_info))
81 * Pointer to data buffer.
82 * Driver keeps a pointer to the data buffer that it made available to
83 * the Octeon device. Since the descriptor ring keeps physical (bus)
84 * addresses, this field is required for the driver to keep track of
85 * the virtual address pointers.
87 struct lio_recv_buffer {
88 /* Packet buffer, including metadata. */
91 /* Data in the packet buffer. */
95 #define LIO_DROQ_RECVBUF_SIZE (sizeof(struct lio_recv_buffer))
97 /* Output Queue statistics. Each output queue has four stats fields. */
98 struct lio_droq_stats {
99 /* Number of packets received in this queue. */
100 uint64_t pkts_received;
102 /* Bytes received by this queue. */
103 uint64_t bytes_received;
105 /* Packets dropped due to no dispatch function. */
106 uint64_t dropped_nodispatch;
108 /* Packets dropped due to no memory available. */
109 uint64_t dropped_nomem;
111 /* Packets dropped due to large number of pkts to process. */
112 uint64_t dropped_toomany;
114 /* Number of packets sent to stack from this queue. */
115 uint64_t rx_pkts_received;
117 /* Number of Bytes sent to stack from this queue. */
118 uint64_t rx_bytes_received;
120 /* Num of Packets dropped due to receive path failures. */
125 /* Num of failures of lio_recv_buffer_alloc() */
126 uint64_t rx_alloc_failure;
131 * The maximum number of buffers that can be dispatched from the
132 * output/dma queue. Set to 64 assuming 1K buffers in DROQ and the fact that
133 * max packet size from DROQ is 64K.
135 #define LIO_MAX_RECV_BUFS 64
138 * Receive Packet format used when dispatching output queue packets
139 * with non-raw opcodes.
140 * The received packet will be sent to the upper layers using this
141 * structure which is passed as a parameter to the dispatch function
143 struct lio_recv_pkt {
144 /* Number of buffers in this received packet */
145 uint16_t buffer_count;
147 /* Id of the device that is sending the packet up */
150 /* Length of data in the packet buffer */
153 /* The receive header */
156 /* Pointer to the OS-specific packet buffer */
157 struct mbuf *buffer_ptr[LIO_MAX_RECV_BUFS];
159 /* Size of the buffers pointed to by ptr's in buffer_ptr */
160 uint32_t buffer_size[LIO_MAX_RECV_BUFS];
163 #define LIO_RECV_PKT_SIZE (sizeof(struct lio_recv_pkt))
166 * The first parameter of a dispatch function.
167 * For a raw mode opcode, the driver dispatches with the device
168 * pointer in this structure.
169 * For non-raw mode opcode, the driver dispatches the recv_pkt
170 * created to contain the buffers with data received from Octeon.
171 * ---------------------
172 * | *recv_pkt ----|---
173 * |-------------------| |
174 * | 0 or more bytes | |
175 * | reserved by driver| |
176 * |-------------------|<-/
179 * |___________________|
181 struct lio_recv_info {
183 struct lio_recv_pkt *recv_pkt;
186 #define LIO_RECV_INFO_SIZE (sizeof(struct lio_recv_info))
189 * Allocate a recv_info structure. The recv_pkt pointer in the recv_info
190 * structure is filled in before this call returns.
191 * @param extra_bytes - extra bytes to be allocated at the end of the recv info
193 * @return - pointer to a newly allocated recv_info structure.
195 static inline struct lio_recv_info *
196 lio_alloc_recv_info(int extra_bytes)
198 struct lio_recv_info *recv_info;
201 buf = malloc(LIO_RECV_PKT_SIZE + LIO_RECV_INFO_SIZE +
202 extra_bytes, M_DEVBUF, M_NOWAIT | M_ZERO);
206 recv_info = (struct lio_recv_info *)buf;
207 recv_info->recv_pkt = (struct lio_recv_pkt *)(buf + LIO_RECV_INFO_SIZE);
208 recv_info->rsvd = NULL;
210 recv_info->rsvd = buf + LIO_RECV_INFO_SIZE + LIO_RECV_PKT_SIZE;
216 * Free a recv_info structure.
217 * @param recv_info - Pointer to receive_info to be freed
220 lio_free_recv_info(struct lio_recv_info *recv_info)
223 free(recv_info, M_DEVBUF);
226 typedef int (*lio_dispatch_fn_t)(struct lio_recv_info *, void *);
229 * Used by NIC module to register packet handler and to get device
230 * information for each octeon device.
232 struct lio_droq_ops {
234 * This registered function will be called by the driver with
235 * the pointer to buffer from droq and length of
236 * data in the buffer. The receive header gives the port
237 * number to the caller. Function pointer is set by caller.
239 void (*fptr) (void *, uint32_t, union octeon_rh *, void *,
244 * Flag indicating if the DROQ handler should drop packets that
245 * it cannot handle in one iteration. Set by caller.
247 uint32_t drop_on_max;
251 * The Descriptor Ring Output Queue structure.
252 * This structure has all the information required to implement a
256 /* A lock to protect access to this ring. */
263 struct lio_droq_ops ops;
265 struct octeon_device *oct_dev;
267 /* The 8B aligned descriptor ring starts at this address. */
268 struct lio_droq_desc *desc_ring;
270 /* Index in the ring where the driver should read the next packet */
274 * Index in the ring where the driver will refill the descriptor's
279 /* Packets pending to be processed */
280 volatile int pkts_pending;
282 /* Number of descriptors in this ring. */
285 /* The number of descriptors pending refill. */
286 uint32_t refill_count;
288 uint32_t pkts_per_intr;
289 uint32_t refill_threshold;
292 * The max number of descriptors in DROQ without a buffer.
293 * This field is used to keep track of empty space threshold. If the
294 * refill_count reaches this value, the DROQ cannot accept a max-sized
297 uint32_t max_empty_descs;
300 * The receive buffer list. This list has the virtual addresses of
303 struct lio_recv_buffer *recv_buf_list;
305 /* The size of each buffer pointed by the buffer pointer. */
306 uint32_t buffer_size;
309 * Offset to packet credit register.
310 * Host writes number of info/buffer ptrs available to this register
312 uint32_t pkts_credit_reg;
315 * Offset packet sent register.
316 * Octeon writes the number of packets DMA'ed to host memory
319 uint32_t pkts_sent_reg;
321 struct lio_stailq_head dispatch_stq_head;
323 /* Statistics for this DROQ. */
324 struct lio_droq_stats stats;
326 /* DMA mapped address of the DROQ descriptor ring. */
327 vm_paddr_t desc_ring_dma;
329 /* application context */
334 struct task droq_task;
335 struct taskqueue *droq_taskqueue;
340 #define LIO_DROQ_SIZE (sizeof(struct lio_droq))
343 * Allocates space for the descriptor ring for the droq and sets the
344 * base addr, num desc etc in Octeon registers.
346 * @param oct_dev - pointer to the octeon device structure
347 * @param q_no - droq no.
348 * @param app_ctx - pointer to application context
349 * @return Success: 0 Failure: 1
351 int lio_init_droq(struct octeon_device *oct_dev,
352 uint32_t q_no, uint32_t num_descs, uint32_t desc_size,
356 * Frees the space for descriptor ring for the droq.
358 * @param oct_dev - pointer to the octeon device structure
359 * @param q_no - droq no.
360 * @return: Success: 0 Failure: 1
362 int lio_delete_droq(struct octeon_device *oct_dev, uint32_t q_no);
365 * Register a change in droq operations. The ops field has a pointer to a
366 * function which will called by the DROQ handler for all packets arriving
367 * on output queues given by q_no irrespective of the type of packet.
368 * The ops field also has a flag which if set tells the DROQ handler to
369 * drop packets if it receives more than what it can process in one
370 * invocation of the handler.
371 * @param oct - octeon device
372 * @param q_no - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
373 * @param ops - the droq_ops settings for this queue
374 * @return - 0 on success, -ENODEV or -EINVAL on error.
376 int lio_register_droq_ops(struct octeon_device *oct, uint32_t q_no,
377 struct lio_droq_ops *ops);
380 * Resets the function pointer and flag settings made by
381 * lio_register_droq_ops(). After this routine is called, the DROQ handler
382 * will lookup dispatch function for each arriving packet on the output queue
384 * @param oct - octeon device
385 * @param q_no - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
386 * @return - 0 on success, -ENODEV or -EINVAL on error.
388 int lio_unregister_droq_ops(struct octeon_device *oct, uint32_t q_no);
391 * Register a dispatch function for a opcode/subcode. The driver will call
392 * this dispatch function when it receives a packet with the given
393 * opcode/subcode in its output queues along with the user specified
395 * @param oct - the octeon device to register with.
396 * @param opcode - the opcode for which the dispatch will be registered.
397 * @param subcode - the subcode for which the dispatch will be registered
398 * @param fn - the dispatch function.
399 * @param fn_arg - user specified that will be passed along with the
400 * dispatch function by the driver.
401 * @return Success: 0; Failure: 1
403 int lio_register_dispatch_fn(struct octeon_device *oct, uint16_t opcode,
404 uint16_t subcode, lio_dispatch_fn_t fn,
408 * Remove registration for an opcode/subcode. This will delete the mapping for
409 * an opcode/subcode. The dispatch function will be unregistered and will no
410 * longer be called if a packet with the opcode/subcode arrives in the driver
412 * @param oct - the octeon device to unregister from.
413 * @param opcode - the opcode to be unregistered.
414 * @param subcode - the subcode to be unregistered.
416 * @return Success: 0; Failure: 1
418 int lio_unregister_dispatch_fn(struct octeon_device *oct, uint16_t opcode,
421 uint32_t lio_droq_check_hw_for_pkts(struct lio_droq *droq);
423 int lio_create_droq(struct octeon_device *oct, uint32_t q_no,
424 uint32_t num_descs, uint32_t desc_size, void *app_ctx);
426 int lio_droq_process_packets(struct octeon_device *oct,
427 struct lio_droq *droq, uint32_t budget);
429 uint32_t lio_droq_refill(struct octeon_device *octeon_dev,
430 struct lio_droq *droq);
431 void lio_droq_bh(void *ptr, int pending __unused);
432 #endif /* __LIO_DROQ_H__ */