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