tilepro.c 63 KB

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