Back to home page

OSCL-LXR

 
 

    


0001 /**********************************************************************
0002  * Author: Cavium, Inc.
0003  *
0004  * Contact: support@cavium.com
0005  *          Please include "LiquidIO" in the subject.
0006  *
0007  * Copyright (c) 2003-2016 Cavium, Inc.
0008  *
0009  * This file is free software; you can redistribute it and/or modify
0010  * it under the terms of the GNU General Public License, Version 2, as
0011  * published by the Free Software Foundation.
0012  *
0013  * This file is distributed in the hope that it will be useful, but
0014  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
0015  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
0016  * NONINFRINGEMENT.  See the GNU General Public License for more details.
0017  ***********************************************************************/
0018 /*!  \file  octeon_droq.h
0019  *   \brief Implementation of Octeon Output queues. "Output" is with
0020  *   respect to the Octeon device on the NIC. From this driver's point of
0021  *   view they are ingress queues.
0022  */
0023 
0024 #ifndef __OCTEON_DROQ_H__
0025 #define __OCTEON_DROQ_H__
0026 
0027 /* Default number of packets that will be processed in one iteration. */
0028 #define MAX_PACKET_BUDGET 0xFFFFFFFF
0029 
0030 /** Octeon descriptor format.
0031  *  The descriptor ring is made of descriptors which have 2 64-bit values:
0032  *  -# Physical (bus) address of the data buffer.
0033  *  -# Physical (bus) address of a octeon_droq_info structure.
0034  *  The Octeon device DMA's incoming packets and its information at the address
0035  *  given by these descriptor fields.
0036  */
0037 struct octeon_droq_desc {
0038     /** The buffer pointer */
0039     u64 buffer_ptr;
0040 
0041     /** The Info pointer */
0042     u64 info_ptr;
0043 };
0044 
0045 #define OCT_DROQ_DESC_SIZE    (sizeof(struct octeon_droq_desc))
0046 
0047 /** Information about packet DMA'ed by Octeon.
0048  *  The format of the information available at Info Pointer after Octeon
0049  *  has posted a packet. Not all descriptors have valid information. Only
0050  *  the Info field of the first descriptor for a packet has information
0051  *  about the packet.
0052  */
0053 struct octeon_droq_info {
0054     /** The Length of the packet. */
0055     u64 length;
0056 
0057     /** The Output Receive Header. */
0058     union octeon_rh rh;
0059 };
0060 
0061 #define OCT_DROQ_INFO_SIZE   (sizeof(struct octeon_droq_info))
0062 
0063 struct octeon_skb_page_info {
0064     /* DMA address for the page */
0065     dma_addr_t dma;
0066 
0067     /* Page for the rx dma  **/
0068     struct page *page;
0069 
0070     /** which offset into page */
0071     unsigned int page_offset;
0072 };
0073 
0074 /** Pointer to data buffer.
0075  *  Driver keeps a pointer to the data buffer that it made available to
0076  *  the Octeon device. Since the descriptor ring keeps physical (bus)
0077  *  addresses, this field is required for the driver to keep track of
0078  *  the virtual address pointers.
0079  */
0080 struct octeon_recv_buffer {
0081     /** Packet buffer, including metadata. */
0082     void *buffer;
0083 
0084     /** Data in the packet buffer.  */
0085     u8 *data;
0086 
0087     /** pg_info **/
0088     struct octeon_skb_page_info pg_info;
0089 };
0090 
0091 #define OCT_DROQ_RECVBUF_SIZE    (sizeof(struct octeon_recv_buffer))
0092 
0093 /** Output Queue statistics. Each output queue has four stats fields. */
0094 struct oct_droq_stats {
0095     /** Number of packets received in this queue. */
0096     u64 pkts_received;
0097 
0098     /** Bytes received by this queue. */
0099     u64 bytes_received;
0100 
0101     /** Packets dropped due to no dispatch function. */
0102     u64 dropped_nodispatch;
0103 
0104     /** Packets dropped due to no memory available. */
0105     u64 dropped_nomem;
0106 
0107     /** Packets dropped due to large number of pkts to process. */
0108     u64 dropped_toomany;
0109 
0110     /** Number of packets  sent to stack from this queue. */
0111     u64 rx_pkts_received;
0112 
0113     /** Number of Bytes sent to stack from this queue. */
0114     u64 rx_bytes_received;
0115 
0116     /** Num of Packets dropped due to receive path failures. */
0117     u64 rx_dropped;
0118 
0119     u64 rx_vxlan;
0120 
0121     /** Num of failures of recv_buffer_alloc() */
0122     u64 rx_alloc_failure;
0123 
0124 };
0125 
0126 /* The maximum number of buffers that can be dispatched from the
0127  * output/dma queue. Set to 64 assuming 1K buffers in DROQ and the fact that
0128  * max packet size from DROQ is 64K.
0129  */
0130 #define    MAX_RECV_BUFS    64
0131 
0132 /** Receive Packet format used when dispatching output queue packets
0133  *  with non-raw opcodes.
0134  *  The received packet will be sent to the upper layers using this
0135  *  structure which is passed as a parameter to the dispatch function
0136  */
0137 struct octeon_recv_pkt {
0138     /**  Number of buffers in this received packet */
0139     u16 buffer_count;
0140 
0141     /** Id of the device that is sending the packet up */
0142     u16 octeon_id;
0143 
0144     /** Length of data in the packet buffer */
0145     u32 length;
0146 
0147     /** The receive header */
0148     union octeon_rh rh;
0149 
0150     /** Pointer to the OS-specific packet buffer */
0151     void *buffer_ptr[MAX_RECV_BUFS];
0152 
0153     /** Size of the buffers pointed to by ptr's in buffer_ptr */
0154     u32 buffer_size[MAX_RECV_BUFS];
0155 };
0156 
0157 #define OCT_RECV_PKT_SIZE    (sizeof(struct octeon_recv_pkt))
0158 
0159 /** The first parameter of a dispatch function.
0160  *  For a raw mode opcode, the driver dispatches with the device
0161  *  pointer in this structure.
0162  *  For non-raw mode opcode, the driver dispatches the recv_pkt
0163  *  created to contain the buffers with data received from Octeon.
0164  *  ---------------------
0165  *  |     *recv_pkt ----|---
0166  *  |-------------------|   |
0167  *  | 0 or more bytes   |   |
0168  *  | reserved by driver|   |
0169  *  |-------------------|<-/
0170  *  | octeon_recv_pkt   |
0171  *  |                   |
0172  *  |___________________|
0173  */
0174 struct octeon_recv_info {
0175     void *rsvd;
0176     struct octeon_recv_pkt *recv_pkt;
0177 };
0178 
0179 #define  OCT_RECV_INFO_SIZE    (sizeof(struct octeon_recv_info))
0180 
0181 /** Allocate a recv_info structure. The recv_pkt pointer in the recv_info
0182  *  structure is filled in before this call returns.
0183  *  @param extra_bytes - extra bytes to be allocated at the end of the recv info
0184  *                       structure.
0185  *  @return - pointer to a newly allocated recv_info structure.
0186  */
0187 static inline struct octeon_recv_info *octeon_alloc_recv_info(int extra_bytes)
0188 {
0189     struct octeon_recv_info *recv_info;
0190     u8 *buf;
0191 
0192     buf = kmalloc(OCT_RECV_PKT_SIZE + OCT_RECV_INFO_SIZE +
0193               extra_bytes, GFP_ATOMIC);
0194     if (!buf)
0195         return NULL;
0196 
0197     recv_info = (struct octeon_recv_info *)buf;
0198     recv_info->recv_pkt =
0199         (struct octeon_recv_pkt *)(buf + OCT_RECV_INFO_SIZE);
0200     recv_info->rsvd = NULL;
0201     if (extra_bytes)
0202         recv_info->rsvd = buf + OCT_RECV_INFO_SIZE + OCT_RECV_PKT_SIZE;
0203 
0204     return recv_info;
0205 }
0206 
0207 /** Free a recv_info structure.
0208  *  @param recv_info - Pointer to receive_info to be freed
0209  */
0210 static inline void octeon_free_recv_info(struct octeon_recv_info *recv_info)
0211 {
0212     kfree(recv_info);
0213 }
0214 
0215 typedef int (*octeon_dispatch_fn_t)(struct octeon_recv_info *, void *);
0216 
0217 /** Used by NIC module to register packet handler and to get device
0218  * information for each octeon device.
0219  */
0220 struct octeon_droq_ops {
0221     /** This registered function will be called by the driver with
0222      *  the octeon id, pointer to buffer from droq and length of
0223      *  data in the buffer. The receive header gives the port
0224      *  number to the caller.  Function pointer is set by caller.
0225      */
0226     void (*fptr)(u32, void *, u32, union octeon_rh *, void *, void *);
0227     void *farg;
0228 
0229     /* This function will be called by the driver for all NAPI related
0230      * events. The first param is the octeon id. The second param is the
0231      * output queue number. The third is the NAPI event that occurred.
0232      */
0233     void (*napi_fn)(void *);
0234 
0235     u32 poll_mode;
0236 
0237     /** Flag indicating if the DROQ handler should drop packets that
0238      *  it cannot handle in one iteration. Set by caller.
0239      */
0240     u32 drop_on_max;
0241 };
0242 
0243 /** The Descriptor Ring Output Queue structure.
0244  *  This structure has all the information required to implement a
0245  *  Octeon DROQ.
0246  */
0247 struct octeon_droq {
0248     u32 q_no;
0249 
0250     u32 pkt_count;
0251 
0252     struct octeon_droq_ops ops;
0253 
0254     struct octeon_device *oct_dev;
0255 
0256     /** The 8B aligned descriptor ring starts at this address. */
0257     struct octeon_droq_desc *desc_ring;
0258 
0259     /** Index in the ring where the driver should read the next packet */
0260     u32 read_idx;
0261 
0262     /** Index in the ring where Octeon will write the next packet */
0263     u32 write_idx;
0264 
0265     /** Index in the ring where the driver will refill the descriptor's
0266      * buffer
0267      */
0268     u32 refill_idx;
0269 
0270     /** Packets pending to be processed */
0271     atomic_t pkts_pending;
0272 
0273     /** Number of  descriptors in this ring. */
0274     u32 max_count;
0275 
0276     /** The number of descriptors pending refill. */
0277     u32 refill_count;
0278 
0279     u32 pkts_per_intr;
0280     u32 refill_threshold;
0281 
0282     /** The max number of descriptors in DROQ without a buffer.
0283      * This field is used to keep track of empty space threshold. If the
0284      * refill_count reaches this value, the DROQ cannot accept a max-sized
0285      * (64K) packet.
0286      */
0287     u32 max_empty_descs;
0288 
0289     /** The receive buffer list. This list has the virtual addresses of the
0290      * buffers.
0291      */
0292     struct octeon_recv_buffer *recv_buf_list;
0293 
0294     /** The size of each buffer pointed by the buffer pointer. */
0295     u32 buffer_size;
0296 
0297     /** Pointer to the mapped packet credit register.
0298      * Host writes number of info/buffer ptrs available to this register
0299      */
0300     void  __iomem *pkts_credit_reg;
0301 
0302     /** Pointer to the mapped packet sent register.
0303      * Octeon writes the number of packets DMA'ed to host memory
0304      * in this register.
0305      */
0306     void __iomem *pkts_sent_reg;
0307 
0308     struct list_head dispatch_list;
0309 
0310     /** Statistics for this DROQ. */
0311     struct oct_droq_stats stats;
0312 
0313     /** DMA mapped address of the DROQ descriptor ring. */
0314     size_t desc_ring_dma;
0315 
0316     /** application context */
0317     void *app_ctx;
0318 
0319     struct napi_struct napi;
0320 
0321     u32 cpu_id;
0322 
0323     call_single_data_t csd;
0324 };
0325 
0326 #define OCT_DROQ_SIZE   (sizeof(struct octeon_droq))
0327 
0328 /**
0329  *  Allocates space for the descriptor ring for the droq and sets the
0330  *   base addr, num desc etc in Octeon registers.
0331  *
0332  * @param  oct_dev    - pointer to the octeon device structure
0333  * @param  q_no       - droq no. ranges from 0 - 3.
0334  * @param app_ctx     - pointer to application context
0335  * @return Success: 0    Failure: 1
0336  */
0337 int octeon_init_droq(struct octeon_device *oct_dev,
0338              u32 q_no,
0339              u32 num_descs,
0340              u32 desc_size,
0341              void *app_ctx);
0342 
0343 /**
0344  *  Frees the space for descriptor ring for the droq.
0345  *
0346  *  @param oct_dev - pointer to the octeon device structure
0347  *  @param q_no    - droq no. ranges from 0 - 3.
0348  *  @return:    Success: 0    Failure: 1
0349  */
0350 int octeon_delete_droq(struct octeon_device *oct_dev, u32 q_no);
0351 
0352 /** Register a change in droq operations. The ops field has a pointer to a
0353  * function which will called by the DROQ handler for all packets arriving
0354  * on output queues given by q_no irrespective of the type of packet.
0355  * The ops field also has a flag which if set tells the DROQ handler to
0356  * drop packets if it receives more than what it can process in one
0357  * invocation of the handler.
0358  * @param oct       - octeon device
0359  * @param q_no      - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
0360  * @param ops       - the droq_ops settings for this queue
0361  * @return          - 0 on success, -ENODEV or -EINVAL on error.
0362  */
0363 int
0364 octeon_register_droq_ops(struct octeon_device *oct,
0365              u32 q_no,
0366              struct octeon_droq_ops *ops);
0367 
0368 /** Resets the function pointer and flag settings made by
0369  * octeon_register_droq_ops(). After this routine is called, the DROQ handler
0370  * will lookup dispatch function for each arriving packet on the output queue
0371  * given by q_no.
0372  * @param oct       - octeon device
0373  * @param q_no      - octeon output queue number (0 <= q_no <= MAX_OCTEON_DROQ-1
0374  * @return          - 0 on success, -ENODEV or -EINVAL on error.
0375  */
0376 int octeon_unregister_droq_ops(struct octeon_device *oct, u32 q_no);
0377 
0378 /**   Register a dispatch function for a opcode/subcode. The driver will call
0379  *    this dispatch function when it receives a packet with the given
0380  *    opcode/subcode in its output queues along with the user specified
0381  *    argument.
0382  *    @param  oct        - the octeon device to register with.
0383  *    @param  opcode     - the opcode for which the dispatch will be registered.
0384  *    @param  subcode    - the subcode for which the dispatch will be registered
0385  *    @param  fn         - the dispatch function.
0386  *    @param  fn_arg     - user specified that will be passed along with the
0387  *                         dispatch function by the driver.
0388  *    @return Success: 0; Failure: 1
0389  */
0390 int octeon_register_dispatch_fn(struct octeon_device *oct,
0391                 u16 opcode,
0392                 u16 subcode,
0393                 octeon_dispatch_fn_t fn, void *fn_arg);
0394 
0395 void *octeon_get_dispatch_arg(struct octeon_device *oct,
0396                   u16 opcode, u16 subcode);
0397 
0398 void octeon_droq_print_stats(void);
0399 
0400 u32 octeon_droq_check_hw_for_pkts(struct octeon_droq *droq);
0401 
0402 int octeon_create_droq(struct octeon_device *oct, u32 q_no,
0403                u32 num_descs, u32 desc_size, void *app_ctx);
0404 
0405 int octeon_droq_process_packets(struct octeon_device *oct,
0406                 struct octeon_droq *droq,
0407                 u32 budget);
0408 
0409 int octeon_droq_process_poll_pkts(struct octeon_device *oct,
0410                   struct octeon_droq *droq, u32 budget);
0411 
0412 int octeon_enable_irq(struct octeon_device *oct, u32 q_no);
0413 
0414 int octeon_retry_droq_refill(struct octeon_droq *droq);
0415 
0416 #endif  /*__OCTEON_DROQ_H__ */