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. config.rx_filter = HWTSTAMP_FILTER_ALL;
  438. break;
  439. default:
  440. return -ERANGE;
  441. }
  442. if (copy_to_user(rq->ifr_data, &config, sizeof(config)))
  443. return -EFAULT;
  444. priv->stamp_cfg = config;
  445. return 0;
  446. }
  447. static int tile_hwtstamp_get(struct net_device *dev, struct ifreq *rq)
  448. {
  449. struct tile_net_priv *priv = netdev_priv(dev);
  450. if (copy_to_user(rq->ifr_data, &priv->stamp_cfg,
  451. sizeof(priv->stamp_cfg)))
  452. return -EFAULT;
  453. return 0;
  454. }
  455. static inline bool filter_packet(struct net_device *dev, void *buf)
  456. {
  457. /* Filter packets received before we're up. */
  458. if (dev == NULL || !(dev->flags & IFF_UP))
  459. return true;
  460. /* Filter out packets that aren't for us. */
  461. if (!(dev->flags & IFF_PROMISC) &&
  462. !is_multicast_ether_addr(buf) &&
  463. !ether_addr_equal(dev->dev_addr, buf))
  464. return true;
  465. return false;
  466. }
  467. static void tile_net_receive_skb(struct net_device *dev, struct sk_buff *skb,
  468. gxio_mpipe_idesc_t *idesc, unsigned long len)
  469. {
  470. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  471. struct tile_net_priv *priv = netdev_priv(dev);
  472. int instance = priv->instance;
  473. /* Encode the actual packet length. */
  474. skb_put(skb, len);
  475. skb->protocol = eth_type_trans(skb, dev);
  476. /* Acknowledge "good" hardware checksums. */
  477. if (idesc->cs && idesc->csum_seed_val == 0xFFFF)
  478. skb->ip_summed = CHECKSUM_UNNECESSARY;
  479. /* Get RX timestamp from idesc. */
  480. tile_rx_timestamp(priv, skb, idesc);
  481. napi_gro_receive(&info->mpipe[instance].napi, skb);
  482. /* Update stats. */
  483. tile_net_stats_add(1, &dev->stats.rx_packets);
  484. tile_net_stats_add(len, &dev->stats.rx_bytes);
  485. /* Need a new buffer. */
  486. if (idesc->size == buffer_size_enums[0])
  487. info->mpipe[instance].num_needed_buffers[0]++;
  488. else if (idesc->size == buffer_size_enums[1])
  489. info->mpipe[instance].num_needed_buffers[1]++;
  490. else
  491. info->mpipe[instance].num_needed_buffers[2]++;
  492. }
  493. /* Handle a packet. Return true if "processed", false if "filtered". */
  494. static bool tile_net_handle_packet(int instance, gxio_mpipe_idesc_t *idesc)
  495. {
  496. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  497. struct mpipe_data *md = &mpipe_data[instance];
  498. struct net_device *dev = md->tile_net_devs_for_channel[idesc->channel];
  499. uint8_t l2_offset;
  500. void *va;
  501. void *buf;
  502. unsigned long len;
  503. bool filter;
  504. /* Drop packets for which no buffer was available (which can
  505. * happen under heavy load), or for which the me/tr/ce flags
  506. * are set (which can happen for jumbo cut-through packets,
  507. * or with a customized classifier).
  508. */
  509. if (idesc->be || idesc->me || idesc->tr || idesc->ce) {
  510. if (dev)
  511. tile_net_stats_add(1, &dev->stats.rx_errors);
  512. goto drop;
  513. }
  514. /* Get the "l2_offset", if allowed. */
  515. l2_offset = custom_flag ? 0 : gxio_mpipe_idesc_get_l2_offset(idesc);
  516. /* Get the VA (including NET_IP_ALIGN bytes of "headroom"). */
  517. va = tile_io_addr_to_va((unsigned long)idesc->va);
  518. /* Get the actual packet start/length. */
  519. buf = va + l2_offset;
  520. len = idesc->l2_size - l2_offset;
  521. /* Point "va" at the raw buffer. */
  522. va -= NET_IP_ALIGN;
  523. filter = filter_packet(dev, buf);
  524. if (filter) {
  525. if (dev)
  526. tile_net_stats_add(1, &dev->stats.rx_dropped);
  527. drop:
  528. gxio_mpipe_iqueue_drop(&info->mpipe[instance].iqueue, idesc);
  529. } else {
  530. struct sk_buff *skb = mpipe_buf_to_skb(va);
  531. /* Skip headroom, and any custom header. */
  532. skb_reserve(skb, NET_IP_ALIGN + l2_offset);
  533. tile_net_receive_skb(dev, skb, idesc, len);
  534. }
  535. gxio_mpipe_iqueue_consume(&info->mpipe[instance].iqueue, idesc);
  536. return !filter;
  537. }
  538. /* Handle some packets for the current CPU.
  539. *
  540. * This function handles up to TILE_NET_BATCH idescs per call.
  541. *
  542. * ISSUE: Since we do not provide new buffers until this function is
  543. * complete, we must initially provide enough buffers for each network
  544. * cpu to fill its iqueue and also its batched idescs.
  545. *
  546. * ISSUE: The "rotting packet" race condition occurs if a packet
  547. * arrives after the queue appears to be empty, and before the
  548. * hypervisor interrupt is re-enabled.
  549. */
  550. static int tile_net_poll(struct napi_struct *napi, int budget)
  551. {
  552. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  553. unsigned int work = 0;
  554. gxio_mpipe_idesc_t *idesc;
  555. int instance, i, n;
  556. struct mpipe_data *md;
  557. struct info_mpipe *info_mpipe =
  558. container_of(napi, struct info_mpipe, napi);
  559. if (budget <= 0)
  560. goto done;
  561. instance = info_mpipe->instance;
  562. while ((n = gxio_mpipe_iqueue_try_peek(
  563. &info_mpipe->iqueue,
  564. &idesc)) > 0) {
  565. for (i = 0; i < n; i++) {
  566. if (i == TILE_NET_BATCH)
  567. goto done;
  568. if (tile_net_handle_packet(instance,
  569. idesc + i)) {
  570. if (++work >= budget)
  571. goto done;
  572. }
  573. }
  574. }
  575. /* There are no packets left. */
  576. napi_complete(&info_mpipe->napi);
  577. md = &mpipe_data[instance];
  578. /* Re-enable hypervisor interrupts. */
  579. gxio_mpipe_enable_notif_ring_interrupt(
  580. &md->context, info->mpipe[instance].iqueue.ring);
  581. /* HACK: Avoid the "rotting packet" problem. */
  582. if (gxio_mpipe_iqueue_try_peek(&info_mpipe->iqueue, &idesc) > 0)
  583. napi_schedule(&info_mpipe->napi);
  584. /* ISSUE: Handle completions? */
  585. done:
  586. tile_net_provide_needed_buffers();
  587. return work;
  588. }
  589. /* Handle an ingress interrupt from an instance on the current cpu. */
  590. static irqreturn_t tile_net_handle_ingress_irq(int irq, void *id)
  591. {
  592. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  593. napi_schedule(&info->mpipe[(uint64_t)id].napi);
  594. return IRQ_HANDLED;
  595. }
  596. /* Free some completions. This must be called with interrupts blocked. */
  597. static int tile_net_free_comps(gxio_mpipe_equeue_t *equeue,
  598. struct tile_net_comps *comps,
  599. int limit, bool force_update)
  600. {
  601. int n = 0;
  602. while (comps->comp_last < comps->comp_next) {
  603. unsigned int cid = comps->comp_last % TILE_NET_MAX_COMPS;
  604. struct tile_net_comp *comp = &comps->comp_queue[cid];
  605. if (!gxio_mpipe_equeue_is_complete(equeue, comp->when,
  606. force_update || n == 0))
  607. break;
  608. dev_kfree_skb_irq(comp->skb);
  609. comps->comp_last++;
  610. if (++n == limit)
  611. break;
  612. }
  613. return n;
  614. }
  615. /* Add a completion. This must be called with interrupts blocked.
  616. * tile_net_equeue_try_reserve() will have ensured a free completion entry.
  617. */
  618. static void add_comp(gxio_mpipe_equeue_t *equeue,
  619. struct tile_net_comps *comps,
  620. uint64_t when, struct sk_buff *skb)
  621. {
  622. int cid = comps->comp_next % TILE_NET_MAX_COMPS;
  623. comps->comp_queue[cid].when = when;
  624. comps->comp_queue[cid].skb = skb;
  625. comps->comp_next++;
  626. }
  627. static void tile_net_schedule_tx_wake_timer(struct net_device *dev,
  628. int tx_queue_idx)
  629. {
  630. struct tile_net_info *info = &per_cpu(per_cpu_info, tx_queue_idx);
  631. struct tile_net_priv *priv = netdev_priv(dev);
  632. int instance = priv->instance;
  633. struct tile_net_tx_wake *tx_wake =
  634. &info->mpipe[instance].tx_wake[priv->echannel];
  635. hrtimer_start(&tx_wake->timer,
  636. TX_TIMER_DELAY_USEC * 1000UL,
  637. HRTIMER_MODE_REL_PINNED);
  638. }
  639. static enum hrtimer_restart tile_net_handle_tx_wake_timer(struct hrtimer *t)
  640. {
  641. struct tile_net_tx_wake *tx_wake =
  642. container_of(t, struct tile_net_tx_wake, timer);
  643. netif_wake_subqueue(tx_wake->dev, tx_wake->tx_queue_idx);
  644. return HRTIMER_NORESTART;
  645. }
  646. /* Make sure the egress timer is scheduled. */
  647. static void tile_net_schedule_egress_timer(void)
  648. {
  649. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  650. if (!info->egress_timer_scheduled) {
  651. hrtimer_start(&info->egress_timer,
  652. EGRESS_TIMER_DELAY_USEC * 1000UL,
  653. HRTIMER_MODE_REL_PINNED);
  654. info->egress_timer_scheduled = true;
  655. }
  656. }
  657. /* The "function" for "info->egress_timer".
  658. *
  659. * This timer will reschedule itself as long as there are any pending
  660. * completions expected for this tile.
  661. */
  662. static enum hrtimer_restart tile_net_handle_egress_timer(struct hrtimer *t)
  663. {
  664. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  665. unsigned long irqflags;
  666. bool pending = false;
  667. int i, instance;
  668. local_irq_save(irqflags);
  669. /* The timer is no longer scheduled. */
  670. info->egress_timer_scheduled = false;
  671. /* Free all possible comps for this tile. */
  672. for (instance = 0; instance < NR_MPIPE_MAX &&
  673. info->mpipe[instance].has_iqueue; instance++) {
  674. for (i = 0; i < TILE_NET_CHANNELS; i++) {
  675. struct tile_net_egress *egress =
  676. &mpipe_data[instance].egress_for_echannel[i];
  677. struct tile_net_comps *comps =
  678. info->mpipe[instance].comps_for_echannel[i];
  679. if (!egress || comps->comp_last >= comps->comp_next)
  680. continue;
  681. tile_net_free_comps(egress->equeue, comps, -1, true);
  682. pending = pending ||
  683. (comps->comp_last < comps->comp_next);
  684. }
  685. }
  686. /* Reschedule timer if needed. */
  687. if (pending)
  688. tile_net_schedule_egress_timer();
  689. local_irq_restore(irqflags);
  690. return HRTIMER_NORESTART;
  691. }
  692. /* PTP clock operations. */
  693. static int ptp_mpipe_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  694. {
  695. int ret = 0;
  696. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  697. mutex_lock(&md->ptp_lock);
  698. if (gxio_mpipe_adjust_timestamp_freq(&md->context, ppb))
  699. ret = -EINVAL;
  700. mutex_unlock(&md->ptp_lock);
  701. return ret;
  702. }
  703. static int ptp_mpipe_adjtime(struct ptp_clock_info *ptp, s64 delta)
  704. {
  705. int ret = 0;
  706. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  707. mutex_lock(&md->ptp_lock);
  708. if (gxio_mpipe_adjust_timestamp(&md->context, delta))
  709. ret = -EBUSY;
  710. mutex_unlock(&md->ptp_lock);
  711. return ret;
  712. }
  713. static int ptp_mpipe_gettime(struct ptp_clock_info *ptp,
  714. struct timespec64 *ts)
  715. {
  716. int ret = 0;
  717. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  718. mutex_lock(&md->ptp_lock);
  719. if (gxio_mpipe_get_timestamp(&md->context, ts))
  720. ret = -EBUSY;
  721. mutex_unlock(&md->ptp_lock);
  722. return ret;
  723. }
  724. static int ptp_mpipe_settime(struct ptp_clock_info *ptp,
  725. const struct timespec64 *ts)
  726. {
  727. int ret = 0;
  728. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  729. mutex_lock(&md->ptp_lock);
  730. if (gxio_mpipe_set_timestamp(&md->context, ts))
  731. ret = -EBUSY;
  732. mutex_unlock(&md->ptp_lock);
  733. return ret;
  734. }
  735. static int ptp_mpipe_enable(struct ptp_clock_info *ptp,
  736. struct ptp_clock_request *request, int on)
  737. {
  738. return -EOPNOTSUPP;
  739. }
  740. static struct ptp_clock_info ptp_mpipe_caps = {
  741. .owner = THIS_MODULE,
  742. .name = "mPIPE clock",
  743. .max_adj = 999999999,
  744. .n_ext_ts = 0,
  745. .n_pins = 0,
  746. .pps = 0,
  747. .adjfreq = ptp_mpipe_adjfreq,
  748. .adjtime = ptp_mpipe_adjtime,
  749. .gettime64 = ptp_mpipe_gettime,
  750. .settime64 = ptp_mpipe_settime,
  751. .enable = ptp_mpipe_enable,
  752. };
  753. /* Sync mPIPE's timestamp up with Linux system time and register PTP clock. */
  754. static void register_ptp_clock(struct net_device *dev, struct mpipe_data *md)
  755. {
  756. struct timespec64 ts;
  757. ktime_get_ts64(&ts);
  758. gxio_mpipe_set_timestamp(&md->context, &ts);
  759. mutex_init(&md->ptp_lock);
  760. md->caps = ptp_mpipe_caps;
  761. md->ptp_clock = ptp_clock_register(&md->caps, NULL);
  762. if (IS_ERR(md->ptp_clock))
  763. netdev_err(dev, "ptp_clock_register failed %ld\n",
  764. PTR_ERR(md->ptp_clock));
  765. }
  766. /* Initialize PTP fields in a new device. */
  767. static void init_ptp_dev(struct tile_net_priv *priv)
  768. {
  769. priv->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
  770. priv->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
  771. }
  772. /* Helper functions for "tile_net_update()". */
  773. static void enable_ingress_irq(void *irq)
  774. {
  775. enable_percpu_irq((long)irq, 0);
  776. }
  777. static void disable_ingress_irq(void *irq)
  778. {
  779. disable_percpu_irq((long)irq);
  780. }
  781. /* Helper function for tile_net_open() and tile_net_stop().
  782. * Always called under tile_net_devs_for_channel_mutex.
  783. */
  784. static int tile_net_update(struct net_device *dev)
  785. {
  786. static gxio_mpipe_rules_t rules; /* too big to fit on the stack */
  787. bool saw_channel = false;
  788. int instance = mpipe_instance(dev);
  789. struct mpipe_data *md = &mpipe_data[instance];
  790. int channel;
  791. int rc;
  792. int cpu;
  793. saw_channel = false;
  794. gxio_mpipe_rules_init(&rules, &md->context);
  795. for (channel = 0; channel < TILE_NET_CHANNELS; channel++) {
  796. if (md->tile_net_devs_for_channel[channel] == NULL)
  797. continue;
  798. if (!saw_channel) {
  799. saw_channel = true;
  800. gxio_mpipe_rules_begin(&rules, md->first_bucket,
  801. md->num_buckets, NULL);
  802. gxio_mpipe_rules_set_headroom(&rules, NET_IP_ALIGN);
  803. }
  804. gxio_mpipe_rules_add_channel(&rules, channel);
  805. }
  806. /* NOTE: This can fail if there is no classifier.
  807. * ISSUE: Can anything else cause it to fail?
  808. */
  809. rc = gxio_mpipe_rules_commit(&rules);
  810. if (rc != 0) {
  811. netdev_warn(dev, "gxio_mpipe_rules_commit: mpipe[%d] %d\n",
  812. instance, rc);
  813. return -EIO;
  814. }
  815. /* Update all cpus, sequentially (to protect "netif_napi_add()").
  816. * We use on_each_cpu to handle the IPI mask or unmask.
  817. */
  818. if (!saw_channel)
  819. on_each_cpu(disable_ingress_irq,
  820. (void *)(long)(md->ingress_irq), 1);
  821. for_each_online_cpu(cpu) {
  822. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  823. if (!info->mpipe[instance].has_iqueue)
  824. continue;
  825. if (saw_channel) {
  826. if (!info->mpipe[instance].napi_added) {
  827. netif_napi_add(dev, &info->mpipe[instance].napi,
  828. tile_net_poll, TILE_NET_WEIGHT);
  829. info->mpipe[instance].napi_added = true;
  830. }
  831. if (!info->mpipe[instance].napi_enabled) {
  832. napi_enable(&info->mpipe[instance].napi);
  833. info->mpipe[instance].napi_enabled = true;
  834. }
  835. } else {
  836. if (info->mpipe[instance].napi_enabled) {
  837. napi_disable(&info->mpipe[instance].napi);
  838. info->mpipe[instance].napi_enabled = false;
  839. }
  840. /* FIXME: Drain the iqueue. */
  841. }
  842. }
  843. if (saw_channel)
  844. on_each_cpu(enable_ingress_irq,
  845. (void *)(long)(md->ingress_irq), 1);
  846. /* HACK: Allow packets to flow in the simulator. */
  847. if (saw_channel)
  848. sim_enable_mpipe_links(instance, -1);
  849. return 0;
  850. }
  851. /* Initialize a buffer stack. */
  852. static int create_buffer_stack(struct net_device *dev,
  853. int kind, size_t num_buffers)
  854. {
  855. pte_t hash_pte = pte_set_home((pte_t) { 0 }, PAGE_HOME_HASH);
  856. int instance = mpipe_instance(dev);
  857. struct mpipe_data *md = &mpipe_data[instance];
  858. size_t needed = gxio_mpipe_calc_buffer_stack_bytes(num_buffers);
  859. int stack_idx = md->first_buffer_stack + kind;
  860. void *va;
  861. int i, rc;
  862. /* Round up to 64KB and then use alloc_pages() so we get the
  863. * required 64KB alignment.
  864. */
  865. md->buffer_stack_bytes[kind] =
  866. ALIGN(needed, 64 * 1024);
  867. va = alloc_pages_exact(md->buffer_stack_bytes[kind], GFP_KERNEL);
  868. if (va == NULL) {
  869. netdev_err(dev,
  870. "Could not alloc %zd bytes for buffer stack %d\n",
  871. md->buffer_stack_bytes[kind], kind);
  872. return -ENOMEM;
  873. }
  874. /* Initialize the buffer stack. */
  875. rc = gxio_mpipe_init_buffer_stack(&md->context, stack_idx,
  876. buffer_size_enums[kind], va,
  877. md->buffer_stack_bytes[kind], 0);
  878. if (rc != 0) {
  879. netdev_err(dev, "gxio_mpipe_init_buffer_stack: mpipe[%d] %d\n",
  880. instance, rc);
  881. free_pages_exact(va, md->buffer_stack_bytes[kind]);
  882. return rc;
  883. }
  884. md->buffer_stack_vas[kind] = va;
  885. rc = gxio_mpipe_register_client_memory(&md->context, stack_idx,
  886. hash_pte, 0);
  887. if (rc != 0) {
  888. netdev_err(dev,
  889. "gxio_mpipe_register_client_memory: mpipe[%d] %d\n",
  890. instance, rc);
  891. return rc;
  892. }
  893. /* Provide initial buffers. */
  894. for (i = 0; i < num_buffers; i++) {
  895. if (!tile_net_provide_buffer(instance, kind)) {
  896. netdev_err(dev, "Cannot allocate initial sk_bufs!\n");
  897. return -ENOMEM;
  898. }
  899. }
  900. return 0;
  901. }
  902. /* Allocate and initialize mpipe buffer stacks, and register them in
  903. * the mPIPE TLBs, for small, large, and (possibly) jumbo packet sizes.
  904. * This routine supports tile_net_init_mpipe(), below.
  905. */
  906. static int init_buffer_stacks(struct net_device *dev,
  907. int network_cpus_count)
  908. {
  909. int num_kinds = MAX_KINDS - (jumbo_num == 0);
  910. size_t num_buffers;
  911. int rc;
  912. int instance = mpipe_instance(dev);
  913. struct mpipe_data *md = &mpipe_data[instance];
  914. /* Allocate the buffer stacks. */
  915. rc = gxio_mpipe_alloc_buffer_stacks(&md->context, num_kinds, 0, 0);
  916. if (rc < 0) {
  917. netdev_err(dev,
  918. "gxio_mpipe_alloc_buffer_stacks: mpipe[%d] %d\n",
  919. instance, rc);
  920. return rc;
  921. }
  922. md->first_buffer_stack = rc;
  923. /* Enough small/large buffers to (normally) avoid buffer errors. */
  924. num_buffers =
  925. network_cpus_count * (IQUEUE_ENTRIES + TILE_NET_BATCH);
  926. /* Allocate the small memory stack. */
  927. if (rc >= 0)
  928. rc = create_buffer_stack(dev, 0, num_buffers);
  929. /* Allocate the large buffer stack. */
  930. if (rc >= 0)
  931. rc = create_buffer_stack(dev, 1, num_buffers);
  932. /* Allocate the jumbo buffer stack if needed. */
  933. if (rc >= 0 && jumbo_num != 0)
  934. rc = create_buffer_stack(dev, 2, jumbo_num);
  935. return rc;
  936. }
  937. /* Allocate per-cpu resources (memory for completions and idescs).
  938. * This routine supports tile_net_init_mpipe(), below.
  939. */
  940. static int alloc_percpu_mpipe_resources(struct net_device *dev,
  941. int cpu, int ring)
  942. {
  943. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  944. int order, i, rc;
  945. int instance = mpipe_instance(dev);
  946. struct mpipe_data *md = &mpipe_data[instance];
  947. struct page *page;
  948. void *addr;
  949. /* Allocate the "comps". */
  950. order = get_order(COMPS_SIZE);
  951. page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
  952. if (page == NULL) {
  953. netdev_err(dev, "Failed to alloc %zd bytes comps memory\n",
  954. COMPS_SIZE);
  955. return -ENOMEM;
  956. }
  957. addr = pfn_to_kaddr(page_to_pfn(page));
  958. memset(addr, 0, COMPS_SIZE);
  959. for (i = 0; i < TILE_NET_CHANNELS; i++)
  960. info->mpipe[instance].comps_for_echannel[i] =
  961. addr + i * sizeof(struct tile_net_comps);
  962. /* If this is a network cpu, create an iqueue. */
  963. if (cpumask_test_cpu(cpu, &network_cpus_map)) {
  964. order = get_order(NOTIF_RING_SIZE);
  965. page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
  966. if (page == NULL) {
  967. netdev_err(dev,
  968. "Failed to alloc %zd bytes iqueue memory\n",
  969. NOTIF_RING_SIZE);
  970. return -ENOMEM;
  971. }
  972. addr = pfn_to_kaddr(page_to_pfn(page));
  973. rc = gxio_mpipe_iqueue_init(&info->mpipe[instance].iqueue,
  974. &md->context, ring++, addr,
  975. NOTIF_RING_SIZE, 0);
  976. if (rc < 0) {
  977. netdev_err(dev,
  978. "gxio_mpipe_iqueue_init failed: %d\n", rc);
  979. return rc;
  980. }
  981. info->mpipe[instance].has_iqueue = true;
  982. }
  983. return ring;
  984. }
  985. /* Initialize NotifGroup and buckets.
  986. * This routine supports tile_net_init_mpipe(), below.
  987. */
  988. static int init_notif_group_and_buckets(struct net_device *dev,
  989. int ring, int network_cpus_count)
  990. {
  991. int group, rc;
  992. int instance = mpipe_instance(dev);
  993. struct mpipe_data *md = &mpipe_data[instance];
  994. /* Allocate one NotifGroup. */
  995. rc = gxio_mpipe_alloc_notif_groups(&md->context, 1, 0, 0);
  996. if (rc < 0) {
  997. netdev_err(dev, "gxio_mpipe_alloc_notif_groups: mpipe[%d] %d\n",
  998. instance, rc);
  999. return rc;
  1000. }
  1001. group = rc;
  1002. /* Initialize global num_buckets value. */
  1003. if (network_cpus_count > 4)
  1004. md->num_buckets = 256;
  1005. else if (network_cpus_count > 1)
  1006. md->num_buckets = 16;
  1007. /* Allocate some buckets, and set global first_bucket value. */
  1008. rc = gxio_mpipe_alloc_buckets(&md->context, md->num_buckets, 0, 0);
  1009. if (rc < 0) {
  1010. netdev_err(dev, "gxio_mpipe_alloc_buckets: mpipe[%d] %d\n",
  1011. instance, rc);
  1012. return rc;
  1013. }
  1014. md->first_bucket = rc;
  1015. /* Init group and buckets. */
  1016. rc = gxio_mpipe_init_notif_group_and_buckets(
  1017. &md->context, group, ring, network_cpus_count,
  1018. md->first_bucket, md->num_buckets,
  1019. GXIO_MPIPE_BUCKET_STICKY_FLOW_LOCALITY);
  1020. if (rc != 0) {
  1021. netdev_err(dev, "gxio_mpipe_init_notif_group_and_buckets: "
  1022. "mpipe[%d] %d\n", instance, rc);
  1023. return rc;
  1024. }
  1025. return 0;
  1026. }
  1027. /* Create an irq and register it, then activate the irq and request
  1028. * interrupts on all cores. Note that "ingress_irq" being initialized
  1029. * is how we know not to call tile_net_init_mpipe() again.
  1030. * This routine supports tile_net_init_mpipe(), below.
  1031. */
  1032. static int tile_net_setup_interrupts(struct net_device *dev)
  1033. {
  1034. int cpu, rc, irq;
  1035. int instance = mpipe_instance(dev);
  1036. struct mpipe_data *md = &mpipe_data[instance];
  1037. irq = md->ingress_irq;
  1038. if (irq < 0) {
  1039. irq = irq_alloc_hwirq(-1);
  1040. if (!irq) {
  1041. netdev_err(dev,
  1042. "create_irq failed: mpipe[%d] %d\n",
  1043. instance, irq);
  1044. return irq;
  1045. }
  1046. tile_irq_activate(irq, TILE_IRQ_PERCPU);
  1047. rc = request_irq(irq, tile_net_handle_ingress_irq,
  1048. 0, "tile_net", (void *)((uint64_t)instance));
  1049. if (rc != 0) {
  1050. netdev_err(dev, "request_irq failed: mpipe[%d] %d\n",
  1051. instance, rc);
  1052. irq_free_hwirq(irq);
  1053. return rc;
  1054. }
  1055. md->ingress_irq = irq;
  1056. }
  1057. for_each_online_cpu(cpu) {
  1058. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1059. if (info->mpipe[instance].has_iqueue) {
  1060. gxio_mpipe_request_notif_ring_interrupt(&md->context,
  1061. cpu_x(cpu), cpu_y(cpu), KERNEL_PL, irq,
  1062. info->mpipe[instance].iqueue.ring);
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. /* Undo any state set up partially by a failed call to tile_net_init_mpipe. */
  1068. static void tile_net_init_mpipe_fail(int instance)
  1069. {
  1070. int kind, cpu;
  1071. struct mpipe_data *md = &mpipe_data[instance];
  1072. /* Do cleanups that require the mpipe context first. */
  1073. for (kind = 0; kind < MAX_KINDS; kind++) {
  1074. if (md->buffer_stack_vas[kind] != NULL) {
  1075. tile_net_pop_all_buffers(instance,
  1076. md->first_buffer_stack +
  1077. kind);
  1078. }
  1079. }
  1080. /* Destroy mpipe context so the hardware no longer owns any memory. */
  1081. gxio_mpipe_destroy(&md->context);
  1082. for_each_online_cpu(cpu) {
  1083. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1084. free_pages(
  1085. (unsigned long)(
  1086. info->mpipe[instance].comps_for_echannel[0]),
  1087. get_order(COMPS_SIZE));
  1088. info->mpipe[instance].comps_for_echannel[0] = NULL;
  1089. free_pages((unsigned long)(info->mpipe[instance].iqueue.idescs),
  1090. get_order(NOTIF_RING_SIZE));
  1091. info->mpipe[instance].iqueue.idescs = NULL;
  1092. }
  1093. for (kind = 0; kind < MAX_KINDS; kind++) {
  1094. if (md->buffer_stack_vas[kind] != NULL) {
  1095. free_pages_exact(md->buffer_stack_vas[kind],
  1096. md->buffer_stack_bytes[kind]);
  1097. md->buffer_stack_vas[kind] = NULL;
  1098. }
  1099. }
  1100. md->first_buffer_stack = -1;
  1101. md->first_bucket = -1;
  1102. }
  1103. /* The first time any tilegx network device is opened, we initialize
  1104. * the global mpipe state. If this step fails, we fail to open the
  1105. * device, but if it succeeds, we never need to do it again, and since
  1106. * tile_net can't be unloaded, we never undo it.
  1107. *
  1108. * Note that some resources in this path (buffer stack indices,
  1109. * bindings from init_buffer_stack, etc.) are hypervisor resources
  1110. * that are freed implicitly by gxio_mpipe_destroy().
  1111. */
  1112. static int tile_net_init_mpipe(struct net_device *dev)
  1113. {
  1114. int rc;
  1115. int cpu;
  1116. int first_ring, ring;
  1117. int instance = mpipe_instance(dev);
  1118. struct mpipe_data *md = &mpipe_data[instance];
  1119. int network_cpus_count = cpumask_weight(&network_cpus_map);
  1120. if (!hash_default) {
  1121. netdev_err(dev, "Networking requires hash_default!\n");
  1122. return -EIO;
  1123. }
  1124. rc = gxio_mpipe_init(&md->context, instance);
  1125. if (rc != 0) {
  1126. netdev_err(dev, "gxio_mpipe_init: mpipe[%d] %d\n",
  1127. instance, rc);
  1128. return -EIO;
  1129. }
  1130. /* Set up the buffer stacks. */
  1131. rc = init_buffer_stacks(dev, network_cpus_count);
  1132. if (rc != 0)
  1133. goto fail;
  1134. /* Allocate one NotifRing for each network cpu. */
  1135. rc = gxio_mpipe_alloc_notif_rings(&md->context,
  1136. network_cpus_count, 0, 0);
  1137. if (rc < 0) {
  1138. netdev_err(dev, "gxio_mpipe_alloc_notif_rings failed %d\n",
  1139. rc);
  1140. goto fail;
  1141. }
  1142. /* Init NotifRings per-cpu. */
  1143. first_ring = rc;
  1144. ring = first_ring;
  1145. for_each_online_cpu(cpu) {
  1146. rc = alloc_percpu_mpipe_resources(dev, cpu, ring);
  1147. if (rc < 0)
  1148. goto fail;
  1149. ring = rc;
  1150. }
  1151. /* Initialize NotifGroup and buckets. */
  1152. rc = init_notif_group_and_buckets(dev, first_ring, network_cpus_count);
  1153. if (rc != 0)
  1154. goto fail;
  1155. /* Create and enable interrupts. */
  1156. rc = tile_net_setup_interrupts(dev);
  1157. if (rc != 0)
  1158. goto fail;
  1159. /* Register PTP clock and set mPIPE timestamp, if configured. */
  1160. register_ptp_clock(dev, md);
  1161. return 0;
  1162. fail:
  1163. tile_net_init_mpipe_fail(instance);
  1164. return rc;
  1165. }
  1166. /* Create persistent egress info for a given egress channel.
  1167. * Note that this may be shared between, say, "gbe0" and "xgbe0".
  1168. * ISSUE: Defer header allocation until TSO is actually needed?
  1169. */
  1170. static int tile_net_init_egress(struct net_device *dev, int echannel)
  1171. {
  1172. static int ering = -1;
  1173. struct page *headers_page, *edescs_page, *equeue_page;
  1174. gxio_mpipe_edesc_t *edescs;
  1175. gxio_mpipe_equeue_t *equeue;
  1176. unsigned char *headers;
  1177. int headers_order, edescs_order, equeue_order;
  1178. size_t edescs_size;
  1179. int rc = -ENOMEM;
  1180. int instance = mpipe_instance(dev);
  1181. struct mpipe_data *md = &mpipe_data[instance];
  1182. /* Only initialize once. */
  1183. if (md->egress_for_echannel[echannel].equeue != NULL)
  1184. return 0;
  1185. /* Allocate memory for the "headers". */
  1186. headers_order = get_order(EQUEUE_ENTRIES * HEADER_BYTES);
  1187. headers_page = alloc_pages(GFP_KERNEL, headers_order);
  1188. if (headers_page == NULL) {
  1189. netdev_warn(dev,
  1190. "Could not alloc %zd bytes for TSO headers.\n",
  1191. PAGE_SIZE << headers_order);
  1192. goto fail;
  1193. }
  1194. headers = pfn_to_kaddr(page_to_pfn(headers_page));
  1195. /* Allocate memory for the "edescs". */
  1196. edescs_size = EQUEUE_ENTRIES * sizeof(*edescs);
  1197. edescs_order = get_order(edescs_size);
  1198. edescs_page = alloc_pages(GFP_KERNEL, edescs_order);
  1199. if (edescs_page == NULL) {
  1200. netdev_warn(dev,
  1201. "Could not alloc %zd bytes for eDMA ring.\n",
  1202. edescs_size);
  1203. goto fail_headers;
  1204. }
  1205. edescs = pfn_to_kaddr(page_to_pfn(edescs_page));
  1206. /* Allocate memory for the "equeue". */
  1207. equeue_order = get_order(sizeof(*equeue));
  1208. equeue_page = alloc_pages(GFP_KERNEL, equeue_order);
  1209. if (equeue_page == NULL) {
  1210. netdev_warn(dev,
  1211. "Could not alloc %zd bytes for equeue info.\n",
  1212. PAGE_SIZE << equeue_order);
  1213. goto fail_edescs;
  1214. }
  1215. equeue = pfn_to_kaddr(page_to_pfn(equeue_page));
  1216. /* Allocate an edma ring (using a one entry "free list"). */
  1217. if (ering < 0) {
  1218. rc = gxio_mpipe_alloc_edma_rings(&md->context, 1, 0, 0);
  1219. if (rc < 0) {
  1220. netdev_warn(dev, "gxio_mpipe_alloc_edma_rings: "
  1221. "mpipe[%d] %d\n", instance, rc);
  1222. goto fail_equeue;
  1223. }
  1224. ering = rc;
  1225. }
  1226. /* Initialize the equeue. */
  1227. rc = gxio_mpipe_equeue_init(equeue, &md->context, ering, echannel,
  1228. edescs, edescs_size, 0);
  1229. if (rc != 0) {
  1230. netdev_err(dev, "gxio_mpipe_equeue_init: mpipe[%d] %d\n",
  1231. instance, rc);
  1232. goto fail_equeue;
  1233. }
  1234. /* Don't reuse the ering later. */
  1235. ering = -1;
  1236. if (jumbo_num != 0) {
  1237. /* Make sure "jumbo" packets can be egressed safely. */
  1238. if (gxio_mpipe_equeue_set_snf_size(equeue, 10368) < 0) {
  1239. /* ISSUE: There is no "gxio_mpipe_equeue_destroy()". */
  1240. netdev_warn(dev, "Jumbo packets may not be egressed"
  1241. " properly on channel %d\n", echannel);
  1242. }
  1243. }
  1244. /* Done. */
  1245. md->egress_for_echannel[echannel].equeue = equeue;
  1246. md->egress_for_echannel[echannel].headers = headers;
  1247. return 0;
  1248. fail_equeue:
  1249. __free_pages(equeue_page, equeue_order);
  1250. fail_edescs:
  1251. __free_pages(edescs_page, edescs_order);
  1252. fail_headers:
  1253. __free_pages(headers_page, headers_order);
  1254. fail:
  1255. return rc;
  1256. }
  1257. /* Return channel number for a newly-opened link. */
  1258. static int tile_net_link_open(struct net_device *dev, gxio_mpipe_link_t *link,
  1259. const char *link_name)
  1260. {
  1261. int instance = mpipe_instance(dev);
  1262. struct mpipe_data *md = &mpipe_data[instance];
  1263. int rc = gxio_mpipe_link_open(link, &md->context, link_name, 0);
  1264. if (rc < 0) {
  1265. netdev_err(dev, "Failed to open '%s', mpipe[%d], %d\n",
  1266. link_name, instance, rc);
  1267. return rc;
  1268. }
  1269. if (jumbo_num != 0) {
  1270. u32 attr = GXIO_MPIPE_LINK_RECEIVE_JUMBO;
  1271. rc = gxio_mpipe_link_set_attr(link, attr, 1);
  1272. if (rc != 0) {
  1273. netdev_err(dev,
  1274. "Cannot receive jumbo packets on '%s'\n",
  1275. link_name);
  1276. gxio_mpipe_link_close(link);
  1277. return rc;
  1278. }
  1279. }
  1280. rc = gxio_mpipe_link_channel(link);
  1281. if (rc < 0 || rc >= TILE_NET_CHANNELS) {
  1282. netdev_err(dev, "gxio_mpipe_link_channel bad value: %d\n", rc);
  1283. gxio_mpipe_link_close(link);
  1284. return -EINVAL;
  1285. }
  1286. return rc;
  1287. }
  1288. /* Help the kernel activate the given network interface. */
  1289. static int tile_net_open(struct net_device *dev)
  1290. {
  1291. struct tile_net_priv *priv = netdev_priv(dev);
  1292. int cpu, rc, instance;
  1293. mutex_lock(&tile_net_devs_for_channel_mutex);
  1294. /* Get the instance info. */
  1295. rc = gxio_mpipe_link_instance(dev->name);
  1296. if (rc < 0 || rc >= NR_MPIPE_MAX) {
  1297. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1298. return -EIO;
  1299. }
  1300. priv->instance = rc;
  1301. instance = rc;
  1302. if (!mpipe_data[rc].context.mmio_fast_base) {
  1303. /* Do one-time initialization per instance the first time
  1304. * any device is opened.
  1305. */
  1306. rc = tile_net_init_mpipe(dev);
  1307. if (rc != 0)
  1308. goto fail;
  1309. }
  1310. /* Determine if this is the "loopify" device. */
  1311. if (unlikely((loopify_link_name != NULL) &&
  1312. !strcmp(dev->name, loopify_link_name))) {
  1313. rc = tile_net_link_open(dev, &priv->link, "loop0");
  1314. if (rc < 0)
  1315. goto fail;
  1316. priv->channel = rc;
  1317. rc = tile_net_link_open(dev, &priv->loopify_link, "loop1");
  1318. if (rc < 0)
  1319. goto fail;
  1320. priv->loopify_channel = rc;
  1321. priv->echannel = rc;
  1322. } else {
  1323. rc = tile_net_link_open(dev, &priv->link, dev->name);
  1324. if (rc < 0)
  1325. goto fail;
  1326. priv->channel = rc;
  1327. priv->echannel = rc;
  1328. }
  1329. /* Initialize egress info (if needed). Once ever, per echannel. */
  1330. rc = tile_net_init_egress(dev, priv->echannel);
  1331. if (rc != 0)
  1332. goto fail;
  1333. mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = dev;
  1334. rc = tile_net_update(dev);
  1335. if (rc != 0)
  1336. goto fail;
  1337. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1338. /* Initialize the transmit wake timer for this device for each cpu. */
  1339. for_each_online_cpu(cpu) {
  1340. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1341. struct tile_net_tx_wake *tx_wake =
  1342. &info->mpipe[instance].tx_wake[priv->echannel];
  1343. hrtimer_init(&tx_wake->timer, CLOCK_MONOTONIC,
  1344. HRTIMER_MODE_REL);
  1345. tx_wake->tx_queue_idx = cpu;
  1346. tx_wake->timer.function = tile_net_handle_tx_wake_timer;
  1347. tx_wake->dev = dev;
  1348. }
  1349. for_each_online_cpu(cpu)
  1350. netif_start_subqueue(dev, cpu);
  1351. netif_carrier_on(dev);
  1352. return 0;
  1353. fail:
  1354. if (priv->loopify_channel >= 0) {
  1355. if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
  1356. netdev_warn(dev, "Failed to close loopify link!\n");
  1357. priv->loopify_channel = -1;
  1358. }
  1359. if (priv->channel >= 0) {
  1360. if (gxio_mpipe_link_close(&priv->link) != 0)
  1361. netdev_warn(dev, "Failed to close link!\n");
  1362. priv->channel = -1;
  1363. }
  1364. priv->echannel = -1;
  1365. mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = NULL;
  1366. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1367. /* Don't return raw gxio error codes to generic Linux. */
  1368. return (rc > -512) ? rc : -EIO;
  1369. }
  1370. /* Help the kernel deactivate the given network interface. */
  1371. static int tile_net_stop(struct net_device *dev)
  1372. {
  1373. struct tile_net_priv *priv = netdev_priv(dev);
  1374. int cpu;
  1375. int instance = priv->instance;
  1376. struct mpipe_data *md = &mpipe_data[instance];
  1377. for_each_online_cpu(cpu) {
  1378. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1379. struct tile_net_tx_wake *tx_wake =
  1380. &info->mpipe[instance].tx_wake[priv->echannel];
  1381. hrtimer_cancel(&tx_wake->timer);
  1382. netif_stop_subqueue(dev, cpu);
  1383. }
  1384. mutex_lock(&tile_net_devs_for_channel_mutex);
  1385. md->tile_net_devs_for_channel[priv->channel] = NULL;
  1386. (void)tile_net_update(dev);
  1387. if (priv->loopify_channel >= 0) {
  1388. if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
  1389. netdev_warn(dev, "Failed to close loopify link!\n");
  1390. priv->loopify_channel = -1;
  1391. }
  1392. if (priv->channel >= 0) {
  1393. if (gxio_mpipe_link_close(&priv->link) != 0)
  1394. netdev_warn(dev, "Failed to close link!\n");
  1395. priv->channel = -1;
  1396. }
  1397. priv->echannel = -1;
  1398. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1399. return 0;
  1400. }
  1401. /* Determine the VA for a fragment. */
  1402. static inline void *tile_net_frag_buf(skb_frag_t *f)
  1403. {
  1404. unsigned long pfn = page_to_pfn(skb_frag_page(f));
  1405. return pfn_to_kaddr(pfn) + f->page_offset;
  1406. }
  1407. /* Acquire a completion entry and an egress slot, or if we can't,
  1408. * stop the queue and schedule the tx_wake timer.
  1409. */
  1410. static s64 tile_net_equeue_try_reserve(struct net_device *dev,
  1411. int tx_queue_idx,
  1412. struct tile_net_comps *comps,
  1413. gxio_mpipe_equeue_t *equeue,
  1414. int num_edescs)
  1415. {
  1416. /* Try to acquire a completion entry. */
  1417. if (comps->comp_next - comps->comp_last < TILE_NET_MAX_COMPS - 1 ||
  1418. tile_net_free_comps(equeue, comps, 32, false) != 0) {
  1419. /* Try to acquire an egress slot. */
  1420. s64 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
  1421. if (slot >= 0)
  1422. return slot;
  1423. /* Freeing some completions gives the equeue time to drain. */
  1424. tile_net_free_comps(equeue, comps, TILE_NET_MAX_COMPS, false);
  1425. slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
  1426. if (slot >= 0)
  1427. return slot;
  1428. }
  1429. /* Still nothing; give up and stop the queue for a short while. */
  1430. netif_stop_subqueue(dev, tx_queue_idx);
  1431. tile_net_schedule_tx_wake_timer(dev, tx_queue_idx);
  1432. return -1;
  1433. }
  1434. /* Determine how many edesc's are needed for TSO.
  1435. *
  1436. * Sometimes, if "sendfile()" requires copying, we will be called with
  1437. * "data" containing the header and payload, with "frags" being empty.
  1438. * Sometimes, for example when using NFS over TCP, a single segment can
  1439. * span 3 fragments. This requires special care.
  1440. */
  1441. static int tso_count_edescs(struct sk_buff *skb)
  1442. {
  1443. struct skb_shared_info *sh = skb_shinfo(skb);
  1444. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1445. unsigned int data_len = skb->len - sh_len;
  1446. unsigned int p_len = sh->gso_size;
  1447. long f_id = -1; /* id of the current fragment */
  1448. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1449. long f_used = 0; /* bytes used from the current fragment */
  1450. long n; /* size of the current piece of payload */
  1451. int num_edescs = 0;
  1452. int segment;
  1453. for (segment = 0; segment < sh->gso_segs; segment++) {
  1454. unsigned int p_used = 0;
  1455. /* One edesc for header and for each piece of the payload. */
  1456. for (num_edescs++; p_used < p_len; num_edescs++) {
  1457. /* Advance as needed. */
  1458. while (f_used >= f_size) {
  1459. f_id++;
  1460. f_size = skb_frag_size(&sh->frags[f_id]);
  1461. f_used = 0;
  1462. }
  1463. /* Use bytes from the current fragment. */
  1464. n = p_len - p_used;
  1465. if (n > f_size - f_used)
  1466. n = f_size - f_used;
  1467. f_used += n;
  1468. p_used += n;
  1469. }
  1470. /* The last segment may be less than gso_size. */
  1471. data_len -= p_len;
  1472. if (data_len < p_len)
  1473. p_len = data_len;
  1474. }
  1475. return num_edescs;
  1476. }
  1477. /* Prepare modified copies of the skbuff headers. */
  1478. static void tso_headers_prepare(struct sk_buff *skb, unsigned char *headers,
  1479. s64 slot)
  1480. {
  1481. struct skb_shared_info *sh = skb_shinfo(skb);
  1482. struct iphdr *ih;
  1483. struct ipv6hdr *ih6;
  1484. struct tcphdr *th;
  1485. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1486. unsigned int data_len = skb->len - sh_len;
  1487. unsigned char *data = skb->data;
  1488. unsigned int ih_off, th_off, p_len;
  1489. unsigned int isum_seed, tsum_seed, seq;
  1490. unsigned int uninitialized_var(id);
  1491. int is_ipv6;
  1492. long f_id = -1; /* id of the current fragment */
  1493. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1494. long f_used = 0; /* bytes used from the current fragment */
  1495. long n; /* size of the current piece of payload */
  1496. int segment;
  1497. /* Locate original headers and compute various lengths. */
  1498. is_ipv6 = skb_is_gso_v6(skb);
  1499. if (is_ipv6) {
  1500. ih6 = ipv6_hdr(skb);
  1501. ih_off = skb_network_offset(skb);
  1502. } else {
  1503. ih = ip_hdr(skb);
  1504. ih_off = skb_network_offset(skb);
  1505. isum_seed = ((0xFFFF - ih->check) +
  1506. (0xFFFF - ih->tot_len) +
  1507. (0xFFFF - ih->id));
  1508. id = ntohs(ih->id);
  1509. }
  1510. th = tcp_hdr(skb);
  1511. th_off = skb_transport_offset(skb);
  1512. p_len = sh->gso_size;
  1513. tsum_seed = th->check + (0xFFFF ^ htons(skb->len));
  1514. seq = ntohl(th->seq);
  1515. /* Prepare all the headers. */
  1516. for (segment = 0; segment < sh->gso_segs; segment++) {
  1517. unsigned char *buf;
  1518. unsigned int p_used = 0;
  1519. /* Copy to the header memory for this segment. */
  1520. buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
  1521. NET_IP_ALIGN;
  1522. memcpy(buf, data, sh_len);
  1523. /* Update copied ip header. */
  1524. if (is_ipv6) {
  1525. ih6 = (struct ipv6hdr *)(buf + ih_off);
  1526. ih6->payload_len = htons(sh_len + p_len - ih_off -
  1527. sizeof(*ih6));
  1528. } else {
  1529. ih = (struct iphdr *)(buf + ih_off);
  1530. ih->tot_len = htons(sh_len + p_len - ih_off);
  1531. ih->id = htons(id++);
  1532. ih->check = csum_long(isum_seed + ih->tot_len +
  1533. ih->id) ^ 0xffff;
  1534. }
  1535. /* Update copied tcp header. */
  1536. th = (struct tcphdr *)(buf + th_off);
  1537. th->seq = htonl(seq);
  1538. th->check = csum_long(tsum_seed + htons(sh_len + p_len));
  1539. if (segment != sh->gso_segs - 1) {
  1540. th->fin = 0;
  1541. th->psh = 0;
  1542. }
  1543. /* Skip past the header. */
  1544. slot++;
  1545. /* Skip past the payload. */
  1546. while (p_used < p_len) {
  1547. /* Advance as needed. */
  1548. while (f_used >= f_size) {
  1549. f_id++;
  1550. f_size = skb_frag_size(&sh->frags[f_id]);
  1551. f_used = 0;
  1552. }
  1553. /* Use bytes from the current fragment. */
  1554. n = p_len - p_used;
  1555. if (n > f_size - f_used)
  1556. n = f_size - f_used;
  1557. f_used += n;
  1558. p_used += n;
  1559. slot++;
  1560. }
  1561. seq += p_len;
  1562. /* The last segment may be less than gso_size. */
  1563. data_len -= p_len;
  1564. if (data_len < p_len)
  1565. p_len = data_len;
  1566. }
  1567. /* Flush the headers so they are ready for hardware DMA. */
  1568. wmb();
  1569. }
  1570. /* Pass all the data to mpipe for egress. */
  1571. static void tso_egress(struct net_device *dev, gxio_mpipe_equeue_t *equeue,
  1572. struct sk_buff *skb, unsigned char *headers, s64 slot)
  1573. {
  1574. struct skb_shared_info *sh = skb_shinfo(skb);
  1575. int instance = mpipe_instance(dev);
  1576. struct mpipe_data *md = &mpipe_data[instance];
  1577. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1578. unsigned int data_len = skb->len - sh_len;
  1579. unsigned int p_len = sh->gso_size;
  1580. gxio_mpipe_edesc_t edesc_head = { { 0 } };
  1581. gxio_mpipe_edesc_t edesc_body = { { 0 } };
  1582. long f_id = -1; /* id of the current fragment */
  1583. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1584. long f_used = 0; /* bytes used from the current fragment */
  1585. void *f_data = skb->data + sh_len;
  1586. long n; /* size of the current piece of payload */
  1587. unsigned long tx_packets = 0, tx_bytes = 0;
  1588. unsigned int csum_start;
  1589. int segment;
  1590. /* Prepare to egress the headers: set up header edesc. */
  1591. csum_start = skb_checksum_start_offset(skb);
  1592. edesc_head.csum = 1;
  1593. edesc_head.csum_start = csum_start;
  1594. edesc_head.csum_dest = csum_start + skb->csum_offset;
  1595. edesc_head.xfer_size = sh_len;
  1596. /* This is only used to specify the TLB. */
  1597. edesc_head.stack_idx = md->first_buffer_stack;
  1598. edesc_body.stack_idx = md->first_buffer_stack;
  1599. /* Egress all the edescs. */
  1600. for (segment = 0; segment < sh->gso_segs; segment++) {
  1601. unsigned char *buf;
  1602. unsigned int p_used = 0;
  1603. /* Egress the header. */
  1604. buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
  1605. NET_IP_ALIGN;
  1606. edesc_head.va = va_to_tile_io_addr(buf);
  1607. gxio_mpipe_equeue_put_at(equeue, edesc_head, slot);
  1608. slot++;
  1609. /* Egress the payload. */
  1610. while (p_used < p_len) {
  1611. void *va;
  1612. /* Advance as needed. */
  1613. while (f_used >= f_size) {
  1614. f_id++;
  1615. f_size = skb_frag_size(&sh->frags[f_id]);
  1616. f_data = tile_net_frag_buf(&sh->frags[f_id]);
  1617. f_used = 0;
  1618. }
  1619. va = f_data + f_used;
  1620. /* Use bytes from the current fragment. */
  1621. n = p_len - p_used;
  1622. if (n > f_size - f_used)
  1623. n = f_size - f_used;
  1624. f_used += n;
  1625. p_used += n;
  1626. /* Egress a piece of the payload. */
  1627. edesc_body.va = va_to_tile_io_addr(va);
  1628. edesc_body.xfer_size = n;
  1629. edesc_body.bound = !(p_used < p_len);
  1630. gxio_mpipe_equeue_put_at(equeue, edesc_body, slot);
  1631. slot++;
  1632. }
  1633. tx_packets++;
  1634. tx_bytes += sh_len + p_len;
  1635. /* The last segment may be less than gso_size. */
  1636. data_len -= p_len;
  1637. if (data_len < p_len)
  1638. p_len = data_len;
  1639. }
  1640. /* Update stats. */
  1641. tile_net_stats_add(tx_packets, &dev->stats.tx_packets);
  1642. tile_net_stats_add(tx_bytes, &dev->stats.tx_bytes);
  1643. }
  1644. /* Do "TSO" handling for egress.
  1645. *
  1646. * Normally drivers set NETIF_F_TSO only to support hardware TSO;
  1647. * otherwise the stack uses scatter-gather to implement GSO in software.
  1648. * On our testing, enabling GSO support (via NETIF_F_SG) drops network
  1649. * performance down to around 7.5 Gbps on the 10G interfaces, although
  1650. * also dropping cpu utilization way down, to under 8%. But
  1651. * implementing "TSO" in the driver brings performance back up to line
  1652. * rate, while dropping cpu usage even further, to less than 4%. In
  1653. * practice, profiling of GSO shows that skb_segment() is what causes
  1654. * the performance overheads; we benefit in the driver from using
  1655. * preallocated memory to duplicate the TCP/IP headers.
  1656. */
  1657. static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
  1658. {
  1659. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1660. struct tile_net_priv *priv = netdev_priv(dev);
  1661. int channel = priv->echannel;
  1662. int instance = priv->instance;
  1663. struct mpipe_data *md = &mpipe_data[instance];
  1664. struct tile_net_egress *egress = &md->egress_for_echannel[channel];
  1665. struct tile_net_comps *comps =
  1666. info->mpipe[instance].comps_for_echannel[channel];
  1667. gxio_mpipe_equeue_t *equeue = egress->equeue;
  1668. unsigned long irqflags;
  1669. int num_edescs;
  1670. s64 slot;
  1671. /* Determine how many mpipe edesc's are needed. */
  1672. num_edescs = tso_count_edescs(skb);
  1673. local_irq_save(irqflags);
  1674. /* Try to acquire a completion entry and an egress slot. */
  1675. slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
  1676. equeue, num_edescs);
  1677. if (slot < 0) {
  1678. local_irq_restore(irqflags);
  1679. return NETDEV_TX_BUSY;
  1680. }
  1681. /* Set up copies of header data properly. */
  1682. tso_headers_prepare(skb, egress->headers, slot);
  1683. /* Actually pass the data to the network hardware. */
  1684. tso_egress(dev, equeue, skb, egress->headers, slot);
  1685. /* Add a completion record. */
  1686. add_comp(equeue, comps, slot + num_edescs - 1, skb);
  1687. local_irq_restore(irqflags);
  1688. /* Make sure the egress timer is scheduled. */
  1689. tile_net_schedule_egress_timer();
  1690. return NETDEV_TX_OK;
  1691. }
  1692. /* Analyze the body and frags for a transmit request. */
  1693. static unsigned int tile_net_tx_frags(struct frag *frags,
  1694. struct sk_buff *skb,
  1695. void *b_data, unsigned int b_len)
  1696. {
  1697. unsigned int i, n = 0;
  1698. struct skb_shared_info *sh = skb_shinfo(skb);
  1699. if (b_len != 0) {
  1700. frags[n].buf = b_data;
  1701. frags[n++].length = b_len;
  1702. }
  1703. for (i = 0; i < sh->nr_frags; i++) {
  1704. skb_frag_t *f = &sh->frags[i];
  1705. frags[n].buf = tile_net_frag_buf(f);
  1706. frags[n++].length = skb_frag_size(f);
  1707. }
  1708. return n;
  1709. }
  1710. /* Help the kernel transmit a packet. */
  1711. static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
  1712. {
  1713. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1714. struct tile_net_priv *priv = netdev_priv(dev);
  1715. int instance = priv->instance;
  1716. struct mpipe_data *md = &mpipe_data[instance];
  1717. struct tile_net_egress *egress =
  1718. &md->egress_for_echannel[priv->echannel];
  1719. gxio_mpipe_equeue_t *equeue = egress->equeue;
  1720. struct tile_net_comps *comps =
  1721. info->mpipe[instance].comps_for_echannel[priv->echannel];
  1722. unsigned int len = skb->len;
  1723. unsigned char *data = skb->data;
  1724. unsigned int num_edescs;
  1725. struct frag frags[MAX_FRAGS];
  1726. gxio_mpipe_edesc_t edescs[MAX_FRAGS];
  1727. unsigned long irqflags;
  1728. gxio_mpipe_edesc_t edesc = { { 0 } };
  1729. unsigned int i;
  1730. s64 slot;
  1731. if (skb_is_gso(skb))
  1732. return tile_net_tx_tso(skb, dev);
  1733. num_edescs = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
  1734. /* This is only used to specify the TLB. */
  1735. edesc.stack_idx = md->first_buffer_stack;
  1736. /* Prepare the edescs. */
  1737. for (i = 0; i < num_edescs; i++) {
  1738. edesc.xfer_size = frags[i].length;
  1739. edesc.va = va_to_tile_io_addr(frags[i].buf);
  1740. edescs[i] = edesc;
  1741. }
  1742. /* Mark the final edesc. */
  1743. edescs[num_edescs - 1].bound = 1;
  1744. /* Add checksum info to the initial edesc, if needed. */
  1745. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1746. unsigned int csum_start = skb_checksum_start_offset(skb);
  1747. edescs[0].csum = 1;
  1748. edescs[0].csum_start = csum_start;
  1749. edescs[0].csum_dest = csum_start + skb->csum_offset;
  1750. }
  1751. local_irq_save(irqflags);
  1752. /* Try to acquire a completion entry and an egress slot. */
  1753. slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
  1754. equeue, num_edescs);
  1755. if (slot < 0) {
  1756. local_irq_restore(irqflags);
  1757. return NETDEV_TX_BUSY;
  1758. }
  1759. for (i = 0; i < num_edescs; i++)
  1760. gxio_mpipe_equeue_put_at(equeue, edescs[i], slot++);
  1761. /* Store TX timestamp if needed. */
  1762. tile_tx_timestamp(skb, instance);
  1763. /* Add a completion record. */
  1764. add_comp(equeue, comps, slot - 1, skb);
  1765. /* NOTE: Use ETH_ZLEN for short packets (e.g. 42 < 60). */
  1766. tile_net_stats_add(1, &dev->stats.tx_packets);
  1767. tile_net_stats_add(max_t(unsigned int, len, ETH_ZLEN),
  1768. &dev->stats.tx_bytes);
  1769. local_irq_restore(irqflags);
  1770. /* Make sure the egress timer is scheduled. */
  1771. tile_net_schedule_egress_timer();
  1772. return NETDEV_TX_OK;
  1773. }
  1774. /* Return subqueue id on this core (one per core). */
  1775. static u16 tile_net_select_queue(struct net_device *dev, struct sk_buff *skb,
  1776. void *accel_priv, select_queue_fallback_t fallback)
  1777. {
  1778. return smp_processor_id();
  1779. }
  1780. /* Deal with a transmit timeout. */
  1781. static void tile_net_tx_timeout(struct net_device *dev)
  1782. {
  1783. int cpu;
  1784. for_each_online_cpu(cpu)
  1785. netif_wake_subqueue(dev, cpu);
  1786. }
  1787. /* Ioctl commands. */
  1788. static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1789. {
  1790. if (cmd == SIOCSHWTSTAMP)
  1791. return tile_hwtstamp_set(dev, rq);
  1792. if (cmd == SIOCGHWTSTAMP)
  1793. return tile_hwtstamp_get(dev, rq);
  1794. return -EOPNOTSUPP;
  1795. }
  1796. /* Change the Ethernet address of the NIC.
  1797. *
  1798. * The hypervisor driver does not support changing MAC address. However,
  1799. * the hardware does not do anything with the MAC address, so the address
  1800. * which gets used on outgoing packets, and which is accepted on incoming
  1801. * packets, is completely up to us.
  1802. *
  1803. * Returns 0 on success, negative on failure.
  1804. */
  1805. static int tile_net_set_mac_address(struct net_device *dev, void *p)
  1806. {
  1807. struct sockaddr *addr = p;
  1808. if (!is_valid_ether_addr(addr->sa_data))
  1809. return -EINVAL;
  1810. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1811. return 0;
  1812. }
  1813. #ifdef CONFIG_NET_POLL_CONTROLLER
  1814. /* Polling 'interrupt' - used by things like netconsole to send skbs
  1815. * without having to re-enable interrupts. It's not called while
  1816. * the interrupt routine is executing.
  1817. */
  1818. static void tile_net_netpoll(struct net_device *dev)
  1819. {
  1820. int instance = mpipe_instance(dev);
  1821. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1822. struct mpipe_data *md = &mpipe_data[instance];
  1823. disable_percpu_irq(md->ingress_irq);
  1824. napi_schedule(&info->mpipe[instance].napi);
  1825. enable_percpu_irq(md->ingress_irq, 0);
  1826. }
  1827. #endif
  1828. static const struct net_device_ops tile_net_ops = {
  1829. .ndo_open = tile_net_open,
  1830. .ndo_stop = tile_net_stop,
  1831. .ndo_start_xmit = tile_net_tx,
  1832. .ndo_select_queue = tile_net_select_queue,
  1833. .ndo_do_ioctl = tile_net_ioctl,
  1834. .ndo_tx_timeout = tile_net_tx_timeout,
  1835. .ndo_set_mac_address = tile_net_set_mac_address,
  1836. #ifdef CONFIG_NET_POLL_CONTROLLER
  1837. .ndo_poll_controller = tile_net_netpoll,
  1838. #endif
  1839. };
  1840. /* The setup function.
  1841. *
  1842. * This uses ether_setup() to assign various fields in dev, including
  1843. * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
  1844. */
  1845. static void tile_net_setup(struct net_device *dev)
  1846. {
  1847. netdev_features_t features = 0;
  1848. ether_setup(dev);
  1849. dev->netdev_ops = &tile_net_ops;
  1850. dev->watchdog_timeo = TILE_NET_TIMEOUT;
  1851. /* MTU range: 68 - 1500 or 9000 */
  1852. dev->mtu = ETH_DATA_LEN;
  1853. dev->min_mtu = ETH_MIN_MTU;
  1854. dev->max_mtu = jumbo_num ? TILE_JUMBO_MAX_MTU : ETH_DATA_LEN;
  1855. features |= NETIF_F_HW_CSUM;
  1856. features |= NETIF_F_SG;
  1857. features |= NETIF_F_TSO;
  1858. features |= NETIF_F_TSO6;
  1859. dev->hw_features |= features;
  1860. dev->vlan_features |= features;
  1861. dev->features |= features;
  1862. }
  1863. /* Allocate the device structure, register the device, and obtain the
  1864. * MAC address from the hypervisor.
  1865. */
  1866. static void tile_net_dev_init(const char *name, const uint8_t *mac)
  1867. {
  1868. int ret;
  1869. struct net_device *dev;
  1870. struct tile_net_priv *priv;
  1871. /* HACK: Ignore "loop" links. */
  1872. if (strncmp(name, "loop", 4) == 0)
  1873. return;
  1874. /* Allocate the device structure. Normally, "name" is a
  1875. * template, instantiated by register_netdev(), but not for us.
  1876. */
  1877. dev = alloc_netdev_mqs(sizeof(*priv), name, NET_NAME_UNKNOWN,
  1878. tile_net_setup, NR_CPUS, 1);
  1879. if (!dev) {
  1880. pr_err("alloc_netdev_mqs(%s) failed\n", name);
  1881. return;
  1882. }
  1883. /* Initialize "priv". */
  1884. priv = netdev_priv(dev);
  1885. priv->dev = dev;
  1886. priv->channel = -1;
  1887. priv->loopify_channel = -1;
  1888. priv->echannel = -1;
  1889. init_ptp_dev(priv);
  1890. /* Get the MAC address and set it in the device struct; this must
  1891. * be done before the device is opened. If the MAC is all zeroes,
  1892. * we use a random address, since we're probably on the simulator.
  1893. */
  1894. if (!is_zero_ether_addr(mac))
  1895. ether_addr_copy(dev->dev_addr, mac);
  1896. else
  1897. eth_hw_addr_random(dev);
  1898. /* Register the network device. */
  1899. ret = register_netdev(dev);
  1900. if (ret) {
  1901. netdev_err(dev, "register_netdev failed %d\n", ret);
  1902. free_netdev(dev);
  1903. return;
  1904. }
  1905. }
  1906. /* Per-cpu module initialization. */
  1907. static void tile_net_init_module_percpu(void *unused)
  1908. {
  1909. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1910. int my_cpu = smp_processor_id();
  1911. int instance;
  1912. for (instance = 0; instance < NR_MPIPE_MAX; instance++) {
  1913. info->mpipe[instance].has_iqueue = false;
  1914. info->mpipe[instance].instance = instance;
  1915. }
  1916. info->my_cpu = my_cpu;
  1917. /* Initialize the egress timer. */
  1918. hrtimer_init(&info->egress_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1919. info->egress_timer.function = tile_net_handle_egress_timer;
  1920. }
  1921. /* Module initialization. */
  1922. static int __init tile_net_init_module(void)
  1923. {
  1924. int i;
  1925. char name[GXIO_MPIPE_LINK_NAME_LEN];
  1926. uint8_t mac[6];
  1927. pr_info("Tilera Network Driver\n");
  1928. BUILD_BUG_ON(NR_MPIPE_MAX != 2);
  1929. mutex_init(&tile_net_devs_for_channel_mutex);
  1930. /* Initialize each CPU. */
  1931. on_each_cpu(tile_net_init_module_percpu, NULL, 1);
  1932. /* Find out what devices we have, and initialize them. */
  1933. for (i = 0; gxio_mpipe_link_enumerate_mac(i, name, mac) >= 0; i++)
  1934. tile_net_dev_init(name, mac);
  1935. if (!network_cpus_init())
  1936. cpumask_and(&network_cpus_map, housekeeping_cpumask(),
  1937. cpu_online_mask);
  1938. return 0;
  1939. }
  1940. module_init(tile_net_init_module);