tilepro.c 63 KB

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