tilepro.c 59 KB

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