tilegx.c 64 KB

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