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