netvsc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. dev_info(&device->device, "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. dev_notice(&device->device, "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. out_channel = net_device->chn_table[q_idx];
  758. packet->send_buf_index = NETVSC_INVALID_INDEX;
  759. packet->cp_partial = false;
  760. /* Send control message directly without accessing msd (Multi-Send
  761. * Data) field which may be changed during data packet processing.
  762. */
  763. if (!skb) {
  764. cur_send = packet;
  765. goto send_now;
  766. }
  767. msdp = &net_device->msd[q_idx];
  768. /* batch packets in send buffer if possible */
  769. if (msdp->pkt)
  770. msd_len = msdp->pkt->total_data_buflen;
  771. try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
  772. net_device->max_pkt;
  773. if (try_batch && msd_len + pktlen + net_device->pkt_align <
  774. net_device->send_section_size) {
  775. section_index = msdp->pkt->send_buf_index;
  776. } else if (try_batch && msd_len + packet->rmsg_size <
  777. net_device->send_section_size) {
  778. section_index = msdp->pkt->send_buf_index;
  779. packet->cp_partial = true;
  780. } else if ((skb != NULL) && pktlen + net_device->pkt_align <
  781. net_device->send_section_size) {
  782. section_index = netvsc_get_next_send_section(net_device);
  783. if (section_index != NETVSC_INVALID_INDEX) {
  784. move_pkt_msd(&msd_send, &msd_skb, msdp);
  785. msd_len = 0;
  786. }
  787. }
  788. if (section_index != NETVSC_INVALID_INDEX) {
  789. netvsc_copy_to_send_buf(net_device,
  790. section_index, msd_len,
  791. packet, rndis_msg, pb, skb);
  792. packet->send_buf_index = section_index;
  793. if (packet->cp_partial) {
  794. packet->page_buf_cnt -= packet->rmsg_pgcnt;
  795. packet->total_data_buflen = msd_len + packet->rmsg_size;
  796. } else {
  797. packet->page_buf_cnt = 0;
  798. packet->total_data_buflen += msd_len;
  799. }
  800. if (msdp->skb)
  801. dev_consume_skb_any(msdp->skb);
  802. if (xmit_more && !packet->cp_partial) {
  803. msdp->skb = skb;
  804. msdp->pkt = packet;
  805. msdp->count++;
  806. } else {
  807. cur_send = packet;
  808. msdp->skb = NULL;
  809. msdp->pkt = NULL;
  810. msdp->count = 0;
  811. }
  812. } else {
  813. move_pkt_msd(&msd_send, &msd_skb, msdp);
  814. cur_send = packet;
  815. }
  816. if (msd_send) {
  817. int m_ret = netvsc_send_pkt(device, msd_send, net_device,
  818. NULL, msd_skb);
  819. if (m_ret != 0) {
  820. netvsc_free_send_slot(net_device,
  821. msd_send->send_buf_index);
  822. dev_kfree_skb_any(msd_skb);
  823. }
  824. }
  825. send_now:
  826. if (cur_send)
  827. ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
  828. if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
  829. netvsc_free_send_slot(net_device, section_index);
  830. return ret;
  831. }
  832. static int netvsc_send_recv_completion(struct vmbus_channel *channel,
  833. u64 transaction_id, u32 status)
  834. {
  835. struct nvsp_message recvcompMessage;
  836. int ret;
  837. recvcompMessage.hdr.msg_type =
  838. NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
  839. recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
  840. /* Send the completion */
  841. ret = vmbus_sendpacket(channel, &recvcompMessage,
  842. sizeof(struct nvsp_message_header) + sizeof(u32),
  843. transaction_id, VM_PKT_COMP, 0);
  844. return ret;
  845. }
  846. static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
  847. u32 *filled, u32 *avail)
  848. {
  849. u32 first = nvdev->mrc[q_idx].first;
  850. u32 next = nvdev->mrc[q_idx].next;
  851. *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
  852. next - first;
  853. *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
  854. }
  855. /* Read the first filled slot, no change to index */
  856. static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
  857. *nvdev, u16 q_idx)
  858. {
  859. u32 filled, avail;
  860. if (!nvdev->mrc[q_idx].buf)
  861. return NULL;
  862. count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
  863. if (!filled)
  864. return NULL;
  865. return nvdev->mrc[q_idx].buf + nvdev->mrc[q_idx].first *
  866. sizeof(struct recv_comp_data);
  867. }
  868. /* Put the first filled slot back to available pool */
  869. static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
  870. {
  871. int num_recv;
  872. nvdev->mrc[q_idx].first = (nvdev->mrc[q_idx].first + 1) %
  873. NETVSC_RECVSLOT_MAX;
  874. num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
  875. if (nvdev->destroy && num_recv == 0)
  876. wake_up(&nvdev->wait_drain);
  877. }
  878. /* Check and send pending recv completions */
  879. static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
  880. struct vmbus_channel *channel, u16 q_idx)
  881. {
  882. struct recv_comp_data *rcd;
  883. int ret;
  884. while (true) {
  885. rcd = read_recv_comp_slot(nvdev, q_idx);
  886. if (!rcd)
  887. break;
  888. ret = netvsc_send_recv_completion(channel, rcd->tid,
  889. rcd->status);
  890. if (ret)
  891. break;
  892. put_recv_comp_slot(nvdev, q_idx);
  893. }
  894. }
  895. #define NETVSC_RCD_WATERMARK 80
  896. /* Get next available slot */
  897. static inline struct recv_comp_data *get_recv_comp_slot(
  898. struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
  899. {
  900. u32 filled, avail, next;
  901. struct recv_comp_data *rcd;
  902. if (!nvdev->recv_section)
  903. return NULL;
  904. if (!nvdev->mrc[q_idx].buf)
  905. return NULL;
  906. if (atomic_read(&nvdev->num_outstanding_recvs) >
  907. nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
  908. netvsc_chk_recv_comp(nvdev, channel, q_idx);
  909. count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
  910. if (!avail)
  911. return NULL;
  912. next = nvdev->mrc[q_idx].next;
  913. rcd = nvdev->mrc[q_idx].buf + next * sizeof(struct recv_comp_data);
  914. nvdev->mrc[q_idx].next = (next + 1) % NETVSC_RECVSLOT_MAX;
  915. atomic_inc(&nvdev->num_outstanding_recvs);
  916. return rcd;
  917. }
  918. static void netvsc_receive(struct netvsc_device *net_device,
  919. struct vmbus_channel *channel,
  920. struct hv_device *device,
  921. struct vmpacket_descriptor *packet)
  922. {
  923. struct vmtransfer_page_packet_header *vmxferpage_packet;
  924. struct nvsp_message *nvsp_packet;
  925. struct hv_netvsc_packet nv_pkt;
  926. struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
  927. u32 status = NVSP_STAT_SUCCESS;
  928. int i;
  929. int count = 0;
  930. struct net_device *ndev = hv_get_drvdata(device);
  931. void *data;
  932. int ret;
  933. struct recv_comp_data *rcd;
  934. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  935. /*
  936. * All inbound packets other than send completion should be xfer page
  937. * packet
  938. */
  939. if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
  940. netdev_err(ndev, "Unknown packet type received - %d\n",
  941. packet->type);
  942. return;
  943. }
  944. nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
  945. (packet->offset8 << 3));
  946. /* Make sure this is a valid nvsp packet */
  947. if (nvsp_packet->hdr.msg_type !=
  948. NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
  949. netdev_err(ndev, "Unknown nvsp packet type received-"
  950. " %d\n", nvsp_packet->hdr.msg_type);
  951. return;
  952. }
  953. vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
  954. if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
  955. netdev_err(ndev, "Invalid xfer page set id - "
  956. "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
  957. vmxferpage_packet->xfer_pageset_id);
  958. return;
  959. }
  960. count = vmxferpage_packet->range_cnt;
  961. /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
  962. for (i = 0; i < count; i++) {
  963. /* Initialize the netvsc packet */
  964. data = (void *)((unsigned long)net_device->
  965. recv_buf + vmxferpage_packet->ranges[i].byte_offset);
  966. netvsc_packet->total_data_buflen =
  967. vmxferpage_packet->ranges[i].byte_count;
  968. /* Pass it to the upper layer */
  969. status = rndis_filter_receive(device, netvsc_packet, &data,
  970. channel);
  971. }
  972. if (!net_device->mrc[q_idx].buf) {
  973. ret = netvsc_send_recv_completion(channel,
  974. vmxferpage_packet->d.trans_id,
  975. status);
  976. if (ret)
  977. netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
  978. q_idx, vmxferpage_packet->d.trans_id, ret);
  979. return;
  980. }
  981. rcd = get_recv_comp_slot(net_device, channel, q_idx);
  982. if (!rcd) {
  983. netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
  984. q_idx, vmxferpage_packet->d.trans_id);
  985. return;
  986. }
  987. rcd->tid = vmxferpage_packet->d.trans_id;
  988. rcd->status = status;
  989. }
  990. static void netvsc_send_table(struct hv_device *hdev,
  991. struct nvsp_message *nvmsg)
  992. {
  993. struct netvsc_device *nvscdev;
  994. struct net_device *ndev = hv_get_drvdata(hdev);
  995. int i;
  996. u32 count, *tab;
  997. nvscdev = get_outbound_net_device(hdev);
  998. if (!nvscdev)
  999. return;
  1000. count = nvmsg->msg.v5_msg.send_table.count;
  1001. if (count != VRSS_SEND_TAB_SIZE) {
  1002. netdev_err(ndev, "Received wrong send-table size:%u\n", count);
  1003. return;
  1004. }
  1005. tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
  1006. nvmsg->msg.v5_msg.send_table.offset);
  1007. for (i = 0; i < count; i++)
  1008. nvscdev->send_table[i] = tab[i];
  1009. }
  1010. static void netvsc_send_vf(struct net_device_context *net_device_ctx,
  1011. struct nvsp_message *nvmsg)
  1012. {
  1013. net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
  1014. net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
  1015. }
  1016. static inline void netvsc_receive_inband(struct hv_device *hdev,
  1017. struct net_device_context *net_device_ctx,
  1018. struct nvsp_message *nvmsg)
  1019. {
  1020. switch (nvmsg->hdr.msg_type) {
  1021. case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
  1022. netvsc_send_table(hdev, nvmsg);
  1023. break;
  1024. case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
  1025. netvsc_send_vf(net_device_ctx, nvmsg);
  1026. break;
  1027. }
  1028. }
  1029. static void netvsc_process_raw_pkt(struct hv_device *device,
  1030. struct vmbus_channel *channel,
  1031. struct netvsc_device *net_device,
  1032. struct net_device *ndev,
  1033. u64 request_id,
  1034. struct vmpacket_descriptor *desc)
  1035. {
  1036. struct nvsp_message *nvmsg;
  1037. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1038. nvmsg = (struct nvsp_message *)((unsigned long)
  1039. desc + (desc->offset8 << 3));
  1040. switch (desc->type) {
  1041. case VM_PKT_COMP:
  1042. netvsc_send_completion(net_device, channel, device, desc);
  1043. break;
  1044. case VM_PKT_DATA_USING_XFER_PAGES:
  1045. netvsc_receive(net_device, channel, device, desc);
  1046. break;
  1047. case VM_PKT_DATA_INBAND:
  1048. netvsc_receive_inband(device, net_device_ctx, nvmsg);
  1049. break;
  1050. default:
  1051. netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
  1052. desc->type, request_id);
  1053. break;
  1054. }
  1055. }
  1056. void netvsc_channel_cb(void *context)
  1057. {
  1058. int ret;
  1059. struct vmbus_channel *channel = (struct vmbus_channel *)context;
  1060. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  1061. struct hv_device *device;
  1062. struct netvsc_device *net_device;
  1063. u32 bytes_recvd;
  1064. u64 request_id;
  1065. struct vmpacket_descriptor *desc;
  1066. unsigned char *buffer;
  1067. int bufferlen = NETVSC_PACKET_SIZE;
  1068. struct net_device *ndev;
  1069. bool need_to_commit = false;
  1070. if (channel->primary_channel != NULL)
  1071. device = channel->primary_channel->device_obj;
  1072. else
  1073. device = channel->device_obj;
  1074. net_device = get_inbound_net_device(device);
  1075. if (!net_device)
  1076. return;
  1077. ndev = hv_get_drvdata(device);
  1078. buffer = get_per_channel_state(channel);
  1079. /* commit_rd_index() -> hv_signal_on_read() needs this. */
  1080. init_cached_read_index(channel);
  1081. do {
  1082. desc = get_next_pkt_raw(channel);
  1083. if (desc != NULL) {
  1084. netvsc_process_raw_pkt(device,
  1085. channel,
  1086. net_device,
  1087. ndev,
  1088. desc->trans_id,
  1089. desc);
  1090. put_pkt_raw(channel, desc);
  1091. need_to_commit = true;
  1092. continue;
  1093. }
  1094. if (need_to_commit) {
  1095. need_to_commit = false;
  1096. commit_rd_index(channel);
  1097. }
  1098. ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
  1099. &bytes_recvd, &request_id);
  1100. if (ret == 0) {
  1101. if (bytes_recvd > 0) {
  1102. desc = (struct vmpacket_descriptor *)buffer;
  1103. netvsc_process_raw_pkt(device,
  1104. channel,
  1105. net_device,
  1106. ndev,
  1107. request_id,
  1108. desc);
  1109. } else {
  1110. /*
  1111. * We are done for this pass.
  1112. */
  1113. break;
  1114. }
  1115. } else if (ret == -ENOBUFS) {
  1116. if (bufferlen > NETVSC_PACKET_SIZE)
  1117. kfree(buffer);
  1118. /* Handle large packet */
  1119. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  1120. if (buffer == NULL) {
  1121. /* Try again next time around */
  1122. netdev_err(ndev,
  1123. "unable to allocate buffer of size "
  1124. "(%d)!!\n", bytes_recvd);
  1125. break;
  1126. }
  1127. bufferlen = bytes_recvd;
  1128. }
  1129. init_cached_read_index(channel);
  1130. } while (1);
  1131. if (bufferlen > NETVSC_PACKET_SIZE)
  1132. kfree(buffer);
  1133. netvsc_chk_recv_comp(net_device, channel, q_idx);
  1134. }
  1135. /*
  1136. * netvsc_device_add - Callback when the device belonging to this
  1137. * driver is added
  1138. */
  1139. int netvsc_device_add(struct hv_device *device, void *additional_info)
  1140. {
  1141. int i, ret = 0;
  1142. int ring_size =
  1143. ((struct netvsc_device_info *)additional_info)->ring_size;
  1144. struct netvsc_device *net_device;
  1145. struct net_device *ndev = hv_get_drvdata(device);
  1146. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  1147. net_device = alloc_net_device();
  1148. if (!net_device)
  1149. return -ENOMEM;
  1150. net_device->ring_size = ring_size;
  1151. set_per_channel_state(device->channel, net_device->cb_buffer);
  1152. /* Open the channel */
  1153. ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
  1154. ring_size * PAGE_SIZE, NULL, 0,
  1155. netvsc_channel_cb, device->channel);
  1156. if (ret != 0) {
  1157. netdev_err(ndev, "unable to open channel: %d\n", ret);
  1158. goto cleanup;
  1159. }
  1160. /* Channel is opened */
  1161. pr_info("hv_netvsc channel opened successfully\n");
  1162. /* If we're reopening the device we may have multiple queues, fill the
  1163. * chn_table with the default channel to use it before subchannels are
  1164. * opened.
  1165. */
  1166. for (i = 0; i < VRSS_CHANNEL_MAX; i++)
  1167. net_device->chn_table[i] = device->channel;
  1168. /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
  1169. * populated.
  1170. */
  1171. wmb();
  1172. net_device_ctx->nvdev = net_device;
  1173. /* Connect with the NetVsp */
  1174. ret = netvsc_connect_vsp(device);
  1175. if (ret != 0) {
  1176. netdev_err(ndev,
  1177. "unable to connect to NetVSP - %d\n", ret);
  1178. goto close;
  1179. }
  1180. return ret;
  1181. close:
  1182. /* Now, we can close the channel safely */
  1183. vmbus_close(device->channel);
  1184. cleanup:
  1185. free_netvsc_device(net_device);
  1186. return ret;
  1187. }