tilegx.c 64 KB

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