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