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