tilegx.c 64 KB

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