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