virtio_net.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /* A network driver using virtio.
  2. *
  3. * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. //#define DEBUG
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/ethtool.h>
  23. #include <linux/module.h>
  24. #include <linux/virtio.h>
  25. #include <linux/virtio_net.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/if_vlan.h>
  28. #include <linux/slab.h>
  29. static int napi_weight = 128;
  30. module_param(napi_weight, int, 0444);
  31. static bool csum = true, gso = true;
  32. module_param(csum, bool, 0444);
  33. module_param(gso, bool, 0444);
  34. /* FIXME: MTU in config. */
  35. #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  36. #define GOOD_COPY_LEN 128
  37. #define VIRTNET_SEND_COMMAND_SG_MAX 2
  38. #define VIRTNET_DRIVER_VERSION "1.0.0"
  39. struct virtnet_stats {
  40. struct u64_stats_sync tx_syncp;
  41. struct u64_stats_sync rx_syncp;
  42. u64 tx_bytes;
  43. u64 tx_packets;
  44. u64 rx_bytes;
  45. u64 rx_packets;
  46. };
  47. struct virtnet_info {
  48. struct virtio_device *vdev;
  49. struct virtqueue *rvq, *svq, *cvq;
  50. struct net_device *dev;
  51. struct napi_struct napi;
  52. unsigned int status;
  53. /* Number of input buffers, and max we've ever had. */
  54. unsigned int num, max;
  55. /* I like... big packets and I cannot lie! */
  56. bool big_packets;
  57. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  58. bool mergeable_rx_bufs;
  59. /* enable config space updates */
  60. bool config_enable;
  61. /* Active statistics */
  62. struct virtnet_stats __percpu *stats;
  63. /* Work struct for refilling if we run low on memory. */
  64. struct delayed_work refill;
  65. /* Work struct for config space updates */
  66. struct work_struct config_work;
  67. /* Lock for config space updates */
  68. struct mutex config_lock;
  69. /* Chain pages by the private ptr. */
  70. struct page *pages;
  71. /* fragments + linear part + virtio header */
  72. struct scatterlist rx_sg[MAX_SKB_FRAGS + 2];
  73. struct scatterlist tx_sg[MAX_SKB_FRAGS + 2];
  74. };
  75. struct skb_vnet_hdr {
  76. union {
  77. struct virtio_net_hdr hdr;
  78. struct virtio_net_hdr_mrg_rxbuf mhdr;
  79. };
  80. };
  81. struct padded_vnet_hdr {
  82. struct virtio_net_hdr hdr;
  83. /*
  84. * virtio_net_hdr should be in a separated sg buffer because of a
  85. * QEMU bug, and data sg buffer shares same page with this header sg.
  86. * This padding makes next sg 16 byte aligned after virtio_net_hdr.
  87. */
  88. char padding[6];
  89. };
  90. static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
  91. {
  92. return (struct skb_vnet_hdr *)skb->cb;
  93. }
  94. /*
  95. * private is used to chain pages for big packets, put the whole
  96. * most recent used list in the beginning for reuse
  97. */
  98. static void give_pages(struct virtnet_info *vi, struct page *page)
  99. {
  100. struct page *end;
  101. /* Find end of list, sew whole thing into vi->pages. */
  102. for (end = page; end->private; end = (struct page *)end->private);
  103. end->private = (unsigned long)vi->pages;
  104. vi->pages = page;
  105. }
  106. static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
  107. {
  108. struct page *p = vi->pages;
  109. if (p) {
  110. vi->pages = (struct page *)p->private;
  111. /* clear private here, it is used to chain pages */
  112. p->private = 0;
  113. } else
  114. p = alloc_page(gfp_mask);
  115. return p;
  116. }
  117. static void skb_xmit_done(struct virtqueue *svq)
  118. {
  119. struct virtnet_info *vi = svq->vdev->priv;
  120. /* Suppress further interrupts. */
  121. virtqueue_disable_cb(svq);
  122. /* We were probably waiting for more output buffers. */
  123. netif_wake_queue(vi->dev);
  124. }
  125. static void set_skb_frag(struct sk_buff *skb, struct page *page,
  126. unsigned int offset, unsigned int *len)
  127. {
  128. int size = min((unsigned)PAGE_SIZE - offset, *len);
  129. int i = skb_shinfo(skb)->nr_frags;
  130. __skb_fill_page_desc(skb, i, page, offset, size);
  131. skb->data_len += size;
  132. skb->len += size;
  133. skb->truesize += PAGE_SIZE;
  134. skb_shinfo(skb)->nr_frags++;
  135. *len -= size;
  136. }
  137. /* Called from bottom half context */
  138. static struct sk_buff *page_to_skb(struct virtnet_info *vi,
  139. struct page *page, unsigned int len)
  140. {
  141. struct sk_buff *skb;
  142. struct skb_vnet_hdr *hdr;
  143. unsigned int copy, hdr_len, offset;
  144. char *p;
  145. p = page_address(page);
  146. /* copy small packet so we can reuse these pages for small data */
  147. skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
  148. if (unlikely(!skb))
  149. return NULL;
  150. hdr = skb_vnet_hdr(skb);
  151. if (vi->mergeable_rx_bufs) {
  152. hdr_len = sizeof hdr->mhdr;
  153. offset = hdr_len;
  154. } else {
  155. hdr_len = sizeof hdr->hdr;
  156. offset = sizeof(struct padded_vnet_hdr);
  157. }
  158. memcpy(hdr, p, hdr_len);
  159. len -= hdr_len;
  160. p += offset;
  161. copy = len;
  162. if (copy > skb_tailroom(skb))
  163. copy = skb_tailroom(skb);
  164. memcpy(skb_put(skb, copy), p, copy);
  165. len -= copy;
  166. offset += copy;
  167. /*
  168. * Verify that we can indeed put this data into a skb.
  169. * This is here to handle cases when the device erroneously
  170. * tries to receive more than is possible. This is usually
  171. * the case of a broken device.
  172. */
  173. if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
  174. if (net_ratelimit())
  175. pr_debug("%s: too much data\n", skb->dev->name);
  176. dev_kfree_skb(skb);
  177. return NULL;
  178. }
  179. while (len) {
  180. set_skb_frag(skb, page, offset, &len);
  181. page = (struct page *)page->private;
  182. offset = 0;
  183. }
  184. if (page)
  185. give_pages(vi, page);
  186. return skb;
  187. }
  188. static int receive_mergeable(struct virtnet_info *vi, struct sk_buff *skb)
  189. {
  190. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  191. struct page *page;
  192. int num_buf, i, len;
  193. num_buf = hdr->mhdr.num_buffers;
  194. while (--num_buf) {
  195. i = skb_shinfo(skb)->nr_frags;
  196. if (i >= MAX_SKB_FRAGS) {
  197. pr_debug("%s: packet too long\n", skb->dev->name);
  198. skb->dev->stats.rx_length_errors++;
  199. return -EINVAL;
  200. }
  201. page = virtqueue_get_buf(vi->rvq, &len);
  202. if (!page) {
  203. pr_debug("%s: rx error: %d buffers missing\n",
  204. skb->dev->name, hdr->mhdr.num_buffers);
  205. skb->dev->stats.rx_length_errors++;
  206. return -EINVAL;
  207. }
  208. if (len > PAGE_SIZE)
  209. len = PAGE_SIZE;
  210. set_skb_frag(skb, page, 0, &len);
  211. --vi->num;
  212. }
  213. return 0;
  214. }
  215. static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
  216. {
  217. struct virtnet_info *vi = netdev_priv(dev);
  218. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  219. struct sk_buff *skb;
  220. struct page *page;
  221. struct skb_vnet_hdr *hdr;
  222. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  223. pr_debug("%s: short packet %i\n", dev->name, len);
  224. dev->stats.rx_length_errors++;
  225. if (vi->mergeable_rx_bufs || vi->big_packets)
  226. give_pages(vi, buf);
  227. else
  228. dev_kfree_skb(buf);
  229. return;
  230. }
  231. if (!vi->mergeable_rx_bufs && !vi->big_packets) {
  232. skb = buf;
  233. len -= sizeof(struct virtio_net_hdr);
  234. skb_trim(skb, len);
  235. } else {
  236. page = buf;
  237. skb = page_to_skb(vi, page, len);
  238. if (unlikely(!skb)) {
  239. dev->stats.rx_dropped++;
  240. give_pages(vi, page);
  241. return;
  242. }
  243. if (vi->mergeable_rx_bufs)
  244. if (receive_mergeable(vi, skb)) {
  245. dev_kfree_skb(skb);
  246. return;
  247. }
  248. }
  249. hdr = skb_vnet_hdr(skb);
  250. u64_stats_update_begin(&stats->rx_syncp);
  251. stats->rx_bytes += skb->len;
  252. stats->rx_packets++;
  253. u64_stats_update_end(&stats->rx_syncp);
  254. if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  255. pr_debug("Needs csum!\n");
  256. if (!skb_partial_csum_set(skb,
  257. hdr->hdr.csum_start,
  258. hdr->hdr.csum_offset))
  259. goto frame_err;
  260. } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
  261. skb->ip_summed = CHECKSUM_UNNECESSARY;
  262. }
  263. skb->protocol = eth_type_trans(skb, dev);
  264. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  265. ntohs(skb->protocol), skb->len, skb->pkt_type);
  266. if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  267. pr_debug("GSO!\n");
  268. switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  269. case VIRTIO_NET_HDR_GSO_TCPV4:
  270. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  271. break;
  272. case VIRTIO_NET_HDR_GSO_UDP:
  273. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  274. break;
  275. case VIRTIO_NET_HDR_GSO_TCPV6:
  276. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  277. break;
  278. default:
  279. if (net_ratelimit())
  280. printk(KERN_WARNING "%s: bad gso type %u.\n",
  281. dev->name, hdr->hdr.gso_type);
  282. goto frame_err;
  283. }
  284. if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  285. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  286. skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
  287. if (skb_shinfo(skb)->gso_size == 0) {
  288. if (net_ratelimit())
  289. printk(KERN_WARNING "%s: zero gso size.\n",
  290. dev->name);
  291. goto frame_err;
  292. }
  293. /* Header must be checked, and gso_segs computed. */
  294. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  295. skb_shinfo(skb)->gso_segs = 0;
  296. }
  297. netif_receive_skb(skb);
  298. return;
  299. frame_err:
  300. dev->stats.rx_frame_errors++;
  301. dev_kfree_skb(skb);
  302. }
  303. static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
  304. {
  305. struct sk_buff *skb;
  306. struct skb_vnet_hdr *hdr;
  307. int err;
  308. skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
  309. if (unlikely(!skb))
  310. return -ENOMEM;
  311. skb_put(skb, MAX_PACKET_LEN);
  312. hdr = skb_vnet_hdr(skb);
  313. sg_set_buf(vi->rx_sg, &hdr->hdr, sizeof hdr->hdr);
  314. skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
  315. err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
  316. if (err < 0)
  317. dev_kfree_skb(skb);
  318. return err;
  319. }
  320. static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
  321. {
  322. struct page *first, *list = NULL;
  323. char *p;
  324. int i, err, offset;
  325. /* page in vi->rx_sg[MAX_SKB_FRAGS + 1] is list tail */
  326. for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
  327. first = get_a_page(vi, gfp);
  328. if (!first) {
  329. if (list)
  330. give_pages(vi, list);
  331. return -ENOMEM;
  332. }
  333. sg_set_buf(&vi->rx_sg[i], page_address(first), PAGE_SIZE);
  334. /* chain new page in list head to match sg */
  335. first->private = (unsigned long)list;
  336. list = first;
  337. }
  338. first = get_a_page(vi, gfp);
  339. if (!first) {
  340. give_pages(vi, list);
  341. return -ENOMEM;
  342. }
  343. p = page_address(first);
  344. /* vi->rx_sg[0], vi->rx_sg[1] share the same page */
  345. /* a separated vi->rx_sg[0] for virtio_net_hdr only due to QEMU bug */
  346. sg_set_buf(&vi->rx_sg[0], p, sizeof(struct virtio_net_hdr));
  347. /* vi->rx_sg[1] for data packet, from offset */
  348. offset = sizeof(struct padded_vnet_hdr);
  349. sg_set_buf(&vi->rx_sg[1], p + offset, PAGE_SIZE - offset);
  350. /* chain first in list head */
  351. first->private = (unsigned long)list;
  352. err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
  353. first, gfp);
  354. if (err < 0)
  355. give_pages(vi, first);
  356. return err;
  357. }
  358. static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
  359. {
  360. struct page *page;
  361. int err;
  362. page = get_a_page(vi, gfp);
  363. if (!page)
  364. return -ENOMEM;
  365. sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
  366. err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
  367. if (err < 0)
  368. give_pages(vi, page);
  369. return err;
  370. }
  371. /*
  372. * Returns false if we couldn't fill entirely (OOM).
  373. *
  374. * Normally run in the receive path, but can also be run from ndo_open
  375. * before we're receiving packets, or from refill_work which is
  376. * careful to disable receiving (using napi_disable).
  377. */
  378. static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
  379. {
  380. int err;
  381. bool oom;
  382. do {
  383. if (vi->mergeable_rx_bufs)
  384. err = add_recvbuf_mergeable(vi, gfp);
  385. else if (vi->big_packets)
  386. err = add_recvbuf_big(vi, gfp);
  387. else
  388. err = add_recvbuf_small(vi, gfp);
  389. oom = err == -ENOMEM;
  390. if (err)
  391. break;
  392. ++vi->num;
  393. } while (vi->rvq->num_free);
  394. if (unlikely(vi->num > vi->max))
  395. vi->max = vi->num;
  396. virtqueue_kick(vi->rvq);
  397. return !oom;
  398. }
  399. static void skb_recv_done(struct virtqueue *rvq)
  400. {
  401. struct virtnet_info *vi = rvq->vdev->priv;
  402. /* Schedule NAPI, Suppress further interrupts if successful. */
  403. if (napi_schedule_prep(&vi->napi)) {
  404. virtqueue_disable_cb(rvq);
  405. __napi_schedule(&vi->napi);
  406. }
  407. }
  408. static void virtnet_napi_enable(struct virtnet_info *vi)
  409. {
  410. napi_enable(&vi->napi);
  411. /* If all buffers were filled by other side before we napi_enabled, we
  412. * won't get another interrupt, so process any outstanding packets
  413. * now. virtnet_poll wants re-enable the queue, so we disable here.
  414. * We synchronize against interrupts via NAPI_STATE_SCHED */
  415. if (napi_schedule_prep(&vi->napi)) {
  416. virtqueue_disable_cb(vi->rvq);
  417. local_bh_disable();
  418. __napi_schedule(&vi->napi);
  419. local_bh_enable();
  420. }
  421. }
  422. static void refill_work(struct work_struct *work)
  423. {
  424. struct virtnet_info *vi;
  425. bool still_empty;
  426. vi = container_of(work, struct virtnet_info, refill.work);
  427. napi_disable(&vi->napi);
  428. still_empty = !try_fill_recv(vi, GFP_KERNEL);
  429. virtnet_napi_enable(vi);
  430. /* In theory, this can happen: if we don't get any buffers in
  431. * we will *never* try to fill again. */
  432. if (still_empty)
  433. schedule_delayed_work(&vi->refill, HZ/2);
  434. }
  435. static int virtnet_poll(struct napi_struct *napi, int budget)
  436. {
  437. struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
  438. void *buf;
  439. unsigned int len, received = 0;
  440. again:
  441. while (received < budget &&
  442. (buf = virtqueue_get_buf(vi->rvq, &len)) != NULL) {
  443. receive_buf(vi->dev, buf, len);
  444. --vi->num;
  445. received++;
  446. }
  447. if (vi->num < vi->max / 2) {
  448. if (!try_fill_recv(vi, GFP_ATOMIC))
  449. schedule_delayed_work(&vi->refill, 0);
  450. }
  451. /* Out of packets? */
  452. if (received < budget) {
  453. napi_complete(napi);
  454. if (unlikely(!virtqueue_enable_cb(vi->rvq)) &&
  455. napi_schedule_prep(napi)) {
  456. virtqueue_disable_cb(vi->rvq);
  457. __napi_schedule(napi);
  458. goto again;
  459. }
  460. }
  461. return received;
  462. }
  463. static void free_old_xmit_skbs(struct virtnet_info *vi)
  464. {
  465. struct sk_buff *skb;
  466. unsigned int len;
  467. struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
  468. while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
  469. pr_debug("Sent skb %p\n", skb);
  470. u64_stats_update_begin(&stats->tx_syncp);
  471. stats->tx_bytes += skb->len;
  472. stats->tx_packets++;
  473. u64_stats_update_end(&stats->tx_syncp);
  474. dev_kfree_skb_any(skb);
  475. }
  476. }
  477. static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
  478. {
  479. struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
  480. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  481. unsigned num_sg;
  482. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  483. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  484. hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  485. hdr->hdr.csum_start = skb_checksum_start_offset(skb);
  486. hdr->hdr.csum_offset = skb->csum_offset;
  487. } else {
  488. hdr->hdr.flags = 0;
  489. hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
  490. }
  491. if (skb_is_gso(skb)) {
  492. hdr->hdr.hdr_len = skb_headlen(skb);
  493. hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
  494. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  495. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  496. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  497. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  498. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  499. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  500. else
  501. BUG();
  502. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  503. hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  504. } else {
  505. hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  506. hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
  507. }
  508. hdr->mhdr.num_buffers = 0;
  509. /* Encode metadata header at front. */
  510. if (vi->mergeable_rx_bufs)
  511. sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
  512. else
  513. sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
  514. num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
  515. return virtqueue_add_buf(vi->svq, vi->tx_sg, num_sg,
  516. 0, skb, GFP_ATOMIC);
  517. }
  518. static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
  519. {
  520. struct virtnet_info *vi = netdev_priv(dev);
  521. int err;
  522. /* Free up any pending old buffers before queueing new ones. */
  523. free_old_xmit_skbs(vi);
  524. /* Try to transmit */
  525. err = xmit_skb(vi, skb);
  526. /* This should not happen! */
  527. if (unlikely(err)) {
  528. dev->stats.tx_fifo_errors++;
  529. if (net_ratelimit())
  530. dev_warn(&dev->dev,
  531. "Unexpected TX queue failure: %d\n", err);
  532. dev->stats.tx_dropped++;
  533. kfree_skb(skb);
  534. return NETDEV_TX_OK;
  535. }
  536. virtqueue_kick(vi->svq);
  537. /* Don't wait up for transmitted skbs to be freed. */
  538. skb_orphan(skb);
  539. nf_reset(skb);
  540. /* Apparently nice girls don't return TX_BUSY; stop the queue
  541. * before it gets out of hand. Naturally, this wastes entries. */
  542. if (vi->svq->num_free < 2+MAX_SKB_FRAGS) {
  543. netif_stop_queue(dev);
  544. if (unlikely(!virtqueue_enable_cb_delayed(vi->svq))) {
  545. /* More just got used, free them then recheck. */
  546. free_old_xmit_skbs(vi);
  547. if (vi->svq->num_free >= 2+MAX_SKB_FRAGS) {
  548. netif_start_queue(dev);
  549. virtqueue_disable_cb(vi->svq);
  550. }
  551. }
  552. }
  553. return NETDEV_TX_OK;
  554. }
  555. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  556. {
  557. struct virtnet_info *vi = netdev_priv(dev);
  558. struct virtio_device *vdev = vi->vdev;
  559. int ret;
  560. ret = eth_mac_addr(dev, p);
  561. if (ret)
  562. return ret;
  563. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  564. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  565. dev->dev_addr, dev->addr_len);
  566. return 0;
  567. }
  568. static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
  569. struct rtnl_link_stats64 *tot)
  570. {
  571. struct virtnet_info *vi = netdev_priv(dev);
  572. int cpu;
  573. unsigned int start;
  574. for_each_possible_cpu(cpu) {
  575. struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
  576. u64 tpackets, tbytes, rpackets, rbytes;
  577. do {
  578. start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
  579. tpackets = stats->tx_packets;
  580. tbytes = stats->tx_bytes;
  581. } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
  582. do {
  583. start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
  584. rpackets = stats->rx_packets;
  585. rbytes = stats->rx_bytes;
  586. } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
  587. tot->rx_packets += rpackets;
  588. tot->tx_packets += tpackets;
  589. tot->rx_bytes += rbytes;
  590. tot->tx_bytes += tbytes;
  591. }
  592. tot->tx_dropped = dev->stats.tx_dropped;
  593. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  594. tot->rx_dropped = dev->stats.rx_dropped;
  595. tot->rx_length_errors = dev->stats.rx_length_errors;
  596. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  597. return tot;
  598. }
  599. #ifdef CONFIG_NET_POLL_CONTROLLER
  600. static void virtnet_netpoll(struct net_device *dev)
  601. {
  602. struct virtnet_info *vi = netdev_priv(dev);
  603. napi_schedule(&vi->napi);
  604. }
  605. #endif
  606. static int virtnet_open(struct net_device *dev)
  607. {
  608. struct virtnet_info *vi = netdev_priv(dev);
  609. /* Make sure we have some buffers: if oom use wq. */
  610. if (!try_fill_recv(vi, GFP_KERNEL))
  611. schedule_delayed_work(&vi->refill, 0);
  612. virtnet_napi_enable(vi);
  613. return 0;
  614. }
  615. /*
  616. * Send command via the control virtqueue and check status. Commands
  617. * supported by the hypervisor, as indicated by feature bits, should
  618. * never fail unless improperly formated.
  619. */
  620. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  621. struct scatterlist *data, int out, int in)
  622. {
  623. struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  624. struct virtio_net_ctrl_hdr ctrl;
  625. virtio_net_ctrl_ack status = ~0;
  626. unsigned int tmp;
  627. int i;
  628. /* Caller should know better */
  629. BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
  630. (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
  631. out++; /* Add header */
  632. in++; /* Add return status */
  633. ctrl.class = class;
  634. ctrl.cmd = cmd;
  635. sg_init_table(sg, out + in);
  636. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  637. for_each_sg(data, s, out + in - 2, i)
  638. sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
  639. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  640. BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
  641. virtqueue_kick(vi->cvq);
  642. /*
  643. * Spin for a response, the kick causes an ioport write, trapping
  644. * into the hypervisor, so the request should be handled immediately.
  645. */
  646. while (!virtqueue_get_buf(vi->cvq, &tmp))
  647. cpu_relax();
  648. return status == VIRTIO_NET_OK;
  649. }
  650. static void virtnet_ack_link_announce(struct virtnet_info *vi)
  651. {
  652. rtnl_lock();
  653. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
  654. VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL,
  655. 0, 0))
  656. dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
  657. rtnl_unlock();
  658. }
  659. static int virtnet_close(struct net_device *dev)
  660. {
  661. struct virtnet_info *vi = netdev_priv(dev);
  662. /* Make sure refill_work doesn't re-enable napi! */
  663. cancel_delayed_work_sync(&vi->refill);
  664. napi_disable(&vi->napi);
  665. return 0;
  666. }
  667. static void virtnet_set_rx_mode(struct net_device *dev)
  668. {
  669. struct virtnet_info *vi = netdev_priv(dev);
  670. struct scatterlist sg[2];
  671. u8 promisc, allmulti;
  672. struct virtio_net_ctrl_mac *mac_data;
  673. struct netdev_hw_addr *ha;
  674. int uc_count;
  675. int mc_count;
  676. void *buf;
  677. int i;
  678. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  679. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  680. return;
  681. promisc = ((dev->flags & IFF_PROMISC) != 0);
  682. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  683. sg_init_one(sg, &promisc, sizeof(promisc));
  684. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  685. VIRTIO_NET_CTRL_RX_PROMISC,
  686. sg, 1, 0))
  687. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  688. promisc ? "en" : "dis");
  689. sg_init_one(sg, &allmulti, sizeof(allmulti));
  690. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  691. VIRTIO_NET_CTRL_RX_ALLMULTI,
  692. sg, 1, 0))
  693. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  694. allmulti ? "en" : "dis");
  695. uc_count = netdev_uc_count(dev);
  696. mc_count = netdev_mc_count(dev);
  697. /* MAC filter - use one buffer for both lists */
  698. buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
  699. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  700. mac_data = buf;
  701. if (!buf) {
  702. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  703. return;
  704. }
  705. sg_init_table(sg, 2);
  706. /* Store the unicast list and count in the front of the buffer */
  707. mac_data->entries = uc_count;
  708. i = 0;
  709. netdev_for_each_uc_addr(ha, dev)
  710. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  711. sg_set_buf(&sg[0], mac_data,
  712. sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
  713. /* multicast list and count fill the end */
  714. mac_data = (void *)&mac_data->macs[uc_count][0];
  715. mac_data->entries = mc_count;
  716. i = 0;
  717. netdev_for_each_mc_addr(ha, dev)
  718. memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
  719. sg_set_buf(&sg[1], mac_data,
  720. sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
  721. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  722. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  723. sg, 2, 0))
  724. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  725. kfree(buf);
  726. }
  727. static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  728. {
  729. struct virtnet_info *vi = netdev_priv(dev);
  730. struct scatterlist sg;
  731. sg_init_one(&sg, &vid, sizeof(vid));
  732. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  733. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  734. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  735. return 0;
  736. }
  737. static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  738. {
  739. struct virtnet_info *vi = netdev_priv(dev);
  740. struct scatterlist sg;
  741. sg_init_one(&sg, &vid, sizeof(vid));
  742. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  743. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  744. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  745. return 0;
  746. }
  747. static void virtnet_get_ringparam(struct net_device *dev,
  748. struct ethtool_ringparam *ring)
  749. {
  750. struct virtnet_info *vi = netdev_priv(dev);
  751. ring->rx_max_pending = virtqueue_get_vring_size(vi->rvq);
  752. ring->tx_max_pending = virtqueue_get_vring_size(vi->svq);
  753. ring->rx_pending = ring->rx_max_pending;
  754. ring->tx_pending = ring->tx_max_pending;
  755. }
  756. static void virtnet_get_drvinfo(struct net_device *dev,
  757. struct ethtool_drvinfo *info)
  758. {
  759. struct virtnet_info *vi = netdev_priv(dev);
  760. struct virtio_device *vdev = vi->vdev;
  761. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  762. strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
  763. strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
  764. }
  765. static const struct ethtool_ops virtnet_ethtool_ops = {
  766. .get_drvinfo = virtnet_get_drvinfo,
  767. .get_link = ethtool_op_get_link,
  768. .get_ringparam = virtnet_get_ringparam,
  769. };
  770. #define MIN_MTU 68
  771. #define MAX_MTU 65535
  772. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  773. {
  774. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  775. return -EINVAL;
  776. dev->mtu = new_mtu;
  777. return 0;
  778. }
  779. static const struct net_device_ops virtnet_netdev = {
  780. .ndo_open = virtnet_open,
  781. .ndo_stop = virtnet_close,
  782. .ndo_start_xmit = start_xmit,
  783. .ndo_validate_addr = eth_validate_addr,
  784. .ndo_set_mac_address = virtnet_set_mac_address,
  785. .ndo_set_rx_mode = virtnet_set_rx_mode,
  786. .ndo_change_mtu = virtnet_change_mtu,
  787. .ndo_get_stats64 = virtnet_stats,
  788. .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
  789. .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
  790. #ifdef CONFIG_NET_POLL_CONTROLLER
  791. .ndo_poll_controller = virtnet_netpoll,
  792. #endif
  793. };
  794. static void virtnet_config_changed_work(struct work_struct *work)
  795. {
  796. struct virtnet_info *vi =
  797. container_of(work, struct virtnet_info, config_work);
  798. u16 v;
  799. mutex_lock(&vi->config_lock);
  800. if (!vi->config_enable)
  801. goto done;
  802. if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
  803. offsetof(struct virtio_net_config, status),
  804. &v) < 0)
  805. goto done;
  806. if (v & VIRTIO_NET_S_ANNOUNCE) {
  807. netdev_notify_peers(vi->dev);
  808. virtnet_ack_link_announce(vi);
  809. }
  810. /* Ignore unknown (future) status bits */
  811. v &= VIRTIO_NET_S_LINK_UP;
  812. if (vi->status == v)
  813. goto done;
  814. vi->status = v;
  815. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  816. netif_carrier_on(vi->dev);
  817. netif_wake_queue(vi->dev);
  818. } else {
  819. netif_carrier_off(vi->dev);
  820. netif_stop_queue(vi->dev);
  821. }
  822. done:
  823. mutex_unlock(&vi->config_lock);
  824. }
  825. static void virtnet_config_changed(struct virtio_device *vdev)
  826. {
  827. struct virtnet_info *vi = vdev->priv;
  828. schedule_work(&vi->config_work);
  829. }
  830. static int init_vqs(struct virtnet_info *vi)
  831. {
  832. struct virtqueue *vqs[3];
  833. vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
  834. const char *names[] = { "input", "output", "control" };
  835. int nvqs, err;
  836. /* We expect two virtqueues, receive then send,
  837. * and optionally control. */
  838. nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
  839. err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
  840. if (err)
  841. return err;
  842. vi->rvq = vqs[0];
  843. vi->svq = vqs[1];
  844. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  845. vi->cvq = vqs[2];
  846. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  847. vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
  848. }
  849. return 0;
  850. }
  851. static int virtnet_probe(struct virtio_device *vdev)
  852. {
  853. int err;
  854. struct net_device *dev;
  855. struct virtnet_info *vi;
  856. /* Allocate ourselves a network device with room for our info */
  857. dev = alloc_etherdev(sizeof(struct virtnet_info));
  858. if (!dev)
  859. return -ENOMEM;
  860. /* Set up network device as normal. */
  861. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  862. dev->netdev_ops = &virtnet_netdev;
  863. dev->features = NETIF_F_HIGHDMA;
  864. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  865. SET_NETDEV_DEV(dev, &vdev->dev);
  866. /* Do we support "hardware" checksums? */
  867. if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  868. /* This opens up the world of extra features. */
  869. dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  870. if (csum)
  871. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  872. if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  873. dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
  874. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  875. }
  876. /* Individual feature bits: what can host handle? */
  877. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  878. dev->hw_features |= NETIF_F_TSO;
  879. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  880. dev->hw_features |= NETIF_F_TSO6;
  881. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  882. dev->hw_features |= NETIF_F_TSO_ECN;
  883. if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  884. dev->hw_features |= NETIF_F_UFO;
  885. if (gso)
  886. dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
  887. /* (!csum && gso) case will be fixed by register_netdev() */
  888. }
  889. /* Configuration may specify what MAC to use. Otherwise random. */
  890. if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
  891. offsetof(struct virtio_net_config, mac),
  892. dev->dev_addr, dev->addr_len) < 0)
  893. eth_hw_addr_random(dev);
  894. /* Set up our device-specific information */
  895. vi = netdev_priv(dev);
  896. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  897. vi->dev = dev;
  898. vi->vdev = vdev;
  899. vdev->priv = vi;
  900. vi->pages = NULL;
  901. vi->stats = alloc_percpu(struct virtnet_stats);
  902. err = -ENOMEM;
  903. if (vi->stats == NULL)
  904. goto free;
  905. INIT_DELAYED_WORK(&vi->refill, refill_work);
  906. mutex_init(&vi->config_lock);
  907. vi->config_enable = true;
  908. INIT_WORK(&vi->config_work, virtnet_config_changed_work);
  909. sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
  910. sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
  911. /* If we can receive ANY GSO packets, we must allocate large ones. */
  912. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
  913. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
  914. virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  915. vi->big_packets = true;
  916. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  917. vi->mergeable_rx_bufs = true;
  918. err = init_vqs(vi);
  919. if (err)
  920. goto free_stats;
  921. err = register_netdev(dev);
  922. if (err) {
  923. pr_debug("virtio_net: registering device failed\n");
  924. goto free_vqs;
  925. }
  926. /* Last of all, set up some receive buffers. */
  927. try_fill_recv(vi, GFP_KERNEL);
  928. /* If we didn't even get one input buffer, we're useless. */
  929. if (vi->num == 0) {
  930. err = -ENOMEM;
  931. goto unregister;
  932. }
  933. /* Assume link up if device can't report link status,
  934. otherwise get link status from config. */
  935. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
  936. netif_carrier_off(dev);
  937. schedule_work(&vi->config_work);
  938. } else {
  939. vi->status = VIRTIO_NET_S_LINK_UP;
  940. netif_carrier_on(dev);
  941. }
  942. pr_debug("virtnet: registered device %s\n", dev->name);
  943. return 0;
  944. unregister:
  945. unregister_netdev(dev);
  946. free_vqs:
  947. vdev->config->del_vqs(vdev);
  948. free_stats:
  949. free_percpu(vi->stats);
  950. free:
  951. free_netdev(dev);
  952. return err;
  953. }
  954. static void free_unused_bufs(struct virtnet_info *vi)
  955. {
  956. void *buf;
  957. while (1) {
  958. buf = virtqueue_detach_unused_buf(vi->svq);
  959. if (!buf)
  960. break;
  961. dev_kfree_skb(buf);
  962. }
  963. while (1) {
  964. buf = virtqueue_detach_unused_buf(vi->rvq);
  965. if (!buf)
  966. break;
  967. if (vi->mergeable_rx_bufs || vi->big_packets)
  968. give_pages(vi, buf);
  969. else
  970. dev_kfree_skb(buf);
  971. --vi->num;
  972. }
  973. BUG_ON(vi->num != 0);
  974. }
  975. static void remove_vq_common(struct virtnet_info *vi)
  976. {
  977. vi->vdev->config->reset(vi->vdev);
  978. /* Free unused buffers in both send and recv, if any. */
  979. free_unused_bufs(vi);
  980. vi->vdev->config->del_vqs(vi->vdev);
  981. while (vi->pages)
  982. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  983. }
  984. static void __devexit virtnet_remove(struct virtio_device *vdev)
  985. {
  986. struct virtnet_info *vi = vdev->priv;
  987. /* Prevent config work handler from accessing the device. */
  988. mutex_lock(&vi->config_lock);
  989. vi->config_enable = false;
  990. mutex_unlock(&vi->config_lock);
  991. unregister_netdev(vi->dev);
  992. remove_vq_common(vi);
  993. flush_work(&vi->config_work);
  994. free_percpu(vi->stats);
  995. free_netdev(vi->dev);
  996. }
  997. #ifdef CONFIG_PM
  998. static int virtnet_freeze(struct virtio_device *vdev)
  999. {
  1000. struct virtnet_info *vi = vdev->priv;
  1001. /* Prevent config work handler from accessing the device */
  1002. mutex_lock(&vi->config_lock);
  1003. vi->config_enable = false;
  1004. mutex_unlock(&vi->config_lock);
  1005. netif_device_detach(vi->dev);
  1006. cancel_delayed_work_sync(&vi->refill);
  1007. if (netif_running(vi->dev))
  1008. napi_disable(&vi->napi);
  1009. remove_vq_common(vi);
  1010. flush_work(&vi->config_work);
  1011. return 0;
  1012. }
  1013. static int virtnet_restore(struct virtio_device *vdev)
  1014. {
  1015. struct virtnet_info *vi = vdev->priv;
  1016. int err;
  1017. err = init_vqs(vi);
  1018. if (err)
  1019. return err;
  1020. if (netif_running(vi->dev))
  1021. virtnet_napi_enable(vi);
  1022. netif_device_attach(vi->dev);
  1023. if (!try_fill_recv(vi, GFP_KERNEL))
  1024. schedule_delayed_work(&vi->refill, 0);
  1025. mutex_lock(&vi->config_lock);
  1026. vi->config_enable = true;
  1027. mutex_unlock(&vi->config_lock);
  1028. return 0;
  1029. }
  1030. #endif
  1031. static struct virtio_device_id id_table[] = {
  1032. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  1033. { 0 },
  1034. };
  1035. static unsigned int features[] = {
  1036. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  1037. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  1038. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  1039. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  1040. VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
  1041. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  1042. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  1043. VIRTIO_NET_F_GUEST_ANNOUNCE,
  1044. };
  1045. static struct virtio_driver virtio_net_driver = {
  1046. .feature_table = features,
  1047. .feature_table_size = ARRAY_SIZE(features),
  1048. .driver.name = KBUILD_MODNAME,
  1049. .driver.owner = THIS_MODULE,
  1050. .id_table = id_table,
  1051. .probe = virtnet_probe,
  1052. .remove = __devexit_p(virtnet_remove),
  1053. .config_changed = virtnet_config_changed,
  1054. #ifdef CONFIG_PM
  1055. .freeze = virtnet_freeze,
  1056. .restore = virtnet_restore,
  1057. #endif
  1058. };
  1059. static int __init init(void)
  1060. {
  1061. return register_virtio_driver(&virtio_net_driver);
  1062. }
  1063. static void __exit fini(void)
  1064. {
  1065. unregister_virtio_driver(&virtio_net_driver);
  1066. }
  1067. module_init(init);
  1068. module_exit(fini);
  1069. MODULE_DEVICE_TABLE(virtio, id_table);
  1070. MODULE_DESCRIPTION("Virtio network driver");
  1071. MODULE_LICENSE("GPL");