First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / staging / hv / blkvsc_drv.c
blob99c49261a8b4f125b9c05b8fa8cf1604ea70358b
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Hank Janssen <hjanssen@microsoft.com>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/blkdev.h>
24 #include <linux/major.h>
25 #include <linux/delay.h>
26 #include <linux/hdreg.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_eh.h>
30 #include <scsi/scsi_dbg.h>
31 #include "osd.h"
32 #include "logging.h"
33 #include "vmbus.h"
34 #include "StorVscApi.h"
37 #define BLKVSC_MINORS 64
39 enum blkvsc_device_type {
40 UNKNOWN_DEV_TYPE,
41 HARDDISK_TYPE,
42 DVD_TYPE,
46 * This request ties the struct request and struct
47 * blkvsc_request/hv_storvsc_request together A struct request may be
48 * represented by 1 or more struct blkvsc_request
50 struct blkvsc_request_group {
51 int outstanding;
52 int status;
53 struct list_head blkvsc_req_list; /* list of blkvsc_requests */
56 struct blkvsc_request {
57 /* blkvsc_request_group.blkvsc_req_list */
58 struct list_head req_entry;
60 /* block_device_context.pending_list */
61 struct list_head pend_entry;
63 /* This may be null if we generate a request internally */
64 struct request *req;
66 struct block_device_context *dev;
68 /* The group this request is part of. Maybe null */
69 struct blkvsc_request_group *group;
71 wait_queue_head_t wevent;
72 int cond;
74 int write;
75 sector_t sector_start;
76 unsigned long sector_count;
78 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
79 unsigned char cmd_len;
80 unsigned char cmnd[MAX_COMMAND_SIZE];
82 struct hv_storvsc_request request;
84 * !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap,
85 * because - The extension buffer falls right here and is pointed to by
86 * request.Extension;
87 * Which sounds like a horrible idea, who designed this?
91 /* Per device structure */
92 struct block_device_context {
93 /* point back to our device context */
94 struct device_context *device_ctx;
95 struct kmem_cache *request_pool;
96 spinlock_t lock;
97 struct gendisk *gd;
98 enum blkvsc_device_type device_type;
99 struct list_head pending_list;
101 unsigned char device_id[64];
102 unsigned int device_id_len;
103 int num_outstanding_reqs;
104 int shutting_down;
105 int media_not_present;
106 unsigned int sector_size;
107 sector_t capacity;
108 unsigned int port;
109 unsigned char path;
110 unsigned char target;
111 int users;
114 /* Per driver */
115 struct blkvsc_driver_context {
116 /* !! These must be the first 2 fields !! */
117 /* FIXME this is a bug! */
118 struct driver_context drv_ctx;
119 struct storvsc_driver_object drv_obj;
122 /* Static decl */
123 static int blkvsc_probe(struct device *dev);
124 static int blkvsc_remove(struct device *device);
125 static void blkvsc_shutdown(struct device *device);
127 static int blkvsc_open(struct block_device *bdev, fmode_t mode);
128 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
129 static int blkvsc_media_changed(struct gendisk *gd);
130 static int blkvsc_revalidate_disk(struct gendisk *gd);
131 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
132 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
133 unsigned cmd, unsigned long argument);
134 static void blkvsc_request(struct request_queue *queue);
135 static void blkvsc_request_completion(struct hv_storvsc_request *request);
136 static int blkvsc_do_request(struct block_device_context *blkdev,
137 struct request *req);
138 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
139 void (*request_completion)(struct hv_storvsc_request *));
140 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
141 static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
142 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
143 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
144 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
145 static int blkvsc_do_flush(struct block_device_context *blkdev);
146 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
147 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
150 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
152 /* The one and only one */
153 static struct blkvsc_driver_context g_blkvsc_drv;
155 static struct block_device_operations block_ops = {
156 .owner = THIS_MODULE,
157 .open = blkvsc_open,
158 .release = blkvsc_release,
159 .media_changed = blkvsc_media_changed,
160 .revalidate_disk = blkvsc_revalidate_disk,
161 .getgeo = blkvsc_getgeo,
162 .ioctl = blkvsc_ioctl,
166 * blkvsc_drv_init - BlkVsc driver initialization.
168 static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
170 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
171 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
172 int ret;
174 DPRINT_ENTER(BLKVSC_DRV);
176 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
178 storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
180 /* Callback to client driver to complete the initialization */
181 drv_init(&storvsc_drv_obj->Base);
183 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
184 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
185 sizeof(struct hv_guid));
187 drv_ctx->probe = blkvsc_probe;
188 drv_ctx->remove = blkvsc_remove;
189 drv_ctx->shutdown = blkvsc_shutdown;
191 /* The driver belongs to vmbus */
192 ret = vmbus_child_driver_register(drv_ctx);
194 DPRINT_EXIT(BLKVSC_DRV);
196 return ret;
199 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
201 struct device **curr = (struct device **)data;
202 *curr = dev;
203 return 1; /* stop iterating */
206 static void blkvsc_drv_exit(void)
208 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
209 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
210 struct device *current_dev;
211 int ret;
213 DPRINT_ENTER(BLKVSC_DRV);
215 while (1) {
216 current_dev = NULL;
218 /* Get the device */
219 ret = driver_for_each_device(&drv_ctx->driver, NULL,
220 (void *) &current_dev,
221 blkvsc_drv_exit_cb);
223 if (ret)
224 DPRINT_WARN(BLKVSC_DRV,
225 "driver_for_each_device returned %d", ret);
228 if (current_dev == NULL)
229 break;
231 /* Initiate removal from the top-down */
232 device_unregister(current_dev);
235 if (storvsc_drv_obj->Base.OnCleanup)
236 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
238 vmbus_child_driver_unregister(drv_ctx);
240 DPRINT_EXIT(BLKVSC_DRV);
242 return;
246 * blkvsc_probe - Add a new device for this driver
248 static int blkvsc_probe(struct device *device)
250 struct driver_context *driver_ctx =
251 driver_to_driver_context(device->driver);
252 struct blkvsc_driver_context *blkvsc_drv_ctx =
253 (struct blkvsc_driver_context *)driver_ctx;
254 struct storvsc_driver_object *storvsc_drv_obj =
255 &blkvsc_drv_ctx->drv_obj;
256 struct device_context *device_ctx = device_to_device_context(device);
257 struct hv_device *device_obj = &device_ctx->device_obj;
259 struct block_device_context *blkdev = NULL;
260 struct storvsc_device_info device_info;
261 int major = 0;
262 int devnum = 0;
263 int ret = 0;
264 static int ide0_registered;
265 static int ide1_registered;
267 DPRINT_ENTER(BLKVSC_DRV);
269 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
271 if (!storvsc_drv_obj->Base.OnDeviceAdd) {
272 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
273 ret = -1;
274 goto Cleanup;
277 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
278 if (!blkdev) {
279 ret = -ENOMEM;
280 goto Cleanup;
283 INIT_LIST_HEAD(&blkdev->pending_list);
285 /* Initialize what we can here */
286 spin_lock_init(&blkdev->lock);
288 ASSERT(sizeof(struct blkvsc_request_group) <=
289 sizeof(struct blkvsc_request));
291 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
292 sizeof(struct blkvsc_request) +
293 storvsc_drv_obj->RequestExtSize, 0,
294 SLAB_HWCACHE_ALIGN, NULL);
295 if (!blkdev->request_pool) {
296 ret = -ENOMEM;
297 goto Cleanup;
301 /* Call to the vsc driver to add the device */
302 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
303 if (ret != 0) {
304 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
305 goto Cleanup;
308 blkdev->device_ctx = device_ctx;
309 /* this identified the device 0 or 1 */
310 blkdev->target = device_info.TargetId;
311 /* this identified the ide ctrl 0 or 1 */
312 blkdev->path = device_info.PathId;
314 dev_set_drvdata(device, blkdev);
316 /* Calculate the major and device num */
317 if (blkdev->path == 0) {
318 major = IDE0_MAJOR;
319 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
321 if (!ide0_registered) {
322 ret = register_blkdev(major, "ide");
323 if (ret != 0) {
324 DPRINT_ERR(BLKVSC_DRV,
325 "register_blkdev() failed! ret %d",
326 ret);
327 goto Remove;
330 ide0_registered = 1;
332 } else if (blkdev->path == 1) {
333 major = IDE1_MAJOR;
334 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
336 if (!ide1_registered) {
337 ret = register_blkdev(major, "ide");
338 if (ret != 0) {
339 DPRINT_ERR(BLKVSC_DRV,
340 "register_blkdev() failed! ret %d",
341 ret);
342 goto Remove;
345 ide1_registered = 1;
347 } else {
348 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
349 ret = -1;
350 goto Cleanup;
353 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
355 blkdev->gd = alloc_disk(BLKVSC_MINORS);
356 if (!blkdev->gd) {
357 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
358 ret = -1;
359 goto Cleanup;
362 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
364 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
365 blk_queue_max_phys_segments(blkdev->gd->queue,
366 MAX_MULTIPAGE_BUFFER_COUNT);
367 blk_queue_max_hw_segments(blkdev->gd->queue,
368 MAX_MULTIPAGE_BUFFER_COUNT);
369 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
370 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
371 blk_queue_dma_alignment(blkdev->gd->queue, 511);
373 blkdev->gd->major = major;
374 if (devnum == 1 || devnum == 3)
375 blkdev->gd->first_minor = BLKVSC_MINORS;
376 else
377 blkdev->gd->first_minor = 0;
378 blkdev->gd->fops = &block_ops;
379 blkdev->gd->private_data = blkdev;
380 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
382 blkvsc_do_inquiry(blkdev);
383 if (blkdev->device_type == DVD_TYPE) {
384 set_disk_ro(blkdev->gd, 1);
385 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
386 blkvsc_do_read_capacity(blkdev);
387 } else {
388 blkvsc_do_read_capacity16(blkdev);
391 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
392 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
393 /* go! */
394 add_disk(blkdev->gd);
396 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
397 blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
398 blkdev->sector_size);
400 return ret;
402 Remove:
403 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
405 Cleanup:
406 if (blkdev) {
407 if (blkdev->request_pool) {
408 kmem_cache_destroy(blkdev->request_pool);
409 blkdev->request_pool = NULL;
411 kfree(blkdev);
412 blkdev = NULL;
415 DPRINT_EXIT(BLKVSC_DRV);
417 return ret;
420 static void blkvsc_shutdown(struct device *device)
422 struct block_device_context *blkdev = dev_get_drvdata(device);
423 unsigned long flags;
425 if (!blkdev)
426 return;
428 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
429 blkdev->users, blkdev->gd->disk_name);
431 spin_lock_irqsave(&blkdev->lock, flags);
433 blkdev->shutting_down = 1;
435 blk_stop_queue(blkdev->gd->queue);
437 spin_unlock_irqrestore(&blkdev->lock, flags);
439 while (blkdev->num_outstanding_reqs) {
440 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
441 blkdev->num_outstanding_reqs);
442 udelay(100);
445 blkvsc_do_flush(blkdev);
447 spin_lock_irqsave(&blkdev->lock, flags);
449 blkvsc_cancel_pending_reqs(blkdev);
451 spin_unlock_irqrestore(&blkdev->lock, flags);
454 static int blkvsc_do_flush(struct block_device_context *blkdev)
456 struct blkvsc_request *blkvsc_req;
458 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
460 if (blkdev->device_type != HARDDISK_TYPE)
461 return 0;
463 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
464 if (!blkvsc_req)
465 return -ENOMEM;
467 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
468 init_waitqueue_head(&blkvsc_req->wevent);
469 blkvsc_req->dev = blkdev;
470 blkvsc_req->req = NULL;
471 blkvsc_req->write = 0;
473 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
474 blkvsc_req->request.DataBuffer.Offset = 0;
475 blkvsc_req->request.DataBuffer.Length = 0;
477 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
478 blkvsc_req->cmd_len = 10;
481 * Set this here since the completion routine may be invoked and
482 * completed before we return
484 blkvsc_req->cond = 0;
485 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
487 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
489 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
491 return 0;
494 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
495 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
497 struct blkvsc_request *blkvsc_req;
498 struct page *page_buf;
499 unsigned char *buf;
500 unsigned char device_type;
502 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
504 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
505 if (!blkvsc_req)
506 return -ENOMEM;
508 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
509 page_buf = alloc_page(GFP_KERNEL);
510 if (!page_buf) {
511 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
512 return -ENOMEM;
515 init_waitqueue_head(&blkvsc_req->wevent);
516 blkvsc_req->dev = blkdev;
517 blkvsc_req->req = NULL;
518 blkvsc_req->write = 0;
520 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
521 blkvsc_req->request.DataBuffer.Offset = 0;
522 blkvsc_req->request.DataBuffer.Length = 64;
524 blkvsc_req->cmnd[0] = INQUIRY;
525 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
526 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
527 blkvsc_req->cmnd[4] = 64;
528 blkvsc_req->cmd_len = 6;
531 * Set this here since the completion routine may be invoked and
532 * completed before we return
534 blkvsc_req->cond = 0;
536 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
538 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
539 blkvsc_req, blkvsc_req->cond);
541 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
543 buf = kmap(page_buf);
545 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
546 /* be to le */
547 device_type = buf[0] & 0x1F;
549 if (device_type == 0x0) {
550 blkdev->device_type = HARDDISK_TYPE;
551 } else if (device_type == 0x5) {
552 blkdev->device_type = DVD_TYPE;
553 } else {
554 /* TODO: this is currently unsupported device type */
555 blkdev->device_type = UNKNOWN_DEV_TYPE;
558 DPRINT_DBG(BLKVSC_DRV, "device type %d \n", device_type);
560 blkdev->device_id_len = buf[7];
561 if (blkdev->device_id_len > 64)
562 blkdev->device_id_len = 64;
564 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
565 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
566 * blkdev->device_id_len); */
568 kunmap(page_buf);
570 __free_page(page_buf);
572 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
574 return 0;
577 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
578 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
580 struct blkvsc_request *blkvsc_req;
581 struct page *page_buf;
582 unsigned char *buf;
583 struct scsi_sense_hdr sense_hdr;
585 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
587 blkdev->sector_size = 0;
588 blkdev->capacity = 0;
589 blkdev->media_not_present = 0; /* assume a disk is present */
591 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
592 if (!blkvsc_req)
593 return -ENOMEM;
595 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
596 page_buf = alloc_page(GFP_KERNEL);
597 if (!page_buf) {
598 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
599 return -ENOMEM;
602 init_waitqueue_head(&blkvsc_req->wevent);
603 blkvsc_req->dev = blkdev;
604 blkvsc_req->req = NULL;
605 blkvsc_req->write = 0;
607 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
608 blkvsc_req->request.DataBuffer.Offset = 0;
609 blkvsc_req->request.DataBuffer.Length = 8;
611 blkvsc_req->cmnd[0] = READ_CAPACITY;
612 blkvsc_req->cmd_len = 16;
615 * Set this here since the completion routine may be invoked
616 * and completed before we return
618 blkvsc_req->cond = 0;
620 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
622 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
623 blkvsc_req, blkvsc_req->cond);
625 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
627 /* check error */
628 if (blkvsc_req->request.Status) {
629 scsi_normalize_sense(blkvsc_req->sense_buffer,
630 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
632 if (sense_hdr.asc == 0x3A) {
633 /* Medium not present */
634 blkdev->media_not_present = 1;
636 return 0;
638 buf = kmap(page_buf);
640 /* be to le */
641 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
642 (buf[2] << 8) | buf[3]) + 1;
643 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
644 (buf[6] << 8) | buf[7];
646 kunmap(page_buf);
648 __free_page(page_buf);
650 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
652 return 0;
655 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
657 struct blkvsc_request *blkvsc_req;
658 struct page *page_buf;
659 unsigned char *buf;
660 struct scsi_sense_hdr sense_hdr;
662 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
664 blkdev->sector_size = 0;
665 blkdev->capacity = 0;
666 blkdev->media_not_present = 0; /* assume a disk is present */
668 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
669 if (!blkvsc_req)
670 return -ENOMEM;
672 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
673 page_buf = alloc_page(GFP_KERNEL);
674 if (!page_buf) {
675 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
676 return -ENOMEM;
679 init_waitqueue_head(&blkvsc_req->wevent);
680 blkvsc_req->dev = blkdev;
681 blkvsc_req->req = NULL;
682 blkvsc_req->write = 0;
684 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
685 blkvsc_req->request.DataBuffer.Offset = 0;
686 blkvsc_req->request.DataBuffer.Length = 12;
688 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
689 blkvsc_req->cmd_len = 16;
692 * Set this here since the completion routine may be invoked
693 * and completed before we return
695 blkvsc_req->cond = 0;
697 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
699 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
700 blkvsc_req, blkvsc_req->cond);
702 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
704 /* check error */
705 if (blkvsc_req->request.Status) {
706 scsi_normalize_sense(blkvsc_req->sense_buffer,
707 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
708 if (sense_hdr.asc == 0x3A) {
709 /* Medium not present */
710 blkdev->media_not_present = 1;
712 return 0;
714 buf = kmap(page_buf);
716 /* be to le */
717 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
718 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
720 #if 0
721 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
722 (buf[2] << 8) | buf[3]) + 1;
723 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
724 (buf[6] << 8) | buf[7];
725 #endif
727 kunmap(page_buf);
729 __free_page(page_buf);
731 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
733 return 0;
737 * blkvsc_remove() - Callback when our device is removed
739 static int blkvsc_remove(struct device *device)
741 struct driver_context *driver_ctx =
742 driver_to_driver_context(device->driver);
743 struct blkvsc_driver_context *blkvsc_drv_ctx =
744 (struct blkvsc_driver_context *)driver_ctx;
745 struct storvsc_driver_object *storvsc_drv_obj =
746 &blkvsc_drv_ctx->drv_obj;
747 struct device_context *device_ctx = device_to_device_context(device);
748 struct hv_device *device_obj = &device_ctx->device_obj;
749 struct block_device_context *blkdev = dev_get_drvdata(device);
750 unsigned long flags;
751 int ret;
753 DPRINT_ENTER(BLKVSC_DRV);
755 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
757 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
758 DPRINT_EXIT(BLKVSC_DRV);
759 return -1;
763 * Call to the vsc driver to let it know that the device is being
764 * removed
766 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
767 if (ret != 0) {
768 /* TODO: */
769 DPRINT_ERR(BLKVSC_DRV,
770 "unable to remove blkvsc device (ret %d)", ret);
773 /* Get to a known state */
774 spin_lock_irqsave(&blkdev->lock, flags);
776 blkdev->shutting_down = 1;
778 blk_stop_queue(blkdev->gd->queue);
780 spin_unlock_irqrestore(&blkdev->lock, flags);
782 while (blkdev->num_outstanding_reqs) {
783 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
784 blkdev->num_outstanding_reqs);
785 udelay(100);
788 blkvsc_do_flush(blkdev);
790 spin_lock_irqsave(&blkdev->lock, flags);
792 blkvsc_cancel_pending_reqs(blkdev);
794 spin_unlock_irqrestore(&blkdev->lock, flags);
796 blk_cleanup_queue(blkdev->gd->queue);
798 del_gendisk(blkdev->gd);
800 kmem_cache_destroy(blkdev->request_pool);
802 kfree(blkdev);
804 DPRINT_EXIT(BLKVSC_DRV);
806 return ret;
809 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
811 ASSERT(blkvsc_req->req);
812 ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8));
814 blkvsc_req->cmd_len = 16;
816 if (blkvsc_req->sector_start > 0xffffffff) {
817 if (rq_data_dir(blkvsc_req->req)) {
818 blkvsc_req->write = 1;
819 blkvsc_req->cmnd[0] = WRITE_16;
820 } else {
821 blkvsc_req->write = 0;
822 blkvsc_req->cmnd[0] = READ_16;
825 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
827 *(unsigned long long *)&blkvsc_req->cmnd[2] =
828 cpu_to_be64(blkvsc_req->sector_start);
829 *(unsigned int *)&blkvsc_req->cmnd[10] =
830 cpu_to_be32(blkvsc_req->sector_count);
831 } else if ((blkvsc_req->sector_count > 0xff) ||
832 (blkvsc_req->sector_start > 0x1fffff)) {
833 if (rq_data_dir(blkvsc_req->req)) {
834 blkvsc_req->write = 1;
835 blkvsc_req->cmnd[0] = WRITE_10;
836 } else {
837 blkvsc_req->write = 0;
838 blkvsc_req->cmnd[0] = READ_10;
841 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
843 *(unsigned int *)&blkvsc_req->cmnd[2] =
844 cpu_to_be32(blkvsc_req->sector_start);
845 *(unsigned short *)&blkvsc_req->cmnd[7] =
846 cpu_to_be16(blkvsc_req->sector_count);
847 } else {
848 if (rq_data_dir(blkvsc_req->req)) {
849 blkvsc_req->write = 1;
850 blkvsc_req->cmnd[0] = WRITE_6;
851 } else {
852 blkvsc_req->write = 0;
853 blkvsc_req->cmnd[0] = READ_6;
856 *(unsigned int *)&blkvsc_req->cmnd[1] =
857 cpu_to_be32(blkvsc_req->sector_start) >> 8;
858 blkvsc_req->cmnd[1] &= 0x1f;
859 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
863 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
864 void (*request_completion)(struct hv_storvsc_request *))
866 struct block_device_context *blkdev = blkvsc_req->dev;
867 struct device_context *device_ctx = blkdev->device_ctx;
868 struct driver_context *driver_ctx =
869 driver_to_driver_context(device_ctx->device.driver);
870 struct blkvsc_driver_context *blkvsc_drv_ctx =
871 (struct blkvsc_driver_context *)driver_ctx;
872 struct storvsc_driver_object *storvsc_drv_obj =
873 &blkvsc_drv_ctx->drv_obj;
874 struct hv_storvsc_request *storvsc_req;
875 int ret;
877 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
878 "req %p type %s start_sector %lu count %ld offset %d "
879 "len %d\n", blkvsc_req,
880 (blkvsc_req->write) ? "WRITE" : "READ",
881 (unsigned long) blkvsc_req->sector_start,
882 blkvsc_req->sector_count,
883 blkvsc_req->request.DataBuffer.Offset,
884 blkvsc_req->request.DataBuffer.Length);
885 #if 0
886 for (i = 0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++) {
887 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
888 "req %p pfn[%d] %llx\n",
889 blkvsc_req, i,
890 blkvsc_req->request.DataBuffer.PfnArray[i]);
892 #endif
894 storvsc_req = &blkvsc_req->request;
895 storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
896 sizeof(struct blkvsc_request));
898 storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
900 storvsc_req->OnIOCompletion = request_completion;
901 storvsc_req->Context = blkvsc_req;
903 storvsc_req->Host = blkdev->port;
904 storvsc_req->Bus = blkdev->path;
905 storvsc_req->TargetId = blkdev->target;
906 storvsc_req->LunId = 0; /* this is not really used at all */
908 storvsc_req->CdbLen = blkvsc_req->cmd_len;
909 storvsc_req->Cdb = blkvsc_req->cmnd;
911 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
912 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
914 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
915 &blkvsc_req->request);
916 if (ret == 0)
917 blkdev->num_outstanding_reqs++;
919 return ret;
923 * We break the request into 1 or more blkvsc_requests and submit
924 * them. If we cant submit them all, we put them on the
925 * pending_list. The blkvsc_request() will work on the pending_list.
927 static int blkvsc_do_request(struct block_device_context *blkdev,
928 struct request *req)
930 struct bio *bio = NULL;
931 struct bio_vec *bvec = NULL;
932 struct bio_vec *prev_bvec = NULL;
933 struct blkvsc_request *blkvsc_req = NULL;
934 struct blkvsc_request *tmp;
935 int databuf_idx = 0;
936 int seg_idx = 0;
937 sector_t start_sector;
938 unsigned long num_sectors = 0;
939 int ret = 0;
940 int pending = 0;
941 struct blkvsc_request_group *group = NULL;
943 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu \n", blkdev, req,
944 (unsigned long)blk_rq_pos(req));
946 /* Create a group to tie req to list of blkvsc_reqs */
947 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
948 if (!group)
949 return -ENOMEM;
951 INIT_LIST_HEAD(&group->blkvsc_req_list);
952 group->outstanding = group->status = 0;
954 start_sector = blk_rq_pos(req);
956 /* foreach bio in the request */
957 if (req->bio) {
958 for (bio = req->bio; bio; bio = bio->bi_next) {
960 * Map this bio into an existing or new storvsc request
962 bio_for_each_segment(bvec, bio, seg_idx) {
963 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
964 "- req %p bio %p bvec %p seg_idx %d "
965 "databuf_idx %d\n", req, bio, bvec,
966 seg_idx, databuf_idx);
968 /* Get a new storvsc request */
969 /* 1st-time */
970 if ((!blkvsc_req) ||
971 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
972 /* hole at the begin of page */
973 || (bvec->bv_offset != 0) ||
974 /* hold at the end of page */
975 (prev_bvec &&
976 (prev_bvec->bv_len != PAGE_SIZE))) {
977 /* submit the prev one */
978 if (blkvsc_req) {
979 blkvsc_req->sector_start = start_sector;
980 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
982 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
983 blkvsc_init_rw(blkvsc_req);
987 * Create new blkvsc_req to represent
988 * the current bvec
990 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
991 if (!blkvsc_req) {
992 /* free up everything */
993 list_for_each_entry_safe(
994 blkvsc_req, tmp,
995 &group->blkvsc_req_list,
996 req_entry) {
997 list_del(&blkvsc_req->req_entry);
998 kmem_cache_free(blkdev->request_pool, blkvsc_req);
1001 kmem_cache_free(blkdev->request_pool, group);
1002 return -ENOMEM;
1005 memset(blkvsc_req, 0,
1006 sizeof(struct blkvsc_request));
1008 blkvsc_req->dev = blkdev;
1009 blkvsc_req->req = req;
1010 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1011 blkvsc_req->request.DataBuffer.Length = 0;
1013 /* Add to the group */
1014 blkvsc_req->group = group;
1015 blkvsc_req->group->outstanding++;
1016 list_add_tail(&blkvsc_req->req_entry,
1017 &blkvsc_req->group->blkvsc_req_list);
1019 start_sector += num_sectors;
1020 num_sectors = 0;
1021 databuf_idx = 0;
1024 /* Add the curr bvec/segment to the curr blkvsc_req */
1025 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1026 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1028 prev_bvec = bvec;
1030 databuf_idx++;
1031 num_sectors += bvec->bv_len >> 9;
1033 } /* bio_for_each_segment */
1035 } /* rq_for_each_bio */
1038 /* Handle the last one */
1039 if (blkvsc_req) {
1040 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1041 blkdev, req, blkvsc_req->group,
1042 blkvsc_req->group->outstanding);
1044 blkvsc_req->sector_start = start_sector;
1045 sector_div(blkvsc_req->sector_start,
1046 (blkdev->sector_size >> 9));
1048 blkvsc_req->sector_count = num_sectors /
1049 (blkdev->sector_size >> 9);
1051 blkvsc_init_rw(blkvsc_req);
1054 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1055 if (pending) {
1056 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1057 "pending_list - blkvsc_req %p start_sect %lu"
1058 " sect_count %ld (%lu %ld)\n", blkvsc_req,
1059 (unsigned long)blkvsc_req->sector_start,
1060 blkvsc_req->sector_count,
1061 (unsigned long)start_sector,
1062 (unsigned long)num_sectors);
1064 list_add_tail(&blkvsc_req->pend_entry,
1065 &blkdev->pending_list);
1066 } else {
1067 ret = blkvsc_submit_request(blkvsc_req,
1068 blkvsc_request_completion);
1069 if (ret == -1) {
1070 pending = 1;
1071 list_add_tail(&blkvsc_req->pend_entry,
1072 &blkdev->pending_list);
1075 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1076 "start_sect %lu sect_count %ld (%lu %ld) "
1077 "ret %d\n", blkvsc_req,
1078 (unsigned long)blkvsc_req->sector_start,
1079 blkvsc_req->sector_count,
1080 (unsigned long)start_sector,
1081 num_sectors, ret);
1085 return pending;
1088 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1090 struct blkvsc_request *blkvsc_req =
1091 (struct blkvsc_request *)request->Context;
1092 struct block_device_context *blkdev =
1093 (struct block_device_context *)blkvsc_req->dev;
1094 struct scsi_sense_hdr sense_hdr;
1096 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1097 blkvsc_req);
1099 blkdev->num_outstanding_reqs--;
1101 if (blkvsc_req->request.Status)
1102 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1103 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1104 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1106 blkvsc_req->cond = 1;
1107 wake_up_interruptible(&blkvsc_req->wevent);
1110 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1112 struct blkvsc_request *blkvsc_req =
1113 (struct blkvsc_request *)request->Context;
1114 struct block_device_context *blkdev =
1115 (struct block_device_context *)blkvsc_req->dev;
1116 unsigned long flags;
1117 struct blkvsc_request *comp_req, *tmp;
1119 ASSERT(blkvsc_req->group);
1121 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1122 "sect_start %lu sect_count %ld len %d group outstd %d "
1123 "total outstd %d\n",
1124 blkdev, blkvsc_req, blkvsc_req->group,
1125 (blkvsc_req->write) ? "WRITE" : "READ",
1126 (unsigned long)blkvsc_req->sector_start,
1127 blkvsc_req->sector_count,
1128 blkvsc_req->request.DataBuffer.Length,
1129 blkvsc_req->group->outstanding,
1130 blkdev->num_outstanding_reqs);
1132 spin_lock_irqsave(&blkdev->lock, flags);
1134 blkdev->num_outstanding_reqs--;
1135 blkvsc_req->group->outstanding--;
1138 * Only start processing when all the blkvsc_reqs are
1139 * completed. This guarantees no out-of-order blkvsc_req
1140 * completion when calling end_that_request_first()
1142 if (blkvsc_req->group->outstanding == 0) {
1143 list_for_each_entry_safe(comp_req, tmp,
1144 &blkvsc_req->group->blkvsc_req_list,
1145 req_entry) {
1146 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1147 "sect_start %lu sect_count %ld \n",
1148 comp_req,
1149 (unsigned long)comp_req->sector_start,
1150 comp_req->sector_count);
1152 list_del(&comp_req->req_entry);
1154 if (!__blk_end_request(comp_req->req,
1155 (!comp_req->request.Status ? 0 : -EIO),
1156 comp_req->sector_count * blkdev->sector_size)) {
1158 * All the sectors have been xferred ie the
1159 * request is done
1161 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1162 comp_req->req);
1163 kmem_cache_free(blkdev->request_pool,
1164 comp_req->group);
1167 kmem_cache_free(blkdev->request_pool, comp_req);
1170 if (!blkdev->shutting_down) {
1171 blkvsc_do_pending_reqs(blkdev);
1172 blk_start_queue(blkdev->gd->queue);
1173 blkvsc_request(blkdev->gd->queue);
1177 spin_unlock_irqrestore(&blkdev->lock, flags);
1180 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1182 struct blkvsc_request *pend_req, *tmp;
1183 struct blkvsc_request *comp_req, *tmp2;
1185 int ret = 0;
1187 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1189 /* Flush the pending list first */
1190 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1191 pend_entry) {
1193 * The pend_req could be part of a partially completed
1194 * request. If so, complete those req first until we
1195 * hit the pend_req
1197 list_for_each_entry_safe(comp_req, tmp2,
1198 &pend_req->group->blkvsc_req_list,
1199 req_entry) {
1200 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1201 "sect_start %lu sect_count %ld \n",
1202 comp_req,
1203 (unsigned long) comp_req->sector_start,
1204 comp_req->sector_count);
1206 if (comp_req == pend_req)
1207 break;
1209 list_del(&comp_req->req_entry);
1211 if (comp_req->req) {
1212 ret = __blk_end_request(comp_req->req,
1213 (!comp_req->request.Status ? 0 : -EIO),
1214 comp_req->sector_count *
1215 blkdev->sector_size);
1216 ASSERT(ret != 0);
1219 kmem_cache_free(blkdev->request_pool, comp_req);
1222 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1223 pend_req);
1225 list_del(&pend_req->pend_entry);
1227 list_del(&pend_req->req_entry);
1229 if (comp_req->req) {
1230 if (!__blk_end_request(pend_req->req, -EIO,
1231 pend_req->sector_count *
1232 blkdev->sector_size)) {
1234 * All the sectors have been xferred ie the
1235 * request is done
1237 DPRINT_DBG(BLKVSC_DRV,
1238 "blkvsc_cancel_pending_reqs() - "
1239 "req %p COMPLETED\n", pend_req->req);
1240 kmem_cache_free(blkdev->request_pool,
1241 pend_req->group);
1245 kmem_cache_free(blkdev->request_pool, pend_req);
1248 return ret;
1251 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1253 struct blkvsc_request *pend_req, *tmp;
1254 int ret = 0;
1256 /* Flush the pending list first */
1257 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1258 pend_entry) {
1259 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1260 pend_req);
1262 ret = blkvsc_submit_request(pend_req,
1263 blkvsc_request_completion);
1264 if (ret != 0)
1265 break;
1266 else
1267 list_del(&pend_req->pend_entry);
1270 return ret;
1273 static void blkvsc_request(struct request_queue *queue)
1275 struct block_device_context *blkdev = NULL;
1276 struct request *req;
1277 int ret = 0;
1279 DPRINT_DBG(BLKVSC_DRV, "- enter \n");
1280 while ((req = blk_peek_request(queue)) != NULL) {
1281 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1283 blkdev = req->rq_disk->private_data;
1284 if (blkdev->shutting_down || !blk_fs_request(req) ||
1285 blkdev->media_not_present) {
1286 __blk_end_request_cur(req, 0);
1287 continue;
1290 ret = blkvsc_do_pending_reqs(blkdev);
1292 if (ret != 0) {
1293 DPRINT_DBG(BLKVSC_DRV,
1294 "- stop queue - pending_list not empty\n");
1295 blk_stop_queue(queue);
1296 break;
1299 blk_start_request(req);
1301 ret = blkvsc_do_request(blkdev, req);
1302 if (ret > 0) {
1303 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1304 blk_stop_queue(queue);
1305 break;
1306 } else if (ret < 0) {
1307 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1308 blk_requeue_request(queue, req);
1309 blk_stop_queue(queue);
1310 break;
1315 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1317 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1319 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1320 blkdev->gd->disk_name);
1322 spin_lock(&blkdev->lock);
1324 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1325 spin_unlock(&blkdev->lock);
1326 check_disk_change(bdev);
1327 spin_lock(&blkdev->lock);
1330 blkdev->users++;
1332 spin_unlock(&blkdev->lock);
1333 return 0;
1336 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1338 struct block_device_context *blkdev = disk->private_data;
1340 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1341 blkdev->gd->disk_name);
1343 spin_lock(&blkdev->lock);
1344 if (blkdev->users == 1) {
1345 spin_unlock(&blkdev->lock);
1346 blkvsc_do_flush(blkdev);
1347 spin_lock(&blkdev->lock);
1350 blkdev->users--;
1352 spin_unlock(&blkdev->lock);
1353 return 0;
1356 static int blkvsc_media_changed(struct gendisk *gd)
1358 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1359 return 1;
1362 static int blkvsc_revalidate_disk(struct gendisk *gd)
1364 struct block_device_context *blkdev = gd->private_data;
1366 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1368 if (blkdev->device_type == DVD_TYPE) {
1369 blkvsc_do_read_capacity(blkdev);
1370 set_capacity(blkdev->gd, blkdev->capacity *
1371 (blkdev->sector_size/512));
1372 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1374 return 0;
1377 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1379 sector_t total_sectors = get_capacity(bd->bd_disk);
1380 sector_t cylinder_times_heads = 0;
1381 sector_t temp = 0;
1383 int sectors_per_track = 0;
1384 int heads = 0;
1385 int cylinders = 0;
1386 int rem = 0;
1388 if (total_sectors > (65535 * 16 * 255))
1389 total_sectors = (65535 * 16 * 255);
1391 if (total_sectors >= (65535 * 16 * 63)) {
1392 sectors_per_track = 255;
1393 heads = 16;
1395 cylinder_times_heads = total_sectors;
1396 /* sector_div stores the quotient in cylinder_times_heads */
1397 rem = sector_div(cylinder_times_heads, sectors_per_track);
1398 } else {
1399 sectors_per_track = 17;
1401 cylinder_times_heads = total_sectors;
1402 /* sector_div stores the quotient in cylinder_times_heads */
1403 rem = sector_div(cylinder_times_heads, sectors_per_track);
1405 temp = cylinder_times_heads + 1023;
1406 /* sector_div stores the quotient in temp */
1407 rem = sector_div(temp, 1024);
1409 heads = temp;
1411 if (heads < 4)
1412 heads = 4;
1415 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1416 sectors_per_track = 31;
1417 heads = 16;
1419 cylinder_times_heads = total_sectors;
1421 * sector_div stores the quotient in
1422 * cylinder_times_heads
1424 rem = sector_div(cylinder_times_heads,
1425 sectors_per_track);
1428 if (cylinder_times_heads >= (heads * 1024)) {
1429 sectors_per_track = 63;
1430 heads = 16;
1432 cylinder_times_heads = total_sectors;
1434 * sector_div stores the quotient in
1435 * cylinder_times_heads
1437 rem = sector_div(cylinder_times_heads,
1438 sectors_per_track);
1442 temp = cylinder_times_heads;
1443 /* sector_div stores the quotient in temp */
1444 rem = sector_div(temp, heads);
1445 cylinders = temp;
1447 hg->heads = heads;
1448 hg->sectors = sectors_per_track;
1449 hg->cylinders = cylinders;
1451 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1452 sectors_per_track);
1454 return 0;
1457 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1458 unsigned cmd, unsigned long argument)
1460 /* struct block_device_context *blkdev = bd->bd_disk->private_data; */
1461 int ret;
1463 switch (cmd) {
1465 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1466 * than just a GUID. Commented it out for now.
1468 #if 0
1469 case HDIO_GET_IDENTITY:
1470 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1471 if (copy_to_user((void __user *)arg, blkdev->device_id,
1472 blkdev->device_id_len))
1473 ret = -EFAULT;
1474 break;
1475 #endif
1476 default:
1477 ret = -EINVAL;
1478 break;
1481 return ret;
1484 static int __init blkvsc_init(void)
1486 int ret;
1488 ASSERT(sizeof(sector_t) == 8); /* Make sure CONFIG_LBD is set */
1490 DPRINT_ENTER(BLKVSC_DRV);
1492 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1494 ret = blkvsc_drv_init(BlkVscInitialize);
1496 DPRINT_EXIT(BLKVSC_DRV);
1498 return ret;
1501 static void __exit blkvsc_exit(void)
1503 DPRINT_ENTER(BLKVSC_DRV);
1504 blkvsc_drv_exit();
1505 DPRINT_ENTER(BLKVSC_DRV);
1508 MODULE_LICENSE("GPL");
1509 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
1510 module_init(blkvsc_init);
1511 module_exit(blkvsc_exit);