netvsc.c 25 KB

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