tilegx.c 53 KB

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