netvsc.c 24 KB

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