netvsc.c 32 KB

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