netvsc.c 25 KB

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