mpipe.h 71 KB

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