~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/staging/hv/netvsc.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *   Haiyang Zhang <haiyangz@microsoft.com>
19
19
 *   Hank Janssen  <hjanssen@microsoft.com>
20
20
 */
 
21
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
22
 
21
23
#include <linux/kernel.h>
 
24
#include <linux/sched.h>
 
25
#include <linux/wait.h>
22
26
#include <linux/mm.h>
23
27
#include <linux/delay.h>
24
28
#include <linux/io.h>
25
29
#include <linux/slab.h>
26
 
#include "osd.h"
27
 
#include "logging.h"
28
 
#include "netvsc.h"
29
 
#include "rndis_filter.h"
30
 
#include "channel.h"
 
30
 
 
31
#include "hyperv.h"
 
32
#include "hyperv_net.h"
31
33
 
32
34
 
33
35
/* Globals */
41
43
        }
42
44
};
43
45
 
44
 
static int netvsc_device_add(struct hv_device *device, void *additional_info);
45
 
 
46
 
static int netvsc_device_remove(struct hv_device *device);
47
 
 
48
 
static void netvsc_cleanup(struct hv_driver *driver);
49
 
 
50
 
static void netvsc_channel_cb(void *context);
51
 
 
52
 
static int netvsc_init_send_buf(struct hv_device *device);
53
 
 
54
 
static int netvsc_init_recv_buf(struct hv_device *device);
55
 
 
56
 
static int netvsc_destroy_send_buf(struct netvsc_device *net_device);
57
 
 
58
 
static int netvsc_destroy_recv_buf(struct netvsc_device *net_device);
59
 
 
60
 
static int netvsc_connect_vsp(struct hv_device *device);
61
 
 
62
 
static void netvsc_send_completion(struct hv_device *device,
63
 
                                   struct vmpacket_descriptor *packet);
64
 
 
65
 
static int netvsc_send(struct hv_device *device,
66
 
                        struct hv_netvsc_packet *packet);
67
 
 
68
 
static void netvsc_receive(struct hv_device *device,
69
 
                            struct vmpacket_descriptor *packet);
70
 
 
71
 
static void netvsc_receive_completion(void *context);
72
 
 
73
 
static void netvsc_send_recv_completion(struct hv_device *device,
74
 
                                        u64 transaction_id);
75
 
 
76
46
 
77
47
static struct netvsc_device *alloc_net_device(struct hv_device *device)
78
48
{
86
56
        atomic_cmpxchg(&net_device->refcnt, 0, 2);
87
57
 
88
58
        net_device->dev = device;
89
 
        device->Extension = net_device;
 
59
        device->ext = net_device;
90
60
 
91
61
        return net_device;
92
62
}
93
63
 
94
64
static void free_net_device(struct netvsc_device *device)
95
65
{
96
 
        WARN_ON(atomic_read(&device->refcnt) == 0);
97
 
        device->dev->Extension = NULL;
 
66
        WARN_ON(atomic_read(&device->refcnt) != 0);
 
67
        device->dev->ext = NULL;
98
68
        kfree(device);
99
69
}
100
70
 
104
74
{
105
75
        struct netvsc_device *net_device;
106
76
 
107
 
        net_device = device->Extension;
 
77
        net_device = device->ext;
108
78
        if (net_device && atomic_read(&net_device->refcnt) > 1)
109
79
                atomic_inc(&net_device->refcnt);
110
80
        else
118
88
{
119
89
        struct netvsc_device *net_device;
120
90
 
121
 
        net_device = device->Extension;
 
91
        net_device = device->ext;
122
92
        if (net_device && atomic_read(&net_device->refcnt))
123
93
                atomic_inc(&net_device->refcnt);
124
94
        else
131
101
{
132
102
        struct netvsc_device *net_device;
133
103
 
134
 
        net_device = device->Extension;
135
 
        /* ASSERT(netDevice); */
 
104
        net_device = device->ext;
136
105
 
137
106
        atomic_dec(&net_device->refcnt);
138
107
}
142
111
{
143
112
        struct netvsc_device *net_device;
144
113
 
145
 
        net_device = device->Extension;
 
114
        net_device = device->ext;
146
115
        if (net_device == NULL)
147
116
                return NULL;
148
117
 
158
127
{
159
128
        struct netvsc_device *net_device;
160
129
 
161
 
        net_device = device->Extension;
 
130
        net_device = device->ext;
162
131
        if (net_device == NULL)
163
132
                return NULL;
164
133
 
166
135
        while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
167
136
                udelay(100);
168
137
 
169
 
        device->Extension = NULL;
 
138
        device->ext = NULL;
170
139
        return net_device;
171
140
}
172
141
 
173
 
/*
174
 
 * netvsc_initialize - Main entry point
175
 
 */
176
 
int netvsc_initialize(struct hv_driver *drv)
 
142
static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
177
143
{
178
 
        struct netvsc_driver *driver = (struct netvsc_driver *)drv;
179
 
 
180
 
        DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
181
 
                   "sizeof(struct nvsp_message)=%zd, "
182
 
                   "sizeof(struct vmtransfer_page_packet_header)=%zd",
183
 
                   sizeof(struct hv_netvsc_packet),
184
 
                   sizeof(struct nvsp_message),
185
 
                   sizeof(struct vmtransfer_page_packet_header));
186
 
 
187
 
        /* Make sure we are at least 2 pages since 1 page is used for control */
188
 
        /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
189
 
 
190
 
        drv->name = driver_name;
191
 
        memcpy(&drv->deviceType, &netvsc_device_type, sizeof(struct hv_guid));
192
 
 
193
 
        /* Make sure it is set by the caller */
194
 
        /* FIXME: These probably should still be tested in some way */
195
 
        /* ASSERT(driver->OnReceiveCallback); */
196
 
        /* ASSERT(driver->OnLinkStatusChanged); */
197
 
 
198
 
        /* Setup the dispatch table */
199
 
        driver->base.OnDeviceAdd        = netvsc_device_add;
200
 
        driver->base.OnDeviceRemove     = netvsc_device_remove;
201
 
        driver->base.OnCleanup          = netvsc_cleanup;
202
 
 
203
 
        driver->send                    = netvsc_send;
204
 
 
205
 
        rndis_filter_init(driver);
206
 
        return 0;
 
144
        struct nvsp_message *revoke_packet;
 
145
        int ret = 0;
 
146
 
 
147
        /*
 
148
         * If we got a section count, it means we received a
 
149
         * SendReceiveBufferComplete msg (ie sent
 
150
         * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
 
151
         * to send a revoke msg here
 
152
         */
 
153
        if (net_device->recv_section_cnt) {
 
154
                /* Send the revoke receive buffer */
 
155
                revoke_packet = &net_device->revoke_packet;
 
156
                memset(revoke_packet, 0, sizeof(struct nvsp_message));
 
157
 
 
158
                revoke_packet->hdr.msg_type =
 
159
                        NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
 
160
                revoke_packet->msg.v1_msg.
 
161
                revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
 
162
 
 
163
                ret = vmbus_sendpacket(net_device->dev->channel,
 
164
                                       revoke_packet,
 
165
                                       sizeof(struct nvsp_message),
 
166
                                       (unsigned long)revoke_packet,
 
167
                                       VM_PKT_DATA_INBAND, 0);
 
168
                /*
 
169
                 * If we failed here, we might as well return and
 
170
                 * have a leak rather than continue and a bugchk
 
171
                 */
 
172
                if (ret != 0) {
 
173
                        dev_err(&net_device->dev->device, "unable to send "
 
174
                                "revoke receive buffer to netvsp");
 
175
                        return -1;
 
176
                }
 
177
        }
 
178
 
 
179
        /* Teardown the gpadl on the vsp end */
 
180
        if (net_device->recv_buf_gpadl_handle) {
 
181
                ret = vmbus_teardown_gpadl(net_device->dev->channel,
 
182
                           net_device->recv_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
                        dev_err(&net_device->dev->device,
 
189
                                   "unable to teardown receive buffer's gpadl");
 
190
                        return -1;
 
191
                }
 
192
                net_device->recv_buf_gpadl_handle = 0;
 
193
        }
 
194
 
 
195
        if (net_device->recv_buf) {
 
196
                /* Free up the receive buffer */
 
197
                free_pages((unsigned long)net_device->recv_buf,
 
198
                        get_order(net_device->recv_buf_size));
 
199
                net_device->recv_buf = NULL;
 
200
        }
 
201
 
 
202
        if (net_device->recv_section) {
 
203
                net_device->recv_section_cnt = 0;
 
204
                kfree(net_device->recv_section);
 
205
                net_device->recv_section = NULL;
 
206
        }
 
207
 
 
208
        return ret;
207
209
}
208
210
 
209
211
static int netvsc_init_recv_buf(struct hv_device *device)
210
212
{
211
213
        int ret = 0;
 
214
        int t;
212
215
        struct netvsc_device *net_device;
213
216
        struct nvsp_message *init_packet;
214
217
 
215
218
        net_device = get_outbound_net_device(device);
216
219
        if (!net_device) {
217
 
                DPRINT_ERR(NETVSC, "unable to get net device..."
 
220
                dev_err(&device->device, "unable to get net device..."
218
221
                           "device being destroyed?");
219
222
                return -1;
220
223
        }
221
 
        /* ASSERT(netDevice->ReceiveBufferSize > 0); */
222
 
        /* page-size grandularity */
223
 
        /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
224
224
 
225
225
        net_device->recv_buf =
226
 
                osd_page_alloc(net_device->recv_buf_size >> PAGE_SHIFT);
 
226
                (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
 
227
                                get_order(net_device->recv_buf_size));
227
228
        if (!net_device->recv_buf) {
228
 
                DPRINT_ERR(NETVSC,
229
 
                           "unable to allocate receive buffer of size %d",
230
 
                           net_device->recv_buf_size);
 
229
                dev_err(&device->device, "unable to allocate receive "
 
230
                        "buffer of size %d", net_device->recv_buf_size);
231
231
                ret = -1;
232
 
                goto Cleanup;
 
232
                goto cleanup;
233
233
        }
234
 
        /* page-aligned buffer */
235
 
        /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
236
 
        /*      0); */
237
 
 
238
 
        DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
239
234
 
240
235
        /*
241
236
         * Establish the gpadl handle for this buffer on this
246
241
                                    net_device->recv_buf_size,
247
242
                                    &net_device->recv_buf_gpadl_handle);
248
243
        if (ret != 0) {
249
 
                DPRINT_ERR(NETVSC,
250
 
                           "unable to establish receive buffer's gpadl");
251
 
                goto Cleanup;
 
244
                dev_err(&device->device,
 
245
                        "unable to establish receive buffer's gpadl");
 
246
                goto cleanup;
252
247
        }
253
248
 
254
 
        /* osd_waitevent_wait(ext->ChannelInitEvent); */
255
249
 
256
250
        /* Notify the NetVsp of the gpadl handle */
257
 
        DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
258
 
 
259
251
        init_packet = &net_device->channel_init_pkt;
260
252
 
261
253
        memset(init_packet, 0, sizeof(struct nvsp_message));
270
262
        ret = vmbus_sendpacket(device->channel, init_packet,
271
263
                               sizeof(struct nvsp_message),
272
264
                               (unsigned long)init_packet,
273
 
                               VmbusPacketTypeDataInBand,
 
265
                               VM_PKT_DATA_INBAND,
274
266
                               VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
275
267
        if (ret != 0) {
276
 
                DPRINT_ERR(NETVSC,
277
 
                           "unable to send receive buffer's gpadl to netvsp");
278
 
                goto Cleanup;
 
268
                dev_err(&device->device,
 
269
                        "unable to send receive buffer's gpadl to netvsp");
 
270
                goto cleanup;
279
271
        }
280
272
 
281
 
        osd_waitevent_wait(net_device->channel_init_event);
 
273
        t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
 
274
        BUG_ON(t == 0);
 
275
 
282
276
 
283
277
        /* Check the response */
284
278
        if (init_packet->msg.v1_msg.
285
279
            send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
286
 
                DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
 
280
                dev_err(&device->device, "Unable to complete receive buffer "
287
281
                           "initialzation with NetVsp - status %d",
288
282
                           init_packet->msg.v1_msg.
289
283
                           send_recv_buf_complete.status);
290
284
                ret = -1;
291
 
                goto Cleanup;
 
285
                goto cleanup;
292
286
        }
293
287
 
294
288
        /* Parse the response */
295
 
        /* ASSERT(netDevice->ReceiveSectionCount == 0); */
296
 
        /* ASSERT(netDevice->ReceiveSections == NULL); */
297
289
 
298
290
        net_device->recv_section_cnt = init_packet->msg.
299
291
                v1_msg.send_recv_buf_complete.num_sections;
302
294
                * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
303
295
        if (net_device->recv_section == NULL) {
304
296
                ret = -1;
305
 
                goto Cleanup;
 
297
                goto cleanup;
306
298
        }
307
299
 
308
300
        memcpy(net_device->recv_section,
311
303
                net_device->recv_section_cnt *
312
304
               sizeof(struct nvsp_1_receive_buffer_section));
313
305
 
314
 
        DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
315
 
                    "endoffset %d, suballoc size %d, num suballocs %d)",
316
 
                    net_device->recv_section_cnt,
317
 
                    net_device->recv_section[0].offset,
318
 
                    net_device->recv_section[0].end_offset,
319
 
                    net_device->recv_section[0].sub_alloc_size,
320
 
                    net_device->recv_section[0].num_sub_allocs);
321
 
 
322
306
        /*
323
307
         * For 1st release, there should only be 1 section that represents the
324
308
         * entire receive buffer
326
310
        if (net_device->recv_section_cnt != 1 ||
327
311
            net_device->recv_section->offset != 0) {
328
312
                ret = -1;
329
 
                goto Cleanup;
 
313
                goto cleanup;
330
314
        }
331
315
 
332
 
        goto Exit;
 
316
        goto exit;
333
317
 
334
 
Cleanup:
 
318
cleanup:
335
319
        netvsc_destroy_recv_buf(net_device);
336
320
 
337
 
Exit:
 
321
exit:
338
322
        put_net_device(device);
339
323
        return ret;
340
324
}
341
325
 
 
326
static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
 
327
{
 
328
        struct nvsp_message *revoke_packet;
 
329
        int ret = 0;
 
330
 
 
331
        /*
 
332
         * If we got a section count, it means we received a
 
333
         *  SendReceiveBufferComplete msg (ie sent
 
334
         *  NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
 
335
         *  to send a revoke msg here
 
336
         */
 
337
        if (net_device->send_section_size) {
 
338
                /* Send the revoke send buffer */
 
339
                revoke_packet = &net_device->revoke_packet;
 
340
                memset(revoke_packet, 0, sizeof(struct nvsp_message));
 
341
 
 
342
                revoke_packet->hdr.msg_type =
 
343
                        NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
 
344
                revoke_packet->msg.v1_msg.
 
345
                        revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
 
346
 
 
347
                ret = vmbus_sendpacket(net_device->dev->channel,
 
348
                                       revoke_packet,
 
349
                                       sizeof(struct nvsp_message),
 
350
                                       (unsigned long)revoke_packet,
 
351
                                       VM_PKT_DATA_INBAND, 0);
 
352
                /*
 
353
                 * If we failed here, we might as well return and have a leak
 
354
                 * rather than continue and a bugchk
 
355
                 */
 
356
                if (ret != 0) {
 
357
                        dev_err(&net_device->dev->device, "unable to send "
 
358
                                "revoke send buffer to netvsp");
 
359
                        return -1;
 
360
                }
 
361
        }
 
362
 
 
363
        /* Teardown the gpadl on the vsp end */
 
364
        if (net_device->send_buf_gpadl_handle) {
 
365
                ret = vmbus_teardown_gpadl(net_device->dev->channel,
 
366
                                           net_device->send_buf_gpadl_handle);
 
367
 
 
368
                /*
 
369
                 * If we failed here, we might as well return and have a leak
 
370
                 * rather than continue and a bugchk
 
371
                 */
 
372
                if (ret != 0) {
 
373
                        dev_err(&net_device->dev->device,
 
374
                                "unable to teardown send buffer's gpadl");
 
375
                        return -1;
 
376
                }
 
377
                net_device->send_buf_gpadl_handle = 0;
 
378
        }
 
379
 
 
380
        if (net_device->send_buf) {
 
381
                /* Free up the receive buffer */
 
382
                free_pages((unsigned long)net_device->send_buf,
 
383
                                get_order(net_device->send_buf_size));
 
384
                net_device->send_buf = NULL;
 
385
        }
 
386
 
 
387
        return ret;
 
388
}
 
389
 
342
390
static int netvsc_init_send_buf(struct hv_device *device)
343
391
{
344
392
        int ret = 0;
 
393
        int t;
345
394
        struct netvsc_device *net_device;
346
395
        struct nvsp_message *init_packet;
347
396
 
348
397
        net_device = get_outbound_net_device(device);
349
398
        if (!net_device) {
350
 
                DPRINT_ERR(NETVSC, "unable to get net device..."
 
399
                dev_err(&device->device, "unable to get net device..."
351
400
                           "device being destroyed?");
352
401
                return -1;
353
402
        }
354
403
        if (net_device->send_buf_size <= 0) {
355
404
                ret = -EINVAL;
356
 
                goto Cleanup;
 
405
                goto cleanup;
357
406
        }
358
407
 
359
 
        /* page-size grandularity */
360
 
        /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
361
 
 
362
408
        net_device->send_buf =
363
 
                osd_page_alloc(net_device->send_buf_size >> PAGE_SHIFT);
 
409
                (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
 
410
                                get_order(net_device->send_buf_size));
364
411
        if (!net_device->send_buf) {
365
 
                DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
366
 
                           net_device->send_buf_size);
 
412
                dev_err(&device->device, "unable to allocate send "
 
413
                        "buffer of size %d", net_device->send_buf_size);
367
414
                ret = -1;
368
 
                goto Cleanup;
 
415
                goto cleanup;
369
416
        }
370
 
        /* page-aligned buffer */
371
 
        /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
372
 
 
373
 
        DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
374
417
 
375
418
        /*
376
419
         * Establish the gpadl handle for this buffer on this
381
424
                                    net_device->send_buf_size,
382
425
                                    &net_device->send_buf_gpadl_handle);
383
426
        if (ret != 0) {
384
 
                DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
385
 
                goto Cleanup;
 
427
                dev_err(&device->device, "unable to establish send buffer's gpadl");
 
428
                goto cleanup;
386
429
        }
387
430
 
388
 
        /* osd_waitevent_wait(ext->ChannelInitEvent); */
389
 
 
390
431
        /* Notify the NetVsp of the gpadl handle */
391
 
        DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
392
 
 
393
432
        init_packet = &net_device->channel_init_pkt;
394
433
 
395
434
        memset(init_packet, 0, sizeof(struct nvsp_message));
404
443
        ret = vmbus_sendpacket(device->channel, init_packet,
405
444
                               sizeof(struct nvsp_message),
406
445
                               (unsigned long)init_packet,
407
 
                               VmbusPacketTypeDataInBand,
 
446
                               VM_PKT_DATA_INBAND,
408
447
                               VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
409
448
        if (ret != 0) {
410
 
                DPRINT_ERR(NETVSC,
 
449
                dev_err(&device->device,
411
450
                           "unable to send receive buffer's gpadl to netvsp");
412
 
                goto Cleanup;
 
451
                goto cleanup;
413
452
        }
414
453
 
415
 
        osd_waitevent_wait(net_device->channel_init_event);
 
454
        t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
 
455
 
 
456
        BUG_ON(t == 0);
416
457
 
417
458
        /* Check the response */
418
459
        if (init_packet->msg.v1_msg.
419
460
            send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
420
 
                DPRINT_ERR(NETVSC, "Unable to complete send buffer "
 
461
                dev_err(&device->device, "Unable to complete send buffer "
421
462
                           "initialzation with NetVsp - status %d",
422
463
                           init_packet->msg.v1_msg.
423
464
                           send_send_buf_complete.status);
424
465
                ret = -1;
425
 
                goto Cleanup;
 
466
                goto cleanup;
426
467
        }
427
468
 
428
469
        net_device->send_section_size = init_packet->
429
470
        msg.v1_msg.send_send_buf_complete.section_size;
430
471
 
431
 
        goto Exit;
 
472
        goto exit;
432
473
 
433
 
Cleanup:
 
474
cleanup:
434
475
        netvsc_destroy_send_buf(net_device);
435
476
 
436
 
Exit:
 
477
exit:
437
478
        put_net_device(device);
438
479
        return ret;
439
480
}
440
481
 
441
 
static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
442
 
{
443
 
        struct nvsp_message *revoke_packet;
444
 
        int ret = 0;
445
 
 
446
 
        /*
447
 
         * If we got a section count, it means we received a
448
 
         * SendReceiveBufferComplete msg (ie sent
449
 
         * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
450
 
         * to send a revoke msg here
451
 
         */
452
 
        if (net_device->recv_section_cnt) {
453
 
                DPRINT_INFO(NETVSC,
454
 
                            "Sending NvspMessage1TypeRevokeReceiveBuffer...");
455
 
 
456
 
                /* Send the revoke receive buffer */
457
 
                revoke_packet = &net_device->revoke_packet;
458
 
                memset(revoke_packet, 0, sizeof(struct nvsp_message));
459
 
 
460
 
                revoke_packet->hdr.msg_type =
461
 
                        NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
462
 
                revoke_packet->msg.v1_msg.
463
 
                revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
464
 
 
465
 
                ret = vmbus_sendpacket(net_device->dev->channel,
466
 
                                       revoke_packet,
467
 
                                       sizeof(struct nvsp_message),
468
 
                                       (unsigned long)revoke_packet,
469
 
                                       VmbusPacketTypeDataInBand, 0);
470
 
                /*
471
 
                 * If we failed here, we might as well return and
472
 
                 * have a leak rather than continue and a bugchk
473
 
                 */
474
 
                if (ret != 0) {
475
 
                        DPRINT_ERR(NETVSC, "unable to send revoke receive "
476
 
                                   "buffer to netvsp");
477
 
                        return -1;
478
 
                }
479
 
        }
480
 
 
481
 
        /* Teardown the gpadl on the vsp end */
482
 
        if (net_device->recv_buf_gpadl_handle) {
483
 
                DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
484
 
 
485
 
                ret = vmbus_teardown_gpadl(net_device->dev->channel,
486
 
                           net_device->recv_buf_gpadl_handle);
487
 
 
488
 
                /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
489
 
                if (ret != 0) {
490
 
                        DPRINT_ERR(NETVSC,
491
 
                                   "unable to teardown receive buffer's gpadl");
492
 
                        return -1;
493
 
                }
494
 
                net_device->recv_buf_gpadl_handle = 0;
495
 
        }
496
 
 
497
 
        if (net_device->recv_buf) {
498
 
                DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
499
 
 
500
 
                /* Free up the receive buffer */
501
 
                osd_page_free(net_device->recv_buf,
502
 
                             net_device->recv_buf_size >> PAGE_SHIFT);
503
 
                net_device->recv_buf = NULL;
504
 
        }
505
 
 
506
 
        if (net_device->recv_section) {
507
 
                net_device->recv_section_cnt = 0;
508
 
                kfree(net_device->recv_section);
509
 
                net_device->recv_section = NULL;
510
 
        }
511
 
 
512
 
        return ret;
513
 
}
514
 
 
515
 
static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
516
 
{
517
 
        struct nvsp_message *revoke_packet;
518
 
        int ret = 0;
519
 
 
520
 
        /*
521
 
         * If we got a section count, it means we received a
522
 
         *  SendReceiveBufferComplete msg (ie sent
523
 
         *  NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
524
 
         *  to send a revoke msg here
525
 
         */
526
 
        if (net_device->send_section_size) {
527
 
                DPRINT_INFO(NETVSC,
528
 
                            "Sending NvspMessage1TypeRevokeSendBuffer...");
529
 
 
530
 
                /* Send the revoke send buffer */
531
 
                revoke_packet = &net_device->revoke_packet;
532
 
                memset(revoke_packet, 0, sizeof(struct nvsp_message));
533
 
 
534
 
                revoke_packet->hdr.msg_type =
535
 
                        NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
536
 
                revoke_packet->msg.v1_msg.
537
 
                        revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
538
 
 
539
 
                ret = vmbus_sendpacket(net_device->dev->channel,
540
 
                                       revoke_packet,
541
 
                                       sizeof(struct nvsp_message),
542
 
                                       (unsigned long)revoke_packet,
543
 
                                       VmbusPacketTypeDataInBand, 0);
544
 
                /*
545
 
                 * If we failed here, we might as well return and have a leak
546
 
                 * rather than continue and a bugchk
547
 
                 */
548
 
                if (ret != 0) {
549
 
                        DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
550
 
                                   "to netvsp");
551
 
                        return -1;
552
 
                }
553
 
        }
554
 
 
555
 
        /* Teardown the gpadl on the vsp end */
556
 
        if (net_device->send_buf_gpadl_handle) {
557
 
                DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
558
 
                ret = vmbus_teardown_gpadl(net_device->dev->channel,
559
 
                                           net_device->send_buf_gpadl_handle);
560
 
 
561
 
                /*
562
 
                 * If we failed here, we might as well return and have a leak
563
 
                 * rather than continue and a bugchk
564
 
                 */
565
 
                if (ret != 0) {
566
 
                        DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
567
 
                                   "gpadl");
568
 
                        return -1;
569
 
                }
570
 
                net_device->send_buf_gpadl_handle = 0;
571
 
        }
572
 
 
573
 
        if (net_device->send_buf) {
574
 
                DPRINT_INFO(NETVSC, "Freeing up send buffer...");
575
 
 
576
 
                /* Free up the receive buffer */
577
 
                osd_page_free(net_device->send_buf,
578
 
                             net_device->send_buf_size >> PAGE_SHIFT);
579
 
                net_device->send_buf = NULL;
580
 
        }
581
 
 
582
 
        return ret;
583
 
}
584
 
 
585
482
 
586
483
static int netvsc_connect_vsp(struct hv_device *device)
587
484
{
588
 
        int ret;
 
485
        int ret, t;
589
486
        struct netvsc_device *net_device;
590
487
        struct nvsp_message *init_packet;
591
488
        int ndis_version;
592
489
 
593
490
        net_device = get_outbound_net_device(device);
594
491
        if (!net_device) {
595
 
                DPRINT_ERR(NETVSC, "unable to get net device..."
 
492
                dev_err(&device->device, "unable to get net device..."
596
493
                           "device being destroyed?");
597
494
                return -1;
598
495
        }
606
503
        init_packet->msg.init_msg.init.max_protocol_ver =
607
504
                NVSP_MAX_PROTOCOL_VERSION;
608
505
 
609
 
        DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
610
 
 
611
506
        /* Send the init request */
612
507
        ret = vmbus_sendpacket(device->channel, init_packet,
613
508
                               sizeof(struct nvsp_message),
614
509
                               (unsigned long)init_packet,
615
 
                               VmbusPacketTypeDataInBand,
 
510
                               VM_PKT_DATA_INBAND,
616
511
                               VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
617
512
 
618
 
        if (ret != 0) {
619
 
                DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
620
 
                goto Cleanup;
 
513
        if (ret != 0)
 
514
                goto cleanup;
 
515
 
 
516
        t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
 
517
 
 
518
        if (t == 0) {
 
519
                ret = -ETIMEDOUT;
 
520
                goto cleanup;
621
521
        }
622
522
 
623
 
        osd_waitevent_wait(net_device->channel_init_event);
624
 
 
625
 
        /* Now, check the response */
626
 
        /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
627
 
        DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
628
 
                init_packet->msg.init_msg.init_complete.status,
629
 
                init_packet->msg.init_msg.
630
 
                    init_complete.max_mdl_chain_len);
631
 
 
632
523
        if (init_packet->msg.init_msg.init_complete.status !=
633
524
            NVSP_STAT_SUCCESS) {
634
 
                DPRINT_ERR(NETVSC,
635
 
                        "unable to initialize with netvsp (status 0x%x)",
636
 
                        init_packet->msg.init_msg.init_complete.status);
637
525
                ret = -1;
638
 
                goto Cleanup;
 
526
                goto cleanup;
639
527
        }
640
528
 
641
529
        if (init_packet->msg.init_msg.init_complete.
642
530
            negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
643
 
                DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
644
 
                           "(version expected 1 got %d)",
645
 
                           init_packet->msg.init_msg.
646
 
                           init_complete.negotiated_protocol_ver);
647
531
                ret = -1;
648
 
                goto Cleanup;
 
532
                goto cleanup;
649
533
        }
650
 
        DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
651
 
 
652
534
        /* Send the ndis version */
653
535
        memset(init_packet, 0, sizeof(struct nvsp_message));
654
536
 
664
546
 
665
547
        /* Send the init request */
666
548
        ret = vmbus_sendpacket(device->channel, init_packet,
667
 
                               sizeof(struct nvsp_message),
668
 
                               (unsigned long)init_packet,
669
 
                               VmbusPacketTypeDataInBand, 0);
 
549
                                sizeof(struct nvsp_message),
 
550
                                (unsigned long)init_packet,
 
551
                                VM_PKT_DATA_INBAND, 0);
670
552
        if (ret != 0) {
671
 
                DPRINT_ERR(NETVSC,
672
 
                           "unable to send NvspMessage1TypeSendNdisVersion");
673
553
                ret = -1;
674
 
                goto Cleanup;
 
554
                goto cleanup;
675
555
        }
676
 
        /*
677
 
         * BUGBUG - We have to wait for the above msg since the
678
 
         * netvsp uses KMCL which acknowledges packet (completion
679
 
         * packet) since our Vmbus always set the
680
 
         * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
681
 
         */
682
 
         /* osd_waitevent_wait(NetVscChannel->ChannelInitEvent); */
683
556
 
684
557
        /* Post the big receive buffer to NetVSP */
685
558
        ret = netvsc_init_recv_buf(device);
686
559
        if (ret == 0)
687
560
                ret = netvsc_init_send_buf(device);
688
561
 
689
 
Cleanup:
 
562
cleanup:
690
563
        put_net_device(device);
691
564
        return ret;
692
565
}
693
566
 
694
 
static void NetVscDisconnectFromVsp(struct netvsc_device *net_device)
 
567
static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
695
568
{
696
569
        netvsc_destroy_recv_buf(net_device);
697
570
        netvsc_destroy_send_buf(net_device);
698
571
}
699
572
 
700
573
/*
701
 
 * netvsc_device_add - Callback when the device belonging to this
702
 
 * driver is added
703
 
 */
704
 
static int netvsc_device_add(struct hv_device *device, void *additional_info)
705
 
{
706
 
        int ret = 0;
707
 
        int i;
708
 
        struct netvsc_device *net_device;
709
 
        struct hv_netvsc_packet *packet, *pos;
710
 
        struct netvsc_driver *net_driver =
711
 
                                (struct netvsc_driver *)device->Driver;
712
 
 
713
 
        net_device = alloc_net_device(device);
714
 
        if (!net_device) {
715
 
                ret = -1;
716
 
                goto Cleanup;
717
 
        }
718
 
 
719
 
        DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", net_device);
720
 
 
721
 
        /* Initialize the NetVSC channel extension */
722
 
        net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
723
 
        spin_lock_init(&net_device->recv_pkt_list_lock);
724
 
 
725
 
        net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
726
 
 
727
 
        INIT_LIST_HEAD(&net_device->recv_pkt_list);
728
 
 
729
 
        for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
730
 
                packet = kzalloc(sizeof(struct hv_netvsc_packet) +
731
 
                                 (NETVSC_RECEIVE_SG_COUNT *
732
 
                                  sizeof(struct hv_page_buffer)), GFP_KERNEL);
733
 
                if (!packet) {
734
 
                        DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
735
 
                                   "for receive pool (wanted %d got %d)",
736
 
                                   NETVSC_RECEIVE_PACKETLIST_COUNT, i);
737
 
                        break;
738
 
                }
739
 
                list_add_tail(&packet->list_ent,
740
 
                              &net_device->recv_pkt_list);
741
 
        }
742
 
        net_device->channel_init_event = osd_waitevent_create();
743
 
        if (!net_device->channel_init_event) {
744
 
                ret = -ENOMEM;
745
 
                goto Cleanup;
746
 
        }
747
 
 
748
 
        /* Open the channel */
749
 
        ret = vmbus_open(device->channel, net_driver->ring_buf_size,
750
 
                         net_driver->ring_buf_size, NULL, 0,
751
 
                         netvsc_channel_cb, device);
752
 
 
753
 
        if (ret != 0) {
754
 
                DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
755
 
                ret = -1;
756
 
                goto Cleanup;
757
 
        }
758
 
 
759
 
        /* Channel is opened */
760
 
        DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
761
 
 
762
 
        /* Connect with the NetVsp */
763
 
        ret = netvsc_connect_vsp(device);
764
 
        if (ret != 0) {
765
 
                DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
766
 
                ret = -1;
767
 
                goto close;
768
 
        }
769
 
 
770
 
        DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
771
 
                    ret);
772
 
 
773
 
        return ret;
774
 
 
775
 
close:
776
 
        /* Now, we can close the channel safely */
777
 
        vmbus_close(device->channel);
778
 
 
779
 
Cleanup:
780
 
 
781
 
        if (net_device) {
782
 
                kfree(net_device->channel_init_event);
783
 
 
784
 
                list_for_each_entry_safe(packet, pos,
785
 
                                         &net_device->recv_pkt_list,
786
 
                                         list_ent) {
787
 
                        list_del(&packet->list_ent);
788
 
                        kfree(packet);
789
 
                }
790
 
 
791
 
                release_outbound_net_device(device);
792
 
                release_inbound_net_device(device);
793
 
 
794
 
                free_net_device(net_device);
795
 
        }
796
 
 
797
 
        return ret;
798
 
}
799
 
 
800
 
/*
801
574
 * netvsc_device_remove - Callback when the root bus device is removed
802
575
 */
803
 
static int netvsc_device_remove(struct hv_device *device)
 
576
int netvsc_device_remove(struct hv_device *device)
804
577
{
805
578
        struct netvsc_device *net_device;
806
579
        struct hv_netvsc_packet *netvsc_packet, *pos;
807
580
 
808
 
        DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
809
 
                    device->Extension);
810
 
 
811
581
        /* Stop outbound traffic ie sends and receives completions */
812
582
        net_device = release_outbound_net_device(device);
813
583
        if (!net_device) {
814
 
                DPRINT_ERR(NETVSC, "No net device present!!");
 
584
                dev_err(&device->device, "No net device present!!");
815
585
                return -1;
816
586
        }
817
587
 
818
588
        /* Wait for all send completions */
819
589
        while (atomic_read(&net_device->num_outstanding_sends)) {
820
 
                DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
821
 
                            atomic_read(&net_device->num_outstanding_sends));
 
590
                dev_err(&device->device,
 
591
                        "waiting for %d requests to complete...",
 
592
                        atomic_read(&net_device->num_outstanding_sends));
822
593
                udelay(100);
823
594
        }
824
595
 
825
 
        DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
826
 
 
827
 
        NetVscDisconnectFromVsp(net_device);
828
 
 
829
 
        DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
830
 
                    device->Extension);
 
596
        netvsc_disconnect_vsp(net_device);
831
597
 
832
598
        /* Stop inbound traffic ie receives and sends completions */
833
599
        net_device = release_inbound_net_device(device);
834
600
 
835
601
        /* At this point, no one should be accessing netDevice except in here */
836
 
        DPRINT_INFO(NETVSC, "net device (%p) safe to remove", net_device);
 
602
        dev_notice(&device->device, "net device safe to remove");
837
603
 
838
604
        /* Now, we can close the channel safely */
839
605
        vmbus_close(device->channel);
845
611
                kfree(netvsc_packet);
846
612
        }
847
613
 
848
 
        kfree(net_device->channel_init_event);
849
614
        free_net_device(net_device);
850
615
        return 0;
851
616
}
852
617
 
853
 
/*
854
 
 * netvsc_cleanup - Perform any cleanup when the driver is removed
855
 
 */
856
 
static void netvsc_cleanup(struct hv_driver *drv)
857
 
{
858
 
}
859
 
 
860
618
static void netvsc_send_completion(struct hv_device *device,
861
619
                                   struct vmpacket_descriptor *packet)
862
620
{
866
624
 
867
625
        net_device = get_inbound_net_device(device);
868
626
        if (!net_device) {
869
 
                DPRINT_ERR(NETVSC, "unable to get net device..."
 
627
                dev_err(&device->device, "unable to get net device..."
870
628
                           "device being destroyed?");
871
629
                return;
872
630
        }
873
631
 
874
632
        nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
875
 
                        (packet->DataOffset8 << 3));
876
 
 
877
 
        DPRINT_DBG(NETVSC, "send completion packet - type %d",
878
 
                   nvsp_packet->hdr.msg_type);
 
633
                        (packet->offset8 << 3));
879
634
 
880
635
        if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
881
636
            (nvsp_packet->hdr.msg_type ==
885
640
                /* Copy the response back */
886
641
                memcpy(&net_device->channel_init_pkt, nvsp_packet,
887
642
                       sizeof(struct nvsp_message));
888
 
                osd_waitevent_set(net_device->channel_init_event);
 
643
                complete(&net_device->channel_init_wait);
889
644
        } else if (nvsp_packet->hdr.msg_type ==
890
645
                   NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
891
646
                /* Get the send context */
892
647
                nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
893
 
                        packet->TransactionId;
894
 
                /* ASSERT(nvscPacket); */
 
648
                        packet->trans_id;
895
649
 
896
650
                /* Notify the layer above us */
897
651
                nvsc_packet->completion.send.send_completion(
899
653
 
900
654
                atomic_dec(&net_device->num_outstanding_sends);
901
655
        } else {
902
 
                DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
 
656
                dev_err(&device->device, "Unknown send completion packet type- "
903
657
                           "%d received!!", nvsp_packet->hdr.msg_type);
904
658
        }
905
659
 
906
660
        put_net_device(device);
907
661
}
908
662
 
909
 
static int netvsc_send(struct hv_device *device,
 
663
int netvsc_send(struct hv_device *device,
910
664
                        struct hv_netvsc_packet *packet)
911
665
{
912
666
        struct netvsc_device *net_device;
916
670
 
917
671
        net_device = get_outbound_net_device(device);
918
672
        if (!net_device) {
919
 
                DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
 
673
                dev_err(&device->device, "net device (%p) shutting down..."
920
674
                           "ignoring outbound packets", net_device);
921
675
                return -2;
922
676
        }
946
700
                ret = vmbus_sendpacket(device->channel, &sendMessage,
947
701
                                       sizeof(struct nvsp_message),
948
702
                                       (unsigned long)packet,
949
 
                                       VmbusPacketTypeDataInBand,
 
703
                                       VM_PKT_DATA_INBAND,
950
704
                                       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
951
705
 
952
706
        }
953
707
 
954
708
        if (ret != 0)
955
 
                DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
 
709
                dev_err(&device->device, "Unable to send packet %p ret %d",
956
710
                           packet, ret);
957
711
 
958
712
        atomic_inc(&net_device->num_outstanding_sends);
960
714
        return ret;
961
715
}
962
716
 
 
717
static void netvsc_send_recv_completion(struct hv_device *device,
 
718
                                        u64 transaction_id)
 
719
{
 
720
        struct nvsp_message recvcompMessage;
 
721
        int retries = 0;
 
722
        int ret;
 
723
 
 
724
        recvcompMessage.hdr.msg_type =
 
725
                                NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
 
726
 
 
727
        /* FIXME: Pass in the status */
 
728
        recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
 
729
                NVSP_STAT_SUCCESS;
 
730
 
 
731
retry_send_cmplt:
 
732
        /* Send the completion */
 
733
        ret = vmbus_sendpacket(device->channel, &recvcompMessage,
 
734
                               sizeof(struct nvsp_message), transaction_id,
 
735
                               VM_PKT_COMP, 0);
 
736
        if (ret == 0) {
 
737
                /* success */
 
738
                /* no-op */
 
739
        } else if (ret == -1) {
 
740
                /* no more room...wait a bit and attempt to retry 3 times */
 
741
                retries++;
 
742
                dev_err(&device->device, "unable to send receive completion pkt"
 
743
                        " (tid %llx)...retrying %d", transaction_id, retries);
 
744
 
 
745
                if (retries < 4) {
 
746
                        udelay(100);
 
747
                        goto retry_send_cmplt;
 
748
                } else {
 
749
                        dev_err(&device->device, "unable to send receive "
 
750
                                "completion pkt (tid %llx)...give up retrying",
 
751
                                transaction_id);
 
752
                }
 
753
        } else {
 
754
                dev_err(&device->device, "unable to send receive "
 
755
                        "completion pkt - %llx", transaction_id);
 
756
        }
 
757
}
 
758
 
 
759
/* Send a receive completion packet to RNDIS device (ie NetVsp) */
 
760
static void netvsc_receive_completion(void *context)
 
761
{
 
762
        struct hv_netvsc_packet *packet = context;
 
763
        struct hv_device *device = (struct hv_device *)packet->device;
 
764
        struct netvsc_device *net_device;
 
765
        u64 transaction_id = 0;
 
766
        bool fsend_receive_comp = false;
 
767
        unsigned long flags;
 
768
 
 
769
        /*
 
770
         * Even though it seems logical to do a GetOutboundNetDevice() here to
 
771
         * send out receive completion, we are using GetInboundNetDevice()
 
772
         * since we may have disable outbound traffic already.
 
773
         */
 
774
        net_device = get_inbound_net_device(device);
 
775
        if (!net_device) {
 
776
                dev_err(&device->device, "unable to get net device..."
 
777
                           "device being destroyed?");
 
778
                return;
 
779
        }
 
780
 
 
781
        /* Overloading use of the lock. */
 
782
        spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
 
783
 
 
784
        packet->xfer_page_pkt->count--;
 
785
 
 
786
        /*
 
787
         * Last one in the line that represent 1 xfer page packet.
 
788
         * Return the xfer page packet itself to the freelist
 
789
         */
 
790
        if (packet->xfer_page_pkt->count == 0) {
 
791
                fsend_receive_comp = true;
 
792
                transaction_id = packet->completion.recv.recv_completion_tid;
 
793
                list_add_tail(&packet->xfer_page_pkt->list_ent,
 
794
                              &net_device->recv_pkt_list);
 
795
 
 
796
        }
 
797
 
 
798
        /* Put the packet back */
 
799
        list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
 
800
        spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
 
801
 
 
802
        /* Send a receive completion for the xfer page packet */
 
803
        if (fsend_receive_comp)
 
804
                netvsc_send_recv_completion(device, transaction_id);
 
805
 
 
806
        put_net_device(device);
 
807
}
 
808
 
963
809
static void netvsc_receive(struct hv_device *device,
964
810
                            struct vmpacket_descriptor *packet)
965
811
{
974
820
        int i, j;
975
821
        int count = 0, bytes_remain = 0;
976
822
        unsigned long flags;
 
823
 
977
824
        LIST_HEAD(listHead);
978
825
 
979
826
        net_device = get_inbound_net_device(device);
980
827
        if (!net_device) {
981
 
                DPRINT_ERR(NETVSC, "unable to get net device..."
 
828
                dev_err(&device->device, "unable to get net device..."
982
829
                           "device being destroyed?");
983
830
                return;
984
831
        }
987
834
         * All inbound packets other than send completion should be xfer page
988
835
         * packet
989
836
         */
990
 
        if (packet->Type != VmbusPacketTypeDataUsingTransferPages) {
991
 
                DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
992
 
                           packet->Type);
 
837
        if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
 
838
                dev_err(&device->device, "Unknown packet type received - %d",
 
839
                           packet->type);
993
840
                put_net_device(device);
994
841
                return;
995
842
        }
996
843
 
997
844
        nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
998
 
                        (packet->DataOffset8 << 3));
 
845
                        (packet->offset8 << 3));
999
846
 
1000
847
        /* Make sure this is a valid nvsp packet */
1001
848
        if (nvsp_packet->hdr.msg_type !=
1002
849
            NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1003
 
                DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1004
 
                           nvsp_packet->hdr.msg_type);
 
850
                dev_err(&device->device, "Unknown nvsp packet type received-"
 
851
                        " %d", nvsp_packet->hdr.msg_type);
1005
852
                put_net_device(device);
1006
853
                return;
1007
854
        }
1008
855
 
1009
 
        DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1010
 
                   nvsp_packet->hdr.msg_type);
1011
 
 
1012
856
        vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1013
857
 
1014
 
        if (vmxferpage_packet->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1015
 
                DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
 
858
        if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
 
859
                dev_err(&device->device, "Invalid xfer page set id - "
1016
860
                           "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1017
 
                           vmxferpage_packet->TransferPageSetId);
 
861
                           vmxferpage_packet->xfer_pageset_id);
1018
862
                put_net_device(device);
1019
863
                return;
1020
864
        }
1021
865
 
1022
 
        DPRINT_DBG(NETVSC, "xfer page - range count %d",
1023
 
                   vmxferpage_packet->RangeCount);
1024
 
 
1025
866
        /*
1026
867
         * Grab free packets (range count + 1) to represent this xfer
1027
868
         * page packet. +1 to represent the xfer page packet itself.
1031
872
        spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1032
873
        while (!list_empty(&net_device->recv_pkt_list)) {
1033
874
                list_move_tail(net_device->recv_pkt_list.next, &listHead);
1034
 
                if (++count == vmxferpage_packet->RangeCount + 1)
 
875
                if (++count == vmxferpage_packet->range_cnt + 1)
1035
876
                        break;
1036
877
        }
1037
878
        spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1042
883
         * some of the xfer page packet ranges...
1043
884
         */
1044
885
        if (count < 2) {
1045
 
                DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1046
 
                           "Dropping this xfer page packet completely!",
1047
 
                           count, vmxferpage_packet->RangeCount + 1);
 
886
                dev_err(&device->device, "Got only %d netvsc pkt...needed "
 
887
                        "%d pkts. Dropping this xfer page packet completely!",
 
888
                        count, vmxferpage_packet->range_cnt + 1);
1048
889
 
1049
890
                /* Return it to the freelist */
1050
891
                spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1056
897
                                       flags);
1057
898
 
1058
899
                netvsc_send_recv_completion(device,
1059
 
                                            vmxferpage_packet->d.TransactionId);
 
900
                                            vmxferpage_packet->d.trans_id);
1060
901
 
1061
902
                put_net_device(device);
1062
903
                return;
1068
909
 
1069
910
        /* This is how much we can satisfy */
1070
911
        xferpage_packet->count = count - 1;
1071
 
        /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1072
 
        /*      vmxferpagePacket->RangeCount); */
1073
912
 
1074
 
        if (xferpage_packet->count != vmxferpage_packet->RangeCount) {
1075
 
                DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1076
 
                            "page...got %d", vmxferpage_packet->RangeCount,
1077
 
                            xferpage_packet->count);
 
913
        if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
 
914
                dev_err(&device->device, "Needed %d netvsc pkts to satisy "
 
915
                        "this xfer page...got %d",
 
916
                        vmxferpage_packet->range_cnt, xferpage_packet->count);
1078
917
        }
1079
918
 
1080
919
        /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1091
930
                netvsc_packet->device = device;
1092
931
                /* Save this so that we can send it back */
1093
932
                netvsc_packet->completion.recv.recv_completion_tid =
1094
 
                                        vmxferpage_packet->d.TransactionId;
 
933
                                        vmxferpage_packet->d.trans_id;
1095
934
 
1096
935
                netvsc_packet->total_data_buflen =
1097
 
                                        vmxferpage_packet->Ranges[i].ByteCount;
 
936
                                        vmxferpage_packet->ranges[i].byte_count;
1098
937
                netvsc_packet->page_buf_cnt = 1;
1099
938
 
1100
 
                /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1101
 
                /*      vmxferpagePacket->Ranges[i].ByteCount < */
1102
 
                /*      netDevice->ReceiveBufferSize); */
1103
 
 
1104
 
                netvsc_packet->page_buf[0].Length =
1105
 
                                        vmxferpage_packet->Ranges[i].ByteCount;
 
939
                netvsc_packet->page_buf[0].len =
 
940
                                        vmxferpage_packet->ranges[i].byte_count;
1106
941
 
1107
942
                start = virt_to_phys((void *)((unsigned long)net_device->
1108
 
                recv_buf + vmxferpage_packet->Ranges[i].ByteOffset));
 
943
                recv_buf + vmxferpage_packet->ranges[i].byte_offset));
1109
944
 
1110
 
                netvsc_packet->page_buf[0].Pfn = start >> PAGE_SHIFT;
 
945
                netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
1111
946
                end_virtual = (unsigned long)net_device->recv_buf
1112
 
                    + vmxferpage_packet->Ranges[i].ByteOffset
1113
 
                    + vmxferpage_packet->Ranges[i].ByteCount - 1;
 
947
                    + vmxferpage_packet->ranges[i].byte_offset
 
948
                    + vmxferpage_packet->ranges[i].byte_count - 1;
1114
949
                end = virt_to_phys((void *)end_virtual);
1115
950
 
1116
951
                /* Calculate the page relative offset */
1117
 
                netvsc_packet->page_buf[0].Offset =
1118
 
                        vmxferpage_packet->Ranges[i].ByteOffset &
 
952
                netvsc_packet->page_buf[0].offset =
 
953
                        vmxferpage_packet->ranges[i].byte_offset &
1119
954
                        (PAGE_SIZE - 1);
1120
955
                if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1121
956
                        /* Handle frame across multiple pages: */
1122
 
                        netvsc_packet->page_buf[0].Length =
1123
 
                                (netvsc_packet->page_buf[0].Pfn <<
 
957
                        netvsc_packet->page_buf[0].len =
 
958
                                (netvsc_packet->page_buf[0].pfn <<
1124
959
                                 PAGE_SHIFT)
1125
960
                                + PAGE_SIZE - start;
1126
961
                        bytes_remain = netvsc_packet->total_data_buflen -
1127
 
                                        netvsc_packet->page_buf[0].Length;
 
962
                                        netvsc_packet->page_buf[0].len;
1128
963
                        for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1129
 
                                netvsc_packet->page_buf[j].Offset = 0;
 
964
                                netvsc_packet->page_buf[j].offset = 0;
1130
965
                                if (bytes_remain <= PAGE_SIZE) {
1131
 
                                        netvsc_packet->page_buf[j].Length =
 
966
                                        netvsc_packet->page_buf[j].len =
1132
967
                                                bytes_remain;
1133
968
                                        bytes_remain = 0;
1134
969
                                } else {
1135
 
                                        netvsc_packet->page_buf[j].Length =
 
970
                                        netvsc_packet->page_buf[j].len =
1136
971
                                                PAGE_SIZE;
1137
972
                                        bytes_remain -= PAGE_SIZE;
1138
973
                                }
1139
 
                                netvsc_packet->page_buf[j].Pfn =
 
974
                                netvsc_packet->page_buf[j].pfn =
1140
975
                                    virt_to_phys((void *)(end_virtual -
1141
976
                                                bytes_remain)) >> PAGE_SHIFT;
1142
977
                                netvsc_packet->page_buf_cnt++;
1143
978
                                if (bytes_remain == 0)
1144
979
                                        break;
1145
980
                        }
1146
 
                        /* ASSERT(bytesRemain == 0); */
1147
981
                }
1148
 
                DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1149
 
                           "(pfn %llx, offset %u, len %u)", i,
1150
 
                           vmxferpage_packet->Ranges[i].ByteOffset,
1151
 
                           vmxferpage_packet->Ranges[i].ByteCount,
1152
 
                           netvsc_packet->page_buf[0].Pfn,
1153
 
                           netvsc_packet->page_buf[0].Offset,
1154
 
                           netvsc_packet->page_buf[0].Length);
1155
982
 
1156
983
                /* Pass it to the upper layer */
1157
 
                ((struct netvsc_driver *)device->Driver)->
1158
 
                        recv_cb(device, netvsc_packet);
 
984
                rndis_filter_receive(device, netvsc_packet);
1159
985
 
1160
986
                netvsc_receive_completion(netvsc_packet->
1161
987
                                completion.recv.recv_completion_ctx);
1162
988
        }
1163
989
 
1164
 
        /* ASSERT(list_empty(&listHead)); */
1165
 
 
1166
 
        put_net_device(device);
1167
 
}
1168
 
 
1169
 
static void netvsc_send_recv_completion(struct hv_device *device,
1170
 
                                        u64 transaction_id)
1171
 
{
1172
 
        struct nvsp_message recvcompMessage;
1173
 
        int retries = 0;
1174
 
        int ret;
1175
 
 
1176
 
        DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1177
 
                   transaction_id);
1178
 
 
1179
 
        recvcompMessage.hdr.msg_type =
1180
 
                                NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1181
 
 
1182
 
        /* FIXME: Pass in the status */
1183
 
        recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
1184
 
                NVSP_STAT_SUCCESS;
1185
 
 
1186
 
retry_send_cmplt:
1187
 
        /* Send the completion */
1188
 
        ret = vmbus_sendpacket(device->channel, &recvcompMessage,
1189
 
                               sizeof(struct nvsp_message), transaction_id,
1190
 
                               VmbusPacketTypeCompletion, 0);
1191
 
        if (ret == 0) {
1192
 
                /* success */
1193
 
                /* no-op */
1194
 
        } else if (ret == -1) {
1195
 
                /* no more room...wait a bit and attempt to retry 3 times */
1196
 
                retries++;
1197
 
                DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1198
 
                           "(tid %llx)...retrying %d", transaction_id, retries);
1199
 
 
1200
 
                if (retries < 4) {
1201
 
                        udelay(100);
1202
 
                        goto retry_send_cmplt;
1203
 
                } else {
1204
 
                        DPRINT_ERR(NETVSC, "unable to send receive completion "
1205
 
                                  "pkt (tid %llx)...give up retrying",
1206
 
                                  transaction_id);
1207
 
                }
1208
 
        } else {
1209
 
                DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1210
 
                           "%llx", transaction_id);
1211
 
        }
1212
 
}
1213
 
 
1214
 
/* Send a receive completion packet to RNDIS device (ie NetVsp) */
1215
 
static void netvsc_receive_completion(void *context)
1216
 
{
1217
 
        struct hv_netvsc_packet *packet = context;
1218
 
        struct hv_device *device = (struct hv_device *)packet->device;
1219
 
        struct netvsc_device *net_device;
1220
 
        u64 transaction_id = 0;
1221
 
        bool fsend_receive_comp = false;
1222
 
        unsigned long flags;
1223
 
 
1224
 
        /* ASSERT(packet->XferPagePacket); */
1225
 
 
1226
 
        /*
1227
 
         * Even though it seems logical to do a GetOutboundNetDevice() here to
1228
 
         * send out receive completion, we are using GetInboundNetDevice()
1229
 
         * since we may have disable outbound traffic already.
1230
 
         */
1231
 
        net_device = get_inbound_net_device(device);
1232
 
        if (!net_device) {
1233
 
                DPRINT_ERR(NETVSC, "unable to get net device..."
1234
 
                           "device being destroyed?");
1235
 
                return;
1236
 
        }
1237
 
 
1238
 
        /* Overloading use of the lock. */
1239
 
        spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
1240
 
 
1241
 
        /* ASSERT(packet->XferPagePacket->Count > 0); */
1242
 
        packet->xfer_page_pkt->count--;
1243
 
 
1244
 
        /*
1245
 
         * Last one in the line that represent 1 xfer page packet.
1246
 
         * Return the xfer page packet itself to the freelist
1247
 
         */
1248
 
        if (packet->xfer_page_pkt->count == 0) {
1249
 
                fsend_receive_comp = true;
1250
 
                transaction_id = packet->completion.recv.recv_completion_tid;
1251
 
                list_add_tail(&packet->xfer_page_pkt->list_ent,
1252
 
                              &net_device->recv_pkt_list);
1253
 
 
1254
 
        }
1255
 
 
1256
 
        /* Put the packet back */
1257
 
        list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
1258
 
        spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
1259
 
 
1260
 
        /* Send a receive completion for the xfer page packet */
1261
 
        if (fsend_receive_comp)
1262
 
                netvsc_send_recv_completion(device, transaction_id);
1263
 
 
1264
990
        put_net_device(device);
1265
991
}
1266
992
 
1276
1002
        unsigned char *buffer;
1277
1003
        int bufferlen = NETVSC_PACKET_SIZE;
1278
1004
 
1279
 
        /* ASSERT(device); */
1280
 
 
1281
1005
        packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
1282
1006
                         GFP_ATOMIC);
1283
1007
        if (!packet)
1286
1010
 
1287
1011
        net_device = get_inbound_net_device(device);
1288
1012
        if (!net_device) {
1289
 
                DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
 
1013
                dev_err(&device->device, "net device (%p) shutting down..."
1290
1014
                           "ignoring inbound packets", net_device);
1291
1015
                goto out;
1292
1016
        }
1296
1020
                                           &bytes_recvd, &request_id);
1297
1021
                if (ret == 0) {
1298
1022
                        if (bytes_recvd > 0) {
1299
 
                                DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1300
 
                                           bytes_recvd, request_id);
1301
 
 
1302
1023
                                desc = (struct vmpacket_descriptor *)buffer;
1303
 
                                switch (desc->Type) {
1304
 
                                case VmbusPacketTypeCompletion:
 
1024
                                switch (desc->type) {
 
1025
                                case VM_PKT_COMP:
1305
1026
                                        netvsc_send_completion(device, desc);
1306
1027
                                        break;
1307
1028
 
1308
 
                                case VmbusPacketTypeDataUsingTransferPages:
 
1029
                                case VM_PKT_DATA_USING_XFER_PAGES:
1309
1030
                                        netvsc_receive(device, desc);
1310
1031
                                        break;
1311
1032
 
1312
1033
                                default:
1313
 
                                        DPRINT_ERR(NETVSC,
 
1034
                                        dev_err(&device->device,
1314
1035
                                                   "unhandled packet type %d, "
1315
1036
                                                   "tid %llx len %d\n",
1316
 
                                                   desc->Type, request_id,
 
1037
                                                   desc->type, request_id,
1317
1038
                                                   bytes_recvd);
1318
1039
                                        break;
1319
1040
                                }
1339
1060
                        buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1340
1061
                        if (buffer == NULL) {
1341
1062
                                /* Try again next time around */
1342
 
                                DPRINT_ERR(NETVSC,
 
1063
                                dev_err(&device->device,
1343
1064
                                           "unable to allocate buffer of size "
1344
1065
                                           "(%d)!!", bytes_recvd);
1345
1066
                                break;
1354
1075
        kfree(buffer);
1355
1076
        return;
1356
1077
}
 
1078
 
 
1079
/*
 
1080
 * netvsc_device_add - Callback when the device belonging to this
 
1081
 * driver is added
 
1082
 */
 
1083
int netvsc_device_add(struct hv_device *device, void *additional_info)
 
1084
{
 
1085
        int ret = 0;
 
1086
        int i;
 
1087
        int ring_size =
 
1088
        ((struct netvsc_device_info *)additional_info)->ring_size;
 
1089
        struct netvsc_device *net_device;
 
1090
        struct hv_netvsc_packet *packet, *pos;
 
1091
 
 
1092
        net_device = alloc_net_device(device);
 
1093
        if (!net_device) {
 
1094
                ret = -1;
 
1095
                goto cleanup;
 
1096
        }
 
1097
 
 
1098
        /* Initialize the NetVSC channel extension */
 
1099
        net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
 
1100
        spin_lock_init(&net_device->recv_pkt_list_lock);
 
1101
 
 
1102
        net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
 
1103
 
 
1104
        INIT_LIST_HEAD(&net_device->recv_pkt_list);
 
1105
 
 
1106
        for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
 
1107
                packet = kzalloc(sizeof(struct hv_netvsc_packet) +
 
1108
                                 (NETVSC_RECEIVE_SG_COUNT *
 
1109
                                  sizeof(struct hv_page_buffer)), GFP_KERNEL);
 
1110
                if (!packet)
 
1111
                        break;
 
1112
 
 
1113
                list_add_tail(&packet->list_ent,
 
1114
                              &net_device->recv_pkt_list);
 
1115
        }
 
1116
        init_completion(&net_device->channel_init_wait);
 
1117
 
 
1118
        /* Open the channel */
 
1119
        ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
 
1120
                         ring_size * PAGE_SIZE, NULL, 0,
 
1121
                         netvsc_channel_cb, device);
 
1122
 
 
1123
        if (ret != 0) {
 
1124
                dev_err(&device->device, "unable to open channel: %d", ret);
 
1125
                ret = -1;
 
1126
                goto cleanup;
 
1127
        }
 
1128
 
 
1129
        /* Channel is opened */
 
1130
        pr_info("hv_netvsc channel opened successfully");
 
1131
 
 
1132
        /* Connect with the NetVsp */
 
1133
        ret = netvsc_connect_vsp(device);
 
1134
        if (ret != 0) {
 
1135
                dev_err(&device->device,
 
1136
                        "unable to connect to NetVSP - %d", ret);
 
1137
                ret = -1;
 
1138
                goto close;
 
1139
        }
 
1140
 
 
1141
        return ret;
 
1142
 
 
1143
close:
 
1144
        /* Now, we can close the channel safely */
 
1145
        vmbus_close(device->channel);
 
1146
 
 
1147
cleanup:
 
1148
 
 
1149
        if (net_device) {
 
1150
                list_for_each_entry_safe(packet, pos,
 
1151
                                         &net_device->recv_pkt_list,
 
1152
                                         list_ent) {
 
1153
                        list_del(&packet->list_ent);
 
1154
                        kfree(packet);
 
1155
                }
 
1156
 
 
1157
                release_outbound_net_device(device);
 
1158
                release_inbound_net_device(device);
 
1159
 
 
1160
                free_net_device(net_device);
 
1161
        }
 
1162
 
 
1163
        return ret;
 
1164
}
 
1165
 
 
1166
/*
 
1167
 * netvsc_initialize - Main entry point
 
1168
 */
 
1169
int netvsc_initialize(struct hv_driver *drv)
 
1170
{
 
1171
 
 
1172
        drv->name = driver_name;
 
1173
        memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));
 
1174
 
 
1175
        return 0;
 
1176
}