tilepro.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420
  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(&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. #ifdef TILE_NET_DUMP_PACKETS
  593. dump_packet(buf, len, "rx");
  594. #endif /* TILE_NET_DUMP_PACKETS */
  595. #ifdef TILE_NET_VERIFY_INGRESS
  596. if (pkt_status == NETIO_PKT_STATUS_OVERSIZE && len >= 64) {
  597. dump_packet(buf, len, "rx");
  598. panic("Unexpected OVERSIZE.");
  599. }
  600. #endif
  601. filter = 0;
  602. if (pkt_status == NETIO_PKT_STATUS_BAD) {
  603. /* Handle CRC error and hardware truncation. */
  604. filter = 2;
  605. } else if (!(dev->flags & IFF_UP)) {
  606. /* Filter packets received before we're up. */
  607. filter = 1;
  608. } else if (NETIO_PKT_ETHERTYPE_RECOGNIZED_M(metadata, pkt) &&
  609. pkt_status == NETIO_PKT_STATUS_UNDERSIZE) {
  610. /* Filter "truncated" packets. */
  611. filter = 2;
  612. } else if (!(dev->flags & IFF_PROMISC)) {
  613. if (!is_multicast_ether_addr(buf)) {
  614. /* Filter packets not for our address. */
  615. const u8 *mine = dev->dev_addr;
  616. filter = !ether_addr_equal(mine, buf);
  617. }
  618. }
  619. u64_stats_update_begin(&stats->syncp);
  620. if (filter != 0) {
  621. if (filter == 1)
  622. stats->rx_dropped++;
  623. else
  624. stats->rx_errors++;
  625. tile_net_provide_linux_buffer(info, va, small);
  626. } else {
  627. /* Acquire the associated "skb". */
  628. struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
  629. struct sk_buff *skb = *skb_ptr;
  630. /* Paranoia. */
  631. if (skb->data != buf)
  632. panic("Corrupt linux buffer from LIPP! "
  633. "VA=%p, skb=%p, skb->data=%p\n",
  634. va, skb, skb->data);
  635. /* Encode the actual packet length. */
  636. skb_put(skb, len);
  637. /* NOTE: This call also sets "skb->dev = dev". */
  638. skb->protocol = eth_type_trans(skb, dev);
  639. /* Avoid recomputing "good" TCP/UDP checksums. */
  640. if (NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt))
  641. skb->ip_summed = CHECKSUM_UNNECESSARY;
  642. netif_receive_skb(skb);
  643. stats->rx_packets++;
  644. stats->rx_bytes += len;
  645. }
  646. u64_stats_update_end(&stats->syncp);
  647. /* ISSUE: It would be nice to defer this until the packet has */
  648. /* actually been processed. */
  649. tile_net_return_credit(info);
  650. /* Consume this packet. */
  651. qup->__packet_receive_read = index2;
  652. return !filter;
  653. }
  654. /*
  655. * Handle some packets for the given device on the current CPU.
  656. *
  657. * If "tile_net_stop()" is called on some other tile while this
  658. * function is running, we will return, hopefully before that
  659. * other tile asks us to call "napi_disable()".
  660. *
  661. * The "rotting packet" race condition occurs if a packet arrives
  662. * during the extremely narrow window between the queue appearing to
  663. * be empty, and the ingress interrupt being re-enabled. This happens
  664. * a LOT under heavy network load.
  665. */
  666. static int tile_net_poll(struct napi_struct *napi, int budget)
  667. {
  668. struct net_device *dev = napi->dev;
  669. struct tile_net_priv *priv = netdev_priv(dev);
  670. int my_cpu = smp_processor_id();
  671. struct tile_net_cpu *info = priv->cpu[my_cpu];
  672. struct tile_netio_queue *queue = &info->queue;
  673. netio_queue_impl_t *qsp = queue->__system_part;
  674. netio_queue_user_impl_t *qup = &queue->__user_part;
  675. unsigned int work = 0;
  676. if (budget <= 0)
  677. goto done;
  678. while (priv->active) {
  679. int index = qup->__packet_receive_read;
  680. if (index == qsp->__packet_receive_queue.__packet_write)
  681. break;
  682. if (tile_net_poll_aux(info, index)) {
  683. if (++work >= budget)
  684. goto done;
  685. }
  686. }
  687. napi_complete(&info->napi);
  688. if (!priv->active)
  689. goto done;
  690. /* Re-enable the ingress interrupt. */
  691. enable_percpu_irq(priv->intr_id, 0);
  692. /* HACK: Avoid the "rotting packet" problem (see above). */
  693. if (qup->__packet_receive_read !=
  694. qsp->__packet_receive_queue.__packet_write) {
  695. /* ISSUE: Sometimes this returns zero, presumably */
  696. /* because an interrupt was handled for this tile. */
  697. (void)napi_reschedule(&info->napi);
  698. }
  699. done:
  700. if (priv->active)
  701. tile_net_provide_needed_buffers(info);
  702. return work;
  703. }
  704. /*
  705. * Handle an ingress interrupt for the given device on the current cpu.
  706. *
  707. * ISSUE: Sometimes this gets called after "disable_percpu_irq()" has
  708. * been called! This is probably due to "pending hypervisor downcalls".
  709. *
  710. * ISSUE: Is there any race condition between the "napi_schedule()" here
  711. * and the "napi_complete()" call above?
  712. */
  713. static irqreturn_t tile_net_handle_ingress_interrupt(int irq, void *dev_ptr)
  714. {
  715. struct net_device *dev = (struct net_device *)dev_ptr;
  716. struct tile_net_priv *priv = netdev_priv(dev);
  717. int my_cpu = smp_processor_id();
  718. struct tile_net_cpu *info = priv->cpu[my_cpu];
  719. /* Disable the ingress interrupt. */
  720. disable_percpu_irq(priv->intr_id);
  721. /* Ignore unwanted interrupts. */
  722. if (!priv->active)
  723. return IRQ_HANDLED;
  724. /* ISSUE: Sometimes "info->napi_enabled" is false here. */
  725. napi_schedule(&info->napi);
  726. return IRQ_HANDLED;
  727. }
  728. /*
  729. * One time initialization per interface.
  730. */
  731. static int tile_net_open_aux(struct net_device *dev)
  732. {
  733. struct tile_net_priv *priv = netdev_priv(dev);
  734. int ret;
  735. int dummy;
  736. unsigned int epp_lotar;
  737. /*
  738. * Find out where EPP memory should be homed.
  739. */
  740. ret = hv_dev_pread(priv->hv_devhdl, 0,
  741. (HV_VirtAddr)&epp_lotar, sizeof(epp_lotar),
  742. NETIO_EPP_SHM_OFF);
  743. if (ret < 0) {
  744. pr_err("could not read epp_shm_queue lotar.\n");
  745. return -EIO;
  746. }
  747. /*
  748. * Home the page on the EPP.
  749. */
  750. {
  751. int epp_home = hv_lotar_to_cpu(epp_lotar);
  752. homecache_change_page_home(priv->eq_pages, EQ_ORDER, epp_home);
  753. }
  754. /*
  755. * Register the EPP shared memory queue.
  756. */
  757. {
  758. netio_ipp_address_t ea = {
  759. .va = 0,
  760. .pa = __pa(priv->eq),
  761. .pte = hv_pte(0),
  762. .size = EQ_SIZE,
  763. };
  764. ea.pte = hv_pte_set_lotar(ea.pte, epp_lotar);
  765. ea.pte = hv_pte_set_mode(ea.pte, HV_PTE_MODE_CACHE_TILE_L3);
  766. ret = hv_dev_pwrite(priv->hv_devhdl, 0,
  767. (HV_VirtAddr)&ea,
  768. sizeof(ea),
  769. NETIO_EPP_SHM_OFF);
  770. if (ret < 0)
  771. return -EIO;
  772. }
  773. /*
  774. * Start LIPP/LEPP.
  775. */
  776. if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  777. sizeof(dummy), NETIO_IPP_START_SHIM_OFF) < 0) {
  778. pr_warn("Failed to start LIPP/LEPP\n");
  779. return -EIO;
  780. }
  781. return 0;
  782. }
  783. /*
  784. * Register with hypervisor on the current CPU.
  785. *
  786. * Strangely, this function does important things even if it "fails",
  787. * which is especially common if the link is not up yet. Hopefully
  788. * these things are all "harmless" if done twice!
  789. */
  790. static void tile_net_register(void *dev_ptr)
  791. {
  792. struct net_device *dev = (struct net_device *)dev_ptr;
  793. struct tile_net_priv *priv = netdev_priv(dev);
  794. int my_cpu = smp_processor_id();
  795. struct tile_net_cpu *info;
  796. struct tile_netio_queue *queue;
  797. /* Only network cpus can receive packets. */
  798. int queue_id =
  799. cpumask_test_cpu(my_cpu, &priv->network_cpus_map) ? 0 : 255;
  800. netio_input_config_t config = {
  801. .flags = 0,
  802. .num_receive_packets = priv->network_cpus_credits,
  803. .queue_id = queue_id
  804. };
  805. int ret = 0;
  806. netio_queue_impl_t *queuep;
  807. PDEBUG("tile_net_register(queue_id %d)\n", queue_id);
  808. if (!strcmp(dev->name, "xgbe0"))
  809. info = this_cpu_ptr(&hv_xgbe0);
  810. else if (!strcmp(dev->name, "xgbe1"))
  811. info = this_cpu_ptr(&hv_xgbe1);
  812. else if (!strcmp(dev->name, "gbe0"))
  813. info = this_cpu_ptr(&hv_gbe0);
  814. else if (!strcmp(dev->name, "gbe1"))
  815. info = this_cpu_ptr(&hv_gbe1);
  816. else
  817. BUG();
  818. /* Initialize the egress timer. */
  819. init_timer_pinned(&info->egress_timer);
  820. info->egress_timer.data = (long)info;
  821. info->egress_timer.function = tile_net_handle_egress_timer;
  822. u64_stats_init(&info->stats.syncp);
  823. priv->cpu[my_cpu] = info;
  824. /*
  825. * Register ourselves with LIPP. This does a lot of stuff,
  826. * including invoking the LIPP registration code.
  827. */
  828. ret = hv_dev_pwrite(priv->hv_devhdl, 0,
  829. (HV_VirtAddr)&config,
  830. sizeof(netio_input_config_t),
  831. NETIO_IPP_INPUT_REGISTER_OFF);
  832. PDEBUG("hv_dev_pwrite(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
  833. ret);
  834. if (ret < 0) {
  835. if (ret != NETIO_LINK_DOWN) {
  836. printk(KERN_DEBUG "hv_dev_pwrite "
  837. "NETIO_IPP_INPUT_REGISTER_OFF failure %d\n",
  838. ret);
  839. }
  840. info->link_down = (ret == NETIO_LINK_DOWN);
  841. return;
  842. }
  843. /*
  844. * Get the pointer to our queue's system part.
  845. */
  846. ret = hv_dev_pread(priv->hv_devhdl, 0,
  847. (HV_VirtAddr)&queuep,
  848. sizeof(netio_queue_impl_t *),
  849. NETIO_IPP_INPUT_REGISTER_OFF);
  850. PDEBUG("hv_dev_pread(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
  851. ret);
  852. PDEBUG("queuep %p\n", queuep);
  853. if (ret <= 0) {
  854. /* ISSUE: Shouldn't this be a fatal error? */
  855. pr_err("hv_dev_pread NETIO_IPP_INPUT_REGISTER_OFF failure\n");
  856. return;
  857. }
  858. queue = &info->queue;
  859. queue->__system_part = queuep;
  860. memset(&queue->__user_part, 0, sizeof(netio_queue_user_impl_t));
  861. /* This is traditionally "config.num_receive_packets / 2". */
  862. queue->__user_part.__receive_credit_interval = 4;
  863. queue->__user_part.__receive_credit_remaining =
  864. queue->__user_part.__receive_credit_interval;
  865. /*
  866. * Get a fastio index from the hypervisor.
  867. * ISSUE: Shouldn't this check the result?
  868. */
  869. ret = hv_dev_pread(priv->hv_devhdl, 0,
  870. (HV_VirtAddr)&queue->__user_part.__fastio_index,
  871. sizeof(queue->__user_part.__fastio_index),
  872. NETIO_IPP_GET_FASTIO_OFF);
  873. PDEBUG("hv_dev_pread(NETIO_IPP_GET_FASTIO_OFF) returned %d\n", ret);
  874. /* Now we are registered. */
  875. info->registered = true;
  876. }
  877. /*
  878. * Deregister with hypervisor on the current CPU.
  879. *
  880. * This simply discards all our credits, so no more packets will be
  881. * delivered to this tile. There may still be packets in our queue.
  882. *
  883. * Also, disable the ingress interrupt.
  884. */
  885. static void tile_net_deregister(void *dev_ptr)
  886. {
  887. struct net_device *dev = (struct net_device *)dev_ptr;
  888. struct tile_net_priv *priv = netdev_priv(dev);
  889. int my_cpu = smp_processor_id();
  890. struct tile_net_cpu *info = priv->cpu[my_cpu];
  891. /* Disable the ingress interrupt. */
  892. disable_percpu_irq(priv->intr_id);
  893. /* Do nothing else if not registered. */
  894. if (info == NULL || !info->registered)
  895. return;
  896. {
  897. struct tile_netio_queue *queue = &info->queue;
  898. netio_queue_user_impl_t *qup = &queue->__user_part;
  899. /* Discard all our credits. */
  900. __netio_fastio_return_credits(qup->__fastio_index, -1);
  901. }
  902. }
  903. /*
  904. * Unregister with hypervisor on the current CPU.
  905. *
  906. * Also, disable the ingress interrupt.
  907. */
  908. static void tile_net_unregister(void *dev_ptr)
  909. {
  910. struct net_device *dev = (struct net_device *)dev_ptr;
  911. struct tile_net_priv *priv = netdev_priv(dev);
  912. int my_cpu = smp_processor_id();
  913. struct tile_net_cpu *info = priv->cpu[my_cpu];
  914. int ret;
  915. int dummy = 0;
  916. /* Disable the ingress interrupt. */
  917. disable_percpu_irq(priv->intr_id);
  918. /* Do nothing else if not registered. */
  919. if (info == NULL || !info->registered)
  920. return;
  921. /* Unregister ourselves with LIPP/LEPP. */
  922. ret = hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  923. sizeof(dummy), NETIO_IPP_INPUT_UNREGISTER_OFF);
  924. if (ret < 0)
  925. panic("Failed to unregister with LIPP/LEPP!\n");
  926. /* Discard all packets still in our NetIO queue. */
  927. tile_net_discard_packets(dev);
  928. /* Reset state. */
  929. info->num_needed_small_buffers = 0;
  930. info->num_needed_large_buffers = 0;
  931. /* Cancel egress timer. */
  932. del_timer(&info->egress_timer);
  933. info->egress_timer_scheduled = false;
  934. }
  935. /*
  936. * Helper function for "tile_net_stop()".
  937. *
  938. * Also used to handle registration failure in "tile_net_open_inner()",
  939. * when the various extra steps in "tile_net_stop()" are not necessary.
  940. */
  941. static void tile_net_stop_aux(struct net_device *dev)
  942. {
  943. struct tile_net_priv *priv = netdev_priv(dev);
  944. int i;
  945. int dummy = 0;
  946. /*
  947. * Unregister all tiles, so LIPP will stop delivering packets.
  948. * Also, delete all the "napi" objects (sequentially, to protect
  949. * "dev->napi_list").
  950. */
  951. on_each_cpu(tile_net_unregister, (void *)dev, 1);
  952. for_each_online_cpu(i) {
  953. struct tile_net_cpu *info = priv->cpu[i];
  954. if (info != NULL && info->registered) {
  955. netif_napi_del(&info->napi);
  956. info->registered = false;
  957. }
  958. }
  959. /* Stop LIPP/LEPP. */
  960. if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  961. sizeof(dummy), NETIO_IPP_STOP_SHIM_OFF) < 0)
  962. panic("Failed to stop LIPP/LEPP!\n");
  963. priv->partly_opened = false;
  964. }
  965. /*
  966. * Disable NAPI for the given device on the current cpu.
  967. */
  968. static void tile_net_stop_disable(void *dev_ptr)
  969. {
  970. struct net_device *dev = (struct net_device *)dev_ptr;
  971. struct tile_net_priv *priv = netdev_priv(dev);
  972. int my_cpu = smp_processor_id();
  973. struct tile_net_cpu *info = priv->cpu[my_cpu];
  974. /* Disable NAPI if needed. */
  975. if (info != NULL && info->napi_enabled) {
  976. napi_disable(&info->napi);
  977. info->napi_enabled = false;
  978. }
  979. }
  980. /*
  981. * Enable NAPI and the ingress interrupt for the given device
  982. * on the current cpu.
  983. *
  984. * ISSUE: Only do this for "network cpus"?
  985. */
  986. static void tile_net_open_enable(void *dev_ptr)
  987. {
  988. struct net_device *dev = (struct net_device *)dev_ptr;
  989. struct tile_net_priv *priv = netdev_priv(dev);
  990. int my_cpu = smp_processor_id();
  991. struct tile_net_cpu *info = priv->cpu[my_cpu];
  992. /* Enable NAPI. */
  993. napi_enable(&info->napi);
  994. info->napi_enabled = true;
  995. /* Enable the ingress interrupt. */
  996. enable_percpu_irq(priv->intr_id, 0);
  997. }
  998. /*
  999. * tile_net_open_inner does most of the work of bringing up the interface.
  1000. * It's called from tile_net_open(), and also from tile_net_retry_open().
  1001. * The return value is 0 if the interface was brought up, < 0 if
  1002. * tile_net_open() should return the return value as an error, and > 0 if
  1003. * tile_net_open() should return success and schedule a work item to
  1004. * periodically retry the bringup.
  1005. */
  1006. static int tile_net_open_inner(struct net_device *dev)
  1007. {
  1008. struct tile_net_priv *priv = netdev_priv(dev);
  1009. int my_cpu = smp_processor_id();
  1010. struct tile_net_cpu *info;
  1011. struct tile_netio_queue *queue;
  1012. int result = 0;
  1013. int i;
  1014. int dummy = 0;
  1015. /*
  1016. * First try to register just on the local CPU, and handle any
  1017. * semi-expected "link down" failure specially. Note that we
  1018. * do NOT call "tile_net_stop_aux()", unlike below.
  1019. */
  1020. tile_net_register(dev);
  1021. info = priv->cpu[my_cpu];
  1022. if (!info->registered) {
  1023. if (info->link_down)
  1024. return 1;
  1025. return -EAGAIN;
  1026. }
  1027. /*
  1028. * Now register everywhere else. If any registration fails,
  1029. * even for "link down" (which might not be possible), we
  1030. * clean up using "tile_net_stop_aux()". Also, add all the
  1031. * "napi" objects (sequentially, to protect "dev->napi_list").
  1032. * ISSUE: Only use "netif_napi_add()" for "network cpus"?
  1033. */
  1034. smp_call_function(tile_net_register, (void *)dev, 1);
  1035. for_each_online_cpu(i) {
  1036. struct tile_net_cpu *info = priv->cpu[i];
  1037. if (info->registered)
  1038. netif_napi_add(dev, &info->napi, tile_net_poll, 64);
  1039. else
  1040. result = -EAGAIN;
  1041. }
  1042. if (result != 0) {
  1043. tile_net_stop_aux(dev);
  1044. return result;
  1045. }
  1046. queue = &info->queue;
  1047. if (priv->intr_id == 0) {
  1048. unsigned int irq;
  1049. /*
  1050. * Acquire the irq allocated by the hypervisor. Every
  1051. * queue gets the same irq. The "__intr_id" field is
  1052. * "1 << irq", so we use "__ffs()" to extract "irq".
  1053. */
  1054. priv->intr_id = queue->__system_part->__intr_id;
  1055. BUG_ON(priv->intr_id == 0);
  1056. irq = __ffs(priv->intr_id);
  1057. /*
  1058. * Register the ingress interrupt handler for this
  1059. * device, permanently.
  1060. *
  1061. * We used to call "free_irq()" in "tile_net_stop()",
  1062. * and then re-register the handler here every time,
  1063. * but that caused DNP errors in "handle_IRQ_event()"
  1064. * because "desc->action" was NULL. See bug 9143.
  1065. */
  1066. tile_irq_activate(irq, TILE_IRQ_PERCPU);
  1067. BUG_ON(request_irq(irq, tile_net_handle_ingress_interrupt,
  1068. 0, dev->name, (void *)dev) != 0);
  1069. }
  1070. {
  1071. /* Allocate initial buffers. */
  1072. int max_buffers =
  1073. priv->network_cpus_count * priv->network_cpus_credits;
  1074. info->num_needed_small_buffers =
  1075. min(LIPP_SMALL_BUFFERS, max_buffers);
  1076. info->num_needed_large_buffers =
  1077. min(LIPP_LARGE_BUFFERS, max_buffers);
  1078. tile_net_provide_needed_buffers(info);
  1079. if (info->num_needed_small_buffers != 0 ||
  1080. info->num_needed_large_buffers != 0)
  1081. panic("Insufficient memory for buffer stack!");
  1082. }
  1083. /* We are about to be active. */
  1084. priv->active = true;
  1085. /* Make sure "active" is visible to all tiles. */
  1086. mb();
  1087. /* On each tile, enable NAPI and the ingress interrupt. */
  1088. on_each_cpu(tile_net_open_enable, (void *)dev, 1);
  1089. /* Start LIPP/LEPP and activate "ingress" at the shim. */
  1090. if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
  1091. sizeof(dummy), NETIO_IPP_INPUT_INIT_OFF) < 0)
  1092. panic("Failed to activate the LIPP Shim!\n");
  1093. /* Start our transmit queue. */
  1094. netif_start_queue(dev);
  1095. return 0;
  1096. }
  1097. /*
  1098. * Called periodically to retry bringing up the NetIO interface,
  1099. * if it doesn't come up cleanly during tile_net_open().
  1100. */
  1101. static void tile_net_open_retry(struct work_struct *w)
  1102. {
  1103. struct delayed_work *dw = to_delayed_work(w);
  1104. struct tile_net_priv *priv =
  1105. container_of(dw, struct tile_net_priv, retry_work);
  1106. /*
  1107. * Try to bring the NetIO interface up. If it fails, reschedule
  1108. * ourselves to try again later; otherwise, tell Linux we now have
  1109. * a working link. ISSUE: What if the return value is negative?
  1110. */
  1111. if (tile_net_open_inner(priv->dev) != 0)
  1112. schedule_delayed_work(&priv->retry_work,
  1113. TILE_NET_RETRY_INTERVAL);
  1114. else
  1115. netif_carrier_on(priv->dev);
  1116. }
  1117. /*
  1118. * Called when a network interface is made active.
  1119. *
  1120. * Returns 0 on success, negative value on failure.
  1121. *
  1122. * The open entry point is called when a network interface is made
  1123. * active by the system (IFF_UP). At this point all resources needed
  1124. * for transmit and receive operations are allocated, the interrupt
  1125. * handler is registered with the OS (if needed), the watchdog timer
  1126. * is started, and the stack is notified that the interface is ready.
  1127. *
  1128. * If the actual link is not available yet, then we tell Linux that
  1129. * we have no carrier, and we keep checking until the link comes up.
  1130. */
  1131. static int tile_net_open(struct net_device *dev)
  1132. {
  1133. int ret = 0;
  1134. struct tile_net_priv *priv = netdev_priv(dev);
  1135. /*
  1136. * We rely on priv->partly_opened to tell us if this is the
  1137. * first time this interface is being brought up. If it is
  1138. * set, the IPP was already initialized and should not be
  1139. * initialized again.
  1140. */
  1141. if (!priv->partly_opened) {
  1142. int count;
  1143. int credits;
  1144. /* Initialize LIPP/LEPP, and start the Shim. */
  1145. ret = tile_net_open_aux(dev);
  1146. if (ret < 0) {
  1147. pr_err("tile_net_open_aux failed: %d\n", ret);
  1148. return ret;
  1149. }
  1150. /* Analyze the network cpus. */
  1151. if (network_cpus_used)
  1152. cpumask_copy(&priv->network_cpus_map,
  1153. &network_cpus_map);
  1154. else
  1155. cpumask_copy(&priv->network_cpus_map, cpu_online_mask);
  1156. count = cpumask_weight(&priv->network_cpus_map);
  1157. /* Limit credits to available buffers, and apply min. */
  1158. credits = max(16, (LIPP_LARGE_BUFFERS / count) & ~1);
  1159. /* Apply "GBE" max limit. */
  1160. /* ISSUE: Use higher limit for XGBE? */
  1161. credits = min(NETIO_MAX_RECEIVE_PKTS, credits);
  1162. priv->network_cpus_count = count;
  1163. priv->network_cpus_credits = credits;
  1164. #ifdef TILE_NET_DEBUG
  1165. pr_info("Using %d network cpus, with %d credits each\n",
  1166. priv->network_cpus_count, priv->network_cpus_credits);
  1167. #endif
  1168. priv->partly_opened = true;
  1169. } else {
  1170. /* FIXME: Is this possible? */
  1171. /* printk("Already partly opened.\n"); */
  1172. }
  1173. /*
  1174. * Attempt to bring up the link.
  1175. */
  1176. ret = tile_net_open_inner(dev);
  1177. if (ret <= 0) {
  1178. if (ret == 0)
  1179. netif_carrier_on(dev);
  1180. return ret;
  1181. }
  1182. /*
  1183. * We were unable to bring up the NetIO interface, but we want to
  1184. * try again in a little bit. Tell Linux that we have no carrier
  1185. * so it doesn't try to use the interface before the link comes up
  1186. * and then remember to try again later.
  1187. */
  1188. netif_carrier_off(dev);
  1189. schedule_delayed_work(&priv->retry_work, TILE_NET_RETRY_INTERVAL);
  1190. return 0;
  1191. }
  1192. static int tile_net_drain_lipp_buffers(struct tile_net_priv *priv)
  1193. {
  1194. int n = 0;
  1195. /* Drain all the LIPP buffers. */
  1196. while (true) {
  1197. unsigned int buffer;
  1198. /* NOTE: This should never fail. */
  1199. if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&buffer,
  1200. sizeof(buffer), NETIO_IPP_DRAIN_OFF) < 0)
  1201. break;
  1202. /* Stop when done. */
  1203. if (buffer == 0)
  1204. break;
  1205. {
  1206. /* Convert "linux_buffer_t" to "va". */
  1207. void *va = __va((phys_addr_t)(buffer >> 1) << 7);
  1208. /* Acquire the associated "skb". */
  1209. struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
  1210. struct sk_buff *skb = *skb_ptr;
  1211. kfree_skb(skb);
  1212. }
  1213. n++;
  1214. }
  1215. return n;
  1216. }
  1217. /*
  1218. * Disables a network interface.
  1219. *
  1220. * Returns 0, this is not allowed to fail.
  1221. *
  1222. * The close entry point is called when an interface is de-activated
  1223. * by the OS. The hardware is still under the drivers control, but
  1224. * needs to be disabled. A global MAC reset is issued to stop the
  1225. * hardware, and all transmit and receive resources are freed.
  1226. *
  1227. * ISSUE: How closely does "netif_running(dev)" mirror "priv->active"?
  1228. *
  1229. * Before we are called by "__dev_close()", "netif_running()" will
  1230. * have been cleared, so no NEW calls to "tile_net_poll()" will be
  1231. * made by "netpoll_poll_dev()".
  1232. *
  1233. * Often, this can cause some tiles to still have packets in their
  1234. * queues, so we must call "tile_net_discard_packets()" later.
  1235. *
  1236. * Note that some other tile may still be INSIDE "tile_net_poll()",
  1237. * and in fact, many will be, if there is heavy network load.
  1238. *
  1239. * Calling "on_each_cpu(tile_net_stop_disable, (void *)dev, 1)" when
  1240. * any tile is still "napi_schedule()"'d will induce a horrible crash
  1241. * when "msleep()" is called. This includes tiles which are inside
  1242. * "tile_net_poll()" which have not yet called "napi_complete()".
  1243. *
  1244. * So, we must first try to wait long enough for other tiles to finish
  1245. * with any current "tile_net_poll()" call, and, hopefully, to clear
  1246. * the "scheduled" flag. ISSUE: It is unclear what happens to tiles
  1247. * which have called "napi_schedule()" but which had not yet tried to
  1248. * call "tile_net_poll()", or which exhausted their budget inside
  1249. * "tile_net_poll()" just before this function was called.
  1250. */
  1251. static int tile_net_stop(struct net_device *dev)
  1252. {
  1253. struct tile_net_priv *priv = netdev_priv(dev);
  1254. PDEBUG("tile_net_stop()\n");
  1255. /* Start discarding packets. */
  1256. priv->active = false;
  1257. /* Make sure "active" is visible to all tiles. */
  1258. mb();
  1259. /*
  1260. * On each tile, make sure no NEW packets get delivered, and
  1261. * disable the ingress interrupt.
  1262. *
  1263. * Note that the ingress interrupt can fire AFTER this,
  1264. * presumably due to packets which were recently delivered,
  1265. * but it will have no effect.
  1266. */
  1267. on_each_cpu(tile_net_deregister, (void *)dev, 1);
  1268. /* Optimistically drain LIPP buffers. */
  1269. (void)tile_net_drain_lipp_buffers(priv);
  1270. /* ISSUE: Only needed if not yet fully open. */
  1271. cancel_delayed_work_sync(&priv->retry_work);
  1272. /* Can't transmit any more. */
  1273. netif_stop_queue(dev);
  1274. /* Disable NAPI on each tile. */
  1275. on_each_cpu(tile_net_stop_disable, (void *)dev, 1);
  1276. /*
  1277. * Drain any remaining LIPP buffers. NOTE: This "printk()"
  1278. * has never been observed, but in theory it could happen.
  1279. */
  1280. if (tile_net_drain_lipp_buffers(priv) != 0)
  1281. printk("Had to drain some extra LIPP buffers!\n");
  1282. /* Stop LIPP/LEPP. */
  1283. tile_net_stop_aux(dev);
  1284. /*
  1285. * ISSUE: It appears that, in practice anyway, by the time we
  1286. * get here, there are no pending completions, but just in case,
  1287. * we free (all of) them anyway.
  1288. */
  1289. while (tile_net_lepp_free_comps(dev, true))
  1290. /* loop */;
  1291. /* Wipe the EPP queue, and wait till the stores hit the EPP. */
  1292. memset(priv->eq, 0, sizeof(lepp_queue_t));
  1293. mb();
  1294. return 0;
  1295. }
  1296. /*
  1297. * Prepare the "frags" info for the resulting LEPP command.
  1298. *
  1299. * If needed, flush the memory used by the frags.
  1300. */
  1301. static unsigned int tile_net_tx_frags(lepp_frag_t *frags,
  1302. struct sk_buff *skb,
  1303. void *b_data, unsigned int b_len)
  1304. {
  1305. unsigned int i, n = 0;
  1306. struct skb_shared_info *sh = skb_shinfo(skb);
  1307. phys_addr_t cpa;
  1308. if (b_len != 0) {
  1309. if (!hash_default)
  1310. finv_buffer_remote(b_data, b_len, 0);
  1311. cpa = __pa(b_data);
  1312. frags[n].cpa_lo = cpa;
  1313. frags[n].cpa_hi = cpa >> 32;
  1314. frags[n].length = b_len;
  1315. frags[n].hash_for_home = hash_default;
  1316. n++;
  1317. }
  1318. for (i = 0; i < sh->nr_frags; i++) {
  1319. skb_frag_t *f = &sh->frags[i];
  1320. unsigned long pfn = page_to_pfn(skb_frag_page(f));
  1321. /* FIXME: Compute "hash_for_home" properly. */
  1322. /* ISSUE: The hypervisor checks CHIP_HAS_REV1_DMA_PACKETS(). */
  1323. int hash_for_home = hash_default;
  1324. /* FIXME: Hmmm. */
  1325. if (!hash_default) {
  1326. void *va = pfn_to_kaddr(pfn) + f->page_offset;
  1327. BUG_ON(PageHighMem(skb_frag_page(f)));
  1328. finv_buffer_remote(va, skb_frag_size(f), 0);
  1329. }
  1330. cpa = ((phys_addr_t)pfn << PAGE_SHIFT) + f->page_offset;
  1331. frags[n].cpa_lo = cpa;
  1332. frags[n].cpa_hi = cpa >> 32;
  1333. frags[n].length = skb_frag_size(f);
  1334. frags[n].hash_for_home = hash_for_home;
  1335. n++;
  1336. }
  1337. return n;
  1338. }
  1339. /*
  1340. * This function takes "skb", consisting of a header template and a
  1341. * payload, and hands it to LEPP, to emit as one or more segments,
  1342. * each consisting of a possibly modified header, plus a piece of the
  1343. * payload, via a process known as "tcp segmentation offload".
  1344. *
  1345. * Usually, "data" will contain the header template, of size "sh_len",
  1346. * and "sh->frags" will contain "skb->data_len" bytes of payload, and
  1347. * there will be "sh->gso_segs" segments.
  1348. *
  1349. * Sometimes, if "sendfile()" requires copying, we will be called with
  1350. * "data" containing the header and payload, with "frags" being empty.
  1351. *
  1352. * Sometimes, for example when using NFS over TCP, a single segment can
  1353. * span 3 fragments, which must be handled carefully in LEPP.
  1354. *
  1355. * See "emulate_large_send_offload()" for some reference code, which
  1356. * does not handle checksumming.
  1357. *
  1358. * ISSUE: How do we make sure that high memory DMA does not migrate?
  1359. */
  1360. static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
  1361. {
  1362. struct tile_net_priv *priv = netdev_priv(dev);
  1363. int my_cpu = smp_processor_id();
  1364. struct tile_net_cpu *info = priv->cpu[my_cpu];
  1365. struct tile_net_stats_t *stats = &info->stats;
  1366. struct skb_shared_info *sh = skb_shinfo(skb);
  1367. unsigned char *data = skb->data;
  1368. /* The ip header follows the ethernet header. */
  1369. struct iphdr *ih = ip_hdr(skb);
  1370. unsigned int ih_len = ih->ihl * 4;
  1371. /* Note that "nh == ih", by definition. */
  1372. unsigned char *nh = skb_network_header(skb);
  1373. unsigned int eh_len = nh - data;
  1374. /* The tcp header follows the ip header. */
  1375. struct tcphdr *th = (struct tcphdr *)(nh + ih_len);
  1376. unsigned int th_len = th->doff * 4;
  1377. /* The total number of header bytes. */
  1378. /* NOTE: This may be less than skb_headlen(skb). */
  1379. unsigned int sh_len = eh_len + ih_len + th_len;
  1380. /* The number of payload bytes at "skb->data + sh_len". */
  1381. /* This is non-zero for sendfile() without HIGHDMA. */
  1382. unsigned int b_len = skb_headlen(skb) - sh_len;
  1383. /* The total number of payload bytes. */
  1384. unsigned int d_len = b_len + skb->data_len;
  1385. /* The maximum payload size. */
  1386. unsigned int p_len = sh->gso_size;
  1387. /* The total number of segments. */
  1388. unsigned int num_segs = sh->gso_segs;
  1389. /* The temporary copy of the command. */
  1390. u32 cmd_body[(LEPP_MAX_CMD_SIZE + 3) / 4];
  1391. lepp_tso_cmd_t *cmd = (lepp_tso_cmd_t *)cmd_body;
  1392. /* Analyze the "frags". */
  1393. unsigned int num_frags =
  1394. tile_net_tx_frags(cmd->frags, skb, data + sh_len, b_len);
  1395. /* The size of the command, including frags and header. */
  1396. size_t cmd_size = LEPP_TSO_CMD_SIZE(num_frags, sh_len);
  1397. /* The command header. */
  1398. lepp_tso_cmd_t cmd_init = {
  1399. .tso = true,
  1400. .header_size = sh_len,
  1401. .ip_offset = eh_len,
  1402. .tcp_offset = eh_len + ih_len,
  1403. .payload_size = p_len,
  1404. .num_frags = num_frags,
  1405. };
  1406. unsigned long irqflags;
  1407. lepp_queue_t *eq = priv->eq;
  1408. struct sk_buff *olds[8];
  1409. unsigned int wanted = 8;
  1410. unsigned int i, nolds = 0;
  1411. unsigned int cmd_head, cmd_tail, cmd_next;
  1412. unsigned int comp_tail;
  1413. /* Paranoia. */
  1414. BUG_ON(skb->protocol != htons(ETH_P_IP));
  1415. BUG_ON(ih->protocol != IPPROTO_TCP);
  1416. BUG_ON(skb->ip_summed != CHECKSUM_PARTIAL);
  1417. BUG_ON(num_frags > LEPP_MAX_FRAGS);
  1418. /*--BUG_ON(num_segs != (d_len + (p_len - 1)) / p_len); */
  1419. BUG_ON(num_segs <= 1);
  1420. /* Finish preparing the command. */
  1421. /* Copy the command header. */
  1422. *cmd = cmd_init;
  1423. /* Copy the "header". */
  1424. memcpy(&cmd->frags[num_frags], data, sh_len);
  1425. /* Prefetch and wait, to minimize time spent holding the spinlock. */
  1426. prefetch_L1(&eq->comp_tail);
  1427. prefetch_L1(&eq->cmd_tail);
  1428. mb();
  1429. /* Enqueue the command. */
  1430. spin_lock_irqsave(&priv->eq_lock, irqflags);
  1431. /* Handle completions if needed to make room. */
  1432. /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
  1433. if (lepp_num_free_comp_slots(eq) == 0) {
  1434. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
  1435. if (nolds == 0) {
  1436. busy:
  1437. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1438. return NETDEV_TX_BUSY;
  1439. }
  1440. }
  1441. cmd_head = eq->cmd_head;
  1442. cmd_tail = eq->cmd_tail;
  1443. /* Prepare to advance, detecting full queue. */
  1444. /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
  1445. cmd_next = cmd_tail + cmd_size;
  1446. if (cmd_tail < cmd_head && cmd_next >= cmd_head)
  1447. goto busy;
  1448. if (cmd_next > LEPP_CMD_LIMIT) {
  1449. cmd_next = 0;
  1450. if (cmd_next == cmd_head)
  1451. goto busy;
  1452. }
  1453. /* Copy the command. */
  1454. memcpy(&eq->cmds[cmd_tail], cmd, cmd_size);
  1455. /* Advance. */
  1456. cmd_tail = cmd_next;
  1457. /* Record "skb" for eventual freeing. */
  1458. comp_tail = eq->comp_tail;
  1459. eq->comps[comp_tail] = skb;
  1460. LEPP_QINC(comp_tail);
  1461. eq->comp_tail = comp_tail;
  1462. /* Flush before allowing LEPP to handle the command. */
  1463. /* ISSUE: Is this the optimal location for the flush? */
  1464. __insn_mf();
  1465. eq->cmd_tail = cmd_tail;
  1466. /* NOTE: Using "4" here is more efficient than "0" or "2", */
  1467. /* and, strangely, more efficient than pre-checking the number */
  1468. /* of available completions, and comparing it to 4. */
  1469. if (nolds == 0)
  1470. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
  1471. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1472. /* Handle completions. */
  1473. for (i = 0; i < nolds; i++)
  1474. dev_consume_skb_any(olds[i]);
  1475. /* Update stats. */
  1476. u64_stats_update_begin(&stats->syncp);
  1477. stats->tx_packets += num_segs;
  1478. stats->tx_bytes += (num_segs * sh_len) + d_len;
  1479. u64_stats_update_end(&stats->syncp);
  1480. /* Make sure the egress timer is scheduled. */
  1481. tile_net_schedule_egress_timer(info);
  1482. return NETDEV_TX_OK;
  1483. }
  1484. /*
  1485. * Transmit a packet (called by the kernel via "hard_start_xmit" hook).
  1486. */
  1487. static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
  1488. {
  1489. struct tile_net_priv *priv = netdev_priv(dev);
  1490. int my_cpu = smp_processor_id();
  1491. struct tile_net_cpu *info = priv->cpu[my_cpu];
  1492. struct tile_net_stats_t *stats = &info->stats;
  1493. unsigned long irqflags;
  1494. struct skb_shared_info *sh = skb_shinfo(skb);
  1495. unsigned int len = skb->len;
  1496. unsigned char *data = skb->data;
  1497. unsigned int csum_start = skb_checksum_start_offset(skb);
  1498. lepp_frag_t frags[1 + MAX_SKB_FRAGS];
  1499. unsigned int num_frags;
  1500. lepp_queue_t *eq = priv->eq;
  1501. struct sk_buff *olds[8];
  1502. unsigned int wanted = 8;
  1503. unsigned int i, nolds = 0;
  1504. unsigned int cmd_size = sizeof(lepp_cmd_t);
  1505. unsigned int cmd_head, cmd_tail, cmd_next;
  1506. unsigned int comp_tail;
  1507. lepp_cmd_t cmds[1 + MAX_SKB_FRAGS];
  1508. /*
  1509. * This is paranoia, since we think that if the link doesn't come
  1510. * up, telling Linux we have no carrier will keep it from trying
  1511. * to transmit. If it does, though, we can't execute this routine,
  1512. * since data structures we depend on aren't set up yet.
  1513. */
  1514. if (!info->registered)
  1515. return NETDEV_TX_BUSY;
  1516. /* Save the timestamp. */
  1517. netif_trans_update(dev);
  1518. #ifdef TILE_NET_PARANOIA
  1519. #if CHIP_HAS_CBOX_HOME_MAP()
  1520. if (hash_default) {
  1521. HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)data);
  1522. if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
  1523. panic("Non-HFH egress buffer! VA=%p Mode=%d PTE=%llx",
  1524. data, hv_pte_get_mode(pte), hv_pte_val(pte));
  1525. }
  1526. #endif
  1527. #endif
  1528. #ifdef TILE_NET_DUMP_PACKETS
  1529. /* ISSUE: Does not dump the "frags". */
  1530. dump_packet(data, skb_headlen(skb), "tx");
  1531. #endif /* TILE_NET_DUMP_PACKETS */
  1532. if (sh->gso_size != 0)
  1533. return tile_net_tx_tso(skb, dev);
  1534. /* Prepare the commands. */
  1535. num_frags = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
  1536. for (i = 0; i < num_frags; i++) {
  1537. bool final = (i == num_frags - 1);
  1538. lepp_cmd_t cmd = {
  1539. .cpa_lo = frags[i].cpa_lo,
  1540. .cpa_hi = frags[i].cpa_hi,
  1541. .length = frags[i].length,
  1542. .hash_for_home = frags[i].hash_for_home,
  1543. .send_completion = final,
  1544. .end_of_packet = final
  1545. };
  1546. if (i == 0 && skb->ip_summed == CHECKSUM_PARTIAL) {
  1547. cmd.compute_checksum = 1;
  1548. cmd.checksum_data.bits.start_byte = csum_start;
  1549. cmd.checksum_data.bits.count = len - csum_start;
  1550. cmd.checksum_data.bits.destination_byte =
  1551. csum_start + skb->csum_offset;
  1552. }
  1553. cmds[i] = cmd;
  1554. }
  1555. /* Prefetch and wait, to minimize time spent holding the spinlock. */
  1556. prefetch_L1(&eq->comp_tail);
  1557. prefetch_L1(&eq->cmd_tail);
  1558. mb();
  1559. /* Enqueue the commands. */
  1560. spin_lock_irqsave(&priv->eq_lock, irqflags);
  1561. /* Handle completions if needed to make room. */
  1562. /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
  1563. if (lepp_num_free_comp_slots(eq) == 0) {
  1564. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
  1565. if (nolds == 0) {
  1566. busy:
  1567. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1568. return NETDEV_TX_BUSY;
  1569. }
  1570. }
  1571. cmd_head = eq->cmd_head;
  1572. cmd_tail = eq->cmd_tail;
  1573. /* Copy the commands, or fail. */
  1574. /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
  1575. for (i = 0; i < num_frags; i++) {
  1576. /* Prepare to advance, detecting full queue. */
  1577. cmd_next = cmd_tail + cmd_size;
  1578. if (cmd_tail < cmd_head && cmd_next >= cmd_head)
  1579. goto busy;
  1580. if (cmd_next > LEPP_CMD_LIMIT) {
  1581. cmd_next = 0;
  1582. if (cmd_next == cmd_head)
  1583. goto busy;
  1584. }
  1585. /* Copy the command. */
  1586. *(lepp_cmd_t *)&eq->cmds[cmd_tail] = cmds[i];
  1587. /* Advance. */
  1588. cmd_tail = cmd_next;
  1589. }
  1590. /* Record "skb" for eventual freeing. */
  1591. comp_tail = eq->comp_tail;
  1592. eq->comps[comp_tail] = skb;
  1593. LEPP_QINC(comp_tail);
  1594. eq->comp_tail = comp_tail;
  1595. /* Flush before allowing LEPP to handle the command. */
  1596. /* ISSUE: Is this the optimal location for the flush? */
  1597. __insn_mf();
  1598. eq->cmd_tail = cmd_tail;
  1599. /* NOTE: Using "4" here is more efficient than "0" or "2", */
  1600. /* and, strangely, more efficient than pre-checking the number */
  1601. /* of available completions, and comparing it to 4. */
  1602. if (nolds == 0)
  1603. nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
  1604. spin_unlock_irqrestore(&priv->eq_lock, irqflags);
  1605. /* Handle completions. */
  1606. for (i = 0; i < nolds; i++)
  1607. dev_consume_skb_any(olds[i]);
  1608. /* HACK: Track "expanded" size for short packets (e.g. 42 < 60). */
  1609. u64_stats_update_begin(&stats->syncp);
  1610. stats->tx_packets++;
  1611. stats->tx_bytes += ((len >= ETH_ZLEN) ? len : ETH_ZLEN);
  1612. u64_stats_update_end(&stats->syncp);
  1613. /* Make sure the egress timer is scheduled. */
  1614. tile_net_schedule_egress_timer(info);
  1615. return NETDEV_TX_OK;
  1616. }
  1617. /*
  1618. * Deal with a transmit timeout.
  1619. */
  1620. static void tile_net_tx_timeout(struct net_device *dev)
  1621. {
  1622. PDEBUG("tile_net_tx_timeout()\n");
  1623. PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
  1624. jiffies - dev_trans_start(dev));
  1625. /* XXX: ISSUE: This doesn't seem useful for us. */
  1626. netif_wake_queue(dev);
  1627. }
  1628. /*
  1629. * Ioctl commands.
  1630. */
  1631. static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1632. {
  1633. return -EOPNOTSUPP;
  1634. }
  1635. /*
  1636. * Get System Network Statistics.
  1637. *
  1638. * Returns the address of the device statistics structure.
  1639. */
  1640. static struct rtnl_link_stats64 *tile_net_get_stats64(struct net_device *dev,
  1641. struct rtnl_link_stats64 *stats)
  1642. {
  1643. struct tile_net_priv *priv = netdev_priv(dev);
  1644. u64 rx_packets = 0, tx_packets = 0;
  1645. u64 rx_bytes = 0, tx_bytes = 0;
  1646. u64 rx_errors = 0, rx_dropped = 0;
  1647. int i;
  1648. for_each_online_cpu(i) {
  1649. struct tile_net_stats_t *cpu_stats;
  1650. u64 trx_packets, ttx_packets, trx_bytes, ttx_bytes;
  1651. u64 trx_errors, trx_dropped;
  1652. unsigned int start;
  1653. if (priv->cpu[i] == NULL)
  1654. continue;
  1655. cpu_stats = &priv->cpu[i]->stats;
  1656. do {
  1657. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  1658. trx_packets = cpu_stats->rx_packets;
  1659. ttx_packets = cpu_stats->tx_packets;
  1660. trx_bytes = cpu_stats->rx_bytes;
  1661. ttx_bytes = cpu_stats->tx_bytes;
  1662. trx_errors = cpu_stats->rx_errors;
  1663. trx_dropped = cpu_stats->rx_dropped;
  1664. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  1665. rx_packets += trx_packets;
  1666. tx_packets += ttx_packets;
  1667. rx_bytes += trx_bytes;
  1668. tx_bytes += ttx_bytes;
  1669. rx_errors += trx_errors;
  1670. rx_dropped += trx_dropped;
  1671. }
  1672. stats->rx_packets = rx_packets;
  1673. stats->tx_packets = tx_packets;
  1674. stats->rx_bytes = rx_bytes;
  1675. stats->tx_bytes = tx_bytes;
  1676. stats->rx_errors = rx_errors;
  1677. stats->rx_dropped = rx_dropped;
  1678. return stats;
  1679. }
  1680. /*
  1681. * Change the "mtu".
  1682. *
  1683. * The "change_mtu" method is usually not needed.
  1684. * If you need it, it must be like this.
  1685. */
  1686. static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
  1687. {
  1688. PDEBUG("tile_net_change_mtu()\n");
  1689. /* Check ranges. */
  1690. if ((new_mtu < 68) || (new_mtu > 1500))
  1691. return -EINVAL;
  1692. /* Accept the value. */
  1693. dev->mtu = new_mtu;
  1694. return 0;
  1695. }
  1696. /*
  1697. * Change the Ethernet Address of the NIC.
  1698. *
  1699. * The hypervisor driver does not support changing MAC address. However,
  1700. * the IPP does not do anything with the MAC address, so the address which
  1701. * gets used on outgoing packets, and which is accepted on incoming packets,
  1702. * is completely up to the NetIO program or kernel driver which is actually
  1703. * handling them.
  1704. *
  1705. * Returns 0 on success, negative on failure.
  1706. */
  1707. static int tile_net_set_mac_address(struct net_device *dev, void *p)
  1708. {
  1709. struct sockaddr *addr = p;
  1710. if (!is_valid_ether_addr(addr->sa_data))
  1711. return -EADDRNOTAVAIL;
  1712. /* ISSUE: Note that "dev_addr" is now a pointer. */
  1713. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1714. return 0;
  1715. }
  1716. /*
  1717. * Obtain the MAC address from the hypervisor.
  1718. * This must be done before opening the device.
  1719. */
  1720. static int tile_net_get_mac(struct net_device *dev)
  1721. {
  1722. struct tile_net_priv *priv = netdev_priv(dev);
  1723. char hv_dev_name[32];
  1724. int len;
  1725. __netio_getset_offset_t offset = { .word = NETIO_IPP_PARAM_OFF };
  1726. int ret;
  1727. /* For example, "xgbe0". */
  1728. strcpy(hv_dev_name, dev->name);
  1729. len = strlen(hv_dev_name);
  1730. /* For example, "xgbe/0". */
  1731. hv_dev_name[len] = hv_dev_name[len - 1];
  1732. hv_dev_name[len - 1] = '/';
  1733. len++;
  1734. /* For example, "xgbe/0/native_hash". */
  1735. strcpy(hv_dev_name + len, hash_default ? "/native_hash" : "/native");
  1736. /* Get the hypervisor handle for this device. */
  1737. priv->hv_devhdl = hv_dev_open((HV_VirtAddr)hv_dev_name, 0);
  1738. PDEBUG("hv_dev_open(%s) returned %d %p\n",
  1739. hv_dev_name, priv->hv_devhdl, &priv->hv_devhdl);
  1740. if (priv->hv_devhdl < 0) {
  1741. if (priv->hv_devhdl == HV_ENODEV)
  1742. printk(KERN_DEBUG "Ignoring unconfigured device %s\n",
  1743. hv_dev_name);
  1744. else
  1745. printk(KERN_DEBUG "hv_dev_open(%s) returned %d\n",
  1746. hv_dev_name, priv->hv_devhdl);
  1747. return -1;
  1748. }
  1749. /*
  1750. * Read the hardware address from the hypervisor.
  1751. * ISSUE: Note that "dev_addr" is now a pointer.
  1752. */
  1753. offset.bits.class = NETIO_PARAM;
  1754. offset.bits.addr = NETIO_PARAM_MAC;
  1755. ret = hv_dev_pread(priv->hv_devhdl, 0,
  1756. (HV_VirtAddr)dev->dev_addr, dev->addr_len,
  1757. offset.word);
  1758. PDEBUG("hv_dev_pread(NETIO_PARAM_MAC) returned %d\n", ret);
  1759. if (ret <= 0) {
  1760. printk(KERN_DEBUG "hv_dev_pread(NETIO_PARAM_MAC) %s failed\n",
  1761. dev->name);
  1762. /*
  1763. * Since the device is configured by the hypervisor but we
  1764. * can't get its MAC address, we are most likely running
  1765. * the simulator, so let's generate a random MAC address.
  1766. */
  1767. eth_hw_addr_random(dev);
  1768. }
  1769. return 0;
  1770. }
  1771. #ifdef CONFIG_NET_POLL_CONTROLLER
  1772. /*
  1773. * Polling 'interrupt' - used by things like netconsole to send skbs
  1774. * without having to re-enable interrupts. It's not called while
  1775. * the interrupt routine is executing.
  1776. */
  1777. static void tile_net_netpoll(struct net_device *dev)
  1778. {
  1779. struct tile_net_priv *priv = netdev_priv(dev);
  1780. disable_percpu_irq(priv->intr_id);
  1781. tile_net_handle_ingress_interrupt(priv->intr_id, dev);
  1782. enable_percpu_irq(priv->intr_id, 0);
  1783. }
  1784. #endif
  1785. static const struct net_device_ops tile_net_ops = {
  1786. .ndo_open = tile_net_open,
  1787. .ndo_stop = tile_net_stop,
  1788. .ndo_start_xmit = tile_net_tx,
  1789. .ndo_do_ioctl = tile_net_ioctl,
  1790. .ndo_get_stats64 = tile_net_get_stats64,
  1791. .ndo_change_mtu = tile_net_change_mtu,
  1792. .ndo_tx_timeout = tile_net_tx_timeout,
  1793. .ndo_set_mac_address = tile_net_set_mac_address,
  1794. #ifdef CONFIG_NET_POLL_CONTROLLER
  1795. .ndo_poll_controller = tile_net_netpoll,
  1796. #endif
  1797. };
  1798. /*
  1799. * The setup function.
  1800. *
  1801. * This uses ether_setup() to assign various fields in dev, including
  1802. * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
  1803. */
  1804. static void tile_net_setup(struct net_device *dev)
  1805. {
  1806. netdev_features_t features = 0;
  1807. ether_setup(dev);
  1808. dev->netdev_ops = &tile_net_ops;
  1809. dev->watchdog_timeo = TILE_NET_TIMEOUT;
  1810. dev->tx_queue_len = TILE_NET_TX_QUEUE_LEN;
  1811. dev->mtu = TILE_NET_MTU;
  1812. features |= NETIF_F_HW_CSUM;
  1813. features |= NETIF_F_SG;
  1814. /* We support TSO iff the HV supports sufficient frags. */
  1815. if (LEPP_MAX_FRAGS >= 1 + MAX_SKB_FRAGS)
  1816. features |= NETIF_F_TSO;
  1817. /* We can't support HIGHDMA without hash_default, since we need
  1818. * to be able to finv() with a VA if we don't have hash_default.
  1819. */
  1820. if (hash_default)
  1821. features |= NETIF_F_HIGHDMA;
  1822. dev->hw_features |= features;
  1823. dev->vlan_features |= features;
  1824. dev->features |= features;
  1825. }
  1826. /*
  1827. * Allocate the device structure, register the device, and obtain the
  1828. * MAC address from the hypervisor.
  1829. */
  1830. static struct net_device *tile_net_dev_init(const char *name)
  1831. {
  1832. int ret;
  1833. struct net_device *dev;
  1834. struct tile_net_priv *priv;
  1835. /*
  1836. * Allocate the device structure. This allocates "priv", calls
  1837. * tile_net_setup(), and saves "name". Normally, "name" is a
  1838. * template, instantiated by register_netdev(), but not for us.
  1839. */
  1840. dev = alloc_netdev(sizeof(*priv), name, NET_NAME_UNKNOWN,
  1841. tile_net_setup);
  1842. if (!dev) {
  1843. pr_err("alloc_netdev(%s) failed\n", name);
  1844. return NULL;
  1845. }
  1846. priv = netdev_priv(dev);
  1847. /* Initialize "priv". */
  1848. memset(priv, 0, sizeof(*priv));
  1849. /* Save "dev" for "tile_net_open_retry()". */
  1850. priv->dev = dev;
  1851. INIT_DELAYED_WORK(&priv->retry_work, tile_net_open_retry);
  1852. spin_lock_init(&priv->eq_lock);
  1853. /* Allocate "eq". */
  1854. priv->eq_pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, EQ_ORDER);
  1855. if (!priv->eq_pages) {
  1856. free_netdev(dev);
  1857. return NULL;
  1858. }
  1859. priv->eq = page_address(priv->eq_pages);
  1860. /* Register the network device. */
  1861. ret = register_netdev(dev);
  1862. if (ret) {
  1863. pr_err("register_netdev %s failed %d\n", dev->name, ret);
  1864. __free_pages(priv->eq_pages, EQ_ORDER);
  1865. free_netdev(dev);
  1866. return NULL;
  1867. }
  1868. /* Get the MAC address. */
  1869. ret = tile_net_get_mac(dev);
  1870. if (ret < 0) {
  1871. unregister_netdev(dev);
  1872. __free_pages(priv->eq_pages, EQ_ORDER);
  1873. free_netdev(dev);
  1874. return NULL;
  1875. }
  1876. return dev;
  1877. }
  1878. /*
  1879. * Module cleanup.
  1880. *
  1881. * FIXME: If compiled as a module, this module cannot be "unloaded",
  1882. * because the "ingress interrupt handler" is registered permanently.
  1883. */
  1884. static void tile_net_cleanup(void)
  1885. {
  1886. int i;
  1887. for (i = 0; i < TILE_NET_DEVS; i++) {
  1888. if (tile_net_devs[i]) {
  1889. struct net_device *dev = tile_net_devs[i];
  1890. struct tile_net_priv *priv = netdev_priv(dev);
  1891. unregister_netdev(dev);
  1892. finv_buffer_remote(priv->eq, EQ_SIZE, 0);
  1893. __free_pages(priv->eq_pages, EQ_ORDER);
  1894. free_netdev(dev);
  1895. }
  1896. }
  1897. }
  1898. /*
  1899. * Module initialization.
  1900. */
  1901. static int tile_net_init_module(void)
  1902. {
  1903. pr_info("Tilera Network Driver\n");
  1904. tile_net_devs[0] = tile_net_dev_init("xgbe0");
  1905. tile_net_devs[1] = tile_net_dev_init("xgbe1");
  1906. tile_net_devs[2] = tile_net_dev_init("gbe0");
  1907. tile_net_devs[3] = tile_net_dev_init("gbe1");
  1908. return 0;
  1909. }
  1910. module_init(tile_net_init_module);
  1911. module_exit(tile_net_cleanup);
  1912. #ifndef MODULE
  1913. /*
  1914. * The "network_cpus" boot argument specifies the cpus that are dedicated
  1915. * to handle ingress packets.
  1916. *
  1917. * The parameter should be in the form "network_cpus=m-n[,x-y]", where
  1918. * m, n, x, y are integer numbers that represent the cpus that can be
  1919. * neither a dedicated cpu nor a dataplane cpu.
  1920. */
  1921. static int __init network_cpus_setup(char *str)
  1922. {
  1923. int rc = cpulist_parse_crop(str, &network_cpus_map);
  1924. if (rc != 0) {
  1925. pr_warn("network_cpus=%s: malformed cpu list\n", str);
  1926. } else {
  1927. /* Remove dedicated cpus. */
  1928. cpumask_and(&network_cpus_map, &network_cpus_map,
  1929. cpu_possible_mask);
  1930. if (cpumask_empty(&network_cpus_map)) {
  1931. pr_warn("Ignoring network_cpus='%s'\n", str);
  1932. } else {
  1933. pr_info("Linux network CPUs: %*pbl\n",
  1934. cpumask_pr_args(&network_cpus_map));
  1935. network_cpus_used = true;
  1936. }
  1937. }
  1938. return 0;
  1939. }
  1940. __setup("network_cpus=", network_cpus_setup);
  1941. #endif