tilegx.c 64 KB

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