netvsc.c 38 KB

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