netvsc.c 33 KB

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