Staging: hv: add the Hyper-V virtual block driver
[linux-2.6/btrfs-unstable.git] / drivers / staging / hv / blkvsc_drv.c
blob2f6798152cfa901a8bfb4d9eb49cfea1fbc2b99f
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Authors:
19 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/blkdev.h>
28 #include <linux/major.h>
29 #include <linux/delay.h>
30 #include <linux/hdreg.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_dbg.h>
37 #include "logging.h"
38 #include "vmbus.h"
40 #include "StorVscApi.h"
43 // #defines
45 #define BLKVSC_MINORS 64
48 // Data types
50 enum blkvsc_device_type {
51 UNKNOWN_DEV_TYPE,
52 HARDDISK_TYPE,
53 DVD_TYPE,
56 // This request ties the struct request and struct blkvsc_request/STORVSC_REQUEST together
57 // A struct request may be represented by 1 or more struct blkvsc_request
58 struct blkvsc_request_group {
59 int outstanding;
60 int status;
62 struct list_head blkvsc_req_list; // list of blkvsc_requests
66 struct blkvsc_request {
67 struct list_head req_entry; // blkvsc_request_group.blkvsc_req_list
69 struct list_head pend_entry; // block_device_context.pending_list
71 struct request *req; // This may be null if we generate a request internally
72 struct block_device_context *dev;
73 struct blkvsc_request_group *group; // The group this request is part of. Maybe null
75 wait_queue_head_t wevent;
76 int cond;
78 int write;
79 sector_t sector_start;
80 unsigned long sector_count;
82 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
83 unsigned char cmd_len;
84 unsigned char cmnd[MAX_COMMAND_SIZE];
86 STORVSC_REQUEST request;
87 // !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap, because -
88 // The extension buffer falls right here and is pointed to by request.Extension;
91 // Per device structure
92 struct block_device_context {
93 struct device_context *device_ctx; // point back to our device context
94 struct kmem_cache *request_pool;
95 spinlock_t lock;
96 struct gendisk *gd;
97 enum blkvsc_device_type device_type;
98 struct list_head pending_list;
100 unsigned char device_id[64];
101 unsigned int device_id_len;
102 int num_outstanding_reqs;
103 int shutting_down;
104 int media_not_present;
105 unsigned int sector_size;
106 sector_t capacity;
107 unsigned int port;
108 unsigned char path;
109 unsigned char target;
110 int users;
113 // Per driver
114 struct blkvsc_driver_context {
115 // !! These must be the first 2 fields !!
116 struct driver_context drv_ctx;
117 STORVSC_DRIVER_OBJECT drv_obj;
120 // Static decl
121 static int blkvsc_probe(struct device *dev);
122 static int blkvsc_remove(struct device *device);
123 static void blkvsc_shutdown(struct device *device);
125 static int blkvsc_open(struct inode *inode, struct file *filep);
126 static int blkvsc_release(struct inode *inode, struct file *filep);
127 static int blkvsc_media_changed(struct gendisk *gd);
128 static int blkvsc_revalidate_disk(struct gendisk *gd);
129 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
130 static int blkvsc_ioctl(struct inode *inode, struct file *filep, unsigned cmd, unsigned long arg);
132 static void blkvsc_request(struct request_queue *queue);
133 static void blkvsc_request_completion(STORVSC_REQUEST* request);
134 static int blkvsc_do_request(struct block_device_context *blkdev, struct request *req);
135 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req, void (*request_completion)(STORVSC_REQUEST*) );
136 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
137 static void blkvsc_cmd_completion(STORVSC_REQUEST* request);
138 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
139 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
140 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
141 static int blkvsc_do_flush(struct block_device_context *blkdev);
142 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
143 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
146 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
148 // The one and only one
149 static struct blkvsc_driver_context g_blkvsc_drv;
152 static struct block_device_operations block_ops =
154 .owner = THIS_MODULE,
155 .open = blkvsc_open,
156 .release = blkvsc_release,
157 .media_changed = blkvsc_media_changed,
158 .revalidate_disk = blkvsc_revalidate_disk,
159 .getgeo = blkvsc_getgeo,
160 .ioctl = blkvsc_ioctl,
163 /*++
165 Name: blkvsc_drv_init()
167 Desc: BlkVsc driver initialization.
169 --*/
170 int blkvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
172 int ret=0;
173 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_blkvsc_drv.drv_obj;
174 struct driver_context *drv_ctx=&g_blkvsc_drv.drv_ctx;
176 DPRINT_ENTER(BLKVSC_DRV);
178 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
180 storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
182 // Callback to client driver to complete the initialization
183 pfn_drv_init(&storvsc_drv_obj->Base);
185 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
186 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, sizeof(GUID));
188 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
189 drv_ctx->driver.probe = blkvsc_probe;
190 drv_ctx->driver.remove = blkvsc_remove;
191 #else
192 drv_ctx->probe = blkvsc_probe;
193 drv_ctx->remove = blkvsc_remove;
194 drv_ctx->shutdown = blkvsc_shutdown;
195 #endif
197 // The driver belongs to vmbus
198 vmbus_child_driver_register(drv_ctx);
200 DPRINT_EXIT(BLKVSC_DRV);
202 return ret;
206 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
208 struct device **curr = (struct device **)data;
209 *curr = dev;
210 return 1; // stop iterating
213 /*++
215 Name: blkvsc_drv_exit()
217 Desc:
219 --*/
220 void blkvsc_drv_exit(void)
222 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_blkvsc_drv.drv_obj;
223 struct driver_context *drv_ctx=&g_blkvsc_drv.drv_ctx;
225 struct device *current_dev=NULL;
227 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
228 #define driver_for_each_device(drv, start, data, fn) \
229 struct list_head *ptr, *n; \
230 list_for_each_safe(ptr, n, &((drv)->devices)) {\
231 struct device *curr_dev;\
232 curr_dev = list_entry(ptr, struct device, driver_list);\
233 fn(curr_dev, data);\
235 #endif // KERNEL_2_6_9
237 DPRINT_ENTER(BLKVSC_DRV);
239 while (1)
241 current_dev = NULL;
243 // Get the device
244 driver_for_each_device(&drv_ctx->driver, NULL, (void*)&current_dev, blkvsc_drv_exit_cb);
246 if (current_dev == NULL)
247 break;
249 // Initiate removal from the top-down
250 device_unregister(current_dev);
253 if (storvsc_drv_obj->Base.OnCleanup)
254 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
256 vmbus_child_driver_unregister(drv_ctx);
258 DPRINT_EXIT(BLKVSC_DRV);
260 return;
263 /*++
265 Name: blkvsc_probe()
267 Desc: Add a new device for this driver
269 --*/
270 static int blkvsc_probe(struct device *device)
272 int ret=0;
274 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
275 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
276 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
278 struct device_context *device_ctx = device_to_device_context(device);
279 DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
281 struct block_device_context *blkdev=NULL;
282 STORVSC_DEVICE_INFO device_info;
283 int major=0;
284 int devnum=0;
286 static int ide0_registered=0;
287 static int ide1_registered=0;
289 DPRINT_ENTER(BLKVSC_DRV);
291 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
293 if (!storvsc_drv_obj->Base.OnDeviceAdd)
295 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
297 ret = -1;
298 goto Cleanup;
301 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
302 if (!blkdev)
304 ret = -ENOMEM;
305 goto Cleanup;
308 INIT_LIST_HEAD(&blkdev->pending_list);
310 // Initialize what we can here
311 spin_lock_init(&blkdev->lock);
313 ASSERT(sizeof(struct blkvsc_request_group) <= sizeof(struct blkvsc_request));
315 #ifdef KERNEL_2_6_27
316 blkdev->request_pool = kmem_cache_create(device_ctx->device.bus_id,
317 sizeof(struct blkvsc_request) + storvsc_drv_obj->RequestExtSize, 0,
318 SLAB_HWCACHE_ALIGN, NULL);
319 #else
320 blkdev->request_pool = kmem_cache_create(device_ctx->device.bus_id,
321 sizeof(struct blkvsc_request) + storvsc_drv_obj->RequestExtSize, 0,
322 SLAB_HWCACHE_ALIGN, NULL, NULL);
323 #endif
324 if (!blkdev->request_pool)
326 ret = -ENOMEM;
327 goto Cleanup;
331 // Call to the vsc driver to add the device
332 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
333 if (ret != 0)
335 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
336 goto Cleanup;
339 blkdev->device_ctx = device_ctx;
340 blkdev->target = device_info.TargetId; // this identified the device 0 or 1
341 blkdev->path = device_info.PathId; // this identified the ide ctrl 0 or 1
343 device->driver_data = blkdev;
345 // Calculate the major and device num
346 if (blkdev->path == 0)
348 major = IDE0_MAJOR;
349 devnum = blkdev->path + blkdev->target; // 0 or 1
351 if (!ide0_registered)
353 ret = register_blkdev(major, "ide");
354 if (ret != 0)
356 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
357 goto Remove;
360 ide0_registered = 1;
363 else if (blkdev->path == 1)
365 major = IDE1_MAJOR;
366 devnum = blkdev->path + blkdev->target + 1; // 2 or 3
368 if (!ide1_registered)
370 ret = register_blkdev(major, "ide");
371 if (ret != 0)
373 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
374 goto Remove;
377 ide1_registered = 1;
381 else
383 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
384 ret = -1;
385 goto Cleanup;
388 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
390 blkdev->gd = alloc_disk(BLKVSC_MINORS);
391 if (!blkdev->gd)
393 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
394 ret = -1;
395 goto Cleanup;
398 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
400 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
401 blk_queue_max_phys_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
402 blk_queue_max_hw_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
403 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
404 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
405 blk_queue_dma_alignment(blkdev->gd->queue, 511);
407 blkdev->gd->major = major;
408 if (devnum == 1 || devnum == 3)
409 blkdev->gd->first_minor = BLKVSC_MINORS;
410 else
411 blkdev->gd->first_minor = 0;
412 blkdev->gd->fops = &block_ops;
413 blkdev->gd->private_data = blkdev;
414 sprintf(blkdev->gd->disk_name, "hd%c", 'a'+ devnum);
416 blkvsc_do_inquiry(blkdev);
417 if (blkdev->device_type == DVD_TYPE)
419 set_disk_ro(blkdev->gd, 1);
420 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
421 blkvsc_do_read_capacity(blkdev);
423 else
425 blkvsc_do_read_capacity16(blkdev);
428 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
429 blk_queue_hardsect_size(blkdev->gd->queue, blkdev->sector_size);
430 // go!
431 add_disk(blkdev->gd);
433 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %llu sector_size %d", blkdev->gd->disk_name, blkdev->capacity, blkdev->sector_size);
435 return ret;
437 Remove:
438 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
440 Cleanup:
441 if (blkdev)
443 if (blkdev->request_pool)
445 kmem_cache_destroy(blkdev->request_pool);
446 blkdev->request_pool = NULL;
448 kfree(blkdev);
449 blkdev = NULL;
452 DPRINT_EXIT(BLKVSC_DRV);
454 return ret;
457 static void blkvsc_shutdown(struct device *device)
459 struct block_device_context *blkdev = (struct block_device_context*)device->driver_data;
460 unsigned long flags;
462 if (!blkdev)
463 return;
465 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
467 spin_lock_irqsave(&blkdev->lock, flags);
469 blkdev->shutting_down = 1;
471 blk_stop_queue(blkdev->gd->queue);
473 spin_unlock_irqrestore(&blkdev->lock, flags);
475 while (blkdev->num_outstanding_reqs)
477 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", blkdev->num_outstanding_reqs);
479 udelay(100);
482 blkvsc_do_flush(blkdev);
484 spin_lock_irqsave(&blkdev->lock, flags);
486 blkvsc_cancel_pending_reqs(blkdev);
488 spin_unlock_irqrestore(&blkdev->lock, flags);
491 static int blkvsc_do_flush(struct block_device_context *blkdev)
493 struct blkvsc_request *blkvsc_req=NULL;
495 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
497 if (blkdev->device_type != HARDDISK_TYPE)
498 return 0;
500 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
501 if (!blkvsc_req)
503 return -ENOMEM;
506 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
507 init_waitqueue_head(&blkvsc_req->wevent);
508 blkvsc_req->dev = blkdev;
509 blkvsc_req->req = NULL;
510 blkvsc_req->write = 0;
512 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
513 blkvsc_req->request.DataBuffer.Offset = 0;
514 blkvsc_req->request.DataBuffer.Length = 0;
516 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
517 blkvsc_req->cmd_len = 10;
519 // Set this here since the completion routine may be invoked and completed before we return
520 blkvsc_req->cond =0;
521 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
523 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
525 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
527 return 0;
530 // Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd)
531 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
533 struct blkvsc_request *blkvsc_req=NULL;
534 struct page *page_buf;
535 unsigned char *buf;
536 unsigned char device_type;
538 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
540 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
541 if (!blkvsc_req)
543 return -ENOMEM;
546 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
547 page_buf = alloc_page(GFP_KERNEL);
548 if (!page_buf)
550 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
551 return -ENOMEM;
554 init_waitqueue_head(&blkvsc_req->wevent);
555 blkvsc_req->dev = blkdev;
556 blkvsc_req->req = NULL;
557 blkvsc_req->write = 0;
559 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
560 blkvsc_req->request.DataBuffer.Offset = 0;
561 blkvsc_req->request.DataBuffer.Length = 64;
563 blkvsc_req->cmnd[0] = INQUIRY;
564 blkvsc_req->cmnd[1] = 0x1; // Get product data
565 blkvsc_req->cmnd[2] = 0x83; // mode page 83
566 blkvsc_req->cmnd[4] = 64;
567 blkvsc_req->cmd_len = 6;
569 // Set this here since the completion routine may be invoked and completed before we return
570 blkvsc_req->cond =0;
572 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
574 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
576 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
578 buf = kmap(page_buf);
580 //PrintBytes(buf, 64);
581 // be to le
582 device_type = buf[0] & 0x1F;
584 if (device_type == 0x0)
586 blkdev->device_type = HARDDISK_TYPE;
588 else if (device_type == 0x5)
590 blkdev->device_type = DVD_TYPE;
592 else
594 // TODO: this is currently unsupported device type
595 blkdev->device_type = UNKNOWN_DEV_TYPE;
598 DPRINT_DBG(BLKVSC_DRV, "device type %d \n", device_type);
600 blkdev->device_id_len = buf[7];
601 if (blkdev->device_id_len > 64)
602 blkdev->device_id_len = 64;
604 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
605 //PrintBytes(blkdev->device_id, blkdev->device_id_len);
607 kunmap(page_buf);
609 __free_page(page_buf);
611 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
613 return 0;
616 // Do a scsi READ_CAPACITY cmd here to get the size of the disk
617 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
619 struct blkvsc_request *blkvsc_req=NULL;
620 struct page *page_buf;
621 unsigned char *buf;
622 struct scsi_sense_hdr sense_hdr;
624 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
626 blkdev->sector_size = 0;
627 blkdev->capacity = 0;
628 blkdev->media_not_present = 0; // assume a disk is present
630 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
631 if (!blkvsc_req)
633 return -ENOMEM;
636 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
637 page_buf = alloc_page(GFP_KERNEL);
638 if (!page_buf)
640 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
641 return -ENOMEM;
644 init_waitqueue_head(&blkvsc_req->wevent);
645 blkvsc_req->dev = blkdev;
646 blkvsc_req->req = NULL;
647 blkvsc_req->write = 0;
649 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
650 blkvsc_req->request.DataBuffer.Offset = 0;
651 blkvsc_req->request.DataBuffer.Length = 8;
653 blkvsc_req->cmnd[0] = READ_CAPACITY;
654 blkvsc_req->cmd_len = 16;
656 // Set this here since the completion routine may be invoked and completed before we return
657 blkvsc_req->cond =0;
659 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
661 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
663 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
665 // check error
666 if (blkvsc_req->request.Status)
668 scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
670 if (sense_hdr.asc == 0x3A) // Medium not present
672 blkdev->media_not_present = 1;
675 return 0;
677 buf = kmap(page_buf);
679 // be to le
680 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) + 1;
681 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
683 kunmap(page_buf);
685 __free_page(page_buf);
687 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
689 return 0;
693 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
695 struct blkvsc_request *blkvsc_req=NULL;
696 struct page *page_buf;
697 unsigned char *buf;
698 struct scsi_sense_hdr sense_hdr;
700 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
702 blkdev->sector_size = 0;
703 blkdev->capacity = 0;
704 blkdev->media_not_present = 0; // assume a disk is present
706 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
707 if (!blkvsc_req)
709 return -ENOMEM;
712 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
713 page_buf = alloc_page(GFP_KERNEL);
714 if (!page_buf)
716 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
717 return -ENOMEM;
720 init_waitqueue_head(&blkvsc_req->wevent);
721 blkvsc_req->dev = blkdev;
722 blkvsc_req->req = NULL;
723 blkvsc_req->write = 0;
725 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
726 blkvsc_req->request.DataBuffer.Offset = 0;
727 blkvsc_req->request.DataBuffer.Length = 12;
729 blkvsc_req->cmnd[0] = 0x9E; //READ_CAPACITY16;
730 blkvsc_req->cmd_len = 16;
732 // Set this here since the completion routine may be invoked and completed before we return
733 blkvsc_req->cond =0;
735 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
737 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
739 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
741 // check error
742 if (blkvsc_req->request.Status)
744 scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
746 if (sense_hdr.asc == 0x3A) // Medium not present
748 blkdev->media_not_present = 1;
751 return 0;
753 buf = kmap(page_buf);
755 // be to le
756 blkdev->capacity = be64_to_cpu(*(unsigned long long*) &buf[0]) + 1;
757 blkdev->sector_size = be32_to_cpu(*(unsigned int*)&buf[8]);
759 //blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) + 1;
760 //blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
762 kunmap(page_buf);
764 __free_page(page_buf);
766 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
768 return 0;
771 /*++
773 Name: blkvsc_remove()
775 Desc: Callback when our device is removed
777 --*/
778 static int blkvsc_remove(struct device *device)
780 int ret=0;
782 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
783 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
784 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
786 struct device_context *device_ctx = device_to_device_context(device);
787 DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
788 struct block_device_context *blkdev = (struct block_device_context*)device->driver_data;
789 unsigned long flags;
791 DPRINT_ENTER(BLKVSC_DRV);
793 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
795 if (!storvsc_drv_obj->Base.OnDeviceRemove)
797 DPRINT_EXIT(BLKVSC_DRV);
798 return -1;
801 // Call to the vsc driver to let it know that the device is being removed
802 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
803 if (ret != 0)
805 // TODO:
806 DPRINT_ERR(BLKVSC_DRV, "unable to remove blkvsc device (ret %d)", ret);
809 // Get to a known state
810 spin_lock_irqsave(&blkdev->lock, flags);
812 blkdev->shutting_down = 1;
814 blk_stop_queue(blkdev->gd->queue);
816 spin_unlock_irqrestore(&blkdev->lock, flags);
818 while (blkdev->num_outstanding_reqs)
820 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", blkdev->num_outstanding_reqs);
822 udelay(100);
825 blkvsc_do_flush(blkdev);
827 spin_lock_irqsave(&blkdev->lock, flags);
829 blkvsc_cancel_pending_reqs(blkdev);
831 spin_unlock_irqrestore(&blkdev->lock, flags);
833 blk_cleanup_queue(blkdev->gd->queue);
835 del_gendisk(blkdev->gd);
837 kmem_cache_destroy(blkdev->request_pool);
839 kfree(blkdev);
841 DPRINT_EXIT(BLKVSC_DRV);
843 return ret;
846 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
848 ASSERT(blkvsc_req->req);
849 ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8));
851 blkvsc_req->cmd_len = 16;
853 if (blkvsc_req->sector_start > 0xffffffff)
855 if (rq_data_dir(blkvsc_req->req))
857 blkvsc_req->write = 1;
858 blkvsc_req->cmnd[0] = WRITE_16;
860 else
862 blkvsc_req->write = 0;
863 blkvsc_req->cmnd[0] = READ_16;
866 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
868 *(unsigned long long*)&blkvsc_req->cmnd[2] = cpu_to_be64(blkvsc_req->sector_start);
869 *(unsigned int*)&blkvsc_req->cmnd[10] = cpu_to_be32(blkvsc_req->sector_count);
871 else if ((blkvsc_req->sector_count > 0xff) || (blkvsc_req->sector_start > 0x1fffff))
873 if (rq_data_dir(blkvsc_req->req))
875 blkvsc_req->write = 1;
876 blkvsc_req->cmnd[0] = WRITE_10;
878 else
880 blkvsc_req->write = 0;
881 blkvsc_req->cmnd[0] = READ_10;
884 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
886 *(unsigned int *)&blkvsc_req->cmnd[2] = cpu_to_be32(blkvsc_req->sector_start);
887 *(unsigned short*)&blkvsc_req->cmnd[7] = cpu_to_be16(blkvsc_req->sector_count);
889 else
891 if (rq_data_dir(blkvsc_req->req))
893 blkvsc_req->write = 1;
894 blkvsc_req->cmnd[0] = WRITE_6;
896 else
898 blkvsc_req->write = 0;
899 blkvsc_req->cmnd[0] = READ_6;
902 *(unsigned int *)&blkvsc_req->cmnd[1] = cpu_to_be32(blkvsc_req->sector_start) >> 8;
903 blkvsc_req->cmnd[1] &= 0x1f;
904 blkvsc_req->cmnd[4] = (unsigned char) blkvsc_req->sector_count;
908 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req, void (*request_completion)(STORVSC_REQUEST*) )
910 struct block_device_context *blkdev = blkvsc_req->dev;
911 struct device_context *device_ctx=blkdev->device_ctx;
912 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
913 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
914 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
915 int ret =0;
917 STORVSC_REQUEST *storvsc_req;
919 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - req %p type %s start_sector %llu count %d offset %d len %d\n",
920 blkvsc_req,
921 (blkvsc_req->write)?"WRITE":"READ",
922 blkvsc_req->sector_start,
923 blkvsc_req->sector_count,
924 blkvsc_req->request.DataBuffer.Offset,
925 blkvsc_req->request.DataBuffer.Length);
927 /*for (i=0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++)
929 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - req %p pfn[%d] %llx\n",
930 blkvsc_req,
932 blkvsc_req->request.DataBuffer.PfnArray[i]);
935 storvsc_req = &blkvsc_req->request;
936 storvsc_req->Extension = (void*)((unsigned long)blkvsc_req + sizeof(struct blkvsc_request));
938 storvsc_req->Type = blkvsc_req->write? WRITE_TYPE : READ_TYPE;
940 storvsc_req->OnIOCompletion = request_completion;
941 storvsc_req->Context = blkvsc_req;
943 storvsc_req->Host = blkdev->port;
944 storvsc_req->Bus = blkdev->path;
945 storvsc_req->TargetId = blkdev->target;
946 storvsc_req->LunId = 0; // this is not really used at all
948 storvsc_req->CdbLen = blkvsc_req->cmd_len;
949 storvsc_req->Cdb = blkvsc_req->cmnd;
951 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
952 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
954 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj, &blkvsc_req->request);
955 if (ret == 0)
957 blkdev->num_outstanding_reqs++;
960 return ret;
964 // We break the request into 1 or more blkvsc_requests and submit them.
965 // If we cant submit them all, we put them on the pending_list. The
966 // blkvsc_request() will work on the pending_list.
968 static int blkvsc_do_request(struct block_device_context *blkdev, struct request *req)
970 struct bio *bio=NULL;
971 struct bio_vec *bvec=NULL;
972 struct bio_vec *prev_bvec=NULL;
974 struct blkvsc_request *blkvsc_req=NULL;
975 struct blkvsc_request *tmp;
976 int databuf_idx=0;
977 int seg_idx=0;
979 sector_t start_sector;
980 unsigned long num_sectors = 0;
981 int ret=0;
982 int pending=0;
983 struct blkvsc_request_group *group=NULL;
985 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %llu \n", blkdev, req, req->sector);
987 // Create a group to tie req to list of blkvsc_reqs
988 group = (struct blkvsc_request_group*)kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
989 if (!group)
991 return -ENOMEM;
994 INIT_LIST_HEAD(&group->blkvsc_req_list);
995 group->outstanding = group->status = 0;
997 start_sector = req->sector;
999 // foreach bio in the request
1000 if (req->bio)
1001 for (bio = req->bio; bio; bio = bio->bi_next)
1003 // Map this bio into an existing or new storvsc request
1004 bio_for_each_segment (bvec, bio, seg_idx)
1006 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() - req %p bio %p bvec %p seg_idx %d databuf_idx %d\n",
1007 req, bio, bvec, seg_idx, databuf_idx);
1009 // Get a new storvsc request
1010 if ( (!blkvsc_req) || // 1st-time
1011 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT) ||
1012 (bvec->bv_offset != 0) || // hole at the begin of page
1013 (prev_bvec && (prev_bvec->bv_len != PAGE_SIZE)) ) // hold at the end of page
1015 // submit the prev one
1016 if (blkvsc_req)
1018 blkvsc_req->sector_start = start_sector;
1019 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
1021 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
1023 blkvsc_init_rw(blkvsc_req);
1026 // Create new blkvsc_req to represent the current bvec
1027 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
1028 if (!blkvsc_req)
1030 // free up everything
1031 list_for_each_entry_safe(blkvsc_req, tmp, &group->blkvsc_req_list, req_entry)
1033 list_del(&blkvsc_req->req_entry);
1034 kmem_cache_free(blkdev->request_pool, blkvsc_req);
1037 kmem_cache_free(blkdev->request_pool, group);
1038 return -ENOMEM;
1041 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
1043 blkvsc_req->dev = blkdev;
1044 blkvsc_req->req = req;
1045 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1046 blkvsc_req->request.DataBuffer.Length = 0;
1048 // Add to the group
1049 blkvsc_req->group = group;
1050 blkvsc_req->group->outstanding++;
1051 list_add_tail(&blkvsc_req->req_entry, &blkvsc_req->group->blkvsc_req_list);
1053 start_sector += num_sectors;
1054 num_sectors = 0;
1055 databuf_idx = 0;
1058 // Add the curr bvec/segment to the curr blkvsc_req
1059 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1060 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1062 prev_bvec = bvec;
1064 databuf_idx++;
1065 num_sectors += bvec->bv_len >> 9;
1067 } // bio_for_each_segment
1069 } // rq_for_each_bio
1071 // Handle the last one
1072 if (blkvsc_req)
1074 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n", blkdev, req, blkvsc_req->group, blkvsc_req->group->outstanding);
1076 blkvsc_req->sector_start = start_sector;
1077 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
1079 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
1081 blkvsc_init_rw(blkvsc_req);
1084 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry)
1086 if (pending)
1088 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to pending_list - blkvsc_req %p start_sect %llu sect_count %d (%llu %d)\n",
1089 blkvsc_req, blkvsc_req->sector_start, blkvsc_req->sector_count, start_sector, num_sectors);
1091 list_add_tail(&blkvsc_req->pend_entry, &blkdev->pending_list);
1093 else
1095 ret = blkvsc_submit_request(blkvsc_req, blkvsc_request_completion);
1096 if (ret == -1)
1098 pending = 1;
1099 list_add_tail(&blkvsc_req->pend_entry, &blkdev->pending_list);
1102 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p start_sect %llu sect_count %d (%llu %d) ret %d\n",
1103 blkvsc_req, blkvsc_req->sector_start, blkvsc_req->sector_count, start_sector, num_sectors, ret);
1107 return pending;
1110 static void blkvsc_cmd_completion(STORVSC_REQUEST* request)
1112 struct blkvsc_request *blkvsc_req=(struct blkvsc_request*)request->Context;
1113 struct block_device_context *blkdev = (struct block_device_context*)blkvsc_req->dev;
1115 struct scsi_sense_hdr sense_hdr;
1117 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n", blkvsc_req);
1119 blkdev->num_outstanding_reqs--;
1121 if (blkvsc_req->request.Status)
1123 if (scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1125 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1129 blkvsc_req->cond =1;
1130 wake_up_interruptible(&blkvsc_req->wevent);
1133 static void blkvsc_request_completion(STORVSC_REQUEST* request)
1135 struct blkvsc_request *blkvsc_req=(struct blkvsc_request*)request->Context;
1136 struct block_device_context *blkdev = (struct block_device_context*)blkvsc_req->dev;
1137 unsigned long flags;
1138 struct blkvsc_request *comp_req, *tmp;
1140 ASSERT(blkvsc_req->group);
1142 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s sect_start %llu sect_count %d len %d group outstd %d total outstd %d\n",
1143 blkdev,
1144 blkvsc_req,
1145 blkvsc_req->group,
1146 (blkvsc_req->write)?"WRITE":"READ",
1147 blkvsc_req->sector_start,
1148 blkvsc_req->sector_count,
1149 blkvsc_req->request.DataBuffer.Length,
1150 blkvsc_req->group->outstanding,
1151 blkdev->num_outstanding_reqs);
1153 spin_lock_irqsave(&blkdev->lock, flags);
1155 blkdev->num_outstanding_reqs--;
1156 blkvsc_req->group->outstanding--;
1158 // Only start processing when all the blkvsc_reqs are completed. This guarantees no out-of-order
1159 // blkvsc_req completion when calling end_that_request_first()
1160 if (blkvsc_req->group->outstanding == 0)
1162 list_for_each_entry_safe(comp_req, tmp, &blkvsc_req->group->blkvsc_req_list, req_entry)
1164 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p sect_start %llu sect_count %d \n",
1165 comp_req,
1166 comp_req->sector_start,
1167 comp_req->sector_count);
1169 list_del(&comp_req->req_entry);
1171 #ifdef KERNEL_2_6_27
1172 if (!__blk_end_request(
1173 comp_req->req,
1174 (!comp_req->request.Status ? 0: -EIO),
1175 comp_req->sector_count * blkdev->sector_size))
1177 //All the sectors have been xferred ie the request is done
1178 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n", comp_req->req);
1179 kmem_cache_free(blkdev->request_pool, comp_req->group);
1181 #else
1182 if (!end_that_request_first(comp_req->req, !comp_req->request.Status, (comp_req->sector_count * (blkdev->sector_size >> 9))))
1184 //All the sectors have been xferred ie the request is done
1185 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n", comp_req->req);
1187 end_that_request_last(comp_req->req, !comp_req->request.Status);
1189 kmem_cache_free(blkdev->request_pool, comp_req->group);
1191 #endif
1193 kmem_cache_free(blkdev->request_pool, comp_req);
1196 if (!blkdev->shutting_down)
1198 blkvsc_do_pending_reqs(blkdev);
1199 blk_start_queue(blkdev->gd->queue);
1200 blkvsc_request(blkdev->gd->queue);
1204 spin_unlock_irqrestore(&blkdev->lock, flags);
1207 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1209 struct blkvsc_request *pend_req, *tmp;
1210 struct blkvsc_request *comp_req, *tmp2;
1212 int ret=0;
1214 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1216 // Flush the pending list first
1217 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list, pend_entry)
1219 // The pend_req could be part of a partially completed request. If so, complete those req first
1220 // until we hit the pend_req
1221 list_for_each_entry_safe(comp_req, tmp2, &pend_req->group->blkvsc_req_list, req_entry)
1223 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p sect_start %llu sect_count %d \n",
1224 comp_req,
1225 comp_req->sector_start,
1226 comp_req->sector_count);
1228 if (comp_req == pend_req)
1229 break;
1231 list_del(&comp_req->req_entry);
1233 if (comp_req->req)
1235 #ifdef KERNEL_2_6_27
1236 ret = __blk_end_request(
1237 comp_req->req,
1238 (!comp_req->request.Status ? 0 : -EIO),
1239 comp_req->sector_count * blkdev->sector_size);
1240 #else
1241 ret = end_that_request_first(comp_req->req, !comp_req->request.Status, (comp_req->sector_count * (blkdev->sector_size >> 9)));
1242 #endif
1243 ASSERT(ret != 0);
1246 kmem_cache_free(blkdev->request_pool, comp_req);
1249 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n", pend_req);
1251 list_del(&pend_req->pend_entry);
1253 list_del(&pend_req->req_entry);
1255 if (comp_req->req)
1257 #ifdef KERNEL_2_6_27
1258 if (!__blk_end_request(
1259 pend_req->req,
1260 -EIO,
1261 pend_req->sector_count * blkdev->sector_size))
1263 //All the sectors have been xferred ie the request is done
1264 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs() - req %p COMPLETED\n", pend_req->req);
1265 kmem_cache_free(blkdev->request_pool, pend_req->group);
1267 #else
1268 if (!end_that_request_first(pend_req->req, 0, (pend_req->sector_count * (blkdev->sector_size >> 9))))
1270 //All the sectors have been xferred ie the request is done
1271 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs() - req %p COMPLETED\n", pend_req->req);
1273 end_that_request_last(pend_req->req, 0);
1275 kmem_cache_free(blkdev->request_pool, pend_req->group);
1277 #endif
1280 kmem_cache_free(blkdev->request_pool, pend_req);
1283 return ret;
1286 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1288 struct blkvsc_request *pend_req, *tmp;
1289 int ret=0;
1291 // Flush the pending list first
1292 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list, pend_entry)
1294 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n", pend_req);
1296 ret = blkvsc_submit_request(pend_req, blkvsc_request_completion);
1297 if (ret != 0)
1299 break;
1301 else
1303 list_del(&pend_req->pend_entry);
1307 return ret;
1310 static void blkvsc_request(struct request_queue *queue)
1312 struct block_device_context *blkdev = NULL;
1313 struct request *req;
1314 int ret=0;
1316 DPRINT_DBG(BLKVSC_DRV, "- enter \n");
1317 while ((req = elv_next_request(queue)) != NULL)
1319 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1321 blkdev = req->rq_disk->private_data;
1322 if (blkdev->shutting_down || !blk_fs_request(req) || blkdev->media_not_present) {
1323 end_request(req, 0);
1324 continue;
1327 ret = blkvsc_do_pending_reqs(blkdev);
1329 if (ret != 0)
1331 DPRINT_DBG(BLKVSC_DRV, "- stop queue - pending_list not empty\n");
1332 blk_stop_queue(queue);
1333 break;
1336 blkdev_dequeue_request(req);
1338 ret = blkvsc_do_request(blkdev, req);
1339 if (ret > 0)
1341 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1342 blk_stop_queue(queue);
1343 break;
1345 else if (ret < 0)
1347 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1348 blk_requeue_request(queue, req);
1349 blk_stop_queue(queue);
1350 break;
1355 static int blkvsc_open(struct inode *inode, struct file *filep)
1357 struct block_device_context *blkdev = inode->i_bdev->bd_disk->private_data;
1359 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
1361 spin_lock(&blkdev->lock);
1363 if (!blkdev->users && blkdev->device_type == DVD_TYPE)
1365 spin_unlock(&blkdev->lock);
1366 check_disk_change(inode->i_bdev);
1367 spin_lock(&blkdev->lock);
1370 blkdev->users++;
1372 spin_unlock(&blkdev->lock);
1373 return 0;
1376 static int blkvsc_release(struct inode *inode, struct file *filep)
1378 struct block_device_context *blkdev = inode->i_bdev->bd_disk->private_data;
1380 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
1382 spin_lock(&blkdev->lock);
1383 if (blkdev->users == 1)
1385 spin_unlock(&blkdev->lock);
1386 blkvsc_do_flush(blkdev);
1387 spin_lock(&blkdev->lock);
1390 blkdev->users--;
1392 spin_unlock(&blkdev->lock);
1393 return 0;
1396 static int blkvsc_media_changed(struct gendisk *gd)
1398 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1400 return 1;
1403 static int blkvsc_revalidate_disk(struct gendisk *gd)
1405 struct block_device_context *blkdev = gd->private_data;
1407 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1409 if (blkdev->device_type == DVD_TYPE)
1411 blkvsc_do_read_capacity(blkdev);
1412 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
1413 blk_queue_hardsect_size(gd->queue, blkdev->sector_size);
1415 return 0;
1418 int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1420 sector_t total_sectors = get_capacity(bd->bd_disk);
1421 sector_t cylinder_times_heads=0;
1422 sector_t temp=0;
1424 int sectors_per_track=0;
1425 int heads=0;
1426 int cylinders=0;
1427 int rem=0;
1429 if (total_sectors > (65535 * 16 * 255)) {
1430 total_sectors = (65535 * 16 * 255);
1433 if (total_sectors >= (65535 * 16 * 63)) {
1434 sectors_per_track = 255;
1435 heads = 16;
1437 cylinder_times_heads = total_sectors;
1438 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1440 else
1442 sectors_per_track = 17;
1444 cylinder_times_heads = total_sectors;
1445 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1447 temp = cylinder_times_heads + 1023;
1448 rem = sector_div(temp, 1024); // sector_div stores the quotient in temp
1450 heads = temp;
1452 if (heads < 4) {
1453 heads = 4;
1456 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1457 sectors_per_track = 31;
1458 heads = 16;
1460 cylinder_times_heads = total_sectors;
1461 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1464 if (cylinder_times_heads >= (heads * 1024)) {
1465 sectors_per_track = 63;
1466 heads = 16;
1468 cylinder_times_heads = total_sectors;
1469 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1473 temp = cylinder_times_heads;
1474 rem = sector_div(temp, heads); // sector_div stores the quotient in temp
1475 cylinders = temp;
1477 hg->heads = heads;
1478 hg->sectors = sectors_per_track;
1479 hg->cylinders = cylinders;
1481 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
1483 return 0;
1486 static int blkvsc_ioctl(struct inode *inode, struct file *filep, unsigned cmd, unsigned long arg)
1488 struct block_device *bd = inode->i_bdev;
1489 struct block_device_context *blkdev = bd->bd_disk->private_data;
1490 int ret=0;
1492 switch (cmd)
1494 // TODO: I think there is certain format for HDIO_GET_IDENTITY rather than just
1495 // a GUID. Commented it out for now.
1496 /*case HDIO_GET_IDENTITY:
1497 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1499 if (copy_to_user((void __user *)arg, blkdev->device_id, blkdev->device_id_len))
1501 ret = -EFAULT;
1504 break;*/
1505 default:
1506 ret = -EINVAL;
1507 break;
1510 return ret;
1514 MODULE_LICENSE("GPL");
1516 static int __init blkvsc_init(void)
1518 int ret;
1520 ASSERT(sizeof(sector_t) == 8); // Make sure CONFIG_LBD is set
1522 DPRINT_ENTER(BLKVSC_DRV);
1524 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1526 ret = blkvsc_drv_init(BlkVscInitialize);
1528 DPRINT_EXIT(BLKVSC_DRV);
1530 return ret;
1533 static void __exit blkvsc_exit(void)
1535 DPRINT_ENTER(BLKVSC_DRV);
1537 blkvsc_drv_exit();
1539 DPRINT_ENTER(BLKVSC_DRV);
1542 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
1544 module_init(blkvsc_init);
1545 module_exit(blkvsc_exit);
1547 // eof