tilegx.c 64 KB

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