netvsc.c 25 KB

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