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