netvsc.c 38 KB

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