net/liquidio: initialize Rx queue
[dpdk.git] / drivers / net / liquidio / lio_rxtx.h
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 #ifndef _LIO_RXTX_H_
35 #define _LIO_RXTX_H_
36
37 #include <stdio.h>
38 #include <stdint.h>
39
40 #include <rte_spinlock.h>
41 #include <rte_memory.h>
42
43 #include "lio_struct.h"
44
45 #define LIO_STQUEUE_FIRST_ENTRY(ptr, type, elem)        \
46         (type *)((char *)((ptr)->stqh_first) - offsetof(type, elem))
47
48 #define lio_check_timeout(cur_time, chk_time) ((cur_time) > (chk_time))
49
50 #define lio_uptime              \
51         (size_t)(rte_get_timer_cycles() / rte_get_timer_hz())
52
53 /** Descriptor format.
54  *  The descriptor ring is made of descriptors which have 2 64-bit values:
55  *  -# Physical (bus) address of the data buffer.
56  *  -# Physical (bus) address of a lio_droq_info structure.
57  *  The device DMA's incoming packets and its information at the address
58  *  given by these descriptor fields.
59  */
60 struct lio_droq_desc {
61         /** The buffer pointer */
62         uint64_t buffer_ptr;
63
64         /** The Info pointer */
65         uint64_t info_ptr;
66 };
67
68 #define LIO_DROQ_DESC_SIZE      (sizeof(struct lio_droq_desc))
69
70 /** Information about packet DMA'ed by Octeon.
71  *  The format of the information available at Info Pointer after Octeon
72  *  has posted a packet. Not all descriptors have valid information. Only
73  *  the Info field of the first descriptor for a packet has information
74  *  about the packet.
75  */
76 struct lio_droq_info {
77         /** The Output Receive Header. */
78         union octeon_rh rh;
79
80         /** The Length of the packet. */
81         uint64_t length;
82 };
83
84 #define LIO_DROQ_INFO_SIZE      (sizeof(struct lio_droq_info))
85
86 /** Pointer to data buffer.
87  *  Driver keeps a pointer to the data buffer that it made available to
88  *  the Octeon device. Since the descriptor ring keeps physical (bus)
89  *  addresses, this field is required for the driver to keep track of
90  *  the virtual address pointers.
91  */
92 struct lio_recv_buffer {
93         /** Packet buffer, including meta data. */
94         void *buffer;
95
96         /** Data in the packet buffer. */
97         uint8_t *data;
98
99 };
100
101 #define LIO_DROQ_RECVBUF_SIZE   (sizeof(struct lio_recv_buffer))
102
103 #define LIO_DROQ_SIZE           (sizeof(struct lio_droq))
104
105 #define LIO_IQ_SEND_OK          0
106 #define LIO_IQ_SEND_STOP        1
107 #define LIO_IQ_SEND_FAILED      -1
108
109 /* conditions */
110 #define LIO_REQTYPE_NONE                0
111 #define LIO_REQTYPE_NORESP_NET          1
112 #define LIO_REQTYPE_NORESP_NET_SG       2
113 #define LIO_REQTYPE_SOFT_COMMAND        3
114
115 struct lio_request_list {
116         uint32_t reqtype;
117         void *buf;
118 };
119
120 /*----------------------  INSTRUCTION FORMAT ----------------------------*/
121
122 struct lio_instr3_64B {
123         /** Pointer where the input data is available. */
124         uint64_t dptr;
125
126         /** Instruction Header. */
127         uint64_t ih3;
128
129         /** Instruction Header. */
130         uint64_t pki_ih3;
131
132         /** Input Request Header. */
133         uint64_t irh;
134
135         /** opcode/subcode specific parameters */
136         uint64_t ossp[2];
137
138         /** Return Data Parameters */
139         uint64_t rdp;
140
141         /** Pointer where the response for a RAW mode packet will be written
142          *  by Octeon.
143          */
144         uint64_t rptr;
145
146 };
147
148 union lio_instr_64B {
149         struct lio_instr3_64B cmd3;
150 };
151
152 /** The size of each buffer in soft command buffer pool */
153 #define LIO_SOFT_COMMAND_BUFFER_SIZE    1536
154
155 /** Maximum number of buffers to allocate into soft command buffer pool */
156 #define LIO_MAX_SOFT_COMMAND_BUFFERS    255
157
158 struct lio_soft_command {
159         /** Soft command buffer info. */
160         struct lio_stailq_node node;
161         uint64_t dma_addr;
162         uint32_t size;
163
164         /** Command and return status */
165         union lio_instr_64B cmd;
166
167 #define LIO_COMPLETION_WORD_INIT        0xffffffffffffffffULL
168         uint64_t *status_word;
169
170         /** Data buffer info */
171         void *virtdptr;
172         uint64_t dmadptr;
173         uint32_t datasize;
174
175         /** Return buffer info */
176         void *virtrptr;
177         uint64_t dmarptr;
178         uint32_t rdatasize;
179
180         /** Context buffer info */
181         void *ctxptr;
182         uint32_t ctxsize;
183
184         /** Time out and callback */
185         size_t wait_time;
186         size_t timeout;
187         uint32_t iq_no;
188         void (*callback)(uint32_t, void *);
189         void *callback_arg;
190         struct rte_mbuf *mbuf;
191 };
192
193 struct lio_iq_post_status {
194         int status;
195         int index;
196 };
197
198 /*   wqe
199  *  ---------------  0
200  * |  wqe  word0-3 |
201  *  ---------------  32
202  * |    PCI IH     |
203  *  ---------------  40
204  * |     RPTR      |
205  *  ---------------  48
206  * |    PCI IRH    |
207  *  ---------------  56
208  * |    OCTEON_CMD |
209  *  ---------------  64
210  * | Addtl 8-BData |
211  * |               |
212  *  ---------------
213  */
214
215 union octeon_cmd {
216         uint64_t cmd64;
217
218         struct  {
219 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
220                 uint64_t cmd : 5;
221
222                 uint64_t more : 6; /* How many udd words follow the command */
223
224                 uint64_t reserved : 29;
225
226                 uint64_t param1 : 16;
227
228                 uint64_t param2 : 8;
229
230 #elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
231
232                 uint64_t param2 : 8;
233
234                 uint64_t param1 : 16;
235
236                 uint64_t reserved : 29;
237
238                 uint64_t more : 6;
239
240                 uint64_t cmd : 5;
241
242 #endif
243         } s;
244 };
245
246 #define OCTEON_CMD_SIZE (sizeof(union octeon_cmd))
247
248 /* Instruction Header */
249 struct octeon_instr_ih3 {
250 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
251
252         /** Reserved3 */
253         uint64_t reserved3 : 1;
254
255         /** Gather indicator 1=gather*/
256         uint64_t gather : 1;
257
258         /** Data length OR no. of entries in gather list */
259         uint64_t dlengsz : 14;
260
261         /** Front Data size */
262         uint64_t fsz : 6;
263
264         /** Reserved2 */
265         uint64_t reserved2 : 4;
266
267         /** PKI port kind - PKIND */
268         uint64_t pkind : 6;
269
270         /** Reserved1 */
271         uint64_t reserved1 : 32;
272
273 #elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
274         /** Reserved1 */
275         uint64_t reserved1 : 32;
276
277         /** PKI port kind - PKIND */
278         uint64_t pkind : 6;
279
280         /** Reserved2 */
281         uint64_t reserved2 : 4;
282
283         /** Front Data size */
284         uint64_t fsz : 6;
285
286         /** Data length OR no. of entries in gather list */
287         uint64_t dlengsz : 14;
288
289         /** Gather indicator 1=gather*/
290         uint64_t gather : 1;
291
292         /** Reserved3 */
293         uint64_t reserved3 : 1;
294
295 #endif
296 };
297
298 /* PKI Instruction Header(PKI IH) */
299 struct octeon_instr_pki_ih3 {
300 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
301
302         /** Wider bit */
303         uint64_t w : 1;
304
305         /** Raw mode indicator 1 = RAW */
306         uint64_t raw : 1;
307
308         /** Use Tag */
309         uint64_t utag : 1;
310
311         /** Use QPG */
312         uint64_t uqpg : 1;
313
314         /** Reserved2 */
315         uint64_t reserved2 : 1;
316
317         /** Parse Mode */
318         uint64_t pm : 3;
319
320         /** Skip Length */
321         uint64_t sl : 8;
322
323         /** Use Tag Type */
324         uint64_t utt : 1;
325
326         /** Tag type */
327         uint64_t tagtype : 2;
328
329         /** Reserved1 */
330         uint64_t reserved1 : 2;
331
332         /** QPG Value */
333         uint64_t qpg : 11;
334
335         /** Tag Value */
336         uint64_t tag : 32;
337
338 #elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
339
340         /** Tag Value */
341         uint64_t tag : 32;
342
343         /** QPG Value */
344         uint64_t qpg : 11;
345
346         /** Reserved1 */
347         uint64_t reserved1 : 2;
348
349         /** Tag type */
350         uint64_t tagtype : 2;
351
352         /** Use Tag Type */
353         uint64_t utt : 1;
354
355         /** Skip Length */
356         uint64_t sl : 8;
357
358         /** Parse Mode */
359         uint64_t pm : 3;
360
361         /** Reserved2 */
362         uint64_t reserved2 : 1;
363
364         /** Use QPG */
365         uint64_t uqpg : 1;
366
367         /** Use Tag */
368         uint64_t utag : 1;
369
370         /** Raw mode indicator 1 = RAW */
371         uint64_t raw : 1;
372
373         /** Wider bit */
374         uint64_t w : 1;
375 #endif
376 };
377
378 /** Input Request Header */
379 struct octeon_instr_irh {
380 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
381         uint64_t opcode : 4;
382         uint64_t rflag : 1;
383         uint64_t subcode : 7;
384         uint64_t vlan : 12;
385         uint64_t priority : 3;
386         uint64_t reserved : 5;
387         uint64_t ossp : 32; /* opcode/subcode specific parameters */
388 #elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
389         uint64_t ossp : 32; /* opcode/subcode specific parameters */
390         uint64_t reserved : 5;
391         uint64_t priority : 3;
392         uint64_t vlan : 12;
393         uint64_t subcode : 7;
394         uint64_t rflag : 1;
395         uint64_t opcode : 4;
396 #endif
397 };
398
399 /* pkiih3 + irh + ossp[0] + ossp[1] + rdp + rptr = 40 bytes */
400 #define OCTEON_SOFT_CMD_RESP_IH3        (40 + 8)
401 /* pki_h3 + irh + ossp[0] + ossp[1] = 32 bytes */
402 #define OCTEON_PCI_CMD_O3               (24 + 8)
403
404 /** Return Data Parameters */
405 struct octeon_instr_rdp {
406 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
407         uint64_t reserved : 49;
408         uint64_t pcie_port : 3;
409         uint64_t rlen : 12;
410 #elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
411         uint64_t rlen : 12;
412         uint64_t pcie_port : 3;
413         uint64_t reserved : 49;
414 #endif
415 };
416
417 int lio_setup_sc_buffer_pool(struct lio_device *lio_dev);
418 void lio_free_sc_buffer_pool(struct lio_device *lio_dev);
419
420 struct lio_soft_command *
421 lio_alloc_soft_command(struct lio_device *lio_dev,
422                        uint32_t datasize, uint32_t rdatasize,
423                        uint32_t ctxsize);
424 void lio_prepare_soft_command(struct lio_device *lio_dev,
425                               struct lio_soft_command *sc,
426                               uint8_t opcode, uint8_t subcode,
427                               uint32_t irh_ossp, uint64_t ossp0,
428                               uint64_t ossp1);
429 int lio_send_soft_command(struct lio_device *lio_dev,
430                           struct lio_soft_command *sc);
431 void lio_free_soft_command(struct lio_soft_command *sc);
432
433 /** Maximum ordered requests to process in every invocation of
434  *  lio_process_ordered_list(). The function will continue to process requests
435  *  as long as it can find one that has finished processing. If it keeps
436  *  finding requests that have completed, the function can run for ever. The
437  *  value defined here sets an upper limit on the number of requests it can
438  *  process before it returns control to the poll thread.
439  */
440 #define LIO_MAX_ORD_REQS_TO_PROCESS     4096
441
442 /** Error codes used in Octeon Host-Core communication.
443  *
444  *   31         16 15           0
445  *   ----------------------------
446  * |            |               |
447  *   ----------------------------
448  *   Error codes are 32-bit wide. The upper 16-bits, called Major Error Number,
449  *   are reserved to identify the group to which the error code belongs. The
450  *   lower 16-bits, called Minor Error Number, carry the actual code.
451  *
452  *   So error codes are (MAJOR NUMBER << 16)| MINOR_NUMBER.
453  */
454 /** Status for a request.
455  *  If the request is successfully queued, the driver will return
456  *  a LIO_REQUEST_PENDING status. LIO_REQUEST_TIMEOUT is only returned by
457  *  the driver if the response for request failed to arrive before a
458  *  time-out period or if the request processing * got interrupted due to
459  *  a signal respectively.
460  */
461 enum {
462         /** A value of 0x00000000 indicates no error i.e. success */
463         LIO_REQUEST_DONE        = 0x00000000,
464         /** (Major number: 0x0000; Minor Number: 0x0001) */
465         LIO_REQUEST_PENDING     = 0x00000001,
466         LIO_REQUEST_TIMEOUT     = 0x00000003,
467
468 };
469
470 /*------ Error codes used by firmware (bits 15..0 set by firmware */
471 #define LIO_FIRMWARE_MAJOR_ERROR_CODE    0x0001
472 #define LIO_FIRMWARE_STATUS_CODE(status) \
473         ((LIO_FIRMWARE_MAJOR_ERROR_CODE << 16) | (status))
474
475 /** Initialize the response lists. The number of response lists to create is
476  *  given by count.
477  *  @param lio_dev - the lio device structure.
478  */
479 void lio_setup_response_list(struct lio_device *lio_dev);
480
481 /** Check the status of first entry in the ordered list. If the instruction at
482  *  that entry finished processing or has timed-out, the entry is cleaned.
483  *  @param lio_dev - the lio device structure.
484  *  @return 1 if the ordered list is empty, 0 otherwise.
485  */
486 int lio_process_ordered_list(struct lio_device *lio_dev);
487
488 static inline void
489 lio_swap_8B_data(uint64_t *data, uint32_t blocks)
490 {
491         while (blocks) {
492                 *data = rte_cpu_to_be_64(*data);
493                 blocks--;
494                 data++;
495         }
496 }
497
498 static inline uint64_t
499 lio_map_ring(void *buf)
500 {
501         phys_addr_t dma_addr;
502
503         dma_addr = rte_mbuf_data_dma_addr_default(((struct rte_mbuf *)buf));
504
505         return (uint64_t)dma_addr;
506 }
507
508 static inline uint64_t
509 lio_map_ring_info(struct lio_droq *droq, uint32_t i)
510 {
511         phys_addr_t dma_addr;
512
513         dma_addr = droq->info_list_dma + (i * LIO_DROQ_INFO_SIZE);
514
515         return (uint64_t)dma_addr;
516 }
517
518 /* Macro to increment index.
519  * Index is incremented by count; if the sum exceeds
520  * max, index is wrapped-around to the start.
521  */
522 static inline uint32_t
523 lio_incr_index(uint32_t index, uint32_t count, uint32_t max)
524 {
525         if ((index + count) >= max)
526                 index = index + count - max;
527         else
528                 index += count;
529
530         return index;
531 }
532
533 int lio_setup_droq(struct lio_device *lio_dev, int q_no, int num_descs,
534                    int desc_size, struct rte_mempool *mpool,
535                    unsigned int socket_id);
536
537 /** Setup instruction queue zero for the device
538  *  @param lio_dev which lio device to setup
539  *
540  *  @return 0 if success. -1 if fails
541  */
542 int lio_setup_instr_queue0(struct lio_device *lio_dev);
543 void lio_free_instr_queue0(struct lio_device *lio_dev);
544 #endif  /* _LIO_RXTX_H_ */