tilepro.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. /*
  2. * Copyright 2011 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/netdevice.h> /* struct device, and other headers */
  25. #include <linux/etherdevice.h> /* eth_type_trans */
  26. #include <linux/skbuff.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/cdev.h>
  29. #include <linux/hugetlb.h>
  30. #include <linux/in6.h>
  31. #include <linux/timer.h>
  32. #include <linux/io.h>
  33. #include <linux/u64_stats_sync.h>
  34. #include <asm/checksum.h>
  35. #include <asm/homecache.h>
  36. #include <hv/drv_xgbe_intf.h>
  37. #include <hv/drv_xgbe_impl.h>
  38. #include <hv/hypervisor.h>
  39. #include <hv/netio_intf.h>
  40. /* For TSO */
  41. #include <linux/ip.h>
  42. #include <linux/tcp.h>
  43. /*
  44. * First, "tile_net_init_module()" initializes all four "devices" which
  45. * can be used by linux.
  46. *
  47. * Then, "ifconfig DEVICE up" calls "tile_net_open()", which analyzes
  48. * the network cpus, then uses "tile_net_open_aux()" to initialize
  49. * LIPP/LEPP, and then uses "tile_net_open_inner()" to register all
  50. * the tiles, provide buffers to LIPP, allow ingress to start, and
  51. * turn on hypervisor interrupt handling (and NAPI) on all tiles.
  52. *
  53. * If registration fails due to the link being down, then "retry_work"
  54. * is used to keep calling "tile_net_open_inner()" until it succeeds.
  55. *
  56. * If "ifconfig DEVICE down" is called, it uses "tile_net_stop()" to
  57. * stop egress, drain the LIPP buffers, unregister all the tiles, stop
  58. * LIPP/LEPP, and wipe the LEPP queue.
  59. *
  60. * We start out with the ingress interrupt enabled on each CPU. When
  61. * this interrupt fires, we disable it, and call "napi_schedule()".
  62. * This will cause "tile_net_poll()" to be called, which will pull
  63. * packets from the netio queue, filtering them out, or passing them
  64. * to "netif_receive_skb()". If our budget is exhausted, we will
  65. * return, knowing we will be called again later. Otherwise, we
  66. * reenable the ingress interrupt, and call "napi_complete()".
  67. *
  68. * HACK: Since disabling the ingress interrupt is not reliable, we
  69. * ignore the interrupt if the global "active" flag is false.
  70. *
  71. *
  72. * NOTE: The use of "native_driver" ensures that EPP exists, and that
  73. * we are using "LIPP" and "LEPP".
  74. *
  75. * NOTE: Failing to free completions for an arbitrarily long time
  76. * (which is defined to be illegal) does in fact cause bizarre
  77. * problems. The "egress_timer" helps prevent this from happening.
  78. */
  79. /* HACK: Allow use of "jumbo" packets. */
  80. /* This should be 1500 if "jumbo" is not set in LIPP. */
  81. /* This should be at most 10226 (10240 - 14) if "jumbo" is set in LIPP. */
  82. /* ISSUE: This has not been thoroughly tested (except at 1500). */
  83. #define TILE_NET_MTU 1500
  84. /* HACK: Define this to verify incoming packets. */
  85. /* #define TILE_NET_VERIFY_INGRESS */
  86. /* Use 3000 to enable the Linux Traffic Control (QoS) layer, else 0. */
  87. #define TILE_NET_TX_QUEUE_LEN 0
  88. /* Define to dump packets (prints out the whole packet on tx and rx). */
  89. /* #define TILE_NET_DUMP_PACKETS */
  90. /* Define to enable debug spew (all PDEBUG's are enabled). */
  91. /* #define TILE_NET_DEBUG */
  92. /* Define to activate paranoia checks. */
  93. /* #define TILE_NET_PARANOIA */
  94. /* Default transmit lockup timeout period, in jiffies. */
  95. #define TILE_NET_TIMEOUT (5 * HZ)
  96. /* Default retry interval for bringing up the NetIO interface, in jiffies. */
  97. #define TILE_NET_RETRY_INTERVAL (5 * HZ)
  98. /* Number of ports (xgbe0, xgbe1, gbe0, gbe1). */
  99. #define TILE_NET_DEVS 4
  100. /* Paranoia. */
  101. #if NET_IP_ALIGN != LIPP_PACKET_PADDING
  102. #error "NET_IP_ALIGN must match LIPP_PACKET_PADDING."
  103. #endif
  104. /* Debug print. */
  105. #ifdef TILE_NET_DEBUG
  106. #define PDEBUG(fmt, args...) net_printk(fmt, ## args)
  107. #else
  108. #define PDEBUG(fmt, args...)
  109. #endif
  110. MODULE_AUTHOR("Tilera");
  111. MODULE_LICENSE("GPL");
  112. /*
  113. * Queue of incoming packets for a specific cpu and device.
  114. *
  115. * Includes a pointer to the "system" data, and the actual "user" data.
  116. */
  117. struct tile_netio_queue {
  118. netio_queue_impl_t *__system_part;
  119. netio_queue_user_impl_t __user_part;
  120. };
  121. /*
  122. * Statistics counters for a specific cpu and device.
  123. */
  124. struct tile_net_stats_t {
  125. struct u64_stats_sync syncp;
  126. u64 rx_packets; /* total packets received */
  127. u64 tx_packets; /* total packets transmitted */
  128. u64 rx_bytes; /* total bytes received */
  129. u64 tx_bytes; /* total bytes transmitted */
  130. u64 rx_errors; /* packets truncated or marked bad by hw */
  131. u64 rx_dropped; /* packets not for us or intf not up */
  132. };
  133. /*
  134. * Info for a specific cpu and device.
  135. *
  136. * ISSUE: There is a "dev" pointer in "napi" as well.
  137. */
  138. struct tile_net_cpu {
  139. /* The NAPI struct. */
  140. struct napi_struct napi;
  141. /* Packet queue. */
  142. struct tile_netio_queue queue;
  143. /* Statistics. */
  144. struct tile_net_stats_t stats;
  145. /* True iff NAPI is enabled. */
  146. bool napi_enabled;
  147. /* True if this tile has successfully registered with the IPP. */
  148. bool registered;
  149. /* True if the link was down last time we tried to register. */
  150. bool link_down;
  151. /* True if "egress_timer" is scheduled. */
  152. bool egress_timer_scheduled;
  153. /* Number of small sk_buffs which must still be provided. */
  154. unsigned int num_needed_small_buffers;
  155. /* Number of large sk_buffs which must still be provided. */
  156. unsigned int num_needed_large_buffers;
  157. /* A timer for handling egress completions. */
  158. struct timer_list egress_timer;
  159. };
  160. /*
  161. * Info for a specific device.
  162. */
  163. struct tile_net_priv {
  164. /* Our network device. */
  165. struct net_device *dev;
  166. /* Pages making up the egress queue. */
  167. struct page *eq_pages;
  168. /* Address of the actual egress queue. */
  169. lepp_queue_t *eq;
  170. /* Protects "eq". */
  171. spinlock_t eq_lock;
  172. /* The hypervisor handle for this interface. */
  173. int hv_devhdl;
  174. /* The intr bit mask that IDs this device. */
  175. u32 intr_id;
  176. /* True iff "tile_net_open_aux()" has succeeded. */
  177. bool partly_opened;
  178. /* True iff the device is "active". */
  179. bool active;
  180. /* Effective network cpus. */
  181. struct cpumask network_cpus_map;
  182. /* Number of network cpus. */
  183. int network_cpus_count;
  184. /* Credits per network cpu. */
  185. int network_cpus_credits;
  186. /* For NetIO bringup retries. */
  187. struct delayed_work retry_work;
  188. /* Quick access to per cpu data. */
  189. struct tile_net_cpu *cpu[NR_CPUS];
  190. };
  191. /* Log2 of the number of small pages needed for the egress queue. */
  192. #define EQ_ORDER get_order(sizeof(lepp_queue_t))
  193. /* Size of the egress queue's pages. */
  194. #define EQ_SIZE (1 << (PAGE_SHIFT + EQ_ORDER))
  195. /*
  196. * The actual devices (xgbe0, xgbe1, gbe0, gbe1).
  197. */
  198. static struct net_device *tile_net_devs[TILE_NET_DEVS];
  199. /*
  200. * The "tile_net_cpu" structures for each device.
  201. */
  202. static DEFINE_PER_CPU(struct tile_net_cpu, hv_xgbe0);
  203. static DEFINE_PER_CPU(struct tile_net_cpu, hv_xgbe1);
  204. static DEFINE_PER_CPU(struct tile_net_cpu, hv_gbe0);
  205. static DEFINE_PER_CPU(struct tile_net_cpu, hv_gbe1);
  206. /*
  207. * True if "network_cpus" was specified.
  208. */
  209. static bool network_cpus_used;
  210. /*
  211. * The actual cpus in "network_cpus".
  212. */
  213. static struct cpumask network_cpus_map;
  214. #ifdef TILE_NET_DEBUG
  215. /*
  216. * printk with extra stuff.
  217. *
  218. * We print the CPU we're running in brackets.
  219. */
  220. static void net_printk(char *fmt, ...)
  221. {
  222. int i;
  223. int len;
  224. va_list args;
  225. static char buf[256];
  226. len = sprintf(buf, "tile_net[%2.2d]: ", smp_processor_id());
  227. va_start(args, fmt);
  228. i = vscnprintf(buf + len, sizeof(buf) - len - 1, fmt, args);
  229. va_end(args);
  230. buf[255] = '\0';
  231. pr_notice(buf);
  232. }
  233. #endif
  234. #ifdef TILE_NET_DUMP_PACKETS
  235. /*
  236. * Dump a packet.
  237. */
  238. static void dump_packet(unsigned char *data, unsigned long length, char *s)
  239. {
  240. int my_cpu = smp_processor_id();
  241. unsigned long i;
  242. char buf[128];
  243. static unsigned int count;
  244. pr_info("dump_packet(data %p, length 0x%lx s %s count 0x%x)\n",
  245. data, length, s, count++);
  246. pr_info("\n");
  247. for (i = 0; i < length; i++) {
  248. if ((i & 0xf) == 0)
  249. sprintf(buf, "[%02d] %8.8lx:", my_cpu, i);
  250. sprintf(buf + strlen(buf), " %2.2x", data[i]);
  251. if ((i & 0xf) == 0xf || i == length - 1) {
  252. strcat(buf, "\n");
  253. pr_info("%s", buf);
  254. }
  255. }
  256. }
  257. #endif
  258. /*
  259. * Provide support for the __netio_fastio1() swint
  260. * (see <hv/drv_xgbe_intf.h> for how it is used).
  261. *
  262. * The fastio swint2 call may clobber all the caller-saved registers.
  263. * It rarely clobbers memory, but we allow for the possibility in
  264. * the signature just to be on the safe side.
  265. *
  266. * Also, gcc doesn't seem to allow an input operand to be
  267. * clobbered, so we fake it with dummy outputs.
  268. *
  269. * This function can't be static because of the way it is declared
  270. * in the netio header.
  271. */
  272. inline int __netio_fastio1(u32 fastio_index, u32 arg0)
  273. {
  274. long result, clobber_r1, clobber_r10;
  275. asm volatile("swint2"
  276. : "=R00" (result),
  277. "=R01" (clobber_r1), "=R10" (clobber_r10)
  278. : "R10" (fastio_index), "R01" (arg0)
  279. : "memory", "r2", "r3", "r4",
  280. "r5", "r6", "r7", "r8", "r9",
  281. "r11", "r12", "r13", "r14",
  282. "r15", "r16", "r17", "r18", "r19",
  283. "r20", "r21", "r22", "r23", "r24",
  284. "r25", "r26", "r27", "r28", "r29");
  285. return result;
  286. }
  287. static void tile_net_return_credit(struct tile_net_cpu *info)
  288. {
  289. struct tile_netio_queue *queue = &info->queue;
  290. netio_queue_user_impl_t *qup = &queue->__user_part;
  291. /* Return four credits after every fourth packet. */
  292. if (--qup->__receive_credit_remaining == 0) {
  293. u32 interval = qup->__receive_credit_interval;
  294. qup->__receive_credit_remaining = interval;
  295. __netio_fastio_return_credits(qup->__fastio_index, interval);
  296. }
  297. }
  298. /*
  299. * Provide a linux buffer to LIPP.
  300. */
  301. static void tile_net_provide_linux_buffer(struct tile_net_cpu *info,
  302. void *va, bool small)
  303. {
  304. struct tile_netio_queue *queue = &info->queue;
  305. /* Convert "va" and "small" to "linux_buffer_t". */
  306. unsigned int buffer = ((unsigned int)(__pa(va) >> 7) << 1) + small;
  307. __netio_fastio_free_buffer(queue->__user_part.__fastio_index, buffer);
  308. }
  309. /*
  310. * Provide a linux buffer for LIPP.
  311. *
  312. * Note that the ACTUAL allocation for each buffer is a "struct sk_buff",
  313. * plus a chunk of memory that includes not only the requested bytes, but
  314. * also NET_SKB_PAD bytes of initial padding, and a "struct skb_shared_info".
  315. *
  316. * Note that "struct skb_shared_info" is 88 bytes with 64K pages and
  317. * 268 bytes with 4K pages (since the frags[] array needs 18 entries).
  318. *
  319. * Without jumbo packets, the maximum packet size will be 1536 bytes,
  320. * and we use 2 bytes (NET_IP_ALIGN) of padding. ISSUE: If we told
  321. * the hardware to clip at 1518 bytes instead of 1536 bytes, then we
  322. * could save an entire cache line, but in practice, we don't need it.
  323. *
  324. * Since CPAs are 38 bits, and we can only encode the high 31 bits in
  325. * a "linux_buffer_t", the low 7 bits must be zero, and thus, we must
  326. * align the actual "va" mod 128.
  327. *
  328. * We assume that the underlying "head" will be aligned mod 64. Note
  329. * that in practice, we have seen "head" NOT aligned mod 128 even when
  330. * using 2048 byte allocations, which is surprising.
  331. *
  332. * If "head" WAS always aligned mod 128, we could change LIPP to
  333. * assume that the low SIX bits are zero, and the 7th bit is one, that
  334. * is, align the actual "va" mod 128 plus 64, which would be "free".
  335. *
  336. * For now, the actual "head" pointer points at NET_SKB_PAD bytes of
  337. * padding, plus 28 or 92 bytes of extra padding, plus the sk_buff
  338. * pointer, plus the NET_IP_ALIGN padding, plus 126 or 1536 bytes for
  339. * the actual packet, plus 62 bytes of empty padding, plus some
  340. * padding and the "struct skb_shared_info".
  341. *
  342. * With 64K pages, a large buffer thus needs 32+92+4+2+1536+62+88
  343. * bytes, or 1816 bytes, which fits comfortably into 2048 bytes.
  344. *
  345. * With 64K pages, a small buffer thus needs 32+92+4+2+126+88
  346. * bytes, or 344 bytes, which means we are wasting 64+ bytes, and
  347. * could presumably increase the size of small buffers.
  348. *
  349. * With 4K pages, a large buffer thus needs 32+92+4+2+1536+62+268
  350. * bytes, or 1996 bytes, which fits comfortably into 2048 bytes.
  351. *
  352. * With 4K pages, a small buffer thus needs 32+92+4+2+126+268
  353. * bytes, or 524 bytes, which is annoyingly wasteful.
  354. *
  355. * Maybe we should increase LIPP_SMALL_PACKET_SIZE to 192?
  356. *
  357. * ISSUE: Maybe we should increase "NET_SKB_PAD" to 64?
  358. */
  359. static bool tile_net_provide_needed_buffer(struct tile_net_cpu *info,
  360. bool small)
  361. {
  362. #if TILE_NET_MTU <= 1536
  363. /* Without "jumbo", 2 + 1536 should be sufficient. */
  364. unsigned int large_size = NET_IP_ALIGN + 1536;
  365. #else
  366. /* ISSUE: This has not been tested. */
  367. unsigned int large_size = NET_IP_ALIGN + TILE_NET_MTU + 100;
  368. #endif
  369. /* Avoid "false sharing" with last cache line. */
  370. /* ISSUE: This is already done by "netdev_alloc_skb()". */
  371. unsigned int len =
  372. (((small ? LIPP_SMALL_PACKET_SIZE : large_size) +
  373. CHIP_L2_LINE_SIZE() - 1) & -CHIP_L2_LINE_SIZE());
  374. unsigned int padding = 128 - NET_SKB_PAD;
  375. unsigned int align;
  376. struct sk_buff *skb;
  377. void *va;
  378. struct sk_buff **skb_ptr;
  379. /* Request 96 extra bytes for alignment purposes. */
  380. skb = netdev_alloc_skb(info->napi.dev, len + padding);
  381. if (skb == NULL)
  382. return false;
  383. /* Skip 32 or 96 bytes to align "data" mod 128. */
  384. align = -(long)skb->data & (128 - 1);
  385. BUG_ON(align > padding);
  386. skb_reserve(skb, align);
  387. /* This address is given to IPP. */
  388. va = skb->data;
  389. /* Buffers must not span a huge page. */
  390. BUG_ON(((((long)va & ~HPAGE_MASK) + len) & HPAGE_MASK) != 0);
  391. #ifdef TILE_NET_PARANOIA
  392. #if CHIP_HAS_CBOX_HOME_MAP()
  393. if (hash_default) {
  394. HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)va);
  395. if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
  396. panic("Non-HFH ingress buffer! VA=%p Mode=%d PTE=%llx",
  397. va, hv_pte_get_mode(pte), hv_pte_val(pte));
  398. }
  399. #endif
  400. #endif
  401. /* Invalidate the packet buffer. */
  402. if (!hash_default)
  403. __inv_buffer(va, len);
  404. /* Skip two bytes to satisfy LIPP assumptions. */
  405. /* Note that this aligns IP on a 16 byte boundary. */
  406. /* ISSUE: Do this when the packet arrives? */
  407. skb_reserve(skb, NET_IP_ALIGN);
  408. /* Save a back-pointer to 'skb'. */
  409. skb_ptr = va - sizeof(*skb_ptr);
  410. *skb_ptr = skb;
  411. /* Make sure "skb_ptr" has been flushed. */
  412. __insn_mf();
  413. /* Provide the new buffer. */
  414. tile_net_provide_linux_buffer(info, va, small);
  415. return true;
  416. }
  417. /*
  418. * Provide linux buffers for LIPP.
  419. */
  420. static void tile_net_provide_needed_buffers(struct tile_net_cpu *info)
  421. {
  422. while (info->num_needed_small_buffers != 0) {
  423. if (!tile_net_provide_needed_buffer(info, true))
  424. goto oops;
  425. info->num_needed_small_buffers--;
  426. }
  427. while (info->num_needed_large_buffers != 0) {
  428. if (!tile_net_provide_needed_buffer(info, false))
  429. goto oops;
  430. info->num_needed_large_buffers--;
  431. }
  432. return;
  433. oops:
  434. /* Add a description to the page allocation failure dump. */
  435. pr_notice("Could not provide a linux buffer to LIPP.\n");
  436. }
  437. /*
  438. * Grab some LEPP completions, and store them in "comps", of size
  439. * "comps_size", and return the number of completions which were
  440. * stored, so the caller can free them.
  441. */
  442. static unsigned int tile_net_lepp_grab_comps(lepp_queue_t *eq,
  443. struct sk_buff *comps[],
  444. unsigned int comps_size,
  445. unsigned int min_size)
  446. {
  447. unsigned int n = 0;
  448. unsigned int comp_head = eq->comp_head;
  449. unsigned int comp_busy = eq->comp_busy;
  450. while (comp_head != comp_busy && n < comps_size) {
  451. comps[n++] = eq->comps[comp_head];
  452. LEPP_QINC(comp_head);
  453. }
  454. if (n < min_size)
  455. return 0;
  456. eq->comp_head = comp_head;
  457. return n;
  458. }
  459. /*
  460. * Free some comps, and return true iff there are still some pending.
  461. */
  462. static bool tile_net_lepp_free_comps(struct net_device *dev, bool all)
  463. {
  464. struct tile_net_priv *priv = netdev_priv(dev);
  465. lepp_queue_t *eq = priv->eq;
  466. struct sk_buff *olds[64];
  467. unsigned int wanted = 64;
  468. unsigned int i, n;
  469. bool pending;
  470. spin_lock(&priv->eq_lock);
  471. if (all)
  472. eq->comp_busy = eq->comp_tail;
  473. n = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
  474. pending = (eq->comp_head != eq->comp_tail);
  475. spin_unlock(&priv->eq_lock);
  476. for (i = 0; i < n; i++)
  477. kfree_skb(olds[i]);
  478. return pending;
  479. }
  480. /*
  481. * Make sure the egress timer is scheduled.
  482. *
  483. * Note that we use "schedule if not scheduled" logic instead of the more
  484. * obvious "reschedule" logic, because "reschedule" is fairly expensive.
  485. */
  486. static void tile_net_schedule_egress_timer(struct tile_net_cpu *info)
  487. {
  488. if (!info->egress_timer_scheduled) {
  489. mod_timer_pinned(&info->egress_timer, jiffies + 1);
  490. info->egress_timer_scheduled = true;
  491. }
  492. }
  493. /*
  494. * The "function" for "info->egress_timer".
  495. *
  496. * This timer will reschedule itself as long as there are any pending
  497. * completions expected (on behalf of any tile).
  498. *
  499. * ISSUE: Realistically, will the timer ever stop scheduling itself?
  500. *
  501. * ISSUE: This timer is almost never actually needed, so just use a global
  502. * timer that can run on any tile.
  503. *
  504. * ISSUE: Maybe instead track number of expected completions, and free
  505. * only that many, resetting to zero if "pending" is ever false.
  506. */
  507. static void tile_net_handle_egress_timer(unsigned long arg)
  508. {
  509. struct tile_net_cpu *info = (struct tile_net_cpu *)arg;
  510. struct net_device *dev = info->napi.dev;
  511. /* The timer is no longer scheduled. */
  512. info->egress_timer_scheduled = false;
  513. /* Free comps, and reschedule timer if more are pending. */
  514. if (tile_net_lepp_free_comps(dev, false))
  515. tile_net_schedule_egress_timer(info);
  516. }
  517. static void tile_net_discard_aux(struct tile_net_cpu *info, int index)
  518. {
  519. struct tile_netio_queue *queue = &info->queue;
  520. netio_queue_impl_t *qsp = queue->__system_part;
  521. netio_queue_user_impl_t *qup = &queue->__user_part;
  522. int index2_aux = index + sizeof(netio_pkt_t);
  523. int index2 =
  524. ((index2_aux ==
  525. qsp->__packet_receive_queue.__last_packet_plus_one) ?
  526. 0 : index2_aux);
  527. netio_pkt_t *pkt = (netio_pkt_t *)((unsigned long) &qsp[1] + index);
  528. /* Extract the "linux_buffer_t". */
  529. unsigned int buffer = pkt->__packet.word;
  530. /* Convert "linux_buffer_t" to "va". */
  531. void *va = __va((phys_addr_t)(buffer >> 1) << 7);
  532. /* Acquire the associated "skb". */
  533. struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
  534. struct sk_buff *skb = *skb_ptr;
  535. kfree_skb(skb);
  536. /* Consume this packet. */
  537. qup->__packet_receive_read = index2;
  538. }
  539. /*
  540. * Like "tile_net_poll()", but just discard packets.
  541. */
  542. static void tile_net_discard_packets(struct net_device *dev)
  543. {
  544. struct tile_net_priv *priv = netdev_priv(dev);
  545. int my_cpu = smp_processor_id();
  546. struct tile_net_cpu *info = priv->cpu[my_cpu];
  547. struct tile_netio_queue *queue = &info->queue;
  548. netio_queue_impl_t *qsp = queue->__system_part;
  549. netio_queue_user_impl_t *qup = &queue->__user_part;
  550. while (qup->__packet_receive_read !=
  551. qsp->__packet_receive_queue.__packet_write) {
  552. int index = qup->__packet_receive_read;
  553. tile_net_discard_aux(info, index);
  554. }
  555. }
  556. /*
  557. * Handle the next packet. Return true if "processed", false if "filtered".
  558. */
  559. static bool tile_net_poll_aux(struct tile_net_cpu *info, int index)
  560. {
  561. struct net_device *dev = info->napi.dev;
  562. struct tile_netio_queue *queue = &info->queue;
  563. netio_queue_impl_t *qsp = queue->__system_part;
  564. netio_queue_user_impl_t *qup = &queue->__user_part;
  565. struct tile_net_stats_t *stats = &info->stats;
  566. int filter;
  567. int index2_aux = index + sizeof(netio_pkt_t);
  568. int index2 =
  569. ((index2_aux ==
  570. qsp->__packet_receive_queue.__last_packet_plus_one) ?
  571. 0 : index2_aux);
  572. netio_pkt_t *pkt = (netio_pkt_t *)((unsigned long) &qsp[1] + index);
  573. netio_pkt_metadata_t *metadata = NETIO_PKT_METADATA(pkt);
  574. netio_pkt_status_t pkt_status = NETIO_PKT_STATUS_M(metadata, pkt);
  575. /* Extract the packet size. FIXME: Shouldn't the second line */
  576. /* get subtracted? Mostly moot, since it should be "zero". */
  577. unsigned long len =
  578. (NETIO_PKT_CUSTOM_LENGTH(pkt) +
  579. NET_IP_ALIGN - NETIO_PACKET_PADDING);
  580. /* Extract the "linux_buffer_t". */
  581. unsigned int buffer = pkt->__packet.word;
  582. /* Extract "small" (vs "large"). */
  583. bool small = ((buffer & 1) != 0);
  584. /* Convert "linux_buffer_t" to "va". */
  585. void *va = __va((phys_addr_t)(buffer >> 1) << 7);
  586. /* Extract the packet data pointer. */
  587. /* Compare to "NETIO_PKT_CUSTOM_DATA(pkt)". */
  588. unsigned char *buf = va + NET_IP_ALIGN;
  589. /* Invalidate the packet buffer. */
  590. if (!hash_default)
  591. __inv_buffer(buf, len);
  592. /* ISSUE: Is this needed? */
  593. dev->last_rx = jiffies;
  594. #ifdef TILE_NET_DUMP_PACKETS
  595. dump_packet(buf, len, "rx");
  596. #endif /* TILE_NET_DUMP_PACKETS */
  597. #ifdef TILE_NET_VERIFY_INGRESS
  598. if (pkt_status == NETIO_PKT_STATUS_OVERSIZE && len >= 64) {
  599. dump_packet(buf, len, "rx");
  600. panic("Unexpected OVERSIZE.");
  601. }
  602. #endif
  603. filter = 0;
  604. if (pkt_status == NETIO_PKT_STATUS_BAD) {
  605. /* Handle CRC error and hardware truncation. */
  606. filter = 2;
  607. } else if (!(dev->flags & IFF_UP)) {
  608. /* Filter packets received before we're up. */
  609. filter = 1;
  610. } else if (NETIO_PKT_ETHERTYPE_RECOGNIZED_M(metadata, pkt) &&
  611. pkt_status == NETIO_PKT_STATUS_UNDERSIZE) {
  612. /* Filter "truncated" packets. */
  613. filter = 2;
  614. } else if (!(dev->flags & IFF_PROMISC)) {
  615. if (!is_multicast_ether_addr(buf)) {
  616. /* Filter packets not for our address. */
  617. const u8 *mine = dev->dev_addr;
  618. filter = !ether_addr_equal(mine, buf);
  619. }
  620. }
  621. u64_stats_update_begin(&stats->syncp);
  622. if (filter != 0) {
  623. if (filter == 1)
  624. stats->rx_dropped++;
  625. else
  626. stats->rx_errors++;
  627. tile_net_provide_linux_buffer(info, va, small);
  628. } else {
  629. /* Acquire the associated "skb". */
  630. struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
  631. struct sk_buff *skb = *skb_ptr;
  632. /* Paranoia. */
  633. if (skb->data != buf)
  634. panic("Corrupt linux buffer from LIPP! "
  635. "VA=%p, skb=%p, skb->data=%p\n",
  636. va, skb, skb->data);
  637. /* Encode the actual packet length. */
  638. skb_put(skb, len);
  639. /* NOTE: This call also sets "skb->dev = dev". */
  640. skb->protocol = eth_type_trans(skb, dev);
  641. /* Avoid recomputing "good" TCP/UDP checksums. */
  642. if (NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt))
  643. skb->ip_summed = CHECKSUM_UNNECESSARY;
  644. netif_receive_skb(skb);
  645. stats->rx_packets++;
  646. stats->rx_bytes += len;
  647. }
  648. u64_stats_update_end(&stats->syncp);
  649. /* ISSUE: It would be nice to defer this until the packet has */
  650. /* actually been processed. */
  651. tile_net_return_credit(info);
  652. /* Consume this packet. */
  653. qup->__packet_receive_read = index2;
  654. return !filter;
  655. }
  656. /*
  657. * Handle some packets for the given device on the current CPU.
  658. *
  659. * If "tile_net_stop()" is called on some other tile while this
  660. * function is running, we will return, hopefully before that
  661. * other tile asks us to call "napi_disable()".
  662. *
  663. * The "rotting packet" race condition occurs if a packet arrives
  664. * during the extremely narrow window between the queue appearing to
  665. * be empty, and the ingress interrupt being re-enabled. This happens
  666. * a LOT under heavy network load.
  667. */
  668. static int tile_net_poll(struct napi_struct *napi, int budget)
  669. {
  670. struct net_device *dev = napi->dev;
  671. struct tile_net_priv *priv = netdev_priv(dev);
  672. int my_cpu = smp_processor_id();
  673. struct tile_net_cpu *info = priv->cpu[my_cpu];
  674. struct tile_netio_queue *queue = &info->queue;
  675. netio_queue_impl_t *qsp = queue->__system_part;
  676. netio_queue_user_impl_t *qup = &queue->__user_part;
  677. unsigned int work = 0;
  678. if (budget <= 0)
  679. goto done;
  680. while (priv->active) {
  681. int index = qup->__packet_receive_read;
  682. if (index == qsp->__packet_receive_queue.__packet_write)
  683. break;
  684. if (tile_net_poll_aux(info, index)) {
  685. if (++work >= budget)
  686. goto done;
  687. }
  688. }
  689. napi_complete(&info->napi);
  690. if (!priv->active)
  691. goto done;
  692. /* Re-enable the ingress interrupt. */
  693. enable_percpu_irq(priv->intr_id, 0);
  694. /* HACK: Avoid the "rotting packet" problem (see above). */
  695. if (qup->__packet_receive_read !=
  696. qsp->__packet_receive_queue.__packet_write) {
  697. /* ISSUE: Sometimes this returns zero, presumably */
  698. /* because an interrupt was handled for this tile. */
  699. (void)napi_reschedule(&info->napi);
  700. }
  701. done:
  702. if (priv->active)
  703. tile_net_provide_needed_buffers(info);
  704. return work;
  705. }
  706. /*
  707. * Handle an ingress interrupt for the given device on the current cpu.
  708. *
  709. * ISSUE: Sometimes this gets called after "disable_percpu_irq()" has
  710. * been called! This is probably due to "pending hypervisor downcalls".
  711. *
  712. * ISSUE: Is there any race condition between the "napi_schedule()" here
  713. * and the "napi_complete()" call above?
  714. */
  715. static irqreturn_t tile_net_handle_ingress_interrupt(int irq, void *dev_ptr)
  716. {
  717. struct net_device *dev = (struct net_device *)dev_ptr;
  718. struct tile_net_priv *priv = netdev_priv(dev);
  719. int my_cpu = smp_processor_id();
  720. struct tile_net_cpu *info = priv->cpu[my_cpu];
  721. /* Disable the ingress interrupt. */
  722. disable_percpu_irq(priv->intr_id);
  723. /* Ignore unwanted interrupts. */
  724. if (!priv->active)
  725. return IRQ_HANDLED;
  726. /* ISSUE: Sometimes "info->napi_enabled" is false here. */
  727. napi_schedule(&info->napi);
  728. return IRQ_HANDLED;
  729. }
  730. /*
  731. * One time initialization per interface.
  732. */
  733. static int tile_net_open_aux(struct net_device *dev)
  734. {
  735. struct tile_net_priv *priv = netdev_priv(dev);
  736. int ret;
  737. int dummy;
  738. unsigned int epp_lotar;
  739. /*
  740. * Find out where EPP memory should be homed.
  741. */
  742. ret = hv_dev_pread(priv->hv_devhdl, 0,
  743. (HV_VirtAddr)&epp_lotar, sizeof(epp_lotar),
  744. NETIO_EPP_SHM_OFF);
  745. if (ret < 0) {
  746. pr_err("could not read epp_shm_queue lotar.\n");
  747. return -EIO;
  748. }
  749. /*
  750. * Home the page on the EPP.
  751. */
  752. {
  753. int epp_home = hv_lotar_to_cpu(epp_lotar);
  754. homecache_change_page_home(priv->eq_pages, EQ_ORDER, epp_home);
  755. }
  756. /*
  757. * Register the EPP shared memory queue.
  758. */
  759. {
  760. netio_ipp_address_t ea = {
  761. .va = 0,
  762. .pa = __pa(priv->eq),
  763. .pte = hv_pte(0),
  764. .size = EQ_SIZE,
  765. };
  766. ea.pte = hv_pte_set_lotar(ea.pte, epp_lotar);
  767. ea.pte = hv_pte_set_mode(ea.pte, HV_PTE_MODE_CACHE_TILE_L3);
  768. ret = hv_dev_pwrite(priv->hv_devhdl, 0,
  769. (HV_VirtAddr)&ea,
  770. sizeof(ea),
  771. NETIO_EPP_SHM_OFF);
  772. if (ret < 0)
  773. return -EIO;
  774. }
  775. /*
  776. * Start LIPP/LEPP.
  777. */
  778. if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  779. sizeof(dummy), NETIO_IPP_START_SHIM_OFF) < 0) {
  780. pr_warn("Failed to start LIPP/LEPP\n");
  781. return -EIO;
  782. }
  783. return 0;
  784. }
  785. /*
  786. * Register with hypervisor on the current CPU.
  787. *
  788. * Strangely, this function does important things even if it "fails",
  789. * which is especially common if the link is not up yet. Hopefully
  790. * these things are all "harmless" if done twice!
  791. */
  792. static void tile_net_register(void *dev_ptr)
  793. {
  794. struct net_device *dev = (struct net_device *)dev_ptr;
  795. struct tile_net_priv *priv = netdev_priv(dev);
  796. int my_cpu = smp_processor_id();
  797. struct tile_net_cpu *info;
  798. struct tile_netio_queue *queue;
  799. /* Only network cpus can receive packets. */
  800. int queue_id =
  801. cpumask_test_cpu(my_cpu, &priv->network_cpus_map) ? 0 : 255;
  802. netio_input_config_t config = {
  803. .flags = 0,
  804. .num_receive_packets = priv->network_cpus_credits,
  805. .queue_id = queue_id
  806. };
  807. int ret = 0;
  808. netio_queue_impl_t *queuep;
  809. PDEBUG("tile_net_register(queue_id %d)\n", queue_id);
  810. if (!strcmp(dev->name, "xgbe0"))
  811. info = this_cpu_ptr(&hv_xgbe0);
  812. else if (!strcmp(dev->name, "xgbe1"))
  813. info = this_cpu_ptr(&hv_xgbe1);
  814. else if (!strcmp(dev->name, "gbe0"))
  815. info = this_cpu_ptr(&hv_gbe0);
  816. else if (!strcmp(dev->name, "gbe1"))
  817. info = this_cpu_ptr(&hv_gbe1);
  818. else
  819. BUG();
  820. /* Initialize the egress timer. */
  821. init_timer(&info->egress_timer);
  822. info->egress_timer.data = (long)info;
  823. info->egress_timer.function = tile_net_handle_egress_timer;
  824. u64_stats_init(&info->stats.syncp);
  825. priv->cpu[my_cpu] = info;
  826. /*
  827. * Register ourselves with LIPP. This does a lot of stuff,
  828. * including invoking the LIPP registration code.
  829. */
  830. ret = hv_dev_pwrite(priv->hv_devhdl, 0,
  831. (HV_VirtAddr)&config,
  832. sizeof(netio_input_config_t),
  833. NETIO_IPP_INPUT_REGISTER_OFF);
  834. PDEBUG("hv_dev_pwrite(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
  835. ret);
  836. if (ret < 0) {
  837. if (ret != NETIO_LINK_DOWN) {
  838. printk(KERN_DEBUG "hv_dev_pwrite "
  839. "NETIO_IPP_INPUT_REGISTER_OFF failure %d\n",
  840. ret);
  841. }
  842. info->link_down = (ret == NETIO_LINK_DOWN);
  843. return;
  844. }
  845. /*
  846. * Get the pointer to our queue's system part.
  847. */
  848. ret = hv_dev_pread(priv->hv_devhdl, 0,
  849. (HV_VirtAddr)&queuep,
  850. sizeof(netio_queue_impl_t *),
  851. NETIO_IPP_INPUT_REGISTER_OFF);
  852. PDEBUG("hv_dev_pread(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
  853. ret);
  854. PDEBUG("queuep %p\n", queuep);
  855. if (ret <= 0) {
  856. /* ISSUE: Shouldn't this be a fatal error? */
  857. pr_err("hv_dev_pread NETIO_IPP_INPUT_REGISTER_OFF failure\n");
  858. return;
  859. }
  860. queue = &info->queue;
  861. queue->__system_part = queuep;
  862. memset(&queue->__user_part, 0, sizeof(netio_queue_user_impl_t));
  863. /* This is traditionally "config.num_receive_packets / 2". */
  864. queue->__user_part.__receive_credit_interval = 4;
  865. queue->__user_part.__receive_credit_remaining =
  866. queue->__user_part.__receive_credit_interval;
  867. /*
  868. * Get a fastio index from the hypervisor.
  869. * ISSUE: Shouldn't this check the result?
  870. */
  871. ret = hv_dev_pread(priv->hv_devhdl, 0,
  872. (HV_VirtAddr)&queue->__user_part.__fastio_index,
  873. sizeof(queue->__user_part.__fastio_index),
  874. NETIO_IPP_GET_FASTIO_OFF);
  875. PDEBUG("hv_dev_pread(NETIO_IPP_GET_FASTIO_OFF) returned %d\n", ret);
  876. /* Now we are registered. */
  877. info->registered = true;
  878. }
  879. /*
  880. * Deregister with hypervisor on the current CPU.
  881. *
  882. * This simply discards all our credits, so no more packets will be
  883. * delivered to this tile. There may still be packets in our queue.
  884. *
  885. * Also, disable the ingress interrupt.
  886. */
  887. static void tile_net_deregister(void *dev_ptr)
  888. {
  889. struct net_device *dev = (struct net_device *)dev_ptr;
  890. struct tile_net_priv *priv = netdev_priv(dev);
  891. int my_cpu = smp_processor_id();
  892. struct tile_net_cpu *info = priv->cpu[my_cpu];
  893. /* Disable the ingress interrupt. */
  894. disable_percpu_irq(priv->intr_id);
  895. /* Do nothing else if not registered. */
  896. if (info == NULL || !info->registered)
  897. return;
  898. {
  899. struct tile_netio_queue *queue = &info->queue;
  900. netio_queue_user_impl_t *qup = &queue->__user_part;
  901. /* Discard all our credits. */
  902. __netio_fastio_return_credits(qup->__fastio_index, -1);
  903. }
  904. }
  905. /*
  906. * Unregister with hypervisor on the current CPU.
  907. *
  908. * Also, disable the ingress interrupt.
  909. */
  910. static void tile_net_unregister(void *dev_ptr)
  911. {
  912. struct net_device *dev = (struct net_device *)dev_ptr;
  913. struct tile_net_priv *priv = netdev_priv(dev);
  914. int my_cpu = smp_processor_id();
  915. struct tile_net_cpu *info = priv->cpu[my_cpu];
  916. int ret;
  917. int dummy = 0;
  918. /* Disable the ingress interrupt. */
  919. disable_percpu_irq(priv->intr_id);
  920. /* Do nothing else if not registered. */
  921. if (info == NULL || !info->registered)
  922. return;
  923. /* Unregister ourselves with LIPP/LEPP. */
  924. ret = hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  925. sizeof(dummy), NETIO_IPP_INPUT_UNREGISTER_OFF);
  926. if (ret < 0)
  927. panic("Failed to unregister with LIPP/LEPP!\n");
  928. /* Discard all packets still in our NetIO queue. */
  929. tile_net_discard_packets(dev);
  930. /* Reset state. */
  931. info->num_needed_small_buffers = 0;
  932. info->num_needed_large_buffers = 0;
  933. /* Cancel egress timer. */
  934. del_timer(&info->egress_timer);
  935. info->egress_timer_scheduled = false;
  936. }
  937. /*
  938. * Helper function for "tile_net_stop()".
  939. *
  940. * Also used to handle registration failure in "tile_net_open_inner()",
  941. * when the various extra steps in "tile_net_stop()" are not necessary.
  942. */
  943. static void tile_net_stop_aux(struct net_device *dev)
  944. {
  945. struct tile_net_priv *priv = netdev_priv(dev);
  946. int i;
  947. int dummy = 0;
  948. /*
  949. * Unregister all tiles, so LIPP will stop delivering packets.
  950. * Also, delete all the "napi" objects (sequentially, to protect
  951. * "dev->napi_list").
  952. */
  953. on_each_cpu(tile_net_unregister, (void *)dev, 1);
  954. for_each_online_cpu(i) {
  955. struct tile_net_cpu *info = priv->cpu[i];
  956. if (info != NULL && info->registered) {
  957. netif_napi_del(&info->napi);
  958. info->registered = false;
  959. }
  960. }
  961. /* Stop LIPP/LEPP. */
  962. if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  963. sizeof(dummy), NETIO_IPP_STOP_SHIM_OFF) < 0)
  964. panic("Failed to stop LIPP/LEPP!\n");
  965. priv->partly_opened = false;
  966. }
  967. /*
  968. * Disable NAPI for the given device on the current cpu.
  969. */
  970. static void tile_net_stop_disable(void *dev_ptr)
  971. {
  972. struct net_device *dev = (struct net_device *)dev_ptr;
  973. struct tile_net_priv *priv = netdev_priv(dev);
  974. int my_cpu = smp_processor_id();
  975. struct tile_net_cpu *info = priv->cpu[my_cpu];
  976. /* Disable NAPI if needed. */
  977. if (info != NULL && info->napi_enabled) {
  978. napi_disable(&info->napi);
  979. info->napi_enabled = false;
  980. }
  981. }
  982. /*
  983. * Enable NAPI and the ingress interrupt for the given device
  984. * on the current cpu.
  985. *
  986. * ISSUE: Only do this for "network cpus"?
  987. */
  988. static void tile_net_open_enable(void *dev_ptr)
  989. {
  990. struct net_device *dev = (struct net_device *)dev_ptr;
  991. struct tile_net_priv *priv = netdev_priv(dev);
  992. int my_cpu = smp_processor_id();
  993. struct tile_net_cpu *info = priv->cpu[my_cpu];
  994. /* Enable NAPI. */
  995. napi_enable(&info->napi);
  996. info->napi_enabled = true;
  997. /* Enable the ingress interrupt. */
  998. enable_percpu_irq(priv->intr_id, 0);
  999. }
  1000. /*
  1001. * tile_net_open_inner does most of the work of bringing up the interface.
  1002. * It's called from tile_net_open(), and also from tile_net_retry_open().
  1003. * The return value is 0 if the interface was brought up, < 0 if
  1004. * tile_net_open() should return the return value as an error, and > 0 if
  1005. * tile_net_open() should return success and schedule a work item to
  1006. * periodically retry the bringup.
  1007. */
  1008. static int tile_net_open_inner(struct net_device *dev)
  1009. {
  1010. struct tile_net_priv *priv = netdev_priv(dev);
  1011. int my_cpu = smp_processor_id();
  1012. struct tile_net_cpu *info;
  1013. struct tile_netio_queue *queue;
  1014. int result = 0;
  1015. int i;
  1016. int dummy = 0;
  1017. /*
  1018. * First try to register just on the local CPU, and handle any
  1019. * semi-expected "link down" failure specially. Note that we
  1020. * do NOT call "tile_net_stop_aux()", unlike below.
  1021. */
  1022. tile_net_register(dev);
  1023. info = priv->cpu[my_cpu];
  1024. if (!info->registered) {
  1025. if (info->link_down)
  1026. return 1;
  1027. return -EAGAIN;
  1028. }
  1029. /*
  1030. * Now register everywhere else. If any registration fails,
  1031. * even for "link down" (which might not be possible), we
  1032. * clean up using "tile_net_stop_aux()". Also, add all the
  1033. * "napi" objects (sequentially, to protect "dev->napi_list").
  1034. * ISSUE: Only use "netif_napi_add()" for "network cpus"?
  1035. */
  1036. smp_call_function(tile_net_register, (void *)dev, 1);
  1037. for_each_online_cpu(i) {
  1038. struct tile_net_cpu *info = priv->cpu[i];
  1039. if (info->registered)
  1040. netif_napi_add(dev, &info->napi, tile_net_poll, 64);
  1041. else
  1042. result = -EAGAIN;
  1043. }
  1044. if (result != 0) {
  1045. tile_net_stop_aux(dev);
  1046. return result;
  1047. }
  1048. queue = &info->queue;
  1049. if (priv->intr_id == 0) {
  1050. unsigned int irq;
  1051. /*
  1052. * Acquire the irq allocated by the hypervisor. Every
  1053. * queue gets the same irq. The "__intr_id" field is
  1054. * "1 << irq", so we use "__ffs()" to extract "irq".
  1055. */
  1056. priv->intr_id = queue->__system_part->__intr_id;
  1057. BUG_ON(priv->intr_id == 0);
  1058. irq = __ffs(priv->intr_id);
  1059. /*
  1060. * Register the ingress interrupt handler for this
  1061. * device, permanently.
  1062. *
  1063. * We used to call "free_irq()" in "tile_net_stop()",
  1064. * and then re-register the handler here every time,
  1065. * but that caused DNP errors in "handle_IRQ_event()"
  1066. * because "desc->action" was NULL. See bug 9143.
  1067. */
  1068. tile_irq_activate(irq, TILE_IRQ_PERCPU);
  1069. BUG_ON(request_irq(irq, tile_net_handle_ingress_interrupt,
  1070. 0, dev->name, (void *)dev) != 0);
  1071. }
  1072. {
  1073. /* Allocate initial buffers. */
  1074. int max_buffers =
  1075. priv->network_cpus_count * priv->network_cpus_credits;
  1076. info->num_needed_small_buffers =
  1077. min(LIPP_SMALL_BUFFERS, max_buffers);
  1078. info->num_needed_large_buffers =
  1079. min(LIPP_LARGE_BUFFERS, max_buffers);
  1080. tile_net_provide_needed_buffers(info);
  1081. if (info->num_needed_small_buffers != 0 ||
  1082. info->num_needed_large_buffers != 0)
  1083. panic("Insufficient memory for buffer stack!");
  1084. }
  1085. /* We are about to be active. */
  1086. priv->active = true;
  1087. /* Make sure "active" is visible to all tiles. */
  1088. mb();
  1089. /* On each tile, enable NAPI and the ingress interrupt. */
  1090. on_each_cpu(tile_net_open_enable, (void *)dev, 1);
  1091. /* Start LIPP/LEPP and activate "ingress" at the shim. */
  1092. if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  1093. sizeof(dummy), NETIO_IPP_INPUT_INIT_OFF) < 0)
  1094. panic("Failed to activate the LIPP Shim!\n");
  1095. /* Start our transmit queue. */
  1096. netif_start_queue(dev);
  1097. return 0;
  1098. }
  1099. /*
  1100. * Called periodically to retry bringing up the NetIO interface,
  1101. * if it doesn't come up cleanly during tile_net_open().
  1102. */
  1103. static void tile_net_open_retry(struct work_struct *w)
  1104. {
  1105. struct delayed_work *dw =
  1106. container_of(w, struct delayed_work, work);
  1107. struct tile_net_priv *priv =
  1108. container_of(dw, struct tile_net_priv, retry_work);
  1109. /*
  1110. * Try to bring the NetIO interface up. If it fails, reschedule
  1111. * ourselves to try again later; otherwise, tell Linux we now have
  1112. * a working link. ISSUE: What if the return value is negative?
  1113. */
  1114. if (tile_net_open_inner(priv->dev) != 0)
  1115. schedule_delayed_work(&priv->retry_work,
  1116. TILE_NET_RETRY_INTERVAL);
  1117. else
  1118. netif_carrier_on(priv->dev);
  1119. }
  1120. /*
  1121. * Called when a network interface is made active.
  1122. *
  1123. * Returns 0 on success, negative value on failure.
  1124. *
  1125. * The open entry point is called when a network interface is made
  1126. * active by the system (IFF_UP). At this point all resources needed
  1127. * for transmit and receive operations are allocated, the interrupt
  1128. * handler is registered with the OS (if needed), the watchdog timer
  1129. * is started, and the stack is notified that the interface is ready.
  1130. *
  1131. * If the actual link is not available yet, then we tell Linux that
  1132. * we have no carrier, and we keep checking until the link comes up.
  1133. */
  1134. static int tile_net_open(struct net_device *dev)
  1135. {
  1136. int ret = 0;
  1137. struct tile_net_priv *priv = netdev_priv(dev);
  1138. /*
  1139. * We rely on priv->partly_opened to tell us if this is the
  1140. * first time this interface is being brought up. If it is
  1141. * set, the IPP was already initialized and should not be
  1142. * initialized again.
  1143. */
  1144. if (!priv->partly_opened) {
  1145. int count;
  1146. int credits;
  1147. /* Initialize LIPP/LEPP, and start the Shim. */
  1148. ret = tile_net_open_aux(dev);
  1149. if (ret < 0) {
  1150. pr_err("tile_net_open_aux failed: %d\n", ret);
  1151. return ret;
  1152. }
  1153. /* Analyze the network cpus. */
  1154. if (network_cpus_used)
  1155. cpumask_copy(&priv->network_cpus_map,
  1156. &network_cpus_map);
  1157. else
  1158. cpumask_copy(&priv->network_cpus_map, cpu_online_mask);
  1159. count = cpumask_weight(&priv->network_cpus_map);
  1160. /* Limit credits to available buffers, and apply min. */
  1161. credits = max(16, (LIPP_LARGE_BUFFERS / count) & ~1);
  1162. /* Apply "GBE" max limit. */
  1163. /* ISSUE: Use higher limit for XGBE? */
  1164. credits = min(NETIO_MAX_RECEIVE_PKTS, credits);
  1165. priv->network_cpus_count = count;
  1166. priv->network_cpus_credits = credits;
  1167. #ifdef TILE_NET_DEBUG
  1168. pr_info("Using %d network cpus, with %d credits each\n",
  1169. priv->network_cpus_count, priv->network_cpus_credits);
  1170. #endif
  1171. priv->partly_opened = true;
  1172. } else {
  1173. /* FIXME: Is this possible? */
  1174. /* printk("Already partly opened.\n"); */
  1175. }
  1176. /*
  1177. * Attempt to bring up the link.
  1178. */
  1179. ret = tile_net_open_inner(dev);
  1180. if (ret <= 0) {
  1181. if (ret == 0)
  1182. netif_carrier_on(dev);
  1183. return ret;
  1184. }
  1185. /*
  1186. * We were unable to bring up the NetIO interface, but we want to
  1187. * try again in a little bit. Tell Linux that we have no carrier
  1188. * so it doesn't try to use the interface before the link comes up
  1189. * and then remember to try again later.
  1190. */
  1191. netif_carrier_off(dev);
  1192. schedule_delayed_work(&priv->retry_work, TILE_NET_RETRY_INTERVAL);
  1193. return 0;
  1194. }
  1195. static int tile_net_drain_lipp_buffers(struct tile_net_priv *priv)
  1196. {
  1197. int n = 0;
  1198. /* Drain all the LIPP buffers. */
  1199. while (true) {
  1200. unsigned int buffer;
  1201. /* NOTE: This should never fail. */
  1202. if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&buffer,
  1203. sizeof(buffer), NETIO_IPP_DRAIN_OFF) < 0)
  1204. break;
  1205. /* Stop when done. */
  1206. if (buffer == 0)
  1207. break;
  1208. {
  1209. /* Convert "linux_buffer_t" to "va". */
  1210. void *va = __va((phys_addr_t)(buffer >> 1) << 7);
  1211. /* Acquire the associated "skb". */
  1212. struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
  1213. struct sk_buff *skb = *skb_ptr;
  1214. kfree_skb(skb);
  1215. }
  1216. n++;
  1217. }
  1218. return n;
  1219. }
  1220. /*
  1221. * Disables a network interface.
  1222. *
  1223. * Returns 0, this is not allowed to fail.
  1224. *
  1225. * The close entry point is called when an interface is de-activated
  1226. * by the OS. The hardware is still under the drivers control, but
  1227. * needs to be disabled. A global MAC reset is issued to stop the
  1228. * hardware, and all transmit and receive resources are freed.
  1229. *
  1230. * ISSUE: How closely does "netif_running(dev)" mirror "priv->active"?
  1231. *
  1232. * Before we are called by "__dev_close()", "netif_running()" will
  1233. * have been cleared, so no NEW calls to "tile_net_poll()" will be
  1234. * made by "netpoll_poll_dev()".
  1235. *
  1236. * Often, this can cause some tiles to still have packets in their
  1237. * queues, so we must call "tile_net_discard_packets()" later.
  1238. *
  1239. * Note that some other tile may still be INSIDE "tile_net_poll()",
  1240. * and in fact, many will be, if there is heavy network load.
  1241. *
  1242. * Calling "on_each_cpu(tile_net_stop_disable, (void *)dev, 1)" when
  1243. * any tile is still "napi_schedule()"'d will induce a horrible crash
  1244. * when "msleep()" is called. This includes tiles which are inside
  1245. * "tile_net_poll()" which have not yet called "napi_complete()".
  1246. *
  1247. * So, we must first try to wait long enough for other tiles to finish
  1248. * with any current "tile_net_poll()" call, and, hopefully, to clear
  1249. * the "scheduled" flag. ISSUE: It is unclear what happens to tiles
  1250. * which have called "napi_schedule()" but which had not yet tried to
  1251. * call "tile_net_poll()", or which exhausted their budget inside
  1252. * "tile_net_poll()" just before this function was called.
  1253. */
  1254. static int tile_net_stop(struct net_device *dev)
  1255. {
  1256. struct tile_net_priv *priv = netdev_priv(dev);
  1257. PDEBUG("tile_net_stop()\n");
  1258. /* Start discarding packets. */
  1259. priv->active = false;
  1260. /* Make sure "active" is visible to all tiles. */
  1261. mb();
  1262. /*
  1263. * On each tile, make sure no NEW packets get delivered, and
  1264. * disable the ingress interrupt.
  1265. *
  1266. * Note that the ingress interrupt can fire AFTER this,
  1267. * presumably due to packets which were recently delivered,
  1268. * but it will have no effect.
  1269. */
  1270. on_each_cpu(tile_net_deregister, (void *)dev, 1);
  1271. /* Optimistically drain LIPP buffers. */
  1272. (void)tile_net_drain_lipp_buffers(priv);
  1273. /* ISSUE: Only needed if not yet fully open. */
  1274. cancel_delayed_work_sync(&priv->retry_work);
  1275. /* Can't transmit any more. */
  1276. netif_stop_queue(dev);
  1277. /* Disable NAPI on each tile. */
  1278. on_each_cpu(tile_net_stop_disable, (void *)dev, 1);
  1279. /*
  1280. * Drain any remaining LIPP buffers. NOTE: This "printk()"
  1281. * has never been observed, but in theory it could happen.
  1282. */
  1283. if (tile_net_drain_lipp_buffers(priv) != 0)
  1284. printk("Had to drain some extra LIPP buffers!\n");
  1285. /* Stop LIPP/LEPP. */
  1286. tile_net_stop_aux(dev);
  1287. /*
  1288. * ISSUE: It appears that, in practice anyway, by the time we
  1289. * get here, there are no pending completions, but just in case,
  1290. * we free (all of) them anyway.
  1291. */
  1292. while (tile_net_lepp_free_comps(dev, true))
  1293. /* loop */;
  1294. /* Wipe the EPP queue, and wait till the stores hit the EPP. */
  1295. memset(priv->eq, 0, sizeof(lepp_queue_t));
  1296. mb();
  1297. return 0;
  1298. }
  1299. /*
  1300. * Prepare the "frags" info for the resulting LEPP command.
  1301. *
  1302. * If needed, flush the memory used by the frags.
  1303. */
  1304. static unsigned int tile_net_tx_frags(lepp_frag_t *frags,
  1305. struct sk_buff *skb,
  1306. void *b_data, unsigned int b_len)
  1307. {
  1308. unsigned int i, n = 0;
  1309. struct skb_shared_info *sh = skb_shinfo(skb);
  1310. phys_addr_t cpa;
  1311. if (b_len != 0) {
  1312. if (!hash_default)
  1313. finv_buffer_remote(b_data, b_len, 0);
  1314. cpa = __pa(b_data);
  1315. frags[n].cpa_lo = cpa;
  1316. frags[n].cpa_hi = cpa >> 32;
  1317. frags[n].length = b_len;
  1318. frags[n].hash_for_home = hash_default;
  1319. n++;
  1320. }
  1321. for (i = 0; i < sh->nr_frags; i++) {
  1322. skb_frag_t *f = &sh->frags[i];
  1323. unsigned long pfn = page_to_pfn(skb_frag_page(f));
  1324. /* FIXME: Compute "hash_for_home" properly. */
  1325. /* ISSUE: The hypervisor checks CHIP_HAS_REV1_DMA_PACKETS(). */
  1326. int hash_for_home = hash_default;
  1327. /* FIXME: Hmmm. */
  1328. if (!hash_default) {
  1329. void *va = pfn_to_kaddr(pfn) + f->page_offset;
  1330. BUG_ON(PageHighMem(skb_frag_page(f)));
  1331. finv_buffer_remote(va, skb_frag_size(f), 0);
  1332. }
  1333. cpa = ((phys_addr_t)pfn << PAGE_SHIFT) + f->page_offset;
  1334. frags[n].cpa_lo = cpa;
  1335. frags[n].cpa_hi = cpa >> 32;
  1336. frags[n].length = skb_frag_size(f);
  1337. frags[n].hash_for_home = hash_for_home;
  1338. n++;
  1339. }
  1340. return n;
  1341. }
  1342. /*
  1343. * This function takes "skb", consisting of a header template and a
  1344. * payload, and hands it to LEPP, to emit as one or more segments,
  1345. * each consisting of a possibly modified header, plus a piece of the
  1346. * payload, via a process known as "tcp segmentation offload".
  1347. *
  1348. * Usually, "data" will contain the header template, of size "sh_len",
  1349. * and "sh->frags" will contain "skb->data_len" bytes of payload, and
  1350. * there will be "sh->gso_segs" segments.
  1351. *
  1352. * Sometimes, if "sendfile()" requires copying, we will be called with
  1353. * "data" containing the header and payload, with "frags" being empty.
  1354. *
  1355. * Sometimes, for example when using NFS over TCP, a single segment can
  1356. * span 3 fragments, which must be handled carefully in LEPP.
  1357. *
  1358. * See "emulate_large_send_offload()" for some reference code, which
  1359. * does not handle checksumming.
  1360. *
  1361. * ISSUE: How do we make sure that high memory DMA does not migrate?
  1362. */
  1363. static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
  1364. {
  1365. struct tile_net_priv *priv = netdev_priv(dev);
  1366. int my_cpu = smp_processor_id();
  1367. struct tile_net_cpu *info = priv->cpu[my_cpu];
  1368. struct tile_net_stats_t *stats = &info->stats;
  1369. struct skb_shared_info *sh = skb_shinfo(skb);
  1370. unsigned char *data = skb->data;
  1371. /* The ip header follows the ethernet header. */
  1372. struct iphdr *ih = ip_hdr(skb);
  1373. unsigned int ih_len = ih->ihl * 4;
  1374. /* Note that "nh == ih", by definition. */
  1375. unsigned char *nh = skb_network_header(skb);
  1376. unsigned int eh_len = nh - data;
  1377. /* The tcp header follows the ip header. */
  1378. struct tcphdr *th = (struct tcphdr *)(nh + ih_len);
  1379. unsigned int th_len = th->doff * 4;
  1380. /* The total number of header bytes. */
  1381. /* NOTE: This may be less than skb_headlen(skb). */
  1382. unsigned int sh_len = eh_len + ih_len + th_len;
  1383. /* The number of payload bytes at "skb->data + sh_len". */
  1384. /* This is non-zero for sendfile() without HIGHDMA. */
  1385. unsigned int b_len = skb_headlen(skb) - sh_len;
  1386. /* The total number of payload bytes. */
  1387. unsigned int d_len = b_len + skb->data_len;
  1388. /* The maximum payload size. */
  1389. unsigned int p_len = sh->gso_size;
  1390. /* The total number of segments. */
  1391. unsigned int num_segs = sh->gso_segs;
  1392. /* The temporary copy of the command. */
  1393. u32 cmd_body[(LEPP_MAX_CMD_SIZE + 3) / 4];
  1394. lepp_tso_cmd_t *cmd = (lepp_tso_cmd_t *)cmd_body;
  1395. /* Analyze the "frags". */
  1396. unsigned int num_frags =
  1397. tile_net_tx_frags(cmd->frags, skb, data + sh_len, b_len);
  1398. /* The size of the command, including frags and header. */
  1399. size_t cmd_size = LEPP_TSO_CMD_SIZE(num_frags, sh_len);
  1400. /* The command header. */
  1401. lepp_tso_cmd_t cmd_init = {
  1402. .tso = true,
  1403. .header_size = sh_len,
  1404. .ip_offset = eh_len,
  1405. .tcp_offset = eh_len + ih_len,
  1406. .payload_size = p_len,
  1407. .num_frags = num_frags,
  1408. };
  1409. unsigned long irqflags;
  1410. lepp_queue_t *eq = priv->eq;
  1411. struct sk_buff *olds[8];
  1412. unsigned int wanted = 8;
  1413. unsigned int i, nolds = 0;
  1414. unsigned int cmd_head, cmd_tail, cmd_next;
  1415. unsigned int comp_tail;
  1416. /* Paranoia. */
  1417. BUG_ON(skb->protocol != htons(ETH_P_IP));
  1418. BUG_ON(ih->protocol != IPPROTO_TCP);
  1419. BUG_ON(skb->ip_summed != CHECKSUM_PARTIAL);
  1420. BUG_ON(num_frags > LEPP_MAX_FRAGS);
  1421. /*--BUG_ON(num_segs != (d_len + (p_len - 1)) / p_len); */
  1422. BUG_ON(num_segs <= 1);
  1423. /* Finish preparing the command. */
  1424. /* Copy the command header. */
  1425. *cmd = cmd_init;
  1426. /* Copy the "header". */
  1427. memcpy(&cmd->frags[num_frags], data, sh_len);
  1428. /* Prefetch and wait, to minimize time spent holding the spinlock. */
  1429. prefetch_L1(&eq->comp_tail);
  1430. prefetch_L1(&eq->cmd_tail);
  1431. mb();
  1432. /* Enqueue the command. */
  1433. spin_lock_irqsave(&priv->eq_lock, irqflags);
  1434. /* Handle completions if needed to make room. */
  1435. /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
  1436. if (lepp_num_free_comp_slots(eq) == 0) {
  1437. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
  1438. if (nolds == 0) {
  1439. busy:
  1440. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1441. return NETDEV_TX_BUSY;
  1442. }
  1443. }
  1444. cmd_head = eq->cmd_head;
  1445. cmd_tail = eq->cmd_tail;
  1446. /* Prepare to advance, detecting full queue. */
  1447. /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
  1448. cmd_next = cmd_tail + cmd_size;
  1449. if (cmd_tail < cmd_head && cmd_next >= cmd_head)
  1450. goto busy;
  1451. if (cmd_next > LEPP_CMD_LIMIT) {
  1452. cmd_next = 0;
  1453. if (cmd_next == cmd_head)
  1454. goto busy;
  1455. }
  1456. /* Copy the command. */
  1457. memcpy(&eq->cmds[cmd_tail], cmd, cmd_size);
  1458. /* Advance. */
  1459. cmd_tail = cmd_next;
  1460. /* Record "skb" for eventual freeing. */
  1461. comp_tail = eq->comp_tail;
  1462. eq->comps[comp_tail] = skb;
  1463. LEPP_QINC(comp_tail);
  1464. eq->comp_tail = comp_tail;
  1465. /* Flush before allowing LEPP to handle the command. */
  1466. /* ISSUE: Is this the optimal location for the flush? */
  1467. __insn_mf();
  1468. eq->cmd_tail = cmd_tail;
  1469. /* NOTE: Using "4" here is more efficient than "0" or "2", */
  1470. /* and, strangely, more efficient than pre-checking the number */
  1471. /* of available completions, and comparing it to 4. */
  1472. if (nolds == 0)
  1473. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
  1474. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1475. /* Handle completions. */
  1476. for (i = 0; i < nolds; i++)
  1477. dev_consume_skb_any(olds[i]);
  1478. /* Update stats. */
  1479. u64_stats_update_begin(&stats->syncp);
  1480. stats->tx_packets += num_segs;
  1481. stats->tx_bytes += (num_segs * sh_len) + d_len;
  1482. u64_stats_update_end(&stats->syncp);
  1483. /* Make sure the egress timer is scheduled. */
  1484. tile_net_schedule_egress_timer(info);
  1485. return NETDEV_TX_OK;
  1486. }
  1487. /*
  1488. * Transmit a packet (called by the kernel via "hard_start_xmit" hook).
  1489. */
  1490. static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
  1491. {
  1492. struct tile_net_priv *priv = netdev_priv(dev);
  1493. int my_cpu = smp_processor_id();
  1494. struct tile_net_cpu *info = priv->cpu[my_cpu];
  1495. struct tile_net_stats_t *stats = &info->stats;
  1496. unsigned long irqflags;
  1497. struct skb_shared_info *sh = skb_shinfo(skb);
  1498. unsigned int len = skb->len;
  1499. unsigned char *data = skb->data;
  1500. unsigned int csum_start = skb_checksum_start_offset(skb);
  1501. lepp_frag_t frags[1 + MAX_SKB_FRAGS];
  1502. unsigned int num_frags;
  1503. lepp_queue_t *eq = priv->eq;
  1504. struct sk_buff *olds[8];
  1505. unsigned int wanted = 8;
  1506. unsigned int i, nolds = 0;
  1507. unsigned int cmd_size = sizeof(lepp_cmd_t);
  1508. unsigned int cmd_head, cmd_tail, cmd_next;
  1509. unsigned int comp_tail;
  1510. lepp_cmd_t cmds[1 + MAX_SKB_FRAGS];
  1511. /*
  1512. * This is paranoia, since we think that if the link doesn't come
  1513. * up, telling Linux we have no carrier will keep it from trying
  1514. * to transmit. If it does, though, we can't execute this routine,
  1515. * since data structures we depend on aren't set up yet.
  1516. */
  1517. if (!info->registered)
  1518. return NETDEV_TX_BUSY;
  1519. /* Save the timestamp. */
  1520. dev->trans_start = jiffies;
  1521. #ifdef TILE_NET_PARANOIA
  1522. #if CHIP_HAS_CBOX_HOME_MAP()
  1523. if (hash_default) {
  1524. HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)data);
  1525. if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
  1526. panic("Non-HFH egress buffer! VA=%p Mode=%d PTE=%llx",
  1527. data, hv_pte_get_mode(pte), hv_pte_val(pte));
  1528. }
  1529. #endif
  1530. #endif
  1531. #ifdef TILE_NET_DUMP_PACKETS
  1532. /* ISSUE: Does not dump the "frags". */
  1533. dump_packet(data, skb_headlen(skb), "tx");
  1534. #endif /* TILE_NET_DUMP_PACKETS */
  1535. if (sh->gso_size != 0)
  1536. return tile_net_tx_tso(skb, dev);
  1537. /* Prepare the commands. */
  1538. num_frags = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
  1539. for (i = 0; i < num_frags; i++) {
  1540. bool final = (i == num_frags - 1);
  1541. lepp_cmd_t cmd = {
  1542. .cpa_lo = frags[i].cpa_lo,
  1543. .cpa_hi = frags[i].cpa_hi,
  1544. .length = frags[i].length,
  1545. .hash_for_home = frags[i].hash_for_home,
  1546. .send_completion = final,
  1547. .end_of_packet = final
  1548. };
  1549. if (i == 0 && skb->ip_summed == CHECKSUM_PARTIAL) {
  1550. cmd.compute_checksum = 1;
  1551. cmd.checksum_data.bits.start_byte = csum_start;
  1552. cmd.checksum_data.bits.count = len - csum_start;
  1553. cmd.checksum_data.bits.destination_byte =
  1554. csum_start + skb->csum_offset;
  1555. }
  1556. cmds[i] = cmd;
  1557. }
  1558. /* Prefetch and wait, to minimize time spent holding the spinlock. */
  1559. prefetch_L1(&eq->comp_tail);
  1560. prefetch_L1(&eq->cmd_tail);
  1561. mb();
  1562. /* Enqueue the commands. */
  1563. spin_lock_irqsave(&priv->eq_lock, irqflags);
  1564. /* Handle completions if needed to make room. */
  1565. /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
  1566. if (lepp_num_free_comp_slots(eq) == 0) {
  1567. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
  1568. if (nolds == 0) {
  1569. busy:
  1570. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1571. return NETDEV_TX_BUSY;
  1572. }
  1573. }
  1574. cmd_head = eq->cmd_head;
  1575. cmd_tail = eq->cmd_tail;
  1576. /* Copy the commands, or fail. */
  1577. /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
  1578. for (i = 0; i < num_frags; i++) {
  1579. /* Prepare to advance, detecting full queue. */
  1580. cmd_next = cmd_tail + cmd_size;
  1581. if (cmd_tail < cmd_head && cmd_next >= cmd_head)
  1582. goto busy;
  1583. if (cmd_next > LEPP_CMD_LIMIT) {
  1584. cmd_next = 0;
  1585. if (cmd_next == cmd_head)
  1586. goto busy;
  1587. }
  1588. /* Copy the command. */
  1589. *(lepp_cmd_t *)&eq->cmds[cmd_tail] = cmds[i];
  1590. /* Advance. */
  1591. cmd_tail = cmd_next;
  1592. }
  1593. /* Record "skb" for eventual freeing. */
  1594. comp_tail = eq->comp_tail;
  1595. eq->comps[comp_tail] = skb;
  1596. LEPP_QINC(comp_tail);
  1597. eq->comp_tail = comp_tail;
  1598. /* Flush before allowing LEPP to handle the command. */
  1599. /* ISSUE: Is this the optimal location for the flush? */
  1600. __insn_mf();
  1601. eq->cmd_tail = cmd_tail;
  1602. /* NOTE: Using "4" here is more efficient than "0" or "2", */
  1603. /* and, strangely, more efficient than pre-checking the number */
  1604. /* of available completions, and comparing it to 4. */
  1605. if (nolds == 0)
  1606. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
  1607. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1608. /* Handle completions. */
  1609. for (i = 0; i < nolds; i++)
  1610. dev_consume_skb_any(olds[i]);
  1611. /* HACK: Track "expanded" size for short packets (e.g. 42 < 60). */
  1612. u64_stats_update_begin(&stats->syncp);
  1613. stats->tx_packets++;
  1614. stats->tx_bytes += ((len >= ETH_ZLEN) ? len : ETH_ZLEN);
  1615. u64_stats_update_end(&stats->syncp);
  1616. /* Make sure the egress timer is scheduled. */
  1617. tile_net_schedule_egress_timer(info);
  1618. return NETDEV_TX_OK;
  1619. }
  1620. /*
  1621. * Deal with a transmit timeout.
  1622. */
  1623. static void tile_net_tx_timeout(struct net_device *dev)
  1624. {
  1625. PDEBUG("tile_net_tx_timeout()\n");
  1626. PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
  1627. jiffies - dev->trans_start);
  1628. /* XXX: ISSUE: This doesn't seem useful for us. */
  1629. netif_wake_queue(dev);
  1630. }
  1631. /*
  1632. * Ioctl commands.
  1633. */
  1634. static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1635. {
  1636. return -EOPNOTSUPP;
  1637. }
  1638. /*
  1639. * Get System Network Statistics.
  1640. *
  1641. * Returns the address of the device statistics structure.
  1642. */
  1643. static struct rtnl_link_stats64 *tile_net_get_stats64(struct net_device *dev,
  1644. struct rtnl_link_stats64 *stats)
  1645. {
  1646. struct tile_net_priv *priv = netdev_priv(dev);
  1647. u64 rx_packets = 0, tx_packets = 0;
  1648. u64 rx_bytes = 0, tx_bytes = 0;
  1649. u64 rx_errors = 0, rx_dropped = 0;
  1650. int i;
  1651. for_each_online_cpu(i) {
  1652. struct tile_net_stats_t *cpu_stats;
  1653. u64 trx_packets, ttx_packets, trx_bytes, ttx_bytes;
  1654. u64 trx_errors, trx_dropped;
  1655. unsigned int start;
  1656. if (priv->cpu[i] == NULL)
  1657. continue;
  1658. cpu_stats = &priv->cpu[i]->stats;
  1659. do {
  1660. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  1661. trx_packets = cpu_stats->rx_packets;
  1662. ttx_packets = cpu_stats->tx_packets;
  1663. trx_bytes = cpu_stats->rx_bytes;
  1664. ttx_bytes = cpu_stats->tx_bytes;
  1665. trx_errors = cpu_stats->rx_errors;
  1666. trx_dropped = cpu_stats->rx_dropped;
  1667. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  1668. rx_packets += trx_packets;
  1669. tx_packets += ttx_packets;
  1670. rx_bytes += trx_bytes;
  1671. tx_bytes += ttx_bytes;
  1672. rx_errors += trx_errors;
  1673. rx_dropped += trx_dropped;
  1674. }
  1675. stats->rx_packets = rx_packets;
  1676. stats->tx_packets = tx_packets;
  1677. stats->rx_bytes = rx_bytes;
  1678. stats->tx_bytes = tx_bytes;
  1679. stats->rx_errors = rx_errors;
  1680. stats->rx_dropped = rx_dropped;
  1681. return stats;
  1682. }
  1683. /*
  1684. * Change the "mtu".
  1685. *
  1686. * The "change_mtu" method is usually not needed.
  1687. * If you need it, it must be like this.
  1688. */
  1689. static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
  1690. {
  1691. PDEBUG("tile_net_change_mtu()\n");
  1692. /* Check ranges. */
  1693. if ((new_mtu < 68) || (new_mtu > 1500))
  1694. return -EINVAL;
  1695. /* Accept the value. */
  1696. dev->mtu = new_mtu;
  1697. return 0;
  1698. }
  1699. /*
  1700. * Change the Ethernet Address of the NIC.
  1701. *
  1702. * The hypervisor driver does not support changing MAC address. However,
  1703. * the IPP does not do anything with the MAC address, so the address which
  1704. * gets used on outgoing packets, and which is accepted on incoming packets,
  1705. * is completely up to the NetIO program or kernel driver which is actually
  1706. * handling them.
  1707. *
  1708. * Returns 0 on success, negative on failure.
  1709. */
  1710. static int tile_net_set_mac_address(struct net_device *dev, void *p)
  1711. {
  1712. struct sockaddr *addr = p;
  1713. if (!is_valid_ether_addr(addr->sa_data))
  1714. return -EADDRNOTAVAIL;
  1715. /* ISSUE: Note that "dev_addr" is now a pointer. */
  1716. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1717. return 0;
  1718. }
  1719. /*
  1720. * Obtain the MAC address from the hypervisor.
  1721. * This must be done before opening the device.
  1722. */
  1723. static int tile_net_get_mac(struct net_device *dev)
  1724. {
  1725. struct tile_net_priv *priv = netdev_priv(dev);
  1726. char hv_dev_name[32];
  1727. int len;
  1728. __netio_getset_offset_t offset = { .word = NETIO_IPP_PARAM_OFF };
  1729. int ret;
  1730. /* For example, "xgbe0". */
  1731. strcpy(hv_dev_name, dev->name);
  1732. len = strlen(hv_dev_name);
  1733. /* For example, "xgbe/0". */
  1734. hv_dev_name[len] = hv_dev_name[len - 1];
  1735. hv_dev_name[len - 1] = '/';
  1736. len++;
  1737. /* For example, "xgbe/0/native_hash". */
  1738. strcpy(hv_dev_name + len, hash_default ? "/native_hash" : "/native");
  1739. /* Get the hypervisor handle for this device. */
  1740. priv->hv_devhdl = hv_dev_open((HV_VirtAddr)hv_dev_name, 0);
  1741. PDEBUG("hv_dev_open(%s) returned %d %p\n",
  1742. hv_dev_name, priv->hv_devhdl, &priv->hv_devhdl);
  1743. if (priv->hv_devhdl < 0) {
  1744. if (priv->hv_devhdl == HV_ENODEV)
  1745. printk(KERN_DEBUG "Ignoring unconfigured device %s\n",
  1746. hv_dev_name);
  1747. else
  1748. printk(KERN_DEBUG "hv_dev_open(%s) returned %d\n",
  1749. hv_dev_name, priv->hv_devhdl);
  1750. return -1;
  1751. }
  1752. /*
  1753. * Read the hardware address from the hypervisor.
  1754. * ISSUE: Note that "dev_addr" is now a pointer.
  1755. */
  1756. offset.bits.class = NETIO_PARAM;
  1757. offset.bits.addr = NETIO_PARAM_MAC;
  1758. ret = hv_dev_pread(priv->hv_devhdl, 0,
  1759. (HV_VirtAddr)dev->dev_addr, dev->addr_len,
  1760. offset.word);
  1761. PDEBUG("hv_dev_pread(NETIO_PARAM_MAC) returned %d\n", ret);
  1762. if (ret <= 0) {
  1763. printk(KERN_DEBUG "hv_dev_pread(NETIO_PARAM_MAC) %s failed\n",
  1764. dev->name);
  1765. /*
  1766. * Since the device is configured by the hypervisor but we
  1767. * can't get its MAC address, we are most likely running
  1768. * the simulator, so let's generate a random MAC address.
  1769. */
  1770. eth_hw_addr_random(dev);
  1771. }
  1772. return 0;
  1773. }
  1774. #ifdef CONFIG_NET_POLL_CONTROLLER
  1775. /*
  1776. * Polling 'interrupt' - used by things like netconsole to send skbs
  1777. * without having to re-enable interrupts. It's not called while
  1778. * the interrupt routine is executing.
  1779. */
  1780. static void tile_net_netpoll(struct net_device *dev)
  1781. {
  1782. struct tile_net_priv *priv = netdev_priv(dev);
  1783. disable_percpu_irq(priv->intr_id);
  1784. tile_net_handle_ingress_interrupt(priv->intr_id, dev);
  1785. enable_percpu_irq(priv->intr_id, 0);
  1786. }
  1787. #endif
  1788. static const struct net_device_ops tile_net_ops = {
  1789. .ndo_open = tile_net_open,
  1790. .ndo_stop = tile_net_stop,
  1791. .ndo_start_xmit = tile_net_tx,
  1792. .ndo_do_ioctl = tile_net_ioctl,
  1793. .ndo_get_stats64 = tile_net_get_stats64,
  1794. .ndo_change_mtu = tile_net_change_mtu,
  1795. .ndo_tx_timeout = tile_net_tx_timeout,
  1796. .ndo_set_mac_address = tile_net_set_mac_address,
  1797. #ifdef CONFIG_NET_POLL_CONTROLLER
  1798. .ndo_poll_controller = tile_net_netpoll,
  1799. #endif
  1800. };
  1801. /*
  1802. * The setup function.
  1803. *
  1804. * This uses ether_setup() to assign various fields in dev, including
  1805. * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
  1806. */
  1807. static void tile_net_setup(struct net_device *dev)
  1808. {
  1809. netdev_features_t features = 0;
  1810. ether_setup(dev);
  1811. dev->netdev_ops = &tile_net_ops;
  1812. dev->watchdog_timeo = TILE_NET_TIMEOUT;
  1813. dev->tx_queue_len = TILE_NET_TX_QUEUE_LEN;
  1814. dev->mtu = TILE_NET_MTU;
  1815. features |= NETIF_F_HW_CSUM;
  1816. features |= NETIF_F_SG;
  1817. /* We support TSO iff the HV supports sufficient frags. */
  1818. if (LEPP_MAX_FRAGS >= 1 + MAX_SKB_FRAGS)
  1819. features |= NETIF_F_TSO;
  1820. /* We can't support HIGHDMA without hash_default, since we need
  1821. * to be able to finv() with a VA if we don't have hash_default.
  1822. */
  1823. if (hash_default)
  1824. features |= NETIF_F_HIGHDMA;
  1825. dev->hw_features |= features;
  1826. dev->vlan_features |= features;
  1827. dev->features |= features;
  1828. }
  1829. /*
  1830. * Allocate the device structure, register the device, and obtain the
  1831. * MAC address from the hypervisor.
  1832. */
  1833. static struct net_device *tile_net_dev_init(const char *name)
  1834. {
  1835. int ret;
  1836. struct net_device *dev;
  1837. struct tile_net_priv *priv;
  1838. /*
  1839. * Allocate the device structure. This allocates "priv", calls
  1840. * tile_net_setup(), and saves "name". Normally, "name" is a
  1841. * template, instantiated by register_netdev(), but not for us.
  1842. */
  1843. dev = alloc_netdev(sizeof(*priv), name, NET_NAME_UNKNOWN,
  1844. tile_net_setup);
  1845. if (!dev) {
  1846. pr_err("alloc_netdev(%s) failed\n", name);
  1847. return NULL;
  1848. }
  1849. priv = netdev_priv(dev);
  1850. /* Initialize "priv". */
  1851. memset(priv, 0, sizeof(*priv));
  1852. /* Save "dev" for "tile_net_open_retry()". */
  1853. priv->dev = dev;
  1854. INIT_DELAYED_WORK(&priv->retry_work, tile_net_open_retry);
  1855. spin_lock_init(&priv->eq_lock);
  1856. /* Allocate "eq". */
  1857. priv->eq_pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, EQ_ORDER);
  1858. if (!priv->eq_pages) {
  1859. free_netdev(dev);
  1860. return NULL;
  1861. }
  1862. priv->eq = page_address(priv->eq_pages);
  1863. /* Register the network device. */
  1864. ret = register_netdev(dev);
  1865. if (ret) {
  1866. pr_err("register_netdev %s failed %d\n", dev->name, ret);
  1867. __free_pages(priv->eq_pages, EQ_ORDER);
  1868. free_netdev(dev);
  1869. return NULL;
  1870. }
  1871. /* Get the MAC address. */
  1872. ret = tile_net_get_mac(dev);
  1873. if (ret < 0) {
  1874. unregister_netdev(dev);
  1875. __free_pages(priv->eq_pages, EQ_ORDER);
  1876. free_netdev(dev);
  1877. return NULL;
  1878. }
  1879. return dev;
  1880. }
  1881. /*
  1882. * Module cleanup.
  1883. *
  1884. * FIXME: If compiled as a module, this module cannot be "unloaded",
  1885. * because the "ingress interrupt handler" is registered permanently.
  1886. */
  1887. static void tile_net_cleanup(void)
  1888. {
  1889. int i;
  1890. for (i = 0; i < TILE_NET_DEVS; i++) {
  1891. if (tile_net_devs[i]) {
  1892. struct net_device *dev = tile_net_devs[i];
  1893. struct tile_net_priv *priv = netdev_priv(dev);
  1894. unregister_netdev(dev);
  1895. finv_buffer_remote(priv->eq, EQ_SIZE, 0);
  1896. __free_pages(priv->eq_pages, EQ_ORDER);
  1897. free_netdev(dev);
  1898. }
  1899. }
  1900. }
  1901. /*
  1902. * Module initialization.
  1903. */
  1904. static int tile_net_init_module(void)
  1905. {
  1906. pr_info("Tilera Network Driver\n");
  1907. tile_net_devs[0] = tile_net_dev_init("xgbe0");
  1908. tile_net_devs[1] = tile_net_dev_init("xgbe1");
  1909. tile_net_devs[2] = tile_net_dev_init("gbe0");
  1910. tile_net_devs[3] = tile_net_dev_init("gbe1");
  1911. return 0;
  1912. }
  1913. module_init(tile_net_init_module);
  1914. module_exit(tile_net_cleanup);
  1915. #ifndef MODULE
  1916. /*
  1917. * The "network_cpus" boot argument specifies the cpus that are dedicated
  1918. * to handle ingress packets.
  1919. *
  1920. * The parameter should be in the form "network_cpus=m-n[,x-y]", where
  1921. * m, n, x, y are integer numbers that represent the cpus that can be
  1922. * neither a dedicated cpu nor a dataplane cpu.
  1923. */
  1924. static int __init network_cpus_setup(char *str)
  1925. {
  1926. int rc = cpulist_parse_crop(str, &network_cpus_map);
  1927. if (rc != 0) {
  1928. pr_warn("network_cpus=%s: malformed cpu list\n", str);
  1929. } else {
  1930. /* Remove dedicated cpus. */
  1931. cpumask_and(&network_cpus_map, &network_cpus_map,
  1932. cpu_possible_mask);
  1933. if (cpumask_empty(&network_cpus_map)) {
  1934. pr_warn("Ignoring network_cpus='%s'\n", str);
  1935. } else {
  1936. pr_info("Linux network CPUs: %*pbl\n",
  1937. cpumask_pr_args(&network_cpus_map));
  1938. network_cpus_used = true;
  1939. }
  1940. }
  1941. return 0;
  1942. }
  1943. __setup("network_cpus=", network_cpus_setup);
  1944. #endif