netvsc.c 29 KB

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