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