tilegx.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  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 <linux/tick.h>
  43. #include <asm/checksum.h>
  44. #include <asm/homecache.h>
  45. #include <gxio/mpipe.h>
  46. #include <arch/sim.h>
  47. /* Default transmit lockup timeout period, in jiffies. */
  48. #define TILE_NET_TIMEOUT (5 * HZ)
  49. /* The maximum number of distinct channels (idesc.channel is 5 bits). */
  50. #define TILE_NET_CHANNELS 32
  51. /* Maximum number of idescs to handle per "poll". */
  52. #define TILE_NET_BATCH 128
  53. /* Maximum number of packets to handle per "poll". */
  54. #define TILE_NET_WEIGHT 64
  55. /* Number of entries in each iqueue. */
  56. #define IQUEUE_ENTRIES 512
  57. /* Number of entries in each equeue. */
  58. #define EQUEUE_ENTRIES 2048
  59. /* Total header bytes per equeue slot. Must be big enough for 2 bytes
  60. * of NET_IP_ALIGN alignment, plus 14 bytes (?) of L2 header, plus up to
  61. * 60 bytes of actual TCP header. We round up to align to cache lines.
  62. */
  63. #define HEADER_BYTES 128
  64. /* Maximum completions per cpu per device (must be a power of two).
  65. * ISSUE: What is the right number here? If this is too small, then
  66. * egress might block waiting for free space in a completions array.
  67. * ISSUE: At the least, allocate these only for initialized echannels.
  68. */
  69. #define TILE_NET_MAX_COMPS 64
  70. #define MAX_FRAGS (MAX_SKB_FRAGS + 1)
  71. /* The "kinds" of buffer stacks (small/large/jumbo). */
  72. #define MAX_KINDS 3
  73. /* Size of completions data to allocate.
  74. * ISSUE: Probably more than needed since we don't use all the channels.
  75. */
  76. #define COMPS_SIZE (TILE_NET_CHANNELS * sizeof(struct tile_net_comps))
  77. /* Size of NotifRing data to allocate. */
  78. #define NOTIF_RING_SIZE (IQUEUE_ENTRIES * sizeof(gxio_mpipe_idesc_t))
  79. /* Timeout to wake the per-device TX timer after we stop the queue.
  80. * We don't want the timeout too short (adds overhead, and might end
  81. * up causing stop/wake/stop/wake cycles) or too long (affects performance).
  82. * For the 10 Gb NIC, 30 usec means roughly 30+ 1500-byte packets.
  83. */
  84. #define TX_TIMER_DELAY_USEC 30
  85. /* Timeout to wake the per-cpu egress timer to free completions. */
  86. #define EGRESS_TIMER_DELAY_USEC 1000
  87. MODULE_AUTHOR("Tilera Corporation");
  88. MODULE_LICENSE("GPL");
  89. /* A "packet fragment" (a chunk of memory). */
  90. struct frag {
  91. void *buf;
  92. size_t length;
  93. };
  94. /* A single completion. */
  95. struct tile_net_comp {
  96. /* The "complete_count" when the completion will be complete. */
  97. s64 when;
  98. /* The buffer to be freed when the completion is complete. */
  99. struct sk_buff *skb;
  100. };
  101. /* The completions for a given cpu and echannel. */
  102. struct tile_net_comps {
  103. /* The completions. */
  104. struct tile_net_comp comp_queue[TILE_NET_MAX_COMPS];
  105. /* The number of completions used. */
  106. unsigned long comp_next;
  107. /* The number of completions freed. */
  108. unsigned long comp_last;
  109. };
  110. /* The transmit wake timer for a given cpu and echannel. */
  111. struct tile_net_tx_wake {
  112. int tx_queue_idx;
  113. struct hrtimer timer;
  114. struct net_device *dev;
  115. };
  116. /* Info for a specific cpu. */
  117. struct tile_net_info {
  118. /* Our cpu. */
  119. int my_cpu;
  120. /* A timer for handling egress completions. */
  121. struct hrtimer egress_timer;
  122. /* True if "egress_timer" is scheduled. */
  123. bool egress_timer_scheduled;
  124. struct info_mpipe {
  125. /* Packet queue. */
  126. gxio_mpipe_iqueue_t iqueue;
  127. /* The NAPI struct. */
  128. struct napi_struct napi;
  129. /* Number of buffers (by kind) which must still be provided. */
  130. unsigned int num_needed_buffers[MAX_KINDS];
  131. /* instance id. */
  132. int instance;
  133. /* True if iqueue is valid. */
  134. bool has_iqueue;
  135. /* NAPI flags. */
  136. bool napi_added;
  137. bool napi_enabled;
  138. /* Comps for each egress channel. */
  139. struct tile_net_comps *comps_for_echannel[TILE_NET_CHANNELS];
  140. /* Transmit wake timer for each egress channel. */
  141. struct tile_net_tx_wake tx_wake[TILE_NET_CHANNELS];
  142. } mpipe[NR_MPIPE_MAX];
  143. };
  144. /* Info for egress on a particular egress channel. */
  145. struct tile_net_egress {
  146. /* The "equeue". */
  147. gxio_mpipe_equeue_t *equeue;
  148. /* The headers for TSO. */
  149. unsigned char *headers;
  150. };
  151. /* Info for a specific device. */
  152. struct tile_net_priv {
  153. /* Our network device. */
  154. struct net_device *dev;
  155. /* The primary link. */
  156. gxio_mpipe_link_t link;
  157. /* The primary channel, if open, else -1. */
  158. int channel;
  159. /* The "loopify" egress link, if needed. */
  160. gxio_mpipe_link_t loopify_link;
  161. /* The "loopify" egress channel, if open, else -1. */
  162. int loopify_channel;
  163. /* The egress channel (channel or loopify_channel). */
  164. int echannel;
  165. /* mPIPE instance, 0 or 1. */
  166. int instance;
  167. /* The timestamp config. */
  168. struct hwtstamp_config stamp_cfg;
  169. };
  170. static struct mpipe_data {
  171. /* The ingress irq. */
  172. int ingress_irq;
  173. /* The "context" for all devices. */
  174. gxio_mpipe_context_t context;
  175. /* Egress info, indexed by "priv->echannel"
  176. * (lazily created as needed).
  177. */
  178. struct tile_net_egress
  179. egress_for_echannel[TILE_NET_CHANNELS];
  180. /* Devices currently associated with each channel.
  181. * NOTE: The array entry can become NULL after ifconfig down, but
  182. * we do not free the underlying net_device structures, so it is
  183. * safe to use a pointer after reading it from this array.
  184. */
  185. struct net_device
  186. *tile_net_devs_for_channel[TILE_NET_CHANNELS];
  187. /* The actual memory allocated for the buffer stacks. */
  188. void *buffer_stack_vas[MAX_KINDS];
  189. /* The amount of memory allocated for each buffer stack. */
  190. size_t buffer_stack_bytes[MAX_KINDS];
  191. /* The first buffer stack index
  192. * (small = +0, large = +1, jumbo = +2).
  193. */
  194. int first_buffer_stack;
  195. /* The buckets. */
  196. int first_bucket;
  197. int num_buckets;
  198. /* PTP-specific data. */
  199. struct ptp_clock *ptp_clock;
  200. struct ptp_clock_info caps;
  201. /* Lock for ptp accessors. */
  202. struct mutex ptp_lock;
  203. } mpipe_data[NR_MPIPE_MAX] = {
  204. [0 ... (NR_MPIPE_MAX - 1)] {
  205. .ingress_irq = -1,
  206. .first_buffer_stack = -1,
  207. .first_bucket = -1,
  208. .num_buckets = 1
  209. }
  210. };
  211. /* A mutex for "tile_net_devs_for_channel". */
  212. static DEFINE_MUTEX(tile_net_devs_for_channel_mutex);
  213. /* The per-cpu info. */
  214. static DEFINE_PER_CPU(struct tile_net_info, per_cpu_info);
  215. /* The buffer size enums for each buffer stack.
  216. * See arch/tile/include/gxio/mpipe.h for the set of possible values.
  217. * We avoid the "10384" size because it can induce "false chaining"
  218. * on "cut-through" jumbo packets.
  219. */
  220. static gxio_mpipe_buffer_size_enum_t buffer_size_enums[MAX_KINDS] = {
  221. GXIO_MPIPE_BUFFER_SIZE_128,
  222. GXIO_MPIPE_BUFFER_SIZE_1664,
  223. GXIO_MPIPE_BUFFER_SIZE_16384
  224. };
  225. /* Text value of tile_net.cpus if passed as a module parameter. */
  226. static char *network_cpus_string;
  227. /* The actual cpus in "network_cpus". */
  228. static struct cpumask network_cpus_map;
  229. /* If "tile_net.loopify=LINK" was specified, this is "LINK". */
  230. static char *loopify_link_name;
  231. /* If "tile_net.custom" was specified, this is true. */
  232. static bool custom_flag;
  233. /* If "tile_net.jumbo=NUM" was specified, this is "NUM". */
  234. static uint jumbo_num;
  235. /* Obtain mpipe instance from struct tile_net_priv given struct net_device. */
  236. static inline int mpipe_instance(struct net_device *dev)
  237. {
  238. struct tile_net_priv *priv = netdev_priv(dev);
  239. return priv->instance;
  240. }
  241. /* The "tile_net.cpus" argument specifies the cpus that are dedicated
  242. * to handle ingress packets.
  243. *
  244. * The parameter should be in the form "tile_net.cpus=m-n[,x-y]", where
  245. * m, n, x, y are integer numbers that represent the cpus that can be
  246. * neither a dedicated cpu nor a dataplane cpu.
  247. */
  248. static bool network_cpus_init(void)
  249. {
  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. pr_info("Linux network CPUs: %*pbl\n",
  267. cpumask_pr_args(&network_cpus_map));
  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 = this_cpu_ptr(&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 timespec64 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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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 = this_cpu_ptr(&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,
  712. struct timespec64 *ts)
  713. {
  714. int ret = 0;
  715. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  716. mutex_lock(&md->ptp_lock);
  717. if (gxio_mpipe_get_timestamp(&md->context, ts))
  718. ret = -EBUSY;
  719. mutex_unlock(&md->ptp_lock);
  720. return ret;
  721. }
  722. static int ptp_mpipe_settime(struct ptp_clock_info *ptp,
  723. const struct timespec64 *ts)
  724. {
  725. int ret = 0;
  726. struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
  727. mutex_lock(&md->ptp_lock);
  728. if (gxio_mpipe_set_timestamp(&md->context, ts))
  729. ret = -EBUSY;
  730. mutex_unlock(&md->ptp_lock);
  731. return ret;
  732. }
  733. static int ptp_mpipe_enable(struct ptp_clock_info *ptp,
  734. struct ptp_clock_request *request, int on)
  735. {
  736. return -EOPNOTSUPP;
  737. }
  738. static struct ptp_clock_info ptp_mpipe_caps = {
  739. .owner = THIS_MODULE,
  740. .name = "mPIPE clock",
  741. .max_adj = 999999999,
  742. .n_ext_ts = 0,
  743. .n_pins = 0,
  744. .pps = 0,
  745. .adjfreq = ptp_mpipe_adjfreq,
  746. .adjtime = ptp_mpipe_adjtime,
  747. .gettime64 = ptp_mpipe_gettime,
  748. .settime64 = ptp_mpipe_settime,
  749. .enable = ptp_mpipe_enable,
  750. };
  751. /* Sync mPIPE's timestamp up with Linux system time and register PTP clock. */
  752. static void register_ptp_clock(struct net_device *dev, struct mpipe_data *md)
  753. {
  754. struct timespec64 ts;
  755. ktime_get_ts64(&ts);
  756. gxio_mpipe_set_timestamp(&md->context, &ts);
  757. mutex_init(&md->ptp_lock);
  758. md->caps = ptp_mpipe_caps;
  759. md->ptp_clock = ptp_clock_register(&md->caps, NULL);
  760. if (IS_ERR(md->ptp_clock))
  761. netdev_err(dev, "ptp_clock_register failed %ld\n",
  762. PTR_ERR(md->ptp_clock));
  763. }
  764. /* Initialize PTP fields in a new device. */
  765. static void init_ptp_dev(struct tile_net_priv *priv)
  766. {
  767. priv->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
  768. priv->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
  769. }
  770. /* Helper functions for "tile_net_update()". */
  771. static void enable_ingress_irq(void *irq)
  772. {
  773. enable_percpu_irq((long)irq, 0);
  774. }
  775. static void disable_ingress_irq(void *irq)
  776. {
  777. disable_percpu_irq((long)irq);
  778. }
  779. /* Helper function for tile_net_open() and tile_net_stop().
  780. * Always called under tile_net_devs_for_channel_mutex.
  781. */
  782. static int tile_net_update(struct net_device *dev)
  783. {
  784. static gxio_mpipe_rules_t rules; /* too big to fit on the stack */
  785. bool saw_channel = false;
  786. int instance = mpipe_instance(dev);
  787. struct mpipe_data *md = &mpipe_data[instance];
  788. int channel;
  789. int rc;
  790. int cpu;
  791. saw_channel = false;
  792. gxio_mpipe_rules_init(&rules, &md->context);
  793. for (channel = 0; channel < TILE_NET_CHANNELS; channel++) {
  794. if (md->tile_net_devs_for_channel[channel] == NULL)
  795. continue;
  796. if (!saw_channel) {
  797. saw_channel = true;
  798. gxio_mpipe_rules_begin(&rules, md->first_bucket,
  799. md->num_buckets, NULL);
  800. gxio_mpipe_rules_set_headroom(&rules, NET_IP_ALIGN);
  801. }
  802. gxio_mpipe_rules_add_channel(&rules, channel);
  803. }
  804. /* NOTE: This can fail if there is no classifier.
  805. * ISSUE: Can anything else cause it to fail?
  806. */
  807. rc = gxio_mpipe_rules_commit(&rules);
  808. if (rc != 0) {
  809. netdev_warn(dev, "gxio_mpipe_rules_commit: mpipe[%d] %d\n",
  810. instance, rc);
  811. return -EIO;
  812. }
  813. /* Update all cpus, sequentially (to protect "netif_napi_add()").
  814. * We use on_each_cpu to handle the IPI mask or unmask.
  815. */
  816. if (!saw_channel)
  817. on_each_cpu(disable_ingress_irq,
  818. (void *)(long)(md->ingress_irq), 1);
  819. for_each_online_cpu(cpu) {
  820. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  821. if (!info->mpipe[instance].has_iqueue)
  822. continue;
  823. if (saw_channel) {
  824. if (!info->mpipe[instance].napi_added) {
  825. netif_napi_add(dev, &info->mpipe[instance].napi,
  826. tile_net_poll, TILE_NET_WEIGHT);
  827. info->mpipe[instance].napi_added = true;
  828. }
  829. if (!info->mpipe[instance].napi_enabled) {
  830. napi_enable(&info->mpipe[instance].napi);
  831. info->mpipe[instance].napi_enabled = true;
  832. }
  833. } else {
  834. if (info->mpipe[instance].napi_enabled) {
  835. napi_disable(&info->mpipe[instance].napi);
  836. info->mpipe[instance].napi_enabled = false;
  837. }
  838. /* FIXME: Drain the iqueue. */
  839. }
  840. }
  841. if (saw_channel)
  842. on_each_cpu(enable_ingress_irq,
  843. (void *)(long)(md->ingress_irq), 1);
  844. /* HACK: Allow packets to flow in the simulator. */
  845. if (saw_channel)
  846. sim_enable_mpipe_links(instance, -1);
  847. return 0;
  848. }
  849. /* Initialize a buffer stack. */
  850. static int create_buffer_stack(struct net_device *dev,
  851. int kind, size_t num_buffers)
  852. {
  853. pte_t hash_pte = pte_set_home((pte_t) { 0 }, PAGE_HOME_HASH);
  854. int instance = mpipe_instance(dev);
  855. struct mpipe_data *md = &mpipe_data[instance];
  856. size_t needed = gxio_mpipe_calc_buffer_stack_bytes(num_buffers);
  857. int stack_idx = md->first_buffer_stack + kind;
  858. void *va;
  859. int i, rc;
  860. /* Round up to 64KB and then use alloc_pages() so we get the
  861. * required 64KB alignment.
  862. */
  863. md->buffer_stack_bytes[kind] =
  864. ALIGN(needed, 64 * 1024);
  865. va = alloc_pages_exact(md->buffer_stack_bytes[kind], GFP_KERNEL);
  866. if (va == NULL) {
  867. netdev_err(dev,
  868. "Could not alloc %zd bytes for buffer stack %d\n",
  869. md->buffer_stack_bytes[kind], kind);
  870. return -ENOMEM;
  871. }
  872. /* Initialize the buffer stack. */
  873. rc = gxio_mpipe_init_buffer_stack(&md->context, stack_idx,
  874. buffer_size_enums[kind], va,
  875. md->buffer_stack_bytes[kind], 0);
  876. if (rc != 0) {
  877. netdev_err(dev, "gxio_mpipe_init_buffer_stack: mpipe[%d] %d\n",
  878. instance, rc);
  879. free_pages_exact(va, md->buffer_stack_bytes[kind]);
  880. return rc;
  881. }
  882. md->buffer_stack_vas[kind] = va;
  883. rc = gxio_mpipe_register_client_memory(&md->context, stack_idx,
  884. hash_pte, 0);
  885. if (rc != 0) {
  886. netdev_err(dev,
  887. "gxio_mpipe_register_client_memory: mpipe[%d] %d\n",
  888. instance, rc);
  889. return rc;
  890. }
  891. /* Provide initial buffers. */
  892. for (i = 0; i < num_buffers; i++) {
  893. if (!tile_net_provide_buffer(instance, kind)) {
  894. netdev_err(dev, "Cannot allocate initial sk_bufs!\n");
  895. return -ENOMEM;
  896. }
  897. }
  898. return 0;
  899. }
  900. /* Allocate and initialize mpipe buffer stacks, and register them in
  901. * the mPIPE TLBs, for small, large, and (possibly) jumbo packet sizes.
  902. * This routine supports tile_net_init_mpipe(), below.
  903. */
  904. static int init_buffer_stacks(struct net_device *dev,
  905. int network_cpus_count)
  906. {
  907. int num_kinds = MAX_KINDS - (jumbo_num == 0);
  908. size_t num_buffers;
  909. int rc;
  910. int instance = mpipe_instance(dev);
  911. struct mpipe_data *md = &mpipe_data[instance];
  912. /* Allocate the buffer stacks. */
  913. rc = gxio_mpipe_alloc_buffer_stacks(&md->context, num_kinds, 0, 0);
  914. if (rc < 0) {
  915. netdev_err(dev,
  916. "gxio_mpipe_alloc_buffer_stacks: mpipe[%d] %d\n",
  917. instance, rc);
  918. return rc;
  919. }
  920. md->first_buffer_stack = rc;
  921. /* Enough small/large buffers to (normally) avoid buffer errors. */
  922. num_buffers =
  923. network_cpus_count * (IQUEUE_ENTRIES + TILE_NET_BATCH);
  924. /* Allocate the small memory stack. */
  925. if (rc >= 0)
  926. rc = create_buffer_stack(dev, 0, num_buffers);
  927. /* Allocate the large buffer stack. */
  928. if (rc >= 0)
  929. rc = create_buffer_stack(dev, 1, num_buffers);
  930. /* Allocate the jumbo buffer stack if needed. */
  931. if (rc >= 0 && jumbo_num != 0)
  932. rc = create_buffer_stack(dev, 2, jumbo_num);
  933. return rc;
  934. }
  935. /* Allocate per-cpu resources (memory for completions and idescs).
  936. * This routine supports tile_net_init_mpipe(), below.
  937. */
  938. static int alloc_percpu_mpipe_resources(struct net_device *dev,
  939. int cpu, int ring)
  940. {
  941. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  942. int order, i, rc;
  943. int instance = mpipe_instance(dev);
  944. struct mpipe_data *md = &mpipe_data[instance];
  945. struct page *page;
  946. void *addr;
  947. /* Allocate the "comps". */
  948. order = get_order(COMPS_SIZE);
  949. page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
  950. if (page == NULL) {
  951. netdev_err(dev, "Failed to alloc %zd bytes comps memory\n",
  952. COMPS_SIZE);
  953. return -ENOMEM;
  954. }
  955. addr = pfn_to_kaddr(page_to_pfn(page));
  956. memset(addr, 0, COMPS_SIZE);
  957. for (i = 0; i < TILE_NET_CHANNELS; i++)
  958. info->mpipe[instance].comps_for_echannel[i] =
  959. addr + i * sizeof(struct tile_net_comps);
  960. /* If this is a network cpu, create an iqueue. */
  961. if (cpumask_test_cpu(cpu, &network_cpus_map)) {
  962. order = get_order(NOTIF_RING_SIZE);
  963. page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
  964. if (page == NULL) {
  965. netdev_err(dev,
  966. "Failed to alloc %zd bytes iqueue memory\n",
  967. NOTIF_RING_SIZE);
  968. return -ENOMEM;
  969. }
  970. addr = pfn_to_kaddr(page_to_pfn(page));
  971. rc = gxio_mpipe_iqueue_init(&info->mpipe[instance].iqueue,
  972. &md->context, ring++, addr,
  973. NOTIF_RING_SIZE, 0);
  974. if (rc < 0) {
  975. netdev_err(dev,
  976. "gxio_mpipe_iqueue_init failed: %d\n", rc);
  977. return rc;
  978. }
  979. info->mpipe[instance].has_iqueue = true;
  980. }
  981. return ring;
  982. }
  983. /* Initialize NotifGroup and buckets.
  984. * This routine supports tile_net_init_mpipe(), below.
  985. */
  986. static int init_notif_group_and_buckets(struct net_device *dev,
  987. int ring, int network_cpus_count)
  988. {
  989. int group, rc;
  990. int instance = mpipe_instance(dev);
  991. struct mpipe_data *md = &mpipe_data[instance];
  992. /* Allocate one NotifGroup. */
  993. rc = gxio_mpipe_alloc_notif_groups(&md->context, 1, 0, 0);
  994. if (rc < 0) {
  995. netdev_err(dev, "gxio_mpipe_alloc_notif_groups: mpipe[%d] %d\n",
  996. instance, rc);
  997. return rc;
  998. }
  999. group = rc;
  1000. /* Initialize global num_buckets value. */
  1001. if (network_cpus_count > 4)
  1002. md->num_buckets = 256;
  1003. else if (network_cpus_count > 1)
  1004. md->num_buckets = 16;
  1005. /* Allocate some buckets, and set global first_bucket value. */
  1006. rc = gxio_mpipe_alloc_buckets(&md->context, md->num_buckets, 0, 0);
  1007. if (rc < 0) {
  1008. netdev_err(dev, "gxio_mpipe_alloc_buckets: mpipe[%d] %d\n",
  1009. instance, rc);
  1010. return rc;
  1011. }
  1012. md->first_bucket = rc;
  1013. /* Init group and buckets. */
  1014. rc = gxio_mpipe_init_notif_group_and_buckets(
  1015. &md->context, group, ring, network_cpus_count,
  1016. md->first_bucket, md->num_buckets,
  1017. GXIO_MPIPE_BUCKET_STICKY_FLOW_LOCALITY);
  1018. if (rc != 0) {
  1019. netdev_err(dev, "gxio_mpipe_init_notif_group_and_buckets: "
  1020. "mpipe[%d] %d\n", instance, rc);
  1021. return rc;
  1022. }
  1023. return 0;
  1024. }
  1025. /* Create an irq and register it, then activate the irq and request
  1026. * interrupts on all cores. Note that "ingress_irq" being initialized
  1027. * is how we know not to call tile_net_init_mpipe() again.
  1028. * This routine supports tile_net_init_mpipe(), below.
  1029. */
  1030. static int tile_net_setup_interrupts(struct net_device *dev)
  1031. {
  1032. int cpu, rc, irq;
  1033. int instance = mpipe_instance(dev);
  1034. struct mpipe_data *md = &mpipe_data[instance];
  1035. irq = md->ingress_irq;
  1036. if (irq < 0) {
  1037. irq = irq_alloc_hwirq(-1);
  1038. if (!irq) {
  1039. netdev_err(dev,
  1040. "create_irq failed: mpipe[%d] %d\n",
  1041. instance, irq);
  1042. return irq;
  1043. }
  1044. tile_irq_activate(irq, TILE_IRQ_PERCPU);
  1045. rc = request_irq(irq, tile_net_handle_ingress_irq,
  1046. 0, "tile_net", (void *)((uint64_t)instance));
  1047. if (rc != 0) {
  1048. netdev_err(dev, "request_irq failed: mpipe[%d] %d\n",
  1049. instance, rc);
  1050. irq_free_hwirq(irq);
  1051. return rc;
  1052. }
  1053. md->ingress_irq = irq;
  1054. }
  1055. for_each_online_cpu(cpu) {
  1056. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1057. if (info->mpipe[instance].has_iqueue) {
  1058. gxio_mpipe_request_notif_ring_interrupt(&md->context,
  1059. cpu_x(cpu), cpu_y(cpu), KERNEL_PL, irq,
  1060. info->mpipe[instance].iqueue.ring);
  1061. }
  1062. }
  1063. return 0;
  1064. }
  1065. /* Undo any state set up partially by a failed call to tile_net_init_mpipe. */
  1066. static void tile_net_init_mpipe_fail(int instance)
  1067. {
  1068. int kind, cpu;
  1069. struct mpipe_data *md = &mpipe_data[instance];
  1070. /* Do cleanups that require the mpipe context first. */
  1071. for (kind = 0; kind < MAX_KINDS; kind++) {
  1072. if (md->buffer_stack_vas[kind] != NULL) {
  1073. tile_net_pop_all_buffers(instance,
  1074. md->first_buffer_stack +
  1075. kind);
  1076. }
  1077. }
  1078. /* Destroy mpipe context so the hardware no longer owns any memory. */
  1079. gxio_mpipe_destroy(&md->context);
  1080. for_each_online_cpu(cpu) {
  1081. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1082. free_pages(
  1083. (unsigned long)(
  1084. info->mpipe[instance].comps_for_echannel[0]),
  1085. get_order(COMPS_SIZE));
  1086. info->mpipe[instance].comps_for_echannel[0] = NULL;
  1087. free_pages((unsigned long)(info->mpipe[instance].iqueue.idescs),
  1088. get_order(NOTIF_RING_SIZE));
  1089. info->mpipe[instance].iqueue.idescs = NULL;
  1090. }
  1091. for (kind = 0; kind < MAX_KINDS; kind++) {
  1092. if (md->buffer_stack_vas[kind] != NULL) {
  1093. free_pages_exact(md->buffer_stack_vas[kind],
  1094. md->buffer_stack_bytes[kind]);
  1095. md->buffer_stack_vas[kind] = NULL;
  1096. }
  1097. }
  1098. md->first_buffer_stack = -1;
  1099. md->first_bucket = -1;
  1100. }
  1101. /* The first time any tilegx network device is opened, we initialize
  1102. * the global mpipe state. If this step fails, we fail to open the
  1103. * device, but if it succeeds, we never need to do it again, and since
  1104. * tile_net can't be unloaded, we never undo it.
  1105. *
  1106. * Note that some resources in this path (buffer stack indices,
  1107. * bindings from init_buffer_stack, etc.) are hypervisor resources
  1108. * that are freed implicitly by gxio_mpipe_destroy().
  1109. */
  1110. static int tile_net_init_mpipe(struct net_device *dev)
  1111. {
  1112. int rc;
  1113. int cpu;
  1114. int first_ring, ring;
  1115. int instance = mpipe_instance(dev);
  1116. struct mpipe_data *md = &mpipe_data[instance];
  1117. int network_cpus_count = cpumask_weight(&network_cpus_map);
  1118. if (!hash_default) {
  1119. netdev_err(dev, "Networking requires hash_default!\n");
  1120. return -EIO;
  1121. }
  1122. rc = gxio_mpipe_init(&md->context, instance);
  1123. if (rc != 0) {
  1124. netdev_err(dev, "gxio_mpipe_init: mpipe[%d] %d\n",
  1125. instance, rc);
  1126. return -EIO;
  1127. }
  1128. /* Set up the buffer stacks. */
  1129. rc = init_buffer_stacks(dev, network_cpus_count);
  1130. if (rc != 0)
  1131. goto fail;
  1132. /* Allocate one NotifRing for each network cpu. */
  1133. rc = gxio_mpipe_alloc_notif_rings(&md->context,
  1134. network_cpus_count, 0, 0);
  1135. if (rc < 0) {
  1136. netdev_err(dev, "gxio_mpipe_alloc_notif_rings failed %d\n",
  1137. rc);
  1138. goto fail;
  1139. }
  1140. /* Init NotifRings per-cpu. */
  1141. first_ring = rc;
  1142. ring = first_ring;
  1143. for_each_online_cpu(cpu) {
  1144. rc = alloc_percpu_mpipe_resources(dev, cpu, ring);
  1145. if (rc < 0)
  1146. goto fail;
  1147. ring = rc;
  1148. }
  1149. /* Initialize NotifGroup and buckets. */
  1150. rc = init_notif_group_and_buckets(dev, first_ring, network_cpus_count);
  1151. if (rc != 0)
  1152. goto fail;
  1153. /* Create and enable interrupts. */
  1154. rc = tile_net_setup_interrupts(dev);
  1155. if (rc != 0)
  1156. goto fail;
  1157. /* Register PTP clock and set mPIPE timestamp, if configured. */
  1158. register_ptp_clock(dev, md);
  1159. return 0;
  1160. fail:
  1161. tile_net_init_mpipe_fail(instance);
  1162. return rc;
  1163. }
  1164. /* Create persistent egress info for a given egress channel.
  1165. * Note that this may be shared between, say, "gbe0" and "xgbe0".
  1166. * ISSUE: Defer header allocation until TSO is actually needed?
  1167. */
  1168. static int tile_net_init_egress(struct net_device *dev, int echannel)
  1169. {
  1170. static int ering = -1;
  1171. struct page *headers_page, *edescs_page, *equeue_page;
  1172. gxio_mpipe_edesc_t *edescs;
  1173. gxio_mpipe_equeue_t *equeue;
  1174. unsigned char *headers;
  1175. int headers_order, edescs_order, equeue_order;
  1176. size_t edescs_size;
  1177. int rc = -ENOMEM;
  1178. int instance = mpipe_instance(dev);
  1179. struct mpipe_data *md = &mpipe_data[instance];
  1180. /* Only initialize once. */
  1181. if (md->egress_for_echannel[echannel].equeue != NULL)
  1182. return 0;
  1183. /* Allocate memory for the "headers". */
  1184. headers_order = get_order(EQUEUE_ENTRIES * HEADER_BYTES);
  1185. headers_page = alloc_pages(GFP_KERNEL, headers_order);
  1186. if (headers_page == NULL) {
  1187. netdev_warn(dev,
  1188. "Could not alloc %zd bytes for TSO headers.\n",
  1189. PAGE_SIZE << headers_order);
  1190. goto fail;
  1191. }
  1192. headers = pfn_to_kaddr(page_to_pfn(headers_page));
  1193. /* Allocate memory for the "edescs". */
  1194. edescs_size = EQUEUE_ENTRIES * sizeof(*edescs);
  1195. edescs_order = get_order(edescs_size);
  1196. edescs_page = alloc_pages(GFP_KERNEL, edescs_order);
  1197. if (edescs_page == NULL) {
  1198. netdev_warn(dev,
  1199. "Could not alloc %zd bytes for eDMA ring.\n",
  1200. edescs_size);
  1201. goto fail_headers;
  1202. }
  1203. edescs = pfn_to_kaddr(page_to_pfn(edescs_page));
  1204. /* Allocate memory for the "equeue". */
  1205. equeue_order = get_order(sizeof(*equeue));
  1206. equeue_page = alloc_pages(GFP_KERNEL, equeue_order);
  1207. if (equeue_page == NULL) {
  1208. netdev_warn(dev,
  1209. "Could not alloc %zd bytes for equeue info.\n",
  1210. PAGE_SIZE << equeue_order);
  1211. goto fail_edescs;
  1212. }
  1213. equeue = pfn_to_kaddr(page_to_pfn(equeue_page));
  1214. /* Allocate an edma ring (using a one entry "free list"). */
  1215. if (ering < 0) {
  1216. rc = gxio_mpipe_alloc_edma_rings(&md->context, 1, 0, 0);
  1217. if (rc < 0) {
  1218. netdev_warn(dev, "gxio_mpipe_alloc_edma_rings: "
  1219. "mpipe[%d] %d\n", instance, rc);
  1220. goto fail_equeue;
  1221. }
  1222. ering = rc;
  1223. }
  1224. /* Initialize the equeue. */
  1225. rc = gxio_mpipe_equeue_init(equeue, &md->context, ering, echannel,
  1226. edescs, edescs_size, 0);
  1227. if (rc != 0) {
  1228. netdev_err(dev, "gxio_mpipe_equeue_init: mpipe[%d] %d\n",
  1229. instance, rc);
  1230. goto fail_equeue;
  1231. }
  1232. /* Don't reuse the ering later. */
  1233. ering = -1;
  1234. if (jumbo_num != 0) {
  1235. /* Make sure "jumbo" packets can be egressed safely. */
  1236. if (gxio_mpipe_equeue_set_snf_size(equeue, 10368) < 0) {
  1237. /* ISSUE: There is no "gxio_mpipe_equeue_destroy()". */
  1238. netdev_warn(dev, "Jumbo packets may not be egressed"
  1239. " properly on channel %d\n", echannel);
  1240. }
  1241. }
  1242. /* Done. */
  1243. md->egress_for_echannel[echannel].equeue = equeue;
  1244. md->egress_for_echannel[echannel].headers = headers;
  1245. return 0;
  1246. fail_equeue:
  1247. __free_pages(equeue_page, equeue_order);
  1248. fail_edescs:
  1249. __free_pages(edescs_page, edescs_order);
  1250. fail_headers:
  1251. __free_pages(headers_page, headers_order);
  1252. fail:
  1253. return rc;
  1254. }
  1255. /* Return channel number for a newly-opened link. */
  1256. static int tile_net_link_open(struct net_device *dev, gxio_mpipe_link_t *link,
  1257. const char *link_name)
  1258. {
  1259. int instance = mpipe_instance(dev);
  1260. struct mpipe_data *md = &mpipe_data[instance];
  1261. int rc = gxio_mpipe_link_open(link, &md->context, link_name, 0);
  1262. if (rc < 0) {
  1263. netdev_err(dev, "Failed to open '%s', mpipe[%d], %d\n",
  1264. link_name, instance, rc);
  1265. return rc;
  1266. }
  1267. if (jumbo_num != 0) {
  1268. u32 attr = GXIO_MPIPE_LINK_RECEIVE_JUMBO;
  1269. rc = gxio_mpipe_link_set_attr(link, attr, 1);
  1270. if (rc != 0) {
  1271. netdev_err(dev,
  1272. "Cannot receive jumbo packets on '%s'\n",
  1273. link_name);
  1274. gxio_mpipe_link_close(link);
  1275. return rc;
  1276. }
  1277. }
  1278. rc = gxio_mpipe_link_channel(link);
  1279. if (rc < 0 || rc >= TILE_NET_CHANNELS) {
  1280. netdev_err(dev, "gxio_mpipe_link_channel bad value: %d\n", rc);
  1281. gxio_mpipe_link_close(link);
  1282. return -EINVAL;
  1283. }
  1284. return rc;
  1285. }
  1286. /* Help the kernel activate the given network interface. */
  1287. static int tile_net_open(struct net_device *dev)
  1288. {
  1289. struct tile_net_priv *priv = netdev_priv(dev);
  1290. int cpu, rc, instance;
  1291. mutex_lock(&tile_net_devs_for_channel_mutex);
  1292. /* Get the instance info. */
  1293. rc = gxio_mpipe_link_instance(dev->name);
  1294. if (rc < 0 || rc >= NR_MPIPE_MAX) {
  1295. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1296. return -EIO;
  1297. }
  1298. priv->instance = rc;
  1299. instance = rc;
  1300. if (!mpipe_data[rc].context.mmio_fast_base) {
  1301. /* Do one-time initialization per instance the first time
  1302. * any device is opened.
  1303. */
  1304. rc = tile_net_init_mpipe(dev);
  1305. if (rc != 0)
  1306. goto fail;
  1307. }
  1308. /* Determine if this is the "loopify" device. */
  1309. if (unlikely((loopify_link_name != NULL) &&
  1310. !strcmp(dev->name, loopify_link_name))) {
  1311. rc = tile_net_link_open(dev, &priv->link, "loop0");
  1312. if (rc < 0)
  1313. goto fail;
  1314. priv->channel = rc;
  1315. rc = tile_net_link_open(dev, &priv->loopify_link, "loop1");
  1316. if (rc < 0)
  1317. goto fail;
  1318. priv->loopify_channel = rc;
  1319. priv->echannel = rc;
  1320. } else {
  1321. rc = tile_net_link_open(dev, &priv->link, dev->name);
  1322. if (rc < 0)
  1323. goto fail;
  1324. priv->channel = rc;
  1325. priv->echannel = rc;
  1326. }
  1327. /* Initialize egress info (if needed). Once ever, per echannel. */
  1328. rc = tile_net_init_egress(dev, priv->echannel);
  1329. if (rc != 0)
  1330. goto fail;
  1331. mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = dev;
  1332. rc = tile_net_update(dev);
  1333. if (rc != 0)
  1334. goto fail;
  1335. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1336. /* Initialize the transmit wake timer for this device for each cpu. */
  1337. for_each_online_cpu(cpu) {
  1338. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1339. struct tile_net_tx_wake *tx_wake =
  1340. &info->mpipe[instance].tx_wake[priv->echannel];
  1341. hrtimer_init(&tx_wake->timer, CLOCK_MONOTONIC,
  1342. HRTIMER_MODE_REL);
  1343. tx_wake->tx_queue_idx = cpu;
  1344. tx_wake->timer.function = tile_net_handle_tx_wake_timer;
  1345. tx_wake->dev = dev;
  1346. }
  1347. for_each_online_cpu(cpu)
  1348. netif_start_subqueue(dev, cpu);
  1349. netif_carrier_on(dev);
  1350. return 0;
  1351. fail:
  1352. if (priv->loopify_channel >= 0) {
  1353. if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
  1354. netdev_warn(dev, "Failed to close loopify link!\n");
  1355. priv->loopify_channel = -1;
  1356. }
  1357. if (priv->channel >= 0) {
  1358. if (gxio_mpipe_link_close(&priv->link) != 0)
  1359. netdev_warn(dev, "Failed to close link!\n");
  1360. priv->channel = -1;
  1361. }
  1362. priv->echannel = -1;
  1363. mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = NULL;
  1364. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1365. /* Don't return raw gxio error codes to generic Linux. */
  1366. return (rc > -512) ? rc : -EIO;
  1367. }
  1368. /* Help the kernel deactivate the given network interface. */
  1369. static int tile_net_stop(struct net_device *dev)
  1370. {
  1371. struct tile_net_priv *priv = netdev_priv(dev);
  1372. int cpu;
  1373. int instance = priv->instance;
  1374. struct mpipe_data *md = &mpipe_data[instance];
  1375. for_each_online_cpu(cpu) {
  1376. struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
  1377. struct tile_net_tx_wake *tx_wake =
  1378. &info->mpipe[instance].tx_wake[priv->echannel];
  1379. hrtimer_cancel(&tx_wake->timer);
  1380. netif_stop_subqueue(dev, cpu);
  1381. }
  1382. mutex_lock(&tile_net_devs_for_channel_mutex);
  1383. md->tile_net_devs_for_channel[priv->channel] = NULL;
  1384. (void)tile_net_update(dev);
  1385. if (priv->loopify_channel >= 0) {
  1386. if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
  1387. netdev_warn(dev, "Failed to close loopify link!\n");
  1388. priv->loopify_channel = -1;
  1389. }
  1390. if (priv->channel >= 0) {
  1391. if (gxio_mpipe_link_close(&priv->link) != 0)
  1392. netdev_warn(dev, "Failed to close link!\n");
  1393. priv->channel = -1;
  1394. }
  1395. priv->echannel = -1;
  1396. mutex_unlock(&tile_net_devs_for_channel_mutex);
  1397. return 0;
  1398. }
  1399. /* Determine the VA for a fragment. */
  1400. static inline void *tile_net_frag_buf(skb_frag_t *f)
  1401. {
  1402. unsigned long pfn = page_to_pfn(skb_frag_page(f));
  1403. return pfn_to_kaddr(pfn) + f->page_offset;
  1404. }
  1405. /* Acquire a completion entry and an egress slot, or if we can't,
  1406. * stop the queue and schedule the tx_wake timer.
  1407. */
  1408. static s64 tile_net_equeue_try_reserve(struct net_device *dev,
  1409. int tx_queue_idx,
  1410. struct tile_net_comps *comps,
  1411. gxio_mpipe_equeue_t *equeue,
  1412. int num_edescs)
  1413. {
  1414. /* Try to acquire a completion entry. */
  1415. if (comps->comp_next - comps->comp_last < TILE_NET_MAX_COMPS - 1 ||
  1416. tile_net_free_comps(equeue, comps, 32, false) != 0) {
  1417. /* Try to acquire an egress slot. */
  1418. s64 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
  1419. if (slot >= 0)
  1420. return slot;
  1421. /* Freeing some completions gives the equeue time to drain. */
  1422. tile_net_free_comps(equeue, comps, TILE_NET_MAX_COMPS, false);
  1423. slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
  1424. if (slot >= 0)
  1425. return slot;
  1426. }
  1427. /* Still nothing; give up and stop the queue for a short while. */
  1428. netif_stop_subqueue(dev, tx_queue_idx);
  1429. tile_net_schedule_tx_wake_timer(dev, tx_queue_idx);
  1430. return -1;
  1431. }
  1432. /* Determine how many edesc's are needed for TSO.
  1433. *
  1434. * Sometimes, if "sendfile()" requires copying, we will be called with
  1435. * "data" containing the header and payload, with "frags" being empty.
  1436. * Sometimes, for example when using NFS over TCP, a single segment can
  1437. * span 3 fragments. This requires special care.
  1438. */
  1439. static int tso_count_edescs(struct sk_buff *skb)
  1440. {
  1441. struct skb_shared_info *sh = skb_shinfo(skb);
  1442. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1443. unsigned int data_len = skb->len - sh_len;
  1444. unsigned int p_len = sh->gso_size;
  1445. long f_id = -1; /* id of the current fragment */
  1446. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1447. long f_used = 0; /* bytes used from the current fragment */
  1448. long n; /* size of the current piece of payload */
  1449. int num_edescs = 0;
  1450. int segment;
  1451. for (segment = 0; segment < sh->gso_segs; segment++) {
  1452. unsigned int p_used = 0;
  1453. /* One edesc for header and for each piece of the payload. */
  1454. for (num_edescs++; p_used < p_len; num_edescs++) {
  1455. /* Advance as needed. */
  1456. while (f_used >= f_size) {
  1457. f_id++;
  1458. f_size = skb_frag_size(&sh->frags[f_id]);
  1459. f_used = 0;
  1460. }
  1461. /* Use bytes from the current fragment. */
  1462. n = p_len - p_used;
  1463. if (n > f_size - f_used)
  1464. n = f_size - f_used;
  1465. f_used += n;
  1466. p_used += n;
  1467. }
  1468. /* The last segment may be less than gso_size. */
  1469. data_len -= p_len;
  1470. if (data_len < p_len)
  1471. p_len = data_len;
  1472. }
  1473. return num_edescs;
  1474. }
  1475. /* Prepare modified copies of the skbuff headers. */
  1476. static void tso_headers_prepare(struct sk_buff *skb, unsigned char *headers,
  1477. s64 slot)
  1478. {
  1479. struct skb_shared_info *sh = skb_shinfo(skb);
  1480. struct iphdr *ih;
  1481. struct ipv6hdr *ih6;
  1482. struct tcphdr *th;
  1483. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1484. unsigned int data_len = skb->len - sh_len;
  1485. unsigned char *data = skb->data;
  1486. unsigned int ih_off, th_off, p_len;
  1487. unsigned int isum_seed, tsum_seed, seq;
  1488. unsigned int uninitialized_var(id);
  1489. int is_ipv6;
  1490. long f_id = -1; /* id of the current fragment */
  1491. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1492. long f_used = 0; /* bytes used from the current fragment */
  1493. long n; /* size of the current piece of payload */
  1494. int segment;
  1495. /* Locate original headers and compute various lengths. */
  1496. is_ipv6 = skb_is_gso_v6(skb);
  1497. if (is_ipv6) {
  1498. ih6 = ipv6_hdr(skb);
  1499. ih_off = skb_network_offset(skb);
  1500. } else {
  1501. ih = ip_hdr(skb);
  1502. ih_off = skb_network_offset(skb);
  1503. isum_seed = ((0xFFFF - ih->check) +
  1504. (0xFFFF - ih->tot_len) +
  1505. (0xFFFF - ih->id));
  1506. id = ntohs(ih->id);
  1507. }
  1508. th = tcp_hdr(skb);
  1509. th_off = skb_transport_offset(skb);
  1510. p_len = sh->gso_size;
  1511. tsum_seed = th->check + (0xFFFF ^ htons(skb->len));
  1512. seq = ntohl(th->seq);
  1513. /* Prepare all the headers. */
  1514. for (segment = 0; segment < sh->gso_segs; segment++) {
  1515. unsigned char *buf;
  1516. unsigned int p_used = 0;
  1517. /* Copy to the header memory for this segment. */
  1518. buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
  1519. NET_IP_ALIGN;
  1520. memcpy(buf, data, sh_len);
  1521. /* Update copied ip header. */
  1522. if (is_ipv6) {
  1523. ih6 = (struct ipv6hdr *)(buf + ih_off);
  1524. ih6->payload_len = htons(sh_len + p_len - ih_off -
  1525. sizeof(*ih6));
  1526. } else {
  1527. ih = (struct iphdr *)(buf + ih_off);
  1528. ih->tot_len = htons(sh_len + p_len - ih_off);
  1529. ih->id = htons(id++);
  1530. ih->check = csum_long(isum_seed + ih->tot_len +
  1531. ih->id) ^ 0xffff;
  1532. }
  1533. /* Update copied tcp header. */
  1534. th = (struct tcphdr *)(buf + th_off);
  1535. th->seq = htonl(seq);
  1536. th->check = csum_long(tsum_seed + htons(sh_len + p_len));
  1537. if (segment != sh->gso_segs - 1) {
  1538. th->fin = 0;
  1539. th->psh = 0;
  1540. }
  1541. /* Skip past the header. */
  1542. slot++;
  1543. /* Skip past the payload. */
  1544. while (p_used < p_len) {
  1545. /* Advance as needed. */
  1546. while (f_used >= f_size) {
  1547. f_id++;
  1548. f_size = skb_frag_size(&sh->frags[f_id]);
  1549. f_used = 0;
  1550. }
  1551. /* Use bytes from the current fragment. */
  1552. n = p_len - p_used;
  1553. if (n > f_size - f_used)
  1554. n = f_size - f_used;
  1555. f_used += n;
  1556. p_used += n;
  1557. slot++;
  1558. }
  1559. seq += p_len;
  1560. /* The last segment may be less than gso_size. */
  1561. data_len -= p_len;
  1562. if (data_len < p_len)
  1563. p_len = data_len;
  1564. }
  1565. /* Flush the headers so they are ready for hardware DMA. */
  1566. wmb();
  1567. }
  1568. /* Pass all the data to mpipe for egress. */
  1569. static void tso_egress(struct net_device *dev, gxio_mpipe_equeue_t *equeue,
  1570. struct sk_buff *skb, unsigned char *headers, s64 slot)
  1571. {
  1572. struct skb_shared_info *sh = skb_shinfo(skb);
  1573. int instance = mpipe_instance(dev);
  1574. struct mpipe_data *md = &mpipe_data[instance];
  1575. unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1576. unsigned int data_len = skb->len - sh_len;
  1577. unsigned int p_len = sh->gso_size;
  1578. gxio_mpipe_edesc_t edesc_head = { { 0 } };
  1579. gxio_mpipe_edesc_t edesc_body = { { 0 } };
  1580. long f_id = -1; /* id of the current fragment */
  1581. long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
  1582. long f_used = 0; /* bytes used from the current fragment */
  1583. void *f_data = skb->data + sh_len;
  1584. long n; /* size of the current piece of payload */
  1585. unsigned long tx_packets = 0, tx_bytes = 0;
  1586. unsigned int csum_start;
  1587. int segment;
  1588. /* Prepare to egress the headers: set up header edesc. */
  1589. csum_start = skb_checksum_start_offset(skb);
  1590. edesc_head.csum = 1;
  1591. edesc_head.csum_start = csum_start;
  1592. edesc_head.csum_dest = csum_start + skb->csum_offset;
  1593. edesc_head.xfer_size = sh_len;
  1594. /* This is only used to specify the TLB. */
  1595. edesc_head.stack_idx = md->first_buffer_stack;
  1596. edesc_body.stack_idx = md->first_buffer_stack;
  1597. /* Egress all the edescs. */
  1598. for (segment = 0; segment < sh->gso_segs; segment++) {
  1599. unsigned char *buf;
  1600. unsigned int p_used = 0;
  1601. /* Egress the header. */
  1602. buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
  1603. NET_IP_ALIGN;
  1604. edesc_head.va = va_to_tile_io_addr(buf);
  1605. gxio_mpipe_equeue_put_at(equeue, edesc_head, slot);
  1606. slot++;
  1607. /* Egress the payload. */
  1608. while (p_used < p_len) {
  1609. void *va;
  1610. /* Advance as needed. */
  1611. while (f_used >= f_size) {
  1612. f_id++;
  1613. f_size = skb_frag_size(&sh->frags[f_id]);
  1614. f_data = tile_net_frag_buf(&sh->frags[f_id]);
  1615. f_used = 0;
  1616. }
  1617. va = f_data + f_used;
  1618. /* Use bytes from the current fragment. */
  1619. n = p_len - p_used;
  1620. if (n > f_size - f_used)
  1621. n = f_size - f_used;
  1622. f_used += n;
  1623. p_used += n;
  1624. /* Egress a piece of the payload. */
  1625. edesc_body.va = va_to_tile_io_addr(va);
  1626. edesc_body.xfer_size = n;
  1627. edesc_body.bound = !(p_used < p_len);
  1628. gxio_mpipe_equeue_put_at(equeue, edesc_body, slot);
  1629. slot++;
  1630. }
  1631. tx_packets++;
  1632. tx_bytes += sh_len + p_len;
  1633. /* The last segment may be less than gso_size. */
  1634. data_len -= p_len;
  1635. if (data_len < p_len)
  1636. p_len = data_len;
  1637. }
  1638. /* Update stats. */
  1639. tile_net_stats_add(tx_packets, &dev->stats.tx_packets);
  1640. tile_net_stats_add(tx_bytes, &dev->stats.tx_bytes);
  1641. }
  1642. /* Do "TSO" handling for egress.
  1643. *
  1644. * Normally drivers set NETIF_F_TSO only to support hardware TSO;
  1645. * otherwise the stack uses scatter-gather to implement GSO in software.
  1646. * On our testing, enabling GSO support (via NETIF_F_SG) drops network
  1647. * performance down to around 7.5 Gbps on the 10G interfaces, although
  1648. * also dropping cpu utilization way down, to under 8%. But
  1649. * implementing "TSO" in the driver brings performance back up to line
  1650. * rate, while dropping cpu usage even further, to less than 4%. In
  1651. * practice, profiling of GSO shows that skb_segment() is what causes
  1652. * the performance overheads; we benefit in the driver from using
  1653. * preallocated memory to duplicate the TCP/IP headers.
  1654. */
  1655. static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
  1656. {
  1657. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1658. struct tile_net_priv *priv = netdev_priv(dev);
  1659. int channel = priv->echannel;
  1660. int instance = priv->instance;
  1661. struct mpipe_data *md = &mpipe_data[instance];
  1662. struct tile_net_egress *egress = &md->egress_for_echannel[channel];
  1663. struct tile_net_comps *comps =
  1664. info->mpipe[instance].comps_for_echannel[channel];
  1665. gxio_mpipe_equeue_t *equeue = egress->equeue;
  1666. unsigned long irqflags;
  1667. int num_edescs;
  1668. s64 slot;
  1669. /* Determine how many mpipe edesc's are needed. */
  1670. num_edescs = tso_count_edescs(skb);
  1671. local_irq_save(irqflags);
  1672. /* Try to acquire a completion entry and an egress slot. */
  1673. slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
  1674. equeue, num_edescs);
  1675. if (slot < 0) {
  1676. local_irq_restore(irqflags);
  1677. return NETDEV_TX_BUSY;
  1678. }
  1679. /* Set up copies of header data properly. */
  1680. tso_headers_prepare(skb, egress->headers, slot);
  1681. /* Actually pass the data to the network hardware. */
  1682. tso_egress(dev, equeue, skb, egress->headers, slot);
  1683. /* Add a completion record. */
  1684. add_comp(equeue, comps, slot + num_edescs - 1, skb);
  1685. local_irq_restore(irqflags);
  1686. /* Make sure the egress timer is scheduled. */
  1687. tile_net_schedule_egress_timer();
  1688. return NETDEV_TX_OK;
  1689. }
  1690. /* Analyze the body and frags for a transmit request. */
  1691. static unsigned int tile_net_tx_frags(struct frag *frags,
  1692. struct sk_buff *skb,
  1693. void *b_data, unsigned int b_len)
  1694. {
  1695. unsigned int i, n = 0;
  1696. struct skb_shared_info *sh = skb_shinfo(skb);
  1697. if (b_len != 0) {
  1698. frags[n].buf = b_data;
  1699. frags[n++].length = b_len;
  1700. }
  1701. for (i = 0; i < sh->nr_frags; i++) {
  1702. skb_frag_t *f = &sh->frags[i];
  1703. frags[n].buf = tile_net_frag_buf(f);
  1704. frags[n++].length = skb_frag_size(f);
  1705. }
  1706. return n;
  1707. }
  1708. /* Help the kernel transmit a packet. */
  1709. static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
  1710. {
  1711. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1712. struct tile_net_priv *priv = netdev_priv(dev);
  1713. int instance = priv->instance;
  1714. struct mpipe_data *md = &mpipe_data[instance];
  1715. struct tile_net_egress *egress =
  1716. &md->egress_for_echannel[priv->echannel];
  1717. gxio_mpipe_equeue_t *equeue = egress->equeue;
  1718. struct tile_net_comps *comps =
  1719. info->mpipe[instance].comps_for_echannel[priv->echannel];
  1720. unsigned int len = skb->len;
  1721. unsigned char *data = skb->data;
  1722. unsigned int num_edescs;
  1723. struct frag frags[MAX_FRAGS];
  1724. gxio_mpipe_edesc_t edescs[MAX_FRAGS];
  1725. unsigned long irqflags;
  1726. gxio_mpipe_edesc_t edesc = { { 0 } };
  1727. unsigned int i;
  1728. s64 slot;
  1729. if (skb_is_gso(skb))
  1730. return tile_net_tx_tso(skb, dev);
  1731. num_edescs = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
  1732. /* This is only used to specify the TLB. */
  1733. edesc.stack_idx = md->first_buffer_stack;
  1734. /* Prepare the edescs. */
  1735. for (i = 0; i < num_edescs; i++) {
  1736. edesc.xfer_size = frags[i].length;
  1737. edesc.va = va_to_tile_io_addr(frags[i].buf);
  1738. edescs[i] = edesc;
  1739. }
  1740. /* Mark the final edesc. */
  1741. edescs[num_edescs - 1].bound = 1;
  1742. /* Add checksum info to the initial edesc, if needed. */
  1743. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1744. unsigned int csum_start = skb_checksum_start_offset(skb);
  1745. edescs[0].csum = 1;
  1746. edescs[0].csum_start = csum_start;
  1747. edescs[0].csum_dest = csum_start + skb->csum_offset;
  1748. }
  1749. local_irq_save(irqflags);
  1750. /* Try to acquire a completion entry and an egress slot. */
  1751. slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
  1752. equeue, num_edescs);
  1753. if (slot < 0) {
  1754. local_irq_restore(irqflags);
  1755. return NETDEV_TX_BUSY;
  1756. }
  1757. for (i = 0; i < num_edescs; i++)
  1758. gxio_mpipe_equeue_put_at(equeue, edescs[i], slot++);
  1759. /* Store TX timestamp if needed. */
  1760. tile_tx_timestamp(skb, instance);
  1761. /* Add a completion record. */
  1762. add_comp(equeue, comps, slot - 1, skb);
  1763. /* NOTE: Use ETH_ZLEN for short packets (e.g. 42 < 60). */
  1764. tile_net_stats_add(1, &dev->stats.tx_packets);
  1765. tile_net_stats_add(max_t(unsigned int, len, ETH_ZLEN),
  1766. &dev->stats.tx_bytes);
  1767. local_irq_restore(irqflags);
  1768. /* Make sure the egress timer is scheduled. */
  1769. tile_net_schedule_egress_timer();
  1770. return NETDEV_TX_OK;
  1771. }
  1772. /* Return subqueue id on this core (one per core). */
  1773. static u16 tile_net_select_queue(struct net_device *dev, struct sk_buff *skb,
  1774. void *accel_priv, select_queue_fallback_t fallback)
  1775. {
  1776. return smp_processor_id();
  1777. }
  1778. /* Deal with a transmit timeout. */
  1779. static void tile_net_tx_timeout(struct net_device *dev)
  1780. {
  1781. int cpu;
  1782. for_each_online_cpu(cpu)
  1783. netif_wake_subqueue(dev, cpu);
  1784. }
  1785. /* Ioctl commands. */
  1786. static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1787. {
  1788. if (cmd == SIOCSHWTSTAMP)
  1789. return tile_hwtstamp_set(dev, rq);
  1790. if (cmd == SIOCGHWTSTAMP)
  1791. return tile_hwtstamp_get(dev, rq);
  1792. return -EOPNOTSUPP;
  1793. }
  1794. /* Change the MTU. */
  1795. static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
  1796. {
  1797. if (new_mtu < 68)
  1798. return -EINVAL;
  1799. if (new_mtu > ((jumbo_num != 0) ? 9000 : 1500))
  1800. return -EINVAL;
  1801. dev->mtu = new_mtu;
  1802. return 0;
  1803. }
  1804. /* Change the Ethernet address of the NIC.
  1805. *
  1806. * The hypervisor driver does not support changing MAC address. However,
  1807. * the hardware does not do anything with the MAC address, so the address
  1808. * which gets used on outgoing packets, and which is accepted on incoming
  1809. * packets, is completely up to us.
  1810. *
  1811. * Returns 0 on success, negative on failure.
  1812. */
  1813. static int tile_net_set_mac_address(struct net_device *dev, void *p)
  1814. {
  1815. struct sockaddr *addr = p;
  1816. if (!is_valid_ether_addr(addr->sa_data))
  1817. return -EINVAL;
  1818. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1819. return 0;
  1820. }
  1821. #ifdef CONFIG_NET_POLL_CONTROLLER
  1822. /* Polling 'interrupt' - used by things like netconsole to send skbs
  1823. * without having to re-enable interrupts. It's not called while
  1824. * the interrupt routine is executing.
  1825. */
  1826. static void tile_net_netpoll(struct net_device *dev)
  1827. {
  1828. int instance = mpipe_instance(dev);
  1829. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1830. struct mpipe_data *md = &mpipe_data[instance];
  1831. disable_percpu_irq(md->ingress_irq);
  1832. napi_schedule(&info->mpipe[instance].napi);
  1833. enable_percpu_irq(md->ingress_irq, 0);
  1834. }
  1835. #endif
  1836. static const struct net_device_ops tile_net_ops = {
  1837. .ndo_open = tile_net_open,
  1838. .ndo_stop = tile_net_stop,
  1839. .ndo_start_xmit = tile_net_tx,
  1840. .ndo_select_queue = tile_net_select_queue,
  1841. .ndo_do_ioctl = tile_net_ioctl,
  1842. .ndo_change_mtu = tile_net_change_mtu,
  1843. .ndo_tx_timeout = tile_net_tx_timeout,
  1844. .ndo_set_mac_address = tile_net_set_mac_address,
  1845. #ifdef CONFIG_NET_POLL_CONTROLLER
  1846. .ndo_poll_controller = tile_net_netpoll,
  1847. #endif
  1848. };
  1849. /* The setup function.
  1850. *
  1851. * This uses ether_setup() to assign various fields in dev, including
  1852. * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
  1853. */
  1854. static void tile_net_setup(struct net_device *dev)
  1855. {
  1856. netdev_features_t features = 0;
  1857. ether_setup(dev);
  1858. dev->netdev_ops = &tile_net_ops;
  1859. dev->watchdog_timeo = TILE_NET_TIMEOUT;
  1860. dev->mtu = 1500;
  1861. features |= NETIF_F_HW_CSUM;
  1862. features |= NETIF_F_SG;
  1863. features |= NETIF_F_TSO;
  1864. features |= NETIF_F_TSO6;
  1865. dev->hw_features |= features;
  1866. dev->vlan_features |= features;
  1867. dev->features |= features;
  1868. }
  1869. /* Allocate the device structure, register the device, and obtain the
  1870. * MAC address from the hypervisor.
  1871. */
  1872. static void tile_net_dev_init(const char *name, const uint8_t *mac)
  1873. {
  1874. int ret;
  1875. struct net_device *dev;
  1876. struct tile_net_priv *priv;
  1877. /* HACK: Ignore "loop" links. */
  1878. if (strncmp(name, "loop", 4) == 0)
  1879. return;
  1880. /* Allocate the device structure. Normally, "name" is a
  1881. * template, instantiated by register_netdev(), but not for us.
  1882. */
  1883. dev = alloc_netdev_mqs(sizeof(*priv), name, NET_NAME_UNKNOWN,
  1884. tile_net_setup, NR_CPUS, 1);
  1885. if (!dev) {
  1886. pr_err("alloc_netdev_mqs(%s) failed\n", name);
  1887. return;
  1888. }
  1889. /* Initialize "priv". */
  1890. priv = netdev_priv(dev);
  1891. priv->dev = dev;
  1892. priv->channel = -1;
  1893. priv->loopify_channel = -1;
  1894. priv->echannel = -1;
  1895. init_ptp_dev(priv);
  1896. /* Get the MAC address and set it in the device struct; this must
  1897. * be done before the device is opened. If the MAC is all zeroes,
  1898. * we use a random address, since we're probably on the simulator.
  1899. */
  1900. if (!is_zero_ether_addr(mac))
  1901. ether_addr_copy(dev->dev_addr, mac);
  1902. else
  1903. eth_hw_addr_random(dev);
  1904. /* Register the network device. */
  1905. ret = register_netdev(dev);
  1906. if (ret) {
  1907. netdev_err(dev, "register_netdev failed %d\n", ret);
  1908. free_netdev(dev);
  1909. return;
  1910. }
  1911. }
  1912. /* Per-cpu module initialization. */
  1913. static void tile_net_init_module_percpu(void *unused)
  1914. {
  1915. struct tile_net_info *info = this_cpu_ptr(&per_cpu_info);
  1916. int my_cpu = smp_processor_id();
  1917. int instance;
  1918. for (instance = 0; instance < NR_MPIPE_MAX; instance++) {
  1919. info->mpipe[instance].has_iqueue = false;
  1920. info->mpipe[instance].instance = instance;
  1921. }
  1922. info->my_cpu = my_cpu;
  1923. /* Initialize the egress timer. */
  1924. hrtimer_init(&info->egress_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1925. info->egress_timer.function = tile_net_handle_egress_timer;
  1926. }
  1927. /* Module initialization. */
  1928. static int __init tile_net_init_module(void)
  1929. {
  1930. int i;
  1931. char name[GXIO_MPIPE_LINK_NAME_LEN];
  1932. uint8_t mac[6];
  1933. pr_info("Tilera Network Driver\n");
  1934. BUILD_BUG_ON(NR_MPIPE_MAX != 2);
  1935. mutex_init(&tile_net_devs_for_channel_mutex);
  1936. /* Initialize each CPU. */
  1937. on_each_cpu(tile_net_init_module_percpu, NULL, 1);
  1938. /* Find out what devices we have, and initialize them. */
  1939. for (i = 0; gxio_mpipe_link_enumerate_mac(i, name, mac) >= 0; i++)
  1940. tile_net_dev_init(name, mac);
  1941. if (!network_cpus_init())
  1942. cpumask_and(&network_cpus_map, housekeeping_cpumask(),
  1943. cpu_online_mask);
  1944. return 0;
  1945. }
  1946. module_init(tile_net_init_module);