mpipe.h 73 KB

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