netvsc.c 29 KB

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