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