Staging: hv: remove struct vmbus_channel_interface
[pandora-kernel.git] / drivers / staging / hv / blkvsc_drv.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/blkdev.h>
25 #include <linux/major.h>
26 #include <linux/delay.h>
27 #include <linux/hdreg.h>
28 #include <linux/smp_lock.h>
29 #include <linux/slab.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_dbg.h>
34 #include "osd.h"
35 #include "logging.h"
36 #include "version_info.h"
37 #include "vmbus.h"
38 #include "storvsc_api.h"
39
40
41 #define BLKVSC_MINORS   64
42
43 enum blkvsc_device_type {
44         UNKNOWN_DEV_TYPE,
45         HARDDISK_TYPE,
46         DVD_TYPE,
47 };
48
49 /*
50  * This request ties the struct request and struct
51  * blkvsc_request/hv_storvsc_request together A struct request may be
52  * represented by 1 or more struct blkvsc_request
53  */
54 struct blkvsc_request_group {
55         int outstanding;
56         int status;
57         struct list_head blkvsc_req_list;       /* list of blkvsc_requests */
58 };
59
60 struct blkvsc_request {
61         /* blkvsc_request_group.blkvsc_req_list */
62         struct list_head req_entry;
63
64         /* block_device_context.pending_list */
65         struct list_head pend_entry;
66
67         /* This may be null if we generate a request internally */
68         struct request *req;
69
70         struct block_device_context *dev;
71
72         /* The group this request is part of. Maybe null */
73         struct blkvsc_request_group *group;
74
75         wait_queue_head_t wevent;
76         int cond;
77
78         int write;
79         sector_t sector_start;
80         unsigned long sector_count;
81
82         unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
83         unsigned char cmd_len;
84         unsigned char cmnd[MAX_COMMAND_SIZE];
85
86         struct hv_storvsc_request request;
87         /*
88          * !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap,
89          * because - The extension buffer falls right here and is pointed to by
90          * request.Extension;
91          * Which sounds like a horrible idea, who designed this?
92          */
93 };
94
95 /* Per device structure */
96 struct block_device_context {
97         /* point back to our device context */
98         struct vm_device *device_ctx;
99         struct kmem_cache *request_pool;
100         spinlock_t lock;
101         struct gendisk *gd;
102         enum blkvsc_device_type device_type;
103         struct list_head pending_list;
104
105         unsigned char device_id[64];
106         unsigned int device_id_len;
107         int num_outstanding_reqs;
108         int shutting_down;
109         int media_not_present;
110         unsigned int sector_size;
111         sector_t capacity;
112         unsigned int port;
113         unsigned char path;
114         unsigned char target;
115         int users;
116 };
117
118 /* Per driver */
119 struct blkvsc_driver_context {
120         /* !! These must be the first 2 fields !! */
121         /* FIXME this is a bug! */
122         struct driver_context drv_ctx;
123         struct storvsc_driver_object drv_obj;
124 };
125
126 /* Static decl */
127 static int blkvsc_probe(struct device *dev);
128 static int blkvsc_remove(struct device *device);
129 static void blkvsc_shutdown(struct device *device);
130
131 static int blkvsc_open(struct block_device *bdev,  fmode_t mode);
132 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
133 static int blkvsc_media_changed(struct gendisk *gd);
134 static int blkvsc_revalidate_disk(struct gendisk *gd);
135 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
136 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
137                         unsigned cmd, unsigned long argument);
138 static void blkvsc_request(struct request_queue *queue);
139 static void blkvsc_request_completion(struct hv_storvsc_request *request);
140 static int blkvsc_do_request(struct block_device_context *blkdev,
141                              struct request *req);
142 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
143                 void (*request_completion)(struct hv_storvsc_request *));
144 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
145 static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
146 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
147 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
148 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
149 static int blkvsc_do_flush(struct block_device_context *blkdev);
150 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
151 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
152
153 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
154 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
155 MODULE_PARM_DESC(ring_size, "Ring buffer size (in bytes)");
156
157 /* The one and only one */
158 static struct blkvsc_driver_context g_blkvsc_drv;
159
160 static const struct block_device_operations block_ops = {
161         .owner = THIS_MODULE,
162         .open = blkvsc_open,
163         .release = blkvsc_release,
164         .media_changed = blkvsc_media_changed,
165         .revalidate_disk = blkvsc_revalidate_disk,
166         .getgeo = blkvsc_getgeo,
167         .ioctl  = blkvsc_ioctl,
168 };
169
170 /*
171  * blkvsc_drv_init -  BlkVsc driver initialization.
172  */
173 static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
174 {
175         struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
176         struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
177         int ret;
178
179         storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
180
181         /* Callback to client driver to complete the initialization */
182         drv_init(&storvsc_drv_obj->Base);
183
184         drv_ctx->driver.name = storvsc_drv_obj->Base.name;
185         memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
186                sizeof(struct hv_guid));
187
188         drv_ctx->probe = blkvsc_probe;
189         drv_ctx->remove = blkvsc_remove;
190         drv_ctx->shutdown = blkvsc_shutdown;
191
192         /* The driver belongs to vmbus */
193         ret = vmbus_child_driver_register(drv_ctx);
194
195         return ret;
196 }
197
198 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
199 {
200         struct device **curr = (struct device **)data;
201         *curr = dev;
202         return 1; /* stop iterating */
203 }
204
205 static void blkvsc_drv_exit(void)
206 {
207         struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
208         struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
209         struct device *current_dev;
210         int ret;
211
212         while (1) {
213                 current_dev = NULL;
214
215                 /* Get the device */
216                 ret = driver_for_each_device(&drv_ctx->driver, NULL,
217                                              (void *) &current_dev,
218                                              blkvsc_drv_exit_cb);
219
220                 if (ret)
221                         DPRINT_WARN(BLKVSC_DRV,
222                                     "driver_for_each_device returned %d", ret);
223
224
225                 if (current_dev == NULL)
226                         break;
227
228                 /* Initiate removal from the top-down */
229                 device_unregister(current_dev);
230         }
231
232         if (storvsc_drv_obj->Base.OnCleanup)
233                 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
234
235         vmbus_child_driver_unregister(drv_ctx);
236
237         return;
238 }
239
240 /*
241  * blkvsc_probe - Add a new device for this driver
242  */
243 static int blkvsc_probe(struct device *device)
244 {
245         struct driver_context *driver_ctx =
246                                 driver_to_driver_context(device->driver);
247         struct blkvsc_driver_context *blkvsc_drv_ctx =
248                                 (struct blkvsc_driver_context *)driver_ctx;
249         struct storvsc_driver_object *storvsc_drv_obj =
250                                 &blkvsc_drv_ctx->drv_obj;
251         struct vm_device *device_ctx = device_to_vm_device(device);
252         struct hv_device *device_obj = &device_ctx->device_obj;
253
254         struct block_device_context *blkdev = NULL;
255         struct storvsc_device_info device_info;
256         int major = 0;
257         int devnum = 0;
258         int ret = 0;
259         static int ide0_registered;
260         static int ide1_registered;
261
262         DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
263
264         if (!storvsc_drv_obj->Base.OnDeviceAdd) {
265                 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
266                 ret = -1;
267                 goto Cleanup;
268         }
269
270         blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
271         if (!blkdev) {
272                 ret = -ENOMEM;
273                 goto Cleanup;
274         }
275
276         INIT_LIST_HEAD(&blkdev->pending_list);
277
278         /* Initialize what we can here */
279         spin_lock_init(&blkdev->lock);
280
281         /* ASSERT(sizeof(struct blkvsc_request_group) <= */
282         /*      sizeof(struct blkvsc_request)); */
283
284         blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
285                                         sizeof(struct blkvsc_request) +
286                                         storvsc_drv_obj->RequestExtSize, 0,
287                                         SLAB_HWCACHE_ALIGN, NULL);
288         if (!blkdev->request_pool) {
289                 ret = -ENOMEM;
290                 goto Cleanup;
291         }
292
293
294         /* Call to the vsc driver to add the device */
295         ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
296         if (ret != 0) {
297                 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
298                 goto Cleanup;
299         }
300
301         blkdev->device_ctx = device_ctx;
302         /* this identified the device 0 or 1 */
303         blkdev->target = device_info.TargetId;
304         /* this identified the ide ctrl 0 or 1 */
305         blkdev->path = device_info.PathId;
306
307         dev_set_drvdata(device, blkdev);
308
309         /* Calculate the major and device num */
310         if (blkdev->path == 0) {
311                 major = IDE0_MAJOR;
312                 devnum = blkdev->path + blkdev->target;         /* 0 or 1 */
313
314                 if (!ide0_registered) {
315                         ret = register_blkdev(major, "ide");
316                         if (ret != 0) {
317                                 DPRINT_ERR(BLKVSC_DRV,
318                                            "register_blkdev() failed! ret %d",
319                                            ret);
320                                 goto Remove;
321                         }
322
323                         ide0_registered = 1;
324                 }
325         } else if (blkdev->path == 1) {
326                 major = IDE1_MAJOR;
327                 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
328
329                 if (!ide1_registered) {
330                         ret = register_blkdev(major, "ide");
331                         if (ret != 0) {
332                                 DPRINT_ERR(BLKVSC_DRV,
333                                            "register_blkdev() failed! ret %d",
334                                            ret);
335                                 goto Remove;
336                         }
337
338                         ide1_registered = 1;
339                 }
340         } else {
341                 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
342                 ret = -1;
343                 goto Cleanup;
344         }
345
346         DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
347
348         blkdev->gd = alloc_disk(BLKVSC_MINORS);
349         if (!blkdev->gd) {
350                 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
351                 ret = -1;
352                 goto Cleanup;
353         }
354
355         blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
356
357         blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
358         blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
359         blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
360         blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
361         blk_queue_dma_alignment(blkdev->gd->queue, 511);
362
363         blkdev->gd->major = major;
364         if (devnum == 1 || devnum == 3)
365                 blkdev->gd->first_minor = BLKVSC_MINORS;
366         else
367                 blkdev->gd->first_minor = 0;
368         blkdev->gd->fops = &block_ops;
369         blkdev->gd->private_data = blkdev;
370         sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
371
372         blkvsc_do_inquiry(blkdev);
373         if (blkdev->device_type == DVD_TYPE) {
374                 set_disk_ro(blkdev->gd, 1);
375                 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
376                 blkvsc_do_read_capacity(blkdev);
377         } else {
378                 blkvsc_do_read_capacity16(blkdev);
379         }
380
381         set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
382         blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
383         /* go! */
384         add_disk(blkdev->gd);
385
386         DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
387                     blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
388                     blkdev->sector_size);
389
390         return ret;
391
392 Remove:
393         storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
394
395 Cleanup:
396         if (blkdev) {
397                 if (blkdev->request_pool) {
398                         kmem_cache_destroy(blkdev->request_pool);
399                         blkdev->request_pool = NULL;
400                 }
401                 kfree(blkdev);
402                 blkdev = NULL;
403         }
404
405         return ret;
406 }
407
408 static void blkvsc_shutdown(struct device *device)
409 {
410         struct block_device_context *blkdev = dev_get_drvdata(device);
411         unsigned long flags;
412
413         if (!blkdev)
414                 return;
415
416         DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
417                    blkdev->users, blkdev->gd->disk_name);
418
419         spin_lock_irqsave(&blkdev->lock, flags);
420
421         blkdev->shutting_down = 1;
422
423         blk_stop_queue(blkdev->gd->queue);
424
425         spin_unlock_irqrestore(&blkdev->lock, flags);
426
427         while (blkdev->num_outstanding_reqs) {
428                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
429                             blkdev->num_outstanding_reqs);
430                 udelay(100);
431         }
432
433         blkvsc_do_flush(blkdev);
434
435         spin_lock_irqsave(&blkdev->lock, flags);
436
437         blkvsc_cancel_pending_reqs(blkdev);
438
439         spin_unlock_irqrestore(&blkdev->lock, flags);
440 }
441
442 static int blkvsc_do_flush(struct block_device_context *blkdev)
443 {
444         struct blkvsc_request *blkvsc_req;
445
446         DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
447
448         if (blkdev->device_type != HARDDISK_TYPE)
449                 return 0;
450
451         blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
452         if (!blkvsc_req)
453                 return -ENOMEM;
454
455         memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
456         init_waitqueue_head(&blkvsc_req->wevent);
457         blkvsc_req->dev = blkdev;
458         blkvsc_req->req = NULL;
459         blkvsc_req->write = 0;
460
461         blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
462         blkvsc_req->request.DataBuffer.Offset = 0;
463         blkvsc_req->request.DataBuffer.Length = 0;
464
465         blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
466         blkvsc_req->cmd_len = 10;
467
468         /*
469          * Set this here since the completion routine may be invoked and
470          * completed before we return
471          */
472         blkvsc_req->cond = 0;
473         blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
474
475         wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
476
477         kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
478
479         return 0;
480 }
481
482 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
483 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
484 {
485         struct blkvsc_request *blkvsc_req;
486         struct page *page_buf;
487         unsigned char *buf;
488         unsigned char device_type;
489
490         DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
491
492         blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
493         if (!blkvsc_req)
494                 return -ENOMEM;
495
496         memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
497         page_buf = alloc_page(GFP_KERNEL);
498         if (!page_buf) {
499                 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
500                 return -ENOMEM;
501         }
502
503         init_waitqueue_head(&blkvsc_req->wevent);
504         blkvsc_req->dev = blkdev;
505         blkvsc_req->req = NULL;
506         blkvsc_req->write = 0;
507
508         blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
509         blkvsc_req->request.DataBuffer.Offset = 0;
510         blkvsc_req->request.DataBuffer.Length = 64;
511
512         blkvsc_req->cmnd[0] = INQUIRY;
513         blkvsc_req->cmnd[1] = 0x1;              /* Get product data */
514         blkvsc_req->cmnd[2] = 0x83;             /* mode page 83 */
515         blkvsc_req->cmnd[4] = 64;
516         blkvsc_req->cmd_len = 6;
517
518         /*
519          * Set this here since the completion routine may be invoked and
520          * completed before we return
521          */
522         blkvsc_req->cond = 0;
523
524         blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
525
526         DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
527                    blkvsc_req, blkvsc_req->cond);
528
529         wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
530
531         buf = kmap(page_buf);
532
533         /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
534         /* be to le */
535         device_type = buf[0] & 0x1F;
536
537         if (device_type == 0x0) {
538                 blkdev->device_type = HARDDISK_TYPE;
539         } else if (device_type == 0x5) {
540                 blkdev->device_type = DVD_TYPE;
541         } else {
542                 /* TODO: this is currently unsupported device type */
543                 blkdev->device_type = UNKNOWN_DEV_TYPE;
544         }
545
546         DPRINT_DBG(BLKVSC_DRV, "device type %d\n", device_type);
547
548         blkdev->device_id_len = buf[7];
549         if (blkdev->device_id_len > 64)
550                 blkdev->device_id_len = 64;
551
552         memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
553         /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
554          * blkdev->device_id_len); */
555
556         kunmap(page_buf);
557
558         __free_page(page_buf);
559
560         kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
561
562         return 0;
563 }
564
565 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
566 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
567 {
568         struct blkvsc_request *blkvsc_req;
569         struct page *page_buf;
570         unsigned char *buf;
571         struct scsi_sense_hdr sense_hdr;
572
573         DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
574
575         blkdev->sector_size = 0;
576         blkdev->capacity = 0;
577         blkdev->media_not_present = 0; /* assume a disk is present */
578
579         blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
580         if (!blkvsc_req)
581                 return -ENOMEM;
582
583         memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
584         page_buf = alloc_page(GFP_KERNEL);
585         if (!page_buf) {
586                 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
587                 return -ENOMEM;
588         }
589
590         init_waitqueue_head(&blkvsc_req->wevent);
591         blkvsc_req->dev = blkdev;
592         blkvsc_req->req = NULL;
593         blkvsc_req->write = 0;
594
595         blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
596         blkvsc_req->request.DataBuffer.Offset = 0;
597         blkvsc_req->request.DataBuffer.Length = 8;
598
599         blkvsc_req->cmnd[0] = READ_CAPACITY;
600         blkvsc_req->cmd_len = 16;
601
602         /*
603          * Set this here since the completion routine may be invoked
604          * and completed before we return
605          */
606         blkvsc_req->cond = 0;
607
608         blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
609
610         DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
611                    blkvsc_req, blkvsc_req->cond);
612
613         wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
614
615         /* check error */
616         if (blkvsc_req->request.Status) {
617                 scsi_normalize_sense(blkvsc_req->sense_buffer,
618                                      SCSI_SENSE_BUFFERSIZE, &sense_hdr);
619
620                 if (sense_hdr.asc == 0x3A) {
621                         /* Medium not present */
622                         blkdev->media_not_present = 1;
623                 }
624                 return 0;
625         }
626         buf = kmap(page_buf);
627
628         /* be to le */
629         blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
630                             (buf[2] << 8) | buf[3]) + 1;
631         blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
632                               (buf[6] << 8) | buf[7];
633
634         kunmap(page_buf);
635
636         __free_page(page_buf);
637
638         kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
639
640         return 0;
641 }
642
643 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
644 {
645         struct blkvsc_request *blkvsc_req;
646         struct page *page_buf;
647         unsigned char *buf;
648         struct scsi_sense_hdr sense_hdr;
649
650         DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
651
652         blkdev->sector_size = 0;
653         blkdev->capacity = 0;
654         blkdev->media_not_present = 0; /* assume a disk is present */
655
656         blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
657         if (!blkvsc_req)
658                 return -ENOMEM;
659
660         memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
661         page_buf = alloc_page(GFP_KERNEL);
662         if (!page_buf) {
663                 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
664                 return -ENOMEM;
665         }
666
667         init_waitqueue_head(&blkvsc_req->wevent);
668         blkvsc_req->dev = blkdev;
669         blkvsc_req->req = NULL;
670         blkvsc_req->write = 0;
671
672         blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
673         blkvsc_req->request.DataBuffer.Offset = 0;
674         blkvsc_req->request.DataBuffer.Length = 12;
675
676         blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
677         blkvsc_req->cmd_len = 16;
678
679         /*
680          * Set this here since the completion routine may be invoked
681          * and completed before we return
682          */
683         blkvsc_req->cond = 0;
684
685         blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
686
687         DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
688                    blkvsc_req, blkvsc_req->cond);
689
690         wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
691
692         /* check error */
693         if (blkvsc_req->request.Status) {
694                 scsi_normalize_sense(blkvsc_req->sense_buffer,
695                                      SCSI_SENSE_BUFFERSIZE, &sense_hdr);
696                 if (sense_hdr.asc == 0x3A) {
697                         /* Medium not present */
698                         blkdev->media_not_present = 1;
699                 }
700                 return 0;
701         }
702         buf = kmap(page_buf);
703
704         /* be to le */
705         blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
706         blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
707
708 #if 0
709         blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
710                             (buf[2] << 8) | buf[3]) + 1;
711         blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
712                               (buf[6] << 8) | buf[7];
713 #endif
714
715         kunmap(page_buf);
716
717         __free_page(page_buf);
718
719         kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
720
721         return 0;
722 }
723
724 /*
725  * blkvsc_remove() - Callback when our device is removed
726  */
727 static int blkvsc_remove(struct device *device)
728 {
729         struct driver_context *driver_ctx =
730                                 driver_to_driver_context(device->driver);
731         struct blkvsc_driver_context *blkvsc_drv_ctx =
732                                 (struct blkvsc_driver_context *)driver_ctx;
733         struct storvsc_driver_object *storvsc_drv_obj =
734                                 &blkvsc_drv_ctx->drv_obj;
735         struct vm_device *device_ctx = device_to_vm_device(device);
736         struct hv_device *device_obj = &device_ctx->device_obj;
737         struct block_device_context *blkdev = dev_get_drvdata(device);
738         unsigned long flags;
739         int ret;
740
741         DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
742
743         if (!storvsc_drv_obj->Base.OnDeviceRemove)
744                 return -1;
745
746         /*
747          * Call to the vsc driver to let it know that the device is being
748          * removed
749          */
750         ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
751         if (ret != 0) {
752                 /* TODO: */
753                 DPRINT_ERR(BLKVSC_DRV,
754                            "unable to remove blkvsc device (ret %d)", ret);
755         }
756
757         /* Get to a known state */
758         spin_lock_irqsave(&blkdev->lock, flags);
759
760         blkdev->shutting_down = 1;
761
762         blk_stop_queue(blkdev->gd->queue);
763
764         spin_unlock_irqrestore(&blkdev->lock, flags);
765
766         while (blkdev->num_outstanding_reqs) {
767                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
768                             blkdev->num_outstanding_reqs);
769                 udelay(100);
770         }
771
772         blkvsc_do_flush(blkdev);
773
774         spin_lock_irqsave(&blkdev->lock, flags);
775
776         blkvsc_cancel_pending_reqs(blkdev);
777
778         spin_unlock_irqrestore(&blkdev->lock, flags);
779
780         blk_cleanup_queue(blkdev->gd->queue);
781
782         del_gendisk(blkdev->gd);
783
784         kmem_cache_destroy(blkdev->request_pool);
785
786         kfree(blkdev);
787
788         return ret;
789 }
790
791 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
792 {
793         /* ASSERT(blkvsc_req->req); */
794         /* ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8)); */
795
796         blkvsc_req->cmd_len = 16;
797
798         if (blkvsc_req->sector_start > 0xffffffff) {
799                 if (rq_data_dir(blkvsc_req->req)) {
800                         blkvsc_req->write = 1;
801                         blkvsc_req->cmnd[0] = WRITE_16;
802                 } else {
803                         blkvsc_req->write = 0;
804                         blkvsc_req->cmnd[0] = READ_16;
805                 }
806
807                 blkvsc_req->cmnd[1] |=
808                         (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
809
810                 *(unsigned long long *)&blkvsc_req->cmnd[2] =
811                                 cpu_to_be64(blkvsc_req->sector_start);
812                 *(unsigned int *)&blkvsc_req->cmnd[10] =
813                                 cpu_to_be32(blkvsc_req->sector_count);
814         } else if ((blkvsc_req->sector_count > 0xff) ||
815                    (blkvsc_req->sector_start > 0x1fffff)) {
816                 if (rq_data_dir(blkvsc_req->req)) {
817                         blkvsc_req->write = 1;
818                         blkvsc_req->cmnd[0] = WRITE_10;
819                 } else {
820                         blkvsc_req->write = 0;
821                         blkvsc_req->cmnd[0] = READ_10;
822                 }
823
824                 blkvsc_req->cmnd[1] |=
825                         (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
826
827                 *(unsigned int *)&blkvsc_req->cmnd[2] =
828                                 cpu_to_be32(blkvsc_req->sector_start);
829                 *(unsigned short *)&blkvsc_req->cmnd[7] =
830                                 cpu_to_be16(blkvsc_req->sector_count);
831         } else {
832                 if (rq_data_dir(blkvsc_req->req)) {
833                         blkvsc_req->write = 1;
834                         blkvsc_req->cmnd[0] = WRITE_6;
835                 } else {
836                         blkvsc_req->write = 0;
837                         blkvsc_req->cmnd[0] = READ_6;
838                 }
839
840                 *(unsigned int *)&blkvsc_req->cmnd[1] =
841                                 cpu_to_be32(blkvsc_req->sector_start) >> 8;
842                 blkvsc_req->cmnd[1] &= 0x1f;
843                 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
844         }
845 }
846
847 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
848                         void (*request_completion)(struct hv_storvsc_request *))
849 {
850         struct block_device_context *blkdev = blkvsc_req->dev;
851         struct vm_device *device_ctx = blkdev->device_ctx;
852         struct driver_context *driver_ctx =
853                         driver_to_driver_context(device_ctx->device.driver);
854         struct blkvsc_driver_context *blkvsc_drv_ctx =
855                         (struct blkvsc_driver_context *)driver_ctx;
856         struct storvsc_driver_object *storvsc_drv_obj =
857                         &blkvsc_drv_ctx->drv_obj;
858         struct hv_storvsc_request *storvsc_req;
859         int ret;
860
861         DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
862                    "req %p type %s start_sector %lu count %ld offset %d "
863                    "len %d\n", blkvsc_req,
864                    (blkvsc_req->write) ? "WRITE" : "READ",
865                    (unsigned long) blkvsc_req->sector_start,
866                    blkvsc_req->sector_count,
867                    blkvsc_req->request.DataBuffer.Offset,
868                    blkvsc_req->request.DataBuffer.Length);
869 #if 0
870         for (i = 0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++) {
871                 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
872                            "req %p pfn[%d] %llx\n",
873                            blkvsc_req, i,
874                            blkvsc_req->request.DataBuffer.PfnArray[i]);
875         }
876 #endif
877
878         storvsc_req = &blkvsc_req->request;
879         storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
880                                           sizeof(struct blkvsc_request));
881
882         storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
883
884         storvsc_req->OnIOCompletion = request_completion;
885         storvsc_req->Context = blkvsc_req;
886
887         storvsc_req->Host = blkdev->port;
888         storvsc_req->Bus = blkdev->path;
889         storvsc_req->TargetId = blkdev->target;
890         storvsc_req->LunId = 0;  /* this is not really used at all */
891
892         storvsc_req->CdbLen = blkvsc_req->cmd_len;
893         storvsc_req->Cdb = blkvsc_req->cmnd;
894
895         storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
896         storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
897
898         ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
899                                            &blkvsc_req->request);
900         if (ret == 0)
901                 blkdev->num_outstanding_reqs++;
902
903         return ret;
904 }
905
906 /*
907  * We break the request into 1 or more blkvsc_requests and submit
908  * them.  If we cant submit them all, we put them on the
909  * pending_list. The blkvsc_request() will work on the pending_list.
910  */
911 static int blkvsc_do_request(struct block_device_context *blkdev,
912                              struct request *req)
913 {
914         struct bio *bio = NULL;
915         struct bio_vec *bvec = NULL;
916         struct bio_vec *prev_bvec = NULL;
917         struct blkvsc_request *blkvsc_req = NULL;
918         struct blkvsc_request *tmp;
919         int databuf_idx = 0;
920         int seg_idx = 0;
921         sector_t start_sector;
922         unsigned long num_sectors = 0;
923         int ret = 0;
924         int pending = 0;
925         struct blkvsc_request_group *group = NULL;
926
927         DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu\n", blkdev, req,
928                   (unsigned long)blk_rq_pos(req));
929
930         /* Create a group to tie req to list of blkvsc_reqs */
931         group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
932         if (!group)
933                 return -ENOMEM;
934
935         INIT_LIST_HEAD(&group->blkvsc_req_list);
936         group->outstanding = group->status = 0;
937
938         start_sector = blk_rq_pos(req);
939
940         /* foreach bio in the request */
941         if (req->bio) {
942                 for (bio = req->bio; bio; bio = bio->bi_next) {
943                         /*
944                          * Map this bio into an existing or new storvsc request
945                          */
946                         bio_for_each_segment(bvec, bio, seg_idx) {
947                                 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
948                                            "- req %p bio %p bvec %p seg_idx %d "
949                                            "databuf_idx %d\n", req, bio, bvec,
950                                            seg_idx, databuf_idx);
951
952                                 /* Get a new storvsc request */
953                                 /* 1st-time */
954                                 if ((!blkvsc_req) ||
955                                     (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
956                                     /* hole at the begin of page */
957                                     || (bvec->bv_offset != 0) ||
958                                     /* hold at the end of page */
959                                     (prev_bvec &&
960                                      (prev_bvec->bv_len != PAGE_SIZE))) {
961                                         /* submit the prev one */
962                                         if (blkvsc_req) {
963                                                 blkvsc_req->sector_start = start_sector;
964                                                 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
965
966                                                 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
967                                                 blkvsc_init_rw(blkvsc_req);
968                                         }
969
970                                         /*
971                                          * Create new blkvsc_req to represent
972                                          * the current bvec
973                                          */
974                                         blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
975                                         if (!blkvsc_req) {
976                                                 /* free up everything */
977                                                 list_for_each_entry_safe(
978                                                         blkvsc_req, tmp,
979                                                         &group->blkvsc_req_list,
980                                                         req_entry) {
981                                                         list_del(&blkvsc_req->req_entry);
982                                                         kmem_cache_free(blkdev->request_pool, blkvsc_req);
983                                                 }
984
985                                                 kmem_cache_free(blkdev->request_pool, group);
986                                                 return -ENOMEM;
987                                         }
988
989                                         memset(blkvsc_req, 0,
990                                                sizeof(struct blkvsc_request));
991
992                                         blkvsc_req->dev = blkdev;
993                                         blkvsc_req->req = req;
994                                         blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
995                                         blkvsc_req->request.DataBuffer.Length = 0;
996
997                                         /* Add to the group */
998                                         blkvsc_req->group = group;
999                                         blkvsc_req->group->outstanding++;
1000                                         list_add_tail(&blkvsc_req->req_entry,
1001                                                 &blkvsc_req->group->blkvsc_req_list);
1002
1003                                         start_sector += num_sectors;
1004                                         num_sectors = 0;
1005                                         databuf_idx = 0;
1006                                 }
1007
1008                                 /* Add the curr bvec/segment to the curr blkvsc_req */
1009                                 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1010                                 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1011
1012                                 prev_bvec = bvec;
1013
1014                                 databuf_idx++;
1015                                 num_sectors += bvec->bv_len >> 9;
1016
1017                         } /* bio_for_each_segment */
1018
1019                 } /* rq_for_each_bio */
1020         }
1021
1022         /* Handle the last one */
1023         if (blkvsc_req) {
1024                 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1025                            blkdev, req, blkvsc_req->group,
1026                            blkvsc_req->group->outstanding);
1027
1028                 blkvsc_req->sector_start = start_sector;
1029                 sector_div(blkvsc_req->sector_start,
1030                            (blkdev->sector_size >> 9));
1031
1032                 blkvsc_req->sector_count = num_sectors /
1033                                            (blkdev->sector_size >> 9);
1034
1035                 blkvsc_init_rw(blkvsc_req);
1036         }
1037
1038         list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1039                 if (pending) {
1040                         DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1041                                    "pending_list - blkvsc_req %p start_sect %lu"
1042                                    " sect_count %ld (%lu %ld)\n", blkvsc_req,
1043                                    (unsigned long)blkvsc_req->sector_start,
1044                                    blkvsc_req->sector_count,
1045                                    (unsigned long)start_sector,
1046                                    (unsigned long)num_sectors);
1047
1048                         list_add_tail(&blkvsc_req->pend_entry,
1049                                       &blkdev->pending_list);
1050                 } else {
1051                         ret = blkvsc_submit_request(blkvsc_req,
1052                                                     blkvsc_request_completion);
1053                         if (ret == -1) {
1054                                 pending = 1;
1055                                 list_add_tail(&blkvsc_req->pend_entry,
1056                                               &blkdev->pending_list);
1057                         }
1058
1059                         DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1060                                    "start_sect %lu sect_count %ld (%lu %ld) "
1061                                    "ret %d\n", blkvsc_req,
1062                                    (unsigned long)blkvsc_req->sector_start,
1063                                    blkvsc_req->sector_count,
1064                                    (unsigned long)start_sector,
1065                                    num_sectors, ret);
1066                 }
1067         }
1068
1069         return pending;
1070 }
1071
1072 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1073 {
1074         struct blkvsc_request *blkvsc_req =
1075                         (struct blkvsc_request *)request->Context;
1076         struct block_device_context *blkdev =
1077                         (struct block_device_context *)blkvsc_req->dev;
1078         struct scsi_sense_hdr sense_hdr;
1079
1080         DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1081                    blkvsc_req);
1082
1083         blkdev->num_outstanding_reqs--;
1084
1085         if (blkvsc_req->request.Status)
1086                 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1087                                          SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1088                         scsi_print_sense_hdr("blkvsc", &sense_hdr);
1089
1090         blkvsc_req->cond = 1;
1091         wake_up_interruptible(&blkvsc_req->wevent);
1092 }
1093
1094 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1095 {
1096         struct blkvsc_request *blkvsc_req =
1097                         (struct blkvsc_request *)request->Context;
1098         struct block_device_context *blkdev =
1099                         (struct block_device_context *)blkvsc_req->dev;
1100         unsigned long flags;
1101         struct blkvsc_request *comp_req, *tmp;
1102
1103         /* ASSERT(blkvsc_req->group); */
1104
1105         DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1106                    "sect_start %lu sect_count %ld len %d group outstd %d "
1107                    "total outstd %d\n",
1108                    blkdev, blkvsc_req, blkvsc_req->group,
1109                    (blkvsc_req->write) ? "WRITE" : "READ",
1110                    (unsigned long)blkvsc_req->sector_start,
1111                    blkvsc_req->sector_count,
1112                    blkvsc_req->request.DataBuffer.Length,
1113                    blkvsc_req->group->outstanding,
1114                    blkdev->num_outstanding_reqs);
1115
1116         spin_lock_irqsave(&blkdev->lock, flags);
1117
1118         blkdev->num_outstanding_reqs--;
1119         blkvsc_req->group->outstanding--;
1120
1121         /*
1122          * Only start processing when all the blkvsc_reqs are
1123          * completed. This guarantees no out-of-order blkvsc_req
1124          * completion when calling end_that_request_first()
1125          */
1126         if (blkvsc_req->group->outstanding == 0) {
1127                 list_for_each_entry_safe(comp_req, tmp,
1128                                          &blkvsc_req->group->blkvsc_req_list,
1129                                          req_entry) {
1130                         DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1131                                    "sect_start %lu sect_count %ld\n",
1132                                    comp_req,
1133                                    (unsigned long)comp_req->sector_start,
1134                                    comp_req->sector_count);
1135
1136                         list_del(&comp_req->req_entry);
1137
1138                         if (!__blk_end_request(comp_req->req,
1139                                 (!comp_req->request.Status ? 0 : -EIO),
1140                                 comp_req->sector_count * blkdev->sector_size)) {
1141                                 /*
1142                                  * All the sectors have been xferred ie the
1143                                  * request is done
1144                                  */
1145                                 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1146                                            comp_req->req);
1147                                 kmem_cache_free(blkdev->request_pool,
1148                                                 comp_req->group);
1149                         }
1150
1151                         kmem_cache_free(blkdev->request_pool, comp_req);
1152                 }
1153
1154                 if (!blkdev->shutting_down) {
1155                         blkvsc_do_pending_reqs(blkdev);
1156                         blk_start_queue(blkdev->gd->queue);
1157                         blkvsc_request(blkdev->gd->queue);
1158                 }
1159         }
1160
1161         spin_unlock_irqrestore(&blkdev->lock, flags);
1162 }
1163
1164 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1165 {
1166         struct blkvsc_request *pend_req, *tmp;
1167         struct blkvsc_request *comp_req, *tmp2;
1168
1169         int ret = 0;
1170
1171         DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1172
1173         /* Flush the pending list first */
1174         list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1175                                  pend_entry) {
1176                 /*
1177                  * The pend_req could be part of a partially completed
1178                  * request. If so, complete those req first until we
1179                  * hit the pend_req
1180                  */
1181                 list_for_each_entry_safe(comp_req, tmp2,
1182                                          &pend_req->group->blkvsc_req_list,
1183                                          req_entry) {
1184                         DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1185                                    "sect_start %lu sect_count %ld\n",
1186                                    comp_req,
1187                                    (unsigned long) comp_req->sector_start,
1188                                    comp_req->sector_count);
1189
1190                         if (comp_req == pend_req)
1191                                 break;
1192
1193                         list_del(&comp_req->req_entry);
1194
1195                         if (comp_req->req) {
1196                                 ret = __blk_end_request(comp_req->req,
1197                                         (!comp_req->request.Status ? 0 : -EIO),
1198                                         comp_req->sector_count *
1199                                         blkdev->sector_size);
1200
1201                                 /* FIXME: shouldn't this do more than return? */
1202                                 if (ret)
1203                                         goto out;
1204                         }
1205
1206                         kmem_cache_free(blkdev->request_pool, comp_req);
1207                 }
1208
1209                 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1210                            pend_req);
1211
1212                 list_del(&pend_req->pend_entry);
1213
1214                 list_del(&pend_req->req_entry);
1215
1216                 if (comp_req->req) {
1217                         if (!__blk_end_request(pend_req->req, -EIO,
1218                                                pend_req->sector_count *
1219                                                blkdev->sector_size)) {
1220                                 /*
1221                                  * All the sectors have been xferred ie the
1222                                  * request is done
1223                                  */
1224                                 DPRINT_DBG(BLKVSC_DRV,
1225                                            "blkvsc_cancel_pending_reqs() - "
1226                                            "req %p COMPLETED\n", pend_req->req);
1227                                 kmem_cache_free(blkdev->request_pool,
1228                                                 pend_req->group);
1229                         }
1230                 }
1231
1232                 kmem_cache_free(blkdev->request_pool, pend_req);
1233         }
1234
1235 out:
1236         return ret;
1237 }
1238
1239 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1240 {
1241         struct blkvsc_request *pend_req, *tmp;
1242         int ret = 0;
1243
1244         /* Flush the pending list first */
1245         list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1246                                  pend_entry) {
1247                 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1248                            pend_req);
1249
1250                 ret = blkvsc_submit_request(pend_req,
1251                                             blkvsc_request_completion);
1252                 if (ret != 0)
1253                         break;
1254                 else
1255                         list_del(&pend_req->pend_entry);
1256         }
1257
1258         return ret;
1259 }
1260
1261 static void blkvsc_request(struct request_queue *queue)
1262 {
1263         struct block_device_context *blkdev = NULL;
1264         struct request *req;
1265         int ret = 0;
1266
1267         DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1268         while ((req = blk_peek_request(queue)) != NULL) {
1269                 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1270
1271                 blkdev = req->rq_disk->private_data;
1272                 if (blkdev->shutting_down || req->cmd_type != REQ_TYPE_FS ||
1273                     blkdev->media_not_present) {
1274                         __blk_end_request_cur(req, 0);
1275                         continue;
1276                 }
1277
1278                 ret = blkvsc_do_pending_reqs(blkdev);
1279
1280                 if (ret != 0) {
1281                         DPRINT_DBG(BLKVSC_DRV,
1282                                    "- stop queue - pending_list not empty\n");
1283                         blk_stop_queue(queue);
1284                         break;
1285                 }
1286
1287                 blk_start_request(req);
1288
1289                 ret = blkvsc_do_request(blkdev, req);
1290                 if (ret > 0) {
1291                         DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1292                         blk_stop_queue(queue);
1293                         break;
1294                 } else if (ret < 0) {
1295                         DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1296                         blk_requeue_request(queue, req);
1297                         blk_stop_queue(queue);
1298                         break;
1299                 }
1300         }
1301 }
1302
1303 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1304 {
1305         struct block_device_context *blkdev = bdev->bd_disk->private_data;
1306
1307         DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1308                    blkdev->gd->disk_name);
1309
1310         lock_kernel();
1311         spin_lock(&blkdev->lock);
1312
1313         if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1314                 spin_unlock(&blkdev->lock);
1315                 check_disk_change(bdev);
1316                 spin_lock(&blkdev->lock);
1317         }
1318
1319         blkdev->users++;
1320
1321         spin_unlock(&blkdev->lock);
1322         unlock_kernel();
1323         return 0;
1324 }
1325
1326 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1327 {
1328         struct block_device_context *blkdev = disk->private_data;
1329
1330         DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1331                    blkdev->gd->disk_name);
1332
1333         lock_kernel();
1334         spin_lock(&blkdev->lock);
1335         if (blkdev->users == 1) {
1336                 spin_unlock(&blkdev->lock);
1337                 blkvsc_do_flush(blkdev);
1338                 spin_lock(&blkdev->lock);
1339         }
1340
1341         blkdev->users--;
1342
1343         spin_unlock(&blkdev->lock);
1344         unlock_kernel();
1345         return 0;
1346 }
1347
1348 static int blkvsc_media_changed(struct gendisk *gd)
1349 {
1350         DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1351         return 1;
1352 }
1353
1354 static int blkvsc_revalidate_disk(struct gendisk *gd)
1355 {
1356         struct block_device_context *blkdev = gd->private_data;
1357
1358         DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1359
1360         if (blkdev->device_type == DVD_TYPE) {
1361                 blkvsc_do_read_capacity(blkdev);
1362                 set_capacity(blkdev->gd, blkdev->capacity *
1363                             (blkdev->sector_size/512));
1364                 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1365         }
1366         return 0;
1367 }
1368
1369 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1370 {
1371         sector_t total_sectors = get_capacity(bd->bd_disk);
1372         sector_t cylinder_times_heads = 0;
1373         sector_t temp = 0;
1374
1375         int sectors_per_track = 0;
1376         int heads = 0;
1377         int cylinders = 0;
1378         int rem = 0;
1379
1380         if (total_sectors > (65535 * 16 * 255))
1381                 total_sectors = (65535 * 16 * 255);
1382
1383         if (total_sectors >= (65535 * 16 * 63)) {
1384                 sectors_per_track = 255;
1385                 heads = 16;
1386
1387                 cylinder_times_heads = total_sectors;
1388                 /* sector_div stores the quotient in cylinder_times_heads */
1389                 rem = sector_div(cylinder_times_heads, sectors_per_track);
1390         } else {
1391                 sectors_per_track = 17;
1392
1393                 cylinder_times_heads = total_sectors;
1394                 /* sector_div stores the quotient in cylinder_times_heads */
1395                 rem = sector_div(cylinder_times_heads, sectors_per_track);
1396
1397                 temp = cylinder_times_heads + 1023;
1398                 /* sector_div stores the quotient in temp */
1399                 rem = sector_div(temp, 1024);
1400
1401                 heads = temp;
1402
1403                 if (heads < 4)
1404                         heads = 4;
1405
1406
1407                 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1408                         sectors_per_track = 31;
1409                         heads = 16;
1410
1411                         cylinder_times_heads = total_sectors;
1412                         /*
1413                          * sector_div stores the quotient in
1414                          * cylinder_times_heads
1415                          */
1416                         rem = sector_div(cylinder_times_heads,
1417                                          sectors_per_track);
1418                 }
1419
1420                 if (cylinder_times_heads >= (heads * 1024)) {
1421                         sectors_per_track = 63;
1422                         heads = 16;
1423
1424                         cylinder_times_heads = total_sectors;
1425                         /*
1426                          * sector_div stores the quotient in
1427                          * cylinder_times_heads
1428                          */
1429                         rem = sector_div(cylinder_times_heads,
1430                                          sectors_per_track);
1431                 }
1432         }
1433
1434         temp = cylinder_times_heads;
1435         /* sector_div stores the quotient in temp */
1436         rem = sector_div(temp, heads);
1437         cylinders = temp;
1438
1439         hg->heads = heads;
1440         hg->sectors = sectors_per_track;
1441         hg->cylinders = cylinders;
1442
1443         DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1444                     sectors_per_track);
1445
1446     return 0;
1447 }
1448
1449 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1450                         unsigned cmd, unsigned long argument)
1451 {
1452 /*      struct block_device_context *blkdev = bd->bd_disk->private_data; */
1453         int ret;
1454
1455         switch (cmd) {
1456         /*
1457          * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1458          * than just a GUID. Commented it out for now.
1459          */
1460 #if 0
1461         case HDIO_GET_IDENTITY:
1462                 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1463                 if (copy_to_user((void __user *)arg, blkdev->device_id,
1464                                  blkdev->device_id_len))
1465                         ret = -EFAULT;
1466                 break;
1467 #endif
1468         default:
1469                 ret = -EINVAL;
1470                 break;
1471         }
1472
1473         return ret;
1474 }
1475
1476 static int __init blkvsc_init(void)
1477 {
1478         int ret;
1479
1480         BUILD_BUG_ON(sizeof(sector_t) != 8);
1481
1482         DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1483
1484         ret = blkvsc_drv_init(BlkVscInitialize);
1485
1486         return ret;
1487 }
1488
1489 static void __exit blkvsc_exit(void)
1490 {
1491         blkvsc_drv_exit();
1492 }
1493
1494 MODULE_LICENSE("GPL");
1495 MODULE_VERSION(HV_DRV_VERSION);
1496 MODULE_DESCRIPTION("Microsoft Hyper-V virtual block driver");
1497 module_init(blkvsc_init);
1498 module_exit(blkvsc_exit);