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