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 <linux/mutex.h>
29 #include <linux/slab.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_dbg.h>
36 #include "version_info.h"
38 #include "storvsc_api.h"
41 #define BLKVSC_MINORS 64
43 enum blkvsc_device_type {
50 * This request ties the struct request and struct
51 * blkvsc_request/hv_storvsc_request together A struct request may be
52 * represented by 1 or more struct blkvsc_request
54 struct blkvsc_request_group {
57 struct list_head blkvsc_req_list; /* list of blkvsc_requests */
60 struct blkvsc_request {
61 /* blkvsc_request_group.blkvsc_req_list */
62 struct list_head req_entry;
64 /* block_device_context.pending_list */
65 struct list_head pend_entry;
67 /* This may be null if we generate a request internally */
70 struct block_device_context *dev;
72 /* The group this request is part of. Maybe null */
73 struct blkvsc_request_group *group;
75 wait_queue_head_t wevent;
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 struct hv_storvsc_request request;
88 * !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap,
89 * because - The extension buffer falls right here and is pointed to by
91 * Which sounds like a horrible idea, who designed this?
95 /* Per device structure */
96 struct block_device_context {
97 /* point back to our device context */
98 struct vm_device *device_ctx;
99 struct kmem_cache *request_pool;
102 enum blkvsc_device_type device_type;
103 struct list_head pending_list;
105 unsigned char device_id[64];
106 unsigned int device_id_len;
107 int num_outstanding_reqs;
109 int media_not_present;
110 unsigned int sector_size;
114 unsigned char target;
119 struct blkvsc_driver_context {
120 /* !! These must be the first 2 fields !! */
121 /* FIXME this is a bug! */
122 struct driver_context drv_ctx;
123 struct storvsc_driver_object drv_obj;
127 static DEFINE_MUTEX(blkvsc_mutex);
128 static int blkvsc_probe(struct device *dev);
129 static int blkvsc_remove(struct device *device);
130 static void blkvsc_shutdown(struct device *device);
132 static int blkvsc_open(struct block_device *bdev, fmode_t mode);
133 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
134 static int blkvsc_media_changed(struct gendisk *gd);
135 static int blkvsc_revalidate_disk(struct gendisk *gd);
136 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
137 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
138 unsigned cmd, unsigned long argument);
139 static void blkvsc_request(struct request_queue *queue);
140 static void blkvsc_request_completion(struct hv_storvsc_request *request);
141 static int blkvsc_do_request(struct block_device_context *blkdev,
142 struct request *req);
143 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
144 void (*request_completion)(struct hv_storvsc_request *));
145 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
146 static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
147 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
148 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
149 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
150 static int blkvsc_do_flush(struct block_device_context *blkdev);
151 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
152 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
154 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
155 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
156 MODULE_PARM_DESC(ring_size, "Ring buffer size (in bytes)");
158 /* The one and only one */
159 static struct blkvsc_driver_context g_blkvsc_drv;
161 static const struct block_device_operations block_ops = {
162 .owner = THIS_MODULE,
164 .release = blkvsc_release,
165 .media_changed = blkvsc_media_changed,
166 .revalidate_disk = blkvsc_revalidate_disk,
167 .getgeo = blkvsc_getgeo,
168 .ioctl = blkvsc_ioctl,
172 * blkvsc_drv_init - BlkVsc driver initialization.
174 static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
176 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
177 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
180 storvsc_drv_obj->ring_buffer_size = 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);
199 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
201 struct device **curr = (struct device **)data;
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;
217 ret = driver_for_each_device(&drv_ctx->driver, NULL,
218 (void *) ¤t_dev,
222 DPRINT_WARN(BLKVSC_DRV,
223 "driver_for_each_device returned %d", ret);
226 if (current_dev == NULL)
229 /* Initiate removal from the top-down */
230 device_unregister(current_dev);
233 if (storvsc_drv_obj->base.OnCleanup)
234 storvsc_drv_obj->base.OnCleanup(&storvsc_drv_obj->base);
236 vmbus_child_driver_unregister(drv_ctx);
242 * blkvsc_probe - Add a new device for this driver
244 static int blkvsc_probe(struct device *device)
246 struct driver_context *driver_ctx =
247 driver_to_driver_context(device->driver);
248 struct blkvsc_driver_context *blkvsc_drv_ctx =
249 (struct blkvsc_driver_context *)driver_ctx;
250 struct storvsc_driver_object *storvsc_drv_obj =
251 &blkvsc_drv_ctx->drv_obj;
252 struct vm_device *device_ctx = device_to_vm_device(device);
253 struct hv_device *device_obj = &device_ctx->device_obj;
255 struct block_device_context *blkdev = NULL;
256 struct storvsc_device_info device_info;
260 static int ide0_registered;
261 static int ide1_registered;
263 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
265 if (!storvsc_drv_obj->base.OnDeviceAdd) {
266 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
271 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
277 INIT_LIST_HEAD(&blkdev->pending_list);
279 /* Initialize what we can here */
280 spin_lock_init(&blkdev->lock);
282 /* ASSERT(sizeof(struct blkvsc_request_group) <= */
283 /* sizeof(struct blkvsc_request)); */
285 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
286 sizeof(struct blkvsc_request) +
287 storvsc_drv_obj->request_ext_size, 0,
288 SLAB_HWCACHE_ALIGN, NULL);
289 if (!blkdev->request_pool) {
295 /* Call to the vsc driver to add the device */
296 ret = storvsc_drv_obj->base.OnDeviceAdd(device_obj, &device_info);
298 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
302 blkdev->device_ctx = device_ctx;
303 /* this identified the device 0 or 1 */
304 blkdev->target = device_info.target_id;
305 /* this identified the ide ctrl 0 or 1 */
306 blkdev->path = device_info.path_id;
308 dev_set_drvdata(device, blkdev);
310 /* Calculate the major and device num */
311 if (blkdev->path == 0) {
313 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
315 if (!ide0_registered) {
316 ret = register_blkdev(major, "ide");
318 DPRINT_ERR(BLKVSC_DRV,
319 "register_blkdev() failed! ret %d",
326 } else if (blkdev->path == 1) {
328 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
330 if (!ide1_registered) {
331 ret = register_blkdev(major, "ide");
333 DPRINT_ERR(BLKVSC_DRV,
334 "register_blkdev() failed! ret %d",
342 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
347 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
349 blkdev->gd = alloc_disk(BLKVSC_MINORS);
351 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
356 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
358 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
359 blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
360 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
361 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
362 blk_queue_dma_alignment(blkdev->gd->queue, 511);
364 blkdev->gd->major = major;
365 if (devnum == 1 || devnum == 3)
366 blkdev->gd->first_minor = BLKVSC_MINORS;
368 blkdev->gd->first_minor = 0;
369 blkdev->gd->fops = &block_ops;
370 blkdev->gd->private_data = blkdev;
371 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
373 blkvsc_do_inquiry(blkdev);
374 if (blkdev->device_type == DVD_TYPE) {
375 set_disk_ro(blkdev->gd, 1);
376 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
377 blkvsc_do_read_capacity(blkdev);
379 blkvsc_do_read_capacity16(blkdev);
382 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
383 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
385 add_disk(blkdev->gd);
387 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
388 blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
389 blkdev->sector_size);
394 storvsc_drv_obj->base.OnDeviceRemove(device_obj);
398 if (blkdev->request_pool) {
399 kmem_cache_destroy(blkdev->request_pool);
400 blkdev->request_pool = NULL;
409 static void blkvsc_shutdown(struct device *device)
411 struct block_device_context *blkdev = dev_get_drvdata(device);
417 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
418 blkdev->users, blkdev->gd->disk_name);
420 spin_lock_irqsave(&blkdev->lock, flags);
422 blkdev->shutting_down = 1;
424 blk_stop_queue(blkdev->gd->queue);
426 spin_unlock_irqrestore(&blkdev->lock, flags);
428 while (blkdev->num_outstanding_reqs) {
429 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
430 blkdev->num_outstanding_reqs);
434 blkvsc_do_flush(blkdev);
436 spin_lock_irqsave(&blkdev->lock, flags);
438 blkvsc_cancel_pending_reqs(blkdev);
440 spin_unlock_irqrestore(&blkdev->lock, flags);
443 static int blkvsc_do_flush(struct block_device_context *blkdev)
445 struct blkvsc_request *blkvsc_req;
447 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
449 if (blkdev->device_type != HARDDISK_TYPE)
452 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
456 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
457 init_waitqueue_head(&blkvsc_req->wevent);
458 blkvsc_req->dev = blkdev;
459 blkvsc_req->req = NULL;
460 blkvsc_req->write = 0;
462 blkvsc_req->request.data_buffer.PfnArray[0] = 0;
463 blkvsc_req->request.data_buffer.Offset = 0;
464 blkvsc_req->request.data_buffer.Length = 0;
466 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
467 blkvsc_req->cmd_len = 10;
470 * Set this here since the completion routine may be invoked and
471 * completed before we return
473 blkvsc_req->cond = 0;
474 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
476 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
478 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
483 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
484 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
486 struct blkvsc_request *blkvsc_req;
487 struct page *page_buf;
489 unsigned char device_type;
491 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
493 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
497 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
498 page_buf = alloc_page(GFP_KERNEL);
500 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
504 init_waitqueue_head(&blkvsc_req->wevent);
505 blkvsc_req->dev = blkdev;
506 blkvsc_req->req = NULL;
507 blkvsc_req->write = 0;
509 blkvsc_req->request.data_buffer.PfnArray[0] = page_to_pfn(page_buf);
510 blkvsc_req->request.data_buffer.Offset = 0;
511 blkvsc_req->request.data_buffer.Length = 64;
513 blkvsc_req->cmnd[0] = INQUIRY;
514 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
515 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
516 blkvsc_req->cmnd[4] = 64;
517 blkvsc_req->cmd_len = 6;
520 * Set this here since the completion routine may be invoked and
521 * completed before we return
523 blkvsc_req->cond = 0;
525 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
527 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
528 blkvsc_req, blkvsc_req->cond);
530 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
532 buf = kmap(page_buf);
534 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
536 device_type = buf[0] & 0x1F;
538 if (device_type == 0x0) {
539 blkdev->device_type = HARDDISK_TYPE;
540 } else if (device_type == 0x5) {
541 blkdev->device_type = DVD_TYPE;
543 /* TODO: this is currently unsupported device type */
544 blkdev->device_type = UNKNOWN_DEV_TYPE;
547 DPRINT_DBG(BLKVSC_DRV, "device type %d\n", device_type);
549 blkdev->device_id_len = buf[7];
550 if (blkdev->device_id_len > 64)
551 blkdev->device_id_len = 64;
553 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
554 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
555 * blkdev->device_id_len); */
559 __free_page(page_buf);
561 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
566 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
567 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
569 struct blkvsc_request *blkvsc_req;
570 struct page *page_buf;
572 struct scsi_sense_hdr sense_hdr;
574 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
576 blkdev->sector_size = 0;
577 blkdev->capacity = 0;
578 blkdev->media_not_present = 0; /* assume a disk is present */
580 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
584 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
585 page_buf = alloc_page(GFP_KERNEL);
587 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
591 init_waitqueue_head(&blkvsc_req->wevent);
592 blkvsc_req->dev = blkdev;
593 blkvsc_req->req = NULL;
594 blkvsc_req->write = 0;
596 blkvsc_req->request.data_buffer.PfnArray[0] = page_to_pfn(page_buf);
597 blkvsc_req->request.data_buffer.Offset = 0;
598 blkvsc_req->request.data_buffer.Length = 8;
600 blkvsc_req->cmnd[0] = READ_CAPACITY;
601 blkvsc_req->cmd_len = 16;
604 * Set this here since the completion routine may be invoked
605 * and completed before we return
607 blkvsc_req->cond = 0;
609 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
611 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
612 blkvsc_req, blkvsc_req->cond);
614 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
617 if (blkvsc_req->request.status) {
618 scsi_normalize_sense(blkvsc_req->sense_buffer,
619 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
621 if (sense_hdr.asc == 0x3A) {
622 /* Medium not present */
623 blkdev->media_not_present = 1;
627 buf = kmap(page_buf);
630 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
631 (buf[2] << 8) | buf[3]) + 1;
632 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
633 (buf[6] << 8) | buf[7];
637 __free_page(page_buf);
639 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
644 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
646 struct blkvsc_request *blkvsc_req;
647 struct page *page_buf;
649 struct scsi_sense_hdr sense_hdr;
651 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
653 blkdev->sector_size = 0;
654 blkdev->capacity = 0;
655 blkdev->media_not_present = 0; /* assume a disk is present */
657 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
661 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
662 page_buf = alloc_page(GFP_KERNEL);
664 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
668 init_waitqueue_head(&blkvsc_req->wevent);
669 blkvsc_req->dev = blkdev;
670 blkvsc_req->req = NULL;
671 blkvsc_req->write = 0;
673 blkvsc_req->request.data_buffer.PfnArray[0] = page_to_pfn(page_buf);
674 blkvsc_req->request.data_buffer.Offset = 0;
675 blkvsc_req->request.data_buffer.Length = 12;
677 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
678 blkvsc_req->cmd_len = 16;
681 * Set this here since the completion routine may be invoked
682 * and completed before we return
684 blkvsc_req->cond = 0;
686 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
688 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
689 blkvsc_req, blkvsc_req->cond);
691 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
694 if (blkvsc_req->request.status) {
695 scsi_normalize_sense(blkvsc_req->sense_buffer,
696 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
697 if (sense_hdr.asc == 0x3A) {
698 /* Medium not present */
699 blkdev->media_not_present = 1;
703 buf = kmap(page_buf);
706 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
707 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
710 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
711 (buf[2] << 8) | buf[3]) + 1;
712 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
713 (buf[6] << 8) | buf[7];
718 __free_page(page_buf);
720 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
726 * blkvsc_remove() - Callback when our device is removed
728 static int blkvsc_remove(struct device *device)
730 struct driver_context *driver_ctx =
731 driver_to_driver_context(device->driver);
732 struct blkvsc_driver_context *blkvsc_drv_ctx =
733 (struct blkvsc_driver_context *)driver_ctx;
734 struct storvsc_driver_object *storvsc_drv_obj =
735 &blkvsc_drv_ctx->drv_obj;
736 struct vm_device *device_ctx = device_to_vm_device(device);
737 struct hv_device *device_obj = &device_ctx->device_obj;
738 struct block_device_context *blkdev = dev_get_drvdata(device);
742 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
744 if (!storvsc_drv_obj->base.OnDeviceRemove)
748 * Call to the vsc driver to let it know that the device is being
751 ret = storvsc_drv_obj->base.OnDeviceRemove(device_obj);
754 DPRINT_ERR(BLKVSC_DRV,
755 "unable to remove blkvsc device (ret %d)", ret);
758 /* Get to a known state */
759 spin_lock_irqsave(&blkdev->lock, flags);
761 blkdev->shutting_down = 1;
763 blk_stop_queue(blkdev->gd->queue);
765 spin_unlock_irqrestore(&blkdev->lock, flags);
767 while (blkdev->num_outstanding_reqs) {
768 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
769 blkdev->num_outstanding_reqs);
773 blkvsc_do_flush(blkdev);
775 spin_lock_irqsave(&blkdev->lock, flags);
777 blkvsc_cancel_pending_reqs(blkdev);
779 spin_unlock_irqrestore(&blkdev->lock, flags);
781 blk_cleanup_queue(blkdev->gd->queue);
783 del_gendisk(blkdev->gd);
785 kmem_cache_destroy(blkdev->request_pool);
792 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
794 /* ASSERT(blkvsc_req->req); */
795 /* ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8)); */
797 blkvsc_req->cmd_len = 16;
799 if (blkvsc_req->sector_start > 0xffffffff) {
800 if (rq_data_dir(blkvsc_req->req)) {
801 blkvsc_req->write = 1;
802 blkvsc_req->cmnd[0] = WRITE_16;
804 blkvsc_req->write = 0;
805 blkvsc_req->cmnd[0] = READ_16;
808 blkvsc_req->cmnd[1] |=
809 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
811 *(unsigned long long *)&blkvsc_req->cmnd[2] =
812 cpu_to_be64(blkvsc_req->sector_start);
813 *(unsigned int *)&blkvsc_req->cmnd[10] =
814 cpu_to_be32(blkvsc_req->sector_count);
815 } else if ((blkvsc_req->sector_count > 0xff) ||
816 (blkvsc_req->sector_start > 0x1fffff)) {
817 if (rq_data_dir(blkvsc_req->req)) {
818 blkvsc_req->write = 1;
819 blkvsc_req->cmnd[0] = WRITE_10;
821 blkvsc_req->write = 0;
822 blkvsc_req->cmnd[0] = READ_10;
825 blkvsc_req->cmnd[1] |=
826 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
828 *(unsigned int *)&blkvsc_req->cmnd[2] =
829 cpu_to_be32(blkvsc_req->sector_start);
830 *(unsigned short *)&blkvsc_req->cmnd[7] =
831 cpu_to_be16(blkvsc_req->sector_count);
833 if (rq_data_dir(blkvsc_req->req)) {
834 blkvsc_req->write = 1;
835 blkvsc_req->cmnd[0] = WRITE_6;
837 blkvsc_req->write = 0;
838 blkvsc_req->cmnd[0] = READ_6;
841 *(unsigned int *)&blkvsc_req->cmnd[1] =
842 cpu_to_be32(blkvsc_req->sector_start) >> 8;
843 blkvsc_req->cmnd[1] &= 0x1f;
844 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
848 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
849 void (*request_completion)(struct hv_storvsc_request *))
851 struct block_device_context *blkdev = blkvsc_req->dev;
852 struct vm_device *device_ctx = blkdev->device_ctx;
853 struct driver_context *driver_ctx =
854 driver_to_driver_context(device_ctx->device.driver);
855 struct blkvsc_driver_context *blkvsc_drv_ctx =
856 (struct blkvsc_driver_context *)driver_ctx;
857 struct storvsc_driver_object *storvsc_drv_obj =
858 &blkvsc_drv_ctx->drv_obj;
859 struct hv_storvsc_request *storvsc_req;
862 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
863 "req %p type %s start_sector %lu count %ld offset %d "
864 "len %d\n", blkvsc_req,
865 (blkvsc_req->write) ? "WRITE" : "READ",
866 (unsigned long) blkvsc_req->sector_start,
867 blkvsc_req->sector_count,
868 blkvsc_req->request.data_buffer.Offset,
869 blkvsc_req->request.data_buffer.Length);
871 for (i = 0; i < (blkvsc_req->request.data_buffer.Length >> 12); i++) {
872 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
873 "req %p pfn[%d] %llx\n",
875 blkvsc_req->request.data_buffer.PfnArray[i]);
879 storvsc_req = &blkvsc_req->request;
880 storvsc_req->extension = (void *)((unsigned long)blkvsc_req +
881 sizeof(struct blkvsc_request));
883 storvsc_req->type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
885 storvsc_req->on_io_completion = request_completion;
886 storvsc_req->context = blkvsc_req;
888 storvsc_req->host = blkdev->port;
889 storvsc_req->bus = blkdev->path;
890 storvsc_req->target_id = blkdev->target;
891 storvsc_req->lun_id = 0; /* this is not really used at all */
893 storvsc_req->cdb_len = blkvsc_req->cmd_len;
894 storvsc_req->cdb = blkvsc_req->cmnd;
896 storvsc_req->sense_buffer = blkvsc_req->sense_buffer;
897 storvsc_req->sense_buffer_size = SCSI_SENSE_BUFFERSIZE;
899 ret = storvsc_drv_obj->on_io_request(&blkdev->device_ctx->device_obj,
900 &blkvsc_req->request);
902 blkdev->num_outstanding_reqs++;
908 * We break the request into 1 or more blkvsc_requests and submit
909 * them. If we cant submit them all, we put them on the
910 * pending_list. The blkvsc_request() will work on the pending_list.
912 static int blkvsc_do_request(struct block_device_context *blkdev,
915 struct bio *bio = NULL;
916 struct bio_vec *bvec = NULL;
917 struct bio_vec *prev_bvec = NULL;
918 struct blkvsc_request *blkvsc_req = NULL;
919 struct blkvsc_request *tmp;
922 sector_t start_sector;
923 unsigned long num_sectors = 0;
926 struct blkvsc_request_group *group = NULL;
928 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu\n", blkdev, req,
929 (unsigned long)blk_rq_pos(req));
931 /* Create a group to tie req to list of blkvsc_reqs */
932 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
936 INIT_LIST_HEAD(&group->blkvsc_req_list);
937 group->outstanding = group->status = 0;
939 start_sector = blk_rq_pos(req);
941 /* foreach bio in the request */
943 for (bio = req->bio; bio; bio = bio->bi_next) {
945 * Map this bio into an existing or new storvsc request
947 bio_for_each_segment(bvec, bio, seg_idx) {
948 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
949 "- req %p bio %p bvec %p seg_idx %d "
950 "databuf_idx %d\n", req, bio, bvec,
951 seg_idx, databuf_idx);
953 /* Get a new storvsc request */
956 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
957 /* hole at the begin of page */
958 || (bvec->bv_offset != 0) ||
959 /* hold at the end of page */
961 (prev_bvec->bv_len != PAGE_SIZE))) {
962 /* submit the prev one */
964 blkvsc_req->sector_start = start_sector;
965 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
967 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
968 blkvsc_init_rw(blkvsc_req);
972 * Create new blkvsc_req to represent
975 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
977 /* free up everything */
978 list_for_each_entry_safe(
980 &group->blkvsc_req_list,
982 list_del(&blkvsc_req->req_entry);
983 kmem_cache_free(blkdev->request_pool, blkvsc_req);
986 kmem_cache_free(blkdev->request_pool, group);
990 memset(blkvsc_req, 0,
991 sizeof(struct blkvsc_request));
993 blkvsc_req->dev = blkdev;
994 blkvsc_req->req = req;
995 blkvsc_req->request.data_buffer.Offset
997 blkvsc_req->request.data_buffer.Length
1000 /* Add to the group */
1001 blkvsc_req->group = group;
1002 blkvsc_req->group->outstanding++;
1003 list_add_tail(&blkvsc_req->req_entry,
1004 &blkvsc_req->group->blkvsc_req_list);
1006 start_sector += num_sectors;
1011 /* Add the curr bvec/segment to the curr blkvsc_req */
1012 blkvsc_req->request.data_buffer.
1013 PfnArray[databuf_idx]
1014 = page_to_pfn(bvec->bv_page);
1015 blkvsc_req->request.data_buffer.Length
1021 num_sectors += bvec->bv_len >> 9;
1023 } /* bio_for_each_segment */
1025 } /* rq_for_each_bio */
1028 /* Handle the last one */
1030 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1031 blkdev, req, blkvsc_req->group,
1032 blkvsc_req->group->outstanding);
1034 blkvsc_req->sector_start = start_sector;
1035 sector_div(blkvsc_req->sector_start,
1036 (blkdev->sector_size >> 9));
1038 blkvsc_req->sector_count = num_sectors /
1039 (blkdev->sector_size >> 9);
1041 blkvsc_init_rw(blkvsc_req);
1044 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1046 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1047 "pending_list - blkvsc_req %p start_sect %lu"
1048 " sect_count %ld (%lu %ld)\n", blkvsc_req,
1049 (unsigned long)blkvsc_req->sector_start,
1050 blkvsc_req->sector_count,
1051 (unsigned long)start_sector,
1052 (unsigned long)num_sectors);
1054 list_add_tail(&blkvsc_req->pend_entry,
1055 &blkdev->pending_list);
1057 ret = blkvsc_submit_request(blkvsc_req,
1058 blkvsc_request_completion);
1061 list_add_tail(&blkvsc_req->pend_entry,
1062 &blkdev->pending_list);
1065 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1066 "start_sect %lu sect_count %ld (%lu %ld) "
1067 "ret %d\n", blkvsc_req,
1068 (unsigned long)blkvsc_req->sector_start,
1069 blkvsc_req->sector_count,
1070 (unsigned long)start_sector,
1078 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1080 struct blkvsc_request *blkvsc_req =
1081 (struct blkvsc_request *)request->context;
1082 struct block_device_context *blkdev =
1083 (struct block_device_context *)blkvsc_req->dev;
1084 struct scsi_sense_hdr sense_hdr;
1086 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1089 blkdev->num_outstanding_reqs--;
1091 if (blkvsc_req->request.status)
1092 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1093 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1094 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1096 blkvsc_req->cond = 1;
1097 wake_up_interruptible(&blkvsc_req->wevent);
1100 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1102 struct blkvsc_request *blkvsc_req =
1103 (struct blkvsc_request *)request->context;
1104 struct block_device_context *blkdev =
1105 (struct block_device_context *)blkvsc_req->dev;
1106 unsigned long flags;
1107 struct blkvsc_request *comp_req, *tmp;
1109 /* ASSERT(blkvsc_req->group); */
1111 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1112 "sect_start %lu sect_count %ld len %d group outstd %d "
1113 "total outstd %d\n",
1114 blkdev, blkvsc_req, blkvsc_req->group,
1115 (blkvsc_req->write) ? "WRITE" : "READ",
1116 (unsigned long)blkvsc_req->sector_start,
1117 blkvsc_req->sector_count,
1118 blkvsc_req->request.data_buffer.Length,
1119 blkvsc_req->group->outstanding,
1120 blkdev->num_outstanding_reqs);
1122 spin_lock_irqsave(&blkdev->lock, flags);
1124 blkdev->num_outstanding_reqs--;
1125 blkvsc_req->group->outstanding--;
1128 * Only start processing when all the blkvsc_reqs are
1129 * completed. This guarantees no out-of-order blkvsc_req
1130 * completion when calling end_that_request_first()
1132 if (blkvsc_req->group->outstanding == 0) {
1133 list_for_each_entry_safe(comp_req, tmp,
1134 &blkvsc_req->group->blkvsc_req_list,
1136 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1137 "sect_start %lu sect_count %ld\n",
1139 (unsigned long)comp_req->sector_start,
1140 comp_req->sector_count);
1142 list_del(&comp_req->req_entry);
1144 if (!__blk_end_request(comp_req->req,
1145 (!comp_req->request.status ? 0 : -EIO),
1146 comp_req->sector_count * blkdev->sector_size)) {
1148 * All the sectors have been xferred ie the
1151 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1153 kmem_cache_free(blkdev->request_pool,
1157 kmem_cache_free(blkdev->request_pool, comp_req);
1160 if (!blkdev->shutting_down) {
1161 blkvsc_do_pending_reqs(blkdev);
1162 blk_start_queue(blkdev->gd->queue);
1163 blkvsc_request(blkdev->gd->queue);
1167 spin_unlock_irqrestore(&blkdev->lock, flags);
1170 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1172 struct blkvsc_request *pend_req, *tmp;
1173 struct blkvsc_request *comp_req, *tmp2;
1177 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1179 /* Flush the pending list first */
1180 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1183 * The pend_req could be part of a partially completed
1184 * request. If so, complete those req first until we
1187 list_for_each_entry_safe(comp_req, tmp2,
1188 &pend_req->group->blkvsc_req_list,
1190 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1191 "sect_start %lu sect_count %ld\n",
1193 (unsigned long) comp_req->sector_start,
1194 comp_req->sector_count);
1196 if (comp_req == pend_req)
1199 list_del(&comp_req->req_entry);
1201 if (comp_req->req) {
1202 ret = __blk_end_request(comp_req->req,
1203 (!comp_req->request.status ? 0 : -EIO),
1204 comp_req->sector_count *
1205 blkdev->sector_size);
1207 /* FIXME: shouldn't this do more than return? */
1212 kmem_cache_free(blkdev->request_pool, comp_req);
1215 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1218 list_del(&pend_req->pend_entry);
1220 list_del(&pend_req->req_entry);
1222 if (comp_req->req) {
1223 if (!__blk_end_request(pend_req->req, -EIO,
1224 pend_req->sector_count *
1225 blkdev->sector_size)) {
1227 * All the sectors have been xferred ie the
1230 DPRINT_DBG(BLKVSC_DRV,
1231 "blkvsc_cancel_pending_reqs() - "
1232 "req %p COMPLETED\n", pend_req->req);
1233 kmem_cache_free(blkdev->request_pool,
1238 kmem_cache_free(blkdev->request_pool, pend_req);
1245 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1247 struct blkvsc_request *pend_req, *tmp;
1250 /* Flush the pending list first */
1251 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1253 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1256 ret = blkvsc_submit_request(pend_req,
1257 blkvsc_request_completion);
1261 list_del(&pend_req->pend_entry);
1267 static void blkvsc_request(struct request_queue *queue)
1269 struct block_device_context *blkdev = NULL;
1270 struct request *req;
1273 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1274 while ((req = blk_peek_request(queue)) != NULL) {
1275 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1277 blkdev = req->rq_disk->private_data;
1278 if (blkdev->shutting_down || req->cmd_type != REQ_TYPE_FS ||
1279 blkdev->media_not_present) {
1280 __blk_end_request_cur(req, 0);
1284 ret = blkvsc_do_pending_reqs(blkdev);
1287 DPRINT_DBG(BLKVSC_DRV,
1288 "- stop queue - pending_list not empty\n");
1289 blk_stop_queue(queue);
1293 blk_start_request(req);
1295 ret = blkvsc_do_request(blkdev, req);
1297 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1298 blk_stop_queue(queue);
1300 } else if (ret < 0) {
1301 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1302 blk_requeue_request(queue, req);
1303 blk_stop_queue(queue);
1309 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1311 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1313 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1314 blkdev->gd->disk_name);
1316 mutex_lock(&blkvsc_mutex);
1317 spin_lock(&blkdev->lock);
1319 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1320 spin_unlock(&blkdev->lock);
1321 check_disk_change(bdev);
1322 spin_lock(&blkdev->lock);
1327 spin_unlock(&blkdev->lock);
1328 mutex_unlock(&blkvsc_mutex);
1332 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1334 struct block_device_context *blkdev = disk->private_data;
1336 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1337 blkdev->gd->disk_name);
1339 mutex_lock(&blkvsc_mutex);
1340 spin_lock(&blkdev->lock);
1341 if (blkdev->users == 1) {
1342 spin_unlock(&blkdev->lock);
1343 blkvsc_do_flush(blkdev);
1344 spin_lock(&blkdev->lock);
1349 spin_unlock(&blkdev->lock);
1350 mutex_unlock(&blkvsc_mutex);
1354 static int blkvsc_media_changed(struct gendisk *gd)
1356 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1360 static int blkvsc_revalidate_disk(struct gendisk *gd)
1362 struct block_device_context *blkdev = gd->private_data;
1364 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1366 if (blkdev->device_type == DVD_TYPE) {
1367 blkvsc_do_read_capacity(blkdev);
1368 set_capacity(blkdev->gd, blkdev->capacity *
1369 (blkdev->sector_size/512));
1370 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1375 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1377 sector_t total_sectors = get_capacity(bd->bd_disk);
1378 sector_t cylinder_times_heads = 0;
1381 int sectors_per_track = 0;
1386 if (total_sectors > (65535 * 16 * 255))
1387 total_sectors = (65535 * 16 * 255);
1389 if (total_sectors >= (65535 * 16 * 63)) {
1390 sectors_per_track = 255;
1393 cylinder_times_heads = total_sectors;
1394 /* sector_div stores the quotient in cylinder_times_heads */
1395 rem = sector_div(cylinder_times_heads, sectors_per_track);
1397 sectors_per_track = 17;
1399 cylinder_times_heads = total_sectors;
1400 /* sector_div stores the quotient in cylinder_times_heads */
1401 rem = sector_div(cylinder_times_heads, sectors_per_track);
1403 temp = cylinder_times_heads + 1023;
1404 /* sector_div stores the quotient in temp */
1405 rem = sector_div(temp, 1024);
1413 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1414 sectors_per_track = 31;
1417 cylinder_times_heads = total_sectors;
1419 * sector_div stores the quotient in
1420 * cylinder_times_heads
1422 rem = sector_div(cylinder_times_heads,
1426 if (cylinder_times_heads >= (heads * 1024)) {
1427 sectors_per_track = 63;
1430 cylinder_times_heads = total_sectors;
1432 * sector_div stores the quotient in
1433 * cylinder_times_heads
1435 rem = sector_div(cylinder_times_heads,
1440 temp = cylinder_times_heads;
1441 /* sector_div stores the quotient in temp */
1442 rem = sector_div(temp, heads);
1446 hg->sectors = sectors_per_track;
1447 hg->cylinders = cylinders;
1449 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1455 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1456 unsigned cmd, unsigned long argument)
1458 /* struct block_device_context *blkdev = bd->bd_disk->private_data; */
1463 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1464 * than just a GUID. Commented it out for now.
1467 case HDIO_GET_IDENTITY:
1468 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1469 if (copy_to_user((void __user *)arg, blkdev->device_id,
1470 blkdev->device_id_len))
1482 static int __init blkvsc_init(void)
1486 BUILD_BUG_ON(sizeof(sector_t) != 8);
1488 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1490 ret = blkvsc_drv_init(blk_vsc_initialize);
1495 static void __exit blkvsc_exit(void)
1500 MODULE_LICENSE("GPL");
1501 MODULE_VERSION(HV_DRV_VERSION);
1502 MODULE_DESCRIPTION("Microsoft Hyper-V virtual block driver");
1503 module_init(blkvsc_init);
1504 module_exit(blkvsc_exit);