netvsc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  317. net_device->destroy = true;
  318. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  319. /* Wait for all send completions */
  320. wait_event(net_device->wait_drain,
  321. atomic_read(&net_device->num_outstanding_sends) == 0);
  322. netvsc_disconnect_vsp(net_device);
  323. /*
  324. * Since we have already drained, we don't need to busy wait
  325. * as was done in final_release_stor_device()
  326. * Note that we cannot set the ext pointer to NULL until
  327. * we have drained - to drain the outgoing packets, we need to
  328. * allow incoming packets.
  329. */
  330. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  331. hv_set_drvdata(device, NULL);
  332. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  333. /*
  334. * At this point, no one should be accessing net_device
  335. * except in here
  336. */
  337. dev_notice(&device->device, "net device safe to remove\n");
  338. /* Now, we can close the channel safely */
  339. vmbus_close(device->channel);
  340. /* Release all resources */
  341. list_for_each_entry_safe(netvsc_packet, pos,
  342. &net_device->recv_pkt_list, list_ent) {
  343. list_del(&netvsc_packet->list_ent);
  344. kfree(netvsc_packet);
  345. }
  346. kfree(net_device);
  347. return 0;
  348. }
  349. #define RING_AVAIL_PERCENT_HIWATER 20
  350. #define RING_AVAIL_PERCENT_LOWATER 10
  351. /*
  352. * Get the percentage of available bytes to write in the ring.
  353. * The return value is in range from 0 to 100.
  354. */
  355. static inline u32 hv_ringbuf_avail_percent(
  356. struct hv_ring_buffer_info *ring_info)
  357. {
  358. u32 avail_read, avail_write;
  359. hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
  360. return avail_write * 100 / ring_info->ring_datasize;
  361. }
  362. static void netvsc_send_completion(struct hv_device *device,
  363. struct vmpacket_descriptor *packet)
  364. {
  365. struct netvsc_device *net_device;
  366. struct nvsp_message *nvsp_packet;
  367. struct hv_netvsc_packet *nvsc_packet;
  368. struct net_device *ndev;
  369. net_device = get_inbound_net_device(device);
  370. if (!net_device)
  371. return;
  372. ndev = net_device->ndev;
  373. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  374. (packet->offset8 << 3));
  375. if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
  376. (nvsp_packet->hdr.msg_type ==
  377. NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
  378. (nvsp_packet->hdr.msg_type ==
  379. NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
  380. /* Copy the response back */
  381. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  382. sizeof(struct nvsp_message));
  383. complete(&net_device->channel_init_wait);
  384. } else if (nvsp_packet->hdr.msg_type ==
  385. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
  386. int num_outstanding_sends;
  387. /* Get the send context */
  388. nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
  389. packet->trans_id;
  390. /* Notify the layer above us */
  391. nvsc_packet->completion.send.send_completion(
  392. nvsc_packet->completion.send.send_completion_ctx);
  393. num_outstanding_sends =
  394. atomic_dec_return(&net_device->num_outstanding_sends);
  395. if (net_device->destroy && num_outstanding_sends == 0)
  396. wake_up(&net_device->wait_drain);
  397. if (netif_queue_stopped(ndev) && !net_device->start_remove &&
  398. (hv_ringbuf_avail_percent(&device->channel->outbound)
  399. > RING_AVAIL_PERCENT_HIWATER ||
  400. num_outstanding_sends < 1))
  401. netif_wake_queue(ndev);
  402. } else {
  403. netdev_err(ndev, "Unknown send completion packet type- "
  404. "%d received!!\n", nvsp_packet->hdr.msg_type);
  405. }
  406. }
  407. int netvsc_send(struct hv_device *device,
  408. struct hv_netvsc_packet *packet)
  409. {
  410. struct netvsc_device *net_device;
  411. int ret = 0;
  412. struct nvsp_message sendMessage;
  413. struct net_device *ndev;
  414. net_device = get_outbound_net_device(device);
  415. if (!net_device)
  416. return -ENODEV;
  417. ndev = net_device->ndev;
  418. sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  419. if (packet->is_data_pkt) {
  420. /* 0 is RMC_DATA; */
  421. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
  422. } else {
  423. /* 1 is RMC_CONTROL; */
  424. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
  425. }
  426. /* Not using send buffer section */
  427. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
  428. 0xFFFFFFFF;
  429. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
  430. if (packet->page_buf_cnt) {
  431. ret = vmbus_sendpacket_pagebuffer(device->channel,
  432. packet->page_buf,
  433. packet->page_buf_cnt,
  434. &sendMessage,
  435. sizeof(struct nvsp_message),
  436. (unsigned long)packet);
  437. } else {
  438. ret = vmbus_sendpacket(device->channel, &sendMessage,
  439. sizeof(struct nvsp_message),
  440. (unsigned long)packet,
  441. VM_PKT_DATA_INBAND,
  442. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  443. }
  444. if (ret == 0) {
  445. atomic_inc(&net_device->num_outstanding_sends);
  446. if (hv_ringbuf_avail_percent(&device->channel->outbound) <
  447. RING_AVAIL_PERCENT_LOWATER) {
  448. netif_stop_queue(ndev);
  449. if (atomic_read(&net_device->
  450. num_outstanding_sends) < 1)
  451. netif_wake_queue(ndev);
  452. }
  453. } else if (ret == -EAGAIN) {
  454. netif_stop_queue(ndev);
  455. if (atomic_read(&net_device->num_outstanding_sends) < 1) {
  456. netif_wake_queue(ndev);
  457. ret = -ENOSPC;
  458. }
  459. } else {
  460. netdev_err(ndev, "Unable to send packet %p ret %d\n",
  461. packet, ret);
  462. }
  463. return ret;
  464. }
  465. static void netvsc_send_recv_completion(struct hv_device *device,
  466. u64 transaction_id)
  467. {
  468. struct nvsp_message recvcompMessage;
  469. int retries = 0;
  470. int ret;
  471. struct net_device *ndev;
  472. struct netvsc_device *net_device = hv_get_drvdata(device);
  473. ndev = net_device->ndev;
  474. recvcompMessage.hdr.msg_type =
  475. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
  476. /* FIXME: Pass in the status */
  477. recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
  478. NVSP_STAT_SUCCESS;
  479. retry_send_cmplt:
  480. /* Send the completion */
  481. ret = vmbus_sendpacket(device->channel, &recvcompMessage,
  482. sizeof(struct nvsp_message), transaction_id,
  483. VM_PKT_COMP, 0);
  484. if (ret == 0) {
  485. /* success */
  486. /* no-op */
  487. } else if (ret == -EAGAIN) {
  488. /* no more room...wait a bit and attempt to retry 3 times */
  489. retries++;
  490. netdev_err(ndev, "unable to send receive completion pkt"
  491. " (tid %llx)...retrying %d\n", transaction_id, retries);
  492. if (retries < 4) {
  493. udelay(100);
  494. goto retry_send_cmplt;
  495. } else {
  496. netdev_err(ndev, "unable to send receive "
  497. "completion pkt (tid %llx)...give up retrying\n",
  498. transaction_id);
  499. }
  500. } else {
  501. netdev_err(ndev, "unable to send receive "
  502. "completion pkt - %llx\n", transaction_id);
  503. }
  504. }
  505. /* Send a receive completion packet to RNDIS device (ie NetVsp) */
  506. static void netvsc_receive_completion(void *context)
  507. {
  508. struct hv_netvsc_packet *packet = context;
  509. struct hv_device *device = packet->device;
  510. struct netvsc_device *net_device;
  511. u64 transaction_id = 0;
  512. bool fsend_receive_comp = false;
  513. unsigned long flags;
  514. struct net_device *ndev;
  515. /*
  516. * Even though it seems logical to do a GetOutboundNetDevice() here to
  517. * send out receive completion, we are using GetInboundNetDevice()
  518. * since we may have disable outbound traffic already.
  519. */
  520. net_device = get_inbound_net_device(device);
  521. if (!net_device)
  522. return;
  523. ndev = net_device->ndev;
  524. /* Overloading use of the lock. */
  525. spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
  526. packet->xfer_page_pkt->count--;
  527. /*
  528. * Last one in the line that represent 1 xfer page packet.
  529. * Return the xfer page packet itself to the freelist
  530. */
  531. if (packet->xfer_page_pkt->count == 0) {
  532. fsend_receive_comp = true;
  533. transaction_id = packet->completion.recv.recv_completion_tid;
  534. list_add_tail(&packet->xfer_page_pkt->list_ent,
  535. &net_device->recv_pkt_list);
  536. }
  537. /* Put the packet back */
  538. list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
  539. spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
  540. /* Send a receive completion for the xfer page packet */
  541. if (fsend_receive_comp)
  542. netvsc_send_recv_completion(device, transaction_id);
  543. }
  544. static void netvsc_receive(struct hv_device *device,
  545. struct vmpacket_descriptor *packet)
  546. {
  547. struct netvsc_device *net_device;
  548. struct vmtransfer_page_packet_header *vmxferpage_packet;
  549. struct nvsp_message *nvsp_packet;
  550. struct hv_netvsc_packet *netvsc_packet = NULL;
  551. /* struct netvsc_driver *netvscDriver; */
  552. struct xferpage_packet *xferpage_packet = NULL;
  553. int i;
  554. int count = 0;
  555. unsigned long flags;
  556. struct net_device *ndev;
  557. LIST_HEAD(listHead);
  558. net_device = get_inbound_net_device(device);
  559. if (!net_device)
  560. return;
  561. ndev = net_device->ndev;
  562. /*
  563. * All inbound packets other than send completion should be xfer page
  564. * packet
  565. */
  566. if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
  567. netdev_err(ndev, "Unknown packet type received - %d\n",
  568. packet->type);
  569. return;
  570. }
  571. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  572. (packet->offset8 << 3));
  573. /* Make sure this is a valid nvsp packet */
  574. if (nvsp_packet->hdr.msg_type !=
  575. NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
  576. netdev_err(ndev, "Unknown nvsp packet type received-"
  577. " %d\n", nvsp_packet->hdr.msg_type);
  578. return;
  579. }
  580. vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
  581. if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
  582. netdev_err(ndev, "Invalid xfer page set id - "
  583. "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
  584. vmxferpage_packet->xfer_pageset_id);
  585. return;
  586. }
  587. /*
  588. * Grab free packets (range count + 1) to represent this xfer
  589. * page packet. +1 to represent the xfer page packet itself.
  590. * We grab it here so that we know exactly how many we can
  591. * fulfil
  592. */
  593. spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
  594. while (!list_empty(&net_device->recv_pkt_list)) {
  595. list_move_tail(net_device->recv_pkt_list.next, &listHead);
  596. if (++count == vmxferpage_packet->range_cnt + 1)
  597. break;
  598. }
  599. spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
  600. /*
  601. * We need at least 2 netvsc pkts (1 to represent the xfer
  602. * page and at least 1 for the range) i.e. we can handled
  603. * some of the xfer page packet ranges...
  604. */
  605. if (count < 2) {
  606. netdev_err(ndev, "Got only %d netvsc pkt...needed "
  607. "%d pkts. Dropping this xfer page packet completely!\n",
  608. count, vmxferpage_packet->range_cnt + 1);
  609. /* Return it to the freelist */
  610. spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
  611. for (i = count; i != 0; i--) {
  612. list_move_tail(listHead.next,
  613. &net_device->recv_pkt_list);
  614. }
  615. spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
  616. flags);
  617. netvsc_send_recv_completion(device,
  618. vmxferpage_packet->d.trans_id);
  619. return;
  620. }
  621. /* Remove the 1st packet to represent the xfer page packet itself */
  622. xferpage_packet = (struct xferpage_packet *)listHead.next;
  623. list_del(&xferpage_packet->list_ent);
  624. /* This is how much we can satisfy */
  625. xferpage_packet->count = count - 1;
  626. if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
  627. netdev_err(ndev, "Needed %d netvsc pkts to satisfy "
  628. "this xfer page...got %d\n",
  629. vmxferpage_packet->range_cnt, xferpage_packet->count);
  630. }
  631. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  632. for (i = 0; i < (count - 1); i++) {
  633. netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
  634. list_del(&netvsc_packet->list_ent);
  635. /* Initialize the netvsc packet */
  636. netvsc_packet->xfer_page_pkt = xferpage_packet;
  637. netvsc_packet->completion.recv.recv_completion =
  638. netvsc_receive_completion;
  639. netvsc_packet->completion.recv.recv_completion_ctx =
  640. netvsc_packet;
  641. netvsc_packet->device = device;
  642. /* Save this so that we can send it back */
  643. netvsc_packet->completion.recv.recv_completion_tid =
  644. vmxferpage_packet->d.trans_id;
  645. netvsc_packet->data = (void *)((unsigned long)net_device->
  646. recv_buf + vmxferpage_packet->ranges[i].byte_offset);
  647. netvsc_packet->total_data_buflen =
  648. vmxferpage_packet->ranges[i].byte_count;
  649. /* Pass it to the upper layer */
  650. rndis_filter_receive(device, netvsc_packet);
  651. netvsc_receive_completion(netvsc_packet->
  652. completion.recv.recv_completion_ctx);
  653. }
  654. }
  655. static void netvsc_channel_cb(void *context)
  656. {
  657. int ret;
  658. struct hv_device *device = context;
  659. struct netvsc_device *net_device;
  660. u32 bytes_recvd;
  661. u64 request_id;
  662. unsigned char *packet;
  663. struct vmpacket_descriptor *desc;
  664. unsigned char *buffer;
  665. int bufferlen = NETVSC_PACKET_SIZE;
  666. struct net_device *ndev;
  667. packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
  668. GFP_ATOMIC);
  669. if (!packet)
  670. return;
  671. buffer = packet;
  672. net_device = get_inbound_net_device(device);
  673. if (!net_device)
  674. goto out;
  675. ndev = net_device->ndev;
  676. do {
  677. ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
  678. &bytes_recvd, &request_id);
  679. if (ret == 0) {
  680. if (bytes_recvd > 0) {
  681. desc = (struct vmpacket_descriptor *)buffer;
  682. switch (desc->type) {
  683. case VM_PKT_COMP:
  684. netvsc_send_completion(device, desc);
  685. break;
  686. case VM_PKT_DATA_USING_XFER_PAGES:
  687. netvsc_receive(device, desc);
  688. break;
  689. default:
  690. netdev_err(ndev,
  691. "unhandled packet type %d, "
  692. "tid %llx len %d\n",
  693. desc->type, request_id,
  694. bytes_recvd);
  695. break;
  696. }
  697. /* reset */
  698. if (bufferlen > NETVSC_PACKET_SIZE) {
  699. kfree(buffer);
  700. buffer = packet;
  701. bufferlen = NETVSC_PACKET_SIZE;
  702. }
  703. } else {
  704. /* reset */
  705. if (bufferlen > NETVSC_PACKET_SIZE) {
  706. kfree(buffer);
  707. buffer = packet;
  708. bufferlen = NETVSC_PACKET_SIZE;
  709. }
  710. break;
  711. }
  712. } else if (ret == -ENOBUFS) {
  713. /* Handle large packet */
  714. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  715. if (buffer == NULL) {
  716. /* Try again next time around */
  717. netdev_err(ndev,
  718. "unable to allocate buffer of size "
  719. "(%d)!!\n", bytes_recvd);
  720. break;
  721. }
  722. bufferlen = bytes_recvd;
  723. }
  724. } while (1);
  725. out:
  726. kfree(buffer);
  727. return;
  728. }
  729. /*
  730. * netvsc_device_add - Callback when the device belonging to this
  731. * driver is added
  732. */
  733. int netvsc_device_add(struct hv_device *device, void *additional_info)
  734. {
  735. int ret = 0;
  736. int i;
  737. int ring_size =
  738. ((struct netvsc_device_info *)additional_info)->ring_size;
  739. struct netvsc_device *net_device;
  740. struct hv_netvsc_packet *packet, *pos;
  741. struct net_device *ndev;
  742. net_device = alloc_net_device(device);
  743. if (!net_device) {
  744. ret = -ENOMEM;
  745. goto cleanup;
  746. }
  747. /*
  748. * Coming into this function, struct net_device * is
  749. * registered as the driver private data.
  750. * In alloc_net_device(), we register struct netvsc_device *
  751. * as the driver private data and stash away struct net_device *
  752. * in struct netvsc_device *.
  753. */
  754. ndev = net_device->ndev;
  755. /* Initialize the NetVSC channel extension */
  756. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  757. spin_lock_init(&net_device->recv_pkt_list_lock);
  758. INIT_LIST_HEAD(&net_device->recv_pkt_list);
  759. for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
  760. packet = kzalloc(sizeof(struct hv_netvsc_packet) +
  761. (NETVSC_RECEIVE_SG_COUNT *
  762. sizeof(struct hv_page_buffer)), GFP_KERNEL);
  763. if (!packet)
  764. break;
  765. list_add_tail(&packet->list_ent,
  766. &net_device->recv_pkt_list);
  767. }
  768. init_completion(&net_device->channel_init_wait);
  769. /* Open the channel */
  770. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  771. ring_size * PAGE_SIZE, NULL, 0,
  772. netvsc_channel_cb, device);
  773. if (ret != 0) {
  774. netdev_err(ndev, "unable to open channel: %d\n", ret);
  775. goto cleanup;
  776. }
  777. /* Channel is opened */
  778. pr_info("hv_netvsc channel opened successfully\n");
  779. /* Connect with the NetVsp */
  780. ret = netvsc_connect_vsp(device);
  781. if (ret != 0) {
  782. netdev_err(ndev,
  783. "unable to connect to NetVSP - %d\n", ret);
  784. goto close;
  785. }
  786. return ret;
  787. close:
  788. /* Now, we can close the channel safely */
  789. vmbus_close(device->channel);
  790. cleanup:
  791. if (net_device) {
  792. list_for_each_entry_safe(packet, pos,
  793. &net_device->recv_pkt_list,
  794. list_ent) {
  795. list_del(&packet->list_ent);
  796. kfree(packet);
  797. }
  798. kfree(net_device);
  799. }
  800. return ret;
  801. }