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