]>
Commit | Line | Data |
---|---|---|
fceaf24a | 1 | /* |
fceaf24a HJ |
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 | |
adf8d3ff | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
fceaf24a HJ |
15 | * |
16 | * Authors: | |
d0e94d17 | 17 | * Haiyang Zhang <haiyangz@microsoft.com> |
fceaf24a | 18 | * Hank Janssen <hjanssen@microsoft.com> |
fceaf24a | 19 | */ |
eb335bc4 HJ |
20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
21 | ||
5654e932 | 22 | #include <linux/kernel.h> |
0c3b7b2f S |
23 | #include <linux/sched.h> |
24 | #include <linux/wait.h> | |
0ffa63b0 | 25 | #include <linux/mm.h> |
b4362c9c | 26 | #include <linux/delay.h> |
21a80820 | 27 | #include <linux/io.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
d9871158 | 29 | #include <linux/netdevice.h> |
f157e78d | 30 | #include <linux/if_ether.h> |
d6472302 | 31 | #include <linux/vmalloc.h> |
9749fed5 | 32 | #include <linux/rtnetlink.h> |
43bf99ce | 33 | #include <linux/prefetch.h> |
9749fed5 | 34 | |
c25aaf81 | 35 | #include <asm/sync_bitops.h> |
3f335ea2 | 36 | |
5ca7252a | 37 | #include "hyperv_net.h" |
fceaf24a | 38 | |
84bf9cef KS |
39 | /* |
40 | * Switch the data path from the synthetic interface to the VF | |
41 | * interface. | |
42 | */ | |
0a1275ca | 43 | void netvsc_switch_datapath(struct net_device *ndev, bool vf) |
84bf9cef | 44 | { |
3d541ac5 VK |
45 | struct net_device_context *net_device_ctx = netdev_priv(ndev); |
46 | struct hv_device *dev = net_device_ctx->device_ctx; | |
79e8cbe7 | 47 | struct netvsc_device *nv_dev = rtnl_dereference(net_device_ctx->nvdev); |
0a1275ca | 48 | struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt; |
84bf9cef KS |
49 | |
50 | memset(init_pkt, 0, sizeof(struct nvsp_message)); | |
51 | init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH; | |
52 | if (vf) | |
53 | init_pkt->msg.v4_msg.active_dp.active_datapath = | |
54 | NVSP_DATAPATH_VF; | |
55 | else | |
56 | init_pkt->msg.v4_msg.active_dp.active_datapath = | |
57 | NVSP_DATAPATH_SYNTHETIC; | |
58 | ||
59 | vmbus_sendpacket(dev->channel, init_pkt, | |
60 | sizeof(struct nvsp_message), | |
61 | (unsigned long)init_pkt, | |
62 | VM_PKT_DATA_INBAND, 0); | |
63 | } | |
64 | ||
2950481a SH |
65 | /* Worker to setup sub channels on initial setup |
66 | * Initial hotplug event occurs in softirq context | |
67 | * and can't wait for channels. | |
68 | */ | |
69 | static void netvsc_subchan_work(struct work_struct *w) | |
70 | { | |
71 | struct netvsc_device *nvdev = | |
72 | container_of(w, struct netvsc_device, subchan_work); | |
73 | struct rndis_device *rdev; | |
74 | int i, ret; | |
75 | ||
76 | /* Avoid deadlock with device removal already under RTNL */ | |
77 | if (!rtnl_trylock()) { | |
78 | schedule_work(w); | |
79 | return; | |
80 | } | |
81 | ||
82 | rdev = nvdev->extension; | |
83 | if (rdev) { | |
84 | ret = rndis_set_subchannel(rdev->ndev, nvdev); | |
85 | if (ret == 0) { | |
86 | netif_device_attach(rdev->ndev); | |
87 | } else { | |
88 | /* fallback to only primary channel */ | |
89 | for (i = 1; i < nvdev->num_chn; i++) | |
90 | netif_napi_del(&nvdev->chan_table[i].napi); | |
91 | ||
92 | nvdev->max_chn = 1; | |
93 | nvdev->num_chn = 1; | |
94 | } | |
95 | } | |
96 | ||
97 | rtnl_unlock(); | |
98 | } | |
99 | ||
88098834 | 100 | static struct netvsc_device *alloc_net_device(void) |
fceaf24a | 101 | { |
85799a37 | 102 | struct netvsc_device *net_device; |
fceaf24a | 103 | |
85799a37 HZ |
104 | net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL); |
105 | if (!net_device) | |
fceaf24a HJ |
106 | return NULL; |
107 | ||
dc5cd894 | 108 | init_waitqueue_head(&net_device->wait_drain); |
c38b9c71 | 109 | net_device->destroy = false; |
84bf9cef | 110 | atomic_set(&net_device->open_cnt, 0); |
7c3877f2 HZ |
111 | net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT; |
112 | net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT; | |
8b532797 | 113 | |
fd612602 | 114 | init_completion(&net_device->channel_init_wait); |
732e4985 | 115 | init_waitqueue_head(&net_device->subchan_open); |
2950481a | 116 | INIT_WORK(&net_device->subchan_work, netvsc_subchan_work); |
7c3877f2 | 117 | |
85799a37 | 118 | return net_device; |
fceaf24a HJ |
119 | } |
120 | ||
545a8e79 | 121 | static void free_netvsc_device(struct rcu_head *head) |
f90251c8 | 122 | { |
545a8e79 | 123 | struct netvsc_device *nvdev |
124 | = container_of(head, struct netvsc_device, rcu); | |
c0b558e5 HZ |
125 | int i; |
126 | ||
592de81e SH |
127 | kfree(nvdev->extension); |
128 | vfree(nvdev->recv_buf); | |
129 | vfree(nvdev->send_buf); | |
130 | kfree(nvdev->send_section_map); | |
131 | ||
c0b558e5 | 132 | for (i = 0; i < VRSS_CHANNEL_MAX; i++) |
7426b1a5 | 133 | vfree(nvdev->chan_table[i].mrc.slots); |
c0b558e5 | 134 | |
f90251c8 HZ |
135 | kfree(nvdev); |
136 | } | |
137 | ||
545a8e79 | 138 | static void free_netvsc_device_rcu(struct netvsc_device *nvdev) |
139 | { | |
140 | call_rcu(&nvdev->rcu, free_netvsc_device); | |
141 | } | |
fceaf24a | 142 | |
caf13133 MG |
143 | static void netvsc_revoke_recv_buf(struct hv_device *device, |
144 | struct netvsc_device *net_device) | |
ec91cd09 | 145 | { |
3d541ac5 | 146 | struct net_device *ndev = hv_get_drvdata(device); |
caf13133 | 147 | struct nvsp_message *revoke_packet; |
7a2a0a84 | 148 | int ret; |
ec91cd09 HZ |
149 | |
150 | /* | |
151 | * If we got a section count, it means we received a | |
152 | * SendReceiveBufferComplete msg (ie sent | |
153 | * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need | |
154 | * to send a revoke msg here | |
155 | */ | |
156 | if (net_device->recv_section_cnt) { | |
157 | /* Send the revoke receive buffer */ | |
158 | revoke_packet = &net_device->revoke_packet; | |
159 | memset(revoke_packet, 0, sizeof(struct nvsp_message)); | |
160 | ||
161 | revoke_packet->hdr.msg_type = | |
162 | NVSP_MSG1_TYPE_REVOKE_RECV_BUF; | |
163 | revoke_packet->msg.v1_msg. | |
164 | revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; | |
165 | ||
3d541ac5 | 166 | ret = vmbus_sendpacket(device->channel, |
ec91cd09 HZ |
167 | revoke_packet, |
168 | sizeof(struct nvsp_message), | |
169 | (unsigned long)revoke_packet, | |
170 | VM_PKT_DATA_INBAND, 0); | |
73e64fa4 S |
171 | /* If the failure is because the channel is rescinded; |
172 | * ignore the failure since we cannot send on a rescinded | |
173 | * channel. This would allow us to properly cleanup | |
174 | * even when the channel is rescinded. | |
175 | */ | |
176 | if (device->channel->rescind) | |
177 | ret = 0; | |
ec91cd09 HZ |
178 | /* |
179 | * If we failed here, we might as well return and | |
180 | * have a leak rather than continue and a bugchk | |
181 | */ | |
182 | if (ret != 0) { | |
d9871158 | 183 | netdev_err(ndev, "unable to send " |
c909ebbd | 184 | "revoke receive buffer to netvsp\n"); |
7a2a0a84 | 185 | return; |
ec91cd09 | 186 | } |
8b532797 | 187 | net_device->recv_section_cnt = 0; |
ec91cd09 | 188 | } |
caf13133 MG |
189 | } |
190 | ||
191 | static void netvsc_revoke_send_buf(struct hv_device *device, | |
192 | struct netvsc_device *net_device) | |
193 | { | |
194 | struct net_device *ndev = hv_get_drvdata(device); | |
195 | struct nvsp_message *revoke_packet; | |
196 | int ret; | |
ec91cd09 | 197 | |
c25aaf81 KS |
198 | /* Deal with the send buffer we may have setup. |
199 | * If we got a send section size, it means we received a | |
c51ed182 HZ |
200 | * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent |
201 | * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need | |
c25aaf81 KS |
202 | * to send a revoke msg here |
203 | */ | |
8b532797 | 204 | if (net_device->send_section_cnt) { |
c25aaf81 KS |
205 | /* Send the revoke receive buffer */ |
206 | revoke_packet = &net_device->revoke_packet; | |
207 | memset(revoke_packet, 0, sizeof(struct nvsp_message)); | |
208 | ||
209 | revoke_packet->hdr.msg_type = | |
210 | NVSP_MSG1_TYPE_REVOKE_SEND_BUF; | |
c51ed182 HZ |
211 | revoke_packet->msg.v1_msg.revoke_send_buf.id = |
212 | NETVSC_SEND_BUFFER_ID; | |
c25aaf81 | 213 | |
3d541ac5 | 214 | ret = vmbus_sendpacket(device->channel, |
c25aaf81 KS |
215 | revoke_packet, |
216 | sizeof(struct nvsp_message), | |
217 | (unsigned long)revoke_packet, | |
218 | VM_PKT_DATA_INBAND, 0); | |
73e64fa4 S |
219 | |
220 | /* If the failure is because the channel is rescinded; | |
221 | * ignore the failure since we cannot send on a rescinded | |
222 | * channel. This would allow us to properly cleanup | |
223 | * even when the channel is rescinded. | |
224 | */ | |
225 | if (device->channel->rescind) | |
226 | ret = 0; | |
227 | ||
c25aaf81 KS |
228 | /* If we failed here, we might as well return and |
229 | * have a leak rather than continue and a bugchk | |
230 | */ | |
231 | if (ret != 0) { | |
232 | netdev_err(ndev, "unable to send " | |
233 | "revoke send buffer to netvsp\n"); | |
7a2a0a84 | 234 | return; |
c25aaf81 | 235 | } |
8b532797 | 236 | net_device->send_section_cnt = 0; |
c25aaf81 | 237 | } |
0cf73780 VK |
238 | } |
239 | ||
caf13133 MG |
240 | static void netvsc_teardown_recv_gpadl(struct hv_device *device, |
241 | struct netvsc_device *net_device) | |
0cf73780 VK |
242 | { |
243 | struct net_device *ndev = hv_get_drvdata(device); | |
244 | int ret; | |
245 | ||
246 | if (net_device->recv_buf_gpadl_handle) { | |
247 | ret = vmbus_teardown_gpadl(device->channel, | |
248 | net_device->recv_buf_gpadl_handle); | |
249 | ||
250 | /* If we failed here, we might as well return and have a leak | |
251 | * rather than continue and a bugchk | |
252 | */ | |
253 | if (ret != 0) { | |
254 | netdev_err(ndev, | |
255 | "unable to teardown receive buffer's gpadl\n"); | |
256 | return; | |
257 | } | |
258 | net_device->recv_buf_gpadl_handle = 0; | |
259 | } | |
caf13133 MG |
260 | } |
261 | ||
262 | static void netvsc_teardown_send_gpadl(struct hv_device *device, | |
263 | struct netvsc_device *net_device) | |
264 | { | |
265 | struct net_device *ndev = hv_get_drvdata(device); | |
266 | int ret; | |
0cf73780 | 267 | |
c25aaf81 | 268 | if (net_device->send_buf_gpadl_handle) { |
3d541ac5 | 269 | ret = vmbus_teardown_gpadl(device->channel, |
c25aaf81 KS |
270 | net_device->send_buf_gpadl_handle); |
271 | ||
272 | /* If we failed here, we might as well return and have a leak | |
273 | * rather than continue and a bugchk | |
274 | */ | |
275 | if (ret != 0) { | |
276 | netdev_err(ndev, | |
277 | "unable to teardown send buffer's gpadl\n"); | |
7a2a0a84 | 278 | return; |
c25aaf81 | 279 | } |
2f18423d | 280 | net_device->send_buf_gpadl_handle = 0; |
c25aaf81 | 281 | } |
ec91cd09 HZ |
282 | } |
283 | ||
7426b1a5 | 284 | int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx) |
285 | { | |
286 | struct netvsc_channel *nvchan = &net_device->chan_table[q_idx]; | |
287 | int node = cpu_to_node(nvchan->channel->target_cpu); | |
288 | size_t size; | |
289 | ||
290 | size = net_device->recv_completion_cnt * sizeof(struct recv_comp_data); | |
291 | nvchan->mrc.slots = vzalloc_node(size, node); | |
292 | if (!nvchan->mrc.slots) | |
293 | nvchan->mrc.slots = vzalloc(size); | |
294 | ||
295 | return nvchan->mrc.slots ? 0 : -ENOMEM; | |
296 | } | |
297 | ||
95790837 | 298 | static int netvsc_init_buf(struct hv_device *device, |
8b532797 | 299 | struct netvsc_device *net_device, |
300 | const struct netvsc_device_info *device_info) | |
fceaf24a | 301 | { |
7426b1a5 | 302 | struct nvsp_1_message_send_receive_buffer_complete *resp; |
95833370 | 303 | struct net_device *ndev = hv_get_drvdata(device); |
304 | struct nvsp_message *init_packet; | |
8b532797 | 305 | unsigned int buf_size; |
fdfb70d2 | 306 | size_t map_words; |
95833370 | 307 | int ret = 0; |
0a726c2b | 308 | |
8b532797 | 309 | /* Get receive buffer area. */ |
0ab09bef | 310 | buf_size = device_info->recv_sections * device_info->recv_section_size; |
8b532797 | 311 | buf_size = roundup(buf_size, PAGE_SIZE); |
312 | ||
e776be25 HZ |
313 | /* Legacy hosts only allow smaller receive buffer */ |
314 | if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2) | |
315 | buf_size = min_t(unsigned int, buf_size, | |
316 | NETVSC_RECEIVE_BUFFER_SIZE_LEGACY); | |
317 | ||
8b532797 | 318 | net_device->recv_buf = vzalloc(buf_size); |
53d21fdb | 319 | if (!net_device->recv_buf) { |
8b532797 | 320 | netdev_err(ndev, |
321 | "unable to allocate receive buffer of size %u\n", | |
322 | buf_size); | |
927bc33c | 323 | ret = -ENOMEM; |
0c3b7b2f | 324 | goto cleanup; |
fceaf24a | 325 | } |
fceaf24a | 326 | |
454f18a9 BP |
327 | /* |
328 | * Establish the gpadl handle for this buffer on this | |
329 | * channel. Note: This call uses the vmbus connection rather | |
330 | * than the channel to establish the gpadl handle. | |
331 | */ | |
53d21fdb | 332 | ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf, |
8b532797 | 333 | buf_size, |
53d21fdb | 334 | &net_device->recv_buf_gpadl_handle); |
21a80820 | 335 | if (ret != 0) { |
d9871158 | 336 | netdev_err(ndev, |
c909ebbd | 337 | "unable to establish receive buffer's gpadl\n"); |
0c3b7b2f | 338 | goto cleanup; |
fceaf24a HJ |
339 | } |
340 | ||
454f18a9 | 341 | /* Notify the NetVsp of the gpadl handle */ |
53d21fdb | 342 | init_packet = &net_device->channel_init_pkt; |
85799a37 | 343 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
53d21fdb HZ |
344 | init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF; |
345 | init_packet->msg.v1_msg.send_recv_buf. | |
346 | gpadl_handle = net_device->recv_buf_gpadl_handle; | |
347 | init_packet->msg.v1_msg. | |
348 | send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID; | |
fceaf24a | 349 | |
454f18a9 | 350 | /* Send the gpadl notification request */ |
85799a37 | 351 | ret = vmbus_sendpacket(device->channel, init_packet, |
5a4df290 | 352 | sizeof(struct nvsp_message), |
85799a37 | 353 | (unsigned long)init_packet, |
415f2287 | 354 | VM_PKT_DATA_INBAND, |
5a4df290 | 355 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
21a80820 | 356 | if (ret != 0) { |
d9871158 | 357 | netdev_err(ndev, |
c909ebbd | 358 | "unable to send receive buffer's gpadl to netvsp\n"); |
0c3b7b2f | 359 | goto cleanup; |
fceaf24a HJ |
360 | } |
361 | ||
5362855a | 362 | wait_for_completion(&net_device->channel_init_wait); |
fceaf24a | 363 | |
454f18a9 | 364 | /* Check the response */ |
7426b1a5 | 365 | resp = &init_packet->msg.v1_msg.send_recv_buf_complete; |
366 | if (resp->status != NVSP_STAT_SUCCESS) { | |
367 | netdev_err(ndev, | |
368 | "Unable to complete receive buffer initialization with NetVsp - status %d\n", | |
369 | resp->status); | |
927bc33c | 370 | ret = -EINVAL; |
0c3b7b2f | 371 | goto cleanup; |
fceaf24a HJ |
372 | } |
373 | ||
454f18a9 | 374 | /* Parse the response */ |
7426b1a5 | 375 | netdev_dbg(ndev, "Receive sections: %u sub_allocs: size %u count: %u\n", |
376 | resp->num_sections, resp->sections[0].sub_alloc_size, | |
377 | resp->sections[0].num_sub_allocs); | |
fceaf24a | 378 | |
8b532797 | 379 | /* There should only be one section for the entire receive buffer */ |
380 | if (resp->num_sections != 1 || resp->sections[0].offset != 0) { | |
927bc33c | 381 | ret = -EINVAL; |
0c3b7b2f | 382 | goto cleanup; |
fceaf24a HJ |
383 | } |
384 | ||
8b532797 | 385 | net_device->recv_section_size = resp->sections[0].sub_alloc_size; |
386 | net_device->recv_section_cnt = resp->sections[0].num_sub_allocs; | |
387 | ||
7426b1a5 | 388 | /* Setup receive completion ring */ |
389 | net_device->recv_completion_cnt | |
8b532797 | 390 | = round_up(net_device->recv_section_cnt + 1, |
7426b1a5 | 391 | PAGE_SIZE / sizeof(u64)); |
392 | ret = netvsc_alloc_recv_comp_ring(net_device, 0); | |
393 | if (ret) | |
394 | goto cleanup; | |
395 | ||
396 | /* Now setup the send buffer. */ | |
0ab09bef | 397 | buf_size = device_info->send_sections * device_info->send_section_size; |
8b532797 | 398 | buf_size = round_up(buf_size, PAGE_SIZE); |
399 | ||
400 | net_device->send_buf = vzalloc(buf_size); | |
c25aaf81 | 401 | if (!net_device->send_buf) { |
8b532797 | 402 | netdev_err(ndev, "unable to allocate send buffer of size %u\n", |
403 | buf_size); | |
c25aaf81 KS |
404 | ret = -ENOMEM; |
405 | goto cleanup; | |
406 | } | |
407 | ||
408 | /* Establish the gpadl handle for this buffer on this | |
409 | * channel. Note: This call uses the vmbus connection rather | |
410 | * than the channel to establish the gpadl handle. | |
411 | */ | |
412 | ret = vmbus_establish_gpadl(device->channel, net_device->send_buf, | |
8b532797 | 413 | buf_size, |
c25aaf81 KS |
414 | &net_device->send_buf_gpadl_handle); |
415 | if (ret != 0) { | |
416 | netdev_err(ndev, | |
417 | "unable to establish send buffer's gpadl\n"); | |
418 | goto cleanup; | |
419 | } | |
420 | ||
421 | /* Notify the NetVsp of the gpadl handle */ | |
422 | init_packet = &net_device->channel_init_pkt; | |
423 | memset(init_packet, 0, sizeof(struct nvsp_message)); | |
424 | init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF; | |
c51ed182 | 425 | init_packet->msg.v1_msg.send_send_buf.gpadl_handle = |
c25aaf81 | 426 | net_device->send_buf_gpadl_handle; |
c51ed182 | 427 | init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID; |
c25aaf81 KS |
428 | |
429 | /* Send the gpadl notification request */ | |
430 | ret = vmbus_sendpacket(device->channel, init_packet, | |
431 | sizeof(struct nvsp_message), | |
432 | (unsigned long)init_packet, | |
433 | VM_PKT_DATA_INBAND, | |
434 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
435 | if (ret != 0) { | |
436 | netdev_err(ndev, | |
437 | "unable to send send buffer's gpadl to netvsp\n"); | |
438 | goto cleanup; | |
439 | } | |
440 | ||
5362855a | 441 | wait_for_completion(&net_device->channel_init_wait); |
c25aaf81 KS |
442 | |
443 | /* Check the response */ | |
444 | if (init_packet->msg.v1_msg. | |
445 | send_send_buf_complete.status != NVSP_STAT_SUCCESS) { | |
446 | netdev_err(ndev, "Unable to complete send buffer " | |
447 | "initialization with NetVsp - status %d\n", | |
448 | init_packet->msg.v1_msg. | |
c51ed182 | 449 | send_send_buf_complete.status); |
c25aaf81 KS |
450 | ret = -EINVAL; |
451 | goto cleanup; | |
452 | } | |
453 | ||
454 | /* Parse the response */ | |
455 | net_device->send_section_size = init_packet->msg. | |
456 | v1_msg.send_send_buf_complete.section_size; | |
457 | ||
8b532797 | 458 | /* Section count is simply the size divided by the section size. */ |
459 | net_device->send_section_cnt = buf_size / net_device->send_section_size; | |
c25aaf81 | 460 | |
93ba2222 VK |
461 | netdev_dbg(ndev, "Send section size: %d, Section count:%d\n", |
462 | net_device->send_section_size, net_device->send_section_cnt); | |
c25aaf81 KS |
463 | |
464 | /* Setup state for managing the send buffer. */ | |
fdfb70d2 | 465 | map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG); |
c25aaf81 | 466 | |
fdfb70d2 | 467 | net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL); |
dd1d3f8f WY |
468 | if (net_device->send_section_map == NULL) { |
469 | ret = -ENOMEM; | |
c25aaf81 | 470 | goto cleanup; |
dd1d3f8f | 471 | } |
c25aaf81 | 472 | |
0c3b7b2f | 473 | goto exit; |
fceaf24a | 474 | |
0c3b7b2f | 475 | cleanup: |
caf13133 MG |
476 | netvsc_revoke_recv_buf(device, net_device); |
477 | netvsc_revoke_send_buf(device, net_device); | |
478 | netvsc_teardown_recv_gpadl(device, net_device); | |
479 | netvsc_teardown_send_gpadl(device, net_device); | |
fceaf24a | 480 | |
0c3b7b2f | 481 | exit: |
fceaf24a HJ |
482 | return ret; |
483 | } | |
484 | ||
f157e78d HZ |
485 | /* Negotiate NVSP protocol version */ |
486 | static int negotiate_nvsp_ver(struct hv_device *device, | |
487 | struct netvsc_device *net_device, | |
488 | struct nvsp_message *init_packet, | |
489 | u32 nvsp_ver) | |
fceaf24a | 490 | { |
0a1275ca | 491 | struct net_device *ndev = hv_get_drvdata(device); |
7390fe9c | 492 | int ret; |
fceaf24a | 493 | |
85799a37 | 494 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
53d21fdb | 495 | init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT; |
f157e78d HZ |
496 | init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver; |
497 | init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver; | |
fceaf24a | 498 | |
454f18a9 | 499 | /* Send the init request */ |
85799a37 | 500 | ret = vmbus_sendpacket(device->channel, init_packet, |
5a4df290 | 501 | sizeof(struct nvsp_message), |
85799a37 | 502 | (unsigned long)init_packet, |
415f2287 | 503 | VM_PKT_DATA_INBAND, |
5a4df290 | 504 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
21a80820 | 505 | |
b8a3d52b | 506 | if (ret != 0) |
f157e78d | 507 | return ret; |
fceaf24a | 508 | |
5362855a | 509 | wait_for_completion(&net_device->channel_init_wait); |
fceaf24a | 510 | |
53d21fdb | 511 | if (init_packet->msg.init_msg.init_complete.status != |
f157e78d HZ |
512 | NVSP_STAT_SUCCESS) |
513 | return -EINVAL; | |
fceaf24a | 514 | |
a1eabb01 | 515 | if (nvsp_ver == NVSP_PROTOCOL_VERSION_1) |
f157e78d HZ |
516 | return 0; |
517 | ||
71790a27 | 518 | /* NVSPv2 or later: Send NDIS config */ |
f157e78d HZ |
519 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
520 | init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG; | |
0a1275ca | 521 | init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN; |
1f5f3a75 | 522 | init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1; |
f157e78d | 523 | |
7f5d5af0 | 524 | if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) { |
71790a27 HZ |
525 | init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1; |
526 | ||
7f5d5af0 HZ |
527 | /* Teaming bit is needed to receive link speed updates */ |
528 | init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1; | |
529 | } | |
530 | ||
f157e78d HZ |
531 | ret = vmbus_sendpacket(device->channel, init_packet, |
532 | sizeof(struct nvsp_message), | |
533 | (unsigned long)init_packet, | |
534 | VM_PKT_DATA_INBAND, 0); | |
535 | ||
536 | return ret; | |
537 | } | |
538 | ||
95790837 | 539 | static int netvsc_connect_vsp(struct hv_device *device, |
8b532797 | 540 | struct netvsc_device *net_device, |
541 | const struct netvsc_device_info *device_info) | |
f157e78d | 542 | { |
1b17ca04 | 543 | static const u32 ver_list[] = { |
e5a78fad | 544 | NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2, |
95790837 | 545 | NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 |
546 | }; | |
547 | struct nvsp_message *init_packet; | |
548 | int ndis_version, i, ret; | |
f157e78d HZ |
549 | |
550 | init_packet = &net_device->channel_init_pkt; | |
551 | ||
552 | /* Negotiate the latest NVSP protocol supported */ | |
e5a78fad | 553 | for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--) |
a1eabb01 HZ |
554 | if (negotiate_nvsp_ver(device, net_device, init_packet, |
555 | ver_list[i]) == 0) { | |
556 | net_device->nvsp_version = ver_list[i]; | |
557 | break; | |
558 | } | |
559 | ||
560 | if (i < 0) { | |
0f48c72c | 561 | ret = -EPROTO; |
0c3b7b2f | 562 | goto cleanup; |
fceaf24a | 563 | } |
f157e78d HZ |
564 | |
565 | pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version); | |
566 | ||
454f18a9 | 567 | /* Send the ndis version */ |
85799a37 | 568 | memset(init_packet, 0, sizeof(struct nvsp_message)); |
fceaf24a | 569 | |
a1eabb01 | 570 | if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4) |
1f73db49 | 571 | ndis_version = 0x00060001; |
a1eabb01 HZ |
572 | else |
573 | ndis_version = 0x0006001e; | |
fceaf24a | 574 | |
53d21fdb HZ |
575 | init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER; |
576 | init_packet->msg.v1_msg. | |
577 | send_ndis_ver.ndis_major_ver = | |
85799a37 | 578 | (ndis_version & 0xFFFF0000) >> 16; |
53d21fdb HZ |
579 | init_packet->msg.v1_msg. |
580 | send_ndis_ver.ndis_minor_ver = | |
85799a37 | 581 | ndis_version & 0xFFFF; |
fceaf24a | 582 | |
454f18a9 | 583 | /* Send the init request */ |
85799a37 | 584 | ret = vmbus_sendpacket(device->channel, init_packet, |
0c3b7b2f S |
585 | sizeof(struct nvsp_message), |
586 | (unsigned long)init_packet, | |
587 | VM_PKT_DATA_INBAND, 0); | |
0f48c72c | 588 | if (ret != 0) |
0c3b7b2f | 589 | goto cleanup; |
454f18a9 | 590 | |
99d3016d | 591 | |
8b532797 | 592 | ret = netvsc_init_buf(device, net_device, device_info); |
fceaf24a | 593 | |
0c3b7b2f | 594 | cleanup: |
fceaf24a HJ |
595 | return ret; |
596 | } | |
597 | ||
3e189519 | 598 | /* |
5a71ae30 | 599 | * netvsc_device_remove - Callback when the root bus device is removed |
21a80820 | 600 | */ |
e08f3ea5 | 601 | void netvsc_device_remove(struct hv_device *device) |
fceaf24a | 602 | { |
3d541ac5 VK |
603 | struct net_device *ndev = hv_get_drvdata(device); |
604 | struct net_device_context *net_device_ctx = netdev_priv(ndev); | |
79e8cbe7 | 605 | struct netvsc_device *net_device |
606 | = rtnl_dereference(net_device_ctx->nvdev); | |
15a863bf | 607 | int i; |
fceaf24a | 608 | |
db2b6498 MG |
609 | /* |
610 | * Revoke receive buffer. If host is pre-Win2016 then tear down | |
611 | * receive buffer GPADL. Do the same for send buffer. | |
612 | */ | |
caf13133 | 613 | netvsc_revoke_recv_buf(device, net_device); |
db2b6498 MG |
614 | if (vmbus_proto_version < VERSION_WIN10) |
615 | netvsc_teardown_recv_gpadl(device, net_device); | |
616 | ||
caf13133 | 617 | netvsc_revoke_send_buf(device, net_device); |
db2b6498 MG |
618 | if (vmbus_proto_version < VERSION_WIN10) |
619 | netvsc_teardown_send_gpadl(device, net_device); | |
9d88f33a | 620 | |
545a8e79 | 621 | RCU_INIT_POINTER(net_device_ctx->nvdev, NULL); |
3852409b | 622 | |
c0ddcaa8 SH |
623 | /* And disassociate NAPI context from device */ |
624 | for (i = 0; i < net_device->num_chn; i++) | |
625 | netif_napi_del(&net_device->chan_table[i].napi); | |
626 | ||
86c921af S |
627 | /* |
628 | * At this point, no one should be accessing net_device | |
629 | * except in here | |
630 | */ | |
93ba2222 | 631 | netdev_dbg(ndev, "net device safe to remove\n"); |
fceaf24a | 632 | |
454f18a9 | 633 | /* Now, we can close the channel safely */ |
85799a37 | 634 | vmbus_close(device->channel); |
fceaf24a | 635 | |
db2b6498 MG |
636 | /* |
637 | * If host is Win2016 or higher then we do the GPADL tear down | |
638 | * here after VMBus is closed. | |
639 | */ | |
caf13133 MG |
640 | if (vmbus_proto_version >= VERSION_WIN10) { |
641 | netvsc_teardown_recv_gpadl(device, net_device); | |
642 | netvsc_teardown_send_gpadl(device, net_device); | |
643 | } | |
0cf73780 | 644 | |
454f18a9 | 645 | /* Release all resources */ |
545a8e79 | 646 | free_netvsc_device_rcu(net_device); |
fceaf24a HJ |
647 | } |
648 | ||
33be96e4 HZ |
649 | #define RING_AVAIL_PERCENT_HIWATER 20 |
650 | #define RING_AVAIL_PERCENT_LOWATER 10 | |
651 | ||
652 | /* | |
653 | * Get the percentage of available bytes to write in the ring. | |
654 | * The return value is in range from 0 to 100. | |
655 | */ | |
656 | static inline u32 hv_ringbuf_avail_percent( | |
657 | struct hv_ring_buffer_info *ring_info) | |
658 | { | |
659 | u32 avail_read, avail_write; | |
660 | ||
661 | hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write); | |
662 | ||
663 | return avail_write * 100 / ring_info->ring_datasize; | |
664 | } | |
665 | ||
c25aaf81 KS |
666 | static inline void netvsc_free_send_slot(struct netvsc_device *net_device, |
667 | u32 index) | |
668 | { | |
669 | sync_change_bit(index, net_device->send_section_map); | |
670 | } | |
671 | ||
bc304dd3 SH |
672 | static void netvsc_send_tx_complete(struct netvsc_device *net_device, |
673 | struct vmbus_channel *incoming_channel, | |
674 | struct hv_device *device, | |
f9645430 | 675 | const struct vmpacket_descriptor *desc, |
676 | int budget) | |
bc304dd3 | 677 | { |
50698d80 | 678 | struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id; |
bc304dd3 | 679 | struct net_device *ndev = hv_get_drvdata(device); |
09af87d1 | 680 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
bc304dd3 | 681 | struct vmbus_channel *channel = device->channel; |
bc304dd3 SH |
682 | u16 q_idx = 0; |
683 | int queue_sends; | |
684 | ||
685 | /* Notify the layer above us */ | |
686 | if (likely(skb)) { | |
793e3955 | 687 | const struct hv_netvsc_packet *packet |
bc304dd3 | 688 | = (struct hv_netvsc_packet *)skb->cb; |
793e3955 | 689 | u32 send_index = packet->send_buf_index; |
690 | struct netvsc_stats *tx_stats; | |
bc304dd3 SH |
691 | |
692 | if (send_index != NETVSC_INVALID_INDEX) | |
693 | netvsc_free_send_slot(net_device, send_index); | |
793e3955 | 694 | q_idx = packet->q_idx; |
bc304dd3 SH |
695 | channel = incoming_channel; |
696 | ||
6c80f3fc | 697 | tx_stats = &net_device->chan_table[q_idx].tx_stats; |
793e3955 | 698 | |
699 | u64_stats_update_begin(&tx_stats->syncp); | |
700 | tx_stats->packets += packet->total_packets; | |
701 | tx_stats->bytes += packet->total_bytes; | |
702 | u64_stats_update_end(&tx_stats->syncp); | |
703 | ||
f9645430 | 704 | napi_consume_skb(skb, budget); |
bc304dd3 SH |
705 | } |
706 | ||
b8b835a8 | 707 | queue_sends = |
708 | atomic_dec_return(&net_device->chan_table[q_idx].queue_sends); | |
bc304dd3 | 709 | |
cef03981 SH |
710 | if (unlikely(net_device->destroy)) { |
711 | if (queue_sends == 0) | |
712 | wake_up(&net_device->wait_drain); | |
713 | } else { | |
714 | struct netdev_queue *txq = netdev_get_tx_queue(ndev, q_idx); | |
715 | ||
716 | if (netif_tx_queue_stopped(txq) && | |
717 | (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER || | |
718 | queue_sends < 1)) { | |
719 | netif_tx_wake_queue(txq); | |
720 | ndev_ctx->eth_stats.wake_queue++; | |
721 | } | |
09af87d1 | 722 | } |
bc304dd3 SH |
723 | } |
724 | ||
97c1723a | 725 | static void netvsc_send_completion(struct netvsc_device *net_device, |
25b85ee8 | 726 | struct vmbus_channel *incoming_channel, |
97c1723a | 727 | struct hv_device *device, |
f9645430 | 728 | const struct vmpacket_descriptor *desc, |
729 | int budget) | |
fceaf24a | 730 | { |
f3dd3f47 | 731 | struct nvsp_message *nvsp_packet = hv_pkt_data(desc); |
3d541ac5 | 732 | struct net_device *ndev = hv_get_drvdata(device); |
fceaf24a | 733 | |
bc304dd3 SH |
734 | switch (nvsp_packet->hdr.msg_type) { |
735 | case NVSP_MSG_TYPE_INIT_COMPLETE: | |
736 | case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE: | |
737 | case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE: | |
738 | case NVSP_MSG5_TYPE_SUBCHANNEL: | |
454f18a9 | 739 | /* Copy the response back */ |
53d21fdb | 740 | memcpy(&net_device->channel_init_pkt, nvsp_packet, |
21a80820 | 741 | sizeof(struct nvsp_message)); |
35abb21a | 742 | complete(&net_device->channel_init_wait); |
bc304dd3 SH |
743 | break; |
744 | ||
745 | case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE: | |
746 | netvsc_send_tx_complete(net_device, incoming_channel, | |
f9645430 | 747 | device, desc, budget); |
bc304dd3 | 748 | break; |
fceaf24a | 749 | |
bc304dd3 SH |
750 | default: |
751 | netdev_err(ndev, | |
752 | "Unknown send completion type %d received!!\n", | |
753 | nvsp_packet->hdr.msg_type); | |
fceaf24a | 754 | } |
fceaf24a HJ |
755 | } |
756 | ||
c25aaf81 KS |
757 | static u32 netvsc_get_next_send_section(struct netvsc_device *net_device) |
758 | { | |
b58a1858 | 759 | unsigned long *map_addr = net_device->send_section_map; |
760 | unsigned int i; | |
761 | ||
fdfb70d2 | 762 | for_each_clear_bit(i, map_addr, net_device->send_section_cnt) { |
b58a1858 | 763 | if (sync_test_and_set_bit(i, map_addr) == 0) |
764 | return i; | |
c25aaf81 | 765 | } |
b58a1858 | 766 | |
767 | return NETVSC_INVALID_INDEX; | |
c25aaf81 KS |
768 | } |
769 | ||
706b71d5 SH |
770 | static void netvsc_copy_to_send_buf(struct netvsc_device *net_device, |
771 | unsigned int section_index, | |
772 | u32 pend_size, | |
773 | struct hv_netvsc_packet *packet, | |
774 | struct rndis_message *rndis_msg, | |
775 | struct hv_page_buffer *pb, | |
776 | bool xmit_more) | |
c25aaf81 KS |
777 | { |
778 | char *start = net_device->send_buf; | |
7c3877f2 HZ |
779 | char *dest = start + (section_index * net_device->send_section_size) |
780 | + pend_size; | |
c25aaf81 KS |
781 | int i; |
782 | u32 msg_size = 0; | |
7c3877f2 HZ |
783 | u32 padding = 0; |
784 | u32 remain = packet->total_data_buflen % net_device->pkt_align; | |
aa0a34be HZ |
785 | u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt : |
786 | packet->page_buf_cnt; | |
7c3877f2 HZ |
787 | |
788 | /* Add padding */ | |
706b71d5 SH |
789 | remain = packet->total_data_buflen & (net_device->pkt_align - 1); |
790 | if (xmit_more && remain) { | |
7c3877f2 | 791 | padding = net_device->pkt_align - remain; |
24476760 | 792 | rndis_msg->msg_len += padding; |
7c3877f2 HZ |
793 | packet->total_data_buflen += padding; |
794 | } | |
c25aaf81 | 795 | |
aa0a34be | 796 | for (i = 0; i < page_count; i++) { |
02b6de01 | 797 | char *src = phys_to_virt(pb[i].pfn << PAGE_SHIFT); |
798 | u32 offset = pb[i].offset; | |
799 | u32 len = pb[i].len; | |
c25aaf81 KS |
800 | |
801 | memcpy(dest, (src + offset), len); | |
802 | msg_size += len; | |
803 | dest += len; | |
804 | } | |
7c3877f2 HZ |
805 | |
806 | if (padding) { | |
807 | memset(dest, 0, padding); | |
808 | msg_size += padding; | |
809 | } | |
c25aaf81 KS |
810 | } |
811 | ||
3a8963ac | 812 | static inline int netvsc_send_pkt( |
0a1275ca | 813 | struct hv_device *device, |
7c3877f2 | 814 | struct hv_netvsc_packet *packet, |
a9f2e2d6 | 815 | struct netvsc_device *net_device, |
02b6de01 | 816 | struct hv_page_buffer *pb, |
3a3d9a0a | 817 | struct sk_buff *skb) |
fceaf24a | 818 | { |
7c3877f2 | 819 | struct nvsp_message nvmsg; |
956a25c9 JP |
820 | struct nvsp_1_message_send_rndis_packet * const rpkt = |
821 | &nvmsg.msg.v1_msg.send_rndis_pkt; | |
822 | struct netvsc_channel * const nvchan = | |
823 | &net_device->chan_table[packet->q_idx]; | |
b8b835a8 | 824 | struct vmbus_channel *out_channel = nvchan->channel; |
0a1275ca | 825 | struct net_device *ndev = hv_get_drvdata(device); |
09af87d1 | 826 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
b8b835a8 | 827 | struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx); |
7c3877f2 HZ |
828 | u64 req_id; |
829 | int ret; | |
82fa3c77 | 830 | u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound); |
c25aaf81 | 831 | |
7c3877f2 | 832 | nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT; |
956a25c9 JP |
833 | if (skb) |
834 | rpkt->channel_type = 0; /* 0 is RMC_DATA */ | |
835 | else | |
836 | rpkt->channel_type = 1; /* 1 is RMC_CONTROL */ | |
fceaf24a | 837 | |
956a25c9 | 838 | rpkt->send_buf_section_index = packet->send_buf_index; |
7c3877f2 | 839 | if (packet->send_buf_index == NETVSC_INVALID_INDEX) |
956a25c9 | 840 | rpkt->send_buf_section_size = 0; |
7c3877f2 | 841 | else |
956a25c9 | 842 | rpkt->send_buf_section_size = packet->total_data_buflen; |
21a80820 | 843 | |
3a3d9a0a | 844 | req_id = (ulong)skb; |
f1ea3cd7 | 845 | |
c3582a2c HZ |
846 | if (out_channel->rescind) |
847 | return -ENODEV; | |
848 | ||
72a2f5bd | 849 | if (packet->page_buf_cnt) { |
02b6de01 | 850 | if (packet->cp_partial) |
851 | pb += packet->rmsg_pgcnt; | |
852 | ||
5a668d8c | 853 | ret = vmbus_sendpacket_pagebuffer(out_channel, |
854 | pb, packet->page_buf_cnt, | |
855 | &nvmsg, sizeof(nvmsg), | |
856 | req_id); | |
21a80820 | 857 | } else { |
5dd0fb9b | 858 | ret = vmbus_sendpacket(out_channel, |
859 | &nvmsg, sizeof(nvmsg), | |
860 | req_id, VM_PKT_DATA_INBAND, | |
861 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
fceaf24a HJ |
862 | } |
863 | ||
1d06825b | 864 | if (ret == 0) { |
b8b835a8 | 865 | atomic_inc_return(&nvchan->queue_sends); |
5b54dac8 | 866 | |
09af87d1 | 867 | if (ring_avail < RING_AVAIL_PERCENT_LOWATER) { |
b8b835a8 | 868 | netif_tx_stop_queue(txq); |
09af87d1 SX |
869 | ndev_ctx->eth_stats.stop_queue++; |
870 | } | |
1d06825b | 871 | } else if (ret == -EAGAIN) { |
b8b835a8 | 872 | netif_tx_stop_queue(txq); |
09af87d1 | 873 | ndev_ctx->eth_stats.stop_queue++; |
b8b835a8 | 874 | if (atomic_read(&nvchan->queue_sends) < 1) { |
875 | netif_tx_wake_queue(txq); | |
09af87d1 | 876 | ndev_ctx->eth_stats.wake_queue++; |
33be96e4 HZ |
877 | ret = -ENOSPC; |
878 | } | |
1d06825b | 879 | } else { |
4a2176c6 | 880 | netdev_err(ndev, |
881 | "Unable to send packet pages %u len %u, ret %d\n", | |
882 | packet->page_buf_cnt, packet->total_data_buflen, | |
883 | ret); | |
1d06825b | 884 | } |
fceaf24a | 885 | |
7c3877f2 HZ |
886 | return ret; |
887 | } | |
888 | ||
c85e4924 HZ |
889 | /* Move packet out of multi send data (msd), and clear msd */ |
890 | static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send, | |
891 | struct sk_buff **msd_skb, | |
892 | struct multi_send_data *msdp) | |
893 | { | |
894 | *msd_skb = msdp->skb; | |
895 | *msd_send = msdp->pkt; | |
896 | msdp->skb = NULL; | |
897 | msdp->pkt = NULL; | |
898 | msdp->count = 0; | |
899 | } | |
900 | ||
2a926f79 | 901 | /* RCU already held by caller */ |
706b71d5 | 902 | int netvsc_send(struct net_device *ndev, |
24476760 | 903 | struct hv_netvsc_packet *packet, |
a9f2e2d6 | 904 | struct rndis_message *rndis_msg, |
02b6de01 | 905 | struct hv_page_buffer *pb, |
3a3d9a0a | 906 | struct sk_buff *skb) |
7c3877f2 | 907 | { |
706b71d5 | 908 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
3962981f | 909 | struct netvsc_device *net_device |
867047c4 | 910 | = rcu_dereference_bh(ndev_ctx->nvdev); |
2a926f79 | 911 | struct hv_device *device = ndev_ctx->device_ctx; |
6c4c137e | 912 | int ret = 0; |
b8b835a8 | 913 | struct netvsc_channel *nvchan; |
7c3877f2 HZ |
914 | u32 pktlen = packet->total_data_buflen, msd_len = 0; |
915 | unsigned int section_index = NETVSC_INVALID_INDEX; | |
7c3877f2 HZ |
916 | struct multi_send_data *msdp; |
917 | struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL; | |
c85e4924 | 918 | struct sk_buff *msd_skb = NULL; |
706b71d5 | 919 | bool try_batch, xmit_more; |
7c3877f2 | 920 | |
592b4fe8 | 921 | /* If device is rescinded, return error and packet will get dropped. */ |
2a926f79 | 922 | if (unlikely(!net_device || net_device->destroy)) |
7c3877f2 HZ |
923 | return -ENODEV; |
924 | ||
b8b835a8 | 925 | nvchan = &net_device->chan_table[packet->q_idx]; |
7c3877f2 | 926 | packet->send_buf_index = NETVSC_INVALID_INDEX; |
aa0a34be | 927 | packet->cp_partial = false; |
7c3877f2 | 928 | |
cf8190e4 HZ |
929 | /* Send control message directly without accessing msd (Multi-Send |
930 | * Data) field which may be changed during data packet processing. | |
931 | */ | |
4d1060fe SH |
932 | if (!skb) |
933 | return netvsc_send_pkt(device, packet, net_device, pb, skb); | |
cf8190e4 | 934 | |
7c3877f2 | 935 | /* batch packets in send buffer if possible */ |
b8b835a8 | 936 | msdp = &nvchan->msd; |
7c3877f2 HZ |
937 | if (msdp->pkt) |
938 | msd_len = msdp->pkt->total_data_buflen; | |
939 | ||
ebc1dcf6 | 940 | try_batch = msd_len > 0 && msdp->count < net_device->max_pkt; |
aa0a34be | 941 | if (try_batch && msd_len + pktlen + net_device->pkt_align < |
7c3877f2 HZ |
942 | net_device->send_section_size) { |
943 | section_index = msdp->pkt->send_buf_index; | |
944 | ||
aa0a34be HZ |
945 | } else if (try_batch && msd_len + packet->rmsg_size < |
946 | net_device->send_section_size) { | |
947 | section_index = msdp->pkt->send_buf_index; | |
948 | packet->cp_partial = true; | |
949 | ||
ebc1dcf6 | 950 | } else if (pktlen + net_device->pkt_align < |
7c3877f2 HZ |
951 | net_device->send_section_size) { |
952 | section_index = netvsc_get_next_send_section(net_device); | |
cad5c197 | 953 | if (unlikely(section_index == NETVSC_INVALID_INDEX)) { |
954 | ++ndev_ctx->eth_stats.tx_send_full; | |
955 | } else { | |
c85e4924 HZ |
956 | move_pkt_msd(&msd_send, &msd_skb, msdp); |
957 | msd_len = 0; | |
7c3877f2 HZ |
958 | } |
959 | } | |
960 | ||
706b71d5 SH |
961 | /* Keep aggregating only if stack says more data is coming |
962 | * and not doing mixed modes send and not flow blocked | |
963 | */ | |
964 | xmit_more = skb->xmit_more && | |
965 | !packet->cp_partial && | |
966 | !netif_xmit_stopped(netdev_get_tx_queue(ndev, packet->q_idx)); | |
967 | ||
7c3877f2 HZ |
968 | if (section_index != NETVSC_INVALID_INDEX) { |
969 | netvsc_copy_to_send_buf(net_device, | |
970 | section_index, msd_len, | |
706b71d5 | 971 | packet, rndis_msg, pb, xmit_more); |
b08cc791 | 972 | |
7c3877f2 | 973 | packet->send_buf_index = section_index; |
aa0a34be HZ |
974 | |
975 | if (packet->cp_partial) { | |
976 | packet->page_buf_cnt -= packet->rmsg_pgcnt; | |
977 | packet->total_data_buflen = msd_len + packet->rmsg_size; | |
978 | } else { | |
979 | packet->page_buf_cnt = 0; | |
980 | packet->total_data_buflen += msd_len; | |
aa0a34be | 981 | } |
7c3877f2 | 982 | |
793e3955 | 983 | if (msdp->pkt) { |
984 | packet->total_packets += msdp->pkt->total_packets; | |
985 | packet->total_bytes += msdp->pkt->total_bytes; | |
986 | } | |
987 | ||
c85e4924 | 988 | if (msdp->skb) |
17db4bce | 989 | dev_consume_skb_any(msdp->skb); |
ee90b812 | 990 | |
706b71d5 | 991 | if (xmit_more) { |
c85e4924 | 992 | msdp->skb = skb; |
7c3877f2 HZ |
993 | msdp->pkt = packet; |
994 | msdp->count++; | |
995 | } else { | |
996 | cur_send = packet; | |
c85e4924 | 997 | msdp->skb = NULL; |
7c3877f2 HZ |
998 | msdp->pkt = NULL; |
999 | msdp->count = 0; | |
1000 | } | |
1001 | } else { | |
c85e4924 | 1002 | move_pkt_msd(&msd_send, &msd_skb, msdp); |
7c3877f2 HZ |
1003 | cur_send = packet; |
1004 | } | |
1005 | ||
7c3877f2 | 1006 | if (msd_send) { |
6c4c137e SH |
1007 | int m_ret = netvsc_send_pkt(device, msd_send, net_device, |
1008 | NULL, msd_skb); | |
7c3877f2 HZ |
1009 | |
1010 | if (m_ret != 0) { | |
1011 | netvsc_free_send_slot(net_device, | |
1012 | msd_send->send_buf_index); | |
c85e4924 | 1013 | dev_kfree_skb_any(msd_skb); |
7c3877f2 HZ |
1014 | } |
1015 | } | |
1016 | ||
1017 | if (cur_send) | |
0a1275ca | 1018 | ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb); |
7c3877f2 | 1019 | |
7aab5159 JS |
1020 | if (ret != 0 && section_index != NETVSC_INVALID_INDEX) |
1021 | netvsc_free_send_slot(net_device, section_index); | |
d953ca4d | 1022 | |
fceaf24a HJ |
1023 | return ret; |
1024 | } | |
1025 | ||
7426b1a5 | 1026 | /* Send pending recv completions */ |
cad5c197 | 1027 | static int send_recv_completions(struct net_device *ndev, |
1028 | struct netvsc_device *nvdev, | |
1029 | struct netvsc_channel *nvchan) | |
5fa9d3c5 | 1030 | { |
7426b1a5 | 1031 | struct multi_recv_comp *mrc = &nvchan->mrc; |
1032 | struct recv_comp_msg { | |
1033 | struct nvsp_message_header hdr; | |
1034 | u32 status; | |
1035 | } __packed; | |
1036 | struct recv_comp_msg msg = { | |
1037 | .hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE, | |
1038 | }; | |
5fa9d3c5 HZ |
1039 | int ret; |
1040 | ||
7426b1a5 | 1041 | while (mrc->first != mrc->next) { |
1042 | const struct recv_comp_data *rcd | |
1043 | = mrc->slots + mrc->first; | |
c0b558e5 | 1044 | |
7426b1a5 | 1045 | msg.status = rcd->status; |
1046 | ret = vmbus_sendpacket(nvchan->channel, &msg, sizeof(msg), | |
1047 | rcd->tid, VM_PKT_COMP, 0); | |
cad5c197 | 1048 | if (unlikely(ret)) { |
1049 | struct net_device_context *ndev_ctx = netdev_priv(ndev); | |
1050 | ||
1051 | ++ndev_ctx->eth_stats.rx_comp_busy; | |
7426b1a5 | 1052 | return ret; |
cad5c197 | 1053 | } |
c0b558e5 | 1054 | |
7426b1a5 | 1055 | if (++mrc->first == nvdev->recv_completion_cnt) |
1056 | mrc->first = 0; | |
1057 | } | |
c0b558e5 | 1058 | |
7426b1a5 | 1059 | /* receive completion ring has been emptied */ |
1060 | if (unlikely(nvdev->destroy)) | |
1061 | wake_up(&nvdev->wait_drain); | |
c0b558e5 | 1062 | |
7426b1a5 | 1063 | return 0; |
c0b558e5 HZ |
1064 | } |
1065 | ||
7426b1a5 | 1066 | /* Count how many receive completions are outstanding */ |
1067 | static void recv_comp_slot_avail(const struct netvsc_device *nvdev, | |
1068 | const struct multi_recv_comp *mrc, | |
1069 | u32 *filled, u32 *avail) | |
c0b558e5 | 1070 | { |
7426b1a5 | 1071 | u32 count = nvdev->recv_completion_cnt; |
c0b558e5 | 1072 | |
7426b1a5 | 1073 | if (mrc->next >= mrc->first) |
1074 | *filled = mrc->next - mrc->first; | |
1075 | else | |
1076 | *filled = (count - mrc->first) + mrc->next; | |
c0b558e5 | 1077 | |
7426b1a5 | 1078 | *avail = count - *filled - 1; |
c0b558e5 HZ |
1079 | } |
1080 | ||
7426b1a5 | 1081 | /* Add receive complete to ring to send to host. */ |
1082 | static void enq_receive_complete(struct net_device *ndev, | |
1083 | struct netvsc_device *nvdev, u16 q_idx, | |
1084 | u64 tid, u32 status) | |
c0b558e5 | 1085 | { |
7426b1a5 | 1086 | struct netvsc_channel *nvchan = &nvdev->chan_table[q_idx]; |
1087 | struct multi_recv_comp *mrc = &nvchan->mrc; | |
c0b558e5 | 1088 | struct recv_comp_data *rcd; |
7426b1a5 | 1089 | u32 filled, avail; |
c0b558e5 | 1090 | |
7426b1a5 | 1091 | recv_comp_slot_avail(nvdev, mrc, &filled, &avail); |
c0b558e5 | 1092 | |
7426b1a5 | 1093 | if (unlikely(filled > NAPI_POLL_WEIGHT)) { |
cad5c197 | 1094 | send_recv_completions(ndev, nvdev, nvchan); |
7426b1a5 | 1095 | recv_comp_slot_avail(nvdev, mrc, &filled, &avail); |
5fa9d3c5 | 1096 | } |
c0b558e5 | 1097 | |
7426b1a5 | 1098 | if (unlikely(!avail)) { |
1099 | netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n", | |
1100 | q_idx, tid); | |
1101 | return; | |
1102 | } | |
c0b558e5 | 1103 | |
7426b1a5 | 1104 | rcd = mrc->slots + mrc->next; |
1105 | rcd->tid = tid; | |
1106 | rcd->status = status; | |
c0b558e5 | 1107 | |
7426b1a5 | 1108 | if (++mrc->next == nvdev->recv_completion_cnt) |
1109 | mrc->next = 0; | |
c0b558e5 HZ |
1110 | } |
1111 | ||
15a863bf | 1112 | static int netvsc_receive(struct net_device *ndev, |
7426b1a5 | 1113 | struct netvsc_device *net_device, |
1114 | struct net_device_context *net_device_ctx, | |
1115 | struct hv_device *device, | |
1116 | struct vmbus_channel *channel, | |
1117 | const struct vmpacket_descriptor *desc, | |
1118 | struct nvsp_message *nvsp) | |
fceaf24a | 1119 | { |
f3dd3f47 | 1120 | const struct vmtransfer_page_packet_header *vmxferpage_packet |
1121 | = container_of(desc, const struct vmtransfer_page_packet_header, d); | |
15a863bf | 1122 | u16 q_idx = channel->offermsg.offer.sub_channel_index; |
dc54a08c | 1123 | char *recv_buf = net_device->recv_buf; |
4baab261 | 1124 | u32 status = NVSP_STAT_SUCCESS; |
45326342 HZ |
1125 | int i; |
1126 | int count = 0; | |
779b4d17 | 1127 | |
454f18a9 | 1128 | /* Make sure this is a valid nvsp packet */ |
dc54a08c | 1129 | if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) { |
1130 | netif_err(net_device_ctx, rx_err, ndev, | |
1131 | "Unknown nvsp packet type received %u\n", | |
1132 | nvsp->hdr.msg_type); | |
15a863bf | 1133 | return 0; |
fceaf24a HJ |
1134 | } |
1135 | ||
dc54a08c | 1136 | if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) { |
1137 | netif_err(net_device_ctx, rx_err, ndev, | |
1138 | "Invalid xfer page set id - expecting %x got %x\n", | |
1139 | NETVSC_RECEIVE_BUFFER_ID, | |
1140 | vmxferpage_packet->xfer_pageset_id); | |
15a863bf | 1141 | return 0; |
fceaf24a HJ |
1142 | } |
1143 | ||
4baab261 | 1144 | count = vmxferpage_packet->range_cnt; |
fceaf24a | 1145 | |
454f18a9 | 1146 | /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */ |
4baab261 | 1147 | for (i = 0; i < count; i++) { |
dc54a08c | 1148 | void *data = recv_buf |
1149 | + vmxferpage_packet->ranges[i].byte_offset; | |
1150 | u32 buflen = vmxferpage_packet->ranges[i].byte_count; | |
8139bb20 | 1151 | int ret; |
fceaf24a | 1152 | |
454f18a9 | 1153 | /* Pass it to the upper layer */ |
8139bb20 | 1154 | ret = rndis_filter_receive(ndev, net_device, device, |
dc54a08c | 1155 | channel, data, buflen); |
8139bb20 HZ |
1156 | |
1157 | if (unlikely(ret != NVSP_STAT_SUCCESS)) | |
1158 | status = NVSP_STAT_FAIL; | |
fceaf24a HJ |
1159 | } |
1160 | ||
7426b1a5 | 1161 | enq_receive_complete(ndev, net_device, q_idx, |
1162 | vmxferpage_packet->d.trans_id, status); | |
15a863bf | 1163 | |
15a863bf | 1164 | return count; |
fceaf24a HJ |
1165 | } |
1166 | ||
5b54dac8 | 1167 | static void netvsc_send_table(struct hv_device *hdev, |
71790a27 | 1168 | struct nvsp_message *nvmsg) |
5b54dac8 | 1169 | { |
0a1275ca | 1170 | struct net_device *ndev = hv_get_drvdata(hdev); |
7ce10124 | 1171 | struct net_device_context *net_device_ctx = netdev_priv(ndev); |
5b54dac8 HZ |
1172 | int i; |
1173 | u32 count, *tab; | |
1174 | ||
5b54dac8 HZ |
1175 | count = nvmsg->msg.v5_msg.send_table.count; |
1176 | if (count != VRSS_SEND_TAB_SIZE) { | |
1177 | netdev_err(ndev, "Received wrong send-table size:%u\n", count); | |
1178 | return; | |
1179 | } | |
1180 | ||
1181 | tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table + | |
1182 | nvmsg->msg.v5_msg.send_table.offset); | |
1183 | ||
1184 | for (i = 0; i < count; i++) | |
39e91cfb | 1185 | net_device_ctx->tx_table[i] = tab[i]; |
5b54dac8 HZ |
1186 | } |
1187 | ||
f9a7da91 | 1188 | static void netvsc_send_vf(struct net_device_context *net_device_ctx, |
71790a27 HZ |
1189 | struct nvsp_message *nvmsg) |
1190 | { | |
f9a7da91 VK |
1191 | net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated; |
1192 | net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial; | |
71790a27 HZ |
1193 | } |
1194 | ||
1195 | static inline void netvsc_receive_inband(struct hv_device *hdev, | |
f9a7da91 VK |
1196 | struct net_device_context *net_device_ctx, |
1197 | struct nvsp_message *nvmsg) | |
71790a27 HZ |
1198 | { |
1199 | switch (nvmsg->hdr.msg_type) { | |
1200 | case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE: | |
1201 | netvsc_send_table(hdev, nvmsg); | |
1202 | break; | |
1203 | ||
1204 | case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION: | |
f9a7da91 | 1205 | netvsc_send_vf(net_device_ctx, nvmsg); |
71790a27 HZ |
1206 | break; |
1207 | } | |
1208 | } | |
1209 | ||
15a863bf | 1210 | static int netvsc_process_raw_pkt(struct hv_device *device, |
1211 | struct vmbus_channel *channel, | |
1212 | struct netvsc_device *net_device, | |
1213 | struct net_device *ndev, | |
f9645430 | 1214 | const struct vmpacket_descriptor *desc, |
1215 | int budget) | |
99a50bb1 | 1216 | { |
f9a7da91 | 1217 | struct net_device_context *net_device_ctx = netdev_priv(ndev); |
f3dd3f47 | 1218 | struct nvsp_message *nvmsg = hv_pkt_data(desc); |
99a50bb1 S |
1219 | |
1220 | switch (desc->type) { | |
1221 | case VM_PKT_COMP: | |
f9645430 | 1222 | netvsc_send_completion(net_device, channel, device, |
1223 | desc, budget); | |
99a50bb1 S |
1224 | break; |
1225 | ||
1226 | case VM_PKT_DATA_USING_XFER_PAGES: | |
15a863bf | 1227 | return netvsc_receive(ndev, net_device, net_device_ctx, |
1228 | device, channel, desc, nvmsg); | |
99a50bb1 S |
1229 | break; |
1230 | ||
1231 | case VM_PKT_DATA_INBAND: | |
f9a7da91 | 1232 | netvsc_receive_inband(device, net_device_ctx, nvmsg); |
99a50bb1 S |
1233 | break; |
1234 | ||
1235 | default: | |
1236 | netdev_err(ndev, "unhandled packet type %d, tid %llx\n", | |
f4f1c23d | 1237 | desc->type, desc->trans_id); |
99a50bb1 S |
1238 | break; |
1239 | } | |
15a863bf | 1240 | |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel) | |
1245 | { | |
1246 | struct vmbus_channel *primary = channel->primary_channel; | |
1247 | ||
1248 | return primary ? primary->device_obj : channel->device_obj; | |
1249 | } | |
1250 | ||
262b7f14 | 1251 | /* Network processing softirq |
1252 | * Process data in incoming ring buffer from host | |
1253 | * Stops when ring is empty or budget is met or exceeded. | |
1254 | */ | |
15a863bf | 1255 | int netvsc_poll(struct napi_struct *napi, int budget) |
1256 | { | |
1257 | struct netvsc_channel *nvchan | |
1258 | = container_of(napi, struct netvsc_channel, napi); | |
35fbbccf | 1259 | struct netvsc_device *net_device = nvchan->net_device; |
15a863bf | 1260 | struct vmbus_channel *channel = nvchan->channel; |
1261 | struct hv_device *device = netvsc_channel_to_device(channel); | |
15a863bf | 1262 | struct net_device *ndev = hv_get_drvdata(device); |
15a863bf | 1263 | int work_done = 0; |
f3a13c57 | 1264 | int ret; |
15a863bf | 1265 | |
f4f1c23d | 1266 | /* If starting a new interval */ |
1267 | if (!nvchan->desc) | |
1268 | nvchan->desc = hv_pkt_iter_first(channel); | |
15a863bf | 1269 | |
f4f1c23d | 1270 | while (nvchan->desc && work_done < budget) { |
1271 | work_done += netvsc_process_raw_pkt(device, channel, net_device, | |
f9645430 | 1272 | ndev, nvchan->desc, budget); |
f4f1c23d | 1273 | nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc); |
15a863bf | 1274 | } |
15a863bf | 1275 | |
f3a13c57 HZ |
1276 | /* Send any pending receive completions */ |
1277 | ret = send_recv_completions(ndev, net_device, nvchan); | |
1278 | ||
1279 | /* If it did not exhaust NAPI budget this time | |
1280 | * and not doing busy poll | |
f4e40363 | 1281 | * then re-enable host interrupts |
f3a13c57 HZ |
1282 | * and reschedule if ring is not empty |
1283 | * or sending receive completion failed. | |
262b7f14 | 1284 | */ |
f3a13c57 | 1285 | if (work_done < budget && |
15a863bf | 1286 | napi_complete_done(napi, work_done) && |
f3a13c57 | 1287 | (ret || hv_end_read(&channel->inbound)) && |
1ae20708 | 1288 | napi_schedule_prep(napi)) { |
7426b1a5 | 1289 | hv_begin_read(&channel->inbound); |
1ae20708 | 1290 | __napi_schedule(napi); |
7426b1a5 | 1291 | } |
f4f1c23d | 1292 | |
1293 | /* Driver may overshoot since multiple packets per descriptor */ | |
1294 | return min(work_done, budget); | |
99a50bb1 S |
1295 | } |
1296 | ||
262b7f14 | 1297 | /* Call back when data is available in host ring buffer. |
1298 | * Processing is deferred until network softirq (NAPI) | |
1299 | */ | |
5b54dac8 | 1300 | void netvsc_channel_cb(void *context) |
fceaf24a | 1301 | { |
6de38af6 | 1302 | struct netvsc_channel *nvchan = context; |
43bf99ce | 1303 | struct vmbus_channel *channel = nvchan->channel; |
1304 | struct hv_ring_buffer_info *rbi = &channel->inbound; | |
1305 | ||
1306 | /* preload first vmpacket descriptor */ | |
1307 | prefetch(hv_get_ring_buffer(rbi) + rbi->priv_read_index); | |
0b307ebd | 1308 | |
f4f1c23d | 1309 | if (napi_schedule_prep(&nvchan->napi)) { |
1310 | /* disable interupts from host */ | |
43bf99ce | 1311 | hv_begin_read(rbi); |
0d6dd357 | 1312 | |
b067106f | 1313 | __napi_schedule_irqoff(&nvchan->napi); |
f4f1c23d | 1314 | } |
fceaf24a | 1315 | } |
af24ce42 | 1316 | |
b637e023 HZ |
1317 | /* |
1318 | * netvsc_device_add - Callback when the device belonging to this | |
1319 | * driver is added | |
1320 | */ | |
9749fed5 | 1321 | struct netvsc_device *netvsc_device_add(struct hv_device *device, |
1322 | const struct netvsc_device_info *device_info) | |
b637e023 | 1323 | { |
88098834 | 1324 | int i, ret = 0; |
2c7f83ca | 1325 | int ring_size = device_info->ring_size; |
b637e023 | 1326 | struct netvsc_device *net_device; |
88098834 VK |
1327 | struct net_device *ndev = hv_get_drvdata(device); |
1328 | struct net_device_context *net_device_ctx = netdev_priv(ndev); | |
b637e023 | 1329 | |
88098834 | 1330 | net_device = alloc_net_device(); |
b1c84927 | 1331 | if (!net_device) |
9749fed5 | 1332 | return ERR_PTR(-ENOMEM); |
b637e023 | 1333 | |
6b0cbe31 HZ |
1334 | for (i = 0; i < VRSS_SEND_TAB_SIZE; i++) |
1335 | net_device_ctx->tx_table[i] = 0; | |
1336 | ||
5b54dac8 HZ |
1337 | net_device->ring_size = ring_size; |
1338 | ||
15a863bf | 1339 | /* Because the device uses NAPI, all the interrupt batching and |
1340 | * control is done via Net softirq, not the channel handling | |
1341 | */ | |
1342 | set_channel_read_mode(device->channel, HV_CALL_ISR); | |
1343 | ||
bffb1842 S |
1344 | /* If we're reopening the device we may have multiple queues, fill the |
1345 | * chn_table with the default channel to use it before subchannels are | |
1346 | * opened. | |
1347 | * Initialize the channel state before we open; | |
1348 | * we can be interrupted as soon as we open the channel. | |
1349 | */ | |
1350 | ||
1351 | for (i = 0; i < VRSS_CHANNEL_MAX; i++) { | |
1352 | struct netvsc_channel *nvchan = &net_device->chan_table[i]; | |
1353 | ||
1354 | nvchan->channel = device->channel; | |
35fbbccf | 1355 | nvchan->net_device = net_device; |
4a0dee1f FF |
1356 | u64_stats_init(&nvchan->tx_stats.syncp); |
1357 | u64_stats_init(&nvchan->rx_stats.syncp); | |
bffb1842 S |
1358 | } |
1359 | ||
2be0f264 | 1360 | /* Enable NAPI handler before init callbacks */ |
1361 | netif_napi_add(ndev, &net_device->chan_table[0].napi, | |
1362 | netvsc_poll, NAPI_POLL_WEIGHT); | |
1363 | ||
b637e023 | 1364 | /* Open the channel */ |
aae23986 S |
1365 | ret = vmbus_open(device->channel, ring_size * PAGE_SIZE, |
1366 | ring_size * PAGE_SIZE, NULL, 0, | |
6de38af6 | 1367 | netvsc_channel_cb, |
1368 | net_device->chan_table); | |
b637e023 HZ |
1369 | |
1370 | if (ret != 0) { | |
d9871158 | 1371 | netdev_err(ndev, "unable to open channel: %d\n", ret); |
b637e023 HZ |
1372 | goto cleanup; |
1373 | } | |
1374 | ||
1375 | /* Channel is opened */ | |
93ba2222 | 1376 | netdev_dbg(ndev, "hv_netvsc channel opened successfully\n"); |
b637e023 | 1377 | |
15a863bf | 1378 | napi_enable(&net_device->chan_table[0].napi); |
88098834 | 1379 | |
b637e023 | 1380 | /* Connect with the NetVsp */ |
8b532797 | 1381 | ret = netvsc_connect_vsp(device, net_device, device_info); |
b637e023 | 1382 | if (ret != 0) { |
d9871158 | 1383 | netdev_err(ndev, |
c909ebbd | 1384 | "unable to connect to NetVSP - %d\n", ret); |
b637e023 HZ |
1385 | goto close; |
1386 | } | |
1387 | ||
4d1060fe SH |
1388 | /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is |
1389 | * populated. | |
1390 | */ | |
1391 | rcu_assign_pointer(net_device_ctx->nvdev, net_device); | |
1392 | ||
9749fed5 | 1393 | return net_device; |
b637e023 HZ |
1394 | |
1395 | close: | |
49393347 | 1396 | RCU_INIT_POINTER(net_device_ctx->nvdev, NULL); |
1397 | napi_disable(&net_device->chan_table[0].napi); | |
15a863bf | 1398 | |
b637e023 HZ |
1399 | /* Now, we can close the channel safely */ |
1400 | vmbus_close(device->channel); | |
1401 | ||
1402 | cleanup: | |
3702b029 | 1403 | netif_napi_del(&net_device->chan_table[0].napi); |
545a8e79 | 1404 | free_netvsc_device(&net_device->rcu); |
b637e023 | 1405 | |
9749fed5 | 1406 | return ERR_PTR(ret); |
b637e023 | 1407 | } |