tilegx.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279
  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. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h> /* printk() */
  19. #include <linux/slab.h> /* kmalloc() */
  20. #include <linux/errno.h> /* error codes */
  21. #include <linux/types.h> /* size_t */
  22. #include <linux/interrupt.h>
  23. #include <linux/in.h>
  24. #include <linux/irq.h>
  25. #include <linux/netdevice.h> /* struct device, and other headers */
  26. #include <linux/etherdevice.h> /* eth_type_trans */
  27. #include <linux/skbuff.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/cdev.h>
  30. #include <linux/hugetlb.h>
  31. #include <linux/in6.h>
  32. #include <linux/timer.h>
  33. #include <linux/hrtimer.h>
  34. #include <linux/ktime.h>
  35. #include <linux/io.h>
  36. #include <linux/ctype.h>
  37. #include <linux/ip.h>
  38. #include <linux/ipv6.h>
  39. #include <linux/tcp.h>
  40. #include <linux/net_tstamp.h>
  41. #include <linux/ptp_clock_kernel.h>
  42. #include <linux/tick.h>
  43. #include <asm/checksum.h>
  44. #include <asm/homecache.h>
  45. #include <gxio/mpipe.h>
  46. #include <arch/sim.h>
  47. /* Default transmit lockup timeout period, in jiffies. */
  48. #define TILE_NET_TIMEOUT (5 * HZ)
  49. /* The maximum number of distinct channels (idesc.channel is 5 bits). */
  50. #define TILE_NET_CHANNELS 32
  51. /* Maximum number of idescs to handle per "poll". */
  52. #define TILE_NET_BATCH 128
  53. /* Maximum number of packets to handle per "poll". */
  54. #define TILE_NET_WEIGHT 64
  55. /* Maximum Jumbo Packet MTU */
  56. #define TILE_JUMBO_MAX_MTU 9000
  57. /* Number of entries in each iqueue. */
  58. #define IQUEUE_ENTRIES 512
  59. /* Number of entries in each equeue. */
  60. #define EQUEUE_ENTRIES 2048
  61. /* Total header bytes per equeue slot. Must be big enough for 2 bytes
  62. * of NET_IP_ALIGN alignment, plus 14 bytes (?) of L2 header, plus up to
  63. * 60 bytes of actual TCP header. We round up to align to cache lines.
  64. */
  65. #define HEADER_BYTES 128
  66. /* Maximum completions per cpu per device (must be a power of two).
  67. * ISSUE: What is the right number here? If this is too small, then
  68. * egress might block waiting for free space in a completions array.
  69. * ISSUE: At the least, allocate these only for initialized echannels.
  70. */
  71. #define TILE_NET_MAX_COMPS 64
  72. #define MAX_FRAGS (MAX_SKB_FRAGS + 1)
  73. /* The "kinds" of buffer stacks (small/large/jumbo). */
  74. #define MAX_KINDS 3
  75. /* Size of completions data to allocate.
  76. * ISSUE: Probably more than needed since we don't use all the channels.
  77. */
  78. #define COMPS_SIZE (TILE_NET_CHANNELS * sizeof(struct tile_net_comps))
  79. /* Size of NotifRing data to allocate. */
  80. #define NOTIF_RING_SIZE (IQUEUE_ENTRIES * sizeof(gxio_mpipe_idesc_t))
  81. /* Timeout to wake the per-device TX timer after we stop the queue.
  82. * We don't want the timeout too short (adds overhead, and might end
  83. * up causing stop/wake/stop/wake cycles) or too long (affects performance).
  84. * For the 10 Gb NIC, 30 usec means roughly 30+ 1500-byte packets.
  85. */
  86. #define TX_TIMER_DELAY_USEC 30
  87. /* Timeout to wake the per-cpu egress timer to free completions. */
  88. #define EGRESS_TIMER_DELAY_USEC 1000
  89. MODULE_AUTHOR("Tilera Corporation");
  90. MODULE_LICENSE("GPL");
  91. /* A "packet fragment" (a chunk of memory). */
  92. struct frag {
  93. void *buf;
  94. size_t length;
  95. };
  96. /* A single completion. */
  97. struct tile_net_comp {
  98. /* The "complete_count" when the completion will be complete. */
  99. s64 when;
  100. /* The buffer to be freed when the completion is complete. */
  101. struct sk_buff *skb;
  102. };
  103. /* The completions for a given cpu and echannel. */
  104. struct tile_net_comps {
  105. /* The completions. */
  106. struct tile_net_comp comp_queue[TILE_NET_MAX_COMPS];
  107. /* The number of completions used. */
  108. unsigned long comp_next;
  109. /* The number of completions freed. */
  110. unsigned long comp_last;
  111. };
  112. /* The transmit wake timer for a given cpu and echannel. */
  113. struct tile_net_tx_wake {
  114. int tx_queue_idx;
  115. struct hrtimer timer;
  116. struct net_device *dev;
  117. };
  118. /* Info for a specific cpu. */
  119. struct tile_net_info {
  120. /* Our cpu. */
  121. int my_cpu;
  122. /* A timer for handling egress completions. */
  123. struct hrtimer egress_timer;
  124. /* True if "egress_timer" is scheduled. */
  125. bool egress_timer_scheduled;
  126. struct info_mpipe {
  127. /* Packet queue. */
  128. gxio_mpipe_iqueue_t iqueue;
  129. /* The NAPI struct. */
  130. struct napi_struct napi;
  131. /* Number of buffers (by kind) which must still be provided. */
  132. unsigned int num_needed_buffers[MAX_KINDS];
  133. /* instance id. */
  134. int instance;
  135. /* True if iqueue is valid. */
  136. bool has_iqueue;
  137. /* NAPI flags. */
  138. bool napi_added;
  139. bool napi_enabled;
  140. /* Comps for each egress channel. */
  141. struct tile_net_comps *comps_for_echannel[TILE_NET_CHANNELS];
  142. /* Transmit wake timer for each egress channel. */
  143. struct tile_net_tx_wake tx_wake[TILE_NET_CHANNELS];
  144. } mpipe[NR_MPIPE_MAX];
  145. };
  146. /* Info for egress on a particular egress channel. */
  147. struct tile_net_egress {
  148. /* The "equeue". */
  149. gxio_mpipe_equeue_t *equeue;
  150. /* The headers for TSO. */
  151. unsigned char *headers;
  152. };
  153. /* Info for a specific device. */
  154. struct tile_net_priv {
  155. /* Our network device. */
  156. struct net_device *dev;
  157. /* The primary link. */
  158. gxio_mpipe_link_t link;
  159. /* The primary channel, if open, else -1. */
  160. int channel;
  161. /* The "loopify" egress link, if needed. */
  162. gxio_mpipe_link_t loopify_link;
  163. /* The "loopify" egress channel, if open, else -1. */
  164. int loopify_channel;
  165. /* The egress channel (channel or loopify_channel). */
  166. int echannel;
  167. /* mPIPE instance, 0 or 1. */
  168. int instance;
  169. /* The timestamp config. */
  170. struct hwtstamp_config stamp_cfg;
  171. };
  172. static struct mpipe_data {
  173. /* The ingress irq. */
  174. int ingress_irq;
  175. /* The "context" for all devices. */
  176. gxio_mpipe_context_t context;
  177. /* Egress info, indexed by "priv->echannel"
  178. * (lazily created as needed).
  179. */
  180. struct tile_net_egress
  181. egress_for_echannel[TILE_NET_CHANNELS];
  182. /* Devices currently associated with each channel.
  183. * NOTE: The array entry can become NULL after ifconfig down, but
  184. * we do not free the underlying net_device structures, so it is
  185. * safe to use a pointer after reading it from this array.
  186. */
  187. struct net_device
  188. *tile_net_devs_for_channel[TILE_NET_CHANNELS];
  189. /* The actual memory allocated for the buffer stacks. */
  190. void *buffer_stack_vas[MAX_KINDS];
  191. /* The amount of memory allocated for each buffer stack. */
  192. size_t buffer_stack_bytes[MAX_KINDS];
  193. /* The first buffer stack index
  194. * (small = +0, large = +1, jumbo = +2).
  195. */
  196. int first_buffer_stack;
  197. /* The buckets. */
  198. int first_bucket;
  199. int num_buckets;
  200. /* PTP-specific data. */
  201. struct ptp_clock *ptp_clock;
  202. struct ptp_clock_info caps;
  203. /* Lock for ptp accessors. */
  204. struct mutex ptp_lock;
  205. } mpipe_data[NR_MPIPE_MAX] = {
  206. [0 ... (NR_MPIPE_MAX - 1)] {
  207. .ingress_irq = -1,
  208. .first_buffer_stack = -1,
  209. .first_bucket = -1,
  210. .num_buckets = 1
  211. }
  212. };
  213. /* A mutex for "tile_net_devs_for_channel". */
  214. static DEFINE_MUTEX(tile_net_devs_for_channel_mutex);
  215. /* The per-cpu info. */
  216. static DEFINE_PER_CPU(struct tile_net_info, per_cpu_info);
  217. /* The buffer size enums for each buffer stack.
  218. * See arch/tile/include/gxio/mpipe.h for the set of possible values.
  219. * We avoid the "10384" size because it can induce "false chaining"
  220. * on "cut-through" jumbo packets.
  221. */
  222. static gxio_mpipe_buffer_size_enum_t buffer_size_enums[MAX_KINDS] = {
  223. GXIO_MPIPE_BUFFER_SIZE_128,
  224. GXIO_MPIPE_BUFFER_SIZE_1664,
  225. GXIO_MPIPE_BUFFER_SIZE_16384
  226. };
  227. /* Text value of tile_net.cpus if passed as a module parameter. */
  228. static char *network_cpus_string;
  229. /* The actual cpus in "network_cpus". */
  230. static struct cpumask network_cpus_map;
  231. /* If "tile_net.loopify=LINK" was specified, this is "LINK". */
  232. static char *loopify_link_name;
  233. /* If "tile_net.custom" was specified, this is true. */
  234. static bool custom_flag;
  235. /* If "tile_net.jumbo=NUM" was specified, this is "NUM". */
  236. static uint jumbo_num;
  237. /* Obtain mpipe instance from struct tile_net_priv given struct net_device. */
  238. static inline int mpipe_instance(struct net_device *dev)
  239. {
  240. struct tile_net_priv *priv = netdev_priv(dev);
  241. return priv->instance;
  242. }
  243. /* The "tile_net.cpus" argument specifies the cpus that are dedicated
  244. * to handle ingress packets.
  245. *
  246. * The parameter should be in the form "tile_net.cpus=m-n[,x-y]", where
  247. * m, n, x, y are integer numbers that represent the cpus that can be
  248. * neither a dedicated cpu nor a dataplane cpu.
  249. */
  250. static bool network_cpus_init(void)
  251. {
  252. int rc;
  253. if (network_cpus_string == NULL)
  254. return false;
  255. rc = cpulist_parse_crop(network_cpus_string, &network_cpus_map);
  256. if (rc != 0) {
  257. pr_warn("tile_net.cpus=%s: malformed cpu list\n",
  258. network_cpus_string);
  259. return false;
  260. }
  261. /* Remove dedicated cpus. */
  262. cpumask_and(&network_cpus_map, &network_cpus_map, cpu_possible_mask);
  263. if (cpumask_empty(&network_cpus_map)) {
  264. pr_warn("Ignoring empty tile_net.cpus='%s'.\n",
  265. network_cpus_string);
  266. return false;
  267. }
  268. pr_info("Linux network CPUs: %*pbl\n",
  269. cpumask_pr_args(&network_cpus_map));
  270. return true;
  271. }
  272. module_param_named(cpus, network_cpus_string, charp, 0444);
  273. MODULE_PARM_DESC(cpus, "cpulist of cores that handle network interrupts");
  274. /* The "tile_net.loopify=LINK" argument causes the named device to
  275. * actually use "loop0" for ingress, and "loop1" for egress. This
  276. * allows an app to sit between the actual link and linux, passing
  277. * (some) packets along to linux, and forwarding (some) packets sent
  278. * out by linux.
  279. */
  280. module_param_named(loopify, loopify_link_name, charp, 0444);
  281. MODULE_PARM_DESC(loopify, "name the device to use loop0/1 for ingress/egress");
  282. /* The "tile_net.custom" argument causes us to ignore the "conventional"
  283. * classifier metadata, in particular, the "l2_offset".
  284. */
  285. module_param_named(custom, custom_flag, bool, 0444);
  286. MODULE_PARM_DESC(custom, "indicates a (heavily) customized classifier");
  287. /* The "tile_net.jumbo" argument causes us to support "jumbo" packets,
  288. * and to allocate the given number of "jumbo" buffers.
  289. */
  290. module_param_named(jumbo, jumbo_num, uint, 0444);
  291. MODULE_PARM_DESC(jumbo, "the number of buffers to support jumbo packets");
  292. /* Atomically update a statistics field.
  293. * Note that on TILE-Gx, this operation is fire-and-forget on the
  294. * issuing core (single-cycle dispatch) and takes only a few cycles
  295. * longer than a regular store when the request reaches the home cache.
  296. * No expensive bus management overhead is required.
  297. */
  298. static void tile_net_stats_add(unsigned long value, unsigned long *field)
  299. {
  300. BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(unsigned long));
  301. atomic_long_add(value, (atomic_long_t *)field);
  302. }
  303. /* Allocate and push a buffer. */
  304. static bool tile_net_provide_buffer(int instance, int kind)
  305. {
  306. struct mpipe_data *md = &mpipe_data[instance];
  307. gxio_mpipe_buffer_size_enum_t bse = buffer_size_enums[kind];
  308. size_t bs = gxio_mpipe_buffer_size_enum_to_buffer_size(bse);
  309. const unsigned long buffer_alignment = 128;
  310. struct sk_buff *skb;
  311. int len;
  312. len = sizeof(struct sk_buff **) + buffer_alignment + bs;
  313. skb = dev_alloc_skb(len);
  314. if (skb == NULL)
  315. return false;
  316. /* Make room for a back-pointer to 'skb' and guarantee alignment. */
  317. skb_reserve(skb, sizeof(struct sk_buff **));
  318. skb_reserve(skb, -(long)skb->data & (buffer_alignment - 1));
  319. /* Save a back-pointer to 'skb'. */
  320. *(struct sk_buff **)(skb->data - sizeof(struct sk_buff **)) = skb;
  321. /* Make sure "skb" and the back-pointer have been flushed. */
  322. wmb();
  323. gxio_mpipe_push_buffer(&md->context, md->first_buffer_stack + kind,
  324. (void *)va_to_tile_io_addr(skb->data));
  325. return true;
  326. }
  327. /* Convert a raw mpipe buffer to its matching skb pointer. */
  328. static struct sk_buff *mpipe_buf_to_skb(void *va)
  329. {
  330. /* Acquire the associated "skb". */
  331. struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
  332. struct sk_buff *skb = *skb_ptr;
  333. /* Paranoia. */
  334. if (skb->data != va) {
  335. /* Panic here since there's a reasonable chance
  336. * that corrupt buffers means generic memory
  337. * corruption, with unpredictable system effects.
  338. */
  339. panic("Corrupt linux buffer! va=%p, skb=%p, skb->data=%p",
  340. va, skb, skb->data);
  341. }
  342. return skb;
  343. }
  344. static void tile_net_pop_all_buffers(int instance, int stack)
  345. {
  346. struct mpipe_data *md = &mpipe_data[instance];
  347. for (;;) {
  348. tile_io_addr_t addr =
  349. (tile_io_addr_t)gxio_mpipe_pop_buffer(&md->context,
  350. stack);
  351. if (addr == 0)
  352. break;
  353. dev_kfree_skb_irq(mpipe_buf_to_skb(tile_io_addr_to_va(addr)));
  354. }
  355. }
  356. /* Provide linux buffers to mPIPE. */
  357. static void tile_net_provide_needed_buffers(void)
  358. {
  359. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  360. int instance, kind;
  361. for (instance = 0; instance < NR_MPIPE_MAX &&
  362. info->mpipe[instance].has_iqueue; instance++) {
  363. for (kind = 0; kind < MAX_KINDS; kind++) {
  364. while (info->mpipe[instance].num_needed_buffers[kind]
  365. != 0) {
  366. if (!tile_net_provide_buffer(instance, kind)) {
  367. pr_notice("Tile %d still needs"
  368. " some buffers\n",
  369. info->my_cpu);
  370. return;
  371. }
  372. info->mpipe[instance].
  373. num_needed_buffers[kind]--;
  374. }
  375. }
  376. }
  377. }
  378. /* Get RX timestamp, and store it in the skb. */
  379. static void tile_rx_timestamp(struct tile_net_priv *priv, struct sk_buff *skb,
  380. gxio_mpipe_idesc_t *idesc)
  381. {
  382. if (unlikely(priv->stamp_cfg.rx_filter != HWTSTAMP_FILTER_NONE)) {
  383. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  384. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  385. shhwtstamps->hwtstamp = ktime_set(idesc->time_stamp_sec,
  386. idesc->time_stamp_ns);
  387. }
  388. }
  389. /* Get TX timestamp, and store it in the skb. */
  390. static void tile_tx_timestamp(struct sk_buff *skb, int instance)
  391. {
  392. struct skb_shared_info *shtx = skb_shinfo(skb);
  393. if (unlikely((shtx->tx_flags & SKBTX_HW_TSTAMP) != 0)) {
  394. struct mpipe_data *md = &mpipe_data[instance];
  395. struct skb_shared_hwtstamps shhwtstamps;
  396. struct timespec64 ts;
  397. shtx->tx_flags |= SKBTX_IN_PROGRESS;
  398. gxio_mpipe_get_timestamp(&md->context, &ts);
  399. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  400. shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
  401. skb_tstamp_tx(skb, &shhwtstamps);
  402. }
  403. }
  404. /* Use ioctl() to enable or disable TX or RX timestamping. */
  405. static int tile_hwtstamp_set(struct net_device *dev, struct ifreq *rq)
  406. {
  407. struct hwtstamp_config config;
  408. struct tile_net_priv *priv = netdev_priv(dev);
  409. if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
  410. return -EFAULT;
  411. if (config.flags) /* reserved for future extensions */
  412. return -EINVAL;
  413. switch (config.tx_type) {
  414. case HWTSTAMP_TX_OFF:
  415. case HWTSTAMP_TX_ON:
  416. break;
  417. default:
  418. return -ERANGE;
  419. }
  420. switch (config.rx_filter) {
  421. case HWTSTAMP_FILTER_NONE:
  422. break;
  423. case HWTSTAMP_FILTER_ALL:
  424. case HWTSTAMP_FILTER_SOME:
  425. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  426. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  427. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  428. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  429. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  430. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  431. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  432. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  433. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  434. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  435. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  436. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  437. case HWTSTAMP_FILTER_NTP_ALL:
  438. config.rx_filter = HWTSTAMP_FILTER_ALL;
  439. break;
  440. default:
  441. return -ERANGE;
  442. }
  443. if (copy_to_user(rq->ifr_data, &config, sizeof(config)))
  444. return -EFAULT;
  445. priv->stamp_cfg = config;
  446. return 0;
  447. }
  448. static int tile_hwtstamp_get(struct net_device *dev, struct ifreq *rq)
  449. {
  450. struct tile_net_priv *priv = netdev_priv(dev);
  451. if (copy_to_user(rq->ifr_data, &priv->stamp_cfg,
  452. sizeof(priv->stamp_cfg)))
  453. return -EFAULT;
  454. return 0;
  455. }
  456. static inline bool filter_packet(struct net_device *dev, void *buf)
  457. {
  458. /* Filter packets received before we're up. */
  459. if (dev == NULL || !(dev->flags & IFF_UP))
  460. return true;
  461. /* Filter out packets that aren't for us. */
  462. if (!(dev->flags & IFF_PROMISC) &&
  463. !is_multicast_ether_addr(buf) &&
  464. !ether_addr_equal(dev->dev_addr, buf))
  465. return true;
  466. return false;
  467. }
  468. static void tile_net_receive_skb(struct net_device *dev, struct sk_buff *skb,
  469. gxio_mpipe_idesc_t *idesc, unsigned long len)
  470. {
  471. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  472. struct tile_net_priv *priv = netdev_priv(dev);
  473. int instance = priv->instance;
  474. /* Encode the actual packet length. */
  475. skb_put(skb, len);
  476. skb->protocol = eth_type_trans(skb, dev);
  477. /* Acknowledge "good" hardware checksums. */
  478. if (idesc->cs && idesc->csum_seed_val == 0xFFFF)
  479. skb->ip_summed = CHECKSUM_UNNECESSARY;
  480. /* Get RX timestamp from idesc. */
  481. tile_rx_timestamp(priv, skb, idesc);
  482. napi_gro_receive(&info->mpipe[instance].napi, skb);
  483. /* Update stats. */
  484. tile_net_stats_add(1, &dev->stats.rx_packets);
  485. tile_net_stats_add(len, &dev->stats.rx_bytes);
  486. /* Need a new buffer. */
  487. if (idesc->size == buffer_size_enums[0])
  488. info->mpipe[instance].num_needed_buffers[0]++;
  489. else if (idesc->size == buffer_size_enums[1])
  490. info->mpipe[instance].num_needed_buffers[1]++;
  491. else
  492. info->mpipe[instance].num_needed_buffers[2]++;
  493. }
  494. /* Handle a packet. Return true if "processed", false if "filtered". */
  495. static bool tile_net_handle_packet(int instance, gxio_mpipe_idesc_t *idesc)
  496. {
  497. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  498. struct mpipe_data *md = &mpipe_data[instance];
  499. struct net_device *dev = md->tile_net_devs_for_channel[idesc->channel];
  500. uint8_t l2_offset;
  501. void *va;
  502. void *buf;
  503. unsigned long len;
  504. bool filter;
  505. /* Drop packets for which no buffer was available (which can
  506. * happen under heavy load), or for which the me/tr/ce flags
  507. * are set (which can happen for jumbo cut-through packets,
  508. * or with a customized classifier).
  509. */
  510. if (idesc->be || idesc->me || idesc->tr || idesc->ce) {
  511. if (dev)
  512. tile_net_stats_add(1, &dev->stats.rx_errors);
  513. goto drop;
  514. }
  515. /* Get the "l2_offset", if allowed. */
  516. l2_offset = custom_flag ? 0 : gxio_mpipe_idesc_get_l2_offset(idesc);
  517. /* Get the VA (including NET_IP_ALIGN bytes of "headroom"). */
  518. va = tile_io_addr_to_va((unsigned long)idesc->va);
  519. /* Get the actual packet start/length. */
  520. buf = va + l2_offset;
  521. len = idesc->l2_size - l2_offset;
  522. /* Point "va" at the raw buffer. */
  523. va -= NET_IP_ALIGN;
  524. filter = filter_packet(dev, buf);
  525. if (filter) {
  526. if (dev)
  527. tile_net_stats_add(1, &dev->stats.rx_dropped);
  528. drop:
  529. gxio_mpipe_iqueue_drop(&info->mpipe[instance].iqueue, idesc);
  530. } else {
  531. struct sk_buff *skb = mpipe_buf_to_skb(va);
  532. /* Skip headroom, and any custom header. */
  533. skb_reserve(skb, NET_IP_ALIGN + l2_offset);
  534. tile_net_receive_skb(dev, skb, idesc, len);
  535. }
  536. gxio_mpipe_iqueue_consume(&info->mpipe[instance].iqueue, idesc);
  537. return !filter;
  538. }
  539. /* Handle some packets for the current CPU.
  540. *
  541. * This function handles up to TILE_NET_BATCH idescs per call.
  542. *
  543. * ISSUE: Since we do not provide new buffers until this function is
  544. * complete, we must initially provide enough buffers for each network
  545. * cpu to fill its iqueue and also its batched idescs.
  546. *
  547. * ISSUE: The "rotting packet" race condition occurs if a packet
  548. * arrives after the queue appears to be empty, and before the
  549. * hypervisor interrupt is re-enabled.
  550. */
  551. static int tile_net_poll(struct napi_struct *napi, int budget)
  552. {
  553. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  554. unsigned int work = 0;
  555. gxio_mpipe_idesc_t *idesc;
  556. int instance, i, n;
  557. struct mpipe_data *md;
  558. struct info_mpipe *info_mpipe =
  559. container_of(napi, struct info_mpipe, napi);
  560. if (budget <= 0)
  561. goto done;
  562. instance = info_mpipe->instance;
  563. while ((n = gxio_mpipe_iqueue_try_peek(
  564. &info_mpipe->iqueue,
  565. &idesc)) > 0) {
  566. for (i = 0; i < n; i++) {
  567. if (i == TILE_NET_BATCH)
  568. goto done;
  569. if (tile_net_handle_packet(instance,
  570. idesc + i)) {
  571. if (++work >= budget)
  572. goto done;
  573. }
  574. }
  575. }
  576. /* There are no packets left. */
  577. napi_complete_done(&info_mpipe->napi, work);
  578. md = &mpipe_data[instance];
  579. /* Re-enable hypervisor interrupts. */
  580. gxio_mpipe_enable_notif_ring_interrupt(
  581. &md->context, info->mpipe[instance].iqueue.ring);
  582. /* HACK: Avoid the "rotting packet" problem. */
  583. if (gxio_mpipe_iqueue_try_peek(&info_mpipe->iqueue, &idesc) > 0)
  584. napi_schedule(&info_mpipe->napi);
  585. /* ISSUE: Handle completions? */
  586. done:
  587. tile_net_provide_needed_buffers();
  588. return work;
  589. }
  590. /* Handle an ingress interrupt from an instance on the current cpu. */
  591. static irqreturn_t tile_net_handle_ingress_irq(int irq, void *id)
  592. {
  593. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  594. napi_schedule(&info->mpipe[(uint64_t)id].napi);
  595. return IRQ_HANDLED;
  596. }
  597. /* Free some completions. This must be called with interrupts blocked. */
  598. static int tile_net_free_comps(gxio_mpipe_equeue_t *equeue,
  599. struct tile_net_comps *comps,
  600. int limit, bool force_update)
  601. {
  602. int n = 0;
  603. while (comps->comp_last < comps->comp_next) {
  604. unsigned int cid = comps->comp_last % TILE_NET_MAX_COMPS;
  605. struct tile_net_comp *comp = &comps->comp_queue[cid];
  606. if (!gxio_mpipe_equeue_is_complete(equeue, comp->when,
  607. force_update || n == 0))
  608. break;
  609. dev_kfree_skb_irq(comp->skb);
  610. comps->comp_last++;
  611. if (++n == limit)
  612. break;
  613. }
  614. return n;
  615. }
  616. /* Add a completion. This must be called with interrupts blocked.
  617. * tile_net_equeue_try_reserve() will have ensured a free completion entry.
  618. */
  619. static void add_comp(gxio_mpipe_equeue_t *equeue,
  620. struct tile_net_comps *comps,
  621. uint64_t when, struct sk_buff *skb)
  622. {
  623. int cid = comps->comp_next % TILE_NET_MAX_COMPS;
  624. comps->comp_queue[cid].when = when;
  625. comps->comp_queue[cid].skb = skb;
  626. comps->comp_next++;
  627. }
  628. static void tile_net_schedule_tx_wake_timer(struct net_device *dev,
  629. int tx_queue_idx)
  630. {
  631. struct tile_net_info *info = &per_cpu(per_cpu_info, tx_queue_idx);
  632. struct tile_net_priv *priv = netdev_priv(dev);
  633. int instance = priv->instance;
  634. struct tile_net_tx_wake *tx_wake =
  635. &info->mpipe[instance].tx_wake[priv->echannel];
  636. hrtimer_start(&tx_wake->timer,
  637. TX_TIMER_DELAY_USEC * 1000UL,
  638. HRTIMER_MODE_REL_PINNED);
  639. }
  640. static enum hrtimer_restart tile_net_handle_tx_wake_timer(struct hrtimer *t)
  641. {
  642. struct tile_net_tx_wake *tx_wake =
  643. container_of(t, struct tile_net_tx_wake, timer);
  644. netif_wake_subqueue(tx_wake->dev, tx_wake->tx_queue_idx);
  645. return HRTIMER_NORESTART;
  646. }
  647. /* Make sure the egress timer is scheduled. */
  648. static void tile_net_schedule_egress_timer(void)
  649. {
  650. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  651. if (!info->egress_timer_scheduled) {
  652. hrtimer_start(&info->egress_timer,
  653. EGRESS_TIMER_DELAY_USEC * 1000UL,
  654. HRTIMER_MODE_REL_PINNED);
  655. info->egress_timer_scheduled = true;
  656. }
  657. }
  658. /* The "function" for "info->egress_timer".
  659. *
  660. * This timer will reschedule itself as long as there are any pending
  661. * completions expected for this tile.
  662. */
  663. static enum hrtimer_restart tile_net_handle_egress_timer(struct hrtimer *t)
  664. {
  665. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  666. unsigned long irqflags;
  667. bool pending = false;
  668. int i, instance;
  669. local_irq_save(irqflags);
  670. /* The timer is no longer scheduled. */
  671. info->egress_timer_scheduled = false;
  672. /* Free all possible comps for this tile. */
  673. for (instance = 0; instance < NR_MPIPE_MAX &&
  674. info->mpipe[instance].has_iqueue; instance++) {
  675. for (i = 0; i < TILE_NET_CHANNELS; i++) {
  676. struct tile_net_egress *egress =
  677. &mpipe_data[instance].egress_for_echannel[i];
  678. struct tile_net_comps *comps =
  679. info->mpipe[instance].comps_for_echannel[i];
  680. if (!egress || comps->comp_last >= comps->comp_next)
  681. continue;
  682. tile_net_free_comps(egress->equeue, comps, -1, true);
  683. pending = pending ||
  684. (comps->comp_last < comps->comp_next);
  685. }
  686. }
  687. /* Reschedule timer if needed. */
  688. if (pending)
  689. tile_net_schedule_egress_timer();
  690. local_irq_restore(irqflags);
  691. return HRTIMER_NORESTART;
  692. }
  693. /* PTP clock operations. */
  694. static int ptp_mpipe_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  695. {
  696. int ret = 0;
  697. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  698. mutex_lock(&md->ptp_lock);
  699. if (gxio_mpipe_adjust_timestamp_freq(&md->context, ppb))
  700. ret = -EINVAL;
  701. mutex_unlock(&md->ptp_lock);
  702. return ret;
  703. }
  704. static int ptp_mpipe_adjtime(struct ptp_clock_info *ptp, s64 delta)
  705. {
  706. int ret = 0;
  707. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  708. mutex_lock(&md->ptp_lock);
  709. if (gxio_mpipe_adjust_timestamp(&md->context, delta))
  710. ret = -EBUSY;
  711. mutex_unlock(&md->ptp_lock);
  712. return ret;
  713. }
  714. static int ptp_mpipe_gettime(struct ptp_clock_info *ptp,
  715. struct timespec64 *ts)
  716. {
  717. int ret = 0;
  718. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  719. mutex_lock(&md->ptp_lock);
  720. if (gxio_mpipe_get_timestamp(&md->context, ts))
  721. ret = -EBUSY;
  722. mutex_unlock(&md->ptp_lock);
  723. return ret;
  724. }
  725. static int ptp_mpipe_settime(struct ptp_clock_info *ptp,
  726. const struct timespec64 *ts)
  727. {
  728. int ret = 0;
  729. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  730. mutex_lock(&md->ptp_lock);
  731. if (gxio_mpipe_set_timestamp(&md->context, ts))
  732. ret = -EBUSY;
  733. mutex_unlock(&md->ptp_lock);
  734. return ret;
  735. }
  736. static int ptp_mpipe_enable(struct ptp_clock_info *ptp,
  737. struct ptp_clock_request *request, int on)
  738. {
  739. return -EOPNOTSUPP;
  740. }
  741. static const struct ptp_clock_info ptp_mpipe_caps = {
  742. .owner = THIS_MODULE,
  743. .name = "mPIPE clock",
  744. .max_adj = 999999999,
  745. .n_ext_ts = 0,
  746. .n_pins = 0,
  747. .pps = 0,
  748. .adjfreq = ptp_mpipe_adjfreq,
  749. .adjtime = ptp_mpipe_adjtime,
  750. .gettime64 = ptp_mpipe_gettime,
  751. .settime64 = ptp_mpipe_settime,
  752. .enable = ptp_mpipe_enable,
  753. };
  754. /* Sync mPIPE's timestamp up with Linux system time and register PTP clock. */
  755. static void register_ptp_clock(struct net_device *dev, struct mpipe_data *md)
  756. {
  757. struct timespec64 ts;
  758. ktime_get_ts64(&ts);
  759. gxio_mpipe_set_timestamp(&md->context, &ts);
  760. mutex_init(&md->ptp_lock);
  761. md->caps = ptp_mpipe_caps;
  762. md->ptp_clock = ptp_clock_register(&md->caps, NULL);
  763. if (IS_ERR(md->ptp_clock))
  764. netdev_err(dev, "ptp_clock_register failed %ld\n",
  765. PTR_ERR(md->ptp_clock));
  766. }
  767. /* Initialize PTP fields in a new device. */
  768. static void init_ptp_dev(struct tile_net_priv *priv)
  769. {
  770. priv->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
  771. priv->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
  772. }
  773. /* Helper functions for "tile_net_update()". */
  774. static void enable_ingress_irq(void *irq)
  775. {
  776. enable_percpu_irq((long)irq, 0);
  777. }
  778. static void disable_ingress_irq(void *irq)
  779. {
  780. disable_percpu_irq((long)irq);
  781. }
  782. /* Helper function for tile_net_open() and tile_net_stop().
  783. * Always called under tile_net_devs_for_channel_mutex.
  784. */
  785. static int tile_net_update(struct net_device *dev)
  786. {
  787. static gxio_mpipe_rules_t rules; /* too big to fit on the stack */
  788. bool saw_channel = false;
  789. int instance = mpipe_instance(dev);
  790. struct mpipe_data *md = &mpipe_data[instance];
  791. int channel;
  792. int rc;
  793. int cpu;
  794. saw_channel = false;
  795. gxio_mpipe_rules_init(&rules, &md->context);
  796. for (channel = 0; channel < TILE_NET_CHANNELS; channel++) {
  797. if (md->tile_net_devs_for_channel[channel] == NULL)
  798. continue;
  799. if (!saw_channel) {
  800. saw_channel = true;
  801. gxio_mpipe_rules_begin(&rules, md->first_bucket,
  802. md->num_buckets, NULL);
  803. gxio_mpipe_rules_set_headroom(&rules, NET_IP_ALIGN);
  804. }
  805. gxio_mpipe_rules_add_channel(&rules, channel);
  806. }
  807. /* NOTE: This can fail if there is no classifier.
  808. * ISSUE: Can anything else cause it to fail?
  809. */
  810. rc = gxio_mpipe_rules_commit(&rules);
  811. if (rc != 0) {
  812. netdev_warn(dev, "gxio_mpipe_rules_commit: mpipe[%d] %d\n",
  813. instance, rc);
  814. return -EIO;
  815. }
  816. /* Update all cpus, sequentially (to protect "netif_napi_add()").
  817. * We use on_each_cpu to handle the IPI mask or unmask.
  818. */
  819. if (!saw_channel)
  820. on_each_cpu(disable_ingress_irq,
  821. (void *)(long)(md->ingress_irq), 1);
  822. for_each_online_cpu(cpu) {
  823. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  824. if (!info->mpipe[instance].has_iqueue)
  825. continue;
  826. if (saw_channel) {
  827. if (!info->mpipe[instance].napi_added) {
  828. netif_napi_add(dev, &info->mpipe[instance].napi,
  829. tile_net_poll, TILE_NET_WEIGHT);
  830. info->mpipe[instance].napi_added = true;
  831. }
  832. if (!info->mpipe[instance].napi_enabled) {
  833. napi_enable(&info->mpipe[instance].napi);
  834. info->mpipe[instance].napi_enabled = true;
  835. }
  836. } else {
  837. if (info->mpipe[instance].napi_enabled) {
  838. napi_disable(&info->mpipe[instance].napi);
  839. info->mpipe[instance].napi_enabled = false;
  840. }
  841. /* FIXME: Drain the iqueue. */
  842. }
  843. }
  844. if (saw_channel)
  845. on_each_cpu(enable_ingress_irq,
  846. (void *)(long)(md->ingress_irq), 1);
  847. /* HACK: Allow packets to flow in the simulator. */
  848. if (saw_channel)
  849. sim_enable_mpipe_links(instance, -1);
  850. return 0;
  851. }
  852. /* Initialize a buffer stack. */
  853. static int create_buffer_stack(struct net_device *dev,
  854. int kind, size_t num_buffers)
  855. {
  856. pte_t hash_pte = pte_set_home((pte_t) { 0 }, PAGE_HOME_HASH);
  857. int instance = mpipe_instance(dev);
  858. struct mpipe_data *md = &mpipe_data[instance];
  859. size_t needed = gxio_mpipe_calc_buffer_stack_bytes(num_buffers);
  860. int stack_idx = md->first_buffer_stack + kind;
  861. void *va;
  862. int i, rc;
  863. /* Round up to 64KB and then use alloc_pages() so we get the
  864. * required 64KB alignment.
  865. */
  866. md->buffer_stack_bytes[kind] =
  867. ALIGN(needed, 64 * 1024);
  868. va = alloc_pages_exact(md->buffer_stack_bytes[kind], GFP_KERNEL);
  869. if (va == NULL) {
  870. netdev_err(dev,
  871. "Could not alloc %zd bytes for buffer stack %d\n",
  872. md->buffer_stack_bytes[kind], kind);
  873. return -ENOMEM;
  874. }
  875. /* Initialize the buffer stack. */
  876. rc = gxio_mpipe_init_buffer_stack(&md->context, stack_idx,
  877. buffer_size_enums[kind], va,
  878. md->buffer_stack_bytes[kind], 0);
  879. if (rc != 0) {
  880. netdev_err(dev, "gxio_mpipe_init_buffer_stack: mpipe[%d] %d\n",
  881. instance, rc);
  882. free_pages_exact(va, md->buffer_stack_bytes[kind]);
  883. return rc;
  884. }
  885. md->buffer_stack_vas[kind] = va;
  886. rc = gxio_mpipe_register_client_memory(&md->context, stack_idx,
  887. hash_pte, 0);
  888. if (rc != 0) {
  889. netdev_err(dev,
  890. "gxio_mpipe_register_client_memory: mpipe[%d] %d\n",
  891. instance, rc);
  892. return rc;
  893. }
  894. /* Provide initial buffers. */
  895. for (i = 0; i < num_buffers; i++) {
  896. if (!tile_net_provide_buffer(instance, kind)) {
  897. netdev_err(dev, "Cannot allocate initial sk_bufs!\n");
  898. return -ENOMEM;
  899. }
  900. }
  901. return 0;
  902. }
  903. /* Allocate and initialize mpipe buffer stacks, and register them in
  904. * the mPIPE TLBs, for small, large, and (possibly) jumbo packet sizes.
  905. * This routine supports tile_net_init_mpipe(), below.
  906. */
  907. static int init_buffer_stacks(struct net_device *dev,
  908. int network_cpus_count)
  909. {
  910. int num_kinds = MAX_KINDS - (jumbo_num == 0);
  911. size_t num_buffers;
  912. int rc;
  913. int instance = mpipe_instance(dev);
  914. struct mpipe_data *md = &mpipe_data[instance];
  915. /* Allocate the buffer stacks. */
  916. rc = gxio_mpipe_alloc_buffer_stacks(&md->context, num_kinds, 0, 0);
  917. if (rc < 0) {
  918. netdev_err(dev,
  919. "gxio_mpipe_alloc_buffer_stacks: mpipe[%d] %d\n",
  920. instance, rc);
  921. return rc;
  922. }
  923. md->first_buffer_stack = rc;
  924. /* Enough small/large buffers to (normally) avoid buffer errors. */
  925. num_buffers =
  926. network_cpus_count * (IQUEUE_ENTRIES + TILE_NET_BATCH);
  927. /* Allocate the small memory stack. */
  928. if (rc >= 0)
  929. rc = create_buffer_stack(dev, 0, num_buffers);
  930. /* Allocate the large buffer stack. */
  931. if (rc >= 0)
  932. rc = create_buffer_stack(dev, 1, num_buffers);
  933. /* Allocate the jumbo buffer stack if needed. */
  934. if (rc >= 0 && jumbo_num != 0)
  935. rc = create_buffer_stack(dev, 2, jumbo_num);
  936. return rc;
  937. }
  938. /* Allocate per-cpu resources (memory for completions and idescs).
  939. * This routine supports tile_net_init_mpipe(), below.
  940. */
  941. static int alloc_percpu_mpipe_resources(struct net_device *dev,
  942. int cpu, int ring)
  943. {
  944. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  945. int order, i, rc;
  946. int instance = mpipe_instance(dev);
  947. struct mpipe_data *md = &mpipe_data[instance];
  948. struct page *page;
  949. void *addr;
  950. /* Allocate the "comps". */
  951. order = get_order(COMPS_SIZE);
  952. page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
  953. if (page == NULL) {
  954. netdev_err(dev, "Failed to alloc %zd bytes comps memory\n",
  955. COMPS_SIZE);
  956. return -ENOMEM;
  957. }
  958. addr = pfn_to_kaddr(page_to_pfn(page));
  959. memset(addr, 0, COMPS_SIZE);
  960. for (i = 0; i < TILE_NET_CHANNELS; i++)
  961. info->mpipe[instance].comps_for_echannel[i] =
  962. addr + i * sizeof(struct tile_net_comps);
  963. /* If this is a network cpu, create an iqueue. */
  964. if (cpumask_test_cpu(cpu, &network_cpus_map)) {
  965. order = get_order(NOTIF_RING_SIZE);
  966. page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
  967. if (page == NULL) {
  968. netdev_err(dev,
  969. "Failed to alloc %zd bytes iqueue memory\n",
  970. NOTIF_RING_SIZE);
  971. return -ENOMEM;
  972. }
  973. addr = pfn_to_kaddr(page_to_pfn(page));
  974. rc = gxio_mpipe_iqueue_init(&info->mpipe[instance].iqueue,
  975. &md->context, ring++, addr,
  976. NOTIF_RING_SIZE, 0);
  977. if (rc < 0) {
  978. netdev_err(dev,
  979. "gxio_mpipe_iqueue_init failed: %d\n", rc);
  980. return rc;
  981. }
  982. info->mpipe[instance].has_iqueue = true;
  983. }
  984. return ring;
  985. }
  986. /* Initialize NotifGroup and buckets.
  987. * This routine supports tile_net_init_mpipe(), below.
  988. */
  989. static int init_notif_group_and_buckets(struct net_device *dev,
  990. int ring, int network_cpus_count)
  991. {
  992. int group, rc;
  993. int instance = mpipe_instance(dev);
  994. struct mpipe_data *md = &mpipe_data[instance];
  995. /* Allocate one NotifGroup. */
  996. rc = gxio_mpipe_alloc_notif_groups(&md->context, 1, 0, 0);
  997. if (rc < 0) {
  998. netdev_err(dev, "gxio_mpipe_alloc_notif_groups: mpipe[%d] %d\n",
  999. instance, rc);
  1000. return rc;
  1001. }
  1002. group = rc;
  1003. /* Initialize global num_buckets value. */
  1004. if (network_cpus_count > 4)
  1005. md->num_buckets = 256;
  1006. else if (network_cpus_count > 1)
  1007. md->num_buckets = 16;
  1008. /* Allocate some buckets, and set global first_bucket value. */
  1009. rc = gxio_mpipe_alloc_buckets(&md->context, md->num_buckets, 0, 0);
  1010. if (rc < 0) {
  1011. netdev_err(dev, "gxio_mpipe_alloc_buckets: mpipe[%d] %d\n",
  1012. instance, rc);
  1013. return rc;
  1014. }
  1015. md->first_bucket = rc;
  1016. /* Init group and buckets. */
  1017. rc = gxio_mpipe_init_notif_group_and_buckets(
  1018. &md->context, group, ring, network_cpus_count,
  1019. md->first_bucket, md->num_buckets,
  1020. GXIO_MPIPE_BUCKET_STICKY_FLOW_LOCALITY);
  1021. if (rc != 0) {
  1022. netdev_err(dev, "gxio_mpipe_init_notif_group_and_buckets: "
  1023. "mpipe[%d] %d\n", instance, rc);
  1024. return rc;
  1025. }
  1026. return 0;
  1027. }
  1028. /* Create an irq and register it, then activate the irq and request
  1029. * interrupts on all cores. Note that "ingress_irq" being initialized
  1030. * is how we know not to call tile_net_init_mpipe() again.
  1031. * This routine supports tile_net_init_mpipe(), below.
  1032. */
  1033. static int tile_net_setup_interrupts(struct net_device *dev)
  1034. {
  1035. int cpu, rc, irq;
  1036. int instance = mpipe_instance(dev);
  1037. struct mpipe_data *md = &mpipe_data[instance];
  1038. irq = md->ingress_irq;
  1039. if (irq < 0) {
  1040. irq = irq_alloc_hwirq(-1);
  1041. if (!irq) {
  1042. netdev_err(dev,
  1043. "create_irq failed: mpipe[%d] %d\n",
  1044. instance, irq);
  1045. return irq;
  1046. }
  1047. tile_irq_activate(irq, TILE_IRQ_PERCPU);
  1048. rc = request_irq(irq, tile_net_handle_ingress_irq,
  1049. 0, "tile_net", (void *)((uint64_t)instance));
  1050. if (rc != 0) {
  1051. netdev_err(dev, "request_irq failed: mpipe[%d] %d\n",
  1052. instance, rc);
  1053. irq_free_hwirq(irq);
  1054. return rc;
  1055. }
  1056. md->ingress_irq = irq;
  1057. }
  1058. for_each_online_cpu(cpu) {
  1059. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1060. if (info->mpipe[instance].has_iqueue) {
  1061. gxio_mpipe_request_notif_ring_interrupt(&md->context,
  1062. cpu_x(cpu), cpu_y(cpu), KERNEL_PL, irq,
  1063. info->mpipe[instance].iqueue.ring);
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. /* Undo any state set up partially by a failed call to tile_net_init_mpipe. */
  1069. static void tile_net_init_mpipe_fail(int instance)
  1070. {
  1071. int kind, cpu;
  1072. struct mpipe_data *md = &mpipe_data[instance];
  1073. /* Do cleanups that require the mpipe context first. */
  1074. for (kind = 0; kind < MAX_KINDS; kind++) {
  1075. if (md->buffer_stack_vas[kind] != NULL) {
  1076. tile_net_pop_all_buffers(instance,
  1077. md->first_buffer_stack +
  1078. kind);
  1079. }
  1080. }
  1081. /* Destroy mpipe context so the hardware no longer owns any memory. */
  1082. gxio_mpipe_destroy(&md->context);
  1083. for_each_online_cpu(cpu) {
  1084. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1085. free_pages(
  1086. (unsigned long)(
  1087. info->mpipe[instance].comps_for_echannel[0]),
  1088. get_order(COMPS_SIZE));
  1089. info->mpipe[instance].comps_for_echannel[0] = NULL;
  1090. free_pages((unsigned long)(info->mpipe[instance].iqueue.idescs),
  1091. get_order(NOTIF_RING_SIZE));
  1092. info->mpipe[instance].iqueue.idescs = NULL;
  1093. }
  1094. for (kind = 0; kind < MAX_KINDS; kind++) {
  1095. if (md->buffer_stack_vas[kind] != NULL) {
  1096. free_pages_exact(md->buffer_stack_vas[kind],
  1097. md->buffer_stack_bytes[kind]);
  1098. md->buffer_stack_vas[kind] = NULL;
  1099. }
  1100. }
  1101. md->first_buffer_stack = -1;
  1102. md->first_bucket = -1;
  1103. }
  1104. /* The first time any tilegx network device is opened, we initialize
  1105. * the global mpipe state. If this step fails, we fail to open the
  1106. * device, but if it succeeds, we never need to do it again, and since
  1107. * tile_net can't be unloaded, we never undo it.
  1108. *
  1109. * Note that some resources in this path (buffer stack indices,
  1110. * bindings from init_buffer_stack, etc.) are hypervisor resources
  1111. * that are freed implicitly by gxio_mpipe_destroy().
  1112. */
  1113. static int tile_net_init_mpipe(struct net_device *dev)
  1114. {
  1115. int rc;
  1116. int cpu;
  1117. int first_ring, ring;
  1118. int instance = mpipe_instance(dev);
  1119. struct mpipe_data *md = &mpipe_data[instance];
  1120. int network_cpus_count = cpumask_weight(&network_cpus_map);
  1121. if (!hash_default) {
  1122. netdev_err(dev, "Networking requires hash_default!\n");
  1123. return -EIO;
  1124. }
  1125. rc = gxio_mpipe_init(&md->context, instance);
  1126. if (rc != 0) {
  1127. netdev_err(dev, "gxio_mpipe_init: mpipe[%d] %d\n",
  1128. instance, rc);
  1129. return -EIO;
  1130. }
  1131. /* Set up the buffer stacks. */
  1132. rc = init_buffer_stacks(dev, network_cpus_count);
  1133. if (rc != 0)
  1134. goto fail;
  1135. /* Allocate one NotifRing for each network cpu. */
  1136. rc = gxio_mpipe_alloc_notif_rings(&md->context,
  1137. network_cpus_count, 0, 0);
  1138. if (rc < 0) {
  1139. netdev_err(dev, "gxio_mpipe_alloc_notif_rings failed %d\n",
  1140. rc);
  1141. goto fail;
  1142. }
  1143. /* Init NotifRings per-cpu. */
  1144. first_ring = rc;
  1145. ring = first_ring;
  1146. for_each_online_cpu(cpu) {
  1147. rc = alloc_percpu_mpipe_resources(dev, cpu, ring);
  1148. if (rc < 0)
  1149. goto fail;
  1150. ring = rc;
  1151. }
  1152. /* Initialize NotifGroup and buckets. */
  1153. rc = init_notif_group_and_buckets(dev, first_ring, network_cpus_count);
  1154. if (rc != 0)
  1155. goto fail;
  1156. /* Create and enable interrupts. */
  1157. rc = tile_net_setup_interrupts(dev);
  1158. if (rc != 0)
  1159. goto fail;
  1160. /* Register PTP clock and set mPIPE timestamp, if configured. */
  1161. register_ptp_clock(dev, md);
  1162. return 0;
  1163. fail:
  1164. tile_net_init_mpipe_fail(instance);
  1165. return rc;
  1166. }
  1167. /* Create persistent egress info for a given egress channel.
  1168. * Note that this may be shared between, say, "gbe0" and "xgbe0".
  1169. * ISSUE: Defer header allocation until TSO is actually needed?
  1170. */
  1171. static int tile_net_init_egress(struct net_device *dev, int echannel)
  1172. {
  1173. static int ering = -1;
  1174. struct page *headers_page, *edescs_page, *equeue_page;
  1175. gxio_mpipe_edesc_t *edescs;
  1176. gxio_mpipe_equeue_t *equeue;
  1177. unsigned char *headers;
  1178. int headers_order, edescs_order, equeue_order;
  1179. size_t edescs_size;
  1180. int rc = -ENOMEM;
  1181. int instance = mpipe_instance(dev);
  1182. struct mpipe_data *md = &mpipe_data[instance];
  1183. /* Only initialize once. */
  1184. if (md->egress_for_echannel[echannel].equeue != NULL)
  1185. return 0;
  1186. /* Allocate memory for the "headers". */
  1187. headers_order = get_order(EQUEUE_ENTRIES * HEADER_BYTES);
  1188. headers_page = alloc_pages(GFP_KERNEL, headers_order);
  1189. if (headers_page == NULL) {
  1190. netdev_warn(dev,
  1191. "Could not alloc %zd bytes for TSO headers.\n",
  1192. PAGE_SIZE << headers_order);
  1193. goto fail;
  1194. }
  1195. headers = pfn_to_kaddr(page_to_pfn(headers_page));
  1196. /* Allocate memory for the "edescs". */
  1197. edescs_size = EQUEUE_ENTRIES * sizeof(*edescs);
  1198. edescs_order = get_order(edescs_size);
  1199. edescs_page = alloc_pages(GFP_KERNEL, edescs_order);
  1200. if (edescs_page == NULL) {
  1201. netdev_warn(dev,
  1202. "Could not alloc %zd bytes for eDMA ring.\n",
  1203. edescs_size);
  1204. goto fail_headers;
  1205. }
  1206. edescs = pfn_to_kaddr(page_to_pfn(edescs_page));
  1207. /* Allocate memory for the "equeue". */
  1208. equeue_order = get_order(sizeof(*equeue));
  1209. equeue_page = alloc_pages(GFP_KERNEL, equeue_order);
  1210. if (equeue_page == NULL) {
  1211. netdev_warn(dev,
  1212. "Could not alloc %zd bytes for equeue info.\n",
  1213. PAGE_SIZE << equeue_order);
  1214. goto fail_edescs;
  1215. }
  1216. equeue = pfn_to_kaddr(page_to_pfn(equeue_page));
  1217. /* Allocate an edma ring (using a one entry "free list"). */
  1218. if (ering < 0) {
  1219. rc = gxio_mpipe_alloc_edma_rings(&md->context, 1, 0, 0);
  1220. if (rc < 0) {
  1221. netdev_warn(dev, "gxio_mpipe_alloc_edma_rings: "
  1222. "mpipe[%d] %d\n", instance, rc);
  1223. goto fail_equeue;
  1224. }
  1225. ering = rc;
  1226. }
  1227. /* Initialize the equeue. */
  1228. rc = gxio_mpipe_equeue_init(equeue, &md->context, ering, echannel,
  1229. edescs, edescs_size, 0);
  1230. if (rc != 0) {
  1231. netdev_err(dev, "gxio_mpipe_equeue_init: mpipe[%d] %d\n",
  1232. instance, rc);
  1233. goto fail_equeue;
  1234. }
  1235. /* Don't reuse the ering later. */
  1236. ering = -1;
  1237. if (jumbo_num != 0) {
  1238. /* Make sure "jumbo" packets can be egressed safely. */
  1239. if (gxio_mpipe_equeue_set_snf_size(equeue, 10368) < 0) {
  1240. /* ISSUE: There is no "gxio_mpipe_equeue_destroy()". */
  1241. netdev_warn(dev, "Jumbo packets may not be egressed"
  1242. " properly on channel %d\n", echannel);
  1243. }
  1244. }
  1245. /* Done. */
  1246. md->egress_for_echannel[echannel].equeue = equeue;
  1247. md->egress_for_echannel[echannel].headers = headers;
  1248. return 0;
  1249. fail_equeue:
  1250. __free_pages(equeue_page, equeue_order);
  1251. fail_edescs:
  1252. __free_pages(edescs_page, edescs_order);
  1253. fail_headers:
  1254. __free_pages(headers_page, headers_order);
  1255. fail:
  1256. return rc;
  1257. }
  1258. /* Return channel number for a newly-opened link. */
  1259. static int tile_net_link_open(struct net_device *dev, gxio_mpipe_link_t *link,
  1260. const char *link_name)
  1261. {
  1262. int instance = mpipe_instance(dev);
  1263. struct mpipe_data *md = &mpipe_data[instance];
  1264. int rc = gxio_mpipe_link_open(link, &md->context, link_name, 0);
  1265. if (rc < 0) {
  1266. netdev_err(dev, "Failed to open '%s', mpipe[%d], %d\n",
  1267. link_name, instance, rc);
  1268. return rc;
  1269. }
  1270. if (jumbo_num != 0) {
  1271. u32 attr = GXIO_MPIPE_LINK_RECEIVE_JUMBO;
  1272. rc = gxio_mpipe_link_set_attr(link, attr, 1);
  1273. if (rc != 0) {
  1274. netdev_err(dev,
  1275. "Cannot receive jumbo packets on '%s'\n",
  1276. link_name);
  1277. gxio_mpipe_link_close(link);
  1278. return rc;
  1279. }
  1280. }
  1281. rc = gxio_mpipe_link_channel(link);
  1282. if (rc < 0 || rc >= TILE_NET_CHANNELS) {
  1283. netdev_err(dev, "gxio_mpipe_link_channel bad value: %d\n", rc);
  1284. gxio_mpipe_link_close(link);
  1285. return -EINVAL;
  1286. }
  1287. return rc;
  1288. }
  1289. /* Help the kernel activate the given network interface. */
  1290. static int tile_net_open(struct net_device *dev)
  1291. {
  1292. struct tile_net_priv *priv = netdev_priv(dev);
  1293. int cpu, rc, instance;
  1294. mutex_lock(&tile_net_devs_for_channel_mutex);
  1295. /* Get the instance info. */
  1296. rc = gxio_mpipe_link_instance(dev->name);
  1297. if (rc < 0 || rc >= NR_MPIPE_MAX) {
  1298. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1299. return -EIO;
  1300. }
  1301. priv->instance = rc;
  1302. instance = rc;
  1303. if (!mpipe_data[rc].context.mmio_fast_base) {
  1304. /* Do one-time initialization per instance the first time
  1305. * any device is opened.
  1306. */
  1307. rc = tile_net_init_mpipe(dev);
  1308. if (rc != 0)
  1309. goto fail;
  1310. }
  1311. /* Determine if this is the "loopify" device. */
  1312. if (unlikely((loopify_link_name != NULL) &&
  1313. !strcmp(dev->name, loopify_link_name))) {
  1314. rc = tile_net_link_open(dev, &priv->link, "loop0");
  1315. if (rc < 0)
  1316. goto fail;
  1317. priv->channel = rc;
  1318. rc = tile_net_link_open(dev, &priv->loopify_link, "loop1");
  1319. if (rc < 0)
  1320. goto fail;
  1321. priv->loopify_channel = rc;
  1322. priv->echannel = rc;
  1323. } else {
  1324. rc = tile_net_link_open(dev, &priv->link, dev->name);
  1325. if (rc < 0)
  1326. goto fail;
  1327. priv->channel = rc;
  1328. priv->echannel = rc;
  1329. }
  1330. /* Initialize egress info (if needed). Once ever, per echannel. */
  1331. rc = tile_net_init_egress(dev, priv->echannel);
  1332. if (rc != 0)
  1333. goto fail;
  1334. mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = dev;
  1335. rc = tile_net_update(dev);
  1336. if (rc != 0)
  1337. goto fail;
  1338. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1339. /* Initialize the transmit wake timer for this device for each cpu. */
  1340. for_each_online_cpu(cpu) {
  1341. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1342. struct tile_net_tx_wake *tx_wake =
  1343. &info->mpipe[instance].tx_wake[priv->echannel];
  1344. hrtimer_init(&tx_wake->timer, CLOCK_MONOTONIC,
  1345. HRTIMER_MODE_REL);
  1346. tx_wake->tx_queue_idx = cpu;
  1347. tx_wake->timer.function = tile_net_handle_tx_wake_timer;
  1348. tx_wake->dev = dev;
  1349. }
  1350. for_each_online_cpu(cpu)
  1351. netif_start_subqueue(dev, cpu);
  1352. netif_carrier_on(dev);
  1353. return 0;
  1354. fail:
  1355. if (priv->loopify_channel >= 0) {
  1356. if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
  1357. netdev_warn(dev, "Failed to close loopify link!\n");
  1358. priv->loopify_channel = -1;
  1359. }
  1360. if (priv->channel >= 0) {
  1361. if (gxio_mpipe_link_close(&priv->link) != 0)
  1362. netdev_warn(dev, "Failed to close link!\n");
  1363. priv->channel = -1;
  1364. }
  1365. priv->echannel = -1;
  1366. mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = NULL;
  1367. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1368. /* Don't return raw gxio error codes to generic Linux. */
  1369. return (rc > -512) ? rc : -EIO;
  1370. }
  1371. /* Help the kernel deactivate the given network interface. */
  1372. static int tile_net_stop(struct net_device *dev)
  1373. {
  1374. struct tile_net_priv *priv = netdev_priv(dev);
  1375. int cpu;
  1376. int instance = priv->instance;
  1377. struct mpipe_data *md = &mpipe_data[instance];
  1378. for_each_online_cpu(cpu) {
  1379. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1380. struct tile_net_tx_wake *tx_wake =
  1381. &info->mpipe[instance].tx_wake[priv->echannel];
  1382. hrtimer_cancel(&tx_wake->timer);
  1383. netif_stop_subqueue(dev, cpu);
  1384. }
  1385. mutex_lock(&tile_net_devs_for_channel_mutex);
  1386. md->tile_net_devs_for_channel[priv->channel] = NULL;
  1387. (void)tile_net_update(dev);
  1388. if (priv->loopify_channel >= 0) {
  1389. if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
  1390. netdev_warn(dev, "Failed to close loopify link!\n");
  1391. priv->loopify_channel = -1;
  1392. }
  1393. if (priv->channel >= 0) {
  1394. if (gxio_mpipe_link_close(&priv->link) != 0)
  1395. netdev_warn(dev, "Failed to close link!\n");
  1396. priv->channel = -1;
  1397. }
  1398. priv->echannel = -1;
  1399. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1400. return 0;
  1401. }
  1402. /* Determine the VA for a fragment. */
  1403. static inline void *tile_net_frag_buf(skb_frag_t *f)
  1404. {
  1405. unsigned long pfn = page_to_pfn(skb_frag_page(f));
  1406. return pfn_to_kaddr(pfn) + f->page_offset;
  1407. }
  1408. /* Acquire a completion entry and an egress slot, or if we can't,
  1409. * stop the queue and schedule the tx_wake timer.
  1410. */
  1411. static s64 tile_net_equeue_try_reserve(struct net_device *dev,
  1412. int tx_queue_idx,
  1413. struct tile_net_comps *comps,
  1414. gxio_mpipe_equeue_t *equeue,
  1415. int num_edescs)
  1416. {
  1417. /* Try to acquire a completion entry. */
  1418. if (comps->comp_next - comps->comp_last < TILE_NET_MAX_COMPS - 1 ||
  1419. tile_net_free_comps(equeue, comps, 32, false) != 0) {
  1420. /* Try to acquire an egress slot. */
  1421. s64 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
  1422. if (slot >= 0)
  1423. return slot;
  1424. /* Freeing some completions gives the equeue time to drain. */
  1425. tile_net_free_comps(equeue, comps, TILE_NET_MAX_COMPS, false);
  1426. slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
  1427. if (slot >= 0)
  1428. return slot;
  1429. }
  1430. /* Still nothing; give up and stop the queue for a short while. */
  1431. netif_stop_subqueue(dev, tx_queue_idx);
  1432. tile_net_schedule_tx_wake_timer(dev, tx_queue_idx);
  1433. return -1;
  1434. }
  1435. /* Determine how many edesc's are needed for TSO.
  1436. *
  1437. * Sometimes, if "sendfile()" requires copying, we will be called with
  1438. * "data" containing the header and payload, with "frags" being empty.
  1439. * Sometimes, for example when using NFS over TCP, a single segment can
  1440. * span 3 fragments. This requires special care.
  1441. */
  1442. static int tso_count_edescs(struct sk_buff *skb)
  1443. {
  1444. struct skb_shared_info *sh = skb_shinfo(skb);
  1445. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1446. unsigned int data_len = skb->len - sh_len;
  1447. unsigned int p_len = sh->gso_size;
  1448. long f_id = -1; /* id of the current fragment */
  1449. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1450. long f_used = 0; /* bytes used from the current fragment */
  1451. long n; /* size of the current piece of payload */
  1452. int num_edescs = 0;
  1453. int segment;
  1454. for (segment = 0; segment < sh->gso_segs; segment++) {
  1455. unsigned int p_used = 0;
  1456. /* One edesc for header and for each piece of the payload. */
  1457. for (num_edescs++; p_used < p_len; num_edescs++) {
  1458. /* Advance as needed. */
  1459. while (f_used >= f_size) {
  1460. f_id++;
  1461. f_size = skb_frag_size(&sh->frags[f_id]);
  1462. f_used = 0;
  1463. }
  1464. /* Use bytes from the current fragment. */
  1465. n = p_len - p_used;
  1466. if (n > f_size - f_used)
  1467. n = f_size - f_used;
  1468. f_used += n;
  1469. p_used += n;
  1470. }
  1471. /* The last segment may be less than gso_size. */
  1472. data_len -= p_len;
  1473. if (data_len < p_len)
  1474. p_len = data_len;
  1475. }
  1476. return num_edescs;
  1477. }
  1478. /* Prepare modified copies of the skbuff headers. */
  1479. static void tso_headers_prepare(struct sk_buff *skb, unsigned char *headers,
  1480. s64 slot)
  1481. {
  1482. struct skb_shared_info *sh = skb_shinfo(skb);
  1483. struct iphdr *ih;
  1484. struct ipv6hdr *ih6;
  1485. struct tcphdr *th;
  1486. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1487. unsigned int data_len = skb->len - sh_len;
  1488. unsigned char *data = skb->data;
  1489. unsigned int ih_off, th_off, p_len;
  1490. unsigned int isum_seed, tsum_seed, seq;
  1491. unsigned int uninitialized_var(id);
  1492. int is_ipv6;
  1493. long f_id = -1; /* id of the current fragment */
  1494. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1495. long f_used = 0; /* bytes used from the current fragment */
  1496. long n; /* size of the current piece of payload */
  1497. int segment;
  1498. /* Locate original headers and compute various lengths. */
  1499. is_ipv6 = skb_is_gso_v6(skb);
  1500. if (is_ipv6) {
  1501. ih6 = ipv6_hdr(skb);
  1502. ih_off = skb_network_offset(skb);
  1503. } else {
  1504. ih = ip_hdr(skb);
  1505. ih_off = skb_network_offset(skb);
  1506. isum_seed = ((0xFFFF - ih->check) +
  1507. (0xFFFF - ih->tot_len) +
  1508. (0xFFFF - ih->id));
  1509. id = ntohs(ih->id);
  1510. }
  1511. th = tcp_hdr(skb);
  1512. th_off = skb_transport_offset(skb);
  1513. p_len = sh->gso_size;
  1514. tsum_seed = th->check + (0xFFFF ^ htons(skb->len));
  1515. seq = ntohl(th->seq);
  1516. /* Prepare all the headers. */
  1517. for (segment = 0; segment < sh->gso_segs; segment++) {
  1518. unsigned char *buf;
  1519. unsigned int p_used = 0;
  1520. /* Copy to the header memory for this segment. */
  1521. buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
  1522. NET_IP_ALIGN;
  1523. memcpy(buf, data, sh_len);
  1524. /* Update copied ip header. */
  1525. if (is_ipv6) {
  1526. ih6 = (struct ipv6hdr *)(buf + ih_off);
  1527. ih6->payload_len = htons(sh_len + p_len - ih_off -
  1528. sizeof(*ih6));
  1529. } else {
  1530. ih = (struct iphdr *)(buf + ih_off);
  1531. ih->tot_len = htons(sh_len + p_len - ih_off);
  1532. ih->id = htons(id++);
  1533. ih->check = csum_long(isum_seed + ih->tot_len +
  1534. ih->id) ^ 0xffff;
  1535. }
  1536. /* Update copied tcp header. */
  1537. th = (struct tcphdr *)(buf + th_off);
  1538. th->seq = htonl(seq);
  1539. th->check = csum_long(tsum_seed + htons(sh_len + p_len));
  1540. if (segment != sh->gso_segs - 1) {
  1541. th->fin = 0;
  1542. th->psh = 0;
  1543. }
  1544. /* Skip past the header. */
  1545. slot++;
  1546. /* Skip past the payload. */
  1547. while (p_used < p_len) {
  1548. /* Advance as needed. */
  1549. while (f_used >= f_size) {
  1550. f_id++;
  1551. f_size = skb_frag_size(&sh->frags[f_id]);
  1552. f_used = 0;
  1553. }
  1554. /* Use bytes from the current fragment. */
  1555. n = p_len - p_used;
  1556. if (n > f_size - f_used)
  1557. n = f_size - f_used;
  1558. f_used += n;
  1559. p_used += n;
  1560. slot++;
  1561. }
  1562. seq += p_len;
  1563. /* The last segment may be less than gso_size. */
  1564. data_len -= p_len;
  1565. if (data_len < p_len)
  1566. p_len = data_len;
  1567. }
  1568. /* Flush the headers so they are ready for hardware DMA. */
  1569. wmb();
  1570. }
  1571. /* Pass all the data to mpipe for egress. */
  1572. static void tso_egress(struct net_device *dev, gxio_mpipe_equeue_t *equeue,
  1573. struct sk_buff *skb, unsigned char *headers, s64 slot)
  1574. {
  1575. struct skb_shared_info *sh = skb_shinfo(skb);
  1576. int instance = mpipe_instance(dev);
  1577. struct mpipe_data *md = &mpipe_data[instance];
  1578. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1579. unsigned int data_len = skb->len - sh_len;
  1580. unsigned int p_len = sh->gso_size;
  1581. gxio_mpipe_edesc_t edesc_head = { { 0 } };
  1582. gxio_mpipe_edesc_t edesc_body = { { 0 } };
  1583. long f_id = -1; /* id of the current fragment */
  1584. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1585. long f_used = 0; /* bytes used from the current fragment */
  1586. void *f_data = skb->data + sh_len;
  1587. long n; /* size of the current piece of payload */
  1588. unsigned long tx_packets = 0, tx_bytes = 0;
  1589. unsigned int csum_start;
  1590. int segment;
  1591. /* Prepare to egress the headers: set up header edesc. */
  1592. csum_start = skb_checksum_start_offset(skb);
  1593. edesc_head.csum = 1;
  1594. edesc_head.csum_start = csum_start;
  1595. edesc_head.csum_dest = csum_start + skb->csum_offset;
  1596. edesc_head.xfer_size = sh_len;
  1597. /* This is only used to specify the TLB. */
  1598. edesc_head.stack_idx = md->first_buffer_stack;
  1599. edesc_body.stack_idx = md->first_buffer_stack;
  1600. /* Egress all the edescs. */
  1601. for (segment = 0; segment < sh->gso_segs; segment++) {
  1602. unsigned char *buf;
  1603. unsigned int p_used = 0;
  1604. /* Egress the header. */
  1605. buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
  1606. NET_IP_ALIGN;
  1607. edesc_head.va = va_to_tile_io_addr(buf);
  1608. gxio_mpipe_equeue_put_at(equeue, edesc_head, slot);
  1609. slot++;
  1610. /* Egress the payload. */
  1611. while (p_used < p_len) {
  1612. void *va;
  1613. /* Advance as needed. */
  1614. while (f_used >= f_size) {
  1615. f_id++;
  1616. f_size = skb_frag_size(&sh->frags[f_id]);
  1617. f_data = tile_net_frag_buf(&sh->frags[f_id]);
  1618. f_used = 0;
  1619. }
  1620. va = f_data + f_used;
  1621. /* Use bytes from the current fragment. */
  1622. n = p_len - p_used;
  1623. if (n > f_size - f_used)
  1624. n = f_size - f_used;
  1625. f_used += n;
  1626. p_used += n;
  1627. /* Egress a piece of the payload. */
  1628. edesc_body.va = va_to_tile_io_addr(va);
  1629. edesc_body.xfer_size = n;
  1630. edesc_body.bound = !(p_used < p_len);
  1631. gxio_mpipe_equeue_put_at(equeue, edesc_body, slot);
  1632. slot++;
  1633. }
  1634. tx_packets++;
  1635. tx_bytes += sh_len + p_len;
  1636. /* The last segment may be less than gso_size. */
  1637. data_len -= p_len;
  1638. if (data_len < p_len)
  1639. p_len = data_len;
  1640. }
  1641. /* Update stats. */
  1642. tile_net_stats_add(tx_packets, &dev->stats.tx_packets);
  1643. tile_net_stats_add(tx_bytes, &dev->stats.tx_bytes);
  1644. }
  1645. /* Do "TSO" handling for egress.
  1646. *
  1647. * Normally drivers set NETIF_F_TSO only to support hardware TSO;
  1648. * otherwise the stack uses scatter-gather to implement GSO in software.
  1649. * On our testing, enabling GSO support (via NETIF_F_SG) drops network
  1650. * performance down to around 7.5 Gbps on the 10G interfaces, although
  1651. * also dropping cpu utilization way down, to under 8%. But
  1652. * implementing "TSO" in the driver brings performance back up to line
  1653. * rate, while dropping cpu usage even further, to less than 4%. In
  1654. * practice, profiling of GSO shows that skb_segment() is what causes
  1655. * the performance overheads; we benefit in the driver from using
  1656. * preallocated memory to duplicate the TCP/IP headers.
  1657. */
  1658. static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
  1659. {
  1660. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1661. struct tile_net_priv *priv = netdev_priv(dev);
  1662. int channel = priv->echannel;
  1663. int instance = priv->instance;
  1664. struct mpipe_data *md = &mpipe_data[instance];
  1665. struct tile_net_egress *egress = &md->egress_for_echannel[channel];
  1666. struct tile_net_comps *comps =
  1667. info->mpipe[instance].comps_for_echannel[channel];
  1668. gxio_mpipe_equeue_t *equeue = egress->equeue;
  1669. unsigned long irqflags;
  1670. int num_edescs;
  1671. s64 slot;
  1672. /* Determine how many mpipe edesc's are needed. */
  1673. num_edescs = tso_count_edescs(skb);
  1674. local_irq_save(irqflags);
  1675. /* Try to acquire a completion entry and an egress slot. */
  1676. slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
  1677. equeue, num_edescs);
  1678. if (slot < 0) {
  1679. local_irq_restore(irqflags);
  1680. return NETDEV_TX_BUSY;
  1681. }
  1682. /* Set up copies of header data properly. */
  1683. tso_headers_prepare(skb, egress->headers, slot);
  1684. /* Actually pass the data to the network hardware. */
  1685. tso_egress(dev, equeue, skb, egress->headers, slot);
  1686. /* Add a completion record. */
  1687. add_comp(equeue, comps, slot + num_edescs - 1, skb);
  1688. local_irq_restore(irqflags);
  1689. /* Make sure the egress timer is scheduled. */
  1690. tile_net_schedule_egress_timer();
  1691. return NETDEV_TX_OK;
  1692. }
  1693. /* Analyze the body and frags for a transmit request. */
  1694. static unsigned int tile_net_tx_frags(struct frag *frags,
  1695. struct sk_buff *skb,
  1696. void *b_data, unsigned int b_len)
  1697. {
  1698. unsigned int i, n = 0;
  1699. struct skb_shared_info *sh = skb_shinfo(skb);
  1700. if (b_len != 0) {
  1701. frags[n].buf = b_data;
  1702. frags[n++].length = b_len;
  1703. }
  1704. for (i = 0; i < sh->nr_frags; i++) {
  1705. skb_frag_t *f = &sh->frags[i];
  1706. frags[n].buf = tile_net_frag_buf(f);
  1707. frags[n++].length = skb_frag_size(f);
  1708. }
  1709. return n;
  1710. }
  1711. /* Help the kernel transmit a packet. */
  1712. static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
  1713. {
  1714. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1715. struct tile_net_priv *priv = netdev_priv(dev);
  1716. int instance = priv->instance;
  1717. struct mpipe_data *md = &mpipe_data[instance];
  1718. struct tile_net_egress *egress =
  1719. &md->egress_for_echannel[priv->echannel];
  1720. gxio_mpipe_equeue_t *equeue = egress->equeue;
  1721. struct tile_net_comps *comps =
  1722. info->mpipe[instance].comps_for_echannel[priv->echannel];
  1723. unsigned int len = skb->len;
  1724. unsigned char *data = skb->data;
  1725. unsigned int num_edescs;
  1726. struct frag frags[MAX_FRAGS];
  1727. gxio_mpipe_edesc_t edescs[MAX_FRAGS];
  1728. unsigned long irqflags;
  1729. gxio_mpipe_edesc_t edesc = { { 0 } };
  1730. unsigned int i;
  1731. s64 slot;
  1732. if (skb_is_gso(skb))
  1733. return tile_net_tx_tso(skb, dev);
  1734. num_edescs = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
  1735. /* This is only used to specify the TLB. */
  1736. edesc.stack_idx = md->first_buffer_stack;
  1737. /* Prepare the edescs. */
  1738. for (i = 0; i < num_edescs; i++) {
  1739. edesc.xfer_size = frags[i].length;
  1740. edesc.va = va_to_tile_io_addr(frags[i].buf);
  1741. edescs[i] = edesc;
  1742. }
  1743. /* Mark the final edesc. */
  1744. edescs[num_edescs - 1].bound = 1;
  1745. /* Add checksum info to the initial edesc, if needed. */
  1746. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1747. unsigned int csum_start = skb_checksum_start_offset(skb);
  1748. edescs[0].csum = 1;
  1749. edescs[0].csum_start = csum_start;
  1750. edescs[0].csum_dest = csum_start + skb->csum_offset;
  1751. }
  1752. local_irq_save(irqflags);
  1753. /* Try to acquire a completion entry and an egress slot. */
  1754. slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
  1755. equeue, num_edescs);
  1756. if (slot < 0) {
  1757. local_irq_restore(irqflags);
  1758. return NETDEV_TX_BUSY;
  1759. }
  1760. for (i = 0; i < num_edescs; i++)
  1761. gxio_mpipe_equeue_put_at(equeue, edescs[i], slot++);
  1762. /* Store TX timestamp if needed. */
  1763. tile_tx_timestamp(skb, instance);
  1764. /* Add a completion record. */
  1765. add_comp(equeue, comps, slot - 1, skb);
  1766. /* NOTE: Use ETH_ZLEN for short packets (e.g. 42 < 60). */
  1767. tile_net_stats_add(1, &dev->stats.tx_packets);
  1768. tile_net_stats_add(max_t(unsigned int, len, ETH_ZLEN),
  1769. &dev->stats.tx_bytes);
  1770. local_irq_restore(irqflags);
  1771. /* Make sure the egress timer is scheduled. */
  1772. tile_net_schedule_egress_timer();
  1773. return NETDEV_TX_OK;
  1774. }
  1775. /* Return subqueue id on this core (one per core). */
  1776. static u16 tile_net_select_queue(struct net_device *dev, struct sk_buff *skb,
  1777. void *accel_priv, select_queue_fallback_t fallback)
  1778. {
  1779. return smp_processor_id();
  1780. }
  1781. /* Deal with a transmit timeout. */
  1782. static void tile_net_tx_timeout(struct net_device *dev)
  1783. {
  1784. int cpu;
  1785. for_each_online_cpu(cpu)
  1786. netif_wake_subqueue(dev, cpu);
  1787. }
  1788. /* Ioctl commands. */
  1789. static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1790. {
  1791. if (cmd == SIOCSHWTSTAMP)
  1792. return tile_hwtstamp_set(dev, rq);
  1793. if (cmd == SIOCGHWTSTAMP)
  1794. return tile_hwtstamp_get(dev, rq);
  1795. return -EOPNOTSUPP;
  1796. }
  1797. /* Change the Ethernet address of the NIC.
  1798. *
  1799. * The hypervisor driver does not support changing MAC address. However,
  1800. * the hardware does not do anything with the MAC address, so the address
  1801. * which gets used on outgoing packets, and which is accepted on incoming
  1802. * packets, is completely up to us.
  1803. *
  1804. * Returns 0 on success, negative on failure.
  1805. */
  1806. static int tile_net_set_mac_address(struct net_device *dev, void *p)
  1807. {
  1808. struct sockaddr *addr = p;
  1809. if (!is_valid_ether_addr(addr->sa_data))
  1810. return -EINVAL;
  1811. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1812. return 0;
  1813. }
  1814. #ifdef CONFIG_NET_POLL_CONTROLLER
  1815. /* Polling 'interrupt' - used by things like netconsole to send skbs
  1816. * without having to re-enable interrupts. It's not called while
  1817. * the interrupt routine is executing.
  1818. */
  1819. static void tile_net_netpoll(struct net_device *dev)
  1820. {
  1821. int instance = mpipe_instance(dev);
  1822. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1823. struct mpipe_data *md = &mpipe_data[instance];
  1824. disable_percpu_irq(md->ingress_irq);
  1825. napi_schedule(&info->mpipe[instance].napi);
  1826. enable_percpu_irq(md->ingress_irq, 0);
  1827. }
  1828. #endif
  1829. static const struct net_device_ops tile_net_ops = {
  1830. .ndo_open = tile_net_open,
  1831. .ndo_stop = tile_net_stop,
  1832. .ndo_start_xmit = tile_net_tx,
  1833. .ndo_select_queue = tile_net_select_queue,
  1834. .ndo_do_ioctl = tile_net_ioctl,
  1835. .ndo_tx_timeout = tile_net_tx_timeout,
  1836. .ndo_set_mac_address = tile_net_set_mac_address,
  1837. #ifdef CONFIG_NET_POLL_CONTROLLER
  1838. .ndo_poll_controller = tile_net_netpoll,
  1839. #endif
  1840. };
  1841. /* The setup function.
  1842. *
  1843. * This uses ether_setup() to assign various fields in dev, including
  1844. * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
  1845. */
  1846. static void tile_net_setup(struct net_device *dev)
  1847. {
  1848. netdev_features_t features = 0;
  1849. ether_setup(dev);
  1850. dev->netdev_ops = &tile_net_ops;
  1851. dev->watchdog_timeo = TILE_NET_TIMEOUT;
  1852. /* MTU range: 68 - 1500 or 9000 */
  1853. dev->mtu = ETH_DATA_LEN;
  1854. dev->min_mtu = ETH_MIN_MTU;
  1855. dev->max_mtu = jumbo_num ? TILE_JUMBO_MAX_MTU : ETH_DATA_LEN;
  1856. features |= NETIF_F_HW_CSUM;
  1857. features |= NETIF_F_SG;
  1858. features |= NETIF_F_TSO;
  1859. features |= NETIF_F_TSO6;
  1860. dev->hw_features |= features;
  1861. dev->vlan_features |= features;
  1862. dev->features |= features;
  1863. }
  1864. /* Allocate the device structure, register the device, and obtain the
  1865. * MAC address from the hypervisor.
  1866. */
  1867. static void tile_net_dev_init(const char *name, const uint8_t *mac)
  1868. {
  1869. int ret;
  1870. struct net_device *dev;
  1871. struct tile_net_priv *priv;
  1872. /* HACK: Ignore "loop" links. */
  1873. if (strncmp(name, "loop", 4) == 0)
  1874. return;
  1875. /* Allocate the device structure. Normally, "name" is a
  1876. * template, instantiated by register_netdev(), but not for us.
  1877. */
  1878. dev = alloc_netdev_mqs(sizeof(*priv), name, NET_NAME_UNKNOWN,
  1879. tile_net_setup, NR_CPUS, 1);
  1880. if (!dev) {
  1881. pr_err("alloc_netdev_mqs(%s) failed\n", name);
  1882. return;
  1883. }
  1884. /* Initialize "priv". */
  1885. priv = netdev_priv(dev);
  1886. priv->dev = dev;
  1887. priv->channel = -1;
  1888. priv->loopify_channel = -1;
  1889. priv->echannel = -1;
  1890. init_ptp_dev(priv);
  1891. /* Get the MAC address and set it in the device struct; this must
  1892. * be done before the device is opened. If the MAC is all zeroes,
  1893. * we use a random address, since we're probably on the simulator.
  1894. */
  1895. if (!is_zero_ether_addr(mac))
  1896. ether_addr_copy(dev->dev_addr, mac);
  1897. else
  1898. eth_hw_addr_random(dev);
  1899. /* Register the network device. */
  1900. ret = register_netdev(dev);
  1901. if (ret) {
  1902. netdev_err(dev, "register_netdev failed %d\n", ret);
  1903. free_netdev(dev);
  1904. return;
  1905. }
  1906. }
  1907. /* Per-cpu module initialization. */
  1908. static void tile_net_init_module_percpu(void *unused)
  1909. {
  1910. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1911. int my_cpu = smp_processor_id();
  1912. int instance;
  1913. for (instance = 0; instance < NR_MPIPE_MAX; instance++) {
  1914. info->mpipe[instance].has_iqueue = false;
  1915. info->mpipe[instance].instance = instance;
  1916. }
  1917. info->my_cpu = my_cpu;
  1918. /* Initialize the egress timer. */
  1919. hrtimer_init(&info->egress_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1920. info->egress_timer.function = tile_net_handle_egress_timer;
  1921. }
  1922. /* Module initialization. */
  1923. static int __init tile_net_init_module(void)
  1924. {
  1925. int i;
  1926. char name[GXIO_MPIPE_LINK_NAME_LEN];
  1927. uint8_t mac[6];
  1928. pr_info("Tilera Network Driver\n");
  1929. BUILD_BUG_ON(NR_MPIPE_MAX != 2);
  1930. mutex_init(&tile_net_devs_for_channel_mutex);
  1931. /* Initialize each CPU. */
  1932. on_each_cpu(tile_net_init_module_percpu, NULL, 1);
  1933. /* Find out what devices we have, and initialize them. */
  1934. for (i = 0; gxio_mpipe_link_enumerate_mac(i, name, mac) >= 0; i++)
  1935. tile_net_dev_init(name, mac);
  1936. if (!network_cpus_init())
  1937. cpumask_and(&network_cpus_map, housekeeping_cpumask(),
  1938. cpu_online_mask);
  1939. return 0;
  1940. }
  1941. module_init(tile_net_init_module);