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