netvsc.c 25 KB

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