netvsc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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. int 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, t;
  350. memset(init_packet, 0, sizeof(struct nvsp_message));
  351. init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
  352. init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
  353. init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
  354. /* Send the init request */
  355. ret = vmbus_sendpacket(device->channel, init_packet,
  356. sizeof(struct nvsp_message),
  357. (unsigned long)init_packet,
  358. VM_PKT_DATA_INBAND,
  359. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  360. if (ret != 0)
  361. return ret;
  362. t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
  363. if (t == 0)
  364. return -ETIMEDOUT;
  365. if (init_packet->msg.init_msg.init_complete.status !=
  366. NVSP_STAT_SUCCESS)
  367. return -EINVAL;
  368. if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
  369. return 0;
  370. /* NVSPv2 only: Send NDIS config */
  371. memset(init_packet, 0, sizeof(struct nvsp_message));
  372. init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
  373. init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu +
  374. ETH_HLEN;
  375. init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
  376. ret = vmbus_sendpacket(device->channel, init_packet,
  377. sizeof(struct nvsp_message),
  378. (unsigned long)init_packet,
  379. VM_PKT_DATA_INBAND, 0);
  380. return ret;
  381. }
  382. static int netvsc_connect_vsp(struct hv_device *device)
  383. {
  384. int ret;
  385. struct netvsc_device *net_device;
  386. struct nvsp_message *init_packet;
  387. int ndis_version;
  388. struct net_device *ndev;
  389. u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
  390. NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
  391. int i, num_ver = 4; /* number of different NVSP versions */
  392. net_device = get_outbound_net_device(device);
  393. if (!net_device)
  394. return -ENODEV;
  395. ndev = net_device->ndev;
  396. init_packet = &net_device->channel_init_pkt;
  397. /* Negotiate the latest NVSP protocol supported */
  398. for (i = num_ver - 1; i >= 0; i--)
  399. if (negotiate_nvsp_ver(device, net_device, init_packet,
  400. ver_list[i]) == 0) {
  401. net_device->nvsp_version = ver_list[i];
  402. break;
  403. }
  404. if (i < 0) {
  405. ret = -EPROTO;
  406. goto cleanup;
  407. }
  408. pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
  409. /* Send the ndis version */
  410. memset(init_packet, 0, sizeof(struct nvsp_message));
  411. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
  412. ndis_version = 0x00060001;
  413. else
  414. ndis_version = 0x0006001e;
  415. init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
  416. init_packet->msg.v1_msg.
  417. send_ndis_ver.ndis_major_ver =
  418. (ndis_version & 0xFFFF0000) >> 16;
  419. init_packet->msg.v1_msg.
  420. send_ndis_ver.ndis_minor_ver =
  421. ndis_version & 0xFFFF;
  422. /* Send the init request */
  423. ret = vmbus_sendpacket(device->channel, init_packet,
  424. sizeof(struct nvsp_message),
  425. (unsigned long)init_packet,
  426. VM_PKT_DATA_INBAND, 0);
  427. if (ret != 0)
  428. goto cleanup;
  429. /* Post the big receive buffer to NetVSP */
  430. if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  431. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  432. else
  433. net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  434. net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
  435. ret = netvsc_init_buf(device);
  436. cleanup:
  437. return ret;
  438. }
  439. static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
  440. {
  441. netvsc_destroy_buf(net_device);
  442. }
  443. /*
  444. * netvsc_device_remove - Callback when the root bus device is removed
  445. */
  446. int netvsc_device_remove(struct hv_device *device)
  447. {
  448. struct netvsc_device *net_device;
  449. unsigned long flags;
  450. net_device = hv_get_drvdata(device);
  451. netvsc_disconnect_vsp(net_device);
  452. /*
  453. * Since we have already drained, we don't need to busy wait
  454. * as was done in final_release_stor_device()
  455. * Note that we cannot set the ext pointer to NULL until
  456. * we have drained - to drain the outgoing packets, we need to
  457. * allow incoming packets.
  458. */
  459. spin_lock_irqsave(&device->channel->inbound_lock, flags);
  460. hv_set_drvdata(device, NULL);
  461. spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
  462. /*
  463. * At this point, no one should be accessing net_device
  464. * except in here
  465. */
  466. dev_notice(&device->device, "net device safe to remove\n");
  467. /* Now, we can close the channel safely */
  468. vmbus_close(device->channel);
  469. /* Release all resources */
  470. vfree(net_device->sub_cb_buf);
  471. free_netvsc_device(net_device);
  472. return 0;
  473. }
  474. #define RING_AVAIL_PERCENT_HIWATER 20
  475. #define RING_AVAIL_PERCENT_LOWATER 10
  476. /*
  477. * Get the percentage of available bytes to write in the ring.
  478. * The return value is in range from 0 to 100.
  479. */
  480. static inline u32 hv_ringbuf_avail_percent(
  481. struct hv_ring_buffer_info *ring_info)
  482. {
  483. u32 avail_read, avail_write;
  484. hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
  485. return avail_write * 100 / ring_info->ring_datasize;
  486. }
  487. static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
  488. u32 index)
  489. {
  490. sync_change_bit(index, net_device->send_section_map);
  491. }
  492. static void netvsc_send_completion(struct netvsc_device *net_device,
  493. struct hv_device *device,
  494. struct vmpacket_descriptor *packet)
  495. {
  496. struct nvsp_message *nvsp_packet;
  497. struct hv_netvsc_packet *nvsc_packet;
  498. struct net_device *ndev;
  499. u32 send_index;
  500. ndev = net_device->ndev;
  501. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  502. (packet->offset8 << 3));
  503. if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
  504. (nvsp_packet->hdr.msg_type ==
  505. NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
  506. (nvsp_packet->hdr.msg_type ==
  507. NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
  508. (nvsp_packet->hdr.msg_type ==
  509. NVSP_MSG5_TYPE_SUBCHANNEL)) {
  510. /* Copy the response back */
  511. memcpy(&net_device->channel_init_pkt, nvsp_packet,
  512. sizeof(struct nvsp_message));
  513. complete(&net_device->channel_init_wait);
  514. } else if (nvsp_packet->hdr.msg_type ==
  515. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
  516. int num_outstanding_sends;
  517. u16 q_idx = 0;
  518. struct vmbus_channel *channel = device->channel;
  519. int queue_sends;
  520. /* Get the send context */
  521. nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
  522. packet->trans_id;
  523. /* Notify the layer above us */
  524. if (nvsc_packet) {
  525. send_index = nvsc_packet->send_buf_index;
  526. if (send_index != NETVSC_INVALID_INDEX)
  527. netvsc_free_send_slot(net_device, send_index);
  528. q_idx = nvsc_packet->q_idx;
  529. channel = nvsc_packet->channel;
  530. nvsc_packet->send_completion(nvsc_packet->
  531. send_completion_ctx);
  532. }
  533. num_outstanding_sends =
  534. atomic_dec_return(&net_device->num_outstanding_sends);
  535. queue_sends = atomic_dec_return(&net_device->
  536. queue_sends[q_idx]);
  537. if (net_device->destroy && num_outstanding_sends == 0)
  538. wake_up(&net_device->wait_drain);
  539. if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
  540. !net_device->start_remove &&
  541. (hv_ringbuf_avail_percent(&channel->outbound) >
  542. RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
  543. netif_tx_wake_queue(netdev_get_tx_queue(
  544. ndev, q_idx));
  545. } else {
  546. netdev_err(ndev, "Unknown send completion packet type- "
  547. "%d received!!\n", nvsp_packet->hdr.msg_type);
  548. }
  549. }
  550. static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
  551. {
  552. unsigned long index;
  553. u32 max_words = net_device->map_words;
  554. unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
  555. u32 section_cnt = net_device->send_section_cnt;
  556. int ret_val = NETVSC_INVALID_INDEX;
  557. int i;
  558. int prev_val;
  559. for (i = 0; i < max_words; i++) {
  560. if (!~(map_addr[i]))
  561. continue;
  562. index = ffz(map_addr[i]);
  563. prev_val = sync_test_and_set_bit(index, &map_addr[i]);
  564. if (prev_val)
  565. continue;
  566. if ((index + (i * BITS_PER_LONG)) >= section_cnt)
  567. break;
  568. ret_val = (index + (i * BITS_PER_LONG));
  569. break;
  570. }
  571. return ret_val;
  572. }
  573. u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
  574. unsigned int section_index,
  575. struct hv_netvsc_packet *packet)
  576. {
  577. char *start = net_device->send_buf;
  578. char *dest = (start + (section_index * net_device->send_section_size));
  579. int i;
  580. u32 msg_size = 0;
  581. for (i = 0; i < packet->page_buf_cnt; i++) {
  582. char *src = phys_to_virt(packet->page_buf[i].pfn << PAGE_SHIFT);
  583. u32 offset = packet->page_buf[i].offset;
  584. u32 len = packet->page_buf[i].len;
  585. memcpy(dest, (src + offset), len);
  586. msg_size += len;
  587. dest += len;
  588. }
  589. return msg_size;
  590. }
  591. int netvsc_send(struct hv_device *device,
  592. struct hv_netvsc_packet *packet)
  593. {
  594. struct netvsc_device *net_device;
  595. int ret = 0;
  596. struct nvsp_message sendMessage;
  597. struct net_device *ndev;
  598. struct vmbus_channel *out_channel = NULL;
  599. u64 req_id;
  600. unsigned int section_index = NETVSC_INVALID_INDEX;
  601. u32 msg_size = 0;
  602. struct sk_buff *skb;
  603. u16 q_idx = packet->q_idx;
  604. net_device = get_outbound_net_device(device);
  605. if (!net_device)
  606. return -ENODEV;
  607. ndev = net_device->ndev;
  608. sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
  609. if (packet->is_data_pkt) {
  610. /* 0 is RMC_DATA; */
  611. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
  612. } else {
  613. /* 1 is RMC_CONTROL; */
  614. sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
  615. }
  616. /* Attempt to send via sendbuf */
  617. if (packet->total_data_buflen < net_device->send_section_size) {
  618. section_index = netvsc_get_next_send_section(net_device);
  619. if (section_index != NETVSC_INVALID_INDEX) {
  620. msg_size = netvsc_copy_to_send_buf(net_device,
  621. section_index,
  622. packet);
  623. skb = (struct sk_buff *)
  624. (unsigned long)packet->send_completion_tid;
  625. if (skb)
  626. dev_kfree_skb_any(skb);
  627. packet->page_buf_cnt = 0;
  628. }
  629. }
  630. packet->send_buf_index = section_index;
  631. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
  632. section_index;
  633. sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = msg_size;
  634. if (packet->send_completion)
  635. req_id = (ulong)packet;
  636. else
  637. req_id = 0;
  638. out_channel = net_device->chn_table[packet->q_idx];
  639. if (out_channel == NULL)
  640. out_channel = device->channel;
  641. packet->channel = out_channel;
  642. if (out_channel->rescind)
  643. return -ENODEV;
  644. if (packet->page_buf_cnt) {
  645. ret = vmbus_sendpacket_pagebuffer(out_channel,
  646. packet->page_buf,
  647. packet->page_buf_cnt,
  648. &sendMessage,
  649. sizeof(struct nvsp_message),
  650. req_id);
  651. } else {
  652. ret = vmbus_sendpacket(out_channel, &sendMessage,
  653. sizeof(struct nvsp_message),
  654. req_id,
  655. VM_PKT_DATA_INBAND,
  656. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  657. }
  658. if (ret == 0) {
  659. atomic_inc(&net_device->num_outstanding_sends);
  660. atomic_inc(&net_device->queue_sends[q_idx]);
  661. if (hv_ringbuf_avail_percent(&out_channel->outbound) <
  662. RING_AVAIL_PERCENT_LOWATER) {
  663. netif_tx_stop_queue(netdev_get_tx_queue(
  664. ndev, q_idx));
  665. if (atomic_read(&net_device->
  666. queue_sends[q_idx]) < 1)
  667. netif_tx_wake_queue(netdev_get_tx_queue(
  668. ndev, q_idx));
  669. }
  670. } else if (ret == -EAGAIN) {
  671. netif_tx_stop_queue(netdev_get_tx_queue(
  672. ndev, q_idx));
  673. if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
  674. netif_tx_wake_queue(netdev_get_tx_queue(
  675. ndev, q_idx));
  676. ret = -ENOSPC;
  677. }
  678. } else {
  679. netdev_err(ndev, "Unable to send packet %p ret %d\n",
  680. packet, ret);
  681. }
  682. return ret;
  683. }
  684. static void netvsc_send_recv_completion(struct hv_device *device,
  685. struct vmbus_channel *channel,
  686. struct netvsc_device *net_device,
  687. u64 transaction_id, u32 status)
  688. {
  689. struct nvsp_message recvcompMessage;
  690. int retries = 0;
  691. int ret;
  692. struct net_device *ndev;
  693. ndev = net_device->ndev;
  694. recvcompMessage.hdr.msg_type =
  695. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
  696. recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
  697. retry_send_cmplt:
  698. /* Send the completion */
  699. ret = vmbus_sendpacket(channel, &recvcompMessage,
  700. sizeof(struct nvsp_message), transaction_id,
  701. VM_PKT_COMP, 0);
  702. if (ret == 0) {
  703. /* success */
  704. /* no-op */
  705. } else if (ret == -EAGAIN) {
  706. /* no more room...wait a bit and attempt to retry 3 times */
  707. retries++;
  708. netdev_err(ndev, "unable to send receive completion pkt"
  709. " (tid %llx)...retrying %d\n", transaction_id, retries);
  710. if (retries < 4) {
  711. udelay(100);
  712. goto retry_send_cmplt;
  713. } else {
  714. netdev_err(ndev, "unable to send receive "
  715. "completion pkt (tid %llx)...give up retrying\n",
  716. transaction_id);
  717. }
  718. } else {
  719. netdev_err(ndev, "unable to send receive "
  720. "completion pkt - %llx\n", transaction_id);
  721. }
  722. }
  723. static void netvsc_receive(struct netvsc_device *net_device,
  724. struct vmbus_channel *channel,
  725. struct hv_device *device,
  726. struct vmpacket_descriptor *packet)
  727. {
  728. struct vmtransfer_page_packet_header *vmxferpage_packet;
  729. struct nvsp_message *nvsp_packet;
  730. struct hv_netvsc_packet nv_pkt;
  731. struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
  732. u32 status = NVSP_STAT_SUCCESS;
  733. int i;
  734. int count = 0;
  735. struct net_device *ndev;
  736. ndev = net_device->ndev;
  737. /*
  738. * All inbound packets other than send completion should be xfer page
  739. * packet
  740. */
  741. if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
  742. netdev_err(ndev, "Unknown packet type received - %d\n",
  743. packet->type);
  744. return;
  745. }
  746. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  747. (packet->offset8 << 3));
  748. /* Make sure this is a valid nvsp packet */
  749. if (nvsp_packet->hdr.msg_type !=
  750. NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
  751. netdev_err(ndev, "Unknown nvsp packet type received-"
  752. " %d\n", nvsp_packet->hdr.msg_type);
  753. return;
  754. }
  755. vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
  756. if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
  757. netdev_err(ndev, "Invalid xfer page set id - "
  758. "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
  759. vmxferpage_packet->xfer_pageset_id);
  760. return;
  761. }
  762. count = vmxferpage_packet->range_cnt;
  763. netvsc_packet->device = device;
  764. netvsc_packet->channel = channel;
  765. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  766. for (i = 0; i < count; i++) {
  767. /* Initialize the netvsc packet */
  768. netvsc_packet->status = NVSP_STAT_SUCCESS;
  769. netvsc_packet->data = (void *)((unsigned long)net_device->
  770. recv_buf + vmxferpage_packet->ranges[i].byte_offset);
  771. netvsc_packet->total_data_buflen =
  772. vmxferpage_packet->ranges[i].byte_count;
  773. /* Pass it to the upper layer */
  774. rndis_filter_receive(device, netvsc_packet);
  775. if (netvsc_packet->status != NVSP_STAT_SUCCESS)
  776. status = NVSP_STAT_FAIL;
  777. }
  778. netvsc_send_recv_completion(device, channel, net_device,
  779. vmxferpage_packet->d.trans_id, status);
  780. }
  781. static void netvsc_send_table(struct hv_device *hdev,
  782. struct vmpacket_descriptor *vmpkt)
  783. {
  784. struct netvsc_device *nvscdev;
  785. struct net_device *ndev;
  786. struct nvsp_message *nvmsg;
  787. int i;
  788. u32 count, *tab;
  789. nvscdev = get_outbound_net_device(hdev);
  790. if (!nvscdev)
  791. return;
  792. ndev = nvscdev->ndev;
  793. nvmsg = (struct nvsp_message *)((unsigned long)vmpkt +
  794. (vmpkt->offset8 << 3));
  795. if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE)
  796. return;
  797. count = nvmsg->msg.v5_msg.send_table.count;
  798. if (count != VRSS_SEND_TAB_SIZE) {
  799. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  800. return;
  801. }
  802. tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
  803. nvmsg->msg.v5_msg.send_table.offset);
  804. for (i = 0; i < count; i++)
  805. nvscdev->send_table[i] = tab[i];
  806. }
  807. void netvsc_channel_cb(void *context)
  808. {
  809. int ret;
  810. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  811. struct hv_device *device;
  812. struct netvsc_device *net_device;
  813. u32 bytes_recvd;
  814. u64 request_id;
  815. struct vmpacket_descriptor *desc;
  816. unsigned char *buffer;
  817. int bufferlen = NETVSC_PACKET_SIZE;
  818. struct net_device *ndev;
  819. if (channel->primary_channel != NULL)
  820. device = channel->primary_channel->device_obj;
  821. else
  822. device = channel->device_obj;
  823. net_device = get_inbound_net_device(device);
  824. if (!net_device)
  825. return;
  826. ndev = net_device->ndev;
  827. buffer = get_per_channel_state(channel);
  828. do {
  829. ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
  830. &bytes_recvd, &request_id);
  831. if (ret == 0) {
  832. if (bytes_recvd > 0) {
  833. desc = (struct vmpacket_descriptor *)buffer;
  834. switch (desc->type) {
  835. case VM_PKT_COMP:
  836. netvsc_send_completion(net_device,
  837. device, desc);
  838. break;
  839. case VM_PKT_DATA_USING_XFER_PAGES:
  840. netvsc_receive(net_device, channel,
  841. device, desc);
  842. break;
  843. case VM_PKT_DATA_INBAND:
  844. netvsc_send_table(device, desc);
  845. break;
  846. default:
  847. netdev_err(ndev,
  848. "unhandled packet type %d, "
  849. "tid %llx len %d\n",
  850. desc->type, request_id,
  851. bytes_recvd);
  852. break;
  853. }
  854. } else {
  855. /*
  856. * We are done for this pass.
  857. */
  858. break;
  859. }
  860. } else if (ret == -ENOBUFS) {
  861. if (bufferlen > NETVSC_PACKET_SIZE)
  862. kfree(buffer);
  863. /* Handle large packet */
  864. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  865. if (buffer == NULL) {
  866. /* Try again next time around */
  867. netdev_err(ndev,
  868. "unable to allocate buffer of size "
  869. "(%d)!!\n", bytes_recvd);
  870. break;
  871. }
  872. bufferlen = bytes_recvd;
  873. }
  874. } while (1);
  875. if (bufferlen > NETVSC_PACKET_SIZE)
  876. kfree(buffer);
  877. return;
  878. }
  879. /*
  880. * netvsc_device_add - Callback when the device belonging to this
  881. * driver is added
  882. */
  883. int netvsc_device_add(struct hv_device *device, void *additional_info)
  884. {
  885. int ret = 0;
  886. int ring_size =
  887. ((struct netvsc_device_info *)additional_info)->ring_size;
  888. struct netvsc_device *net_device;
  889. struct net_device *ndev;
  890. net_device = alloc_net_device(device);
  891. if (!net_device)
  892. return -ENOMEM;
  893. net_device->ring_size = ring_size;
  894. /*
  895. * Coming into this function, struct net_device * is
  896. * registered as the driver private data.
  897. * In alloc_net_device(), we register struct netvsc_device *
  898. * as the driver private data and stash away struct net_device *
  899. * in struct netvsc_device *.
  900. */
  901. ndev = net_device->ndev;
  902. /* Initialize the NetVSC channel extension */
  903. init_completion(&net_device->channel_init_wait);
  904. set_per_channel_state(device->channel, net_device->cb_buffer);
  905. /* Open the channel */
  906. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  907. ring_size * PAGE_SIZE, NULL, 0,
  908. netvsc_channel_cb, device->channel);
  909. if (ret != 0) {
  910. netdev_err(ndev, "unable to open channel: %d\n", ret);
  911. goto cleanup;
  912. }
  913. /* Channel is opened */
  914. pr_info("hv_netvsc channel opened successfully\n");
  915. net_device->chn_table[0] = device->channel;
  916. /* Connect with the NetVsp */
  917. ret = netvsc_connect_vsp(device);
  918. if (ret != 0) {
  919. netdev_err(ndev,
  920. "unable to connect to NetVSP - %d\n", ret);
  921. goto close;
  922. }
  923. return ret;
  924. close:
  925. /* Now, we can close the channel safely */
  926. vmbus_close(device->channel);
  927. cleanup:
  928. free_netvsc_device(net_device);
  929. return ret;
  930. }