mpipe.h 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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 "common.h"
  22. #include "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 ring 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 ring, unsigned int channel,
  759. void *mem, size_t mem_size,
  760. unsigned int mem_flags);
  761. /*****************************************************************
  762. * Classifier Program *
  763. ******************************************************************/
  764. /*
  765. *
  766. * Functions for loading or configuring the mPIPE classifier program.
  767. *
  768. * The mPIPE classification processors all run a special "classifier"
  769. * program which, for each incoming packet, parses the packet headers,
  770. * encodes some packet metadata in the "idesc", and either drops the
  771. * packet, or picks a notif ring to handle the packet, and a buffer
  772. * stack to contain the packet, usually based on the channel, VLAN,
  773. * dMAC, flow hash, and packet size, under the guidance of the "rules"
  774. * API described below.
  775. *
  776. * @section gxio_mpipe_classifier_default Default Classifier
  777. *
  778. * The MDE provides a simple "default" classifier program. It is
  779. * shipped as source in "$TILERA_ROOT/src/sys/mpipe/classifier.c",
  780. * which serves as its official documentation. It is shipped as a
  781. * binary program in "$TILERA_ROOT/tile/boot/classifier", which is
  782. * automatically included in bootroms created by "tile-monitor", and
  783. * is automatically loaded by the hypervisor at boot time.
  784. *
  785. * The L2 analysis handles LLC packets, SNAP packets, and "VLAN
  786. * wrappers" (keeping the outer VLAN).
  787. *
  788. * The L3 analysis handles IPv4 and IPv6, dropping packets with bad
  789. * IPv4 header checksums, requesting computation of a TCP/UDP checksum
  790. * if appropriate, and hashing the dest and src IP addresses, plus the
  791. * ports for TCP/UDP packets, into the flow hash. No special analysis
  792. * is done for "fragmented" packets or "tunneling" protocols. Thus,
  793. * the first fragment of a fragmented TCP/UDP packet is hashed using
  794. * src/dest IP address and ports and all subsequent fragments are only
  795. * hashed according to src/dest IP address.
  796. *
  797. * The L3 analysis handles other packets too, hashing the dMAC
  798. * smac into a flow hash.
  799. *
  800. * The channel, VLAN, and dMAC used to pick a "rule" (see the
  801. * "rules" APIs below), which in turn is used to pick a buffer stack
  802. * (based on the packet size) and a bucket (based on the flow hash).
  803. *
  804. * To receive traffic matching a particular (channel/VLAN/dMAC
  805. * pattern, an application should allocate its own buffer stacks and
  806. * load balancer buckets, and map traffic to those stacks and buckets,
  807. * as decribed by the "rules" API below.
  808. *
  809. * Various packet metadata is encoded in the idesc. The flow hash is
  810. * four bytes at 0x0C. The VLAN is two bytes at 0x10. The ethtype is
  811. * two bytes at 0x12. The l3 start is one byte at 0x14. The l4 start
  812. * is one byte at 0x15 for IPv4 and IPv6 packets, and otherwise zero.
  813. * The protocol is one byte at 0x16 for IPv4 and IPv6 packets, and
  814. * otherwise zero.
  815. *
  816. * @section gxio_mpipe_classifier_custom Custom Classifiers.
  817. *
  818. * A custom classifier may be created using "tile-mpipe-cc" with a
  819. * customized version of the default classifier sources.
  820. *
  821. * The custom classifier may be included in bootroms using the
  822. * "--classifier" option to "tile-monitor", or loaded dynamically
  823. * using gxio_mpipe_classifier_load_from_file().
  824. *
  825. * Be aware that "extreme" customizations may break the assumptions of
  826. * the "rules" APIs described below, but simple customizations, such
  827. * as adding new packet metadata, should be fine.
  828. */
  829. /* A set of classifier rules, plus a context. */
  830. typedef struct {
  831. /* The context. */
  832. gxio_mpipe_context_t *context;
  833. /* The actual rules. */
  834. gxio_mpipe_rules_list_t list;
  835. } gxio_mpipe_rules_t;
  836. /* Initialize a classifier program rules list.
  837. *
  838. * This function can be called on a previously initialized rules list
  839. * to discard any previously added rules.
  840. *
  841. * @param rules Rules list to initialize.
  842. * @param context An initialized mPIPE context.
  843. */
  844. extern void gxio_mpipe_rules_init(gxio_mpipe_rules_t *rules,
  845. gxio_mpipe_context_t *context);
  846. /* Begin a new rule on the indicated rules list.
  847. *
  848. * Note that an empty rule matches all packets, but an empty rule list
  849. * matches no packets.
  850. *
  851. * @param rules Rules list to which new rule is appended.
  852. * @param bucket First load balancer bucket to which packets will be
  853. * delivered.
  854. * @param num_buckets Number of buckets (must be a power of two) across
  855. * which packets will be distributed based on the "flow hash".
  856. * @param stacks Either NULL, to assign each packet to the smallest
  857. * initialized buffer stack which does not induce chaining (and to
  858. * drop packets which exceed the largest initialized buffer stack
  859. * buffer size), or an array, with each entry indicating which buffer
  860. * stack should be used for packets up to that size (with 255
  861. * indicating that those packets should be dropped).
  862. * @return 0 on success, or a negative error code on failure.
  863. */
  864. extern int gxio_mpipe_rules_begin(gxio_mpipe_rules_t *rules,
  865. unsigned int bucket,
  866. unsigned int num_buckets,
  867. gxio_mpipe_rules_stacks_t *stacks);
  868. /* Set the headroom of the current rule.
  869. *
  870. * @param rules Rules list whose current rule will be modified.
  871. * @param headroom The headroom.
  872. * @return 0 on success, or a negative error code on failure.
  873. */
  874. extern int gxio_mpipe_rules_set_headroom(gxio_mpipe_rules_t *rules,
  875. uint8_t headroom);
  876. /* Indicate that packets from a particular channel can be delivered
  877. * to the buckets and buffer stacks associated with the current rule.
  878. *
  879. * Channels added must be associated with links opened by the mPIPE context
  880. * used in gxio_mpipe_rules_init(). A rule with no channels is equivalent
  881. * to a rule naming all such associated channels.
  882. *
  883. * @param rules Rules list whose current rule will be modified.
  884. * @param channel The channel to add.
  885. * @return 0 on success, or a negative error code on failure.
  886. */
  887. extern int gxio_mpipe_rules_add_channel(gxio_mpipe_rules_t *rules,
  888. unsigned int channel);
  889. /* Commit rules.
  890. *
  891. * The rules are sent to the hypervisor, where they are combined with
  892. * the rules from other apps, and used to program the hardware classifier.
  893. *
  894. * Note that if this function returns an error, then the rules will NOT
  895. * have been committed, even if the error is due to interactions with
  896. * rules from another app.
  897. *
  898. * @param rules Rules list to commit.
  899. * @return 0 on success, or a negative error code on failure.
  900. */
  901. extern int gxio_mpipe_rules_commit(gxio_mpipe_rules_t *rules);
  902. /*****************************************************************
  903. * Ingress Queue Wrapper *
  904. ******************************************************************/
  905. /*
  906. *
  907. * Convenience functions for receiving packets from a NotifRing and
  908. * sending packets via an eDMA ring.
  909. *
  910. * The mpipe ingress and egress hardware uses shared memory packet
  911. * descriptors to describe packets that have arrived on ingress or
  912. * are destined for egress. These descriptors are stored in shared
  913. * memory ring buffers and written or read by hardware as necessary.
  914. * The gxio library provides wrapper functions that manage the head and
  915. * tail pointers for these rings, allowing the user to easily read or
  916. * write packet descriptors.
  917. *
  918. * The initialization interface for ingress and egress rings is quite
  919. * similar. For example, to create an ingress queue, the user passes
  920. * a ::gxio_mpipe_iqueue_t state object, a ring number from
  921. * gxio_mpipe_alloc_notif_rings(), and the address of memory to hold a
  922. * ring buffer to the gxio_mpipe_iqueue_init() function. The function
  923. * returns success when the state object has been initialized and the
  924. * hardware configured to deliver packets to the specified ring
  925. * buffer. Similarly, gxio_mpipe_equeue_init() takes a
  926. * ::gxio_mpipe_equeue_t state object, a ring number from
  927. * gxio_mpipe_alloc_edma_rings(), and a shared memory buffer.
  928. *
  929. * @section gxio_mpipe_iqueue Working with Ingress Queues
  930. *
  931. * Once initialized, the gxio_mpipe_iqueue_t API provides two flows
  932. * for getting the ::gxio_mpipe_idesc_t packet descriptor associated
  933. * with incoming packets. The simplest is to call
  934. * gxio_mpipe_iqueue_get() or gxio_mpipe_iqueue_try_get(). These
  935. * functions copy the oldest packet descriptor out of the NotifRing and
  936. * into a descriptor provided by the caller. They also immediately
  937. * inform the hardware that a descriptor has been processed.
  938. *
  939. * For applications with stringent performance requirements, higher
  940. * efficiency can be achieved by avoiding the packet descriptor copy
  941. * and processing multiple descriptors at once. The
  942. * gxio_mpipe_iqueue_peek() and gxio_mpipe_iqueue_try_peek() functions
  943. * allow such optimizations. These functions provide a pointer to the
  944. * next valid ingress descriptor in the NotifRing's shared memory ring
  945. * buffer, and a count of how many contiguous descriptors are ready to
  946. * be processed. The application can then process any number of those
  947. * descriptors in place, calling gxio_mpipe_iqueue_consume() to inform
  948. * the hardware after each one has been processed.
  949. *
  950. * @section gxio_mpipe_equeue Working with Egress Queues
  951. *
  952. * Similarly, the egress queue API provides a high-performance
  953. * interface plus a simple wrapper for use in posting
  954. * ::gxio_mpipe_edesc_t egress packet descriptors. The simple
  955. * version, gxio_mpipe_equeue_put(), allows the programmer to wait for
  956. * an eDMA ring slot to become available and write a single descriptor
  957. * into the ring.
  958. *
  959. * Alternatively, you can reserve slots in the eDMA ring using
  960. * gxio_mpipe_equeue_reserve() or gxio_mpipe_equeue_try_reserve(), and
  961. * then fill in each slot using gxio_mpipe_equeue_put_at(). This
  962. * capability can be used to amortize the cost of reserving slots
  963. * across several packets. It also allows gather operations to be
  964. * performed on a shared equeue, by ensuring that the edescs for all
  965. * the fragments are all contiguous in the eDMA ring.
  966. *
  967. * The gxio_mpipe_equeue_reserve() and gxio_mpipe_equeue_try_reserve()
  968. * functions return a 63-bit "completion slot", which is actually a
  969. * sequence number, the low bits of which indicate the ring buffer
  970. * index and the high bits the number of times the application has
  971. * gone around the egress ring buffer. The extra bits allow an
  972. * application to check for egress completion by calling
  973. * gxio_mpipe_equeue_is_complete() to see whether a particular 'slot'
  974. * number has finished. Given the maximum packet rates of the Gx
  975. * processor, the 63-bit slot number will never wrap.
  976. *
  977. * In practice, most applications use the ::gxio_mpipe_edesc_t::hwb
  978. * bit to indicate that the buffers containing egress packet data
  979. * should be pushed onto a buffer stack when egress is complete. Such
  980. * applications generally do not need to know when an egress operation
  981. * completes (since there is no need to free a buffer post-egress),
  982. * and thus can use the optimized gxio_mpipe_equeue_reserve_fast() or
  983. * gxio_mpipe_equeue_try_reserve_fast() functions, which return a 24
  984. * bit "slot", instead of a 63-bit "completion slot".
  985. *
  986. * Once a slot has been "reserved", it MUST be filled. If the
  987. * application reserves a slot and then decides that it does not
  988. * actually need it, it can set the ::gxio_mpipe_edesc_t::ns (no send)
  989. * bit on the descriptor passed to gxio_mpipe_equeue_put_at() to
  990. * indicate that no data should be sent. This technique can also be
  991. * used to drop an incoming packet, instead of forwarding it, since
  992. * any buffer will still be pushed onto the buffer stack when the
  993. * egress descriptor is processed.
  994. */
  995. /* A convenient interface to a NotifRing, for use by a single thread.
  996. */
  997. typedef struct {
  998. /* The context. */
  999. gxio_mpipe_context_t *context;
  1000. /* The actual NotifRing. */
  1001. gxio_mpipe_idesc_t *idescs;
  1002. /* The number of entries. */
  1003. unsigned long num_entries;
  1004. /* The number of entries minus one. */
  1005. unsigned long mask_num_entries;
  1006. /* The log2() of the number of entries. */
  1007. unsigned long log2_num_entries;
  1008. /* The next entry. */
  1009. unsigned int head;
  1010. /* The NotifRing id. */
  1011. unsigned int ring;
  1012. #ifdef __BIG_ENDIAN__
  1013. /* The number of byteswapped entries. */
  1014. unsigned int swapped;
  1015. #endif
  1016. } gxio_mpipe_iqueue_t;
  1017. /* Initialize an "iqueue".
  1018. *
  1019. * Takes the iqueue plus the same args as gxio_mpipe_init_notif_ring().
  1020. */
  1021. extern int gxio_mpipe_iqueue_init(gxio_mpipe_iqueue_t *iqueue,
  1022. gxio_mpipe_context_t *context,
  1023. unsigned int ring,
  1024. void *mem, size_t mem_size,
  1025. unsigned int mem_flags);
  1026. /* Advance over some old entries in an iqueue.
  1027. *
  1028. * Please see the documentation for gxio_mpipe_iqueue_consume().
  1029. *
  1030. * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
  1031. * @param count The number of entries to advance over.
  1032. */
  1033. static inline void gxio_mpipe_iqueue_advance(gxio_mpipe_iqueue_t *iqueue,
  1034. int count)
  1035. {
  1036. /* Advance with proper wrap. */
  1037. int head = iqueue->head + count;
  1038. iqueue->head =
  1039. (head & iqueue->mask_num_entries) +
  1040. (head >> iqueue->log2_num_entries);
  1041. #ifdef __BIG_ENDIAN__
  1042. /* HACK: Track swapped entries. */
  1043. iqueue->swapped -= count;
  1044. #endif
  1045. }
  1046. /* Release the ring and bucket for an old entry in an iqueue.
  1047. *
  1048. * Releasing the ring allows more packets to be delivered to the ring.
  1049. *
  1050. * Releasing the bucket allows flows using the bucket to be moved to a
  1051. * new ring when using GXIO_MPIPE_BUCKET_DYNAMIC_FLOW_AFFINITY.
  1052. *
  1053. * This function is shorthand for "gxio_mpipe_credit(iqueue->context,
  1054. * iqueue->ring, idesc->bucket_id, 1)", and it may be more convenient
  1055. * to make that underlying call, using those values, instead of
  1056. * tracking the entire "idesc".
  1057. *
  1058. * If packet processing is deferred, optimal performance requires that
  1059. * the releasing be deferred as well.
  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 idesc The descriptor which was processed.
  1065. */
  1066. static inline void gxio_mpipe_iqueue_release(gxio_mpipe_iqueue_t *iqueue,
  1067. gxio_mpipe_idesc_t *idesc)
  1068. {
  1069. gxio_mpipe_credit(iqueue->context, iqueue->ring, idesc->bucket_id, 1);
  1070. }
  1071. /* Consume a packet from an "iqueue".
  1072. *
  1073. * After processing packets peeked at via gxio_mpipe_iqueue_peek()
  1074. * or gxio_mpipe_iqueue_try_peek(), you must call this function, or
  1075. * gxio_mpipe_iqueue_advance() plus gxio_mpipe_iqueue_release(), to
  1076. * advance over those entries, and release their rings and buckets.
  1077. *
  1078. * You may call this function as each packet is processed, or you can
  1079. * wait until several packets have been processed.
  1080. *
  1081. * Note that if you are using a single bucket, and you are handling
  1082. * batches of N packets, then you can replace several calls to this
  1083. * function with calls to "gxio_mpipe_iqueue_advance(iqueue, N)" and
  1084. * "gxio_mpipe_credit(iqueue->context, iqueue->ring, bucket, N)".
  1085. *
  1086. * Note that if your classifier sets "idesc->nr", then you should
  1087. * explicitly call "gxio_mpipe_iqueue_advance(iqueue, idesc)" plus
  1088. * "gxio_mpipe_credit(iqueue->context, iqueue->ring, -1, 1)", to
  1089. * avoid incorrectly crediting the (unused) bucket.
  1090. *
  1091. * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
  1092. * @param idesc The descriptor which was processed.
  1093. */
  1094. static inline void gxio_mpipe_iqueue_consume(gxio_mpipe_iqueue_t *iqueue,
  1095. gxio_mpipe_idesc_t *idesc)
  1096. {
  1097. gxio_mpipe_iqueue_advance(iqueue, 1);
  1098. gxio_mpipe_iqueue_release(iqueue, idesc);
  1099. }
  1100. /* Peek at the next packet(s) in an "iqueue", without waiting.
  1101. *
  1102. * If no packets are available, fills idesc_ref with NULL, and then
  1103. * returns ::GXIO_MPIPE_ERR_IQUEUE_EMPTY. Otherwise, fills idesc_ref
  1104. * with the address of the next valid packet descriptor, and returns
  1105. * the maximum number of valid descriptors which can be processed.
  1106. * You may process fewer descriptors if desired.
  1107. *
  1108. * Call gxio_mpipe_iqueue_consume() on each packet once it has been
  1109. * processed (or dropped), to allow more packets to be delivered.
  1110. *
  1111. * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
  1112. * @param idesc_ref A pointer to a packet descriptor pointer.
  1113. * @return The (positive) number of packets which can be processed,
  1114. * or ::GXIO_MPIPE_ERR_IQUEUE_EMPTY if no packets are available.
  1115. */
  1116. static inline int gxio_mpipe_iqueue_try_peek(gxio_mpipe_iqueue_t *iqueue,
  1117. gxio_mpipe_idesc_t **idesc_ref)
  1118. {
  1119. gxio_mpipe_idesc_t *next;
  1120. uint64_t head = iqueue->head;
  1121. uint64_t tail = __gxio_mmio_read(iqueue->idescs);
  1122. /* Available entries. */
  1123. uint64_t avail =
  1124. (tail >= head) ? (tail - head) : (iqueue->num_entries - head);
  1125. if (avail == 0) {
  1126. *idesc_ref = NULL;
  1127. return GXIO_MPIPE_ERR_IQUEUE_EMPTY;
  1128. }
  1129. next = &iqueue->idescs[head];
  1130. /* ISSUE: Is this helpful? */
  1131. __insn_prefetch(next);
  1132. #ifdef __BIG_ENDIAN__
  1133. /* HACK: Swap new entries directly in memory. */
  1134. {
  1135. int i, j;
  1136. for (i = iqueue->swapped; i < avail; i++) {
  1137. for (j = 0; j < 8; j++)
  1138. next[i].words[j] =
  1139. __builtin_bswap64(next[i].words[j]);
  1140. }
  1141. iqueue->swapped = avail;
  1142. }
  1143. #endif
  1144. *idesc_ref = next;
  1145. return avail;
  1146. }
  1147. /* Drop a packet by pushing its buffer (if appropriate).
  1148. *
  1149. * NOTE: The caller must still call gxio_mpipe_iqueue_consume() if idesc
  1150. * came from gxio_mpipe_iqueue_try_peek() or gxio_mpipe_iqueue_peek().
  1151. *
  1152. * @param iqueue An ingress queue initialized via gxio_mpipe_iqueue_init().
  1153. * @param idesc A packet descriptor.
  1154. */
  1155. static inline void gxio_mpipe_iqueue_drop(gxio_mpipe_iqueue_t *iqueue,
  1156. gxio_mpipe_idesc_t *idesc)
  1157. {
  1158. /* FIXME: Handle "chaining" properly. */
  1159. if (!idesc->be) {
  1160. unsigned char *va = gxio_mpipe_idesc_get_va(idesc);
  1161. gxio_mpipe_push_buffer(iqueue->context, idesc->stack_idx, va);
  1162. }
  1163. }
  1164. /*****************************************************************
  1165. * Egress Queue Wrapper *
  1166. ******************************************************************/
  1167. /* A convenient, thread-safe interface to an eDMA ring. */
  1168. typedef struct {
  1169. /* State object for tracking head and tail pointers. */
  1170. __gxio_dma_queue_t dma_queue;
  1171. /* The ring entries. */
  1172. gxio_mpipe_edesc_t *edescs;
  1173. /* The number of entries minus one. */
  1174. unsigned long mask_num_entries;
  1175. /* The log2() of the number of entries. */
  1176. unsigned long log2_num_entries;
  1177. } gxio_mpipe_equeue_t;
  1178. /* Initialize an "equeue".
  1179. *
  1180. * Takes the equeue plus the same args as gxio_mpipe_init_edma_ring().
  1181. */
  1182. extern int gxio_mpipe_equeue_init(gxio_mpipe_equeue_t *equeue,
  1183. gxio_mpipe_context_t *context,
  1184. unsigned int edma_ring_id,
  1185. unsigned int channel,
  1186. void *mem, unsigned int mem_size,
  1187. unsigned int mem_flags);
  1188. /* Reserve completion slots for edescs.
  1189. *
  1190. * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
  1191. *
  1192. * This function is slower than gxio_mpipe_equeue_reserve_fast(), but
  1193. * returns a full 64 bit completion slot, which can be used with
  1194. * gxio_mpipe_equeue_is_complete().
  1195. *
  1196. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1197. * @param num Number of slots to reserve (must be non-zero).
  1198. * @return The first reserved completion slot, or a negative error code.
  1199. */
  1200. static inline int64_t gxio_mpipe_equeue_reserve(gxio_mpipe_equeue_t *equeue,
  1201. unsigned int num)
  1202. {
  1203. return __gxio_dma_queue_reserve_aux(&equeue->dma_queue, num, true);
  1204. }
  1205. /* Reserve completion slots for edescs, if possible.
  1206. *
  1207. * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
  1208. *
  1209. * This function is slower than gxio_mpipe_equeue_try_reserve_fast(),
  1210. * but returns a full 64 bit completion slot, which can be used with
  1211. * gxio_mpipe_equeue_is_complete().
  1212. *
  1213. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1214. * @param num Number of slots to reserve (must be non-zero).
  1215. * @return The first reserved completion slot, or a negative error code.
  1216. */
  1217. static inline int64_t gxio_mpipe_equeue_try_reserve(gxio_mpipe_equeue_t
  1218. *equeue, unsigned int num)
  1219. {
  1220. return __gxio_dma_queue_reserve_aux(&equeue->dma_queue, num, false);
  1221. }
  1222. /* Reserve slots for edescs.
  1223. *
  1224. * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
  1225. *
  1226. * This function is faster than gxio_mpipe_equeue_reserve(), but
  1227. * returns a 24 bit slot (instead of a 64 bit completion slot), which
  1228. * thus cannot be used with gxio_mpipe_equeue_is_complete().
  1229. *
  1230. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1231. * @param num Number of slots to reserve (should be non-zero).
  1232. * @return The first reserved slot, or a negative error code.
  1233. */
  1234. static inline int64_t gxio_mpipe_equeue_reserve_fast(gxio_mpipe_equeue_t
  1235. *equeue, unsigned int num)
  1236. {
  1237. return __gxio_dma_queue_reserve(&equeue->dma_queue, num, true, false);
  1238. }
  1239. /* Reserve slots for edescs, if possible.
  1240. *
  1241. * Use gxio_mpipe_equeue_put_at() to actually populate the slots.
  1242. *
  1243. * This function is faster than gxio_mpipe_equeue_try_reserve(), but
  1244. * returns a 24 bit slot (instead of a 64 bit completion slot), which
  1245. * thus cannot be used with gxio_mpipe_equeue_is_complete().
  1246. *
  1247. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1248. * @param num Number of slots to reserve (should be non-zero).
  1249. * @return The first reserved slot, or a negative error code.
  1250. */
  1251. static inline int64_t gxio_mpipe_equeue_try_reserve_fast(gxio_mpipe_equeue_t
  1252. *equeue,
  1253. unsigned int num)
  1254. {
  1255. return __gxio_dma_queue_reserve(&equeue->dma_queue, num, false, false);
  1256. }
  1257. /*
  1258. * HACK: This helper function tricks gcc 4.6 into avoiding saving
  1259. * a copy of "edesc->words[0]" on the stack for no obvious reason.
  1260. */
  1261. static inline void gxio_mpipe_equeue_put_at_aux(gxio_mpipe_equeue_t *equeue,
  1262. uint_reg_t ew[2],
  1263. unsigned long slot)
  1264. {
  1265. unsigned long edma_slot = slot & equeue->mask_num_entries;
  1266. gxio_mpipe_edesc_t *edesc_p = &equeue->edescs[edma_slot];
  1267. /*
  1268. * ISSUE: Could set eDMA ring to be on generation 1 at start, which
  1269. * would avoid the negation here, perhaps allowing "__insn_bfins()".
  1270. */
  1271. ew[0] |= !((slot >> equeue->log2_num_entries) & 1);
  1272. /*
  1273. * NOTE: We use "__gxio_mpipe_write()", plus the fact that the eDMA
  1274. * queue alignment restrictions ensure that these two words are on
  1275. * the same cacheline, to force proper ordering between the stores.
  1276. */
  1277. __gxio_mmio_write64(&edesc_p->words[1], ew[1]);
  1278. __gxio_mmio_write64(&edesc_p->words[0], ew[0]);
  1279. }
  1280. /* Post an edesc to a given slot in an equeue.
  1281. *
  1282. * This function copies the supplied edesc into entry "slot mod N" in
  1283. * the underlying ring, setting the "gen" bit to the appropriate value
  1284. * based on "(slot mod N*2)", where "N" is the size of the ring. Note
  1285. * that the higher bits of slot are unused, and thus, this function
  1286. * can handle "slots" as well as "completion slots".
  1287. *
  1288. * Normally this function is used to fill in slots reserved by
  1289. * gxio_mpipe_equeue_try_reserve(), gxio_mpipe_equeue_reserve(),
  1290. * gxio_mpipe_equeue_try_reserve_fast(), or
  1291. * gxio_mpipe_equeue_reserve_fast(),
  1292. *
  1293. * This function can also be used without "reserving" slots, if the
  1294. * application KNOWS that the ring can never overflow, for example, by
  1295. * pushing fewer buffers into the buffer stacks than there are total
  1296. * slots in the equeue, but this is NOT recommended.
  1297. *
  1298. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1299. * @param edesc The egress descriptor to be posted.
  1300. * @param slot An egress slot (only the low bits are actually used).
  1301. */
  1302. static inline void gxio_mpipe_equeue_put_at(gxio_mpipe_equeue_t *equeue,
  1303. gxio_mpipe_edesc_t edesc,
  1304. unsigned long slot)
  1305. {
  1306. gxio_mpipe_equeue_put_at_aux(equeue, edesc.words, slot);
  1307. }
  1308. /* Post an edesc to the next slot in an equeue.
  1309. *
  1310. * This is a convenience wrapper around
  1311. * gxio_mpipe_equeue_reserve_fast() and gxio_mpipe_equeue_put_at().
  1312. *
  1313. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1314. * @param edesc The egress descriptor to be posted.
  1315. * @return 0 on success.
  1316. */
  1317. static inline int gxio_mpipe_equeue_put(gxio_mpipe_equeue_t *equeue,
  1318. gxio_mpipe_edesc_t edesc)
  1319. {
  1320. int64_t slot = gxio_mpipe_equeue_reserve_fast(equeue, 1);
  1321. if (slot < 0)
  1322. return (int)slot;
  1323. gxio_mpipe_equeue_put_at(equeue, edesc, slot);
  1324. return 0;
  1325. }
  1326. /* Ask the mPIPE hardware to egress outstanding packets immediately.
  1327. *
  1328. * This call is not necessary, but may slightly reduce overall latency.
  1329. *
  1330. * Technically, you should flush all gxio_mpipe_equeue_put_at() writes
  1331. * to memory before calling this function, to ensure the descriptors
  1332. * are visible in memory before the mPIPE hardware actually looks for
  1333. * them. But this should be very rare, and the only side effect would
  1334. * be increased latency, so it is up to the caller to decide whether
  1335. * or not to flush memory.
  1336. *
  1337. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1338. */
  1339. static inline void gxio_mpipe_equeue_flush(gxio_mpipe_equeue_t *equeue)
  1340. {
  1341. /* Use "ring_idx = 0" and "count = 0" to "wake up" the eDMA ring. */
  1342. MPIPE_EDMA_POST_REGION_VAL_t val = { {0} };
  1343. /* Flush the write buffers. */
  1344. __insn_flushwb();
  1345. __gxio_mmio_write(equeue->dma_queue.post_region_addr, val.word);
  1346. }
  1347. /* Determine if a given edesc has been completed.
  1348. *
  1349. * Note that this function requires a "completion slot", and thus may
  1350. * NOT be used with a "slot" from gxio_mpipe_equeue_reserve_fast() or
  1351. * gxio_mpipe_equeue_try_reserve_fast().
  1352. *
  1353. * @param equeue An egress queue initialized via gxio_mpipe_equeue_init().
  1354. * @param completion_slot The completion slot used by the edesc.
  1355. * @param update If true, and the desc does not appear to have completed
  1356. * yet, then update any software cache of the hardware completion counter,
  1357. * and check again. This should normally be true.
  1358. * @return True iff the given edesc has been completed.
  1359. */
  1360. static inline int gxio_mpipe_equeue_is_complete(gxio_mpipe_equeue_t *equeue,
  1361. int64_t completion_slot,
  1362. int update)
  1363. {
  1364. return __gxio_dma_queue_is_complete(&equeue->dma_queue,
  1365. completion_slot, update);
  1366. }
  1367. /*****************************************************************
  1368. * Link Management *
  1369. ******************************************************************/
  1370. /*
  1371. *
  1372. * Functions for manipulating and sensing the state and configuration
  1373. * of physical network links.
  1374. *
  1375. * @section gxio_mpipe_link_perm Link Permissions
  1376. *
  1377. * Opening a link (with gxio_mpipe_link_open()) requests a set of link
  1378. * permissions, which control what may be done with the link, and potentially
  1379. * what permissions may be granted to other processes.
  1380. *
  1381. * Data permission allows the process to receive packets from the link by
  1382. * specifying the link's channel number in mPIPE packet distribution rules,
  1383. * and to send packets to the link by using the link's channel number as
  1384. * the target for an eDMA ring.
  1385. *
  1386. * Stats permission allows the process to retrieve link attributes (such as
  1387. * the speeds it is capable of running at, or whether it is currently up), and
  1388. * to read and write certain statistics-related registers in the link's MAC.
  1389. *
  1390. * Control permission allows the process to retrieve and modify link attributes
  1391. * (so that it may, for example, bring the link up and take it down), and
  1392. * read and write many registers in the link's MAC and PHY.
  1393. *
  1394. * Any permission may be requested as shared, which allows other processes
  1395. * to also request shared permission, or exclusive, which prevents other
  1396. * processes from requesting it. In keeping with GXIO's typical usage in
  1397. * an embedded environment, the defaults for all permissions are shared.
  1398. *
  1399. * Permissions are granted on a first-come, first-served basis, so if two
  1400. * applications request an exclusive permission on the same link, the one
  1401. * to run first will win. Note, however, that some system components, like
  1402. * the kernel Ethernet driver, may get an opportunity to open links before
  1403. * any applications run.
  1404. *
  1405. * @section gxio_mpipe_link_names Link Names
  1406. *
  1407. * Link names are of the form gbe<em>number</em> (for Gigabit Ethernet),
  1408. * xgbe<em>number</em> (for 10 Gigabit Ethernet), loop<em>number</em> (for
  1409. * internal mPIPE loopback), or ilk<em>number</em>/<em>channel</em>
  1410. * (for Interlaken links); for instance, gbe0, xgbe1, loop3, and
  1411. * ilk0/12 are all possible link names. The correspondence between
  1412. * the link name and an mPIPE instance number or mPIPE channel number is
  1413. * system-dependent; all links will not exist on all systems, and the set
  1414. * of numbers used for a particular link type may not start at zero and may
  1415. * not be contiguous. Use gxio_mpipe_link_enumerate() to retrieve the set of
  1416. * links which exist on a system, and always use gxio_mpipe_link_instance()
  1417. * to determine which mPIPE controls a particular link.
  1418. *
  1419. * Note that in some cases, links may share hardware, such as PHYs, or
  1420. * internal mPIPE buffers; in these cases, only one of the links may be
  1421. * opened at a time. This is especially common with xgbe and gbe ports,
  1422. * since each xgbe port uses 4 SERDES lanes, each of which may also be
  1423. * configured as one gbe port.
  1424. *
  1425. * @section gxio_mpipe_link_states Link States
  1426. *
  1427. * The mPIPE link management model revolves around three different states,
  1428. * which are maintained for each link:
  1429. *
  1430. * 1. The <em>current</em> link state: is the link up now, and if so, at
  1431. * what speed?
  1432. *
  1433. * 2. The <em>desired</em> link state: what do we want the link state to be?
  1434. * The system is always working to make this state the current state;
  1435. * thus, if the desired state is up, and the link is down, we'll be
  1436. * constantly trying to bring it up, automatically.
  1437. *
  1438. * 3. The <em>possible</em> link state: what speeds are valid for this
  1439. * particular link? Or, in other words, what are the capabilities of
  1440. * the link hardware?
  1441. *
  1442. * These link states are not, strictly speaking, related to application
  1443. * state; they may be manipulated at any time, whether or not the link
  1444. * is currently being used for data transfer. However, for convenience,
  1445. * gxio_mpipe_link_open() and gxio_mpipe_link_close() (or application exit)
  1446. * can affect the link state. These implicit link management operations
  1447. * may be modified or disabled by the use of link open flags.
  1448. *
  1449. * From an application, you can use gxio_mpipe_link_get_attr()
  1450. * and gxio_mpipe_link_set_attr() to manipulate the link states.
  1451. * gxio_mpipe_link_get_attr() with ::GXIO_MPIPE_LINK_POSSIBLE_STATE
  1452. * gets you the possible link state. gxio_mpipe_link_get_attr() with
  1453. * ::GXIO_MPIPE_LINK_CURRENT_STATE gets you the current link state.
  1454. * Finally, gxio_mpipe_link_set_attr() and gxio_mpipe_link_get_attr()
  1455. * with ::GXIO_MPIPE_LINK_DESIRED_STATE allow you to modify or retrieve
  1456. * the desired link state.
  1457. *
  1458. * If you want to manage a link from a part of your application which isn't
  1459. * involved in packet processing, you can use the ::GXIO_MPIPE_LINK_NO_DATA
  1460. * flags on a gxio_mpipe_link_open() call. This opens the link, but does
  1461. * not request data permission, so it does not conflict with any exclusive
  1462. * permissions which may be held by other processes. You can then can use
  1463. * gxio_mpipe_link_get_attr() and gxio_mpipe_link_set_attr() on this link
  1464. * object to bring up or take down the link.
  1465. *
  1466. * Some links support link state bits which support various loopback
  1467. * modes. ::GXIO_MPIPE_LINK_LOOP_MAC tests datapaths within the Tile
  1468. * Processor itself; ::GXIO_MPIPE_LINK_LOOP_PHY tests the datapath between
  1469. * the Tile Processor and the external physical layer interface chip; and
  1470. * ::GXIO_MPIPE_LINK_LOOP_EXT tests the entire network datapath with the
  1471. * aid of an external loopback connector. In addition to enabling hardware
  1472. * testing, such configuration can be useful for software testing, as well.
  1473. *
  1474. * When LOOP_MAC or LOOP_PHY is enabled, packets transmitted on a channel
  1475. * will be received by that channel, instead of being emitted on the
  1476. * physical link, and packets received on the physical link will be ignored.
  1477. * Other than that, all standard GXIO operations work as you might expect.
  1478. * Note that loopback operation requires that the link be brought up using
  1479. * one or more of the GXIO_MPIPE_LINK_SPEED_xxx link state bits.
  1480. *
  1481. * Those familiar with previous versions of the MDE on TILEPro hardware
  1482. * will notice significant similarities between the NetIO link management
  1483. * model and the mPIPE link management model. However, the NetIO model
  1484. * was developed in stages, and some of its features -- for instance,
  1485. * the default setting of certain flags -- were shaped by the need to be
  1486. * compatible with previous versions of NetIO. Since the features provided
  1487. * by the mPIPE hardware and the mPIPE GXIO library are significantly
  1488. * different than those provided by NetIO, in some cases, we have made
  1489. * different choices in the mPIPE link management API. Thus, please read
  1490. * this documentation carefully before assuming that mPIPE link management
  1491. * operations are exactly equivalent to their NetIO counterparts.
  1492. */
  1493. /* An object used to manage mPIPE link state and resources. */
  1494. typedef struct {
  1495. /* The overall mPIPE context. */
  1496. gxio_mpipe_context_t *context;
  1497. /* The channel number used by this link. */
  1498. uint8_t channel;
  1499. /* The MAC index used by this link. */
  1500. uint8_t mac;
  1501. } gxio_mpipe_link_t;
  1502. /* Retrieve one of this system's legal link names, and its MAC address.
  1503. *
  1504. * @param index Link name index. If a system supports N legal link names,
  1505. * then indices between 0 and N - 1, inclusive, each correspond to one of
  1506. * those names. Thus, to retrieve all of a system's legal link names,
  1507. * call this function in a loop, starting with an index of zero, and
  1508. * incrementing it once per iteration until -1 is returned.
  1509. * @param link_name Pointer to the buffer which will receive the retrieved
  1510. * link name. The buffer should contain space for at least
  1511. * ::GXIO_MPIPE_LINK_NAME_LEN bytes; the returned name, including the
  1512. * terminating null byte, will be no longer than that.
  1513. * @param link_name Pointer to the buffer which will receive the retrieved
  1514. * MAC address. The buffer should contain space for at least 6 bytes.
  1515. * @return Zero if a link name was successfully retrieved; -1 if one was
  1516. * not.
  1517. */
  1518. extern int gxio_mpipe_link_enumerate_mac(int index, char *link_name,
  1519. uint8_t *mac_addr);
  1520. /* Open an mPIPE link.
  1521. *
  1522. * A link must be opened before it may be used to send or receive packets,
  1523. * and before its state may be examined or changed. Depending up on the
  1524. * link's intended use, one or more link permissions may be requested via
  1525. * the flags parameter; see @ref gxio_mpipe_link_perm. In addition, flags
  1526. * may request that the link's state be modified at open time. See @ref
  1527. * gxio_mpipe_link_states and @ref gxio_mpipe_link_open_flags for more detail.
  1528. *
  1529. * @param link A link state object, which will be initialized if this
  1530. * function completes successfully.
  1531. * @param context An initialized mPIPE context.
  1532. * @param link_name Name of the link.
  1533. * @param flags Zero or more @ref gxio_mpipe_link_open_flags, ORed together.
  1534. * @return 0 if the link was successfully opened, or a negative error code.
  1535. *
  1536. */
  1537. extern int gxio_mpipe_link_open(gxio_mpipe_link_t *link,
  1538. gxio_mpipe_context_t *context,
  1539. const char *link_name, unsigned int flags);
  1540. /* Close an mPIPE link.
  1541. *
  1542. * Closing a link makes it available for use by other processes. Once
  1543. * a link has been closed, packets may no longer be sent on or received
  1544. * from the link, and its state may not be examined or changed.
  1545. *
  1546. * @param link A link state object, which will no longer be initialized
  1547. * if this function completes successfully.
  1548. * @return 0 if the link was successfully closed, or a negative error code.
  1549. *
  1550. */
  1551. extern int gxio_mpipe_link_close(gxio_mpipe_link_t *link);
  1552. /* Return a link's channel number.
  1553. *
  1554. * @param link A properly initialized link state object.
  1555. * @return The channel number for the link.
  1556. */
  1557. static inline int gxio_mpipe_link_channel(gxio_mpipe_link_t *link)
  1558. {
  1559. return link->channel;
  1560. }
  1561. ///////////////////////////////////////////////////////////////////
  1562. // Timestamp //
  1563. ///////////////////////////////////////////////////////////////////
  1564. /* Get the timestamp of mPIPE when this routine is called.
  1565. *
  1566. * @param context An initialized mPIPE context.
  1567. * @param ts A timespec structure to store the current clock.
  1568. * @return If the call was successful, zero; otherwise, a negative error
  1569. * code.
  1570. */
  1571. extern int gxio_mpipe_get_timestamp(gxio_mpipe_context_t *context,
  1572. struct timespec *ts);
  1573. /* Set the timestamp of mPIPE.
  1574. *
  1575. * @param context An initialized mPIPE context.
  1576. * @param ts A timespec structure to store the requested clock.
  1577. * @return If the call was successful, zero; otherwise, a negative error
  1578. * code.
  1579. */
  1580. extern int gxio_mpipe_set_timestamp(gxio_mpipe_context_t *context,
  1581. const struct timespec *ts);
  1582. /* Adjust the timestamp of mPIPE.
  1583. *
  1584. * @param context An initialized mPIPE context.
  1585. * @param delta A signed time offset to adjust, in nanoseconds.
  1586. * The absolute value of this parameter must be less than or
  1587. * equal to 1000000000.
  1588. * @return If the call was successful, zero; otherwise, a negative error
  1589. * code.
  1590. */
  1591. extern int gxio_mpipe_adjust_timestamp(gxio_mpipe_context_t *context,
  1592. int64_t delta);
  1593. #endif /* !_GXIO_MPIPE_H_ */