blob: ed742e3f95622ddda3c7f1a1cc08b2c8cef536b9 [file] [log] [blame]
Chris Metcalf4875f692012-04-06 16:38:03 -04001/*
2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15#ifndef _GXIO_MPIPE_H_
16#define _GXIO_MPIPE_H_
17
18/*
19 *
20 * An API for allocating, configuring, and manipulating mPIPE hardware
21 * resources.
22 */
23
David Howellsa1ce3922012-10-02 18:01:25 +010024#include <gxio/common.h>
25#include <gxio/dma_queue.h>
Chris Metcalf4875f692012-04-06 16:38:03 -040026
27#include <linux/time.h>
28
29#include <arch/mpipe_def.h>
30#include <arch/mpipe_shm.h>
31
32#include <hv/drv_mpipe_intf.h>
33#include <hv/iorpc.h>
34
35/*
36 *
37 * The TILE-Gx mPIPE&tm; shim provides Ethernet connectivity, packet
38 * classification, and packet load balancing services. The
39 * gxio_mpipe_ API, declared in <gxio/mpipe.h>, allows applications to
40 * allocate mPIPE IO channels, configure packet distribution
41 * parameters, and send and receive Ethernet packets. The API is
42 * designed to be a minimal wrapper around the mPIPE hardware, making
43 * system calls only where necessary to preserve inter-process
44 * protection guarantees.
45 *
46 * The APIs described below allow the programmer to allocate and
47 * configure mPIPE resources. As described below, the mPIPE is a
48 * single shared hardware device that provides partitionable resources
49 * that are shared between all applications in the system. The
50 * gxio_mpipe_ API allows userspace code to make resource request
51 * calls to the hypervisor, which in turns keeps track of the
52 * resources in use by all applications, maintains protection
53 * guarantees, and resets resources upon application shutdown.
54 *
55 * We strongly recommend reading the mPIPE section of the IO Device
56 * Guide (UG404) before working with this API. Most functions in the
57 * gxio_mpipe_ API are directly analogous to hardware interfaces and
58 * the documentation assumes that the reader understands those
59 * hardware interfaces.
60 *
61 * @section mpipe__ingress mPIPE Ingress Hardware Resources
62 *
63 * The mPIPE ingress hardware provides extensive hardware offload for
64 * tasks like packet header parsing, load balancing, and memory
65 * management. This section provides a brief introduction to the
66 * hardware components and the gxio_mpipe_ calls used to manage them;
67 * see the IO Device Guide for a much more detailed description of the
68 * mPIPE's capabilities.
69 *
70 * When a packet arrives at one of the mPIPE's Ethernet MACs, it is
71 * assigned a channel number indicating which MAC received it. It
72 * then proceeds through the following hardware pipeline:
73 *
74 * @subsection mpipe__classification Classification
75 *
76 * A set of classification processors run header parsing code on each
77 * incoming packet, extracting information including the destination
78 * MAC address, VLAN, Ethernet type, and five-tuple hash. Some of
79 * this information is then used to choose which buffer stack will be
80 * used to hold the packet, and which bucket will be used by the load
81 * balancer to determine which application will receive the packet.
82 *
83 * The rules by which the buffer stack and bucket are chosen can be
84 * configured via the @ref gxio_mpipe_classifier API. A given app can
85 * specify multiple rules, each one specifying a bucket range, and a
86 * set of buffer stacks, to be used for packets matching the rule.
87 * Each rule can optionally specify a restricted set of channels,
88 * VLANs, and/or dMACs, in which it is interested. By default, a
89 * given rule starts out matching all channels associated with the
90 * mPIPE context's set of open links; all VLANs; and all dMACs.
91 * Subsequent restrictions can then be added.
92 *
93 * @subsection mpipe__load_balancing Load Balancing
94 *
95 * The mPIPE load balancer is responsible for choosing the NotifRing
96 * to which the packet will be delivered. This decision is based on
97 * the bucket number indicated by the classification program. In
98 * general, the bucket number is based on some number of low bits of
99 * the packet's flow hash (applications that aren't interested in flow
100 * hashing use a single bucket). Each load balancer bucket keeps a
101 * record of the NotifRing to which packets directed to that bucket
102 * are currently being delivered. Based on the bucket's load
103 * balancing mode (@ref gxio_mpipe_bucket_mode_t), the load balancer
104 * either forwards the packet to the previously assigned NotifRing or
105 * decides to choose a new NotifRing. If a new NotifRing is required,
106 * the load balancer chooses the least loaded ring in the NotifGroup
107 * associated with the bucket.
108 *
109 * The load balancer is a shared resource. Each application needs to
110 * explicitly allocate NotifRings, NotifGroups, and buckets, using
111 * gxio_mpipe_alloc_notif_rings(), gxio_mpipe_alloc_notif_groups(),
112 * and gxio_mpipe_alloc_buckets(). Then the application needs to
113 * configure them using gxio_mpipe_init_notif_ring() and
114 * gxio_mpipe_init_notif_group_and_buckets().
115 *
116 * @subsection mpipe__buffers Buffer Selection and Packet Delivery
117 *
118 * Once the load balancer has chosen the destination NotifRing, the
119 * mPIPE DMA engine pops at least one buffer off of the 'buffer stack'
120 * chosen by the classification program and DMAs the packet data into
121 * that buffer. Each buffer stack provides a hardware-accelerated
122 * stack of data buffers with the same size. If the packet data is
123 * larger than the buffers provided by the chosen buffer stack, the
124 * mPIPE hardware pops off multiple buffers and chains the packet data
125 * through a multi-buffer linked list. Once the packet data is
126 * delivered to the buffer(s), the mPIPE hardware writes the
127 * ::gxio_mpipe_idesc_t metadata object (calculated by the classifier)
128 * into the NotifRing and increments the number of packets delivered
129 * to that ring.
130 *
131 * Applications can push buffers onto a buffer stack by calling
132 * gxio_mpipe_push_buffer() or by egressing a packet with the
133 * ::gxio_mpipe_edesc_t::hwb bit set, indicating that the egressed
134 * buffers should be returned to the stack.
135 *
136 * Applications can allocate and initialize buffer stacks with the
137 * gxio_mpipe_alloc_buffer_stacks() and gxio_mpipe_init_buffer_stack()
138 * APIs.
139 *
140 * The application must also register the memory pages that will hold
141 * packets. This requires calling gxio_mpipe_register_page() for each
142 * memory page that will hold packets allocated by the application for
143 * a given buffer stack. Since each buffer stack is limited to 16
144 * registered pages, it may be necessary to use huge pages, or even
145 * extremely huge pages, to hold all the buffers.
146 *
147 * @subsection mpipe__iqueue NotifRings
148 *
149 * Each NotifRing is a region of shared memory, allocated by the
150 * application, to which the mPIPE delivers packet descriptors
151 * (::gxio_mpipe_idesc_t). The application can allocate them via
152 * gxio_mpipe_alloc_notif_rings(). The application can then either
153 * explicitly initialize them with gxio_mpipe_init_notif_ring() and
154 * then read from them manually, or can make use of the convenience
155 * wrappers provided by @ref gxio_mpipe_wrappers.
156 *
157 * @section mpipe__egress mPIPE Egress Hardware
158 *
159 * Applications use eDMA rings to queue packets for egress. The
160 * application can allocate them via gxio_mpipe_alloc_edma_rings().
161 * The application can then either explicitly initialize them with
162 * gxio_mpipe_init_edma_ring() and then write to them manually, or
163 * can make use of the convenience wrappers provided by
164 * @ref gxio_mpipe_wrappers.
165 *
166 * @section gxio__shortcomings Plans for Future API Revisions
167 *
168 * The API defined here is only an initial version of the mPIPE API.
169 * Future plans include:
170 *
171 * - Higher level wrapper functions to provide common initialization
172 * patterns. This should help users start writing mPIPE programs
173 * without having to learn the details of the hardware.
174 *
175 * - Support for reset and deallocation of resources, including
176 * cleanup upon application shutdown.
177 *
178 * - Support for calling these APIs in the BME.
179 *
180 * - Support for IO interrupts.
181 *
182 * - Clearer definitions of thread safety guarantees.
183 *
184 * @section gxio__mpipe_examples Examples
185 *
186 * See the following mPIPE example programs for more information about
187 * allocating mPIPE resources and using them in real applications:
188 *
189 * - @ref mpipe/ingress/app.c : Receiving packets.
190 *
191 * - @ref mpipe/forward/app.c : Forwarding packets.
192 *
193 * Note that there are several more examples.
194 */
195
196/* Flags that can be passed to resource allocation functions. */
197enum gxio_mpipe_alloc_flags_e {
198 /* Require an allocation to start at a specified resource index. */
199 GXIO_MPIPE_ALLOC_FIXED = HV_MPIPE_ALLOC_FIXED,
200};
201
202/* Flags that can be passed to memory registration functions. */
203enum gxio_mpipe_mem_flags_e {
204 /* Do not fill L3 when writing, and invalidate lines upon egress. */
205 GXIO_MPIPE_MEM_FLAG_NT_HINT = IORPC_MEM_BUFFER_FLAG_NT_HINT,
206
207 /* L3 cache fills should only populate IO cache ways. */
208 GXIO_MPIPE_MEM_FLAG_IO_PIN = IORPC_MEM_BUFFER_FLAG_IO_PIN,
209};
210
211/* An ingress packet descriptor. When a packet arrives, the mPIPE
212 * hardware generates this structure and writes it into a NotifRing.
213 */
214typedef MPIPE_PDESC_t gxio_mpipe_idesc_t;
215
216/* An egress command descriptor. Applications write this structure
217 * into eDMA rings and the hardware performs the indicated operation
218 * (normally involving egressing some bytes). Note that egressing a
219 * single packet may involve multiple egress command descriptors.
220 */
221typedef MPIPE_EDMA_DESC_t gxio_mpipe_edesc_t;
222
223/* Get the "va" field from an "idesc".
224 *
225 * This is the address at which the ingress hardware copied the first
226 * byte of the packet.
227 *
228 * If the classifier detected a custom header, then this will point to
229 * the custom header, and gxio_mpipe_idesc_get_l2_start() will point
230 * to the actual L2 header.
231 *
232 * Note that this value may be misleading if "idesc->be" is set.
233 *
234 * @param idesc An ingress packet descriptor.
235 */
236static inline unsigned char *gxio_mpipe_idesc_get_va(gxio_mpipe_idesc_t *idesc)
237{
238 return (unsigned char *)(long)idesc->va;
239}
240
241/* Get the "xfer_size" from an "idesc".
242 *
243 * This is the actual number of packet bytes transferred into memory
244 * by the hardware.
245 *
246 * Note that this value may be misleading if "idesc->be" is set.
247 *
248 * @param idesc An ingress packet descriptor.
249 *
250 * ISSUE: Is this the best name for this?
251 * FIXME: Add more docs about chaining, clipping, etc.
252 */
253static inline unsigned int gxio_mpipe_idesc_get_xfer_size(gxio_mpipe_idesc_t
254 *idesc)
255{
256 return idesc->l2_size;
257}
258
259/* Get the "l2_offset" from an "idesc".
260 *
261 * Extremely customized classifiers might not support this function.
262 *
263 * This is the number of bytes between the "va" and the L2 header.
264 *
265 * The L2 header consists of a destination mac address, a source mac
266 * address, and an initial ethertype. Various initial ethertypes
267 * allow encoding extra information in the L2 header, often including
268 * a vlan, and/or a new ethertype.
269 *
270 * Note that the "l2_offset" will be non-zero if (and only if) the
271 * classifier processed a custom header for the packet.
272 *
273 * @param idesc An ingress packet descriptor.
274 */
275static inline uint8_t gxio_mpipe_idesc_get_l2_offset(gxio_mpipe_idesc_t *idesc)
276{
277 return (idesc->custom1 >> 32) & 0xFF;
278}
279
280/* Get the "l2_start" from an "idesc".
281 *
282 * This is simply gxio_mpipe_idesc_get_va() plus
283 * gxio_mpipe_idesc_get_l2_offset().
284 *
285 * @param idesc An ingress packet descriptor.
286 */
287static inline unsigned char *gxio_mpipe_idesc_get_l2_start(gxio_mpipe_idesc_t
288 *idesc)
289{
290 unsigned char *va = gxio_mpipe_idesc_get_va(idesc);
291 return va + gxio_mpipe_idesc_get_l2_offset(idesc);
292}
293
294/* Get the "l2_length" from an "idesc".
295 *
296 * This is simply gxio_mpipe_idesc_get_xfer_size() minus
297 * gxio_mpipe_idesc_get_l2_offset().
298 *
299 * @param idesc An ingress packet descriptor.
300 */
301static inline unsigned int gxio_mpipe_idesc_get_l2_length(gxio_mpipe_idesc_t
302 *idesc)
303{
304 unsigned int xfer_size = idesc->l2_size;
305 return xfer_size - gxio_mpipe_idesc_get_l2_offset(idesc);
306}
307
308/* A context object used to manage mPIPE hardware resources. */
309typedef struct {
310
311 /* File descriptor for calling up to Linux (and thus the HV). */
312 int fd;
313
314 /* The VA at which configuration registers are mapped. */
315 char *mmio_cfg_base;
316
317 /* The VA at which IDMA, EDMA, and buffer manager are mapped. */
318 char *mmio_fast_base;
319
320 /* The "initialized" buffer stacks. */
321 gxio_mpipe_rules_stacks_t __stacks;
322
323} gxio_mpipe_context_t;
324
325/* This is only used internally, but it's most easily made visible here. */
326typedef gxio_mpipe_context_t gxio_mpipe_info_context_t;
327
328/* Initialize an mPIPE context.
329 *
330 * This function allocates an mPIPE "service domain" and maps the MMIO
331 * registers into the caller's VA space.
332 *
333 * @param context Context object to be initialized.
334 * @param mpipe_instance Instance number of mPIPE shim to be controlled via
335 * context.
336 */
337extern int gxio_mpipe_init(gxio_mpipe_context_t *context,
338 unsigned int mpipe_instance);
339
340/* Destroy an mPIPE context.
341 *
342 * This function frees the mPIPE "service domain" and unmaps the MMIO
343 * registers from the caller's VA space.
344 *
345 * If a user process exits without calling this routine, the kernel
346 * will destroy the mPIPE context as part of process teardown.
347 *
348 * @param context Context object to be destroyed.
349 */
350extern int gxio_mpipe_destroy(gxio_mpipe_context_t *context);
351
352/*****************************************************************
353 * Buffer Stacks *
354 ******************************************************************/
355
356/* Allocate a set of buffer stacks.
357 *
358 * The return value is NOT interesting if count is zero.
359 *
360 * @param context An initialized mPIPE context.
361 * @param count Number of stacks required.
362 * @param first Index of first stack if ::GXIO_MPIPE_ALLOC_FIXED flag is set,
363 * otherwise ignored.
364 * @param flags Flag bits from ::gxio_mpipe_alloc_flags_e.
365 * @return Index of first allocated buffer stack, or
366 * ::GXIO_MPIPE_ERR_NO_BUFFER_STACK if allocation failed.
367 */
368extern int gxio_mpipe_alloc_buffer_stacks(gxio_mpipe_context_t *context,
369 unsigned int count,
370 unsigned int first,
371 unsigned int flags);
372
373/* Enum codes for buffer sizes supported by mPIPE. */
374typedef enum {
375 /* 128 byte packet data buffer. */
376 GXIO_MPIPE_BUFFER_SIZE_128 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_128,
377 /* 256 byte packet data buffer. */
378 GXIO_MPIPE_BUFFER_SIZE_256 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_256,
379 /* 512 byte packet data buffer. */
380 GXIO_MPIPE_BUFFER_SIZE_512 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_512,
381 /* 1024 byte packet data buffer. */
382 GXIO_MPIPE_BUFFER_SIZE_1024 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_1024,
383 /* 1664 byte packet data buffer. */
384 GXIO_MPIPE_BUFFER_SIZE_1664 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_1664,
385 /* 4096 byte packet data buffer. */
386 GXIO_MPIPE_BUFFER_SIZE_4096 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_4096,
387 /* 10368 byte packet data buffer. */
388 GXIO_MPIPE_BUFFER_SIZE_10368 =
389 MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_10368,
390 /* 16384 byte packet data buffer. */
391 GXIO_MPIPE_BUFFER_SIZE_16384 = MPIPE_BSM_INIT_DAT_1__SIZE_VAL_BSZ_16384
392} gxio_mpipe_buffer_size_enum_t;
393
394/* Convert a buffer size in bytes into a buffer size enum. */
395extern gxio_mpipe_buffer_size_enum_t
396gxio_mpipe_buffer_size_to_buffer_size_enum(size_t size);
397
398/* Convert a buffer size enum into a buffer size in bytes. */
399extern size_t
400gxio_mpipe_buffer_size_enum_to_buffer_size(gxio_mpipe_buffer_size_enum_t
401 buffer_size_enum);
402
403/* Calculate the number of bytes required to store a given number of
404 * buffers in the memory registered with a buffer stack via
405 * gxio_mpipe_init_buffer_stack().
406 */
407extern size_t gxio_mpipe_calc_buffer_stack_bytes(unsigned long buffers);
408
409/* Initialize a buffer stack. This function binds a region of memory
410 * to be used by the hardware for storing buffer addresses pushed via
411 * gxio_mpipe_push_buffer() or as the result of sending a buffer out
412 * the egress with the 'push to stack when done' bit set. Once this
413 * function returns, the memory region's contents may be arbitrarily
414 * modified by the hardware at any time and software should not access
415 * the memory region again.
416 *
417 * @param context An initialized mPIPE context.
418 * @param stack The buffer stack index.
419 * @param buffer_size_enum The size of each buffer in the buffer stack,
420 * as an enum.
421 * @param mem The address of the buffer stack. This memory must be
422 * physically contiguous and aligned to a 64kB boundary.
423 * @param mem_size The size of the buffer stack, in bytes.
424 * @param mem_flags ::gxio_mpipe_mem_flags_e memory flags.
425 * @return Zero on success, ::GXIO_MPIPE_ERR_INVAL_BUFFER_SIZE if
426 * buffer_size_enum is invalid, ::GXIO_MPIPE_ERR_BAD_BUFFER_STACK if
427 * stack has not been allocated.
428 */
429extern int gxio_mpipe_init_buffer_stack(gxio_mpipe_context_t *context,
430 unsigned int stack,
431 gxio_mpipe_buffer_size_enum_t
432 buffer_size_enum, void *mem,
433 size_t mem_size,
434 unsigned int mem_flags);
435
436/* Push a buffer onto a previously initialized buffer stack.
437 *
438 * The size of the buffer being pushed must match the size that was
439 * registered with gxio_mpipe_init_buffer_stack(). All packet buffer
440 * addresses are 128-byte aligned; the low 7 bits of the specified
441 * buffer address will be ignored.
442 *
443 * @param context An initialized mPIPE context.
444 * @param stack The buffer stack index.
445 * @param buffer The buffer (the low seven bits are ignored).
446 */
447static inline void gxio_mpipe_push_buffer(gxio_mpipe_context_t *context,
448 unsigned int stack, void *buffer)
449{
450 MPIPE_BSM_REGION_ADDR_t offset = { {0} };
451 MPIPE_BSM_REGION_VAL_t val = { {0} };
452
453 /*
454 * The mmio_fast_base region starts at the IDMA region, so subtract
455 * off that initial offset.
456 */
457 offset.region =
458 MPIPE_MMIO_ADDR__REGION_VAL_BSM -
459 MPIPE_MMIO_ADDR__REGION_VAL_IDMA;
460 offset.stack = stack;
461
462#if __SIZEOF_POINTER__ == 4
463 val.va = ((ulong) buffer) >> MPIPE_BSM_REGION_VAL__VA_SHIFT;
464#else
465 val.va = ((long)buffer) >> MPIPE_BSM_REGION_VAL__VA_SHIFT;
466#endif
467
468 __gxio_mmio_write(context->mmio_fast_base + offset.word, val.word);
469}
470
471/* Pop a buffer off of a previously initialized buffer stack.
472 *
473 * @param context An initialized mPIPE context.
474 * @param stack The buffer stack index.
475 * @return The buffer, or NULL if the stack is empty.
476 */
477static inline void *gxio_mpipe_pop_buffer(gxio_mpipe_context_t *context,
478 unsigned int stack)
479{
480 MPIPE_BSM_REGION_ADDR_t offset = { {0} };
481
482 /*
483 * The mmio_fast_base region starts at the IDMA region, so subtract
484 * off that initial offset.
485 */
486 offset.region =
487 MPIPE_MMIO_ADDR__REGION_VAL_BSM -
488 MPIPE_MMIO_ADDR__REGION_VAL_IDMA;
489 offset.stack = stack;
490
491 while (1) {
492 /*
493 * Case 1: val.c == ..._UNCHAINED, va is non-zero.
494 * Case 2: val.c == ..._INVALID, va is zero.
495 * Case 3: val.c == ..._NOT_RDY, va is zero.
496 */
497 MPIPE_BSM_REGION_VAL_t val;
498 val.word =
499 __gxio_mmio_read(context->mmio_fast_base +
500 offset.word);
501
502 /*
503 * Handle case 1 and 2 by returning the buffer (or NULL).
504 * Handle case 3 by waiting for the prefetch buffer to refill.
505 */
506 if (val.c != MPIPE_EDMA_DESC_WORD1__C_VAL_NOT_RDY)
507 return (void *)((unsigned long)val.
508 va << MPIPE_BSM_REGION_VAL__VA_SHIFT);
509 }
510}
511
512/*****************************************************************
513 * NotifRings *
514 ******************************************************************/
515
516/* Allocate a set of NotifRings.
517 *
518 * The return value is NOT interesting if count is zero.
519 *
520 * Note that NotifRings are allocated in chunks, so allocating one at
521 * a time is much less efficient than allocating several at once.
522 *
523 * @param context An initialized mPIPE context.
524 * @param count Number of NotifRings required.
525 * @param first Index of first NotifRing if ::GXIO_MPIPE_ALLOC_FIXED flag
526 * is set, otherwise ignored.
527 * @param flags Flag bits from ::gxio_mpipe_alloc_flags_e.
528 * @return Index of first allocated buffer NotifRing, or
529 * ::GXIO_MPIPE_ERR_NO_NOTIF_RING if allocation failed.
530 */
531extern int gxio_mpipe_alloc_notif_rings(gxio_mpipe_context_t *context,
532 unsigned int count, unsigned int first,
533 unsigned int flags);
534
535/* Initialize a NotifRing, using the given memory and size.
536 *
537 * @param context An initialized mPIPE context.
538 * @param ring The NotifRing index.
539 * @param mem A physically contiguous region of memory to be filled
540 * with a ring of ::gxio_mpipe_idesc_t structures.
541 * @param mem_size Number of bytes in the ring. Must be 128, 512,
542 * 2048, or 65536 * sizeof(gxio_mpipe_idesc_t).
543 * @param mem_flags ::gxio_mpipe_mem_flags_e memory flags.
544 *
545 * @return 0 on success, ::GXIO_MPIPE_ERR_BAD_NOTIF_RING or
546 * ::GXIO_ERR_INVAL_MEMORY_SIZE on failure.
547 */
548extern int gxio_mpipe_init_notif_ring(gxio_mpipe_context_t *context,
549 unsigned int ring,
550 void *mem, size_t mem_size,
551 unsigned int mem_flags);
552
553/* Configure an interrupt to be sent to a tile on incoming NotifRing
554 * traffic. Once an interrupt is sent for a particular ring, no more
555 * will be sent until gxio_mica_enable_notif_ring_interrupt() is called.
556 *
557 * @param context An initialized mPIPE context.
558 * @param x X coordinate of interrupt target tile.
559 * @param y Y coordinate of interrupt target tile.
560 * @param i Index of the IPI register which will receive the interrupt.
561 * @param e Specific event which will be set in the target IPI register when
562 * the interrupt occurs.
563 * @param ring The NotifRing index.
564 * @return Zero on success, GXIO_ERR_INVAL if params are out of range.
565 */
566extern int gxio_mpipe_request_notif_ring_interrupt(gxio_mpipe_context_t
567 *context, int x, int y,
568 int i, int e,
569 unsigned int ring);
570
571/* Enable an interrupt on incoming NotifRing traffic.
572 *
573 * @param context An initialized mPIPE context.
574 * @param ring The NotifRing index.
575 * @return Zero on success, GXIO_ERR_INVAL if params are out of range.
576 */
577extern int gxio_mpipe_enable_notif_ring_interrupt(gxio_mpipe_context_t
578 *context, unsigned int ring);
579
580/* Map all of a client's memory via the given IOTLB.
581 * @param context An initialized mPIPE context.
582 * @param iotlb IOTLB index.
583 * @param pte Page table entry.
584 * @param flags Flags.
585 * @return Zero on success, or a negative error code.
586 */
587extern int gxio_mpipe_register_client_memory(gxio_mpipe_context_t *context,
588 unsigned int iotlb, HV_PTE pte,
589 unsigned int flags);
590
591/*****************************************************************
592 * Notif Groups *
593 ******************************************************************/
594
595/* Allocate a set of NotifGroups.
596 *
597 * The return value is NOT interesting if count is zero.
598 *
599 * @param context An initialized mPIPE context.
600 * @param count Number of NotifGroups required.
601 * @param first Index of first NotifGroup if ::GXIO_MPIPE_ALLOC_FIXED flag
602 * is set, otherwise ignored.
603 * @param flags Flag bits from ::gxio_mpipe_alloc_flags_e.
604 * @return Index of first allocated buffer NotifGroup, or
605 * ::GXIO_MPIPE_ERR_NO_NOTIF_GROUP if allocation failed.
606 */
607extern int gxio_mpipe_alloc_notif_groups(gxio_mpipe_context_t *context,
608 unsigned int count,
609 unsigned int first,
610 unsigned int flags);
611
612/* Add a NotifRing to a NotifGroup. This only sets a bit in the
613 * application's 'group' object; the hardware NotifGroup can be
614 * initialized by passing 'group' to gxio_mpipe_init_notif_group() or
615 * gxio_mpipe_init_notif_group_and_buckets().
616 */
617static inline void
618gxio_mpipe_notif_group_add_ring(gxio_mpipe_notif_group_bits_t *bits, int ring)
619{
620 bits->ring_mask[ring / 64] |= (1ull << (ring % 64));
621}
622
623/* Set a particular NotifGroup bitmask. Since the load balancer
624 * makes decisions based on both bucket and NotifGroup state, most
625 * applications should use gxio_mpipe_init_notif_group_and_buckets()
626 * rather than using this function to configure just a NotifGroup.
627 */
628extern int gxio_mpipe_init_notif_group(gxio_mpipe_context_t *context,
629 unsigned int group,
630 gxio_mpipe_notif_group_bits_t bits);
631
632/*****************************************************************
633 * Load Balancer *
634 ******************************************************************/
635
636/* Allocate a set of load balancer buckets.
637 *
638 * The return value is NOT interesting if count is zero.
639 *
640 * Note that buckets are allocated in chunks, so allocating one at
641 * a time is much less efficient than allocating several at once.
642 *
643 * Note that the buckets are actually divided into two sub-ranges, of
644 * different sizes, and different chunk sizes, and the range you get
645 * by default is determined by the size of the request. Allocations
646 * cannot span the two sub-ranges.
647 *
648 * @param context An initialized mPIPE context.
649 * @param count Number of buckets required.
650 * @param first Index of first bucket if ::GXIO_MPIPE_ALLOC_FIXED flag is set,
651 * otherwise ignored.
652 * @param flags Flag bits from ::gxio_mpipe_alloc_flags_e.
653 * @return Index of first allocated buffer bucket, or
654 * ::GXIO_MPIPE_ERR_NO_BUCKET if allocation failed.
655 */
656extern int gxio_mpipe_alloc_buckets(gxio_mpipe_context_t *context,
657 unsigned int count, unsigned int first,
658 unsigned int flags);
659
660/* The legal modes for gxio_mpipe_bucket_info_t and
661 * gxio_mpipe_init_notif_group_and_buckets().
662 *
663 * All modes except ::GXIO_MPIPE_BUCKET_ROUND_ROBIN expect that the user
664 * will allocate a power-of-two number of buckets and initialize them
665 * to the same mode. The classifier program then uses the appropriate
666 * number of low bits from the incoming packet's flow hash to choose a
667 * load balancer bucket. Based on that bucket's load balancing mode,
668 * reference count, and currently active NotifRing, the load balancer
669 * chooses the NotifRing to which the packet will be delivered.
670 */
671typedef enum {
672 /* All packets for a bucket go to the same NotifRing unless the
673 * NotifRing gets full, in which case packets will be dropped. If
674 * the bucket reference count ever reaches zero, a new NotifRing may
675 * be chosen.
676 */
677 GXIO_MPIPE_BUCKET_DYNAMIC_FLOW_AFFINITY =
678 MPIPE_LBL_INIT_DAT_BSTS_TBL__MODE_VAL_DFA,
679
680 /* All packets for a bucket always go to the same NotifRing.
681 */
682 GXIO_MPIPE_BUCKET_STATIC_FLOW_AFFINITY =
683 MPIPE_LBL_INIT_DAT_BSTS_TBL__MODE_VAL_FIXED,
684
685 /* All packets for a bucket go to the least full NotifRing in the
686 * group, providing load balancing round robin behavior.
687 */
688 GXIO_MPIPE_BUCKET_ROUND_ROBIN =
689 MPIPE_LBL_INIT_DAT_BSTS_TBL__MODE_VAL_ALWAYS_PICK,
690
691 /* All packets for a bucket go to the same NotifRing unless the
692 * NotifRing gets full, at which point the bucket starts using the
693 * least full NotifRing in the group. If all NotifRings in the
694 * group are full, packets will be dropped.
695 */
696 GXIO_MPIPE_BUCKET_STICKY_FLOW_LOCALITY =
697 MPIPE_LBL_INIT_DAT_BSTS_TBL__MODE_VAL_STICKY,
698
699 /* All packets for a bucket go to the same NotifRing unless the
700 * NotifRing gets full, or a random timer fires, at which point the
701 * bucket starts using the least full NotifRing in the group. If
702 * all NotifRings in the group are full, packets will be dropped.
703 * WARNING: This mode is BROKEN on chips with fewer than 64 tiles.
704 */
705 GXIO_MPIPE_BUCKET_PREFER_FLOW_LOCALITY =
706 MPIPE_LBL_INIT_DAT_BSTS_TBL__MODE_VAL_STICKY_RAND,
707
708} gxio_mpipe_bucket_mode_t;
709
710/* Copy a set of bucket initialization values into the mPIPE
711 * hardware. Since the load balancer makes decisions based on both
712 * bucket and NotifGroup state, most applications should use
713 * gxio_mpipe_init_notif_group_and_buckets() rather than using this
714 * function to configure a single bucket.
715 *
716 * @param context An initialized mPIPE context.
717 * @param bucket Bucket index to be initialized.
718 * @param bucket_info Initial reference count, NotifRing index, and mode.
719 * @return 0 on success, ::GXIO_MPIPE_ERR_BAD_BUCKET on failure.
720 */
721extern int gxio_mpipe_init_bucket(gxio_mpipe_context_t *context,
722 unsigned int bucket,
723 gxio_mpipe_bucket_info_t bucket_info);
724
725/* Initializes a group and range of buckets and range of rings such
726 * that the load balancer runs a particular load balancing function.
727 *
728 * First, the group is initialized with the given rings.
729 *
730 * Second, each bucket is initialized with the mode and group, and a
731 * ring chosen round-robin from the given rings.
732 *
733 * Normally, the classifier picks a bucket, and then the load balancer
734 * picks a ring, based on the bucket's mode, group, and current ring,
735 * possibly updating the bucket's ring.
736 *
737 * @param context An initialized mPIPE context.
738 * @param group The group.
739 * @param ring The first ring.
740 * @param num_rings The number of rings.
741 * @param bucket The first bucket.
742 * @param num_buckets The number of buckets.
743 * @param mode The load balancing mode.
744 *
745 * @return 0 on success, ::GXIO_MPIPE_ERR_BAD_BUCKET,
746 * ::GXIO_MPIPE_ERR_BAD_NOTIF_GROUP, or
747 * ::GXIO_MPIPE_ERR_BAD_NOTIF_RING on failure.
748 */
749extern int gxio_mpipe_init_notif_group_and_buckets(gxio_mpipe_context_t
750 *context,
751 unsigned int group,
752 unsigned int ring,
753 unsigned int num_rings,
754 unsigned int bucket,
755 unsigned int num_buckets,
756 gxio_mpipe_bucket_mode_t
757 mode);
758
759/* Return credits to a NotifRing and/or bucket.
760 *
761 * @param context An initialized mPIPE context.
762 * @param ring The NotifRing index, or -1.
763 * @param bucket The bucket, or -1.
764 * @param count The number of credits to return.
765 */
766static inline void gxio_mpipe_credit(gxio_mpipe_context_t *context,
767 int ring, int bucket, unsigned int count)
768{
769 /* NOTE: Fancy struct initialization would break "C89" header test. */
770
771 MPIPE_IDMA_RELEASE_REGION_ADDR_t offset = { {0} };
772 MPIPE_IDMA_RELEASE_REGION_VAL_t val = { {0} };
773
774 /*
775 * The mmio_fast_base region starts at the IDMA region, so subtract
776 * off that initial offset.
777 */
778 offset.region =
779 MPIPE_MMIO_ADDR__REGION_VAL_IDMA -
780 MPIPE_MMIO_ADDR__REGION_VAL_IDMA;
781 offset.ring = ring;
782 offset.bucket = bucket;
783 offset.ring_enable = (ring >= 0);
784 offset.bucket_enable = (bucket >= 0);
785 val.count = count;
786
787 __gxio_mmio_write(context->mmio_fast_base + offset.word, val.word);
788}
789
790/*****************************************************************
791 * Egress Rings *
792 ******************************************************************/
793
794/* Allocate a set of eDMA rings.
795 *
796 * The return value is NOT interesting if count is zero.
797 *
798 * @param context An initialized mPIPE context.
799 * @param count Number of eDMA rings required.
800 * @param first Index of first eDMA ring if ::GXIO_MPIPE_ALLOC_FIXED flag
801 * is set, otherwise ignored.
802 * @param flags Flag bits from ::gxio_mpipe_alloc_flags_e.
803 * @return Index of first allocated buffer eDMA ring, or
804 * ::GXIO_MPIPE_ERR_NO_EDMA_RING if allocation failed.
805 */
806extern int gxio_mpipe_alloc_edma_rings(gxio_mpipe_context_t *context,
807 unsigned int count, unsigned int first,
808 unsigned int flags);
809
810/* Initialize an eDMA ring, using the given memory and size.
811 *
812 * @param context An initialized mPIPE context.
Chris Metcalf2628e8a2013-08-01 11:36:42 -0400813 * @param ering The eDMA ring index.
Chris Metcalf4875f692012-04-06 16:38:03 -0400814 * @param channel The channel to use. This must be one of the channels
815 * associated with the context's set of open links.
816 * @param mem A physically contiguous region of memory to be filled
817 * with a ring of ::gxio_mpipe_edesc_t structures.
818 * @param mem_size Number of bytes in the ring. Must be 512, 2048,
819 * 8192 or 65536, times 16 (i.e. sizeof(gxio_mpipe_edesc_t)).
820 * @param mem_flags ::gxio_mpipe_mem_flags_e memory flags.
821 *
822 * @return 0 on success, ::GXIO_MPIPE_ERR_BAD_EDMA_RING or
823 * ::GXIO_ERR_INVAL_MEMORY_SIZE on failure.
824 */
825extern int gxio_mpipe_init_edma_ring(gxio_mpipe_context_t *context,
Chris Metcalf2628e8a2013-08-01 11:36:42 -0400826 unsigned int ering, unsigned int channel,
Chris Metcalf4875f692012-04-06 16:38:03 -0400827 void *mem, size_t mem_size,
828 unsigned int mem_flags);
829
Chris Metcalf2628e8a2013-08-01 11:36:42 -0400830/* Set the "max_blks", "min_snf_blks", and "db" fields of
831 * ::MPIPE_EDMA_RG_INIT_DAT_THRESH_t for a given edma ring.
832 *
833 * The global pool of dynamic blocks will be automatically adjusted.
834 *
835 * This function should not be called after any egress has been done
836 * on the edma ring.
837 *
838 * Most applications should just use gxio_mpipe_equeue_set_snf_size().
839 *
840 * @param context An initialized mPIPE context.
841 * @param ering The eDMA ring index.
842 * @param max_blks The number of blocks to dedicate to the ring
843 * (normally min_snf_blks + 1). Must be greater than min_snf_blocks.
844 * @param min_snf_blks The number of blocks which must be stored
845 * prior to starting to send the packet (normally 12).
846 * @param db Whether to allow use of dynamic blocks by the ring
847 * (normally 1).
848 *
849 * @return 0 on success, negative on error.
850 */
851extern int gxio_mpipe_config_edma_ring_blks(gxio_mpipe_context_t *context,
852 unsigned int ering,
853 unsigned int max_blks,
854 unsigned int min_snf_blks,
855 unsigned int db);
856
Chris Metcalf4875f692012-04-06 16:38:03 -0400857/*****************************************************************
858 * Classifier Program *
859 ******************************************************************/
860
861/*
862 *
863 * Functions for loading or configuring the mPIPE classifier program.
864 *
865 * The mPIPE classification processors all run a special "classifier"
866 * program which, for each incoming packet, parses the packet headers,
867 * encodes some packet metadata in the "idesc", and either drops the
868 * packet, or picks a notif ring to handle the packet, and a buffer
869 * stack to contain the packet, usually based on the channel, VLAN,
870 * dMAC, flow hash, and packet size, under the guidance of the "rules"
871 * API described below.
872 *
873 * @section gxio_mpipe_classifier_default Default Classifier
874 *
875 * The MDE provides a simple "default" classifier program. It is
876 * shipped as source in "$TILERA_ROOT/src/sys/mpipe/classifier.c",
877 * which serves as its official documentation. It is shipped as a
878 * binary program in "$TILERA_ROOT/tile/boot/classifier", which is
879 * automatically included in bootroms created by "tile-monitor", and
880 * is automatically loaded by the hypervisor at boot time.
881 *
882 * The L2 analysis handles LLC packets, SNAP packets, and "VLAN
883 * wrappers" (keeping the outer VLAN).
884 *
885 * The L3 analysis handles IPv4 and IPv6, dropping packets with bad
886 * IPv4 header checksums, requesting computation of a TCP/UDP checksum
887 * if appropriate, and hashing the dest and src IP addresses, plus the
888 * ports for TCP/UDP packets, into the flow hash. No special analysis
889 * is done for "fragmented" packets or "tunneling" protocols. Thus,
890 * the first fragment of a fragmented TCP/UDP packet is hashed using
891 * src/dest IP address and ports and all subsequent fragments are only
892 * hashed according to src/dest IP address.
893 *
894 * The L3 analysis handles other packets too, hashing the dMAC
895 * smac into a flow hash.
896 *
897 * The channel, VLAN, and dMAC used to pick a "rule" (see the
898 * "rules" APIs below), which in turn is used to pick a buffer stack
899 * (based on the packet size) and a bucket (based on the flow hash).
900 *
901 * To receive traffic matching a particular (channel/VLAN/dMAC
902 * pattern, an application should allocate its own buffer stacks and
903 * load balancer buckets, and map traffic to those stacks and buckets,
904 * as decribed by the "rules" API below.
905 *
906 * Various packet metadata is encoded in the idesc. The flow hash is
907 * four bytes at 0x0C. The VLAN is two bytes at 0x10. The ethtype is
908 * two bytes at 0x12. The l3 start is one byte at 0x14. The l4 start
909 * is one byte at 0x15 for IPv4 and IPv6 packets, and otherwise zero.
910 * The protocol is one byte at 0x16 for IPv4 and IPv6 packets, and
911 * otherwise zero.
912 *
913 * @section gxio_mpipe_classifier_custom Custom Classifiers.
914 *
915 * A custom classifier may be created using "tile-mpipe-cc" with a
916 * customized version of the default classifier sources.
917 *
918 * The custom classifier may be included in bootroms using the
919 * "--classifier" option to "tile-monitor", or loaded dynamically
920 * using gxio_mpipe_classifier_load_from_file().
921 *
922 * Be aware that "extreme" customizations may break the assumptions of
923 * the "rules" APIs described below, but simple customizations, such
924 * as adding new packet metadata, should be fine.
925 */
926
927/* A set of classifier rules, plus a context. */
928typedef struct {
929
930 /* The context. */
931 gxio_mpipe_context_t *context;
932
933 /* The actual rules. */
934 gxio_mpipe_rules_list_t list;
935
936} gxio_mpipe_rules_t;
937
938/* Initialize a classifier program rules list.
939 *
940 * This function can be called on a previously initialized rules list
941 * to discard any previously added rules.
942 *
943 * @param rules Rules list to initialize.
944 * @param context An initialized mPIPE context.
945 */
946extern void gxio_mpipe_rules_init(gxio_mpipe_rules_t *rules,
947 gxio_mpipe_context_t *context);
948
949/* Begin a new rule on the indicated rules list.
950 *
951 * Note that an empty rule matches all packets, but an empty rule list
952 * matches no packets.
953 *
954 * @param rules Rules list to which new rule is appended.
955 * @param bucket First load balancer bucket to which packets will be
956 * delivered.
957 * @param num_buckets Number of buckets (must be a power of two) across
958 * which packets will be distributed based on the "flow hash".
959 * @param stacks Either NULL, to assign each packet to the smallest
960 * initialized buffer stack which does not induce chaining (and to
961 * drop packets which exceed the largest initialized buffer stack
962 * buffer size), or an array, with each entry indicating which buffer
963 * stack should be used for packets up to that size (with 255
964 * indicating that those packets should be dropped).
965 * @return 0 on success, or a negative error code on failure.
966 */
967extern int gxio_mpipe_rules_begin(gxio_mpipe_rules_t *rules,
968 unsigned int bucket,
969 unsigned int num_buckets,
970 gxio_mpipe_rules_stacks_t *stacks);
971
972/* Set the headroom of the current rule.
973 *
974 * @param rules Rules list whose current rule will be modified.
975 * @param headroom The headroom.
976 * @return 0 on success, or a negative error code on failure.
977 */
978extern int gxio_mpipe_rules_set_headroom(gxio_mpipe_rules_t *rules,
979 uint8_t headroom);
980
981/* Indicate that packets from a particular channel can be delivered
982 * to the buckets and buffer stacks associated with the current rule.
983 *
984 * Channels added must be associated with links opened by the mPIPE context
985 * used in gxio_mpipe_rules_init(). A rule with no channels is equivalent
986 * to a rule naming all such associated channels.
987 *
988 * @param rules Rules list whose current rule will be modified.
989 * @param channel The channel to add.
990 * @return 0 on success, or a negative error code on failure.
991 */
992extern int gxio_mpipe_rules_add_channel(gxio_mpipe_rules_t *rules,
993 unsigned int channel);
994
995/* Commit rules.
996 *
997 * The rules are sent to the hypervisor, where they are combined with
998 * the rules from other apps, and used to program the hardware classifier.
999 *
1000 * Note that if this function returns an error, then the rules will NOT
1001 * have been committed, even if the error is due to interactions with
1002 * rules from another app.
1003 *
1004 * @param rules Rules list to commit.
1005 * @return 0 on success, or a negative error code on failure.
1006 */
1007extern int gxio_mpipe_rules_commit(gxio_mpipe_rules_t *rules);
1008
1009/*****************************************************************
1010 * Ingress Queue Wrapper *
1011 ******************************************************************/
1012
1013/*
1014 *
1015 * Convenience functions for receiving packets from a NotifRing and
1016 * sending packets via an eDMA ring.
1017 *
1018 * The mpipe ingress and egress hardware uses shared memory packet
1019 * descriptors to describe packets that have arrived on ingress or
1020 * are destined for egress. These descriptors are stored in shared
1021 * memory ring buffers and written or read by hardware as necessary.
1022 * The gxio library provides wrapper functions that manage the head and
1023 * tail pointers for these rings, allowing the user to easily read or
1024 * write packet descriptors.
1025 *
1026 * The initialization interface for ingress and egress rings is quite
1027 * similar. For example, to create an ingress queue, the user passes
1028 * a ::gxio_mpipe_iqueue_t state object, a ring number from
1029 * gxio_mpipe_alloc_notif_rings(), and the address of memory to hold a
1030 * ring buffer to the gxio_mpipe_iqueue_init() function. The function
1031 * returns success when the state object has been initialized and the
1032 * hardware configured to deliver packets to the specified ring
1033 * buffer. Similarly, gxio_mpipe_equeue_init() takes a
1034 * ::gxio_mpipe_equeue_t state object, a ring number from
1035 * gxio_mpipe_alloc_edma_rings(), and a shared memory buffer.
1036 *
1037 * @section gxio_mpipe_iqueue Working with Ingress Queues
1038 *
1039 * Once initialized, the gxio_mpipe_iqueue_t API provides two flows
1040 * for getting the ::gxio_mpipe_idesc_t packet descriptor associated
1041 * with incoming packets. The simplest is to call
1042 * gxio_mpipe_iqueue_get() or gxio_mpipe_iqueue_try_get(). These
1043 * functions copy the oldest packet descriptor out of the NotifRing and
1044 * into a descriptor provided by the caller. They also immediately
1045 * inform the hardware that a descriptor has been processed.
1046 *
1047 * For applications with stringent performance requirements, higher
1048 * efficiency can be achieved by avoiding the packet descriptor copy
1049 * and processing multiple descriptors at once. The
1050 * gxio_mpipe_iqueue_peek() and gxio_mpipe_iqueue_try_peek() functions
1051 * allow such optimizations. These functions provide a pointer to the
1052 * next valid ingress descriptor in the NotifRing's shared memory ring
1053 * buffer, and a count of how many contiguous descriptors are ready to
1054 * be processed. The application can then process any number of those
1055 * descriptors in place, calling gxio_mpipe_iqueue_consume() to inform
1056 * the hardware after each one has been processed.
1057 *
1058 * @section gxio_mpipe_equeue Working with Egress Queues
1059 *
1060 * Similarly, the egress queue API provides a high-performance
1061 * interface plus a simple wrapper for use in posting
1062 * ::gxio_mpipe_edesc_t egress packet descriptors. The simple
1063 * version, gxio_mpipe_equeue_put(), allows the programmer to wait for
1064 * an eDMA ring slot to become available and write a single descriptor
1065 * into the ring.
1066 *
1067 * Alternatively, you can reserve slots in the eDMA ring using
1068 * gxio_mpipe_equeue_reserve() or gxio_mpipe_equeue_try_reserve(), and
1069 * then fill in each slot using gxio_mpipe_equeue_put_at(). This
1070 * capability can be used to amortize the cost of reserving slots
1071 * across several packets. It also allows gather operations to be
1072 * performed on a shared equeue, by ensuring that the edescs for all
1073 * the fragments are all contiguous in the eDMA ring.
1074 *
1075 * The gxio_mpipe_equeue_reserve() and gxio_mpipe_equeue_try_reserve()
1076 * functions return a 63-bit "completion slot", which is actually a
1077 * sequence number, the low bits of which indicate the ring buffer
1078 * index and the high bits the number of times the application has
1079 * gone around the egress ring buffer. The extra bits allow an
1080 * application to check for egress completion by calling
1081 * gxio_mpipe_equeue_is_complete() to see whether a particular 'slot'
1082 * number has finished. Given the maximum packet rates of the Gx
1083 * processor, the 63-bit slot number will never wrap.
1084 *
1085 * In practice, most applications use the ::gxio_mpipe_edesc_t::hwb
1086 * bit to indicate that the buffers containing egress packet data
1087 * should be pushed onto a buffer stack when egress is complete. Such
1088 * applications generally do not need to know when an egress operation
1089 * completes (since there is no need to free a buffer post-egress),
1090 * and thus can use the optimized gxio_mpipe_equeue_reserve_fast() or
1091 * gxio_mpipe_equeue_try_reserve_fast() functions, which return a 24
1092 * bit "slot", instead of a 63-bit "completion slot".
1093 *
1094 * Once a slot has been "reserved", it MUST be filled. If the
1095 * application reserves a slot and then decides that it does not
1096 * actually need it, it can set the ::gxio_mpipe_edesc_t::ns (no send)
1097 * bit on the descriptor passed to gxio_mpipe_equeue_put_at() to
1098 * indicate that no data should be sent. This technique can also be
1099 * used to drop an incoming packet, instead of forwarding it, since
1100 * any buffer will still be pushed onto the buffer stack when the
1101 * egress descriptor is processed.
1102 */
1103
1104/* A convenient interface to a NotifRing, for use by a single thread.
1105 */
1106typedef struct {
1107
1108 /* The context. */
1109 gxio_mpipe_context_t *context;
1110
1111 /* The actual NotifRing. */
1112 gxio_mpipe_idesc_t *idescs;
1113
1114 /* The number of entries. */
1115 unsigned long num_entries;
1116
1117 /* The number of entries minus one. */
1118 unsigned long mask_num_entries;
1119
1120 /* The log2() of the number of entries. */
1121 unsigned long log2_num_entries;
1122
1123 /* The next entry. */
1124 unsigned int head;
1125
1126 /* The NotifRing id. */
1127 unsigned int ring;
1128
1129#ifdef __BIG_ENDIAN__
1130 /* The number of byteswapped entries. */
1131 unsigned int swapped;
1132#endif
1133
1134} gxio_mpipe_iqueue_t;
1135
1136/* Initialize an "iqueue".
1137 *
1138 * Takes the iqueue plus the same args as gxio_mpipe_init_notif_ring().
1139 */
1140extern int gxio_mpipe_iqueue_init(gxio_mpipe_iqueue_t *iqueue,
1141 gxio_mpipe_context_t *context,
1142 unsigned int ring,
1143 void *mem, size_t mem_size,
1144 unsigned int mem_flags);
1145
1146/* Advance over some old entries in an iqueue.
1147 *
1148 * Please see the documentation for gxio_mpipe_iqueue_consume().
1149 *
1150 * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
1151 * @param count The number of entries to advance over.
1152 */
1153static inline void gxio_mpipe_iqueue_advance(gxio_mpipe_iqueue_t *iqueue,
1154 int count)
1155{
1156 /* Advance with proper wrap. */
1157 int head = iqueue->head + count;
1158 iqueue->head =
1159 (head & iqueue->mask_num_entries) +
1160 (head >> iqueue->log2_num_entries);
1161
1162#ifdef __BIG_ENDIAN__
1163 /* HACK: Track swapped entries. */
1164 iqueue->swapped -= count;
1165#endif
1166}
1167
1168/* Release the ring and bucket for an old entry in an iqueue.
1169 *
1170 * Releasing the ring allows more packets to be delivered to the ring.
1171 *
1172 * Releasing the bucket allows flows using the bucket to be moved to a
1173 * new ring when using GXIO_MPIPE_BUCKET_DYNAMIC_FLOW_AFFINITY.
1174 *
1175 * This function is shorthand for "gxio_mpipe_credit(iqueue->context,
1176 * iqueue->ring, idesc->bucket_id, 1)", and it may be more convenient
1177 * to make that underlying call, using those values, instead of
1178 * tracking the entire "idesc".
1179 *
1180 * If packet processing is deferred, optimal performance requires that
1181 * the releasing be deferred as well.
1182 *
1183 * Please see the documentation for gxio_mpipe_iqueue_consume().
1184 *
1185 * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
1186 * @param idesc The descriptor which was processed.
1187 */
1188static inline void gxio_mpipe_iqueue_release(gxio_mpipe_iqueue_t *iqueue,
1189 gxio_mpipe_idesc_t *idesc)
1190{
1191 gxio_mpipe_credit(iqueue->context, iqueue->ring, idesc->bucket_id, 1);
1192}
1193
1194/* Consume a packet from an "iqueue".
1195 *
1196 * After processing packets peeked at via gxio_mpipe_iqueue_peek()
1197 * or gxio_mpipe_iqueue_try_peek(), you must call this function, or
1198 * gxio_mpipe_iqueue_advance() plus gxio_mpipe_iqueue_release(), to
1199 * advance over those entries, and release their rings and buckets.
1200 *
1201 * You may call this function as each packet is processed, or you can
1202 * wait until several packets have been processed.
1203 *
1204 * Note that if you are using a single bucket, and you are handling
1205 * batches of N packets, then you can replace several calls to this
1206 * function with calls to "gxio_mpipe_iqueue_advance(iqueue, N)" and
1207 * "gxio_mpipe_credit(iqueue->context, iqueue->ring, bucket, N)".
1208 *
1209 * Note that if your classifier sets "idesc->nr", then you should
1210 * explicitly call "gxio_mpipe_iqueue_advance(iqueue, idesc)" plus
1211 * "gxio_mpipe_credit(iqueue->context, iqueue->ring, -1, 1)", to
1212 * avoid incorrectly crediting the (unused) bucket.
1213 *
1214 * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
1215 * @param idesc The descriptor which was processed.
1216 */
1217static inline void gxio_mpipe_iqueue_consume(gxio_mpipe_iqueue_t *iqueue,
1218 gxio_mpipe_idesc_t *idesc)
1219{
1220 gxio_mpipe_iqueue_advance(iqueue, 1);
1221 gxio_mpipe_iqueue_release(iqueue, idesc);
1222}
1223
1224/* Peek at the next packet(s) in an "iqueue", without waiting.
1225 *
1226 * If no packets are available, fills idesc_ref with NULL, and then
1227 * returns ::GXIO_MPIPE_ERR_IQUEUE_EMPTY. Otherwise, fills idesc_ref
1228 * with the address of the next valid packet descriptor, and returns
1229 * the maximum number of valid descriptors which can be processed.
1230 * You may process fewer descriptors if desired.
1231 *
1232 * Call gxio_mpipe_iqueue_consume() on each packet once it has been
1233 * processed (or dropped), to allow more packets to be delivered.
1234 *
1235 * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
1236 * @param idesc_ref A pointer to a packet descriptor pointer.
1237 * @return The (positive) number of packets which can be processed,
1238 * or ::GXIO_MPIPE_ERR_IQUEUE_EMPTY if no packets are available.
1239 */
1240static inline int gxio_mpipe_iqueue_try_peek(gxio_mpipe_iqueue_t *iqueue,
1241 gxio_mpipe_idesc_t **idesc_ref)
1242{
1243 gxio_mpipe_idesc_t *next;
1244
1245 uint64_t head = iqueue->head;
1246 uint64_t tail = __gxio_mmio_read(iqueue->idescs);
1247
1248 /* Available entries. */
1249 uint64_t avail =
1250 (tail >= head) ? (tail - head) : (iqueue->num_entries - head);
1251
1252 if (avail == 0) {
1253 *idesc_ref = NULL;
1254 return GXIO_MPIPE_ERR_IQUEUE_EMPTY;
1255 }
1256
1257 next = &iqueue->idescs[head];
1258
1259 /* ISSUE: Is this helpful? */
1260 __insn_prefetch(next);
1261
1262#ifdef __BIG_ENDIAN__
1263 /* HACK: Swap new entries directly in memory. */
1264 {
1265 int i, j;
1266 for (i = iqueue->swapped; i < avail; i++) {
1267 for (j = 0; j < 8; j++)
1268 next[i].words[j] =
1269 __builtin_bswap64(next[i].words[j]);
1270 }
1271 iqueue->swapped = avail;
1272 }
1273#endif
1274
1275 *idesc_ref = next;
1276
1277 return avail;
1278}
1279
1280/* Drop a packet by pushing its buffer (if appropriate).
1281 *
1282 * NOTE: The caller must still call gxio_mpipe_iqueue_consume() if idesc
1283 * came from gxio_mpipe_iqueue_try_peek() or gxio_mpipe_iqueue_peek().
1284 *
1285 * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
1286 * @param idesc A packet descriptor.
1287 */
1288static inline void gxio_mpipe_iqueue_drop(gxio_mpipe_iqueue_t *iqueue,
1289 gxio_mpipe_idesc_t *idesc)
1290{
1291 /* FIXME: Handle "chaining" properly. */
1292
1293 if (!idesc->be) {
1294 unsigned char *va = gxio_mpipe_idesc_get_va(idesc);
1295 gxio_mpipe_push_buffer(iqueue->context, idesc->stack_idx, va);
1296 }
1297}
1298
1299/*****************************************************************
1300 * Egress Queue Wrapper *
1301 ******************************************************************/
1302
1303/* A convenient, thread-safe interface to an eDMA ring. */
1304typedef struct {
1305
1306 /* State object for tracking head and tail pointers. */
1307 __gxio_dma_queue_t dma_queue;
1308
1309 /* The ring entries. */
1310 gxio_mpipe_edesc_t *edescs;
1311
1312 /* The number of entries minus one. */
1313 unsigned long mask_num_entries;
1314
1315 /* The log2() of the number of entries. */
1316 unsigned long log2_num_entries;
1317
Chris Metcalf2628e8a2013-08-01 11:36:42 -04001318 /* The context. */
1319 gxio_mpipe_context_t *context;
1320
1321 /* The ering. */
1322 unsigned int ering;
1323
1324 /* The channel. */
1325 unsigned int channel;
1326
Chris Metcalf4875f692012-04-06 16:38:03 -04001327} gxio_mpipe_equeue_t;
1328
1329/* Initialize an "equeue".
1330 *
Chris Metcalf2628e8a2013-08-01 11:36:42 -04001331 * This function uses gxio_mpipe_init_edma_ring() to initialize the
1332 * underlying edma_ring using the provided arguments.
1333 *
1334 * @param equeue An egress queue to be initialized.
1335 * @param context An initialized mPIPE context.
1336 * @param ering The eDMA ring index.
1337 * @param channel The channel to use. This must be one of the channels
1338 * associated with the context's set of open links.
1339 * @param mem A physically contiguous region of memory to be filled
1340 * with a ring of ::gxio_mpipe_edesc_t structures.
1341 * @param mem_size Number of bytes in the ring. Must be 512, 2048,
1342 * 8192 or 65536, times 16 (i.e. sizeof(gxio_mpipe_edesc_t)).
1343 * @param mem_flags ::gxio_mpipe_mem_flags_e memory flags.
1344 *
1345 * @return 0 on success, ::GXIO_MPIPE_ERR_BAD_EDMA_RING or
1346 * ::GXIO_ERR_INVAL_MEMORY_SIZE on failure.
Chris Metcalf4875f692012-04-06 16:38:03 -04001347 */
1348extern int gxio_mpipe_equeue_init(gxio_mpipe_equeue_t *equeue,
1349 gxio_mpipe_context_t *context,
Chris Metcalf2628e8a2013-08-01 11:36:42 -04001350 unsigned int ering,
Chris Metcalf4875f692012-04-06 16:38:03 -04001351 unsigned int channel,
1352 void *mem, unsigned int mem_size,
1353 unsigned int mem_flags);
1354
1355/* Reserve completion slots for edescs.
1356 *
1357 * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
1358 *
1359 * This function is slower than gxio_mpipe_equeue_reserve_fast(), but
1360 * returns a full 64 bit completion slot, which can be used with
1361 * gxio_mpipe_equeue_is_complete().
1362 *
1363 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1364 * @param num Number of slots to reserve (must be non-zero).
1365 * @return The first reserved completion slot, or a negative error code.
1366 */
1367static inline int64_t gxio_mpipe_equeue_reserve(gxio_mpipe_equeue_t *equeue,
1368 unsigned int num)
1369{
1370 return __gxio_dma_queue_reserve_aux(&equeue->dma_queue, num, true);
1371}
1372
1373/* Reserve completion slots for edescs, if possible.
1374 *
1375 * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
1376 *
1377 * This function is slower than gxio_mpipe_equeue_try_reserve_fast(),
1378 * but returns a full 64 bit completion slot, which can be used with
1379 * gxio_mpipe_equeue_is_complete().
1380 *
1381 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1382 * @param num Number of slots to reserve (must be non-zero).
1383 * @return The first reserved completion slot, or a negative error code.
1384 */
1385static inline int64_t gxio_mpipe_equeue_try_reserve(gxio_mpipe_equeue_t
1386 *equeue, unsigned int num)
1387{
1388 return __gxio_dma_queue_reserve_aux(&equeue->dma_queue, num, false);
1389}
1390
1391/* Reserve slots for edescs.
1392 *
1393 * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
1394 *
1395 * This function is faster than gxio_mpipe_equeue_reserve(), but
1396 * returns a 24 bit slot (instead of a 64 bit completion slot), which
1397 * thus cannot be used with gxio_mpipe_equeue_is_complete().
1398 *
1399 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1400 * @param num Number of slots to reserve (should be non-zero).
1401 * @return The first reserved slot, or a negative error code.
1402 */
1403static inline int64_t gxio_mpipe_equeue_reserve_fast(gxio_mpipe_equeue_t
1404 *equeue, unsigned int num)
1405{
1406 return __gxio_dma_queue_reserve(&equeue->dma_queue, num, true, false);
1407}
1408
1409/* Reserve slots for edescs, if possible.
1410 *
1411 * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
1412 *
1413 * This function is faster than gxio_mpipe_equeue_try_reserve(), but
1414 * returns a 24 bit slot (instead of a 64 bit completion slot), which
1415 * thus cannot be used with gxio_mpipe_equeue_is_complete().
1416 *
1417 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1418 * @param num Number of slots to reserve (should be non-zero).
1419 * @return The first reserved slot, or a negative error code.
1420 */
1421static inline int64_t gxio_mpipe_equeue_try_reserve_fast(gxio_mpipe_equeue_t
1422 *equeue,
1423 unsigned int num)
1424{
1425 return __gxio_dma_queue_reserve(&equeue->dma_queue, num, false, false);
1426}
1427
1428/*
1429 * HACK: This helper function tricks gcc 4.6 into avoiding saving
1430 * a copy of "edesc->words[0]" on the stack for no obvious reason.
1431 */
1432
1433static inline void gxio_mpipe_equeue_put_at_aux(gxio_mpipe_equeue_t *equeue,
1434 uint_reg_t ew[2],
1435 unsigned long slot)
1436{
1437 unsigned long edma_slot = slot & equeue->mask_num_entries;
1438 gxio_mpipe_edesc_t *edesc_p = &equeue->edescs[edma_slot];
1439
1440 /*
1441 * ISSUE: Could set eDMA ring to be on generation 1 at start, which
1442 * would avoid the negation here, perhaps allowing "__insn_bfins()".
1443 */
1444 ew[0] |= !((slot >> equeue->log2_num_entries) & 1);
1445
1446 /*
1447 * NOTE: We use "__gxio_mpipe_write()", plus the fact that the eDMA
1448 * queue alignment restrictions ensure that these two words are on
1449 * the same cacheline, to force proper ordering between the stores.
1450 */
1451 __gxio_mmio_write64(&edesc_p->words[1], ew[1]);
1452 __gxio_mmio_write64(&edesc_p->words[0], ew[0]);
1453}
1454
1455/* Post an edesc to a given slot in an equeue.
1456 *
1457 * This function copies the supplied edesc into entry "slot mod N" in
1458 * the underlying ring, setting the "gen" bit to the appropriate value
1459 * based on "(slot mod N*2)", where "N" is the size of the ring. Note
1460 * that the higher bits of slot are unused, and thus, this function
1461 * can handle "slots" as well as "completion slots".
1462 *
1463 * Normally this function is used to fill in slots reserved by
1464 * gxio_mpipe_equeue_try_reserve(), gxio_mpipe_equeue_reserve(),
1465 * gxio_mpipe_equeue_try_reserve_fast(), or
1466 * gxio_mpipe_equeue_reserve_fast(),
1467 *
1468 * This function can also be used without "reserving" slots, if the
1469 * application KNOWS that the ring can never overflow, for example, by
1470 * pushing fewer buffers into the buffer stacks than there are total
1471 * slots in the equeue, but this is NOT recommended.
1472 *
1473 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1474 * @param edesc The egress descriptor to be posted.
1475 * @param slot An egress slot (only the low bits are actually used).
1476 */
1477static inline void gxio_mpipe_equeue_put_at(gxio_mpipe_equeue_t *equeue,
1478 gxio_mpipe_edesc_t edesc,
1479 unsigned long slot)
1480{
1481 gxio_mpipe_equeue_put_at_aux(equeue, edesc.words, slot);
1482}
1483
1484/* Post an edesc to the next slot in an equeue.
1485 *
1486 * This is a convenience wrapper around
1487 * gxio_mpipe_equeue_reserve_fast() and gxio_mpipe_equeue_put_at().
1488 *
1489 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1490 * @param edesc The egress descriptor to be posted.
1491 * @return 0 on success.
1492 */
1493static inline int gxio_mpipe_equeue_put(gxio_mpipe_equeue_t *equeue,
1494 gxio_mpipe_edesc_t edesc)
1495{
1496 int64_t slot = gxio_mpipe_equeue_reserve_fast(equeue, 1);
1497 if (slot < 0)
1498 return (int)slot;
1499
1500 gxio_mpipe_equeue_put_at(equeue, edesc, slot);
1501
1502 return 0;
1503}
1504
1505/* Ask the mPIPE hardware to egress outstanding packets immediately.
1506 *
1507 * This call is not necessary, but may slightly reduce overall latency.
1508 *
1509 * Technically, you should flush all gxio_mpipe_equeue_put_at() writes
1510 * to memory before calling this function, to ensure the descriptors
1511 * are visible in memory before the mPIPE hardware actually looks for
1512 * them. But this should be very rare, and the only side effect would
1513 * be increased latency, so it is up to the caller to decide whether
1514 * or not to flush memory.
1515 *
1516 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1517 */
1518static inline void gxio_mpipe_equeue_flush(gxio_mpipe_equeue_t *equeue)
1519{
1520 /* Use "ring_idx = 0" and "count = 0" to "wake up" the eDMA ring. */
1521 MPIPE_EDMA_POST_REGION_VAL_t val = { {0} };
1522 /* Flush the write buffers. */
1523 __insn_flushwb();
1524 __gxio_mmio_write(equeue->dma_queue.post_region_addr, val.word);
1525}
1526
1527/* Determine if a given edesc has been completed.
1528 *
1529 * Note that this function requires a "completion slot", and thus may
1530 * NOT be used with a "slot" from gxio_mpipe_equeue_reserve_fast() or
1531 * gxio_mpipe_equeue_try_reserve_fast().
1532 *
1533 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1534 * @param completion_slot The completion slot used by the edesc.
1535 * @param update If true, and the desc does not appear to have completed
1536 * yet, then update any software cache of the hardware completion counter,
1537 * and check again. This should normally be true.
1538 * @return True iff the given edesc has been completed.
1539 */
1540static inline int gxio_mpipe_equeue_is_complete(gxio_mpipe_equeue_t *equeue,
1541 int64_t completion_slot,
1542 int update)
1543{
1544 return __gxio_dma_queue_is_complete(&equeue->dma_queue,
1545 completion_slot, update);
1546}
1547
Chris Metcalf2628e8a2013-08-01 11:36:42 -04001548/* Set the snf (store and forward) size for an equeue.
1549 *
1550 * The snf size for an equeue defaults to 1536, and encodes the size
1551 * of the largest packet for which egress is guaranteed to avoid
1552 * transmission underruns and/or corrupt checksums under heavy load.
1553 *
1554 * The snf size affects a global resource pool which cannot support,
1555 * for example, all 24 equeues each requesting an snf size of 8K.
1556 *
1557 * To ensure that jumbo packets can be egressed properly, the snf size
1558 * should be set to the size of the largest possible packet, which
1559 * will usually be limited by the size of the app's largest buffer.
1560 *
1561 * This is a convenience wrapper around
1562 * gxio_mpipe_config_edma_ring_blks().
1563 *
1564 * This function should not be called after any egress has been done
1565 * on the equeue.
1566 *
1567 * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
1568 * @param size The snf size, in bytes.
1569 * @return Zero on success, negative error otherwise.
1570 */
1571static inline int gxio_mpipe_equeue_set_snf_size(gxio_mpipe_equeue_t *equeue,
1572 size_t size)
1573{
1574 int blks = (size + 127) / 128;
1575 return gxio_mpipe_config_edma_ring_blks(equeue->context, equeue->ering,
1576 blks + 1, blks, 1);
1577}
1578
Chris Metcalf4875f692012-04-06 16:38:03 -04001579/*****************************************************************
1580 * Link Management *
1581 ******************************************************************/
1582
1583/*
1584 *
1585 * Functions for manipulating and sensing the state and configuration
1586 * of physical network links.
1587 *
1588 * @section gxio_mpipe_link_perm Link Permissions
1589 *
1590 * Opening a link (with gxio_mpipe_link_open()) requests a set of link
1591 * permissions, which control what may be done with the link, and potentially
1592 * what permissions may be granted to other processes.
1593 *
1594 * Data permission allows the process to receive packets from the link by
1595 * specifying the link's channel number in mPIPE packet distribution rules,
1596 * and to send packets to the link by using the link's channel number as
1597 * the target for an eDMA ring.
1598 *
1599 * Stats permission allows the process to retrieve link attributes (such as
1600 * the speeds it is capable of running at, or whether it is currently up), and
1601 * to read and write certain statistics-related registers in the link's MAC.
1602 *
1603 * Control permission allows the process to retrieve and modify link attributes
1604 * (so that it may, for example, bring the link up and take it down), and
1605 * read and write many registers in the link's MAC and PHY.
1606 *
1607 * Any permission may be requested as shared, which allows other processes
1608 * to also request shared permission, or exclusive, which prevents other
1609 * processes from requesting it. In keeping with GXIO's typical usage in
1610 * an embedded environment, the defaults for all permissions are shared.
1611 *
1612 * Permissions are granted on a first-come, first-served basis, so if two
1613 * applications request an exclusive permission on the same link, the one
1614 * to run first will win. Note, however, that some system components, like
1615 * the kernel Ethernet driver, may get an opportunity to open links before
1616 * any applications run.
1617 *
1618 * @section gxio_mpipe_link_names Link Names
1619 *
1620 * Link names are of the form gbe<em>number</em> (for Gigabit Ethernet),
1621 * xgbe<em>number</em> (for 10 Gigabit Ethernet), loop<em>number</em> (for
1622 * internal mPIPE loopback), or ilk<em>number</em>/<em>channel</em>
1623 * (for Interlaken links); for instance, gbe0, xgbe1, loop3, and
1624 * ilk0/12 are all possible link names. The correspondence between
1625 * the link name and an mPIPE instance number or mPIPE channel number is
1626 * system-dependent; all links will not exist on all systems, and the set
1627 * of numbers used for a particular link type may not start at zero and may
1628 * not be contiguous. Use gxio_mpipe_link_enumerate() to retrieve the set of
1629 * links which exist on a system, and always use gxio_mpipe_link_instance()
1630 * to determine which mPIPE controls a particular link.
1631 *
1632 * Note that in some cases, links may share hardware, such as PHYs, or
1633 * internal mPIPE buffers; in these cases, only one of the links may be
1634 * opened at a time. This is especially common with xgbe and gbe ports,
1635 * since each xgbe port uses 4 SERDES lanes, each of which may also be
1636 * configured as one gbe port.
1637 *
1638 * @section gxio_mpipe_link_states Link States
1639 *
1640 * The mPIPE link management model revolves around three different states,
1641 * which are maintained for each link:
1642 *
1643 * 1. The <em>current</em> link state: is the link up now, and if so, at
1644 * what speed?
1645 *
1646 * 2. The <em>desired</em> link state: what do we want the link state to be?
1647 * The system is always working to make this state the current state;
1648 * thus, if the desired state is up, and the link is down, we'll be
1649 * constantly trying to bring it up, automatically.
1650 *
1651 * 3. The <em>possible</em> link state: what speeds are valid for this
1652 * particular link? Or, in other words, what are the capabilities of
1653 * the link hardware?
1654 *
1655 * These link states are not, strictly speaking, related to application
1656 * state; they may be manipulated at any time, whether or not the link
1657 * is currently being used for data transfer. However, for convenience,
1658 * gxio_mpipe_link_open() and gxio_mpipe_link_close() (or application exit)
1659 * can affect the link state. These implicit link management operations
1660 * may be modified or disabled by the use of link open flags.
1661 *
1662 * From an application, you can use gxio_mpipe_link_get_attr()
1663 * and gxio_mpipe_link_set_attr() to manipulate the link states.
1664 * gxio_mpipe_link_get_attr() with ::GXIO_MPIPE_LINK_POSSIBLE_STATE
1665 * gets you the possible link state. gxio_mpipe_link_get_attr() with
1666 * ::GXIO_MPIPE_LINK_CURRENT_STATE gets you the current link state.
1667 * Finally, gxio_mpipe_link_set_attr() and gxio_mpipe_link_get_attr()
1668 * with ::GXIO_MPIPE_LINK_DESIRED_STATE allow you to modify or retrieve
1669 * the desired link state.
1670 *
1671 * If you want to manage a link from a part of your application which isn't
1672 * involved in packet processing, you can use the ::GXIO_MPIPE_LINK_NO_DATA
1673 * flags on a gxio_mpipe_link_open() call. This opens the link, but does
1674 * not request data permission, so it does not conflict with any exclusive
1675 * permissions which may be held by other processes. You can then can use
1676 * gxio_mpipe_link_get_attr() and gxio_mpipe_link_set_attr() on this link
1677 * object to bring up or take down the link.
1678 *
1679 * Some links support link state bits which support various loopback
1680 * modes. ::GXIO_MPIPE_LINK_LOOP_MAC tests datapaths within the Tile
1681 * Processor itself; ::GXIO_MPIPE_LINK_LOOP_PHY tests the datapath between
1682 * the Tile Processor and the external physical layer interface chip; and
1683 * ::GXIO_MPIPE_LINK_LOOP_EXT tests the entire network datapath with the
1684 * aid of an external loopback connector. In addition to enabling hardware
1685 * testing, such configuration can be useful for software testing, as well.
1686 *
1687 * When LOOP_MAC or LOOP_PHY is enabled, packets transmitted on a channel
1688 * will be received by that channel, instead of being emitted on the
1689 * physical link, and packets received on the physical link will be ignored.
1690 * Other than that, all standard GXIO operations work as you might expect.
1691 * Note that loopback operation requires that the link be brought up using
1692 * one or more of the GXIO_MPIPE_LINK_SPEED_xxx link state bits.
1693 *
1694 * Those familiar with previous versions of the MDE on TILEPro hardware
1695 * will notice significant similarities between the NetIO link management
1696 * model and the mPIPE link management model. However, the NetIO model
1697 * was developed in stages, and some of its features -- for instance,
1698 * the default setting of certain flags -- were shaped by the need to be
1699 * compatible with previous versions of NetIO. Since the features provided
1700 * by the mPIPE hardware and the mPIPE GXIO library are significantly
1701 * different than those provided by NetIO, in some cases, we have made
1702 * different choices in the mPIPE link management API. Thus, please read
1703 * this documentation carefully before assuming that mPIPE link management
1704 * operations are exactly equivalent to their NetIO counterparts.
1705 */
1706
1707/* An object used to manage mPIPE link state and resources. */
1708typedef struct {
1709 /* The overall mPIPE context. */
1710 gxio_mpipe_context_t *context;
1711
1712 /* The channel number used by this link. */
1713 uint8_t channel;
1714
1715 /* The MAC index used by this link. */
1716 uint8_t mac;
1717} gxio_mpipe_link_t;
1718
1719/* Retrieve one of this system's legal link names, and its MAC address.
1720 *
1721 * @param index Link name index. If a system supports N legal link names,
1722 * then indices between 0 and N - 1, inclusive, each correspond to one of
1723 * those names. Thus, to retrieve all of a system's legal link names,
1724 * call this function in a loop, starting with an index of zero, and
1725 * incrementing it once per iteration until -1 is returned.
1726 * @param link_name Pointer to the buffer which will receive the retrieved
1727 * link name. The buffer should contain space for at least
1728 * ::GXIO_MPIPE_LINK_NAME_LEN bytes; the returned name, including the
1729 * terminating null byte, will be no longer than that.
1730 * @param link_name Pointer to the buffer which will receive the retrieved
1731 * MAC address. The buffer should contain space for at least 6 bytes.
1732 * @return Zero if a link name was successfully retrieved; -1 if one was
1733 * not.
1734 */
1735extern int gxio_mpipe_link_enumerate_mac(int index, char *link_name,
1736 uint8_t *mac_addr);
1737
1738/* Open an mPIPE link.
1739 *
1740 * A link must be opened before it may be used to send or receive packets,
1741 * and before its state may be examined or changed. Depending up on the
1742 * link's intended use, one or more link permissions may be requested via
1743 * the flags parameter; see @ref gxio_mpipe_link_perm. In addition, flags
1744 * may request that the link's state be modified at open time. See @ref
1745 * gxio_mpipe_link_states and @ref gxio_mpipe_link_open_flags for more detail.
1746 *
1747 * @param link A link state object, which will be initialized if this
1748 * function completes successfully.
1749 * @param context An initialized mPIPE context.
1750 * @param link_name Name of the link.
1751 * @param flags Zero or more @ref gxio_mpipe_link_open_flags, ORed together.
1752 * @return 0 if the link was successfully opened, or a negative error code.
1753 *
1754 */
1755extern int gxio_mpipe_link_open(gxio_mpipe_link_t *link,
1756 gxio_mpipe_context_t *context,
1757 const char *link_name, unsigned int flags);
1758
1759/* Close an mPIPE link.
1760 *
1761 * Closing a link makes it available for use by other processes. Once
1762 * a link has been closed, packets may no longer be sent on or received
1763 * from the link, and its state may not be examined or changed.
1764 *
1765 * @param link A link state object, which will no longer be initialized
1766 * if this function completes successfully.
1767 * @return 0 if the link was successfully closed, or a negative error code.
1768 *
1769 */
1770extern int gxio_mpipe_link_close(gxio_mpipe_link_t *link);
1771
1772/* Return a link's channel number.
1773 *
1774 * @param link A properly initialized link state object.
1775 * @return The channel number for the link.
1776 */
1777static inline int gxio_mpipe_link_channel(gxio_mpipe_link_t *link)
1778{
1779 return link->channel;
1780}
1781
Chris Metcalf2628e8a2013-08-01 11:36:42 -04001782/* Set a link attribute.
1783 *
1784 * @param link A properly initialized link state object.
1785 * @param attr An attribute from the set of @ref gxio_mpipe_link_attrs.
1786 * @param val New value of the attribute.
1787 * @return 0 if the attribute was successfully set, or a negative error
1788 * code.
1789 */
1790extern int gxio_mpipe_link_set_attr(gxio_mpipe_link_t *link, uint32_t attr,
1791 int64_t val);
1792
Chris Metcalf4875f692012-04-06 16:38:03 -04001793///////////////////////////////////////////////////////////////////
1794// Timestamp //
1795///////////////////////////////////////////////////////////////////
1796
1797/* Get the timestamp of mPIPE when this routine is called.
1798 *
1799 * @param context An initialized mPIPE context.
1800 * @param ts A timespec structure to store the current clock.
1801 * @return If the call was successful, zero; otherwise, a negative error
1802 * code.
1803 */
1804extern int gxio_mpipe_get_timestamp(gxio_mpipe_context_t *context,
1805 struct timespec *ts);
1806
1807/* Set the timestamp of mPIPE.
1808 *
1809 * @param context An initialized mPIPE context.
1810 * @param ts A timespec structure to store the requested clock.
1811 * @return If the call was successful, zero; otherwise, a negative error
1812 * code.
1813 */
1814extern int gxio_mpipe_set_timestamp(gxio_mpipe_context_t *context,
1815 const struct timespec *ts);
1816
1817/* Adjust the timestamp of mPIPE.
1818 *
1819 * @param context An initialized mPIPE context.
1820 * @param delta A signed time offset to adjust, in nanoseconds.
1821 * The absolute value of this parameter must be less than or
1822 * equal to 1000000000.
1823 * @return If the call was successful, zero; otherwise, a negative error
1824 * code.
1825 */
1826extern int gxio_mpipe_adjust_timestamp(gxio_mpipe_context_t *context,
1827 int64_t delta);
1828
1829#endif /* !_GXIO_MPIPE_H_ */