netvsc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include <linux/mm.h>
  26. #include <linux/delay.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_ether.h>
  31. #include "hyperv_net.h"
  32. static struct netvsc_device *alloc_net_device(struct hv_device *device)
  33. {
  34. struct netvsc_device *net_device;
  35. struct net_device *ndev = hv_get_drvdata(device);
  36. net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
  37. if (!net_device)
  38. return NULL;
  39. init_waitqueue_head(&net_device->wait_drain);
  40. net_device->start_remove = false;
  41. net_device->destroy = false;
  42. net_device->dev = device;
  43. net_device->ndev = ndev;
  44. hv_set_drvdata(device, net_device);
  45. return net_device;
  46. }
  47. static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
  48. {
  49. struct netvsc_device *net_device;
  50. net_device = hv_get_drvdata(device);
  51. if (net_device && net_device->destroy)
  52. net_device = NULL;
  53. return net_device;
  54. }
  55. static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
  56. {
  57. struct netvsc_device *net_device;
  58. net_device = hv_get_drvdata(device);
  59. if (!net_device)
  60. goto get_in_err;
  61. if (net_device->destroy &&
  62. atomic_read(&net_device->num_outstanding_sends) == 0)
  63. net_device = NULL;
  64. get_in_err:
  65. return net_device;
  66. }
  67. static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
  68. {
  69. struct nvsp_message *revoke_packet;
  70. int ret = 0;
  71. struct net_device *ndev = net_device->ndev;
  72. /*
  73. * If we got a section count, it means we received a
  74. * SendReceiveBufferComplete msg (ie sent
  75. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  76. * to send a revoke msg here
  77. */
  78. if (net_device->recv_section_cnt) {
  79. /* Send the revoke receive buffer */
  80. revoke_packet = &net_device->revoke_packet;
  81. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  82. revoke_packet->hdr.msg_type =
  83. NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
  84. revoke_packet->msg.v1_msg.
  85. revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  86. ret = vmbus_sendpacket(net_device->dev->channel,
  87. revoke_packet,
  88. sizeof(struct nvsp_message),
  89. (unsigned long)revoke_packet,
  90. VM_PKT_DATA_INBAND, 0);
  91. /*
  92. * If we failed here, we might as well return and
  93. * have a leak rather than continue and a bugchk
  94. */
  95. if (ret != 0) {
  96. netdev_err(ndev, "unable to send "
  97. "revoke receive buffer to netvsp\n");
  98. return ret;
  99. }
  100. }
  101. /* Teardown the gpadl on the vsp end */
  102. if (net_device->recv_buf_gpadl_handle) {
  103. ret = vmbus_teardown_gpadl(net_device->dev->channel,
  104. net_device->recv_buf_gpadl_handle);
  105. /* If we failed here, we might as well return and have a leak
  106. * rather than continue and a bugchk
  107. */
  108. if (ret != 0) {
  109. netdev_err(ndev,
  110. "unable to teardown receive buffer's gpadl\n");
  111. return ret;
  112. }
  113. net_device->recv_buf_gpadl_handle = 0;
  114. }
  115. if (net_device->recv_buf) {
  116. /* Free up the receive buffer */
  117. free_pages((unsigned long)net_device->recv_buf,
  118. get_order(net_device->recv_buf_size));
  119. net_device->recv_buf = NULL;
  120. }
  121. if (net_device->recv_section) {
  122. net_device->recv_section_cnt = 0;
  123. kfree(net_device->recv_section);
  124. net_device->recv_section = NULL;
  125. }
  126. return ret;
  127. }
  128. static int netvsc_init_recv_buf(struct hv_device *device)
  129. {
  130. int ret = 0;
  131. int t;
  132. struct netvsc_device *net_device;
  133. struct nvsp_message *init_packet;
  134. struct net_device *ndev;
  135. net_device = get_outbound_net_device(device);
  136. if (!net_device)
  137. return -ENODEV;
  138. ndev = net_device->ndev;
  139. net_device->recv_buf =
  140. (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
  141. get_order(net_device->recv_buf_size));
  142. if (!net_device->recv_buf) {
  143. netdev_err(ndev, "unable to allocate receive "
  144. "buffer of size %d\n", net_device->recv_buf_size);
  145. ret = -ENOMEM;
  146. goto cleanup;
  147. }
  148. /*
  149. * Establish the gpadl handle for this buffer on this
  150. * channel. Note: This call uses the vmbus connection rather
  151. * than the channel to establish the gpadl handle.
  152. */
  153. ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
  154. net_device->recv_buf_size,
  155. &net_device->recv_buf_gpadl_handle);
  156. if (ret != 0) {
  157. netdev_err(ndev,
  158. "unable to establish receive buffer's gpadl\n");
  159. goto cleanup;
  160. }
  161. /* Notify the NetVsp of the gpadl handle */
  162. init_packet = &net_device->channel_init_pkt;
  163. memset(init_packet, 0, sizeof(struct nvsp_message));
  164. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
  165. init_packet->msg.v1_msg.send_recv_buf.
  166. gpadl_handle = net_device->recv_buf_gpadl_handle;
  167. init_packet->msg.v1_msg.
  168. send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  169. /* Send the gpadl notification request */
  170. ret = vmbus_sendpacket(device->channel, init_packet,
  171. sizeof(struct nvsp_message),
  172. (unsigned long)init_packet,
  173. VM_PKT_DATA_INBAND,
  174. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  175. if (ret != 0) {
  176. netdev_err(ndev,
  177. "unable to send receive buffer's gpadl to netvsp\n");
  178. goto cleanup;
  179. }
  180. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  181. BUG_ON(t == 0);
  182. /* Check the response */
  183. if (init_packet->msg.v1_msg.
  184. send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
  185. netdev_err(ndev, "Unable to complete receive buffer "
  186. "initialization with NetVsp - status %d\n",
  187. init_packet->msg.v1_msg.
  188. send_recv_buf_complete.status);
  189. ret = -EINVAL;
  190. goto cleanup;
  191. }
  192. /* Parse the response */
  193. net_device->recv_section_cnt = init_packet->msg.
  194. v1_msg.send_recv_buf_complete.num_sections;
  195. net_device->recv_section = kmemdup(
  196. init_packet->msg.v1_msg.send_recv_buf_complete.sections,
  197. net_device->recv_section_cnt *
  198. sizeof(struct nvsp_1_receive_buffer_section),
  199. GFP_KERNEL);
  200. if (net_device->recv_section == NULL) {
  201. ret = -EINVAL;
  202. goto cleanup;
  203. }
  204. /*
  205. * For 1st release, there should only be 1 section that represents the
  206. * entire receive buffer
  207. */
  208. if (net_device->recv_section_cnt != 1 ||
  209. net_device->recv_section->offset != 0) {
  210. ret = -EINVAL;
  211. goto cleanup;
  212. }
  213. goto exit;
  214. cleanup:
  215. netvsc_destroy_recv_buf(net_device);
  216. exit:
  217. return ret;
  218. }
  219. /* Negotiate NVSP protocol version */
  220. static int negotiate_nvsp_ver(struct hv_device *device,
  221. struct netvsc_device *net_device,
  222. struct nvsp_message *init_packet,
  223. u32 nvsp_ver)
  224. {
  225. int ret, t;
  226. memset(init_packet, 0, sizeof(struct nvsp_message));
  227. init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
  228. init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
  229. init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
  230. /* Send the init request */
  231. ret = vmbus_sendpacket(device->channel, init_packet,
  232. sizeof(struct nvsp_message),
  233. (unsigned long)init_packet,
  234. VM_PKT_DATA_INBAND,
  235. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  236. if (ret != 0)
  237. return ret;
  238. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  239. if (t == 0)
  240. return -ETIMEDOUT;
  241. if (init_packet->msg.init_msg.init_complete.status !=
  242. NVSP_STAT_SUCCESS)
  243. return -EINVAL;
  244. if (nvsp_ver != NVSP_PROTOCOL_VERSION_2)
  245. return 0;
  246. /* NVSPv2 only: Send NDIS config */
  247. memset(init_packet, 0, sizeof(struct nvsp_message));
  248. init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
  249. init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu;
  250. init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
  251. ret = vmbus_sendpacket(device->channel, init_packet,
  252. sizeof(struct nvsp_message),
  253. (unsigned long)init_packet,
  254. VM_PKT_DATA_INBAND, 0);
  255. return ret;
  256. }
  257. static int netvsc_connect_vsp(struct hv_device *device)
  258. {
  259. int ret;
  260. struct netvsc_device *net_device;
  261. struct nvsp_message *init_packet;
  262. int ndis_version;
  263. struct net_device *ndev;
  264. net_device = get_outbound_net_device(device);
  265. if (!net_device)
  266. return -ENODEV;
  267. ndev = net_device->ndev;
  268. init_packet = &net_device->channel_init_pkt;
  269. /* Negotiate the latest NVSP protocol supported */
  270. if (negotiate_nvsp_ver(device, net_device, init_packet,
  271. NVSP_PROTOCOL_VERSION_2) == 0) {
  272. net_device->nvsp_version = NVSP_PROTOCOL_VERSION_2;
  273. } else if (negotiate_nvsp_ver(device, net_device, init_packet,
  274. NVSP_PROTOCOL_VERSION_1) == 0) {
  275. net_device->nvsp_version = NVSP_PROTOCOL_VERSION_1;
  276. } else {
  277. ret = -EPROTO;
  278. goto cleanup;
  279. }
  280. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  281. /* Send the ndis version */
  282. memset(init_packet, 0, sizeof(struct nvsp_message));
  283. ndis_version = 0x00050001;
  284. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  285. init_packet->msg.v1_msg.
  286. send_ndis_ver.ndis_major_ver =
  287. (ndis_version & 0xFFFF0000) >> 16;
  288. init_packet->msg.v1_msg.
  289. send_ndis_ver.ndis_minor_ver =
  290. ndis_version & 0xFFFF;
  291. /* Send the init request */
  292. ret = vmbus_sendpacket(device->channel, init_packet,
  293. sizeof(struct nvsp_message),
  294. (unsigned long)init_packet,
  295. VM_PKT_DATA_INBAND, 0);
  296. if (ret != 0)
  297. goto cleanup;
  298. /* Post the big receive buffer to NetVSP */
  299. ret = netvsc_init_recv_buf(device);
  300. cleanup:
  301. return ret;
  302. }
  303. static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
  304. {
  305. netvsc_destroy_recv_buf(net_device);
  306. }
  307. /*
  308. * netvsc_device_remove - Callback when the root bus device is removed
  309. */
  310. int netvsc_device_remove(struct hv_device *device)
  311. {
  312. struct netvsc_device *net_device;
  313. struct hv_netvsc_packet *netvsc_packet, *pos;
  314. unsigned long flags;
  315. net_device = hv_get_drvdata(device);
  316. netvsc_disconnect_vsp(net_device);
  317. /*
  318. * Since we have already drained, we don't need to busy wait
  319. * as was done in final_release_stor_device()
  320. * Note that we cannot set the ext pointer to NULL until
  321. * we have drained - to drain the outgoing packets, we need to
  322. * allow incoming packets.
  323. */
  324. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  325. hv_set_drvdata(device, NULL);
  326. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  327. /*
  328. * At this point, no one should be accessing net_device
  329. * except in here
  330. */
  331. dev_notice(&device->device, "net device safe to remove\n");
  332. /* Now, we can close the channel safely */
  333. vmbus_close(device->channel);
  334. /* Release all resources */
  335. list_for_each_entry_safe(netvsc_packet, pos,
  336. &net_device->recv_pkt_list, list_ent) {
  337. list_del(&netvsc_packet->list_ent);
  338. kfree(netvsc_packet);
  339. }
  340. kfree(net_device);
  341. return 0;
  342. }
  343. #define RING_AVAIL_PERCENT_HIWATER 20
  344. #define RING_AVAIL_PERCENT_LOWATER 10
  345. /*
  346. * Get the percentage of available bytes to write in the ring.
  347. * The return value is in range from 0 to 100.
  348. */
  349. static inline u32 hv_ringbuf_avail_percent(
  350. struct hv_ring_buffer_info *ring_info)
  351. {
  352. u32 avail_read, avail_write;
  353. hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
  354. return avail_write * 100 / ring_info->ring_datasize;
  355. }
  356. static void netvsc_send_completion(struct hv_device *device,
  357. struct vmpacket_descriptor *packet)
  358. {
  359. struct netvsc_device *net_device;
  360. struct nvsp_message *nvsp_packet;
  361. struct hv_netvsc_packet *nvsc_packet;
  362. struct net_device *ndev;
  363. net_device = get_inbound_net_device(device);
  364. if (!net_device)
  365. return;
  366. ndev = net_device->ndev;
  367. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  368. (packet->offset8 << 3));
  369. if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
  370. (nvsp_packet->hdr.msg_type ==
  371. NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
  372. (nvsp_packet->hdr.msg_type ==
  373. NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
  374. /* Copy the response back */
  375. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  376. sizeof(struct nvsp_message));
  377. complete(&net_device->channel_init_wait);
  378. } else if (nvsp_packet->hdr.msg_type ==
  379. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
  380. int num_outstanding_sends;
  381. /* Get the send context */
  382. nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
  383. packet->trans_id;
  384. /* Notify the layer above us */
  385. nvsc_packet->completion.send.send_completion(
  386. nvsc_packet->completion.send.send_completion_ctx);
  387. num_outstanding_sends =
  388. atomic_dec_return(&net_device->num_outstanding_sends);
  389. if (net_device->destroy && num_outstanding_sends == 0)
  390. wake_up(&net_device->wait_drain);
  391. if (netif_queue_stopped(ndev) && !net_device->start_remove &&
  392. (hv_ringbuf_avail_percent(&device->channel->outbound)
  393. > RING_AVAIL_PERCENT_HIWATER ||
  394. num_outstanding_sends < 1))
  395. netif_wake_queue(ndev);
  396. } else {
  397. netdev_err(ndev, "Unknown send completion packet type- "
  398. "%d received!!\n", nvsp_packet->hdr.msg_type);
  399. }
  400. }
  401. int netvsc_send(struct hv_device *device,
  402. struct hv_netvsc_packet *packet)
  403. {
  404. struct netvsc_device *net_device;
  405. int ret = 0;
  406. struct nvsp_message sendMessage;
  407. struct net_device *ndev;
  408. net_device = get_outbound_net_device(device);
  409. if (!net_device)
  410. return -ENODEV;
  411. ndev = net_device->ndev;
  412. sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  413. if (packet->is_data_pkt) {
  414. /* 0 is RMC_DATA; */
  415. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
  416. } else {
  417. /* 1 is RMC_CONTROL; */
  418. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
  419. }
  420. /* Not using send buffer section */
  421. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
  422. 0xFFFFFFFF;
  423. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
  424. if (packet->page_buf_cnt) {
  425. ret = vmbus_sendpacket_pagebuffer(device->channel,
  426. packet->page_buf,
  427. packet->page_buf_cnt,
  428. &sendMessage,
  429. sizeof(struct nvsp_message),
  430. (unsigned long)packet);
  431. } else {
  432. ret = vmbus_sendpacket(device->channel, &sendMessage,
  433. sizeof(struct nvsp_message),
  434. (unsigned long)packet,
  435. VM_PKT_DATA_INBAND,
  436. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  437. }
  438. if (ret == 0) {
  439. atomic_inc(&net_device->num_outstanding_sends);
  440. if (hv_ringbuf_avail_percent(&device->channel->outbound) <
  441. RING_AVAIL_PERCENT_LOWATER) {
  442. netif_stop_queue(ndev);
  443. if (atomic_read(&net_device->
  444. num_outstanding_sends) < 1)
  445. netif_wake_queue(ndev);
  446. }
  447. } else if (ret == -EAGAIN) {
  448. netif_stop_queue(ndev);
  449. if (atomic_read(&net_device->num_outstanding_sends) < 1) {
  450. netif_wake_queue(ndev);
  451. ret = -ENOSPC;
  452. }
  453. } else {
  454. netdev_err(ndev, "Unable to send packet %p ret %d\n",
  455. packet, ret);
  456. }
  457. return ret;
  458. }
  459. static void netvsc_send_recv_completion(struct hv_device *device,
  460. u64 transaction_id, u32 status)
  461. {
  462. struct nvsp_message recvcompMessage;
  463. int retries = 0;
  464. int ret;
  465. struct net_device *ndev;
  466. struct netvsc_device *net_device = hv_get_drvdata(device);
  467. ndev = net_device->ndev;
  468. recvcompMessage.hdr.msg_type =
  469. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
  470. recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
  471. retry_send_cmplt:
  472. /* Send the completion */
  473. ret = vmbus_sendpacket(device->channel, &recvcompMessage,
  474. sizeof(struct nvsp_message), transaction_id,
  475. VM_PKT_COMP, 0);
  476. if (ret == 0) {
  477. /* success */
  478. /* no-op */
  479. } else if (ret == -EAGAIN) {
  480. /* no more room...wait a bit and attempt to retry 3 times */
  481. retries++;
  482. netdev_err(ndev, "unable to send receive completion pkt"
  483. " (tid %llx)...retrying %d\n", transaction_id, retries);
  484. if (retries < 4) {
  485. udelay(100);
  486. goto retry_send_cmplt;
  487. } else {
  488. netdev_err(ndev, "unable to send receive "
  489. "completion pkt (tid %llx)...give up retrying\n",
  490. transaction_id);
  491. }
  492. } else {
  493. netdev_err(ndev, "unable to send receive "
  494. "completion pkt - %llx\n", transaction_id);
  495. }
  496. }
  497. /* Send a receive completion packet to RNDIS device (ie NetVsp) */
  498. static void netvsc_receive_completion(void *context)
  499. {
  500. struct hv_netvsc_packet *packet = context;
  501. struct hv_device *device = packet->device;
  502. struct netvsc_device *net_device;
  503. u64 transaction_id = 0;
  504. bool fsend_receive_comp = false;
  505. unsigned long flags;
  506. struct net_device *ndev;
  507. u32 status = NVSP_STAT_NONE;
  508. /*
  509. * Even though it seems logical to do a GetOutboundNetDevice() here to
  510. * send out receive completion, we are using GetInboundNetDevice()
  511. * since we may have disable outbound traffic already.
  512. */
  513. net_device = get_inbound_net_device(device);
  514. if (!net_device)
  515. return;
  516. ndev = net_device->ndev;
  517. /* Overloading use of the lock. */
  518. spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
  519. if (packet->status != NVSP_STAT_SUCCESS)
  520. packet->xfer_page_pkt->status = NVSP_STAT_FAIL;
  521. packet->xfer_page_pkt->count--;
  522. /*
  523. * Last one in the line that represent 1 xfer page packet.
  524. * Return the xfer page packet itself to the freelist
  525. */
  526. if (packet->xfer_page_pkt->count == 0) {
  527. fsend_receive_comp = true;
  528. transaction_id = packet->completion.recv.recv_completion_tid;
  529. status = packet->xfer_page_pkt->status;
  530. list_add_tail(&packet->xfer_page_pkt->list_ent,
  531. &net_device->recv_pkt_list);
  532. }
  533. /* Put the packet back */
  534. list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
  535. spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
  536. /* Send a receive completion for the xfer page packet */
  537. if (fsend_receive_comp)
  538. netvsc_send_recv_completion(device, transaction_id, status);
  539. }
  540. static void netvsc_receive(struct hv_device *device,
  541. struct vmpacket_descriptor *packet)
  542. {
  543. struct netvsc_device *net_device;
  544. struct vmtransfer_page_packet_header *vmxferpage_packet;
  545. struct nvsp_message *nvsp_packet;
  546. struct hv_netvsc_packet *netvsc_packet = NULL;
  547. /* struct netvsc_driver *netvscDriver; */
  548. struct xferpage_packet *xferpage_packet = NULL;
  549. int i;
  550. int count = 0;
  551. unsigned long flags;
  552. struct net_device *ndev;
  553. LIST_HEAD(listHead);
  554. net_device = get_inbound_net_device(device);
  555. if (!net_device)
  556. return;
  557. ndev = net_device->ndev;
  558. /*
  559. * All inbound packets other than send completion should be xfer page
  560. * packet
  561. */
  562. if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
  563. netdev_err(ndev, "Unknown packet type received - %d\n",
  564. packet->type);
  565. return;
  566. }
  567. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  568. (packet->offset8 << 3));
  569. /* Make sure this is a valid nvsp packet */
  570. if (nvsp_packet->hdr.msg_type !=
  571. NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
  572. netdev_err(ndev, "Unknown nvsp packet type received-"
  573. " %d\n", nvsp_packet->hdr.msg_type);
  574. return;
  575. }
  576. vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
  577. if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
  578. netdev_err(ndev, "Invalid xfer page set id - "
  579. "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
  580. vmxferpage_packet->xfer_pageset_id);
  581. return;
  582. }
  583. /*
  584. * Grab free packets (range count + 1) to represent this xfer
  585. * page packet. +1 to represent the xfer page packet itself.
  586. * We grab it here so that we know exactly how many we can
  587. * fulfil
  588. */
  589. spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
  590. while (!list_empty(&net_device->recv_pkt_list)) {
  591. list_move_tail(net_device->recv_pkt_list.next, &listHead);
  592. if (++count == vmxferpage_packet->range_cnt + 1)
  593. break;
  594. }
  595. spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
  596. /*
  597. * We need at least 2 netvsc pkts (1 to represent the xfer
  598. * page and at least 1 for the range) i.e. we can handled
  599. * some of the xfer page packet ranges...
  600. */
  601. if (count < 2) {
  602. netdev_err(ndev, "Got only %d netvsc pkt...needed "
  603. "%d pkts. Dropping this xfer page packet completely!\n",
  604. count, vmxferpage_packet->range_cnt + 1);
  605. /* Return it to the freelist */
  606. spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
  607. for (i = count; i != 0; i--) {
  608. list_move_tail(listHead.next,
  609. &net_device->recv_pkt_list);
  610. }
  611. spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
  612. flags);
  613. netvsc_send_recv_completion(device,
  614. vmxferpage_packet->d.trans_id,
  615. NVSP_STAT_FAIL);
  616. return;
  617. }
  618. /* Remove the 1st packet to represent the xfer page packet itself */
  619. xferpage_packet = (struct xferpage_packet *)listHead.next;
  620. list_del(&xferpage_packet->list_ent);
  621. xferpage_packet->status = NVSP_STAT_SUCCESS;
  622. /* This is how much we can satisfy */
  623. xferpage_packet->count = count - 1;
  624. if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
  625. netdev_err(ndev, "Needed %d netvsc pkts to satisfy "
  626. "this xfer page...got %d\n",
  627. vmxferpage_packet->range_cnt, xferpage_packet->count);
  628. }
  629. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  630. for (i = 0; i < (count - 1); i++) {
  631. netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
  632. list_del(&netvsc_packet->list_ent);
  633. /* Initialize the netvsc packet */
  634. netvsc_packet->status = NVSP_STAT_SUCCESS;
  635. netvsc_packet->xfer_page_pkt = xferpage_packet;
  636. netvsc_packet->completion.recv.recv_completion =
  637. netvsc_receive_completion;
  638. netvsc_packet->completion.recv.recv_completion_ctx =
  639. netvsc_packet;
  640. netvsc_packet->device = device;
  641. /* Save this so that we can send it back */
  642. netvsc_packet->completion.recv.recv_completion_tid =
  643. vmxferpage_packet->d.trans_id;
  644. netvsc_packet->data = (void *)((unsigned long)net_device->
  645. recv_buf + vmxferpage_packet->ranges[i].byte_offset);
  646. netvsc_packet->total_data_buflen =
  647. vmxferpage_packet->ranges[i].byte_count;
  648. /* Pass it to the upper layer */
  649. rndis_filter_receive(device, netvsc_packet);
  650. netvsc_receive_completion(netvsc_packet->
  651. completion.recv.recv_completion_ctx);
  652. }
  653. }
  654. static void netvsc_channel_cb(void *context)
  655. {
  656. int ret;
  657. struct hv_device *device = context;
  658. struct netvsc_device *net_device;
  659. u32 bytes_recvd;
  660. u64 request_id;
  661. unsigned char *packet;
  662. struct vmpacket_descriptor *desc;
  663. unsigned char *buffer;
  664. int bufferlen = NETVSC_PACKET_SIZE;
  665. struct net_device *ndev;
  666. packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
  667. GFP_ATOMIC);
  668. if (!packet)
  669. return;
  670. buffer = packet;
  671. net_device = get_inbound_net_device(device);
  672. if (!net_device)
  673. goto out;
  674. ndev = net_device->ndev;
  675. do {
  676. ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
  677. &bytes_recvd, &request_id);
  678. if (ret == 0) {
  679. if (bytes_recvd > 0) {
  680. desc = (struct vmpacket_descriptor *)buffer;
  681. switch (desc->type) {
  682. case VM_PKT_COMP:
  683. netvsc_send_completion(device, desc);
  684. break;
  685. case VM_PKT_DATA_USING_XFER_PAGES:
  686. netvsc_receive(device, desc);
  687. break;
  688. default:
  689. netdev_err(ndev,
  690. "unhandled packet type %d, "
  691. "tid %llx len %d\n",
  692. desc->type, request_id,
  693. bytes_recvd);
  694. break;
  695. }
  696. /* reset */
  697. if (bufferlen > NETVSC_PACKET_SIZE) {
  698. kfree(buffer);
  699. buffer = packet;
  700. bufferlen = NETVSC_PACKET_SIZE;
  701. }
  702. } else {
  703. /* reset */
  704. if (bufferlen > NETVSC_PACKET_SIZE) {
  705. kfree(buffer);
  706. buffer = packet;
  707. bufferlen = NETVSC_PACKET_SIZE;
  708. }
  709. break;
  710. }
  711. } else if (ret == -ENOBUFS) {
  712. /* Handle large packet */
  713. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  714. if (buffer == NULL) {
  715. /* Try again next time around */
  716. netdev_err(ndev,
  717. "unable to allocate buffer of size "
  718. "(%d)!!\n", bytes_recvd);
  719. break;
  720. }
  721. bufferlen = bytes_recvd;
  722. }
  723. } while (1);
  724. out:
  725. kfree(buffer);
  726. return;
  727. }
  728. /*
  729. * netvsc_device_add - Callback when the device belonging to this
  730. * driver is added
  731. */
  732. int netvsc_device_add(struct hv_device *device, void *additional_info)
  733. {
  734. int ret = 0;
  735. int i;
  736. int ring_size =
  737. ((struct netvsc_device_info *)additional_info)->ring_size;
  738. struct netvsc_device *net_device;
  739. struct hv_netvsc_packet *packet, *pos;
  740. struct net_device *ndev;
  741. net_device = alloc_net_device(device);
  742. if (!net_device) {
  743. ret = -ENOMEM;
  744. goto cleanup;
  745. }
  746. /*
  747. * Coming into this function, struct net_device * is
  748. * registered as the driver private data.
  749. * In alloc_net_device(), we register struct netvsc_device *
  750. * as the driver private data and stash away struct net_device *
  751. * in struct netvsc_device *.
  752. */
  753. ndev = net_device->ndev;
  754. /* Initialize the NetVSC channel extension */
  755. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  756. spin_lock_init(&net_device->recv_pkt_list_lock);
  757. INIT_LIST_HEAD(&net_device->recv_pkt_list);
  758. for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
  759. packet = kzalloc(sizeof(struct hv_netvsc_packet), GFP_KERNEL);
  760. if (!packet)
  761. break;
  762. list_add_tail(&packet->list_ent,
  763. &net_device->recv_pkt_list);
  764. }
  765. init_completion(&net_device->channel_init_wait);
  766. /* Open the channel */
  767. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  768. ring_size * PAGE_SIZE, NULL, 0,
  769. netvsc_channel_cb, device);
  770. if (ret != 0) {
  771. netdev_err(ndev, "unable to open channel: %d\n", ret);
  772. goto cleanup;
  773. }
  774. /* Channel is opened */
  775. pr_info("hv_netvsc channel opened successfully\n");
  776. /* Connect with the NetVsp */
  777. ret = netvsc_connect_vsp(device);
  778. if (ret != 0) {
  779. netdev_err(ndev,
  780. "unable to connect to NetVSP - %d\n", ret);
  781. goto close;
  782. }
  783. return ret;
  784. close:
  785. /* Now, we can close the channel safely */
  786. vmbus_close(device->channel);
  787. cleanup:
  788. if (net_device) {
  789. list_for_each_entry_safe(packet, pos,
  790. &net_device->recv_pkt_list,
  791. list_ent) {
  792. list_del(&packet->list_ent);
  793. kfree(packet);
  794. }
  795. kfree(net_device);
  796. }
  797. return ret;
  798. }