netvsc.c 29 KB

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