tilegx.c 64 KB

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