virtio_net.c 33 KB

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