tilepro.c 63 KB

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