netvsc_drv.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Authors:
  17. * Haiyang Zhang <haiyangz@microsoft.com>
  18. * Hank Janssen <hjanssen@microsoft.com>
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/init.h>
  22. #include <linux/atomic.h>
  23. #include <linux/module.h>
  24. #include <linux/highmem.h>
  25. #include <linux/device.h>
  26. #include <linux/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/pci.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/if_vlan.h>
  34. #include <linux/in.h>
  35. #include <linux/slab.h>
  36. #include <linux/rtnetlink.h>
  37. #include <linux/netpoll.h>
  38. #include <net/arp.h>
  39. #include <net/route.h>
  40. #include <net/sock.h>
  41. #include <net/pkt_sched.h>
  42. #include <net/checksum.h>
  43. #include <net/ip6_checksum.h>
  44. #include "hyperv_net.h"
  45. #define RING_SIZE_MIN 64
  46. #define RETRY_US_LO 5000
  47. #define RETRY_US_HI 10000
  48. #define RETRY_MAX 2000 /* >10 sec */
  49. #define LINKCHANGE_INT (2 * HZ)
  50. #define VF_TAKEOVER_INT (HZ / 10)
  51. static unsigned int ring_size __ro_after_init = 128;
  52. module_param(ring_size, uint, 0444);
  53. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  54. unsigned int netvsc_ring_bytes __ro_after_init;
  55. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  56. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  57. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  58. NETIF_MSG_TX_ERR;
  59. static int debug = -1;
  60. module_param(debug, int, 0444);
  61. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  62. static LIST_HEAD(netvsc_dev_list);
  63. static void netvsc_change_rx_flags(struct net_device *net, int change)
  64. {
  65. struct net_device_context *ndev_ctx = netdev_priv(net);
  66. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  67. int inc;
  68. if (!vf_netdev)
  69. return;
  70. if (change & IFF_PROMISC) {
  71. inc = (net->flags & IFF_PROMISC) ? 1 : -1;
  72. dev_set_promiscuity(vf_netdev, inc);
  73. }
  74. if (change & IFF_ALLMULTI) {
  75. inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
  76. dev_set_allmulti(vf_netdev, inc);
  77. }
  78. }
  79. static void netvsc_set_rx_mode(struct net_device *net)
  80. {
  81. struct net_device_context *ndev_ctx = netdev_priv(net);
  82. struct net_device *vf_netdev;
  83. struct netvsc_device *nvdev;
  84. rcu_read_lock();
  85. vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
  86. if (vf_netdev) {
  87. dev_uc_sync(vf_netdev, net);
  88. dev_mc_sync(vf_netdev, net);
  89. }
  90. nvdev = rcu_dereference(ndev_ctx->nvdev);
  91. if (nvdev)
  92. rndis_filter_update(nvdev);
  93. rcu_read_unlock();
  94. }
  95. static void netvsc_tx_enable(struct netvsc_device *nvscdev,
  96. struct net_device *ndev)
  97. {
  98. nvscdev->tx_disable = false;
  99. virt_wmb(); /* ensure queue wake up mechanism is on */
  100. netif_tx_wake_all_queues(ndev);
  101. }
  102. static int netvsc_open(struct net_device *net)
  103. {
  104. struct net_device_context *ndev_ctx = netdev_priv(net);
  105. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  106. struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
  107. struct rndis_device *rdev;
  108. int ret = 0;
  109. netif_carrier_off(net);
  110. /* Open up the device */
  111. ret = rndis_filter_open(nvdev);
  112. if (ret != 0) {
  113. netdev_err(net, "unable to open device (ret %d).\n", ret);
  114. return ret;
  115. }
  116. rdev = nvdev->extension;
  117. if (!rdev->link_state) {
  118. netif_carrier_on(net);
  119. netvsc_tx_enable(nvdev, net);
  120. }
  121. if (vf_netdev) {
  122. /* Setting synthetic device up transparently sets
  123. * slave as up. If open fails, then slave will be
  124. * still be offline (and not used).
  125. */
  126. ret = dev_open(vf_netdev);
  127. if (ret)
  128. netdev_warn(net,
  129. "unable to open slave: %s: %d\n",
  130. vf_netdev->name, ret);
  131. }
  132. return 0;
  133. }
  134. static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
  135. {
  136. unsigned int retry = 0;
  137. int i;
  138. /* Ensure pending bytes in ring are read */
  139. for (;;) {
  140. u32 aread = 0;
  141. for (i = 0; i < nvdev->num_chn; i++) {
  142. struct vmbus_channel *chn
  143. = nvdev->chan_table[i].channel;
  144. if (!chn)
  145. continue;
  146. /* make sure receive not running now */
  147. napi_synchronize(&nvdev->chan_table[i].napi);
  148. aread = hv_get_bytes_to_read(&chn->inbound);
  149. if (aread)
  150. break;
  151. aread = hv_get_bytes_to_read(&chn->outbound);
  152. if (aread)
  153. break;
  154. }
  155. if (aread == 0)
  156. return 0;
  157. if (++retry > RETRY_MAX)
  158. return -ETIMEDOUT;
  159. usleep_range(RETRY_US_LO, RETRY_US_HI);
  160. }
  161. }
  162. static void netvsc_tx_disable(struct netvsc_device *nvscdev,
  163. struct net_device *ndev)
  164. {
  165. if (nvscdev) {
  166. nvscdev->tx_disable = true;
  167. virt_wmb(); /* ensure txq will not wake up after stop */
  168. }
  169. netif_tx_disable(ndev);
  170. }
  171. static int netvsc_close(struct net_device *net)
  172. {
  173. struct net_device_context *net_device_ctx = netdev_priv(net);
  174. struct net_device *vf_netdev
  175. = rtnl_dereference(net_device_ctx->vf_netdev);
  176. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  177. int ret;
  178. netvsc_tx_disable(nvdev, net);
  179. /* No need to close rndis filter if it is removed already */
  180. if (!nvdev)
  181. return 0;
  182. ret = rndis_filter_close(nvdev);
  183. if (ret != 0) {
  184. netdev_err(net, "unable to close device (ret %d).\n", ret);
  185. return ret;
  186. }
  187. ret = netvsc_wait_until_empty(nvdev);
  188. if (ret)
  189. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  190. if (vf_netdev)
  191. dev_close(vf_netdev);
  192. return ret;
  193. }
  194. static inline void *init_ppi_data(struct rndis_message *msg,
  195. u32 ppi_size, u32 pkt_type)
  196. {
  197. struct rndis_packet *rndis_pkt = &msg->msg.pkt;
  198. struct rndis_per_packet_info *ppi;
  199. rndis_pkt->data_offset += ppi_size;
  200. ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
  201. + rndis_pkt->per_pkt_info_len;
  202. ppi->size = ppi_size;
  203. ppi->type = pkt_type;
  204. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  205. rndis_pkt->per_pkt_info_len += ppi_size;
  206. return ppi + 1;
  207. }
  208. /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
  209. * packets. We can use ethtool to change UDP hash level when necessary.
  210. */
  211. static inline u32 netvsc_get_hash(
  212. struct sk_buff *skb,
  213. const struct net_device_context *ndc)
  214. {
  215. struct flow_keys flow;
  216. u32 hash, pkt_proto = 0;
  217. static u32 hashrnd __read_mostly;
  218. net_get_random_once(&hashrnd, sizeof(hashrnd));
  219. if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
  220. return 0;
  221. switch (flow.basic.ip_proto) {
  222. case IPPROTO_TCP:
  223. if (flow.basic.n_proto == htons(ETH_P_IP))
  224. pkt_proto = HV_TCP4_L4HASH;
  225. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  226. pkt_proto = HV_TCP6_L4HASH;
  227. break;
  228. case IPPROTO_UDP:
  229. if (flow.basic.n_proto == htons(ETH_P_IP))
  230. pkt_proto = HV_UDP4_L4HASH;
  231. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  232. pkt_proto = HV_UDP6_L4HASH;
  233. break;
  234. }
  235. if (pkt_proto & ndc->l4_hash) {
  236. return skb_get_hash(skb);
  237. } else {
  238. if (flow.basic.n_proto == htons(ETH_P_IP))
  239. hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
  240. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  241. hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
  242. else
  243. return 0;
  244. __skb_set_sw_hash(skb, hash, false);
  245. }
  246. return hash;
  247. }
  248. static inline int netvsc_get_tx_queue(struct net_device *ndev,
  249. struct sk_buff *skb, int old_idx)
  250. {
  251. const struct net_device_context *ndc = netdev_priv(ndev);
  252. struct sock *sk = skb->sk;
  253. int q_idx;
  254. q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
  255. (VRSS_SEND_TAB_SIZE - 1)];
  256. /* If queue index changed record the new value */
  257. if (q_idx != old_idx &&
  258. sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
  259. sk_tx_queue_set(sk, q_idx);
  260. return q_idx;
  261. }
  262. /*
  263. * Select queue for transmit.
  264. *
  265. * If a valid queue has already been assigned, then use that.
  266. * Otherwise compute tx queue based on hash and the send table.
  267. *
  268. * This is basically similar to default (__netdev_pick_tx) with the added step
  269. * of using the host send_table when no other queue has been assigned.
  270. *
  271. * TODO support XPS - but get_xps_queue not exported
  272. */
  273. static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
  274. {
  275. int q_idx = sk_tx_queue_get(skb->sk);
  276. if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
  277. /* If forwarding a packet, we use the recorded queue when
  278. * available for better cache locality.
  279. */
  280. if (skb_rx_queue_recorded(skb))
  281. q_idx = skb_get_rx_queue(skb);
  282. else
  283. q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
  284. }
  285. return q_idx;
  286. }
  287. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  288. struct net_device *sb_dev,
  289. select_queue_fallback_t fallback)
  290. {
  291. struct net_device_context *ndc = netdev_priv(ndev);
  292. struct net_device *vf_netdev;
  293. u16 txq;
  294. rcu_read_lock();
  295. vf_netdev = rcu_dereference(ndc->vf_netdev);
  296. if (vf_netdev) {
  297. const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
  298. if (vf_ops->ndo_select_queue)
  299. txq = vf_ops->ndo_select_queue(vf_netdev, skb,
  300. sb_dev, fallback);
  301. else
  302. txq = fallback(vf_netdev, skb, NULL);
  303. /* Record the queue selected by VF so that it can be
  304. * used for common case where VF has more queues than
  305. * the synthetic device.
  306. */
  307. qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
  308. } else {
  309. txq = netvsc_pick_tx(ndev, skb);
  310. }
  311. rcu_read_unlock();
  312. while (txq >= ndev->real_num_tx_queues)
  313. txq -= ndev->real_num_tx_queues;
  314. return txq;
  315. }
  316. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  317. struct hv_page_buffer *pb)
  318. {
  319. int j = 0;
  320. /* Deal with compund pages by ignoring unused part
  321. * of the page.
  322. */
  323. page += (offset >> PAGE_SHIFT);
  324. offset &= ~PAGE_MASK;
  325. while (len > 0) {
  326. unsigned long bytes;
  327. bytes = PAGE_SIZE - offset;
  328. if (bytes > len)
  329. bytes = len;
  330. pb[j].pfn = page_to_pfn(page);
  331. pb[j].offset = offset;
  332. pb[j].len = bytes;
  333. offset += bytes;
  334. len -= bytes;
  335. if (offset == PAGE_SIZE && len) {
  336. page++;
  337. offset = 0;
  338. j++;
  339. }
  340. }
  341. return j + 1;
  342. }
  343. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  344. struct hv_netvsc_packet *packet,
  345. struct hv_page_buffer *pb)
  346. {
  347. u32 slots_used = 0;
  348. char *data = skb->data;
  349. int frags = skb_shinfo(skb)->nr_frags;
  350. int i;
  351. /* The packet is laid out thus:
  352. * 1. hdr: RNDIS header and PPI
  353. * 2. skb linear data
  354. * 3. skb fragment data
  355. */
  356. slots_used += fill_pg_buf(virt_to_page(hdr),
  357. offset_in_page(hdr),
  358. len, &pb[slots_used]);
  359. packet->rmsg_size = len;
  360. packet->rmsg_pgcnt = slots_used;
  361. slots_used += fill_pg_buf(virt_to_page(data),
  362. offset_in_page(data),
  363. skb_headlen(skb), &pb[slots_used]);
  364. for (i = 0; i < frags; i++) {
  365. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  366. slots_used += fill_pg_buf(skb_frag_page(frag),
  367. frag->page_offset,
  368. skb_frag_size(frag), &pb[slots_used]);
  369. }
  370. return slots_used;
  371. }
  372. static int count_skb_frag_slots(struct sk_buff *skb)
  373. {
  374. int i, frags = skb_shinfo(skb)->nr_frags;
  375. int pages = 0;
  376. for (i = 0; i < frags; i++) {
  377. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  378. unsigned long size = skb_frag_size(frag);
  379. unsigned long offset = frag->page_offset;
  380. /* Skip unused frames from start of page */
  381. offset &= ~PAGE_MASK;
  382. pages += PFN_UP(offset + size);
  383. }
  384. return pages;
  385. }
  386. static int netvsc_get_slots(struct sk_buff *skb)
  387. {
  388. char *data = skb->data;
  389. unsigned int offset = offset_in_page(data);
  390. unsigned int len = skb_headlen(skb);
  391. int slots;
  392. int frag_slots;
  393. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  394. frag_slots = count_skb_frag_slots(skb);
  395. return slots + frag_slots;
  396. }
  397. static u32 net_checksum_info(struct sk_buff *skb)
  398. {
  399. if (skb->protocol == htons(ETH_P_IP)) {
  400. struct iphdr *ip = ip_hdr(skb);
  401. if (ip->protocol == IPPROTO_TCP)
  402. return TRANSPORT_INFO_IPV4_TCP;
  403. else if (ip->protocol == IPPROTO_UDP)
  404. return TRANSPORT_INFO_IPV4_UDP;
  405. } else {
  406. struct ipv6hdr *ip6 = ipv6_hdr(skb);
  407. if (ip6->nexthdr == IPPROTO_TCP)
  408. return TRANSPORT_INFO_IPV6_TCP;
  409. else if (ip6->nexthdr == IPPROTO_UDP)
  410. return TRANSPORT_INFO_IPV6_UDP;
  411. }
  412. return TRANSPORT_INFO_NOT_IP;
  413. }
  414. /* Send skb on the slave VF device. */
  415. static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
  416. struct sk_buff *skb)
  417. {
  418. struct net_device_context *ndev_ctx = netdev_priv(net);
  419. unsigned int len = skb->len;
  420. int rc;
  421. skb->dev = vf_netdev;
  422. skb_record_rx_queue(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  423. rc = dev_queue_xmit(skb);
  424. if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
  425. struct netvsc_vf_pcpu_stats *pcpu_stats
  426. = this_cpu_ptr(ndev_ctx->vf_stats);
  427. u64_stats_update_begin(&pcpu_stats->syncp);
  428. pcpu_stats->tx_packets++;
  429. pcpu_stats->tx_bytes += len;
  430. u64_stats_update_end(&pcpu_stats->syncp);
  431. } else {
  432. this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
  433. }
  434. return rc;
  435. }
  436. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  437. {
  438. struct net_device_context *net_device_ctx = netdev_priv(net);
  439. struct hv_netvsc_packet *packet = NULL;
  440. int ret;
  441. unsigned int num_data_pgs;
  442. struct rndis_message *rndis_msg;
  443. struct net_device *vf_netdev;
  444. u32 rndis_msg_size;
  445. u32 hash;
  446. struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
  447. /* If VF is present and up then redirect packets to it.
  448. * Skip the VF if it is marked down or has no carrier.
  449. * If netpoll is in uses, then VF can not be used either.
  450. */
  451. vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
  452. if (vf_netdev && netif_running(vf_netdev) &&
  453. netif_carrier_ok(vf_netdev) && !netpoll_tx_running(net))
  454. return netvsc_vf_xmit(net, vf_netdev, skb);
  455. /* We will atmost need two pages to describe the rndis
  456. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  457. * of pages in a single packet. If skb is scattered around
  458. * more pages we try linearizing it.
  459. */
  460. num_data_pgs = netvsc_get_slots(skb) + 2;
  461. if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
  462. ++net_device_ctx->eth_stats.tx_scattered;
  463. if (skb_linearize(skb))
  464. goto no_memory;
  465. num_data_pgs = netvsc_get_slots(skb) + 2;
  466. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  467. ++net_device_ctx->eth_stats.tx_too_big;
  468. goto drop;
  469. }
  470. }
  471. /*
  472. * Place the rndis header in the skb head room and
  473. * the skb->cb will be used for hv_netvsc_packet
  474. * structure.
  475. */
  476. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  477. if (ret)
  478. goto no_memory;
  479. /* Use the skb control buffer for building up the packet */
  480. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  481. FIELD_SIZEOF(struct sk_buff, cb));
  482. packet = (struct hv_netvsc_packet *)skb->cb;
  483. packet->q_idx = skb_get_queue_mapping(skb);
  484. packet->total_data_buflen = skb->len;
  485. packet->total_bytes = skb->len;
  486. packet->total_packets = 1;
  487. rndis_msg = (struct rndis_message *)skb->head;
  488. /* Add the rndis header */
  489. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  490. rndis_msg->msg_len = packet->total_data_buflen;
  491. rndis_msg->msg.pkt = (struct rndis_packet) {
  492. .data_offset = sizeof(struct rndis_packet),
  493. .data_len = packet->total_data_buflen,
  494. .per_pkt_info_offset = sizeof(struct rndis_packet),
  495. };
  496. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  497. hash = skb_get_hash_raw(skb);
  498. if (hash != 0 && net->real_num_tx_queues > 1) {
  499. u32 *hash_info;
  500. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  501. hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  502. NBL_HASH_VALUE);
  503. *hash_info = hash;
  504. }
  505. if (skb_vlan_tag_present(skb)) {
  506. struct ndis_pkt_8021q_info *vlan;
  507. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  508. vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  509. IEEE_8021Q_INFO);
  510. vlan->value = 0;
  511. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  512. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  513. VLAN_PRIO_SHIFT;
  514. }
  515. if (skb_is_gso(skb)) {
  516. struct ndis_tcp_lso_info *lso_info;
  517. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  518. lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  519. TCP_LARGESEND_PKTINFO);
  520. lso_info->value = 0;
  521. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  522. if (skb->protocol == htons(ETH_P_IP)) {
  523. lso_info->lso_v2_transmit.ip_version =
  524. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  525. ip_hdr(skb)->tot_len = 0;
  526. ip_hdr(skb)->check = 0;
  527. tcp_hdr(skb)->check =
  528. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  529. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  530. } else {
  531. lso_info->lso_v2_transmit.ip_version =
  532. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  533. ipv6_hdr(skb)->payload_len = 0;
  534. tcp_hdr(skb)->check =
  535. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  536. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  537. }
  538. lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
  539. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  540. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  541. if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
  542. struct ndis_tcp_ip_checksum_info *csum_info;
  543. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  544. csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  545. TCPIP_CHKSUM_PKTINFO);
  546. csum_info->value = 0;
  547. csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
  548. if (skb->protocol == htons(ETH_P_IP)) {
  549. csum_info->transmit.is_ipv4 = 1;
  550. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  551. csum_info->transmit.tcp_checksum = 1;
  552. else
  553. csum_info->transmit.udp_checksum = 1;
  554. } else {
  555. csum_info->transmit.is_ipv6 = 1;
  556. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  557. csum_info->transmit.tcp_checksum = 1;
  558. else
  559. csum_info->transmit.udp_checksum = 1;
  560. }
  561. } else {
  562. /* Can't do offload of this type of checksum */
  563. if (skb_checksum_help(skb))
  564. goto drop;
  565. }
  566. }
  567. /* Start filling in the page buffers with the rndis hdr */
  568. rndis_msg->msg_len += rndis_msg_size;
  569. packet->total_data_buflen = rndis_msg->msg_len;
  570. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  571. skb, packet, pb);
  572. /* timestamp packet in software */
  573. skb_tx_timestamp(skb);
  574. ret = netvsc_send(net, packet, rndis_msg, pb, skb);
  575. if (likely(ret == 0))
  576. return NETDEV_TX_OK;
  577. if (ret == -EAGAIN) {
  578. ++net_device_ctx->eth_stats.tx_busy;
  579. return NETDEV_TX_BUSY;
  580. }
  581. if (ret == -ENOSPC)
  582. ++net_device_ctx->eth_stats.tx_no_space;
  583. drop:
  584. dev_kfree_skb_any(skb);
  585. net->stats.tx_dropped++;
  586. return NETDEV_TX_OK;
  587. no_memory:
  588. ++net_device_ctx->eth_stats.tx_no_memory;
  589. goto drop;
  590. }
  591. /*
  592. * netvsc_linkstatus_callback - Link up/down notification
  593. */
  594. void netvsc_linkstatus_callback(struct net_device *net,
  595. struct rndis_message *resp)
  596. {
  597. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  598. struct net_device_context *ndev_ctx = netdev_priv(net);
  599. struct netvsc_reconfig *event;
  600. unsigned long flags;
  601. /* Update the physical link speed when changing to another vSwitch */
  602. if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
  603. u32 speed;
  604. speed = *(u32 *)((void *)indicate
  605. + indicate->status_buf_offset) / 10000;
  606. ndev_ctx->speed = speed;
  607. return;
  608. }
  609. /* Handle these link change statuses below */
  610. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  611. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  612. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  613. return;
  614. if (net->reg_state != NETREG_REGISTERED)
  615. return;
  616. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  617. if (!event)
  618. return;
  619. event->event = indicate->status;
  620. spin_lock_irqsave(&ndev_ctx->lock, flags);
  621. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  622. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  623. schedule_delayed_work(&ndev_ctx->dwork, 0);
  624. }
  625. static void netvsc_comp_ipcsum(struct sk_buff *skb)
  626. {
  627. struct iphdr *iph = (struct iphdr *)skb->data;
  628. iph->check = 0;
  629. iph->check = ip_fast_csum(iph, iph->ihl);
  630. }
  631. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  632. struct napi_struct *napi,
  633. const struct ndis_tcp_ip_checksum_info *csum_info,
  634. const struct ndis_pkt_8021q_info *vlan,
  635. void *data, u32 buflen)
  636. {
  637. struct sk_buff *skb;
  638. skb = napi_alloc_skb(napi, buflen);
  639. if (!skb)
  640. return skb;
  641. /*
  642. * Copy to skb. This copy is needed here since the memory pointed by
  643. * hv_netvsc_packet cannot be deallocated
  644. */
  645. skb_put_data(skb, data, buflen);
  646. skb->protocol = eth_type_trans(skb, net);
  647. /* skb is already created with CHECKSUM_NONE */
  648. skb_checksum_none_assert(skb);
  649. /* Incoming packets may have IP header checksum verified by the host.
  650. * They may not have IP header checksum computed after coalescing.
  651. * We compute it here if the flags are set, because on Linux, the IP
  652. * checksum is always checked.
  653. */
  654. if (csum_info && csum_info->receive.ip_checksum_value_invalid &&
  655. csum_info->receive.ip_checksum_succeeded &&
  656. skb->protocol == htons(ETH_P_IP))
  657. netvsc_comp_ipcsum(skb);
  658. /* Do L4 checksum offload if enabled and present. */
  659. if (csum_info && (net->features & NETIF_F_RXCSUM)) {
  660. if (csum_info->receive.tcp_checksum_succeeded ||
  661. csum_info->receive.udp_checksum_succeeded)
  662. skb->ip_summed = CHECKSUM_UNNECESSARY;
  663. }
  664. if (vlan) {
  665. u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
  666. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  667. vlan_tci);
  668. }
  669. return skb;
  670. }
  671. /*
  672. * netvsc_recv_callback - Callback when we receive a packet from the
  673. * "wire" on the specified device.
  674. */
  675. int netvsc_recv_callback(struct net_device *net,
  676. struct netvsc_device *net_device,
  677. struct vmbus_channel *channel,
  678. void *data, u32 len,
  679. const struct ndis_tcp_ip_checksum_info *csum_info,
  680. const struct ndis_pkt_8021q_info *vlan)
  681. {
  682. struct net_device_context *net_device_ctx = netdev_priv(net);
  683. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  684. struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
  685. struct sk_buff *skb;
  686. struct netvsc_stats *rx_stats;
  687. if (net->reg_state != NETREG_REGISTERED)
  688. return NVSP_STAT_FAIL;
  689. /* Allocate a skb - TODO direct I/O to pages? */
  690. skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
  691. csum_info, vlan, data, len);
  692. if (unlikely(!skb)) {
  693. ++net_device_ctx->eth_stats.rx_no_memory;
  694. return NVSP_STAT_FAIL;
  695. }
  696. skb_record_rx_queue(skb, q_idx);
  697. /*
  698. * Even if injecting the packet, record the statistics
  699. * on the synthetic device because modifying the VF device
  700. * statistics will not work correctly.
  701. */
  702. rx_stats = &nvchan->rx_stats;
  703. u64_stats_update_begin(&rx_stats->syncp);
  704. rx_stats->packets++;
  705. rx_stats->bytes += len;
  706. if (skb->pkt_type == PACKET_BROADCAST)
  707. ++rx_stats->broadcast;
  708. else if (skb->pkt_type == PACKET_MULTICAST)
  709. ++rx_stats->multicast;
  710. u64_stats_update_end(&rx_stats->syncp);
  711. napi_gro_receive(&nvchan->napi, skb);
  712. return NVSP_STAT_SUCCESS;
  713. }
  714. static void netvsc_get_drvinfo(struct net_device *net,
  715. struct ethtool_drvinfo *info)
  716. {
  717. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  718. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  719. }
  720. static void netvsc_get_channels(struct net_device *net,
  721. struct ethtool_channels *channel)
  722. {
  723. struct net_device_context *net_device_ctx = netdev_priv(net);
  724. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  725. if (nvdev) {
  726. channel->max_combined = nvdev->max_chn;
  727. channel->combined_count = nvdev->num_chn;
  728. }
  729. }
  730. /* Alloc struct netvsc_device_info, and initialize it from either existing
  731. * struct netvsc_device, or from default values.
  732. */
  733. static struct netvsc_device_info *netvsc_devinfo_get
  734. (struct netvsc_device *nvdev)
  735. {
  736. struct netvsc_device_info *dev_info;
  737. dev_info = kzalloc(sizeof(*dev_info), GFP_ATOMIC);
  738. if (!dev_info)
  739. return NULL;
  740. if (nvdev) {
  741. dev_info->num_chn = nvdev->num_chn;
  742. dev_info->send_sections = nvdev->send_section_cnt;
  743. dev_info->send_section_size = nvdev->send_section_size;
  744. dev_info->recv_sections = nvdev->recv_section_cnt;
  745. dev_info->recv_section_size = nvdev->recv_section_size;
  746. memcpy(dev_info->rss_key, nvdev->extension->rss_key,
  747. NETVSC_HASH_KEYLEN);
  748. } else {
  749. dev_info->num_chn = VRSS_CHANNEL_DEFAULT;
  750. dev_info->send_sections = NETVSC_DEFAULT_TX;
  751. dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
  752. dev_info->recv_sections = NETVSC_DEFAULT_RX;
  753. dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
  754. }
  755. return dev_info;
  756. }
  757. static int netvsc_detach(struct net_device *ndev,
  758. struct netvsc_device *nvdev)
  759. {
  760. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  761. struct hv_device *hdev = ndev_ctx->device_ctx;
  762. int ret;
  763. /* Don't try continuing to try and setup sub channels */
  764. if (cancel_work_sync(&nvdev->subchan_work))
  765. nvdev->num_chn = 1;
  766. /* If device was up (receiving) then shutdown */
  767. if (netif_running(ndev)) {
  768. netvsc_tx_disable(nvdev, ndev);
  769. ret = rndis_filter_close(nvdev);
  770. if (ret) {
  771. netdev_err(ndev,
  772. "unable to close device (ret %d).\n", ret);
  773. return ret;
  774. }
  775. ret = netvsc_wait_until_empty(nvdev);
  776. if (ret) {
  777. netdev_err(ndev,
  778. "Ring buffer not empty after closing rndis\n");
  779. return ret;
  780. }
  781. }
  782. netif_device_detach(ndev);
  783. rndis_filter_device_remove(hdev, nvdev);
  784. return 0;
  785. }
  786. static int netvsc_attach(struct net_device *ndev,
  787. struct netvsc_device_info *dev_info)
  788. {
  789. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  790. struct hv_device *hdev = ndev_ctx->device_ctx;
  791. struct netvsc_device *nvdev;
  792. struct rndis_device *rdev;
  793. int ret;
  794. nvdev = rndis_filter_device_add(hdev, dev_info);
  795. if (IS_ERR(nvdev))
  796. return PTR_ERR(nvdev);
  797. if (nvdev->num_chn > 1) {
  798. ret = rndis_set_subchannel(ndev, nvdev, dev_info);
  799. /* if unavailable, just proceed with one queue */
  800. if (ret) {
  801. nvdev->max_chn = 1;
  802. nvdev->num_chn = 1;
  803. }
  804. }
  805. /* In any case device is now ready */
  806. nvdev->tx_disable = false;
  807. netif_device_attach(ndev);
  808. /* Note: enable and attach happen when sub-channels setup */
  809. netif_carrier_off(ndev);
  810. if (netif_running(ndev)) {
  811. ret = rndis_filter_open(nvdev);
  812. if (ret)
  813. goto err;
  814. rdev = nvdev->extension;
  815. if (!rdev->link_state)
  816. netif_carrier_on(ndev);
  817. }
  818. return 0;
  819. err:
  820. netif_device_detach(ndev);
  821. rndis_filter_device_remove(hdev, nvdev);
  822. return ret;
  823. }
  824. static int netvsc_set_channels(struct net_device *net,
  825. struct ethtool_channels *channels)
  826. {
  827. struct net_device_context *net_device_ctx = netdev_priv(net);
  828. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  829. unsigned int orig, count = channels->combined_count;
  830. struct netvsc_device_info *device_info;
  831. int ret;
  832. /* We do not support separate count for rx, tx, or other */
  833. if (count == 0 ||
  834. channels->rx_count || channels->tx_count || channels->other_count)
  835. return -EINVAL;
  836. if (!nvdev || nvdev->destroy)
  837. return -ENODEV;
  838. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  839. return -EINVAL;
  840. if (count > nvdev->max_chn)
  841. return -EINVAL;
  842. orig = nvdev->num_chn;
  843. device_info = netvsc_devinfo_get(nvdev);
  844. if (!device_info)
  845. return -ENOMEM;
  846. device_info->num_chn = count;
  847. ret = netvsc_detach(net, nvdev);
  848. if (ret)
  849. goto out;
  850. ret = netvsc_attach(net, device_info);
  851. if (ret) {
  852. device_info->num_chn = orig;
  853. if (netvsc_attach(net, device_info))
  854. netdev_err(net, "restoring channel setting failed\n");
  855. }
  856. out:
  857. kfree(device_info);
  858. return ret;
  859. }
  860. static bool
  861. netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
  862. {
  863. struct ethtool_link_ksettings diff1 = *cmd;
  864. struct ethtool_link_ksettings diff2 = {};
  865. diff1.base.speed = 0;
  866. diff1.base.duplex = 0;
  867. /* advertising and cmd are usually set */
  868. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  869. diff1.base.cmd = 0;
  870. /* We set port to PORT_OTHER */
  871. diff2.base.port = PORT_OTHER;
  872. return !memcmp(&diff1, &diff2, sizeof(diff1));
  873. }
  874. static void netvsc_init_settings(struct net_device *dev)
  875. {
  876. struct net_device_context *ndc = netdev_priv(dev);
  877. ndc->l4_hash = HV_DEFAULT_L4HASH;
  878. ndc->speed = SPEED_UNKNOWN;
  879. ndc->duplex = DUPLEX_FULL;
  880. }
  881. static int netvsc_get_link_ksettings(struct net_device *dev,
  882. struct ethtool_link_ksettings *cmd)
  883. {
  884. struct net_device_context *ndc = netdev_priv(dev);
  885. cmd->base.speed = ndc->speed;
  886. cmd->base.duplex = ndc->duplex;
  887. cmd->base.port = PORT_OTHER;
  888. return 0;
  889. }
  890. static int netvsc_set_link_ksettings(struct net_device *dev,
  891. const struct ethtool_link_ksettings *cmd)
  892. {
  893. struct net_device_context *ndc = netdev_priv(dev);
  894. u32 speed;
  895. speed = cmd->base.speed;
  896. if (!ethtool_validate_speed(speed) ||
  897. !ethtool_validate_duplex(cmd->base.duplex) ||
  898. !netvsc_validate_ethtool_ss_cmd(cmd))
  899. return -EINVAL;
  900. ndc->speed = speed;
  901. ndc->duplex = cmd->base.duplex;
  902. return 0;
  903. }
  904. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  905. {
  906. struct net_device_context *ndevctx = netdev_priv(ndev);
  907. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  908. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  909. int orig_mtu = ndev->mtu;
  910. struct netvsc_device_info *device_info;
  911. int ret = 0;
  912. if (!nvdev || nvdev->destroy)
  913. return -ENODEV;
  914. device_info = netvsc_devinfo_get(nvdev);
  915. if (!device_info)
  916. return -ENOMEM;
  917. /* Change MTU of underlying VF netdev first. */
  918. if (vf_netdev) {
  919. ret = dev_set_mtu(vf_netdev, mtu);
  920. if (ret)
  921. goto out;
  922. }
  923. ret = netvsc_detach(ndev, nvdev);
  924. if (ret)
  925. goto rollback_vf;
  926. ndev->mtu = mtu;
  927. ret = netvsc_attach(ndev, device_info);
  928. if (!ret)
  929. goto out;
  930. /* Attempt rollback to original MTU */
  931. ndev->mtu = orig_mtu;
  932. if (netvsc_attach(ndev, device_info))
  933. netdev_err(ndev, "restoring mtu failed\n");
  934. rollback_vf:
  935. if (vf_netdev)
  936. dev_set_mtu(vf_netdev, orig_mtu);
  937. out:
  938. kfree(device_info);
  939. return ret;
  940. }
  941. static void netvsc_get_vf_stats(struct net_device *net,
  942. struct netvsc_vf_pcpu_stats *tot)
  943. {
  944. struct net_device_context *ndev_ctx = netdev_priv(net);
  945. int i;
  946. memset(tot, 0, sizeof(*tot));
  947. for_each_possible_cpu(i) {
  948. const struct netvsc_vf_pcpu_stats *stats
  949. = per_cpu_ptr(ndev_ctx->vf_stats, i);
  950. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  951. unsigned int start;
  952. do {
  953. start = u64_stats_fetch_begin_irq(&stats->syncp);
  954. rx_packets = stats->rx_packets;
  955. tx_packets = stats->tx_packets;
  956. rx_bytes = stats->rx_bytes;
  957. tx_bytes = stats->tx_bytes;
  958. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  959. tot->rx_packets += rx_packets;
  960. tot->tx_packets += tx_packets;
  961. tot->rx_bytes += rx_bytes;
  962. tot->tx_bytes += tx_bytes;
  963. tot->tx_dropped += stats->tx_dropped;
  964. }
  965. }
  966. static void netvsc_get_pcpu_stats(struct net_device *net,
  967. struct netvsc_ethtool_pcpu_stats *pcpu_tot)
  968. {
  969. struct net_device_context *ndev_ctx = netdev_priv(net);
  970. struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
  971. int i;
  972. /* fetch percpu stats of vf */
  973. for_each_possible_cpu(i) {
  974. const struct netvsc_vf_pcpu_stats *stats =
  975. per_cpu_ptr(ndev_ctx->vf_stats, i);
  976. struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
  977. unsigned int start;
  978. do {
  979. start = u64_stats_fetch_begin_irq(&stats->syncp);
  980. this_tot->vf_rx_packets = stats->rx_packets;
  981. this_tot->vf_tx_packets = stats->tx_packets;
  982. this_tot->vf_rx_bytes = stats->rx_bytes;
  983. this_tot->vf_tx_bytes = stats->tx_bytes;
  984. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  985. this_tot->rx_packets = this_tot->vf_rx_packets;
  986. this_tot->tx_packets = this_tot->vf_tx_packets;
  987. this_tot->rx_bytes = this_tot->vf_rx_bytes;
  988. this_tot->tx_bytes = this_tot->vf_tx_bytes;
  989. }
  990. /* fetch percpu stats of netvsc */
  991. for (i = 0; i < nvdev->num_chn; i++) {
  992. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  993. const struct netvsc_stats *stats;
  994. struct netvsc_ethtool_pcpu_stats *this_tot =
  995. &pcpu_tot[nvchan->channel->target_cpu];
  996. u64 packets, bytes;
  997. unsigned int start;
  998. stats = &nvchan->tx_stats;
  999. do {
  1000. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1001. packets = stats->packets;
  1002. bytes = stats->bytes;
  1003. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1004. this_tot->tx_bytes += bytes;
  1005. this_tot->tx_packets += packets;
  1006. stats = &nvchan->rx_stats;
  1007. do {
  1008. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1009. packets = stats->packets;
  1010. bytes = stats->bytes;
  1011. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1012. this_tot->rx_bytes += bytes;
  1013. this_tot->rx_packets += packets;
  1014. }
  1015. }
  1016. static void netvsc_get_stats64(struct net_device *net,
  1017. struct rtnl_link_stats64 *t)
  1018. {
  1019. struct net_device_context *ndev_ctx = netdev_priv(net);
  1020. struct netvsc_device *nvdev;
  1021. struct netvsc_vf_pcpu_stats vf_tot;
  1022. int i;
  1023. rcu_read_lock();
  1024. nvdev = rcu_dereference(ndev_ctx->nvdev);
  1025. if (!nvdev)
  1026. goto out;
  1027. netdev_stats_to_stats64(t, &net->stats);
  1028. netvsc_get_vf_stats(net, &vf_tot);
  1029. t->rx_packets += vf_tot.rx_packets;
  1030. t->tx_packets += vf_tot.tx_packets;
  1031. t->rx_bytes += vf_tot.rx_bytes;
  1032. t->tx_bytes += vf_tot.tx_bytes;
  1033. t->tx_dropped += vf_tot.tx_dropped;
  1034. for (i = 0; i < nvdev->num_chn; i++) {
  1035. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  1036. const struct netvsc_stats *stats;
  1037. u64 packets, bytes, multicast;
  1038. unsigned int start;
  1039. stats = &nvchan->tx_stats;
  1040. do {
  1041. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1042. packets = stats->packets;
  1043. bytes = stats->bytes;
  1044. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1045. t->tx_bytes += bytes;
  1046. t->tx_packets += packets;
  1047. stats = &nvchan->rx_stats;
  1048. do {
  1049. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1050. packets = stats->packets;
  1051. bytes = stats->bytes;
  1052. multicast = stats->multicast + stats->broadcast;
  1053. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1054. t->rx_bytes += bytes;
  1055. t->rx_packets += packets;
  1056. t->multicast += multicast;
  1057. }
  1058. out:
  1059. rcu_read_unlock();
  1060. }
  1061. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  1062. {
  1063. struct net_device_context *ndc = netdev_priv(ndev);
  1064. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  1065. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1066. struct sockaddr *addr = p;
  1067. int err;
  1068. err = eth_prepare_mac_addr_change(ndev, p);
  1069. if (err)
  1070. return err;
  1071. if (!nvdev)
  1072. return -ENODEV;
  1073. if (vf_netdev) {
  1074. err = dev_set_mac_address(vf_netdev, addr);
  1075. if (err)
  1076. return err;
  1077. }
  1078. err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
  1079. if (!err) {
  1080. eth_commit_mac_addr_change(ndev, p);
  1081. } else if (vf_netdev) {
  1082. /* rollback change on VF */
  1083. memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
  1084. dev_set_mac_address(vf_netdev, addr);
  1085. }
  1086. return err;
  1087. }
  1088. static const struct {
  1089. char name[ETH_GSTRING_LEN];
  1090. u16 offset;
  1091. } netvsc_stats[] = {
  1092. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  1093. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  1094. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  1095. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  1096. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  1097. { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
  1098. { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
  1099. { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
  1100. { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
  1101. { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
  1102. }, pcpu_stats[] = {
  1103. { "cpu%u_rx_packets",
  1104. offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
  1105. { "cpu%u_rx_bytes",
  1106. offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
  1107. { "cpu%u_tx_packets",
  1108. offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
  1109. { "cpu%u_tx_bytes",
  1110. offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
  1111. { "cpu%u_vf_rx_packets",
  1112. offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
  1113. { "cpu%u_vf_rx_bytes",
  1114. offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
  1115. { "cpu%u_vf_tx_packets",
  1116. offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
  1117. { "cpu%u_vf_tx_bytes",
  1118. offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
  1119. }, vf_stats[] = {
  1120. { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
  1121. { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
  1122. { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
  1123. { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
  1124. { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
  1125. };
  1126. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  1127. #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
  1128. /* statistics per queue (rx/tx packets/bytes) */
  1129. #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
  1130. /* 4 statistics per queue (rx/tx packets/bytes) */
  1131. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
  1132. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  1133. {
  1134. struct net_device_context *ndc = netdev_priv(dev);
  1135. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1136. if (!nvdev)
  1137. return -ENODEV;
  1138. switch (string_set) {
  1139. case ETH_SS_STATS:
  1140. return NETVSC_GLOBAL_STATS_LEN
  1141. + NETVSC_VF_STATS_LEN
  1142. + NETVSC_QUEUE_STATS_LEN(nvdev)
  1143. + NETVSC_PCPU_STATS_LEN;
  1144. default:
  1145. return -EINVAL;
  1146. }
  1147. }
  1148. static void netvsc_get_ethtool_stats(struct net_device *dev,
  1149. struct ethtool_stats *stats, u64 *data)
  1150. {
  1151. struct net_device_context *ndc = netdev_priv(dev);
  1152. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1153. const void *nds = &ndc->eth_stats;
  1154. const struct netvsc_stats *qstats;
  1155. struct netvsc_vf_pcpu_stats sum;
  1156. struct netvsc_ethtool_pcpu_stats *pcpu_sum;
  1157. unsigned int start;
  1158. u64 packets, bytes;
  1159. int i, j, cpu;
  1160. if (!nvdev)
  1161. return;
  1162. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  1163. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  1164. netvsc_get_vf_stats(dev, &sum);
  1165. for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
  1166. data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
  1167. for (j = 0; j < nvdev->num_chn; j++) {
  1168. qstats = &nvdev->chan_table[j].tx_stats;
  1169. do {
  1170. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1171. packets = qstats->packets;
  1172. bytes = qstats->bytes;
  1173. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1174. data[i++] = packets;
  1175. data[i++] = bytes;
  1176. qstats = &nvdev->chan_table[j].rx_stats;
  1177. do {
  1178. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1179. packets = qstats->packets;
  1180. bytes = qstats->bytes;
  1181. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1182. data[i++] = packets;
  1183. data[i++] = bytes;
  1184. }
  1185. pcpu_sum = kvmalloc_array(num_possible_cpus(),
  1186. sizeof(struct netvsc_ethtool_pcpu_stats),
  1187. GFP_KERNEL);
  1188. netvsc_get_pcpu_stats(dev, pcpu_sum);
  1189. for_each_present_cpu(cpu) {
  1190. struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
  1191. for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
  1192. data[i++] = *(u64 *)((void *)this_sum
  1193. + pcpu_stats[j].offset);
  1194. }
  1195. kvfree(pcpu_sum);
  1196. }
  1197. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1198. {
  1199. struct net_device_context *ndc = netdev_priv(dev);
  1200. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1201. u8 *p = data;
  1202. int i, cpu;
  1203. if (!nvdev)
  1204. return;
  1205. switch (stringset) {
  1206. case ETH_SS_STATS:
  1207. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
  1208. memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
  1209. p += ETH_GSTRING_LEN;
  1210. }
  1211. for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
  1212. memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
  1213. p += ETH_GSTRING_LEN;
  1214. }
  1215. for (i = 0; i < nvdev->num_chn; i++) {
  1216. sprintf(p, "tx_queue_%u_packets", i);
  1217. p += ETH_GSTRING_LEN;
  1218. sprintf(p, "tx_queue_%u_bytes", i);
  1219. p += ETH_GSTRING_LEN;
  1220. sprintf(p, "rx_queue_%u_packets", i);
  1221. p += ETH_GSTRING_LEN;
  1222. sprintf(p, "rx_queue_%u_bytes", i);
  1223. p += ETH_GSTRING_LEN;
  1224. }
  1225. for_each_present_cpu(cpu) {
  1226. for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++) {
  1227. sprintf(p, pcpu_stats[i].name, cpu);
  1228. p += ETH_GSTRING_LEN;
  1229. }
  1230. }
  1231. break;
  1232. }
  1233. }
  1234. static int
  1235. netvsc_get_rss_hash_opts(struct net_device_context *ndc,
  1236. struct ethtool_rxnfc *info)
  1237. {
  1238. const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1239. info->data = RXH_IP_SRC | RXH_IP_DST;
  1240. switch (info->flow_type) {
  1241. case TCP_V4_FLOW:
  1242. if (ndc->l4_hash & HV_TCP4_L4HASH)
  1243. info->data |= l4_flag;
  1244. break;
  1245. case TCP_V6_FLOW:
  1246. if (ndc->l4_hash & HV_TCP6_L4HASH)
  1247. info->data |= l4_flag;
  1248. break;
  1249. case UDP_V4_FLOW:
  1250. if (ndc->l4_hash & HV_UDP4_L4HASH)
  1251. info->data |= l4_flag;
  1252. break;
  1253. case UDP_V6_FLOW:
  1254. if (ndc->l4_hash & HV_UDP6_L4HASH)
  1255. info->data |= l4_flag;
  1256. break;
  1257. case IPV4_FLOW:
  1258. case IPV6_FLOW:
  1259. break;
  1260. default:
  1261. info->data = 0;
  1262. break;
  1263. }
  1264. return 0;
  1265. }
  1266. static int
  1267. netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  1268. u32 *rules)
  1269. {
  1270. struct net_device_context *ndc = netdev_priv(dev);
  1271. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1272. if (!nvdev)
  1273. return -ENODEV;
  1274. switch (info->cmd) {
  1275. case ETHTOOL_GRXRINGS:
  1276. info->data = nvdev->num_chn;
  1277. return 0;
  1278. case ETHTOOL_GRXFH:
  1279. return netvsc_get_rss_hash_opts(ndc, info);
  1280. }
  1281. return -EOPNOTSUPP;
  1282. }
  1283. static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
  1284. struct ethtool_rxnfc *info)
  1285. {
  1286. if (info->data == (RXH_IP_SRC | RXH_IP_DST |
  1287. RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  1288. switch (info->flow_type) {
  1289. case TCP_V4_FLOW:
  1290. ndc->l4_hash |= HV_TCP4_L4HASH;
  1291. break;
  1292. case TCP_V6_FLOW:
  1293. ndc->l4_hash |= HV_TCP6_L4HASH;
  1294. break;
  1295. case UDP_V4_FLOW:
  1296. ndc->l4_hash |= HV_UDP4_L4HASH;
  1297. break;
  1298. case UDP_V6_FLOW:
  1299. ndc->l4_hash |= HV_UDP6_L4HASH;
  1300. break;
  1301. default:
  1302. return -EOPNOTSUPP;
  1303. }
  1304. return 0;
  1305. }
  1306. if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
  1307. switch (info->flow_type) {
  1308. case TCP_V4_FLOW:
  1309. ndc->l4_hash &= ~HV_TCP4_L4HASH;
  1310. break;
  1311. case TCP_V6_FLOW:
  1312. ndc->l4_hash &= ~HV_TCP6_L4HASH;
  1313. break;
  1314. case UDP_V4_FLOW:
  1315. ndc->l4_hash &= ~HV_UDP4_L4HASH;
  1316. break;
  1317. case UDP_V6_FLOW:
  1318. ndc->l4_hash &= ~HV_UDP6_L4HASH;
  1319. break;
  1320. default:
  1321. return -EOPNOTSUPP;
  1322. }
  1323. return 0;
  1324. }
  1325. return -EOPNOTSUPP;
  1326. }
  1327. static int
  1328. netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
  1329. {
  1330. struct net_device_context *ndc = netdev_priv(ndev);
  1331. if (info->cmd == ETHTOOL_SRXFH)
  1332. return netvsc_set_rss_hash_opts(ndc, info);
  1333. return -EOPNOTSUPP;
  1334. }
  1335. #ifdef CONFIG_NET_POLL_CONTROLLER
  1336. static void netvsc_poll_controller(struct net_device *dev)
  1337. {
  1338. struct net_device_context *ndc = netdev_priv(dev);
  1339. struct netvsc_device *ndev;
  1340. int i;
  1341. rcu_read_lock();
  1342. ndev = rcu_dereference(ndc->nvdev);
  1343. if (ndev) {
  1344. for (i = 0; i < ndev->num_chn; i++) {
  1345. struct netvsc_channel *nvchan = &ndev->chan_table[i];
  1346. napi_schedule(&nvchan->napi);
  1347. }
  1348. }
  1349. rcu_read_unlock();
  1350. }
  1351. #endif
  1352. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  1353. {
  1354. return NETVSC_HASH_KEYLEN;
  1355. }
  1356. static u32 netvsc_rss_indir_size(struct net_device *dev)
  1357. {
  1358. return ITAB_NUM;
  1359. }
  1360. static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  1361. u8 *hfunc)
  1362. {
  1363. struct net_device_context *ndc = netdev_priv(dev);
  1364. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1365. struct rndis_device *rndis_dev;
  1366. int i;
  1367. if (!ndev)
  1368. return -ENODEV;
  1369. if (hfunc)
  1370. *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  1371. rndis_dev = ndev->extension;
  1372. if (indir) {
  1373. for (i = 0; i < ITAB_NUM; i++)
  1374. indir[i] = ndc->rx_table[i];
  1375. }
  1376. if (key)
  1377. memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  1378. return 0;
  1379. }
  1380. static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
  1381. const u8 *key, const u8 hfunc)
  1382. {
  1383. struct net_device_context *ndc = netdev_priv(dev);
  1384. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1385. struct rndis_device *rndis_dev;
  1386. int i;
  1387. if (!ndev)
  1388. return -ENODEV;
  1389. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  1390. return -EOPNOTSUPP;
  1391. rndis_dev = ndev->extension;
  1392. if (indir) {
  1393. for (i = 0; i < ITAB_NUM; i++)
  1394. if (indir[i] >= ndev->num_chn)
  1395. return -EINVAL;
  1396. for (i = 0; i < ITAB_NUM; i++)
  1397. ndc->rx_table[i] = indir[i];
  1398. }
  1399. if (!key) {
  1400. if (!indir)
  1401. return 0;
  1402. key = rndis_dev->rss_key;
  1403. }
  1404. return rndis_filter_set_rss_param(rndis_dev, key);
  1405. }
  1406. /* Hyper-V RNDIS protocol does not have ring in the HW sense.
  1407. * It does have pre-allocated receive area which is divided into sections.
  1408. */
  1409. static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
  1410. struct ethtool_ringparam *ring)
  1411. {
  1412. u32 max_buf_size;
  1413. ring->rx_pending = nvdev->recv_section_cnt;
  1414. ring->tx_pending = nvdev->send_section_cnt;
  1415. if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  1416. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  1417. else
  1418. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  1419. ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
  1420. ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
  1421. / nvdev->send_section_size;
  1422. }
  1423. static void netvsc_get_ringparam(struct net_device *ndev,
  1424. struct ethtool_ringparam *ring)
  1425. {
  1426. struct net_device_context *ndevctx = netdev_priv(ndev);
  1427. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1428. if (!nvdev)
  1429. return;
  1430. __netvsc_get_ringparam(nvdev, ring);
  1431. }
  1432. static int netvsc_set_ringparam(struct net_device *ndev,
  1433. struct ethtool_ringparam *ring)
  1434. {
  1435. struct net_device_context *ndevctx = netdev_priv(ndev);
  1436. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1437. struct netvsc_device_info *device_info;
  1438. struct ethtool_ringparam orig;
  1439. u32 new_tx, new_rx;
  1440. int ret = 0;
  1441. if (!nvdev || nvdev->destroy)
  1442. return -ENODEV;
  1443. memset(&orig, 0, sizeof(orig));
  1444. __netvsc_get_ringparam(nvdev, &orig);
  1445. new_tx = clamp_t(u32, ring->tx_pending,
  1446. NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
  1447. new_rx = clamp_t(u32, ring->rx_pending,
  1448. NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
  1449. if (new_tx == orig.tx_pending &&
  1450. new_rx == orig.rx_pending)
  1451. return 0; /* no change */
  1452. device_info = netvsc_devinfo_get(nvdev);
  1453. if (!device_info)
  1454. return -ENOMEM;
  1455. device_info->send_sections = new_tx;
  1456. device_info->recv_sections = new_rx;
  1457. ret = netvsc_detach(ndev, nvdev);
  1458. if (ret)
  1459. goto out;
  1460. ret = netvsc_attach(ndev, device_info);
  1461. if (ret) {
  1462. device_info->send_sections = orig.tx_pending;
  1463. device_info->recv_sections = orig.rx_pending;
  1464. if (netvsc_attach(ndev, device_info))
  1465. netdev_err(ndev, "restoring ringparam failed");
  1466. }
  1467. out:
  1468. kfree(device_info);
  1469. return ret;
  1470. }
  1471. static u32 netvsc_get_msglevel(struct net_device *ndev)
  1472. {
  1473. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1474. return ndev_ctx->msg_enable;
  1475. }
  1476. static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
  1477. {
  1478. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1479. ndev_ctx->msg_enable = val;
  1480. }
  1481. static const struct ethtool_ops ethtool_ops = {
  1482. .get_drvinfo = netvsc_get_drvinfo,
  1483. .get_msglevel = netvsc_get_msglevel,
  1484. .set_msglevel = netvsc_set_msglevel,
  1485. .get_link = ethtool_op_get_link,
  1486. .get_ethtool_stats = netvsc_get_ethtool_stats,
  1487. .get_sset_count = netvsc_get_sset_count,
  1488. .get_strings = netvsc_get_strings,
  1489. .get_channels = netvsc_get_channels,
  1490. .set_channels = netvsc_set_channels,
  1491. .get_ts_info = ethtool_op_get_ts_info,
  1492. .get_rxnfc = netvsc_get_rxnfc,
  1493. .set_rxnfc = netvsc_set_rxnfc,
  1494. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  1495. .get_rxfh_indir_size = netvsc_rss_indir_size,
  1496. .get_rxfh = netvsc_get_rxfh,
  1497. .set_rxfh = netvsc_set_rxfh,
  1498. .get_link_ksettings = netvsc_get_link_ksettings,
  1499. .set_link_ksettings = netvsc_set_link_ksettings,
  1500. .get_ringparam = netvsc_get_ringparam,
  1501. .set_ringparam = netvsc_set_ringparam,
  1502. };
  1503. static const struct net_device_ops device_ops = {
  1504. .ndo_open = netvsc_open,
  1505. .ndo_stop = netvsc_close,
  1506. .ndo_start_xmit = netvsc_start_xmit,
  1507. .ndo_change_rx_flags = netvsc_change_rx_flags,
  1508. .ndo_set_rx_mode = netvsc_set_rx_mode,
  1509. .ndo_change_mtu = netvsc_change_mtu,
  1510. .ndo_validate_addr = eth_validate_addr,
  1511. .ndo_set_mac_address = netvsc_set_mac_addr,
  1512. .ndo_select_queue = netvsc_select_queue,
  1513. .ndo_get_stats64 = netvsc_get_stats64,
  1514. #ifdef CONFIG_NET_POLL_CONTROLLER
  1515. .ndo_poll_controller = netvsc_poll_controller,
  1516. #endif
  1517. };
  1518. /*
  1519. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  1520. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  1521. * present send GARP packet to network peers with netif_notify_peers().
  1522. */
  1523. static void netvsc_link_change(struct work_struct *w)
  1524. {
  1525. struct net_device_context *ndev_ctx =
  1526. container_of(w, struct net_device_context, dwork.work);
  1527. struct hv_device *device_obj = ndev_ctx->device_ctx;
  1528. struct net_device *net = hv_get_drvdata(device_obj);
  1529. struct netvsc_device *net_device;
  1530. struct rndis_device *rdev;
  1531. struct netvsc_reconfig *event = NULL;
  1532. bool notify = false, reschedule = false;
  1533. unsigned long flags, next_reconfig, delay;
  1534. /* if changes are happening, comeback later */
  1535. if (!rtnl_trylock()) {
  1536. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1537. return;
  1538. }
  1539. net_device = rtnl_dereference(ndev_ctx->nvdev);
  1540. if (!net_device)
  1541. goto out_unlock;
  1542. rdev = net_device->extension;
  1543. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  1544. if (time_is_after_jiffies(next_reconfig)) {
  1545. /* link_watch only sends one notification with current state
  1546. * per second, avoid doing reconfig more frequently. Handle
  1547. * wrap around.
  1548. */
  1549. delay = next_reconfig - jiffies;
  1550. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1551. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1552. goto out_unlock;
  1553. }
  1554. ndev_ctx->last_reconfig = jiffies;
  1555. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1556. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1557. event = list_first_entry(&ndev_ctx->reconfig_events,
  1558. struct netvsc_reconfig, list);
  1559. list_del(&event->list);
  1560. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1561. }
  1562. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1563. if (!event)
  1564. goto out_unlock;
  1565. switch (event->event) {
  1566. /* Only the following events are possible due to the check in
  1567. * netvsc_linkstatus_callback()
  1568. */
  1569. case RNDIS_STATUS_MEDIA_CONNECT:
  1570. if (rdev->link_state) {
  1571. rdev->link_state = false;
  1572. netif_carrier_on(net);
  1573. netvsc_tx_enable(net_device, net);
  1574. } else {
  1575. notify = true;
  1576. }
  1577. kfree(event);
  1578. break;
  1579. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1580. if (!rdev->link_state) {
  1581. rdev->link_state = true;
  1582. netif_carrier_off(net);
  1583. netvsc_tx_disable(net_device, net);
  1584. }
  1585. kfree(event);
  1586. break;
  1587. case RNDIS_STATUS_NETWORK_CHANGE:
  1588. /* Only makes sense if carrier is present */
  1589. if (!rdev->link_state) {
  1590. rdev->link_state = true;
  1591. netif_carrier_off(net);
  1592. netvsc_tx_disable(net_device, net);
  1593. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1594. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1595. list_add(&event->list, &ndev_ctx->reconfig_events);
  1596. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1597. reschedule = true;
  1598. }
  1599. break;
  1600. }
  1601. rtnl_unlock();
  1602. if (notify)
  1603. netdev_notify_peers(net);
  1604. /* link_watch only sends one notification with current state per
  1605. * second, handle next reconfig event in 2 seconds.
  1606. */
  1607. if (reschedule)
  1608. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1609. return;
  1610. out_unlock:
  1611. rtnl_unlock();
  1612. }
  1613. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  1614. {
  1615. struct net_device_context *net_device_ctx;
  1616. struct net_device *dev;
  1617. dev = netdev_master_upper_dev_get(vf_netdev);
  1618. if (!dev || dev->netdev_ops != &device_ops)
  1619. return NULL; /* not a netvsc device */
  1620. net_device_ctx = netdev_priv(dev);
  1621. if (!rtnl_dereference(net_device_ctx->nvdev))
  1622. return NULL; /* device is removed */
  1623. return dev;
  1624. }
  1625. /* Called when VF is injecting data into network stack.
  1626. * Change the associated network device from VF to netvsc.
  1627. * note: already called with rcu_read_lock
  1628. */
  1629. static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
  1630. {
  1631. struct sk_buff *skb = *pskb;
  1632. struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
  1633. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1634. struct netvsc_vf_pcpu_stats *pcpu_stats
  1635. = this_cpu_ptr(ndev_ctx->vf_stats);
  1636. skb = skb_share_check(skb, GFP_ATOMIC);
  1637. if (unlikely(!skb))
  1638. return RX_HANDLER_CONSUMED;
  1639. *pskb = skb;
  1640. skb->dev = ndev;
  1641. u64_stats_update_begin(&pcpu_stats->syncp);
  1642. pcpu_stats->rx_packets++;
  1643. pcpu_stats->rx_bytes += skb->len;
  1644. u64_stats_update_end(&pcpu_stats->syncp);
  1645. return RX_HANDLER_ANOTHER;
  1646. }
  1647. static int netvsc_vf_join(struct net_device *vf_netdev,
  1648. struct net_device *ndev)
  1649. {
  1650. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1651. int ret;
  1652. ret = netdev_rx_handler_register(vf_netdev,
  1653. netvsc_vf_handle_frame, ndev);
  1654. if (ret != 0) {
  1655. netdev_err(vf_netdev,
  1656. "can not register netvsc VF receive handler (err = %d)\n",
  1657. ret);
  1658. goto rx_handler_failed;
  1659. }
  1660. ret = netdev_master_upper_dev_link(vf_netdev, ndev,
  1661. NULL, NULL, NULL);
  1662. if (ret != 0) {
  1663. netdev_err(vf_netdev,
  1664. "can not set master device %s (err = %d)\n",
  1665. ndev->name, ret);
  1666. goto upper_link_failed;
  1667. }
  1668. /* set slave flag before open to prevent IPv6 addrconf */
  1669. vf_netdev->flags |= IFF_SLAVE;
  1670. schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
  1671. call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
  1672. netdev_info(vf_netdev, "joined to %s\n", ndev->name);
  1673. return 0;
  1674. upper_link_failed:
  1675. netdev_rx_handler_unregister(vf_netdev);
  1676. rx_handler_failed:
  1677. return ret;
  1678. }
  1679. static void __netvsc_vf_setup(struct net_device *ndev,
  1680. struct net_device *vf_netdev)
  1681. {
  1682. int ret;
  1683. /* Align MTU of VF with master */
  1684. ret = dev_set_mtu(vf_netdev, ndev->mtu);
  1685. if (ret)
  1686. netdev_warn(vf_netdev,
  1687. "unable to change mtu to %u\n", ndev->mtu);
  1688. /* set multicast etc flags on VF */
  1689. dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE);
  1690. /* sync address list from ndev to VF */
  1691. netif_addr_lock_bh(ndev);
  1692. dev_uc_sync(vf_netdev, ndev);
  1693. dev_mc_sync(vf_netdev, ndev);
  1694. netif_addr_unlock_bh(ndev);
  1695. if (netif_running(ndev)) {
  1696. ret = dev_open(vf_netdev);
  1697. if (ret)
  1698. netdev_warn(vf_netdev,
  1699. "unable to open: %d\n", ret);
  1700. }
  1701. }
  1702. /* Setup VF as slave of the synthetic device.
  1703. * Runs in workqueue to avoid recursion in netlink callbacks.
  1704. */
  1705. static void netvsc_vf_setup(struct work_struct *w)
  1706. {
  1707. struct net_device_context *ndev_ctx
  1708. = container_of(w, struct net_device_context, vf_takeover.work);
  1709. struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1710. struct net_device *vf_netdev;
  1711. if (!rtnl_trylock()) {
  1712. schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
  1713. return;
  1714. }
  1715. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1716. if (vf_netdev)
  1717. __netvsc_vf_setup(ndev, vf_netdev);
  1718. rtnl_unlock();
  1719. }
  1720. /* Find netvsc by VF serial number.
  1721. * The PCI hyperv controller records the serial number as the slot kobj name.
  1722. */
  1723. static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
  1724. {
  1725. struct device *parent = vf_netdev->dev.parent;
  1726. struct net_device_context *ndev_ctx;
  1727. struct pci_dev *pdev;
  1728. u32 serial;
  1729. if (!parent || !dev_is_pci(parent))
  1730. return NULL; /* not a PCI device */
  1731. pdev = to_pci_dev(parent);
  1732. if (!pdev->slot) {
  1733. netdev_notice(vf_netdev, "no PCI slot information\n");
  1734. return NULL;
  1735. }
  1736. if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
  1737. netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
  1738. pci_slot_name(pdev->slot));
  1739. return NULL;
  1740. }
  1741. list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
  1742. if (!ndev_ctx->vf_alloc)
  1743. continue;
  1744. if (ndev_ctx->vf_serial == serial)
  1745. return hv_get_drvdata(ndev_ctx->device_ctx);
  1746. }
  1747. netdev_notice(vf_netdev,
  1748. "no netdev found for vf serial:%u\n", serial);
  1749. return NULL;
  1750. }
  1751. static int netvsc_register_vf(struct net_device *vf_netdev)
  1752. {
  1753. struct net_device_context *net_device_ctx;
  1754. struct netvsc_device *netvsc_dev;
  1755. struct net_device *ndev;
  1756. int ret;
  1757. if (vf_netdev->addr_len != ETH_ALEN)
  1758. return NOTIFY_DONE;
  1759. ndev = get_netvsc_byslot(vf_netdev);
  1760. if (!ndev)
  1761. return NOTIFY_DONE;
  1762. net_device_ctx = netdev_priv(ndev);
  1763. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1764. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1765. return NOTIFY_DONE;
  1766. /* if syntihetic interface is a different namespace,
  1767. * then move the VF to that namespace; join will be
  1768. * done again in that context.
  1769. */
  1770. if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
  1771. ret = dev_change_net_namespace(vf_netdev,
  1772. dev_net(ndev), "eth%d");
  1773. if (ret)
  1774. netdev_err(vf_netdev,
  1775. "could not move to same namespace as %s: %d\n",
  1776. ndev->name, ret);
  1777. else
  1778. netdev_info(vf_netdev,
  1779. "VF moved to namespace with: %s\n",
  1780. ndev->name);
  1781. return NOTIFY_DONE;
  1782. }
  1783. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1784. if (netvsc_vf_join(vf_netdev, ndev) != 0)
  1785. return NOTIFY_DONE;
  1786. dev_hold(vf_netdev);
  1787. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1788. return NOTIFY_OK;
  1789. }
  1790. /* VF up/down change detected, schedule to change data path */
  1791. static int netvsc_vf_changed(struct net_device *vf_netdev)
  1792. {
  1793. struct net_device_context *net_device_ctx;
  1794. struct netvsc_device *netvsc_dev;
  1795. struct net_device *ndev;
  1796. bool vf_is_up = netif_running(vf_netdev);
  1797. ndev = get_netvsc_byref(vf_netdev);
  1798. if (!ndev)
  1799. return NOTIFY_DONE;
  1800. net_device_ctx = netdev_priv(ndev);
  1801. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1802. if (!netvsc_dev)
  1803. return NOTIFY_DONE;
  1804. netvsc_switch_datapath(ndev, vf_is_up);
  1805. netdev_info(ndev, "Data path switched %s VF: %s\n",
  1806. vf_is_up ? "to" : "from", vf_netdev->name);
  1807. return NOTIFY_OK;
  1808. }
  1809. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1810. {
  1811. struct net_device *ndev;
  1812. struct net_device_context *net_device_ctx;
  1813. ndev = get_netvsc_byref(vf_netdev);
  1814. if (!ndev)
  1815. return NOTIFY_DONE;
  1816. net_device_ctx = netdev_priv(ndev);
  1817. cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
  1818. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1819. netdev_rx_handler_unregister(vf_netdev);
  1820. netdev_upper_dev_unlink(vf_netdev, ndev);
  1821. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1822. dev_put(vf_netdev);
  1823. return NOTIFY_OK;
  1824. }
  1825. static int netvsc_probe(struct hv_device *dev,
  1826. const struct hv_vmbus_device_id *dev_id)
  1827. {
  1828. struct net_device *net = NULL;
  1829. struct net_device_context *net_device_ctx;
  1830. struct netvsc_device_info *device_info = NULL;
  1831. struct netvsc_device *nvdev;
  1832. int ret = -ENOMEM;
  1833. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1834. VRSS_CHANNEL_MAX);
  1835. if (!net)
  1836. goto no_net;
  1837. netif_carrier_off(net);
  1838. netvsc_init_settings(net);
  1839. net_device_ctx = netdev_priv(net);
  1840. net_device_ctx->device_ctx = dev;
  1841. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1842. if (netif_msg_probe(net_device_ctx))
  1843. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1844. net_device_ctx->msg_enable);
  1845. hv_set_drvdata(dev, net);
  1846. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1847. spin_lock_init(&net_device_ctx->lock);
  1848. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1849. INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
  1850. net_device_ctx->vf_stats
  1851. = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
  1852. if (!net_device_ctx->vf_stats)
  1853. goto no_stats;
  1854. net->netdev_ops = &device_ops;
  1855. net->ethtool_ops = &ethtool_ops;
  1856. SET_NETDEV_DEV(net, &dev->device);
  1857. /* We always need headroom for rndis header */
  1858. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1859. /* Initialize the number of queues to be 1, we may change it if more
  1860. * channels are offered later.
  1861. */
  1862. netif_set_real_num_tx_queues(net, 1);
  1863. netif_set_real_num_rx_queues(net, 1);
  1864. /* Notify the netvsc driver of the new device */
  1865. device_info = netvsc_devinfo_get(NULL);
  1866. if (!device_info) {
  1867. ret = -ENOMEM;
  1868. goto devinfo_failed;
  1869. }
  1870. nvdev = rndis_filter_device_add(dev, device_info);
  1871. if (IS_ERR(nvdev)) {
  1872. ret = PTR_ERR(nvdev);
  1873. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1874. goto rndis_failed;
  1875. }
  1876. memcpy(net->dev_addr, device_info->mac_adr, ETH_ALEN);
  1877. /* We must get rtnl lock before scheduling nvdev->subchan_work,
  1878. * otherwise netvsc_subchan_work() can get rtnl lock first and wait
  1879. * all subchannels to show up, but that may not happen because
  1880. * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
  1881. * -> ... -> device_add() -> ... -> __device_attach() can't get
  1882. * the device lock, so all the subchannels can't be processed --
  1883. * finally netvsc_subchan_work() hangs for ever.
  1884. */
  1885. rtnl_lock();
  1886. if (nvdev->num_chn > 1)
  1887. schedule_work(&nvdev->subchan_work);
  1888. /* hw_features computed in rndis_netdev_set_hwcaps() */
  1889. net->features = net->hw_features |
  1890. NETIF_F_HIGHDMA | NETIF_F_SG |
  1891. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1892. net->vlan_features = net->features;
  1893. netdev_lockdep_set_classes(net);
  1894. /* MTU range: 68 - 1500 or 65521 */
  1895. net->min_mtu = NETVSC_MTU_MIN;
  1896. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1897. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1898. else
  1899. net->max_mtu = ETH_DATA_LEN;
  1900. nvdev->tx_disable = false;
  1901. ret = register_netdevice(net);
  1902. if (ret != 0) {
  1903. pr_err("Unable to register netdev.\n");
  1904. goto register_failed;
  1905. }
  1906. list_add(&net_device_ctx->list, &netvsc_dev_list);
  1907. rtnl_unlock();
  1908. kfree(device_info);
  1909. return 0;
  1910. register_failed:
  1911. rtnl_unlock();
  1912. rndis_filter_device_remove(dev, nvdev);
  1913. rndis_failed:
  1914. kfree(device_info);
  1915. devinfo_failed:
  1916. free_percpu(net_device_ctx->vf_stats);
  1917. no_stats:
  1918. hv_set_drvdata(dev, NULL);
  1919. free_netdev(net);
  1920. no_net:
  1921. return ret;
  1922. }
  1923. static int netvsc_remove(struct hv_device *dev)
  1924. {
  1925. struct net_device_context *ndev_ctx;
  1926. struct net_device *vf_netdev, *net;
  1927. struct netvsc_device *nvdev;
  1928. net = hv_get_drvdata(dev);
  1929. if (net == NULL) {
  1930. dev_err(&dev->device, "No net device to remove\n");
  1931. return 0;
  1932. }
  1933. ndev_ctx = netdev_priv(net);
  1934. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1935. rtnl_lock();
  1936. nvdev = rtnl_dereference(ndev_ctx->nvdev);
  1937. if (nvdev)
  1938. cancel_work_sync(&nvdev->subchan_work);
  1939. /*
  1940. * Call to the vsc driver to let it know that the device is being
  1941. * removed. Also blocks mtu and channel changes.
  1942. */
  1943. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1944. if (vf_netdev)
  1945. netvsc_unregister_vf(vf_netdev);
  1946. if (nvdev)
  1947. rndis_filter_device_remove(dev, nvdev);
  1948. unregister_netdevice(net);
  1949. list_del(&ndev_ctx->list);
  1950. rtnl_unlock();
  1951. hv_set_drvdata(dev, NULL);
  1952. free_percpu(ndev_ctx->vf_stats);
  1953. free_netdev(net);
  1954. return 0;
  1955. }
  1956. static const struct hv_vmbus_device_id id_table[] = {
  1957. /* Network guid */
  1958. { HV_NIC_GUID, },
  1959. { },
  1960. };
  1961. MODULE_DEVICE_TABLE(vmbus, id_table);
  1962. /* The one and only one */
  1963. static struct hv_driver netvsc_drv = {
  1964. .name = KBUILD_MODNAME,
  1965. .id_table = id_table,
  1966. .probe = netvsc_probe,
  1967. .remove = netvsc_remove,
  1968. .driver = {
  1969. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  1970. },
  1971. };
  1972. /*
  1973. * On Hyper-V, every VF interface is matched with a corresponding
  1974. * synthetic interface. The synthetic interface is presented first
  1975. * to the guest. When the corresponding VF instance is registered,
  1976. * we will take care of switching the data path.
  1977. */
  1978. static int netvsc_netdev_event(struct notifier_block *this,
  1979. unsigned long event, void *ptr)
  1980. {
  1981. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1982. /* Skip our own events */
  1983. if (event_dev->netdev_ops == &device_ops)
  1984. return NOTIFY_DONE;
  1985. /* Avoid non-Ethernet type devices */
  1986. if (event_dev->type != ARPHRD_ETHER)
  1987. return NOTIFY_DONE;
  1988. /* Avoid Vlan dev with same MAC registering as VF */
  1989. if (is_vlan_dev(event_dev))
  1990. return NOTIFY_DONE;
  1991. /* Avoid Bonding master dev with same MAC registering as VF */
  1992. if ((event_dev->priv_flags & IFF_BONDING) &&
  1993. (event_dev->flags & IFF_MASTER))
  1994. return NOTIFY_DONE;
  1995. switch (event) {
  1996. case NETDEV_REGISTER:
  1997. return netvsc_register_vf(event_dev);
  1998. case NETDEV_UNREGISTER:
  1999. return netvsc_unregister_vf(event_dev);
  2000. case NETDEV_UP:
  2001. case NETDEV_DOWN:
  2002. return netvsc_vf_changed(event_dev);
  2003. default:
  2004. return NOTIFY_DONE;
  2005. }
  2006. }
  2007. static struct notifier_block netvsc_netdev_notifier = {
  2008. .notifier_call = netvsc_netdev_event,
  2009. };
  2010. static void __exit netvsc_drv_exit(void)
  2011. {
  2012. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  2013. vmbus_driver_unregister(&netvsc_drv);
  2014. }
  2015. static int __init netvsc_drv_init(void)
  2016. {
  2017. int ret;
  2018. if (ring_size < RING_SIZE_MIN) {
  2019. ring_size = RING_SIZE_MIN;
  2020. pr_info("Increased ring_size to %u (min allowed)\n",
  2021. ring_size);
  2022. }
  2023. netvsc_ring_bytes = ring_size * PAGE_SIZE;
  2024. ret = vmbus_driver_register(&netvsc_drv);
  2025. if (ret)
  2026. return ret;
  2027. register_netdevice_notifier(&netvsc_netdev_notifier);
  2028. return 0;
  2029. }
  2030. MODULE_LICENSE("GPL");
  2031. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  2032. module_init(netvsc_drv_init);
  2033. module_exit(netvsc_drv_exit);