tilegx.c 58 KB

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