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 (unlikely(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->queue_mapping = 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
  448. * already called with rcu_read_lock_bh
  449. */
  450. vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
  451. if (vf_netdev && netif_running(vf_netdev) &&
  452. !netpoll_tx_running(net))
  453. return netvsc_vf_xmit(net, vf_netdev, skb);
  454. /* We will atmost need two pages to describe the rndis
  455. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  456. * of pages in a single packet. If skb is scattered around
  457. * more pages we try linearizing it.
  458. */
  459. num_data_pgs = netvsc_get_slots(skb) + 2;
  460. if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
  461. ++net_device_ctx->eth_stats.tx_scattered;
  462. if (skb_linearize(skb))
  463. goto no_memory;
  464. num_data_pgs = netvsc_get_slots(skb) + 2;
  465. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  466. ++net_device_ctx->eth_stats.tx_too_big;
  467. goto drop;
  468. }
  469. }
  470. /*
  471. * Place the rndis header in the skb head room and
  472. * the skb->cb will be used for hv_netvsc_packet
  473. * structure.
  474. */
  475. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  476. if (ret)
  477. goto no_memory;
  478. /* Use the skb control buffer for building up the packet */
  479. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  480. FIELD_SIZEOF(struct sk_buff, cb));
  481. packet = (struct hv_netvsc_packet *)skb->cb;
  482. packet->q_idx = skb_get_queue_mapping(skb);
  483. packet->total_data_buflen = skb->len;
  484. packet->total_bytes = skb->len;
  485. packet->total_packets = 1;
  486. rndis_msg = (struct rndis_message *)skb->head;
  487. /* Add the rndis header */
  488. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  489. rndis_msg->msg_len = packet->total_data_buflen;
  490. rndis_msg->msg.pkt = (struct rndis_packet) {
  491. .data_offset = sizeof(struct rndis_packet),
  492. .data_len = packet->total_data_buflen,
  493. .per_pkt_info_offset = sizeof(struct rndis_packet),
  494. };
  495. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  496. hash = skb_get_hash_raw(skb);
  497. if (hash != 0 && net->real_num_tx_queues > 1) {
  498. u32 *hash_info;
  499. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  500. hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  501. NBL_HASH_VALUE);
  502. *hash_info = hash;
  503. }
  504. if (skb_vlan_tag_present(skb)) {
  505. struct ndis_pkt_8021q_info *vlan;
  506. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  507. vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  508. IEEE_8021Q_INFO);
  509. vlan->value = 0;
  510. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  511. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  512. VLAN_PRIO_SHIFT;
  513. }
  514. if (skb_is_gso(skb)) {
  515. struct ndis_tcp_lso_info *lso_info;
  516. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  517. lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  518. TCP_LARGESEND_PKTINFO);
  519. lso_info->value = 0;
  520. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  521. if (skb->protocol == htons(ETH_P_IP)) {
  522. lso_info->lso_v2_transmit.ip_version =
  523. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  524. ip_hdr(skb)->tot_len = 0;
  525. ip_hdr(skb)->check = 0;
  526. tcp_hdr(skb)->check =
  527. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  528. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  529. } else {
  530. lso_info->lso_v2_transmit.ip_version =
  531. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  532. ipv6_hdr(skb)->payload_len = 0;
  533. tcp_hdr(skb)->check =
  534. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  535. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  536. }
  537. lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
  538. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  539. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  540. if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
  541. struct ndis_tcp_ip_checksum_info *csum_info;
  542. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  543. csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  544. TCPIP_CHKSUM_PKTINFO);
  545. csum_info->value = 0;
  546. csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
  547. if (skb->protocol == htons(ETH_P_IP)) {
  548. csum_info->transmit.is_ipv4 = 1;
  549. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  550. csum_info->transmit.tcp_checksum = 1;
  551. else
  552. csum_info->transmit.udp_checksum = 1;
  553. } else {
  554. csum_info->transmit.is_ipv6 = 1;
  555. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  556. csum_info->transmit.tcp_checksum = 1;
  557. else
  558. csum_info->transmit.udp_checksum = 1;
  559. }
  560. } else {
  561. /* Can't do offload of this type of checksum */
  562. if (skb_checksum_help(skb))
  563. goto drop;
  564. }
  565. }
  566. /* Start filling in the page buffers with the rndis hdr */
  567. rndis_msg->msg_len += rndis_msg_size;
  568. packet->total_data_buflen = rndis_msg->msg_len;
  569. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  570. skb, packet, pb);
  571. /* timestamp packet in software */
  572. skb_tx_timestamp(skb);
  573. ret = netvsc_send(net, packet, rndis_msg, pb, skb);
  574. if (likely(ret == 0))
  575. return NETDEV_TX_OK;
  576. if (ret == -EAGAIN) {
  577. ++net_device_ctx->eth_stats.tx_busy;
  578. return NETDEV_TX_BUSY;
  579. }
  580. if (ret == -ENOSPC)
  581. ++net_device_ctx->eth_stats.tx_no_space;
  582. drop:
  583. dev_kfree_skb_any(skb);
  584. net->stats.tx_dropped++;
  585. return NETDEV_TX_OK;
  586. no_memory:
  587. ++net_device_ctx->eth_stats.tx_no_memory;
  588. goto drop;
  589. }
  590. /*
  591. * netvsc_linkstatus_callback - Link up/down notification
  592. */
  593. void netvsc_linkstatus_callback(struct net_device *net,
  594. struct rndis_message *resp)
  595. {
  596. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  597. struct net_device_context *ndev_ctx = netdev_priv(net);
  598. struct netvsc_reconfig *event;
  599. unsigned long flags;
  600. /* Update the physical link speed when changing to another vSwitch */
  601. if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
  602. u32 speed;
  603. speed = *(u32 *)((void *)indicate
  604. + indicate->status_buf_offset) / 10000;
  605. ndev_ctx->speed = speed;
  606. return;
  607. }
  608. /* Handle these link change statuses below */
  609. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  610. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  611. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  612. return;
  613. if (net->reg_state != NETREG_REGISTERED)
  614. return;
  615. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  616. if (!event)
  617. return;
  618. event->event = indicate->status;
  619. spin_lock_irqsave(&ndev_ctx->lock, flags);
  620. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  621. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  622. schedule_delayed_work(&ndev_ctx->dwork, 0);
  623. }
  624. static void netvsc_comp_ipcsum(struct sk_buff *skb)
  625. {
  626. struct iphdr *iph = (struct iphdr *)skb->data;
  627. iph->check = 0;
  628. iph->check = ip_fast_csum(iph, iph->ihl);
  629. }
  630. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  631. struct napi_struct *napi,
  632. const struct ndis_tcp_ip_checksum_info *csum_info,
  633. const struct ndis_pkt_8021q_info *vlan,
  634. void *data, u32 buflen)
  635. {
  636. struct sk_buff *skb;
  637. skb = napi_alloc_skb(napi, buflen);
  638. if (!skb)
  639. return skb;
  640. /*
  641. * Copy to skb. This copy is needed here since the memory pointed by
  642. * hv_netvsc_packet cannot be deallocated
  643. */
  644. skb_put_data(skb, data, buflen);
  645. skb->protocol = eth_type_trans(skb, net);
  646. /* skb is already created with CHECKSUM_NONE */
  647. skb_checksum_none_assert(skb);
  648. /* Incoming packets may have IP header checksum verified by the host.
  649. * They may not have IP header checksum computed after coalescing.
  650. * We compute it here if the flags are set, because on Linux, the IP
  651. * checksum is always checked.
  652. */
  653. if (csum_info && csum_info->receive.ip_checksum_value_invalid &&
  654. csum_info->receive.ip_checksum_succeeded &&
  655. skb->protocol == htons(ETH_P_IP))
  656. netvsc_comp_ipcsum(skb);
  657. /* Do L4 checksum offload if enabled and present. */
  658. if (csum_info && (net->features & NETIF_F_RXCSUM)) {
  659. if (csum_info->receive.tcp_checksum_succeeded ||
  660. csum_info->receive.udp_checksum_succeeded)
  661. skb->ip_summed = CHECKSUM_UNNECESSARY;
  662. }
  663. if (vlan) {
  664. u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
  665. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  666. vlan_tci);
  667. }
  668. return skb;
  669. }
  670. /*
  671. * netvsc_recv_callback - Callback when we receive a packet from the
  672. * "wire" on the specified device.
  673. */
  674. int netvsc_recv_callback(struct net_device *net,
  675. struct netvsc_device *net_device,
  676. struct vmbus_channel *channel,
  677. void *data, u32 len,
  678. const struct ndis_tcp_ip_checksum_info *csum_info,
  679. const struct ndis_pkt_8021q_info *vlan)
  680. {
  681. struct net_device_context *net_device_ctx = netdev_priv(net);
  682. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  683. struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
  684. struct sk_buff *skb;
  685. struct netvsc_stats *rx_stats;
  686. if (net->reg_state != NETREG_REGISTERED)
  687. return NVSP_STAT_FAIL;
  688. /* Allocate a skb - TODO direct I/O to pages? */
  689. skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
  690. csum_info, vlan, data, len);
  691. if (unlikely(!skb)) {
  692. ++net_device_ctx->eth_stats.rx_no_memory;
  693. return NVSP_STAT_FAIL;
  694. }
  695. skb_record_rx_queue(skb, q_idx);
  696. /*
  697. * Even if injecting the packet, record the statistics
  698. * on the synthetic device because modifying the VF device
  699. * statistics will not work correctly.
  700. */
  701. rx_stats = &nvchan->rx_stats;
  702. u64_stats_update_begin(&rx_stats->syncp);
  703. rx_stats->packets++;
  704. rx_stats->bytes += len;
  705. if (skb->pkt_type == PACKET_BROADCAST)
  706. ++rx_stats->broadcast;
  707. else if (skb->pkt_type == PACKET_MULTICAST)
  708. ++rx_stats->multicast;
  709. u64_stats_update_end(&rx_stats->syncp);
  710. napi_gro_receive(&nvchan->napi, skb);
  711. return NVSP_STAT_SUCCESS;
  712. }
  713. static void netvsc_get_drvinfo(struct net_device *net,
  714. struct ethtool_drvinfo *info)
  715. {
  716. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  717. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  718. }
  719. static void netvsc_get_channels(struct net_device *net,
  720. struct ethtool_channels *channel)
  721. {
  722. struct net_device_context *net_device_ctx = netdev_priv(net);
  723. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  724. if (nvdev) {
  725. channel->max_combined = nvdev->max_chn;
  726. channel->combined_count = nvdev->num_chn;
  727. }
  728. }
  729. /* Alloc struct netvsc_device_info, and initialize it from either existing
  730. * struct netvsc_device, or from default values.
  731. */
  732. static struct netvsc_device_info *netvsc_devinfo_get
  733. (struct netvsc_device *nvdev)
  734. {
  735. struct netvsc_device_info *dev_info;
  736. dev_info = kzalloc(sizeof(*dev_info), GFP_ATOMIC);
  737. if (!dev_info)
  738. return NULL;
  739. if (nvdev) {
  740. dev_info->num_chn = nvdev->num_chn;
  741. dev_info->send_sections = nvdev->send_section_cnt;
  742. dev_info->send_section_size = nvdev->send_section_size;
  743. dev_info->recv_sections = nvdev->recv_section_cnt;
  744. dev_info->recv_section_size = nvdev->recv_section_size;
  745. memcpy(dev_info->rss_key, nvdev->extension->rss_key,
  746. NETVSC_HASH_KEYLEN);
  747. } else {
  748. dev_info->num_chn = VRSS_CHANNEL_DEFAULT;
  749. dev_info->send_sections = NETVSC_DEFAULT_TX;
  750. dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
  751. dev_info->recv_sections = NETVSC_DEFAULT_RX;
  752. dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
  753. }
  754. return dev_info;
  755. }
  756. static int netvsc_detach(struct net_device *ndev,
  757. struct netvsc_device *nvdev)
  758. {
  759. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  760. struct hv_device *hdev = ndev_ctx->device_ctx;
  761. int ret;
  762. /* Don't try continuing to try and setup sub channels */
  763. if (cancel_work_sync(&nvdev->subchan_work))
  764. nvdev->num_chn = 1;
  765. /* If device was up (receiving) then shutdown */
  766. if (netif_running(ndev)) {
  767. netvsc_tx_disable(nvdev, ndev);
  768. ret = rndis_filter_close(nvdev);
  769. if (ret) {
  770. netdev_err(ndev,
  771. "unable to close device (ret %d).\n", ret);
  772. return ret;
  773. }
  774. ret = netvsc_wait_until_empty(nvdev);
  775. if (ret) {
  776. netdev_err(ndev,
  777. "Ring buffer not empty after closing rndis\n");
  778. return ret;
  779. }
  780. }
  781. netif_device_detach(ndev);
  782. rndis_filter_device_remove(hdev, nvdev);
  783. return 0;
  784. }
  785. static int netvsc_attach(struct net_device *ndev,
  786. struct netvsc_device_info *dev_info)
  787. {
  788. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  789. struct hv_device *hdev = ndev_ctx->device_ctx;
  790. struct netvsc_device *nvdev;
  791. struct rndis_device *rdev;
  792. int ret;
  793. nvdev = rndis_filter_device_add(hdev, dev_info);
  794. if (IS_ERR(nvdev))
  795. return PTR_ERR(nvdev);
  796. if (nvdev->num_chn > 1) {
  797. ret = rndis_set_subchannel(ndev, nvdev, dev_info);
  798. /* if unavailable, just proceed with one queue */
  799. if (ret) {
  800. nvdev->max_chn = 1;
  801. nvdev->num_chn = 1;
  802. }
  803. }
  804. /* In any case device is now ready */
  805. nvdev->tx_disable = false;
  806. netif_device_attach(ndev);
  807. /* Note: enable and attach happen when sub-channels setup */
  808. netif_carrier_off(ndev);
  809. if (netif_running(ndev)) {
  810. ret = rndis_filter_open(nvdev);
  811. if (ret)
  812. goto err;
  813. rdev = nvdev->extension;
  814. if (!rdev->link_state)
  815. netif_carrier_on(ndev);
  816. }
  817. return 0;
  818. err:
  819. netif_device_detach(ndev);
  820. rndis_filter_device_remove(hdev, nvdev);
  821. return ret;
  822. }
  823. static int netvsc_set_channels(struct net_device *net,
  824. struct ethtool_channels *channels)
  825. {
  826. struct net_device_context *net_device_ctx = netdev_priv(net);
  827. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  828. unsigned int orig, count = channels->combined_count;
  829. struct netvsc_device_info *device_info;
  830. int ret;
  831. /* We do not support separate count for rx, tx, or other */
  832. if (count == 0 ||
  833. channels->rx_count || channels->tx_count || channels->other_count)
  834. return -EINVAL;
  835. if (!nvdev || nvdev->destroy)
  836. return -ENODEV;
  837. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  838. return -EINVAL;
  839. if (count > nvdev->max_chn)
  840. return -EINVAL;
  841. orig = nvdev->num_chn;
  842. device_info = netvsc_devinfo_get(nvdev);
  843. if (!device_info)
  844. return -ENOMEM;
  845. device_info->num_chn = count;
  846. ret = netvsc_detach(net, nvdev);
  847. if (ret)
  848. goto out;
  849. ret = netvsc_attach(net, device_info);
  850. if (ret) {
  851. device_info->num_chn = orig;
  852. if (netvsc_attach(net, device_info))
  853. netdev_err(net, "restoring channel setting failed\n");
  854. }
  855. out:
  856. kfree(device_info);
  857. return ret;
  858. }
  859. static bool
  860. netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
  861. {
  862. struct ethtool_link_ksettings diff1 = *cmd;
  863. struct ethtool_link_ksettings diff2 = {};
  864. diff1.base.speed = 0;
  865. diff1.base.duplex = 0;
  866. /* advertising and cmd are usually set */
  867. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  868. diff1.base.cmd = 0;
  869. /* We set port to PORT_OTHER */
  870. diff2.base.port = PORT_OTHER;
  871. return !memcmp(&diff1, &diff2, sizeof(diff1));
  872. }
  873. static void netvsc_init_settings(struct net_device *dev)
  874. {
  875. struct net_device_context *ndc = netdev_priv(dev);
  876. ndc->l4_hash = HV_DEFAULT_L4HASH;
  877. ndc->speed = SPEED_UNKNOWN;
  878. ndc->duplex = DUPLEX_FULL;
  879. }
  880. static int netvsc_get_link_ksettings(struct net_device *dev,
  881. struct ethtool_link_ksettings *cmd)
  882. {
  883. struct net_device_context *ndc = netdev_priv(dev);
  884. cmd->base.speed = ndc->speed;
  885. cmd->base.duplex = ndc->duplex;
  886. cmd->base.port = PORT_OTHER;
  887. return 0;
  888. }
  889. static int netvsc_set_link_ksettings(struct net_device *dev,
  890. const struct ethtool_link_ksettings *cmd)
  891. {
  892. struct net_device_context *ndc = netdev_priv(dev);
  893. u32 speed;
  894. speed = cmd->base.speed;
  895. if (!ethtool_validate_speed(speed) ||
  896. !ethtool_validate_duplex(cmd->base.duplex) ||
  897. !netvsc_validate_ethtool_ss_cmd(cmd))
  898. return -EINVAL;
  899. ndc->speed = speed;
  900. ndc->duplex = cmd->base.duplex;
  901. return 0;
  902. }
  903. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  904. {
  905. struct net_device_context *ndevctx = netdev_priv(ndev);
  906. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  907. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  908. int orig_mtu = ndev->mtu;
  909. struct netvsc_device_info *device_info;
  910. int ret = 0;
  911. if (!nvdev || nvdev->destroy)
  912. return -ENODEV;
  913. device_info = netvsc_devinfo_get(nvdev);
  914. if (!device_info)
  915. return -ENOMEM;
  916. /* Change MTU of underlying VF netdev first. */
  917. if (vf_netdev) {
  918. ret = dev_set_mtu(vf_netdev, mtu);
  919. if (ret)
  920. goto out;
  921. }
  922. ret = netvsc_detach(ndev, nvdev);
  923. if (ret)
  924. goto rollback_vf;
  925. ndev->mtu = mtu;
  926. ret = netvsc_attach(ndev, device_info);
  927. if (!ret)
  928. goto out;
  929. /* Attempt rollback to original MTU */
  930. ndev->mtu = orig_mtu;
  931. if (netvsc_attach(ndev, device_info))
  932. netdev_err(ndev, "restoring mtu failed\n");
  933. rollback_vf:
  934. if (vf_netdev)
  935. dev_set_mtu(vf_netdev, orig_mtu);
  936. out:
  937. kfree(device_info);
  938. return ret;
  939. }
  940. static void netvsc_get_vf_stats(struct net_device *net,
  941. struct netvsc_vf_pcpu_stats *tot)
  942. {
  943. struct net_device_context *ndev_ctx = netdev_priv(net);
  944. int i;
  945. memset(tot, 0, sizeof(*tot));
  946. for_each_possible_cpu(i) {
  947. const struct netvsc_vf_pcpu_stats *stats
  948. = per_cpu_ptr(ndev_ctx->vf_stats, i);
  949. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  950. unsigned int start;
  951. do {
  952. start = u64_stats_fetch_begin_irq(&stats->syncp);
  953. rx_packets = stats->rx_packets;
  954. tx_packets = stats->tx_packets;
  955. rx_bytes = stats->rx_bytes;
  956. tx_bytes = stats->tx_bytes;
  957. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  958. tot->rx_packets += rx_packets;
  959. tot->tx_packets += tx_packets;
  960. tot->rx_bytes += rx_bytes;
  961. tot->tx_bytes += tx_bytes;
  962. tot->tx_dropped += stats->tx_dropped;
  963. }
  964. }
  965. static void netvsc_get_pcpu_stats(struct net_device *net,
  966. struct netvsc_ethtool_pcpu_stats *pcpu_tot)
  967. {
  968. struct net_device_context *ndev_ctx = netdev_priv(net);
  969. struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
  970. int i;
  971. /* fetch percpu stats of vf */
  972. for_each_possible_cpu(i) {
  973. const struct netvsc_vf_pcpu_stats *stats =
  974. per_cpu_ptr(ndev_ctx->vf_stats, i);
  975. struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
  976. unsigned int start;
  977. do {
  978. start = u64_stats_fetch_begin_irq(&stats->syncp);
  979. this_tot->vf_rx_packets = stats->rx_packets;
  980. this_tot->vf_tx_packets = stats->tx_packets;
  981. this_tot->vf_rx_bytes = stats->rx_bytes;
  982. this_tot->vf_tx_bytes = stats->tx_bytes;
  983. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  984. this_tot->rx_packets = this_tot->vf_rx_packets;
  985. this_tot->tx_packets = this_tot->vf_tx_packets;
  986. this_tot->rx_bytes = this_tot->vf_rx_bytes;
  987. this_tot->tx_bytes = this_tot->vf_tx_bytes;
  988. }
  989. /* fetch percpu stats of netvsc */
  990. for (i = 0; i < nvdev->num_chn; i++) {
  991. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  992. const struct netvsc_stats *stats;
  993. struct netvsc_ethtool_pcpu_stats *this_tot =
  994. &pcpu_tot[nvchan->channel->target_cpu];
  995. u64 packets, bytes;
  996. unsigned int start;
  997. stats = &nvchan->tx_stats;
  998. do {
  999. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1000. packets = stats->packets;
  1001. bytes = stats->bytes;
  1002. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1003. this_tot->tx_bytes += bytes;
  1004. this_tot->tx_packets += packets;
  1005. stats = &nvchan->rx_stats;
  1006. do {
  1007. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1008. packets = stats->packets;
  1009. bytes = stats->bytes;
  1010. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1011. this_tot->rx_bytes += bytes;
  1012. this_tot->rx_packets += packets;
  1013. }
  1014. }
  1015. static void netvsc_get_stats64(struct net_device *net,
  1016. struct rtnl_link_stats64 *t)
  1017. {
  1018. struct net_device_context *ndev_ctx = netdev_priv(net);
  1019. struct netvsc_device *nvdev;
  1020. struct netvsc_vf_pcpu_stats vf_tot;
  1021. int i;
  1022. rcu_read_lock();
  1023. nvdev = rcu_dereference(ndev_ctx->nvdev);
  1024. if (!nvdev)
  1025. goto out;
  1026. netdev_stats_to_stats64(t, &net->stats);
  1027. netvsc_get_vf_stats(net, &vf_tot);
  1028. t->rx_packets += vf_tot.rx_packets;
  1029. t->tx_packets += vf_tot.tx_packets;
  1030. t->rx_bytes += vf_tot.rx_bytes;
  1031. t->tx_bytes += vf_tot.tx_bytes;
  1032. t->tx_dropped += vf_tot.tx_dropped;
  1033. for (i = 0; i < nvdev->num_chn; i++) {
  1034. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  1035. const struct netvsc_stats *stats;
  1036. u64 packets, bytes, multicast;
  1037. unsigned int start;
  1038. stats = &nvchan->tx_stats;
  1039. do {
  1040. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1041. packets = stats->packets;
  1042. bytes = stats->bytes;
  1043. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1044. t->tx_bytes += bytes;
  1045. t->tx_packets += packets;
  1046. stats = &nvchan->rx_stats;
  1047. do {
  1048. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1049. packets = stats->packets;
  1050. bytes = stats->bytes;
  1051. multicast = stats->multicast + stats->broadcast;
  1052. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1053. t->rx_bytes += bytes;
  1054. t->rx_packets += packets;
  1055. t->multicast += multicast;
  1056. }
  1057. out:
  1058. rcu_read_unlock();
  1059. }
  1060. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  1061. {
  1062. struct net_device_context *ndc = netdev_priv(ndev);
  1063. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  1064. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1065. struct sockaddr *addr = p;
  1066. int err;
  1067. err = eth_prepare_mac_addr_change(ndev, p);
  1068. if (err)
  1069. return err;
  1070. if (!nvdev)
  1071. return -ENODEV;
  1072. if (vf_netdev) {
  1073. err = dev_set_mac_address(vf_netdev, addr);
  1074. if (err)
  1075. return err;
  1076. }
  1077. err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
  1078. if (!err) {
  1079. eth_commit_mac_addr_change(ndev, p);
  1080. } else if (vf_netdev) {
  1081. /* rollback change on VF */
  1082. memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
  1083. dev_set_mac_address(vf_netdev, addr);
  1084. }
  1085. return err;
  1086. }
  1087. static const struct {
  1088. char name[ETH_GSTRING_LEN];
  1089. u16 offset;
  1090. } netvsc_stats[] = {
  1091. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  1092. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  1093. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  1094. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  1095. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  1096. { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
  1097. { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
  1098. { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
  1099. { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
  1100. { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
  1101. }, pcpu_stats[] = {
  1102. { "cpu%u_rx_packets",
  1103. offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
  1104. { "cpu%u_rx_bytes",
  1105. offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
  1106. { "cpu%u_tx_packets",
  1107. offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
  1108. { "cpu%u_tx_bytes",
  1109. offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
  1110. { "cpu%u_vf_rx_packets",
  1111. offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
  1112. { "cpu%u_vf_rx_bytes",
  1113. offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
  1114. { "cpu%u_vf_tx_packets",
  1115. offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
  1116. { "cpu%u_vf_tx_bytes",
  1117. offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
  1118. }, vf_stats[] = {
  1119. { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
  1120. { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
  1121. { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
  1122. { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
  1123. { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
  1124. };
  1125. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  1126. #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
  1127. /* statistics per queue (rx/tx packets/bytes) */
  1128. #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
  1129. /* 4 statistics per queue (rx/tx packets/bytes) */
  1130. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
  1131. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  1132. {
  1133. struct net_device_context *ndc = netdev_priv(dev);
  1134. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1135. if (!nvdev)
  1136. return -ENODEV;
  1137. switch (string_set) {
  1138. case ETH_SS_STATS:
  1139. return NETVSC_GLOBAL_STATS_LEN
  1140. + NETVSC_VF_STATS_LEN
  1141. + NETVSC_QUEUE_STATS_LEN(nvdev)
  1142. + NETVSC_PCPU_STATS_LEN;
  1143. default:
  1144. return -EINVAL;
  1145. }
  1146. }
  1147. static void netvsc_get_ethtool_stats(struct net_device *dev,
  1148. struct ethtool_stats *stats, u64 *data)
  1149. {
  1150. struct net_device_context *ndc = netdev_priv(dev);
  1151. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1152. const void *nds = &ndc->eth_stats;
  1153. const struct netvsc_stats *qstats;
  1154. struct netvsc_vf_pcpu_stats sum;
  1155. struct netvsc_ethtool_pcpu_stats *pcpu_sum;
  1156. unsigned int start;
  1157. u64 packets, bytes;
  1158. int i, j, cpu;
  1159. if (!nvdev)
  1160. return;
  1161. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  1162. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  1163. netvsc_get_vf_stats(dev, &sum);
  1164. for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
  1165. data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
  1166. for (j = 0; j < nvdev->num_chn; j++) {
  1167. qstats = &nvdev->chan_table[j].tx_stats;
  1168. do {
  1169. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1170. packets = qstats->packets;
  1171. bytes = qstats->bytes;
  1172. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1173. data[i++] = packets;
  1174. data[i++] = bytes;
  1175. qstats = &nvdev->chan_table[j].rx_stats;
  1176. do {
  1177. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1178. packets = qstats->packets;
  1179. bytes = qstats->bytes;
  1180. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1181. data[i++] = packets;
  1182. data[i++] = bytes;
  1183. }
  1184. pcpu_sum = kvmalloc_array(num_possible_cpus(),
  1185. sizeof(struct netvsc_ethtool_pcpu_stats),
  1186. GFP_KERNEL);
  1187. netvsc_get_pcpu_stats(dev, pcpu_sum);
  1188. for_each_present_cpu(cpu) {
  1189. struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
  1190. for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
  1191. data[i++] = *(u64 *)((void *)this_sum
  1192. + pcpu_stats[j].offset);
  1193. }
  1194. kvfree(pcpu_sum);
  1195. }
  1196. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1197. {
  1198. struct net_device_context *ndc = netdev_priv(dev);
  1199. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1200. u8 *p = data;
  1201. int i, cpu;
  1202. if (!nvdev)
  1203. return;
  1204. switch (stringset) {
  1205. case ETH_SS_STATS:
  1206. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
  1207. memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
  1208. p += ETH_GSTRING_LEN;
  1209. }
  1210. for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
  1211. memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
  1212. p += ETH_GSTRING_LEN;
  1213. }
  1214. for (i = 0; i < nvdev->num_chn; i++) {
  1215. sprintf(p, "tx_queue_%u_packets", i);
  1216. p += ETH_GSTRING_LEN;
  1217. sprintf(p, "tx_queue_%u_bytes", i);
  1218. p += ETH_GSTRING_LEN;
  1219. sprintf(p, "rx_queue_%u_packets", i);
  1220. p += ETH_GSTRING_LEN;
  1221. sprintf(p, "rx_queue_%u_bytes", i);
  1222. p += ETH_GSTRING_LEN;
  1223. }
  1224. for_each_present_cpu(cpu) {
  1225. for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++) {
  1226. sprintf(p, pcpu_stats[i].name, cpu);
  1227. p += ETH_GSTRING_LEN;
  1228. }
  1229. }
  1230. break;
  1231. }
  1232. }
  1233. static int
  1234. netvsc_get_rss_hash_opts(struct net_device_context *ndc,
  1235. struct ethtool_rxnfc *info)
  1236. {
  1237. const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1238. info->data = RXH_IP_SRC | RXH_IP_DST;
  1239. switch (info->flow_type) {
  1240. case TCP_V4_FLOW:
  1241. if (ndc->l4_hash & HV_TCP4_L4HASH)
  1242. info->data |= l4_flag;
  1243. break;
  1244. case TCP_V6_FLOW:
  1245. if (ndc->l4_hash & HV_TCP6_L4HASH)
  1246. info->data |= l4_flag;
  1247. break;
  1248. case UDP_V4_FLOW:
  1249. if (ndc->l4_hash & HV_UDP4_L4HASH)
  1250. info->data |= l4_flag;
  1251. break;
  1252. case UDP_V6_FLOW:
  1253. if (ndc->l4_hash & HV_UDP6_L4HASH)
  1254. info->data |= l4_flag;
  1255. break;
  1256. case IPV4_FLOW:
  1257. case IPV6_FLOW:
  1258. break;
  1259. default:
  1260. info->data = 0;
  1261. break;
  1262. }
  1263. return 0;
  1264. }
  1265. static int
  1266. netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  1267. u32 *rules)
  1268. {
  1269. struct net_device_context *ndc = netdev_priv(dev);
  1270. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1271. if (!nvdev)
  1272. return -ENODEV;
  1273. switch (info->cmd) {
  1274. case ETHTOOL_GRXRINGS:
  1275. info->data = nvdev->num_chn;
  1276. return 0;
  1277. case ETHTOOL_GRXFH:
  1278. return netvsc_get_rss_hash_opts(ndc, info);
  1279. }
  1280. return -EOPNOTSUPP;
  1281. }
  1282. static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
  1283. struct ethtool_rxnfc *info)
  1284. {
  1285. if (info->data == (RXH_IP_SRC | RXH_IP_DST |
  1286. RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  1287. switch (info->flow_type) {
  1288. case TCP_V4_FLOW:
  1289. ndc->l4_hash |= HV_TCP4_L4HASH;
  1290. break;
  1291. case TCP_V6_FLOW:
  1292. ndc->l4_hash |= HV_TCP6_L4HASH;
  1293. break;
  1294. case UDP_V4_FLOW:
  1295. ndc->l4_hash |= HV_UDP4_L4HASH;
  1296. break;
  1297. case UDP_V6_FLOW:
  1298. ndc->l4_hash |= HV_UDP6_L4HASH;
  1299. break;
  1300. default:
  1301. return -EOPNOTSUPP;
  1302. }
  1303. return 0;
  1304. }
  1305. if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
  1306. switch (info->flow_type) {
  1307. case TCP_V4_FLOW:
  1308. ndc->l4_hash &= ~HV_TCP4_L4HASH;
  1309. break;
  1310. case TCP_V6_FLOW:
  1311. ndc->l4_hash &= ~HV_TCP6_L4HASH;
  1312. break;
  1313. case UDP_V4_FLOW:
  1314. ndc->l4_hash &= ~HV_UDP4_L4HASH;
  1315. break;
  1316. case UDP_V6_FLOW:
  1317. ndc->l4_hash &= ~HV_UDP6_L4HASH;
  1318. break;
  1319. default:
  1320. return -EOPNOTSUPP;
  1321. }
  1322. return 0;
  1323. }
  1324. return -EOPNOTSUPP;
  1325. }
  1326. static int
  1327. netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
  1328. {
  1329. struct net_device_context *ndc = netdev_priv(ndev);
  1330. if (info->cmd == ETHTOOL_SRXFH)
  1331. return netvsc_set_rss_hash_opts(ndc, info);
  1332. return -EOPNOTSUPP;
  1333. }
  1334. #ifdef CONFIG_NET_POLL_CONTROLLER
  1335. static void netvsc_poll_controller(struct net_device *dev)
  1336. {
  1337. struct net_device_context *ndc = netdev_priv(dev);
  1338. struct netvsc_device *ndev;
  1339. int i;
  1340. rcu_read_lock();
  1341. ndev = rcu_dereference(ndc->nvdev);
  1342. if (ndev) {
  1343. for (i = 0; i < ndev->num_chn; i++) {
  1344. struct netvsc_channel *nvchan = &ndev->chan_table[i];
  1345. napi_schedule(&nvchan->napi);
  1346. }
  1347. }
  1348. rcu_read_unlock();
  1349. }
  1350. #endif
  1351. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  1352. {
  1353. return NETVSC_HASH_KEYLEN;
  1354. }
  1355. static u32 netvsc_rss_indir_size(struct net_device *dev)
  1356. {
  1357. return ITAB_NUM;
  1358. }
  1359. static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  1360. u8 *hfunc)
  1361. {
  1362. struct net_device_context *ndc = netdev_priv(dev);
  1363. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1364. struct rndis_device *rndis_dev;
  1365. int i;
  1366. if (!ndev)
  1367. return -ENODEV;
  1368. if (hfunc)
  1369. *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  1370. rndis_dev = ndev->extension;
  1371. if (indir) {
  1372. for (i = 0; i < ITAB_NUM; i++)
  1373. indir[i] = ndc->rx_table[i];
  1374. }
  1375. if (key)
  1376. memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  1377. return 0;
  1378. }
  1379. static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
  1380. const u8 *key, const u8 hfunc)
  1381. {
  1382. struct net_device_context *ndc = netdev_priv(dev);
  1383. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1384. struct rndis_device *rndis_dev;
  1385. int i;
  1386. if (!ndev)
  1387. return -ENODEV;
  1388. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  1389. return -EOPNOTSUPP;
  1390. rndis_dev = ndev->extension;
  1391. if (indir) {
  1392. for (i = 0; i < ITAB_NUM; i++)
  1393. if (indir[i] >= ndev->num_chn)
  1394. return -EINVAL;
  1395. for (i = 0; i < ITAB_NUM; i++)
  1396. ndc->rx_table[i] = indir[i];
  1397. }
  1398. if (!key) {
  1399. if (!indir)
  1400. return 0;
  1401. key = rndis_dev->rss_key;
  1402. }
  1403. return rndis_filter_set_rss_param(rndis_dev, key);
  1404. }
  1405. /* Hyper-V RNDIS protocol does not have ring in the HW sense.
  1406. * It does have pre-allocated receive area which is divided into sections.
  1407. */
  1408. static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
  1409. struct ethtool_ringparam *ring)
  1410. {
  1411. u32 max_buf_size;
  1412. ring->rx_pending = nvdev->recv_section_cnt;
  1413. ring->tx_pending = nvdev->send_section_cnt;
  1414. if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  1415. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  1416. else
  1417. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  1418. ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
  1419. ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
  1420. / nvdev->send_section_size;
  1421. }
  1422. static void netvsc_get_ringparam(struct net_device *ndev,
  1423. struct ethtool_ringparam *ring)
  1424. {
  1425. struct net_device_context *ndevctx = netdev_priv(ndev);
  1426. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1427. if (!nvdev)
  1428. return;
  1429. __netvsc_get_ringparam(nvdev, ring);
  1430. }
  1431. static int netvsc_set_ringparam(struct net_device *ndev,
  1432. struct ethtool_ringparam *ring)
  1433. {
  1434. struct net_device_context *ndevctx = netdev_priv(ndev);
  1435. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1436. struct netvsc_device_info *device_info;
  1437. struct ethtool_ringparam orig;
  1438. u32 new_tx, new_rx;
  1439. int ret = 0;
  1440. if (!nvdev || nvdev->destroy)
  1441. return -ENODEV;
  1442. memset(&orig, 0, sizeof(orig));
  1443. __netvsc_get_ringparam(nvdev, &orig);
  1444. new_tx = clamp_t(u32, ring->tx_pending,
  1445. NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
  1446. new_rx = clamp_t(u32, ring->rx_pending,
  1447. NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
  1448. if (new_tx == orig.tx_pending &&
  1449. new_rx == orig.rx_pending)
  1450. return 0; /* no change */
  1451. device_info = netvsc_devinfo_get(nvdev);
  1452. if (!device_info)
  1453. return -ENOMEM;
  1454. device_info->send_sections = new_tx;
  1455. device_info->recv_sections = new_rx;
  1456. ret = netvsc_detach(ndev, nvdev);
  1457. if (ret)
  1458. goto out;
  1459. ret = netvsc_attach(ndev, device_info);
  1460. if (ret) {
  1461. device_info->send_sections = orig.tx_pending;
  1462. device_info->recv_sections = orig.rx_pending;
  1463. if (netvsc_attach(ndev, device_info))
  1464. netdev_err(ndev, "restoring ringparam failed");
  1465. }
  1466. out:
  1467. kfree(device_info);
  1468. return ret;
  1469. }
  1470. static u32 netvsc_get_msglevel(struct net_device *ndev)
  1471. {
  1472. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1473. return ndev_ctx->msg_enable;
  1474. }
  1475. static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
  1476. {
  1477. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1478. ndev_ctx->msg_enable = val;
  1479. }
  1480. static const struct ethtool_ops ethtool_ops = {
  1481. .get_drvinfo = netvsc_get_drvinfo,
  1482. .get_msglevel = netvsc_get_msglevel,
  1483. .set_msglevel = netvsc_set_msglevel,
  1484. .get_link = ethtool_op_get_link,
  1485. .get_ethtool_stats = netvsc_get_ethtool_stats,
  1486. .get_sset_count = netvsc_get_sset_count,
  1487. .get_strings = netvsc_get_strings,
  1488. .get_channels = netvsc_get_channels,
  1489. .set_channels = netvsc_set_channels,
  1490. .get_ts_info = ethtool_op_get_ts_info,
  1491. .get_rxnfc = netvsc_get_rxnfc,
  1492. .set_rxnfc = netvsc_set_rxnfc,
  1493. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  1494. .get_rxfh_indir_size = netvsc_rss_indir_size,
  1495. .get_rxfh = netvsc_get_rxfh,
  1496. .set_rxfh = netvsc_set_rxfh,
  1497. .get_link_ksettings = netvsc_get_link_ksettings,
  1498. .set_link_ksettings = netvsc_set_link_ksettings,
  1499. .get_ringparam = netvsc_get_ringparam,
  1500. .set_ringparam = netvsc_set_ringparam,
  1501. };
  1502. static const struct net_device_ops device_ops = {
  1503. .ndo_open = netvsc_open,
  1504. .ndo_stop = netvsc_close,
  1505. .ndo_start_xmit = netvsc_start_xmit,
  1506. .ndo_change_rx_flags = netvsc_change_rx_flags,
  1507. .ndo_set_rx_mode = netvsc_set_rx_mode,
  1508. .ndo_change_mtu = netvsc_change_mtu,
  1509. .ndo_validate_addr = eth_validate_addr,
  1510. .ndo_set_mac_address = netvsc_set_mac_addr,
  1511. .ndo_select_queue = netvsc_select_queue,
  1512. .ndo_get_stats64 = netvsc_get_stats64,
  1513. #ifdef CONFIG_NET_POLL_CONTROLLER
  1514. .ndo_poll_controller = netvsc_poll_controller,
  1515. #endif
  1516. };
  1517. /*
  1518. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  1519. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  1520. * present send GARP packet to network peers with netif_notify_peers().
  1521. */
  1522. static void netvsc_link_change(struct work_struct *w)
  1523. {
  1524. struct net_device_context *ndev_ctx =
  1525. container_of(w, struct net_device_context, dwork.work);
  1526. struct hv_device *device_obj = ndev_ctx->device_ctx;
  1527. struct net_device *net = hv_get_drvdata(device_obj);
  1528. struct netvsc_device *net_device;
  1529. struct rndis_device *rdev;
  1530. struct netvsc_reconfig *event = NULL;
  1531. bool notify = false, reschedule = false;
  1532. unsigned long flags, next_reconfig, delay;
  1533. /* if changes are happening, comeback later */
  1534. if (!rtnl_trylock()) {
  1535. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1536. return;
  1537. }
  1538. net_device = rtnl_dereference(ndev_ctx->nvdev);
  1539. if (!net_device)
  1540. goto out_unlock;
  1541. rdev = net_device->extension;
  1542. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  1543. if (time_is_after_jiffies(next_reconfig)) {
  1544. /* link_watch only sends one notification with current state
  1545. * per second, avoid doing reconfig more frequently. Handle
  1546. * wrap around.
  1547. */
  1548. delay = next_reconfig - jiffies;
  1549. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1550. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1551. goto out_unlock;
  1552. }
  1553. ndev_ctx->last_reconfig = jiffies;
  1554. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1555. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1556. event = list_first_entry(&ndev_ctx->reconfig_events,
  1557. struct netvsc_reconfig, list);
  1558. list_del(&event->list);
  1559. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1560. }
  1561. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1562. if (!event)
  1563. goto out_unlock;
  1564. switch (event->event) {
  1565. /* Only the following events are possible due to the check in
  1566. * netvsc_linkstatus_callback()
  1567. */
  1568. case RNDIS_STATUS_MEDIA_CONNECT:
  1569. if (rdev->link_state) {
  1570. rdev->link_state = false;
  1571. netif_carrier_on(net);
  1572. netvsc_tx_enable(net_device, net);
  1573. } else {
  1574. notify = true;
  1575. }
  1576. kfree(event);
  1577. break;
  1578. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1579. if (!rdev->link_state) {
  1580. rdev->link_state = true;
  1581. netif_carrier_off(net);
  1582. netvsc_tx_disable(net_device, net);
  1583. }
  1584. kfree(event);
  1585. break;
  1586. case RNDIS_STATUS_NETWORK_CHANGE:
  1587. /* Only makes sense if carrier is present */
  1588. if (!rdev->link_state) {
  1589. rdev->link_state = true;
  1590. netif_carrier_off(net);
  1591. netvsc_tx_disable(net_device, net);
  1592. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1593. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1594. list_add(&event->list, &ndev_ctx->reconfig_events);
  1595. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1596. reschedule = true;
  1597. }
  1598. break;
  1599. }
  1600. rtnl_unlock();
  1601. if (notify)
  1602. netdev_notify_peers(net);
  1603. /* link_watch only sends one notification with current state per
  1604. * second, handle next reconfig event in 2 seconds.
  1605. */
  1606. if (reschedule)
  1607. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1608. return;
  1609. out_unlock:
  1610. rtnl_unlock();
  1611. }
  1612. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  1613. {
  1614. struct net_device_context *net_device_ctx;
  1615. struct net_device *dev;
  1616. dev = netdev_master_upper_dev_get(vf_netdev);
  1617. if (!dev || dev->netdev_ops != &device_ops)
  1618. return NULL; /* not a netvsc device */
  1619. net_device_ctx = netdev_priv(dev);
  1620. if (!rtnl_dereference(net_device_ctx->nvdev))
  1621. return NULL; /* device is removed */
  1622. return dev;
  1623. }
  1624. /* Called when VF is injecting data into network stack.
  1625. * Change the associated network device from VF to netvsc.
  1626. * note: already called with rcu_read_lock
  1627. */
  1628. static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
  1629. {
  1630. struct sk_buff *skb = *pskb;
  1631. struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
  1632. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1633. struct netvsc_vf_pcpu_stats *pcpu_stats
  1634. = this_cpu_ptr(ndev_ctx->vf_stats);
  1635. skb = skb_share_check(skb, GFP_ATOMIC);
  1636. if (unlikely(!skb))
  1637. return RX_HANDLER_CONSUMED;
  1638. *pskb = skb;
  1639. skb->dev = ndev;
  1640. u64_stats_update_begin(&pcpu_stats->syncp);
  1641. pcpu_stats->rx_packets++;
  1642. pcpu_stats->rx_bytes += skb->len;
  1643. u64_stats_update_end(&pcpu_stats->syncp);
  1644. return RX_HANDLER_ANOTHER;
  1645. }
  1646. static int netvsc_vf_join(struct net_device *vf_netdev,
  1647. struct net_device *ndev)
  1648. {
  1649. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1650. int ret;
  1651. ret = netdev_rx_handler_register(vf_netdev,
  1652. netvsc_vf_handle_frame, ndev);
  1653. if (ret != 0) {
  1654. netdev_err(vf_netdev,
  1655. "can not register netvsc VF receive handler (err = %d)\n",
  1656. ret);
  1657. goto rx_handler_failed;
  1658. }
  1659. ret = netdev_master_upper_dev_link(vf_netdev, ndev,
  1660. NULL, NULL, NULL);
  1661. if (ret != 0) {
  1662. netdev_err(vf_netdev,
  1663. "can not set master device %s (err = %d)\n",
  1664. ndev->name, ret);
  1665. goto upper_link_failed;
  1666. }
  1667. /* set slave flag before open to prevent IPv6 addrconf */
  1668. vf_netdev->flags |= IFF_SLAVE;
  1669. schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
  1670. call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
  1671. netdev_info(vf_netdev, "joined to %s\n", ndev->name);
  1672. return 0;
  1673. upper_link_failed:
  1674. netdev_rx_handler_unregister(vf_netdev);
  1675. rx_handler_failed:
  1676. return ret;
  1677. }
  1678. static void __netvsc_vf_setup(struct net_device *ndev,
  1679. struct net_device *vf_netdev)
  1680. {
  1681. int ret;
  1682. /* Align MTU of VF with master */
  1683. ret = dev_set_mtu(vf_netdev, ndev->mtu);
  1684. if (ret)
  1685. netdev_warn(vf_netdev,
  1686. "unable to change mtu to %u\n", ndev->mtu);
  1687. /* set multicast etc flags on VF */
  1688. dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE);
  1689. /* sync address list from ndev to VF */
  1690. netif_addr_lock_bh(ndev);
  1691. dev_uc_sync(vf_netdev, ndev);
  1692. dev_mc_sync(vf_netdev, ndev);
  1693. netif_addr_unlock_bh(ndev);
  1694. if (netif_running(ndev)) {
  1695. ret = dev_open(vf_netdev);
  1696. if (ret)
  1697. netdev_warn(vf_netdev,
  1698. "unable to open: %d\n", ret);
  1699. }
  1700. }
  1701. /* Setup VF as slave of the synthetic device.
  1702. * Runs in workqueue to avoid recursion in netlink callbacks.
  1703. */
  1704. static void netvsc_vf_setup(struct work_struct *w)
  1705. {
  1706. struct net_device_context *ndev_ctx
  1707. = container_of(w, struct net_device_context, vf_takeover.work);
  1708. struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1709. struct net_device *vf_netdev;
  1710. if (!rtnl_trylock()) {
  1711. schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
  1712. return;
  1713. }
  1714. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1715. if (vf_netdev)
  1716. __netvsc_vf_setup(ndev, vf_netdev);
  1717. rtnl_unlock();
  1718. }
  1719. /* Find netvsc by VF serial number.
  1720. * The PCI hyperv controller records the serial number as the slot kobj name.
  1721. */
  1722. static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
  1723. {
  1724. struct device *parent = vf_netdev->dev.parent;
  1725. struct net_device_context *ndev_ctx;
  1726. struct pci_dev *pdev;
  1727. u32 serial;
  1728. if (!parent || !dev_is_pci(parent))
  1729. return NULL; /* not a PCI device */
  1730. pdev = to_pci_dev(parent);
  1731. if (!pdev->slot) {
  1732. netdev_notice(vf_netdev, "no PCI slot information\n");
  1733. return NULL;
  1734. }
  1735. if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
  1736. netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
  1737. pci_slot_name(pdev->slot));
  1738. return NULL;
  1739. }
  1740. list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
  1741. if (!ndev_ctx->vf_alloc)
  1742. continue;
  1743. if (ndev_ctx->vf_serial == serial)
  1744. return hv_get_drvdata(ndev_ctx->device_ctx);
  1745. }
  1746. netdev_notice(vf_netdev,
  1747. "no netdev found for vf serial:%u\n", serial);
  1748. return NULL;
  1749. }
  1750. static int netvsc_register_vf(struct net_device *vf_netdev)
  1751. {
  1752. struct net_device_context *net_device_ctx;
  1753. struct netvsc_device *netvsc_dev;
  1754. struct net_device *ndev;
  1755. int ret;
  1756. if (vf_netdev->addr_len != ETH_ALEN)
  1757. return NOTIFY_DONE;
  1758. ndev = get_netvsc_byslot(vf_netdev);
  1759. if (!ndev)
  1760. return NOTIFY_DONE;
  1761. net_device_ctx = netdev_priv(ndev);
  1762. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1763. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1764. return NOTIFY_DONE;
  1765. /* if syntihetic interface is a different namespace,
  1766. * then move the VF to that namespace; join will be
  1767. * done again in that context.
  1768. */
  1769. if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
  1770. ret = dev_change_net_namespace(vf_netdev,
  1771. dev_net(ndev), "eth%d");
  1772. if (ret)
  1773. netdev_err(vf_netdev,
  1774. "could not move to same namespace as %s: %d\n",
  1775. ndev->name, ret);
  1776. else
  1777. netdev_info(vf_netdev,
  1778. "VF moved to namespace with: %s\n",
  1779. ndev->name);
  1780. return NOTIFY_DONE;
  1781. }
  1782. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1783. if (netvsc_vf_join(vf_netdev, ndev) != 0)
  1784. return NOTIFY_DONE;
  1785. dev_hold(vf_netdev);
  1786. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1787. return NOTIFY_OK;
  1788. }
  1789. /* VF up/down change detected, schedule to change data path */
  1790. static int netvsc_vf_changed(struct net_device *vf_netdev)
  1791. {
  1792. struct net_device_context *net_device_ctx;
  1793. struct netvsc_device *netvsc_dev;
  1794. struct net_device *ndev;
  1795. bool vf_is_up = netif_running(vf_netdev);
  1796. ndev = get_netvsc_byref(vf_netdev);
  1797. if (!ndev)
  1798. return NOTIFY_DONE;
  1799. net_device_ctx = netdev_priv(ndev);
  1800. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1801. if (!netvsc_dev)
  1802. return NOTIFY_DONE;
  1803. netvsc_switch_datapath(ndev, vf_is_up);
  1804. netdev_info(ndev, "Data path switched %s VF: %s\n",
  1805. vf_is_up ? "to" : "from", vf_netdev->name);
  1806. return NOTIFY_OK;
  1807. }
  1808. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1809. {
  1810. struct net_device *ndev;
  1811. struct net_device_context *net_device_ctx;
  1812. ndev = get_netvsc_byref(vf_netdev);
  1813. if (!ndev)
  1814. return NOTIFY_DONE;
  1815. net_device_ctx = netdev_priv(ndev);
  1816. cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
  1817. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1818. netdev_rx_handler_unregister(vf_netdev);
  1819. netdev_upper_dev_unlink(vf_netdev, ndev);
  1820. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1821. dev_put(vf_netdev);
  1822. return NOTIFY_OK;
  1823. }
  1824. static int netvsc_probe(struct hv_device *dev,
  1825. const struct hv_vmbus_device_id *dev_id)
  1826. {
  1827. struct net_device *net = NULL;
  1828. struct net_device_context *net_device_ctx;
  1829. struct netvsc_device_info *device_info = NULL;
  1830. struct netvsc_device *nvdev;
  1831. int ret = -ENOMEM;
  1832. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1833. VRSS_CHANNEL_MAX);
  1834. if (!net)
  1835. goto no_net;
  1836. netif_carrier_off(net);
  1837. netvsc_init_settings(net);
  1838. net_device_ctx = netdev_priv(net);
  1839. net_device_ctx->device_ctx = dev;
  1840. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1841. if (netif_msg_probe(net_device_ctx))
  1842. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1843. net_device_ctx->msg_enable);
  1844. hv_set_drvdata(dev, net);
  1845. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1846. spin_lock_init(&net_device_ctx->lock);
  1847. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1848. INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
  1849. net_device_ctx->vf_stats
  1850. = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
  1851. if (!net_device_ctx->vf_stats)
  1852. goto no_stats;
  1853. net->netdev_ops = &device_ops;
  1854. net->ethtool_ops = &ethtool_ops;
  1855. SET_NETDEV_DEV(net, &dev->device);
  1856. /* We always need headroom for rndis header */
  1857. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1858. /* Initialize the number of queues to be 1, we may change it if more
  1859. * channels are offered later.
  1860. */
  1861. netif_set_real_num_tx_queues(net, 1);
  1862. netif_set_real_num_rx_queues(net, 1);
  1863. /* Notify the netvsc driver of the new device */
  1864. device_info = netvsc_devinfo_get(NULL);
  1865. if (!device_info) {
  1866. ret = -ENOMEM;
  1867. goto devinfo_failed;
  1868. }
  1869. nvdev = rndis_filter_device_add(dev, device_info);
  1870. if (IS_ERR(nvdev)) {
  1871. ret = PTR_ERR(nvdev);
  1872. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1873. goto rndis_failed;
  1874. }
  1875. memcpy(net->dev_addr, device_info->mac_adr, ETH_ALEN);
  1876. /* We must get rtnl lock before scheduling nvdev->subchan_work,
  1877. * otherwise netvsc_subchan_work() can get rtnl lock first and wait
  1878. * all subchannels to show up, but that may not happen because
  1879. * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
  1880. * -> ... -> device_add() -> ... -> __device_attach() can't get
  1881. * the device lock, so all the subchannels can't be processed --
  1882. * finally netvsc_subchan_work() hangs for ever.
  1883. */
  1884. rtnl_lock();
  1885. if (nvdev->num_chn > 1)
  1886. schedule_work(&nvdev->subchan_work);
  1887. /* hw_features computed in rndis_netdev_set_hwcaps() */
  1888. net->features = net->hw_features |
  1889. NETIF_F_HIGHDMA | NETIF_F_SG |
  1890. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1891. net->vlan_features = net->features;
  1892. netdev_lockdep_set_classes(net);
  1893. /* MTU range: 68 - 1500 or 65521 */
  1894. net->min_mtu = NETVSC_MTU_MIN;
  1895. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1896. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1897. else
  1898. net->max_mtu = ETH_DATA_LEN;
  1899. nvdev->tx_disable = false;
  1900. ret = register_netdevice(net);
  1901. if (ret != 0) {
  1902. pr_err("Unable to register netdev.\n");
  1903. goto register_failed;
  1904. }
  1905. list_add(&net_device_ctx->list, &netvsc_dev_list);
  1906. rtnl_unlock();
  1907. kfree(device_info);
  1908. return 0;
  1909. register_failed:
  1910. rtnl_unlock();
  1911. rndis_filter_device_remove(dev, nvdev);
  1912. rndis_failed:
  1913. kfree(device_info);
  1914. devinfo_failed:
  1915. free_percpu(net_device_ctx->vf_stats);
  1916. no_stats:
  1917. hv_set_drvdata(dev, NULL);
  1918. free_netdev(net);
  1919. no_net:
  1920. return ret;
  1921. }
  1922. static int netvsc_remove(struct hv_device *dev)
  1923. {
  1924. struct net_device_context *ndev_ctx;
  1925. struct net_device *vf_netdev, *net;
  1926. struct netvsc_device *nvdev;
  1927. net = hv_get_drvdata(dev);
  1928. if (net == NULL) {
  1929. dev_err(&dev->device, "No net device to remove\n");
  1930. return 0;
  1931. }
  1932. ndev_ctx = netdev_priv(net);
  1933. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1934. rtnl_lock();
  1935. nvdev = rtnl_dereference(ndev_ctx->nvdev);
  1936. if (nvdev)
  1937. cancel_work_sync(&nvdev->subchan_work);
  1938. /*
  1939. * Call to the vsc driver to let it know that the device is being
  1940. * removed. Also blocks mtu and channel changes.
  1941. */
  1942. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1943. if (vf_netdev)
  1944. netvsc_unregister_vf(vf_netdev);
  1945. if (nvdev)
  1946. rndis_filter_device_remove(dev, nvdev);
  1947. unregister_netdevice(net);
  1948. list_del(&ndev_ctx->list);
  1949. rtnl_unlock();
  1950. hv_set_drvdata(dev, NULL);
  1951. free_percpu(ndev_ctx->vf_stats);
  1952. free_netdev(net);
  1953. return 0;
  1954. }
  1955. static const struct hv_vmbus_device_id id_table[] = {
  1956. /* Network guid */
  1957. { HV_NIC_GUID, },
  1958. { },
  1959. };
  1960. MODULE_DEVICE_TABLE(vmbus, id_table);
  1961. /* The one and only one */
  1962. static struct hv_driver netvsc_drv = {
  1963. .name = KBUILD_MODNAME,
  1964. .id_table = id_table,
  1965. .probe = netvsc_probe,
  1966. .remove = netvsc_remove,
  1967. .driver = {
  1968. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  1969. },
  1970. };
  1971. /*
  1972. * On Hyper-V, every VF interface is matched with a corresponding
  1973. * synthetic interface. The synthetic interface is presented first
  1974. * to the guest. When the corresponding VF instance is registered,
  1975. * we will take care of switching the data path.
  1976. */
  1977. static int netvsc_netdev_event(struct notifier_block *this,
  1978. unsigned long event, void *ptr)
  1979. {
  1980. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1981. /* Skip our own events */
  1982. if (event_dev->netdev_ops == &device_ops)
  1983. return NOTIFY_DONE;
  1984. /* Avoid non-Ethernet type devices */
  1985. if (event_dev->type != ARPHRD_ETHER)
  1986. return NOTIFY_DONE;
  1987. /* Avoid Vlan dev with same MAC registering as VF */
  1988. if (is_vlan_dev(event_dev))
  1989. return NOTIFY_DONE;
  1990. /* Avoid Bonding master dev with same MAC registering as VF */
  1991. if ((event_dev->priv_flags & IFF_BONDING) &&
  1992. (event_dev->flags & IFF_MASTER))
  1993. return NOTIFY_DONE;
  1994. switch (event) {
  1995. case NETDEV_REGISTER:
  1996. return netvsc_register_vf(event_dev);
  1997. case NETDEV_UNREGISTER:
  1998. return netvsc_unregister_vf(event_dev);
  1999. case NETDEV_UP:
  2000. case NETDEV_DOWN:
  2001. return netvsc_vf_changed(event_dev);
  2002. default:
  2003. return NOTIFY_DONE;
  2004. }
  2005. }
  2006. static struct notifier_block netvsc_netdev_notifier = {
  2007. .notifier_call = netvsc_netdev_event,
  2008. };
  2009. static void __exit netvsc_drv_exit(void)
  2010. {
  2011. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  2012. vmbus_driver_unregister(&netvsc_drv);
  2013. }
  2014. static int __init netvsc_drv_init(void)
  2015. {
  2016. int ret;
  2017. if (ring_size < RING_SIZE_MIN) {
  2018. ring_size = RING_SIZE_MIN;
  2019. pr_info("Increased ring_size to %u (min allowed)\n",
  2020. ring_size);
  2021. }
  2022. netvsc_ring_bytes = ring_size * PAGE_SIZE;
  2023. ret = vmbus_driver_register(&netvsc_drv);
  2024. if (ret)
  2025. return ret;
  2026. register_netdevice_notifier(&netvsc_netdev_notifier);
  2027. return 0;
  2028. }
  2029. MODULE_LICENSE("GPL");
  2030. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  2031. module_init(netvsc_drv_init);
  2032. module_exit(netvsc_drv_exit);