netvsc.c 29 KB

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