netvsc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 <asm/sync_bitops.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->cb_buffer = kzalloc(NETVSC_PACKET_SIZE, GFP_KERNEL);
  40. if (!net_device->cb_buffer) {
  41. kfree(net_device);
  42. return NULL;
  43. }
  44. init_waitqueue_head(&net_device->wait_drain);
  45. net_device->start_remove = false;
  46. net_device->destroy = false;
  47. net_device->dev = device;
  48. net_device->ndev = ndev;
  49. hv_set_drvdata(device, net_device);
  50. return net_device;
  51. }
  52. static void free_netvsc_device(struct netvsc_device *nvdev)
  53. {
  54. kfree(nvdev->cb_buffer);
  55. kfree(nvdev);
  56. }
  57. static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
  58. {
  59. struct netvsc_device *net_device;
  60. net_device = hv_get_drvdata(device);
  61. if (net_device && net_device->destroy)
  62. net_device = NULL;
  63. return net_device;
  64. }
  65. static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
  66. {
  67. struct netvsc_device *net_device;
  68. net_device = hv_get_drvdata(device);
  69. if (!net_device)
  70. goto get_in_err;
  71. if (net_device->destroy &&
  72. atomic_read(&net_device->num_outstanding_sends) == 0)
  73. net_device = NULL;
  74. get_in_err:
  75. return net_device;
  76. }
  77. static int netvsc_destroy_buf(struct netvsc_device *net_device)
  78. {
  79. struct nvsp_message *revoke_packet;
  80. int ret = 0;
  81. struct net_device *ndev = net_device->ndev;
  82. /*
  83. * If we got a section count, it means we received a
  84. * SendReceiveBufferComplete msg (ie sent
  85. * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
  86. * to send a revoke msg here
  87. */
  88. if (net_device->recv_section_cnt) {
  89. /* Send the revoke receive buffer */
  90. revoke_packet = &net_device->revoke_packet;
  91. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  92. revoke_packet->hdr.msg_type =
  93. NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
  94. revoke_packet->msg.v1_msg.
  95. revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  96. ret = vmbus_sendpacket(net_device->dev->channel,
  97. revoke_packet,
  98. sizeof(struct nvsp_message),
  99. (unsigned long)revoke_packet,
  100. VM_PKT_DATA_INBAND, 0);
  101. /*
  102. * If we failed here, we might as well return and
  103. * have a leak rather than continue and a bugchk
  104. */
  105. if (ret != 0) {
  106. netdev_err(ndev, "unable to send "
  107. "revoke receive buffer to netvsp\n");
  108. return ret;
  109. }
  110. }
  111. /* Teardown the gpadl on the vsp end */
  112. if (net_device->recv_buf_gpadl_handle) {
  113. ret = vmbus_teardown_gpadl(net_device->dev->channel,
  114. net_device->recv_buf_gpadl_handle);
  115. /* If we failed here, we might as well return and have a leak
  116. * rather than continue and a bugchk
  117. */
  118. if (ret != 0) {
  119. netdev_err(ndev,
  120. "unable to teardown receive buffer's gpadl\n");
  121. return ret;
  122. }
  123. net_device->recv_buf_gpadl_handle = 0;
  124. }
  125. if (net_device->recv_buf) {
  126. /* Free up the receive buffer */
  127. vfree(net_device->recv_buf);
  128. net_device->recv_buf = NULL;
  129. }
  130. if (net_device->recv_section) {
  131. net_device->recv_section_cnt = 0;
  132. kfree(net_device->recv_section);
  133. net_device->recv_section = NULL;
  134. }
  135. /* Deal with the send buffer we may have setup.
  136. * If we got a send section size, it means we received a
  137. * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
  138. * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
  139. * to send a revoke msg here
  140. */
  141. if (net_device->send_section_size) {
  142. /* Send the revoke receive buffer */
  143. revoke_packet = &net_device->revoke_packet;
  144. memset(revoke_packet, 0, sizeof(struct nvsp_message));
  145. revoke_packet->hdr.msg_type =
  146. NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
  147. revoke_packet->msg.v1_msg.revoke_send_buf.id =
  148. NETVSC_SEND_BUFFER_ID;
  149. ret = vmbus_sendpacket(net_device->dev->channel,
  150. revoke_packet,
  151. sizeof(struct nvsp_message),
  152. (unsigned long)revoke_packet,
  153. VM_PKT_DATA_INBAND, 0);
  154. /* If we failed here, we might as well return and
  155. * have a leak rather than continue and a bugchk
  156. */
  157. if (ret != 0) {
  158. netdev_err(ndev, "unable to send "
  159. "revoke send buffer to netvsp\n");
  160. return ret;
  161. }
  162. }
  163. /* Teardown the gpadl on the vsp end */
  164. if (net_device->send_buf_gpadl_handle) {
  165. ret = vmbus_teardown_gpadl(net_device->dev->channel,
  166. net_device->send_buf_gpadl_handle);
  167. /* If we failed here, we might as well return and have a leak
  168. * rather than continue and a bugchk
  169. */
  170. if (ret != 0) {
  171. netdev_err(ndev,
  172. "unable to teardown send buffer's gpadl\n");
  173. return ret;
  174. }
  175. net_device->send_buf_gpadl_handle = 0;
  176. }
  177. if (net_device->send_buf) {
  178. /* Free up the send buffer */
  179. vfree(net_device->send_buf);
  180. net_device->send_buf = NULL;
  181. }
  182. kfree(net_device->send_section_map);
  183. return ret;
  184. }
  185. static int netvsc_init_buf(struct hv_device *device)
  186. {
  187. int ret = 0;
  188. unsigned long t;
  189. struct netvsc_device *net_device;
  190. struct nvsp_message *init_packet;
  191. struct net_device *ndev;
  192. net_device = get_outbound_net_device(device);
  193. if (!net_device)
  194. return -ENODEV;
  195. ndev = net_device->ndev;
  196. net_device->recv_buf = vzalloc(net_device->recv_buf_size);
  197. if (!net_device->recv_buf) {
  198. netdev_err(ndev, "unable to allocate receive "
  199. "buffer of size %d\n", net_device->recv_buf_size);
  200. ret = -ENOMEM;
  201. goto cleanup;
  202. }
  203. /*
  204. * Establish the gpadl handle for this buffer on this
  205. * channel. Note: This call uses the vmbus connection rather
  206. * than the channel to establish the gpadl handle.
  207. */
  208. ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
  209. net_device->recv_buf_size,
  210. &net_device->recv_buf_gpadl_handle);
  211. if (ret != 0) {
  212. netdev_err(ndev,
  213. "unable to establish receive buffer's gpadl\n");
  214. goto cleanup;
  215. }
  216. /* Notify the NetVsp of the gpadl handle */
  217. init_packet = &net_device->channel_init_pkt;
  218. memset(init_packet, 0, sizeof(struct nvsp_message));
  219. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
  220. init_packet->msg.v1_msg.send_recv_buf.
  221. gpadl_handle = net_device->recv_buf_gpadl_handle;
  222. init_packet->msg.v1_msg.
  223. send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
  224. /* Send the gpadl notification request */
  225. ret = vmbus_sendpacket(device->channel, init_packet,
  226. sizeof(struct nvsp_message),
  227. (unsigned long)init_packet,
  228. VM_PKT_DATA_INBAND,
  229. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  230. if (ret != 0) {
  231. netdev_err(ndev,
  232. "unable to send receive buffer's gpadl to netvsp\n");
  233. goto cleanup;
  234. }
  235. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  236. BUG_ON(t == 0);
  237. /* Check the response */
  238. if (init_packet->msg.v1_msg.
  239. send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
  240. netdev_err(ndev, "Unable to complete receive buffer "
  241. "initialization with NetVsp - status %d\n",
  242. init_packet->msg.v1_msg.
  243. send_recv_buf_complete.status);
  244. ret = -EINVAL;
  245. goto cleanup;
  246. }
  247. /* Parse the response */
  248. net_device->recv_section_cnt = init_packet->msg.
  249. v1_msg.send_recv_buf_complete.num_sections;
  250. net_device->recv_section = kmemdup(
  251. init_packet->msg.v1_msg.send_recv_buf_complete.sections,
  252. net_device->recv_section_cnt *
  253. sizeof(struct nvsp_1_receive_buffer_section),
  254. GFP_KERNEL);
  255. if (net_device->recv_section == NULL) {
  256. ret = -EINVAL;
  257. goto cleanup;
  258. }
  259. /*
  260. * For 1st release, there should only be 1 section that represents the
  261. * entire receive buffer
  262. */
  263. if (net_device->recv_section_cnt != 1 ||
  264. net_device->recv_section->offset != 0) {
  265. ret = -EINVAL;
  266. goto cleanup;
  267. }
  268. /* Now setup the send buffer.
  269. */
  270. net_device->send_buf = vzalloc(net_device->send_buf_size);
  271. if (!net_device->send_buf) {
  272. netdev_err(ndev, "unable to allocate send "
  273. "buffer of size %d\n", net_device->send_buf_size);
  274. ret = -ENOMEM;
  275. goto cleanup;
  276. }
  277. /* Establish the gpadl handle for this buffer on this
  278. * channel. Note: This call uses the vmbus connection rather
  279. * than the channel to establish the gpadl handle.
  280. */
  281. ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
  282. net_device->send_buf_size,
  283. &net_device->send_buf_gpadl_handle);
  284. if (ret != 0) {
  285. netdev_err(ndev,
  286. "unable to establish send buffer's gpadl\n");
  287. goto cleanup;
  288. }
  289. /* Notify the NetVsp of the gpadl handle */
  290. init_packet = &net_device->channel_init_pkt;
  291. memset(init_packet, 0, sizeof(struct nvsp_message));
  292. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
  293. init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
  294. net_device->send_buf_gpadl_handle;
  295. init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
  296. /* Send the gpadl notification request */
  297. ret = vmbus_sendpacket(device->channel, init_packet,
  298. sizeof(struct nvsp_message),
  299. (unsigned long)init_packet,
  300. VM_PKT_DATA_INBAND,
  301. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  302. if (ret != 0) {
  303. netdev_err(ndev,
  304. "unable to send send buffer's gpadl to netvsp\n");
  305. goto cleanup;
  306. }
  307. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  308. BUG_ON(t == 0);
  309. /* Check the response */
  310. if (init_packet->msg.v1_msg.
  311. send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
  312. netdev_err(ndev, "Unable to complete send buffer "
  313. "initialization with NetVsp - status %d\n",
  314. init_packet->msg.v1_msg.
  315. send_send_buf_complete.status);
  316. ret = -EINVAL;
  317. goto cleanup;
  318. }
  319. /* Parse the response */
  320. net_device->send_section_size = init_packet->msg.
  321. v1_msg.send_send_buf_complete.section_size;
  322. /* Section count is simply the size divided by the section size.
  323. */
  324. net_device->send_section_cnt =
  325. net_device->send_buf_size/net_device->send_section_size;
  326. dev_info(&device->device, "Send section size: %d, Section count:%d\n",
  327. net_device->send_section_size, net_device->send_section_cnt);
  328. /* Setup state for managing the send buffer. */
  329. net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
  330. BITS_PER_LONG);
  331. net_device->send_section_map =
  332. kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL);
  333. if (net_device->send_section_map == NULL) {
  334. ret = -ENOMEM;
  335. goto cleanup;
  336. }
  337. goto exit;
  338. cleanup:
  339. netvsc_destroy_buf(net_device);
  340. exit:
  341. return ret;
  342. }
  343. /* Negotiate NVSP protocol version */
  344. static int negotiate_nvsp_ver(struct hv_device *device,
  345. struct netvsc_device *net_device,
  346. struct nvsp_message *init_packet,
  347. u32 nvsp_ver)
  348. {
  349. int ret;
  350. unsigned long t;
  351. memset(init_packet, 0, sizeof(struct nvsp_message));
  352. init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
  353. init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
  354. init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
  355. /* Send the init request */
  356. ret = vmbus_sendpacket(device->channel, init_packet,
  357. sizeof(struct nvsp_message),
  358. (unsigned long)init_packet,
  359. VM_PKT_DATA_INBAND,
  360. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  361. if (ret != 0)
  362. return ret;
  363. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  364. if (t == 0)
  365. return -ETIMEDOUT;
  366. if (init_packet->msg.init_msg.init_complete.status !=
  367. NVSP_STAT_SUCCESS)
  368. return -EINVAL;
  369. if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
  370. return 0;
  371. /* NVSPv2 only: Send NDIS config */
  372. memset(init_packet, 0, sizeof(struct nvsp_message));
  373. init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
  374. init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu +
  375. ETH_HLEN;
  376. init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
  377. ret = vmbus_sendpacket(device->channel, init_packet,
  378. sizeof(struct nvsp_message),
  379. (unsigned long)init_packet,
  380. VM_PKT_DATA_INBAND, 0);
  381. return ret;
  382. }
  383. static int netvsc_connect_vsp(struct hv_device *device)
  384. {
  385. int ret;
  386. struct netvsc_device *net_device;
  387. struct nvsp_message *init_packet;
  388. int ndis_version;
  389. struct net_device *ndev;
  390. u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
  391. NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
  392. int i, num_ver = 4; /* number of different NVSP versions */
  393. net_device = get_outbound_net_device(device);
  394. if (!net_device)
  395. return -ENODEV;
  396. ndev = net_device->ndev;
  397. init_packet = &net_device->channel_init_pkt;
  398. /* Negotiate the latest NVSP protocol supported */
  399. for (i = num_ver - 1; i >= 0; i--)
  400. if (negotiate_nvsp_ver(device, net_device, init_packet,
  401. ver_list[i]) == 0) {
  402. net_device->nvsp_version = ver_list[i];
  403. break;
  404. }
  405. if (i < 0) {
  406. ret = -EPROTO;
  407. goto cleanup;
  408. }
  409. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  410. /* Send the ndis version */
  411. memset(init_packet, 0, sizeof(struct nvsp_message));
  412. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
  413. ndis_version = 0x00060001;
  414. else
  415. ndis_version = 0x0006001e;
  416. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  417. init_packet->msg.v1_msg.
  418. send_ndis_ver.ndis_major_ver =
  419. (ndis_version & 0xFFFF0000) >> 16;
  420. init_packet->msg.v1_msg.
  421. send_ndis_ver.ndis_minor_ver =
  422. ndis_version & 0xFFFF;
  423. /* Send the init request */
  424. ret = vmbus_sendpacket(device->channel, init_packet,
  425. sizeof(struct nvsp_message),
  426. (unsigned long)init_packet,
  427. VM_PKT_DATA_INBAND, 0);
  428. if (ret != 0)
  429. goto cleanup;
  430. /* Post the big receive buffer to NetVSP */
  431. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  432. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  433. else
  434. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  435. net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
  436. ret = netvsc_init_buf(device);
  437. cleanup:
  438. return ret;
  439. }
  440. static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
  441. {
  442. netvsc_destroy_buf(net_device);
  443. }
  444. /*
  445. * netvsc_device_remove - Callback when the root bus device is removed
  446. */
  447. int netvsc_device_remove(struct hv_device *device)
  448. {
  449. struct netvsc_device *net_device;
  450. unsigned long flags;
  451. net_device = hv_get_drvdata(device);
  452. netvsc_disconnect_vsp(net_device);
  453. /*
  454. * Since we have already drained, we don't need to busy wait
  455. * as was done in final_release_stor_device()
  456. * Note that we cannot set the ext pointer to NULL until
  457. * we have drained - to drain the outgoing packets, we need to
  458. * allow incoming packets.
  459. */
  460. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  461. hv_set_drvdata(device, NULL);
  462. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  463. /*
  464. * At this point, no one should be accessing net_device
  465. * except in here
  466. */
  467. dev_notice(&device->device, "net device safe to remove\n");
  468. /* Now, we can close the channel safely */
  469. vmbus_close(device->channel);
  470. /* Release all resources */
  471. vfree(net_device->sub_cb_buf);
  472. free_netvsc_device(net_device);
  473. return 0;
  474. }
  475. #define RING_AVAIL_PERCENT_HIWATER 20
  476. #define RING_AVAIL_PERCENT_LOWATER 10
  477. /*
  478. * Get the percentage of available bytes to write in the ring.
  479. * The return value is in range from 0 to 100.
  480. */
  481. static inline u32 hv_ringbuf_avail_percent(
  482. struct hv_ring_buffer_info *ring_info)
  483. {
  484. u32 avail_read, avail_write;
  485. hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
  486. return avail_write * 100 / ring_info->ring_datasize;
  487. }
  488. static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
  489. u32 index)
  490. {
  491. sync_change_bit(index, net_device->send_section_map);
  492. }
  493. static void netvsc_send_completion(struct netvsc_device *net_device,
  494. struct hv_device *device,
  495. struct vmpacket_descriptor *packet)
  496. {
  497. struct nvsp_message *nvsp_packet;
  498. struct hv_netvsc_packet *nvsc_packet;
  499. struct net_device *ndev;
  500. u32 send_index;
  501. ndev = net_device->ndev;
  502. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  503. (packet->offset8 << 3));
  504. if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
  505. (nvsp_packet->hdr.msg_type ==
  506. NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
  507. (nvsp_packet->hdr.msg_type ==
  508. NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
  509. (nvsp_packet->hdr.msg_type ==
  510. NVSP_MSG5_TYPE_SUBCHANNEL)) {
  511. /* Copy the response back */
  512. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  513. sizeof(struct nvsp_message));
  514. complete(&net_device->channel_init_wait);
  515. } else if (nvsp_packet->hdr.msg_type ==
  516. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
  517. int num_outstanding_sends;
  518. u16 q_idx = 0;
  519. struct vmbus_channel *channel = device->channel;
  520. int queue_sends;
  521. /* Get the send context */
  522. nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
  523. packet->trans_id;
  524. /* Notify the layer above us */
  525. if (nvsc_packet) {
  526. send_index = nvsc_packet->send_buf_index;
  527. if (send_index != NETVSC_INVALID_INDEX)
  528. netvsc_free_send_slot(net_device, send_index);
  529. q_idx = nvsc_packet->q_idx;
  530. channel = nvsc_packet->channel;
  531. nvsc_packet->send_completion(nvsc_packet->
  532. send_completion_ctx);
  533. }
  534. num_outstanding_sends =
  535. atomic_dec_return(&net_device->num_outstanding_sends);
  536. queue_sends = atomic_dec_return(&net_device->
  537. queue_sends[q_idx]);
  538. if (net_device->destroy && num_outstanding_sends == 0)
  539. wake_up(&net_device->wait_drain);
  540. if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
  541. !net_device->start_remove &&
  542. (hv_ringbuf_avail_percent(&channel->outbound) >
  543. RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
  544. netif_tx_wake_queue(netdev_get_tx_queue(
  545. ndev, q_idx));
  546. } else {
  547. netdev_err(ndev, "Unknown send completion packet type- "
  548. "%d received!!\n", nvsp_packet->hdr.msg_type);
  549. }
  550. }
  551. static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
  552. {
  553. unsigned long index;
  554. u32 max_words = net_device->map_words;
  555. unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
  556. u32 section_cnt = net_device->send_section_cnt;
  557. int ret_val = NETVSC_INVALID_INDEX;
  558. int i;
  559. int prev_val;
  560. for (i = 0; i < max_words; i++) {
  561. if (!~(map_addr[i]))
  562. continue;
  563. index = ffz(map_addr[i]);
  564. prev_val = sync_test_and_set_bit(index, &map_addr[i]);
  565. if (prev_val)
  566. continue;
  567. if ((index + (i * BITS_PER_LONG)) >= section_cnt)
  568. break;
  569. ret_val = (index + (i * BITS_PER_LONG));
  570. break;
  571. }
  572. return ret_val;
  573. }
  574. static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
  575. unsigned int section_index,
  576. struct hv_netvsc_packet *packet)
  577. {
  578. char *start = net_device->send_buf;
  579. char *dest = (start + (section_index * net_device->send_section_size));
  580. int i;
  581. u32 msg_size = 0;
  582. for (i = 0; i < packet->page_buf_cnt; i++) {
  583. char *src = phys_to_virt(packet->page_buf[i].pfn << PAGE_SHIFT);
  584. u32 offset = packet->page_buf[i].offset;
  585. u32 len = packet->page_buf[i].len;
  586. memcpy(dest, (src + offset), len);
  587. msg_size += len;
  588. dest += len;
  589. }
  590. return msg_size;
  591. }
  592. int netvsc_send(struct hv_device *device,
  593. struct hv_netvsc_packet *packet)
  594. {
  595. struct netvsc_device *net_device;
  596. int ret = 0;
  597. struct nvsp_message sendMessage;
  598. struct net_device *ndev;
  599. struct vmbus_channel *out_channel = NULL;
  600. u64 req_id;
  601. unsigned int section_index = NETVSC_INVALID_INDEX;
  602. u32 msg_size = 0;
  603. struct sk_buff *skb = NULL;
  604. u16 q_idx = packet->q_idx;
  605. net_device = get_outbound_net_device(device);
  606. if (!net_device)
  607. return -ENODEV;
  608. ndev = net_device->ndev;
  609. sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  610. if (packet->is_data_pkt) {
  611. /* 0 is RMC_DATA; */
  612. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
  613. } else {
  614. /* 1 is RMC_CONTROL; */
  615. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
  616. }
  617. /* Attempt to send via sendbuf */
  618. if (packet->total_data_buflen < net_device->send_section_size) {
  619. section_index = netvsc_get_next_send_section(net_device);
  620. if (section_index != NETVSC_INVALID_INDEX) {
  621. msg_size = netvsc_copy_to_send_buf(net_device,
  622. section_index,
  623. packet);
  624. skb = (struct sk_buff *)
  625. (unsigned long)packet->send_completion_tid;
  626. packet->page_buf_cnt = 0;
  627. }
  628. }
  629. packet->send_buf_index = section_index;
  630. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
  631. section_index;
  632. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = msg_size;
  633. if (packet->send_completion)
  634. req_id = (ulong)packet;
  635. else
  636. req_id = 0;
  637. out_channel = net_device->chn_table[packet->q_idx];
  638. if (out_channel == NULL)
  639. out_channel = device->channel;
  640. packet->channel = out_channel;
  641. if (out_channel->rescind)
  642. return -ENODEV;
  643. if (packet->page_buf_cnt) {
  644. ret = vmbus_sendpacket_pagebuffer(out_channel,
  645. packet->page_buf,
  646. packet->page_buf_cnt,
  647. &sendMessage,
  648. sizeof(struct nvsp_message),
  649. req_id);
  650. } else {
  651. ret = vmbus_sendpacket(out_channel, &sendMessage,
  652. sizeof(struct nvsp_message),
  653. req_id,
  654. VM_PKT_DATA_INBAND,
  655. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  656. }
  657. if (ret == 0) {
  658. atomic_inc(&net_device->num_outstanding_sends);
  659. atomic_inc(&net_device->queue_sends[q_idx]);
  660. if (hv_ringbuf_avail_percent(&out_channel->outbound) <
  661. RING_AVAIL_PERCENT_LOWATER) {
  662. netif_tx_stop_queue(netdev_get_tx_queue(
  663. ndev, q_idx));
  664. if (atomic_read(&net_device->
  665. queue_sends[q_idx]) < 1)
  666. netif_tx_wake_queue(netdev_get_tx_queue(
  667. ndev, q_idx));
  668. }
  669. } else if (ret == -EAGAIN) {
  670. netif_tx_stop_queue(netdev_get_tx_queue(
  671. ndev, q_idx));
  672. if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
  673. netif_tx_wake_queue(netdev_get_tx_queue(
  674. ndev, q_idx));
  675. ret = -ENOSPC;
  676. }
  677. } else {
  678. netdev_err(ndev, "Unable to send packet %p ret %d\n",
  679. packet, ret);
  680. }
  681. if (ret != 0) {
  682. if (section_index != NETVSC_INVALID_INDEX)
  683. netvsc_free_send_slot(net_device, section_index);
  684. } else if (skb) {
  685. dev_kfree_skb_any(skb);
  686. }
  687. return ret;
  688. }
  689. static void netvsc_send_recv_completion(struct hv_device *device,
  690. struct vmbus_channel *channel,
  691. struct netvsc_device *net_device,
  692. u64 transaction_id, u32 status)
  693. {
  694. struct nvsp_message recvcompMessage;
  695. int retries = 0;
  696. int ret;
  697. struct net_device *ndev;
  698. ndev = net_device->ndev;
  699. recvcompMessage.hdr.msg_type =
  700. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
  701. recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
  702. retry_send_cmplt:
  703. /* Send the completion */
  704. ret = vmbus_sendpacket(channel, &recvcompMessage,
  705. sizeof(struct nvsp_message), transaction_id,
  706. VM_PKT_COMP, 0);
  707. if (ret == 0) {
  708. /* success */
  709. /* no-op */
  710. } else if (ret == -EAGAIN) {
  711. /* no more room...wait a bit and attempt to retry 3 times */
  712. retries++;
  713. netdev_err(ndev, "unable to send receive completion pkt"
  714. " (tid %llx)...retrying %d\n", transaction_id, retries);
  715. if (retries < 4) {
  716. udelay(100);
  717. goto retry_send_cmplt;
  718. } else {
  719. netdev_err(ndev, "unable to send receive "
  720. "completion pkt (tid %llx)...give up retrying\n",
  721. transaction_id);
  722. }
  723. } else {
  724. netdev_err(ndev, "unable to send receive "
  725. "completion pkt - %llx\n", transaction_id);
  726. }
  727. }
  728. static void netvsc_receive(struct netvsc_device *net_device,
  729. struct vmbus_channel *channel,
  730. struct hv_device *device,
  731. struct vmpacket_descriptor *packet)
  732. {
  733. struct vmtransfer_page_packet_header *vmxferpage_packet;
  734. struct nvsp_message *nvsp_packet;
  735. struct hv_netvsc_packet nv_pkt;
  736. struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
  737. u32 status = NVSP_STAT_SUCCESS;
  738. int i;
  739. int count = 0;
  740. struct net_device *ndev;
  741. ndev = net_device->ndev;
  742. /*
  743. * All inbound packets other than send completion should be xfer page
  744. * packet
  745. */
  746. if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
  747. netdev_err(ndev, "Unknown packet type received - %d\n",
  748. packet->type);
  749. return;
  750. }
  751. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  752. (packet->offset8 << 3));
  753. /* Make sure this is a valid nvsp packet */
  754. if (nvsp_packet->hdr.msg_type !=
  755. NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
  756. netdev_err(ndev, "Unknown nvsp packet type received-"
  757. " %d\n", nvsp_packet->hdr.msg_type);
  758. return;
  759. }
  760. vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
  761. if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
  762. netdev_err(ndev, "Invalid xfer page set id - "
  763. "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
  764. vmxferpage_packet->xfer_pageset_id);
  765. return;
  766. }
  767. count = vmxferpage_packet->range_cnt;
  768. netvsc_packet->device = device;
  769. netvsc_packet->channel = channel;
  770. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  771. for (i = 0; i < count; i++) {
  772. /* Initialize the netvsc packet */
  773. netvsc_packet->status = NVSP_STAT_SUCCESS;
  774. netvsc_packet->data = (void *)((unsigned long)net_device->
  775. recv_buf + vmxferpage_packet->ranges[i].byte_offset);
  776. netvsc_packet->total_data_buflen =
  777. vmxferpage_packet->ranges[i].byte_count;
  778. /* Pass it to the upper layer */
  779. rndis_filter_receive(device, netvsc_packet);
  780. if (netvsc_packet->status != NVSP_STAT_SUCCESS)
  781. status = NVSP_STAT_FAIL;
  782. }
  783. netvsc_send_recv_completion(device, channel, net_device,
  784. vmxferpage_packet->d.trans_id, status);
  785. }
  786. static void netvsc_send_table(struct hv_device *hdev,
  787. struct vmpacket_descriptor *vmpkt)
  788. {
  789. struct netvsc_device *nvscdev;
  790. struct net_device *ndev;
  791. struct nvsp_message *nvmsg;
  792. int i;
  793. u32 count, *tab;
  794. nvscdev = get_outbound_net_device(hdev);
  795. if (!nvscdev)
  796. return;
  797. ndev = nvscdev->ndev;
  798. nvmsg = (struct nvsp_message *)((unsigned long)vmpkt +
  799. (vmpkt->offset8 << 3));
  800. if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE)
  801. return;
  802. count = nvmsg->msg.v5_msg.send_table.count;
  803. if (count != VRSS_SEND_TAB_SIZE) {
  804. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  805. return;
  806. }
  807. tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
  808. nvmsg->msg.v5_msg.send_table.offset);
  809. for (i = 0; i < count; i++)
  810. nvscdev->send_table[i] = tab[i];
  811. }
  812. void netvsc_channel_cb(void *context)
  813. {
  814. int ret;
  815. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  816. struct hv_device *device;
  817. struct netvsc_device *net_device;
  818. u32 bytes_recvd;
  819. u64 request_id;
  820. struct vmpacket_descriptor *desc;
  821. unsigned char *buffer;
  822. int bufferlen = NETVSC_PACKET_SIZE;
  823. struct net_device *ndev;
  824. if (channel->primary_channel != NULL)
  825. device = channel->primary_channel->device_obj;
  826. else
  827. device = channel->device_obj;
  828. net_device = get_inbound_net_device(device);
  829. if (!net_device)
  830. return;
  831. ndev = net_device->ndev;
  832. buffer = get_per_channel_state(channel);
  833. do {
  834. ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
  835. &bytes_recvd, &request_id);
  836. if (ret == 0) {
  837. if (bytes_recvd > 0) {
  838. desc = (struct vmpacket_descriptor *)buffer;
  839. switch (desc->type) {
  840. case VM_PKT_COMP:
  841. netvsc_send_completion(net_device,
  842. device, desc);
  843. break;
  844. case VM_PKT_DATA_USING_XFER_PAGES:
  845. netvsc_receive(net_device, channel,
  846. device, desc);
  847. break;
  848. case VM_PKT_DATA_INBAND:
  849. netvsc_send_table(device, desc);
  850. break;
  851. default:
  852. netdev_err(ndev,
  853. "unhandled packet type %d, "
  854. "tid %llx len %d\n",
  855. desc->type, request_id,
  856. bytes_recvd);
  857. break;
  858. }
  859. } else {
  860. /*
  861. * We are done for this pass.
  862. */
  863. break;
  864. }
  865. } else if (ret == -ENOBUFS) {
  866. if (bufferlen > NETVSC_PACKET_SIZE)
  867. kfree(buffer);
  868. /* Handle large packet */
  869. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  870. if (buffer == NULL) {
  871. /* Try again next time around */
  872. netdev_err(ndev,
  873. "unable to allocate buffer of size "
  874. "(%d)!!\n", bytes_recvd);
  875. break;
  876. }
  877. bufferlen = bytes_recvd;
  878. }
  879. } while (1);
  880. if (bufferlen > NETVSC_PACKET_SIZE)
  881. kfree(buffer);
  882. return;
  883. }
  884. /*
  885. * netvsc_device_add - Callback when the device belonging to this
  886. * driver is added
  887. */
  888. int netvsc_device_add(struct hv_device *device, void *additional_info)
  889. {
  890. int ret = 0;
  891. int ring_size =
  892. ((struct netvsc_device_info *)additional_info)->ring_size;
  893. struct netvsc_device *net_device;
  894. struct net_device *ndev;
  895. net_device = alloc_net_device(device);
  896. if (!net_device)
  897. return -ENOMEM;
  898. net_device->ring_size = ring_size;
  899. /*
  900. * Coming into this function, struct net_device * is
  901. * registered as the driver private data.
  902. * In alloc_net_device(), we register struct netvsc_device *
  903. * as the driver private data and stash away struct net_device *
  904. * in struct netvsc_device *.
  905. */
  906. ndev = net_device->ndev;
  907. /* Initialize the NetVSC channel extension */
  908. init_completion(&net_device->channel_init_wait);
  909. set_per_channel_state(device->channel, net_device->cb_buffer);
  910. /* Open the channel */
  911. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  912. ring_size * PAGE_SIZE, NULL, 0,
  913. netvsc_channel_cb, device->channel);
  914. if (ret != 0) {
  915. netdev_err(ndev, "unable to open channel: %d\n", ret);
  916. goto cleanup;
  917. }
  918. /* Channel is opened */
  919. pr_info("hv_netvsc channel opened successfully\n");
  920. net_device->chn_table[0] = device->channel;
  921. /* Connect with the NetVsp */
  922. ret = netvsc_connect_vsp(device);
  923. if (ret != 0) {
  924. netdev_err(ndev,
  925. "unable to connect to NetVSP - %d\n", ret);
  926. goto close;
  927. }
  928. return ret;
  929. close:
  930. /* Now, we can close the channel safely */
  931. vmbus_close(device->channel);
  932. cleanup:
  933. free_netvsc_device(net_device);
  934. return ret;
  935. }