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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
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>
34 #include "VersionInfo.h"
36 #include "StorVscApi.h"
39 #define BLKVSC_MINORS 64
41 enum blkvsc_device_type {
48 * This request ties the struct request and struct
49 * blkvsc_request/hv_storvsc_request together A struct request may be
50 * represented by 1 or more struct blkvsc_request
52 struct blkvsc_request_group {
55 struct list_head blkvsc_req_list; /* list of blkvsc_requests */
58 struct blkvsc_request {
59 /* blkvsc_request_group.blkvsc_req_list */
60 struct list_head req_entry;
62 /* block_device_context.pending_list */
63 struct list_head pend_entry;
65 /* This may be null if we generate a request internally */
68 struct block_device_context *dev;
70 /* The group this request is part of. Maybe null */
71 struct blkvsc_request_group *group;
73 wait_queue_head_t wevent;
77 sector_t sector_start;
78 unsigned long sector_count;
80 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
81 unsigned char cmd_len;
82 unsigned char cmnd[MAX_COMMAND_SIZE];
84 struct hv_storvsc_request request;
86 * !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap,
87 * because - The extension buffer falls right here and is pointed to by
89 * Which sounds like a horrible idea, who designed this?
93 /* Per device structure */
94 struct block_device_context {
95 /* point back to our device context */
96 struct vm_device *device_ctx;
97 struct kmem_cache *request_pool;
100 enum blkvsc_device_type device_type;
101 struct list_head pending_list;
103 unsigned char device_id[64];
104 unsigned int device_id_len;
105 int num_outstanding_reqs;
107 int media_not_present;
108 unsigned int sector_size;
112 unsigned char target;
117 struct blkvsc_driver_context {
118 /* !! These must be the first 2 fields !! */
119 /* FIXME this is a bug! */
120 struct driver_context drv_ctx;
121 struct storvsc_driver_object drv_obj;
125 static int blkvsc_probe(struct device *dev);
126 static int blkvsc_remove(struct device *device);
127 static void blkvsc_shutdown(struct device *device);
129 static int blkvsc_open(struct block_device *bdev, fmode_t mode);
130 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
131 static int blkvsc_media_changed(struct gendisk *gd);
132 static int blkvsc_revalidate_disk(struct gendisk *gd);
133 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
134 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
135 unsigned cmd, unsigned long argument);
136 static void blkvsc_request(struct request_queue *queue);
137 static void blkvsc_request_completion(struct hv_storvsc_request *request);
138 static int blkvsc_do_request(struct block_device_context *blkdev,
139 struct request *req);
140 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
141 void (*request_completion)(struct hv_storvsc_request *));
142 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
143 static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
144 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
145 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
146 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
147 static int blkvsc_do_flush(struct block_device_context *blkdev);
148 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
149 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
152 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
154 /* The one and only one */
155 static struct blkvsc_driver_context g_blkvsc_drv;
157 static struct block_device_operations block_ops = {
158 .owner = THIS_MODULE,
160 .release = blkvsc_release,
161 .media_changed = blkvsc_media_changed,
162 .revalidate_disk = blkvsc_revalidate_disk,
163 .getgeo = blkvsc_getgeo,
164 .ioctl = blkvsc_ioctl,
168 * blkvsc_drv_init - BlkVsc driver initialization.
170 static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
172 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
173 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 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,
187 sizeof(struct hv_guid));
189 drv_ctx->probe = blkvsc_probe;
190 drv_ctx->remove = blkvsc_remove;
191 drv_ctx->shutdown = blkvsc_shutdown;
193 /* The driver belongs to vmbus */
194 ret = vmbus_child_driver_register(drv_ctx);
196 DPRINT_EXIT(BLKVSC_DRV);
201 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
203 struct device **curr = (struct device **)data;
205 return 1; /* stop iterating */
208 static void blkvsc_drv_exit(void)
210 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
211 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
212 struct device *current_dev;
215 DPRINT_ENTER(BLKVSC_DRV);
221 ret = driver_for_each_device(&drv_ctx->driver, NULL,
222 (void *) ¤t_dev,
226 DPRINT_WARN(BLKVSC_DRV,
227 "driver_for_each_device returned %d", ret);
230 if (current_dev == NULL)
233 /* Initiate removal from the top-down */
234 device_unregister(current_dev);
237 if (storvsc_drv_obj->Base.OnCleanup)
238 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
240 vmbus_child_driver_unregister(drv_ctx);
242 DPRINT_EXIT(BLKVSC_DRV);
248 * blkvsc_probe - Add a new device for this driver
250 static int blkvsc_probe(struct device *device)
252 struct driver_context *driver_ctx =
253 driver_to_driver_context(device->driver);
254 struct blkvsc_driver_context *blkvsc_drv_ctx =
255 (struct blkvsc_driver_context *)driver_ctx;
256 struct storvsc_driver_object *storvsc_drv_obj =
257 &blkvsc_drv_ctx->drv_obj;
258 struct vm_device *device_ctx = device_to_vm_device(device);
259 struct hv_device *device_obj = &device_ctx->device_obj;
261 struct block_device_context *blkdev = NULL;
262 struct storvsc_device_info device_info;
266 static int ide0_registered;
267 static int ide1_registered;
269 DPRINT_ENTER(BLKVSC_DRV);
271 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
273 if (!storvsc_drv_obj->Base.OnDeviceAdd) {
274 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
279 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
285 INIT_LIST_HEAD(&blkdev->pending_list);
287 /* Initialize what we can here */
288 spin_lock_init(&blkdev->lock);
290 ASSERT(sizeof(struct blkvsc_request_group) <=
291 sizeof(struct blkvsc_request));
293 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
294 sizeof(struct blkvsc_request) +
295 storvsc_drv_obj->RequestExtSize, 0,
296 SLAB_HWCACHE_ALIGN, NULL);
297 if (!blkdev->request_pool) {
303 /* Call to the vsc driver to add the device */
304 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
306 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
310 blkdev->device_ctx = device_ctx;
311 /* this identified the device 0 or 1 */
312 blkdev->target = device_info.TargetId;
313 /* this identified the ide ctrl 0 or 1 */
314 blkdev->path = device_info.PathId;
316 dev_set_drvdata(device, blkdev);
318 /* Calculate the major and device num */
319 if (blkdev->path == 0) {
321 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
323 if (!ide0_registered) {
324 ret = register_blkdev(major, "ide");
326 DPRINT_ERR(BLKVSC_DRV,
327 "register_blkdev() failed! ret %d",
334 } else if (blkdev->path == 1) {
336 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
338 if (!ide1_registered) {
339 ret = register_blkdev(major, "ide");
341 DPRINT_ERR(BLKVSC_DRV,
342 "register_blkdev() failed! ret %d",
350 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
355 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
357 blkdev->gd = alloc_disk(BLKVSC_MINORS);
359 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
364 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
366 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
367 blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
368 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
369 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
370 blk_queue_dma_alignment(blkdev->gd->queue, 511);
372 blkdev->gd->major = major;
373 if (devnum == 1 || devnum == 3)
374 blkdev->gd->first_minor = BLKVSC_MINORS;
376 blkdev->gd->first_minor = 0;
377 blkdev->gd->fops = &block_ops;
378 blkdev->gd->private_data = blkdev;
379 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
381 blkvsc_do_inquiry(blkdev);
382 if (blkdev->device_type == DVD_TYPE) {
383 set_disk_ro(blkdev->gd, 1);
384 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
385 blkvsc_do_read_capacity(blkdev);
387 blkvsc_do_read_capacity16(blkdev);
390 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
391 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
393 add_disk(blkdev->gd);
395 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
396 blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
397 blkdev->sector_size);
402 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
406 if (blkdev->request_pool) {
407 kmem_cache_destroy(blkdev->request_pool);
408 blkdev->request_pool = NULL;
414 DPRINT_EXIT(BLKVSC_DRV);
419 static void blkvsc_shutdown(struct device *device)
421 struct block_device_context *blkdev = dev_get_drvdata(device);
427 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
428 blkdev->users, blkdev->gd->disk_name);
430 spin_lock_irqsave(&blkdev->lock, flags);
432 blkdev->shutting_down = 1;
434 blk_stop_queue(blkdev->gd->queue);
436 spin_unlock_irqrestore(&blkdev->lock, flags);
438 while (blkdev->num_outstanding_reqs) {
439 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
440 blkdev->num_outstanding_reqs);
444 blkvsc_do_flush(blkdev);
446 spin_lock_irqsave(&blkdev->lock, flags);
448 blkvsc_cancel_pending_reqs(blkdev);
450 spin_unlock_irqrestore(&blkdev->lock, flags);
453 static int blkvsc_do_flush(struct block_device_context *blkdev)
455 struct blkvsc_request *blkvsc_req;
457 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
459 if (blkdev->device_type != HARDDISK_TYPE)
462 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
466 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
467 init_waitqueue_head(&blkvsc_req->wevent);
468 blkvsc_req->dev = blkdev;
469 blkvsc_req->req = NULL;
470 blkvsc_req->write = 0;
472 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
473 blkvsc_req->request.DataBuffer.Offset = 0;
474 blkvsc_req->request.DataBuffer.Length = 0;
476 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
477 blkvsc_req->cmd_len = 10;
480 * Set this here since the completion routine may be invoked and
481 * completed before we return
483 blkvsc_req->cond = 0;
484 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
486 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
488 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
493 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
494 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
496 struct blkvsc_request *blkvsc_req;
497 struct page *page_buf;
499 unsigned char device_type;
501 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
503 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
507 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
508 page_buf = alloc_page(GFP_KERNEL);
510 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
514 init_waitqueue_head(&blkvsc_req->wevent);
515 blkvsc_req->dev = blkdev;
516 blkvsc_req->req = NULL;
517 blkvsc_req->write = 0;
519 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
520 blkvsc_req->request.DataBuffer.Offset = 0;
521 blkvsc_req->request.DataBuffer.Length = 64;
523 blkvsc_req->cmnd[0] = INQUIRY;
524 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
525 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
526 blkvsc_req->cmnd[4] = 64;
527 blkvsc_req->cmd_len = 6;
530 * Set this here since the completion routine may be invoked and
531 * completed before we return
533 blkvsc_req->cond = 0;
535 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
537 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
538 blkvsc_req, blkvsc_req->cond);
540 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
542 buf = kmap(page_buf);
544 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
546 device_type = buf[0] & 0x1F;
548 if (device_type == 0x0) {
549 blkdev->device_type = HARDDISK_TYPE;
550 } else if (device_type == 0x5) {
551 blkdev->device_type = DVD_TYPE;
553 /* TODO: this is currently unsupported device type */
554 blkdev->device_type = UNKNOWN_DEV_TYPE;
557 DPRINT_DBG(BLKVSC_DRV, "device type %d \n", device_type);
559 blkdev->device_id_len = buf[7];
560 if (blkdev->device_id_len > 64)
561 blkdev->device_id_len = 64;
563 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
564 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
565 * blkdev->device_id_len); */
569 __free_page(page_buf);
571 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
576 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
577 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
579 struct blkvsc_request *blkvsc_req;
580 struct page *page_buf;
582 struct scsi_sense_hdr sense_hdr;
584 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
586 blkdev->sector_size = 0;
587 blkdev->capacity = 0;
588 blkdev->media_not_present = 0; /* assume a disk is present */
590 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
594 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
595 page_buf = alloc_page(GFP_KERNEL);
597 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
601 init_waitqueue_head(&blkvsc_req->wevent);
602 blkvsc_req->dev = blkdev;
603 blkvsc_req->req = NULL;
604 blkvsc_req->write = 0;
606 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
607 blkvsc_req->request.DataBuffer.Offset = 0;
608 blkvsc_req->request.DataBuffer.Length = 8;
610 blkvsc_req->cmnd[0] = READ_CAPACITY;
611 blkvsc_req->cmd_len = 16;
614 * Set this here since the completion routine may be invoked
615 * and completed before we return
617 blkvsc_req->cond = 0;
619 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
621 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
622 blkvsc_req, blkvsc_req->cond);
624 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
627 if (blkvsc_req->request.Status) {
628 scsi_normalize_sense(blkvsc_req->sense_buffer,
629 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
631 if (sense_hdr.asc == 0x3A) {
632 /* Medium not present */
633 blkdev->media_not_present = 1;
637 buf = kmap(page_buf);
640 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
641 (buf[2] << 8) | buf[3]) + 1;
642 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
643 (buf[6] << 8) | buf[7];
647 __free_page(page_buf);
649 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
654 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
656 struct blkvsc_request *blkvsc_req;
657 struct page *page_buf;
659 struct scsi_sense_hdr sense_hdr;
661 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
663 blkdev->sector_size = 0;
664 blkdev->capacity = 0;
665 blkdev->media_not_present = 0; /* assume a disk is present */
667 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
671 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
672 page_buf = alloc_page(GFP_KERNEL);
674 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
678 init_waitqueue_head(&blkvsc_req->wevent);
679 blkvsc_req->dev = blkdev;
680 blkvsc_req->req = NULL;
681 blkvsc_req->write = 0;
683 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
684 blkvsc_req->request.DataBuffer.Offset = 0;
685 blkvsc_req->request.DataBuffer.Length = 12;
687 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
688 blkvsc_req->cmd_len = 16;
691 * Set this here since the completion routine may be invoked
692 * and completed before we return
694 blkvsc_req->cond = 0;
696 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
698 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
699 blkvsc_req, blkvsc_req->cond);
701 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
704 if (blkvsc_req->request.Status) {
705 scsi_normalize_sense(blkvsc_req->sense_buffer,
706 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
707 if (sense_hdr.asc == 0x3A) {
708 /* Medium not present */
709 blkdev->media_not_present = 1;
713 buf = kmap(page_buf);
716 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
717 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
720 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
721 (buf[2] << 8) | buf[3]) + 1;
722 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
723 (buf[6] << 8) | buf[7];
728 __free_page(page_buf);
730 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
736 * blkvsc_remove() - Callback when our device is removed
738 static int blkvsc_remove(struct device *device)
740 struct driver_context *driver_ctx =
741 driver_to_driver_context(device->driver);
742 struct blkvsc_driver_context *blkvsc_drv_ctx =
743 (struct blkvsc_driver_context *)driver_ctx;
744 struct storvsc_driver_object *storvsc_drv_obj =
745 &blkvsc_drv_ctx->drv_obj;
746 struct vm_device *device_ctx = device_to_vm_device(device);
747 struct hv_device *device_obj = &device_ctx->device_obj;
748 struct block_device_context *blkdev = dev_get_drvdata(device);
752 DPRINT_ENTER(BLKVSC_DRV);
754 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
756 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
757 DPRINT_EXIT(BLKVSC_DRV);
762 * Call to the vsc driver to let it know that the device is being
765 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
768 DPRINT_ERR(BLKVSC_DRV,
769 "unable to remove blkvsc device (ret %d)", ret);
772 /* Get to a known state */
773 spin_lock_irqsave(&blkdev->lock, flags);
775 blkdev->shutting_down = 1;
777 blk_stop_queue(blkdev->gd->queue);
779 spin_unlock_irqrestore(&blkdev->lock, flags);
781 while (blkdev->num_outstanding_reqs) {
782 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
783 blkdev->num_outstanding_reqs);
787 blkvsc_do_flush(blkdev);
789 spin_lock_irqsave(&blkdev->lock, flags);
791 blkvsc_cancel_pending_reqs(blkdev);
793 spin_unlock_irqrestore(&blkdev->lock, flags);
795 blk_cleanup_queue(blkdev->gd->queue);
797 del_gendisk(blkdev->gd);
799 kmem_cache_destroy(blkdev->request_pool);
803 DPRINT_EXIT(BLKVSC_DRV);
808 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
810 ASSERT(blkvsc_req->req);
811 ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8));
813 blkvsc_req->cmd_len = 16;
815 if (blkvsc_req->sector_start > 0xffffffff) {
816 if (rq_data_dir(blkvsc_req->req)) {
817 blkvsc_req->write = 1;
818 blkvsc_req->cmnd[0] = WRITE_16;
820 blkvsc_req->write = 0;
821 blkvsc_req->cmnd[0] = READ_16;
824 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
826 *(unsigned long long *)&blkvsc_req->cmnd[2] =
827 cpu_to_be64(blkvsc_req->sector_start);
828 *(unsigned int *)&blkvsc_req->cmnd[10] =
829 cpu_to_be32(blkvsc_req->sector_count);
830 } else if ((blkvsc_req->sector_count > 0xff) ||
831 (blkvsc_req->sector_start > 0x1fffff)) {
832 if (rq_data_dir(blkvsc_req->req)) {
833 blkvsc_req->write = 1;
834 blkvsc_req->cmnd[0] = WRITE_10;
836 blkvsc_req->write = 0;
837 blkvsc_req->cmnd[0] = READ_10;
840 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
842 *(unsigned int *)&blkvsc_req->cmnd[2] =
843 cpu_to_be32(blkvsc_req->sector_start);
844 *(unsigned short *)&blkvsc_req->cmnd[7] =
845 cpu_to_be16(blkvsc_req->sector_count);
847 if (rq_data_dir(blkvsc_req->req)) {
848 blkvsc_req->write = 1;
849 blkvsc_req->cmnd[0] = WRITE_6;
851 blkvsc_req->write = 0;
852 blkvsc_req->cmnd[0] = READ_6;
855 *(unsigned int *)&blkvsc_req->cmnd[1] =
856 cpu_to_be32(blkvsc_req->sector_start) >> 8;
857 blkvsc_req->cmnd[1] &= 0x1f;
858 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
862 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
863 void (*request_completion)(struct hv_storvsc_request *))
865 struct block_device_context *blkdev = blkvsc_req->dev;
866 struct vm_device *device_ctx = blkdev->device_ctx;
867 struct driver_context *driver_ctx =
868 driver_to_driver_context(device_ctx->device.driver);
869 struct blkvsc_driver_context *blkvsc_drv_ctx =
870 (struct blkvsc_driver_context *)driver_ctx;
871 struct storvsc_driver_object *storvsc_drv_obj =
872 &blkvsc_drv_ctx->drv_obj;
873 struct hv_storvsc_request *storvsc_req;
876 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
877 "req %p type %s start_sector %lu count %ld offset %d "
878 "len %d\n", blkvsc_req,
879 (blkvsc_req->write) ? "WRITE" : "READ",
880 (unsigned long) blkvsc_req->sector_start,
881 blkvsc_req->sector_count,
882 blkvsc_req->request.DataBuffer.Offset,
883 blkvsc_req->request.DataBuffer.Length);
885 for (i = 0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++) {
886 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
887 "req %p pfn[%d] %llx\n",
889 blkvsc_req->request.DataBuffer.PfnArray[i]);
893 storvsc_req = &blkvsc_req->request;
894 storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
895 sizeof(struct blkvsc_request));
897 storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
899 storvsc_req->OnIOCompletion = request_completion;
900 storvsc_req->Context = blkvsc_req;
902 storvsc_req->Host = blkdev->port;
903 storvsc_req->Bus = blkdev->path;
904 storvsc_req->TargetId = blkdev->target;
905 storvsc_req->LunId = 0; /* this is not really used at all */
907 storvsc_req->CdbLen = blkvsc_req->cmd_len;
908 storvsc_req->Cdb = blkvsc_req->cmnd;
910 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
911 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
913 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
914 &blkvsc_req->request);
916 blkdev->num_outstanding_reqs++;
922 * We break the request into 1 or more blkvsc_requests and submit
923 * them. If we cant submit them all, we put them on the
924 * pending_list. The blkvsc_request() will work on the pending_list.
926 static int blkvsc_do_request(struct block_device_context *blkdev,
929 struct bio *bio = NULL;
930 struct bio_vec *bvec = NULL;
931 struct bio_vec *prev_bvec = NULL;
932 struct blkvsc_request *blkvsc_req = NULL;
933 struct blkvsc_request *tmp;
936 sector_t start_sector;
937 unsigned long num_sectors = 0;
940 struct blkvsc_request_group *group = NULL;
942 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu \n", blkdev, req,
943 (unsigned long)blk_rq_pos(req));
945 /* Create a group to tie req to list of blkvsc_reqs */
946 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
950 INIT_LIST_HEAD(&group->blkvsc_req_list);
951 group->outstanding = group->status = 0;
953 start_sector = blk_rq_pos(req);
955 /* foreach bio in the request */
957 for (bio = req->bio; bio; bio = bio->bi_next) {
959 * Map this bio into an existing or new storvsc request
961 bio_for_each_segment(bvec, bio, seg_idx) {
962 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
963 "- req %p bio %p bvec %p seg_idx %d "
964 "databuf_idx %d\n", req, bio, bvec,
965 seg_idx, databuf_idx);
967 /* Get a new storvsc request */
970 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
971 /* hole at the begin of page */
972 || (bvec->bv_offset != 0) ||
973 /* hold at the end of page */
975 (prev_bvec->bv_len != PAGE_SIZE))) {
976 /* submit the prev one */
978 blkvsc_req->sector_start = start_sector;
979 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
981 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
982 blkvsc_init_rw(blkvsc_req);
986 * Create new blkvsc_req to represent
989 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
991 /* free up everything */
992 list_for_each_entry_safe(
994 &group->blkvsc_req_list,
996 list_del(&blkvsc_req->req_entry);
997 kmem_cache_free(blkdev->request_pool, blkvsc_req);
1000 kmem_cache_free(blkdev->request_pool, group);
1004 memset(blkvsc_req, 0,
1005 sizeof(struct blkvsc_request));
1007 blkvsc_req->dev = blkdev;
1008 blkvsc_req->req = req;
1009 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1010 blkvsc_req->request.DataBuffer.Length = 0;
1012 /* Add to the group */
1013 blkvsc_req->group = group;
1014 blkvsc_req->group->outstanding++;
1015 list_add_tail(&blkvsc_req->req_entry,
1016 &blkvsc_req->group->blkvsc_req_list);
1018 start_sector += num_sectors;
1023 /* Add the curr bvec/segment to the curr blkvsc_req */
1024 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1025 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1030 num_sectors += bvec->bv_len >> 9;
1032 } /* bio_for_each_segment */
1034 } /* rq_for_each_bio */
1037 /* Handle the last one */
1039 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1040 blkdev, req, blkvsc_req->group,
1041 blkvsc_req->group->outstanding);
1043 blkvsc_req->sector_start = start_sector;
1044 sector_div(blkvsc_req->sector_start,
1045 (blkdev->sector_size >> 9));
1047 blkvsc_req->sector_count = num_sectors /
1048 (blkdev->sector_size >> 9);
1050 blkvsc_init_rw(blkvsc_req);
1053 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1055 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1056 "pending_list - blkvsc_req %p start_sect %lu"
1057 " sect_count %ld (%lu %ld)\n", blkvsc_req,
1058 (unsigned long)blkvsc_req->sector_start,
1059 blkvsc_req->sector_count,
1060 (unsigned long)start_sector,
1061 (unsigned long)num_sectors);
1063 list_add_tail(&blkvsc_req->pend_entry,
1064 &blkdev->pending_list);
1066 ret = blkvsc_submit_request(blkvsc_req,
1067 blkvsc_request_completion);
1070 list_add_tail(&blkvsc_req->pend_entry,
1071 &blkdev->pending_list);
1074 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1075 "start_sect %lu sect_count %ld (%lu %ld) "
1076 "ret %d\n", blkvsc_req,
1077 (unsigned long)blkvsc_req->sector_start,
1078 blkvsc_req->sector_count,
1079 (unsigned long)start_sector,
1087 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1089 struct blkvsc_request *blkvsc_req =
1090 (struct blkvsc_request *)request->Context;
1091 struct block_device_context *blkdev =
1092 (struct block_device_context *)blkvsc_req->dev;
1093 struct scsi_sense_hdr sense_hdr;
1095 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1098 blkdev->num_outstanding_reqs--;
1100 if (blkvsc_req->request.Status)
1101 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1102 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1103 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1105 blkvsc_req->cond = 1;
1106 wake_up_interruptible(&blkvsc_req->wevent);
1109 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1111 struct blkvsc_request *blkvsc_req =
1112 (struct blkvsc_request *)request->Context;
1113 struct block_device_context *blkdev =
1114 (struct block_device_context *)blkvsc_req->dev;
1115 unsigned long flags;
1116 struct blkvsc_request *comp_req, *tmp;
1118 ASSERT(blkvsc_req->group);
1120 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1121 "sect_start %lu sect_count %ld len %d group outstd %d "
1122 "total outstd %d\n",
1123 blkdev, blkvsc_req, blkvsc_req->group,
1124 (blkvsc_req->write) ? "WRITE" : "READ",
1125 (unsigned long)blkvsc_req->sector_start,
1126 blkvsc_req->sector_count,
1127 blkvsc_req->request.DataBuffer.Length,
1128 blkvsc_req->group->outstanding,
1129 blkdev->num_outstanding_reqs);
1131 spin_lock_irqsave(&blkdev->lock, flags);
1133 blkdev->num_outstanding_reqs--;
1134 blkvsc_req->group->outstanding--;
1137 * Only start processing when all the blkvsc_reqs are
1138 * completed. This guarantees no out-of-order blkvsc_req
1139 * completion when calling end_that_request_first()
1141 if (blkvsc_req->group->outstanding == 0) {
1142 list_for_each_entry_safe(comp_req, tmp,
1143 &blkvsc_req->group->blkvsc_req_list,
1145 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1146 "sect_start %lu sect_count %ld \n",
1148 (unsigned long)comp_req->sector_start,
1149 comp_req->sector_count);
1151 list_del(&comp_req->req_entry);
1153 if (!__blk_end_request(comp_req->req,
1154 (!comp_req->request.Status ? 0 : -EIO),
1155 comp_req->sector_count * blkdev->sector_size)) {
1157 * All the sectors have been xferred ie the
1160 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1162 kmem_cache_free(blkdev->request_pool,
1166 kmem_cache_free(blkdev->request_pool, comp_req);
1169 if (!blkdev->shutting_down) {
1170 blkvsc_do_pending_reqs(blkdev);
1171 blk_start_queue(blkdev->gd->queue);
1172 blkvsc_request(blkdev->gd->queue);
1176 spin_unlock_irqrestore(&blkdev->lock, flags);
1179 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1181 struct blkvsc_request *pend_req, *tmp;
1182 struct blkvsc_request *comp_req, *tmp2;
1186 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1188 /* Flush the pending list first */
1189 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1192 * The pend_req could be part of a partially completed
1193 * request. If so, complete those req first until we
1196 list_for_each_entry_safe(comp_req, tmp2,
1197 &pend_req->group->blkvsc_req_list,
1199 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1200 "sect_start %lu sect_count %ld \n",
1202 (unsigned long) comp_req->sector_start,
1203 comp_req->sector_count);
1205 if (comp_req == pend_req)
1208 list_del(&comp_req->req_entry);
1210 if (comp_req->req) {
1211 ret = __blk_end_request(comp_req->req,
1212 (!comp_req->request.Status ? 0 : -EIO),
1213 comp_req->sector_count *
1214 blkdev->sector_size);
1218 kmem_cache_free(blkdev->request_pool, comp_req);
1221 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1224 list_del(&pend_req->pend_entry);
1226 list_del(&pend_req->req_entry);
1228 if (comp_req->req) {
1229 if (!__blk_end_request(pend_req->req, -EIO,
1230 pend_req->sector_count *
1231 blkdev->sector_size)) {
1233 * All the sectors have been xferred ie the
1236 DPRINT_DBG(BLKVSC_DRV,
1237 "blkvsc_cancel_pending_reqs() - "
1238 "req %p COMPLETED\n", pend_req->req);
1239 kmem_cache_free(blkdev->request_pool,
1244 kmem_cache_free(blkdev->request_pool, pend_req);
1250 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1252 struct blkvsc_request *pend_req, *tmp;
1255 /* Flush the pending list first */
1256 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1258 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1261 ret = blkvsc_submit_request(pend_req,
1262 blkvsc_request_completion);
1266 list_del(&pend_req->pend_entry);
1272 static void blkvsc_request(struct request_queue *queue)
1274 struct block_device_context *blkdev = NULL;
1275 struct request *req;
1278 DPRINT_DBG(BLKVSC_DRV, "- enter \n");
1279 while ((req = blk_peek_request(queue)) != NULL) {
1280 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1282 blkdev = req->rq_disk->private_data;
1283 if (blkdev->shutting_down || !blk_fs_request(req) ||
1284 blkdev->media_not_present) {
1285 __blk_end_request_cur(req, 0);
1289 ret = blkvsc_do_pending_reqs(blkdev);
1292 DPRINT_DBG(BLKVSC_DRV,
1293 "- stop queue - pending_list not empty\n");
1294 blk_stop_queue(queue);
1298 blk_start_request(req);
1300 ret = blkvsc_do_request(blkdev, req);
1302 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1303 blk_stop_queue(queue);
1305 } else if (ret < 0) {
1306 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1307 blk_requeue_request(queue, req);
1308 blk_stop_queue(queue);
1314 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1316 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1318 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1319 blkdev->gd->disk_name);
1321 spin_lock(&blkdev->lock);
1323 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1324 spin_unlock(&blkdev->lock);
1325 check_disk_change(bdev);
1326 spin_lock(&blkdev->lock);
1331 spin_unlock(&blkdev->lock);
1335 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1337 struct block_device_context *blkdev = disk->private_data;
1339 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1340 blkdev->gd->disk_name);
1342 spin_lock(&blkdev->lock);
1343 if (blkdev->users == 1) {
1344 spin_unlock(&blkdev->lock);
1345 blkvsc_do_flush(blkdev);
1346 spin_lock(&blkdev->lock);
1351 spin_unlock(&blkdev->lock);
1355 static int blkvsc_media_changed(struct gendisk *gd)
1357 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1361 static int blkvsc_revalidate_disk(struct gendisk *gd)
1363 struct block_device_context *blkdev = gd->private_data;
1365 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1367 if (blkdev->device_type == DVD_TYPE) {
1368 blkvsc_do_read_capacity(blkdev);
1369 set_capacity(blkdev->gd, blkdev->capacity *
1370 (blkdev->sector_size/512));
1371 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1376 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1378 sector_t total_sectors = get_capacity(bd->bd_disk);
1379 sector_t cylinder_times_heads = 0;
1382 int sectors_per_track = 0;
1387 if (total_sectors > (65535 * 16 * 255))
1388 total_sectors = (65535 * 16 * 255);
1390 if (total_sectors >= (65535 * 16 * 63)) {
1391 sectors_per_track = 255;
1394 cylinder_times_heads = total_sectors;
1395 /* sector_div stores the quotient in cylinder_times_heads */
1396 rem = sector_div(cylinder_times_heads, sectors_per_track);
1398 sectors_per_track = 17;
1400 cylinder_times_heads = total_sectors;
1401 /* sector_div stores the quotient in cylinder_times_heads */
1402 rem = sector_div(cylinder_times_heads, sectors_per_track);
1404 temp = cylinder_times_heads + 1023;
1405 /* sector_div stores the quotient in temp */
1406 rem = sector_div(temp, 1024);
1414 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1415 sectors_per_track = 31;
1418 cylinder_times_heads = total_sectors;
1420 * sector_div stores the quotient in
1421 * cylinder_times_heads
1423 rem = sector_div(cylinder_times_heads,
1427 if (cylinder_times_heads >= (heads * 1024)) {
1428 sectors_per_track = 63;
1431 cylinder_times_heads = total_sectors;
1433 * sector_div stores the quotient in
1434 * cylinder_times_heads
1436 rem = sector_div(cylinder_times_heads,
1441 temp = cylinder_times_heads;
1442 /* sector_div stores the quotient in temp */
1443 rem = sector_div(temp, heads);
1447 hg->sectors = sectors_per_track;
1448 hg->cylinders = cylinders;
1450 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1456 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1457 unsigned cmd, unsigned long argument)
1459 /* struct block_device_context *blkdev = bd->bd_disk->private_data; */
1464 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1465 * than just a GUID. Commented it out for now.
1468 case HDIO_GET_IDENTITY:
1469 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1470 if (copy_to_user((void __user *)arg, blkdev->device_id,
1471 blkdev->device_id_len))
1483 static int __init blkvsc_init(void)
1487 ASSERT(sizeof(sector_t) == 8); /* Make sure CONFIG_LBD is set */
1489 DPRINT_ENTER(BLKVSC_DRV);
1491 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1493 ret = blkvsc_drv_init(BlkVscInitialize);
1495 DPRINT_EXIT(BLKVSC_DRV);
1500 static void __exit blkvsc_exit(void)
1502 DPRINT_ENTER(BLKVSC_DRV);
1504 DPRINT_ENTER(BLKVSC_DRV);
1507 MODULE_LICENSE("GPL");
1508 MODULE_VERSION(HV_DRV_VERSION);
1509 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
1510 module_init(blkvsc_init);
1511 module_exit(blkvsc_exit);