tilepro.c 59 KB

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