tilegx.c 54 KB

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