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/smp_lock.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 int blkvsc_probe(struct device *dev);
128 static int blkvsc_remove(struct device *device);
129 static void blkvsc_shutdown(struct device *device);
131 static int blkvsc_open(struct block_device *bdev, fmode_t mode);
132 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
133 static int blkvsc_media_changed(struct gendisk *gd);
134 static int blkvsc_revalidate_disk(struct gendisk *gd);
135 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
136 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
137 unsigned cmd, unsigned long argument);
138 static void blkvsc_request(struct request_queue *queue);
139 static void blkvsc_request_completion(struct hv_storvsc_request *request);
140 static int blkvsc_do_request(struct block_device_context *blkdev,
141 struct request *req);
142 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
143 void (*request_completion)(struct hv_storvsc_request *));
144 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
145 static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
146 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
147 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
148 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
149 static int blkvsc_do_flush(struct block_device_context *blkdev);
150 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
151 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
153 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
154 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
155 MODULE_PARM_DESC(ring_size, "Ring buffer size (in bytes)");
157 /* The one and only one */
158 static struct blkvsc_driver_context g_blkvsc_drv;
160 static const struct block_device_operations block_ops = {
161 .owner = THIS_MODULE,
163 .release = blkvsc_release,
164 .media_changed = blkvsc_media_changed,
165 .revalidate_disk = blkvsc_revalidate_disk,
166 .getgeo = blkvsc_getgeo,
167 .ioctl = blkvsc_ioctl,
171 * blkvsc_drv_init - BlkVsc driver initialization.
173 static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
175 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
176 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
179 DPRINT_ENTER(BLKVSC_DRV);
181 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
183 storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
185 /* Callback to client driver to complete the initialization */
186 drv_init(&storvsc_drv_obj->Base);
188 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
189 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
190 sizeof(struct hv_guid));
192 drv_ctx->probe = blkvsc_probe;
193 drv_ctx->remove = blkvsc_remove;
194 drv_ctx->shutdown = blkvsc_shutdown;
196 /* The driver belongs to vmbus */
197 ret = vmbus_child_driver_register(drv_ctx);
199 DPRINT_EXIT(BLKVSC_DRV);
204 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
206 struct device **curr = (struct device **)data;
208 return 1; /* stop iterating */
211 static void blkvsc_drv_exit(void)
213 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
214 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
215 struct device *current_dev;
218 DPRINT_ENTER(BLKVSC_DRV);
224 ret = driver_for_each_device(&drv_ctx->driver, NULL,
225 (void *) ¤t_dev,
229 DPRINT_WARN(BLKVSC_DRV,
230 "driver_for_each_device returned %d", ret);
233 if (current_dev == NULL)
236 /* Initiate removal from the top-down */
237 device_unregister(current_dev);
240 if (storvsc_drv_obj->Base.OnCleanup)
241 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
243 vmbus_child_driver_unregister(drv_ctx);
245 DPRINT_EXIT(BLKVSC_DRV);
251 * blkvsc_probe - Add a new device for this driver
253 static int blkvsc_probe(struct device *device)
255 struct driver_context *driver_ctx =
256 driver_to_driver_context(device->driver);
257 struct blkvsc_driver_context *blkvsc_drv_ctx =
258 (struct blkvsc_driver_context *)driver_ctx;
259 struct storvsc_driver_object *storvsc_drv_obj =
260 &blkvsc_drv_ctx->drv_obj;
261 struct vm_device *device_ctx = device_to_vm_device(device);
262 struct hv_device *device_obj = &device_ctx->device_obj;
264 struct block_device_context *blkdev = NULL;
265 struct storvsc_device_info device_info;
269 static int ide0_registered;
270 static int ide1_registered;
272 DPRINT_ENTER(BLKVSC_DRV);
274 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
276 if (!storvsc_drv_obj->Base.OnDeviceAdd) {
277 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
282 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
288 INIT_LIST_HEAD(&blkdev->pending_list);
290 /* Initialize what we can here */
291 spin_lock_init(&blkdev->lock);
293 /* ASSERT(sizeof(struct blkvsc_request_group) <= */
294 /* sizeof(struct blkvsc_request)); */
296 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
297 sizeof(struct blkvsc_request) +
298 storvsc_drv_obj->RequestExtSize, 0,
299 SLAB_HWCACHE_ALIGN, NULL);
300 if (!blkdev->request_pool) {
306 /* Call to the vsc driver to add the device */
307 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
309 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
313 blkdev->device_ctx = device_ctx;
314 /* this identified the device 0 or 1 */
315 blkdev->target = device_info.TargetId;
316 /* this identified the ide ctrl 0 or 1 */
317 blkdev->path = device_info.PathId;
319 dev_set_drvdata(device, blkdev);
321 /* Calculate the major and device num */
322 if (blkdev->path == 0) {
324 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
326 if (!ide0_registered) {
327 ret = register_blkdev(major, "ide");
329 DPRINT_ERR(BLKVSC_DRV,
330 "register_blkdev() failed! ret %d",
337 } else if (blkdev->path == 1) {
339 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
341 if (!ide1_registered) {
342 ret = register_blkdev(major, "ide");
344 DPRINT_ERR(BLKVSC_DRV,
345 "register_blkdev() failed! ret %d",
353 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
358 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
360 blkdev->gd = alloc_disk(BLKVSC_MINORS);
362 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
367 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
369 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
370 blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
371 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
372 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
373 blk_queue_dma_alignment(blkdev->gd->queue, 511);
375 blkdev->gd->major = major;
376 if (devnum == 1 || devnum == 3)
377 blkdev->gd->first_minor = BLKVSC_MINORS;
379 blkdev->gd->first_minor = 0;
380 blkdev->gd->fops = &block_ops;
381 blkdev->gd->private_data = blkdev;
382 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
384 blkvsc_do_inquiry(blkdev);
385 if (blkdev->device_type == DVD_TYPE) {
386 set_disk_ro(blkdev->gd, 1);
387 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
388 blkvsc_do_read_capacity(blkdev);
390 blkvsc_do_read_capacity16(blkdev);
393 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
394 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
396 add_disk(blkdev->gd);
398 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
399 blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
400 blkdev->sector_size);
405 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
409 if (blkdev->request_pool) {
410 kmem_cache_destroy(blkdev->request_pool);
411 blkdev->request_pool = NULL;
417 DPRINT_EXIT(BLKVSC_DRV);
422 static void blkvsc_shutdown(struct device *device)
424 struct block_device_context *blkdev = dev_get_drvdata(device);
430 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
431 blkdev->users, blkdev->gd->disk_name);
433 spin_lock_irqsave(&blkdev->lock, flags);
435 blkdev->shutting_down = 1;
437 blk_stop_queue(blkdev->gd->queue);
439 spin_unlock_irqrestore(&blkdev->lock, flags);
441 while (blkdev->num_outstanding_reqs) {
442 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
443 blkdev->num_outstanding_reqs);
447 blkvsc_do_flush(blkdev);
449 spin_lock_irqsave(&blkdev->lock, flags);
451 blkvsc_cancel_pending_reqs(blkdev);
453 spin_unlock_irqrestore(&blkdev->lock, flags);
456 static int blkvsc_do_flush(struct block_device_context *blkdev)
458 struct blkvsc_request *blkvsc_req;
460 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
462 if (blkdev->device_type != HARDDISK_TYPE)
465 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
469 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
470 init_waitqueue_head(&blkvsc_req->wevent);
471 blkvsc_req->dev = blkdev;
472 blkvsc_req->req = NULL;
473 blkvsc_req->write = 0;
475 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
476 blkvsc_req->request.DataBuffer.Offset = 0;
477 blkvsc_req->request.DataBuffer.Length = 0;
479 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
480 blkvsc_req->cmd_len = 10;
483 * Set this here since the completion routine may be invoked and
484 * completed before we return
486 blkvsc_req->cond = 0;
487 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
489 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
491 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
496 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
497 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
499 struct blkvsc_request *blkvsc_req;
500 struct page *page_buf;
502 unsigned char device_type;
504 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
506 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
510 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
511 page_buf = alloc_page(GFP_KERNEL);
513 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
517 init_waitqueue_head(&blkvsc_req->wevent);
518 blkvsc_req->dev = blkdev;
519 blkvsc_req->req = NULL;
520 blkvsc_req->write = 0;
522 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
523 blkvsc_req->request.DataBuffer.Offset = 0;
524 blkvsc_req->request.DataBuffer.Length = 64;
526 blkvsc_req->cmnd[0] = INQUIRY;
527 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
528 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
529 blkvsc_req->cmnd[4] = 64;
530 blkvsc_req->cmd_len = 6;
533 * Set this here since the completion routine may be invoked and
534 * completed before we return
536 blkvsc_req->cond = 0;
538 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
540 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
541 blkvsc_req, blkvsc_req->cond);
543 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
545 buf = kmap(page_buf);
547 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
549 device_type = buf[0] & 0x1F;
551 if (device_type == 0x0) {
552 blkdev->device_type = HARDDISK_TYPE;
553 } else if (device_type == 0x5) {
554 blkdev->device_type = DVD_TYPE;
556 /* TODO: this is currently unsupported device type */
557 blkdev->device_type = UNKNOWN_DEV_TYPE;
560 DPRINT_DBG(BLKVSC_DRV, "device type %d\n", device_type);
562 blkdev->device_id_len = buf[7];
563 if (blkdev->device_id_len > 64)
564 blkdev->device_id_len = 64;
566 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
567 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
568 * blkdev->device_id_len); */
572 __free_page(page_buf);
574 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
579 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
580 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
582 struct blkvsc_request *blkvsc_req;
583 struct page *page_buf;
585 struct scsi_sense_hdr sense_hdr;
587 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
589 blkdev->sector_size = 0;
590 blkdev->capacity = 0;
591 blkdev->media_not_present = 0; /* assume a disk is present */
593 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
597 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
598 page_buf = alloc_page(GFP_KERNEL);
600 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
604 init_waitqueue_head(&blkvsc_req->wevent);
605 blkvsc_req->dev = blkdev;
606 blkvsc_req->req = NULL;
607 blkvsc_req->write = 0;
609 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
610 blkvsc_req->request.DataBuffer.Offset = 0;
611 blkvsc_req->request.DataBuffer.Length = 8;
613 blkvsc_req->cmnd[0] = READ_CAPACITY;
614 blkvsc_req->cmd_len = 16;
617 * Set this here since the completion routine may be invoked
618 * and completed before we return
620 blkvsc_req->cond = 0;
622 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
624 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
625 blkvsc_req, blkvsc_req->cond);
627 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
630 if (blkvsc_req->request.Status) {
631 scsi_normalize_sense(blkvsc_req->sense_buffer,
632 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
634 if (sense_hdr.asc == 0x3A) {
635 /* Medium not present */
636 blkdev->media_not_present = 1;
640 buf = kmap(page_buf);
643 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
644 (buf[2] << 8) | buf[3]) + 1;
645 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
646 (buf[6] << 8) | buf[7];
650 __free_page(page_buf);
652 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
657 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
659 struct blkvsc_request *blkvsc_req;
660 struct page *page_buf;
662 struct scsi_sense_hdr sense_hdr;
664 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
666 blkdev->sector_size = 0;
667 blkdev->capacity = 0;
668 blkdev->media_not_present = 0; /* assume a disk is present */
670 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
674 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
675 page_buf = alloc_page(GFP_KERNEL);
677 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
681 init_waitqueue_head(&blkvsc_req->wevent);
682 blkvsc_req->dev = blkdev;
683 blkvsc_req->req = NULL;
684 blkvsc_req->write = 0;
686 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
687 blkvsc_req->request.DataBuffer.Offset = 0;
688 blkvsc_req->request.DataBuffer.Length = 12;
690 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
691 blkvsc_req->cmd_len = 16;
694 * Set this here since the completion routine may be invoked
695 * and completed before we return
697 blkvsc_req->cond = 0;
699 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
701 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
702 blkvsc_req, blkvsc_req->cond);
704 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
707 if (blkvsc_req->request.Status) {
708 scsi_normalize_sense(blkvsc_req->sense_buffer,
709 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
710 if (sense_hdr.asc == 0x3A) {
711 /* Medium not present */
712 blkdev->media_not_present = 1;
716 buf = kmap(page_buf);
719 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
720 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
723 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
724 (buf[2] << 8) | buf[3]) + 1;
725 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
726 (buf[6] << 8) | buf[7];
731 __free_page(page_buf);
733 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
739 * blkvsc_remove() - Callback when our device is removed
741 static int blkvsc_remove(struct device *device)
743 struct driver_context *driver_ctx =
744 driver_to_driver_context(device->driver);
745 struct blkvsc_driver_context *blkvsc_drv_ctx =
746 (struct blkvsc_driver_context *)driver_ctx;
747 struct storvsc_driver_object *storvsc_drv_obj =
748 &blkvsc_drv_ctx->drv_obj;
749 struct vm_device *device_ctx = device_to_vm_device(device);
750 struct hv_device *device_obj = &device_ctx->device_obj;
751 struct block_device_context *blkdev = dev_get_drvdata(device);
755 DPRINT_ENTER(BLKVSC_DRV);
757 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
759 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
760 DPRINT_EXIT(BLKVSC_DRV);
765 * Call to the vsc driver to let it know that the device is being
768 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
771 DPRINT_ERR(BLKVSC_DRV,
772 "unable to remove blkvsc device (ret %d)", ret);
775 /* Get to a known state */
776 spin_lock_irqsave(&blkdev->lock, flags);
778 blkdev->shutting_down = 1;
780 blk_stop_queue(blkdev->gd->queue);
782 spin_unlock_irqrestore(&blkdev->lock, flags);
784 while (blkdev->num_outstanding_reqs) {
785 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
786 blkdev->num_outstanding_reqs);
790 blkvsc_do_flush(blkdev);
792 spin_lock_irqsave(&blkdev->lock, flags);
794 blkvsc_cancel_pending_reqs(blkdev);
796 spin_unlock_irqrestore(&blkdev->lock, flags);
798 blk_cleanup_queue(blkdev->gd->queue);
800 del_gendisk(blkdev->gd);
802 kmem_cache_destroy(blkdev->request_pool);
806 DPRINT_EXIT(BLKVSC_DRV);
811 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
813 /* ASSERT(blkvsc_req->req); */
814 /* ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8)); */
816 blkvsc_req->cmd_len = 16;
818 if (blkvsc_req->sector_start > 0xffffffff) {
819 if (rq_data_dir(blkvsc_req->req)) {
820 blkvsc_req->write = 1;
821 blkvsc_req->cmnd[0] = WRITE_16;
823 blkvsc_req->write = 0;
824 blkvsc_req->cmnd[0] = READ_16;
827 blkvsc_req->cmnd[1] |=
828 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
830 *(unsigned long long *)&blkvsc_req->cmnd[2] =
831 cpu_to_be64(blkvsc_req->sector_start);
832 *(unsigned int *)&blkvsc_req->cmnd[10] =
833 cpu_to_be32(blkvsc_req->sector_count);
834 } else if ((blkvsc_req->sector_count > 0xff) ||
835 (blkvsc_req->sector_start > 0x1fffff)) {
836 if (rq_data_dir(blkvsc_req->req)) {
837 blkvsc_req->write = 1;
838 blkvsc_req->cmnd[0] = WRITE_10;
840 blkvsc_req->write = 0;
841 blkvsc_req->cmnd[0] = READ_10;
844 blkvsc_req->cmnd[1] |=
845 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
847 *(unsigned int *)&blkvsc_req->cmnd[2] =
848 cpu_to_be32(blkvsc_req->sector_start);
849 *(unsigned short *)&blkvsc_req->cmnd[7] =
850 cpu_to_be16(blkvsc_req->sector_count);
852 if (rq_data_dir(blkvsc_req->req)) {
853 blkvsc_req->write = 1;
854 blkvsc_req->cmnd[0] = WRITE_6;
856 blkvsc_req->write = 0;
857 blkvsc_req->cmnd[0] = READ_6;
860 *(unsigned int *)&blkvsc_req->cmnd[1] =
861 cpu_to_be32(blkvsc_req->sector_start) >> 8;
862 blkvsc_req->cmnd[1] &= 0x1f;
863 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
867 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
868 void (*request_completion)(struct hv_storvsc_request *))
870 struct block_device_context *blkdev = blkvsc_req->dev;
871 struct vm_device *device_ctx = blkdev->device_ctx;
872 struct driver_context *driver_ctx =
873 driver_to_driver_context(device_ctx->device.driver);
874 struct blkvsc_driver_context *blkvsc_drv_ctx =
875 (struct blkvsc_driver_context *)driver_ctx;
876 struct storvsc_driver_object *storvsc_drv_obj =
877 &blkvsc_drv_ctx->drv_obj;
878 struct hv_storvsc_request *storvsc_req;
881 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
882 "req %p type %s start_sector %lu count %ld offset %d "
883 "len %d\n", blkvsc_req,
884 (blkvsc_req->write) ? "WRITE" : "READ",
885 (unsigned long) blkvsc_req->sector_start,
886 blkvsc_req->sector_count,
887 blkvsc_req->request.DataBuffer.Offset,
888 blkvsc_req->request.DataBuffer.Length);
890 for (i = 0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++) {
891 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
892 "req %p pfn[%d] %llx\n",
894 blkvsc_req->request.DataBuffer.PfnArray[i]);
898 storvsc_req = &blkvsc_req->request;
899 storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
900 sizeof(struct blkvsc_request));
902 storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
904 storvsc_req->OnIOCompletion = request_completion;
905 storvsc_req->Context = blkvsc_req;
907 storvsc_req->Host = blkdev->port;
908 storvsc_req->Bus = blkdev->path;
909 storvsc_req->TargetId = blkdev->target;
910 storvsc_req->LunId = 0; /* this is not really used at all */
912 storvsc_req->CdbLen = blkvsc_req->cmd_len;
913 storvsc_req->Cdb = blkvsc_req->cmnd;
915 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
916 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
918 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
919 &blkvsc_req->request);
921 blkdev->num_outstanding_reqs++;
927 * We break the request into 1 or more blkvsc_requests and submit
928 * them. If we cant submit them all, we put them on the
929 * pending_list. The blkvsc_request() will work on the pending_list.
931 static int blkvsc_do_request(struct block_device_context *blkdev,
934 struct bio *bio = NULL;
935 struct bio_vec *bvec = NULL;
936 struct bio_vec *prev_bvec = NULL;
937 struct blkvsc_request *blkvsc_req = NULL;
938 struct blkvsc_request *tmp;
941 sector_t start_sector;
942 unsigned long num_sectors = 0;
945 struct blkvsc_request_group *group = NULL;
947 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu\n", blkdev, req,
948 (unsigned long)blk_rq_pos(req));
950 /* Create a group to tie req to list of blkvsc_reqs */
951 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
955 INIT_LIST_HEAD(&group->blkvsc_req_list);
956 group->outstanding = group->status = 0;
958 start_sector = blk_rq_pos(req);
960 /* foreach bio in the request */
962 for (bio = req->bio; bio; bio = bio->bi_next) {
964 * Map this bio into an existing or new storvsc request
966 bio_for_each_segment(bvec, bio, seg_idx) {
967 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
968 "- req %p bio %p bvec %p seg_idx %d "
969 "databuf_idx %d\n", req, bio, bvec,
970 seg_idx, databuf_idx);
972 /* Get a new storvsc request */
975 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
976 /* hole at the begin of page */
977 || (bvec->bv_offset != 0) ||
978 /* hold at the end of page */
980 (prev_bvec->bv_len != PAGE_SIZE))) {
981 /* submit the prev one */
983 blkvsc_req->sector_start = start_sector;
984 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
986 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
987 blkvsc_init_rw(blkvsc_req);
991 * Create new blkvsc_req to represent
994 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
996 /* free up everything */
997 list_for_each_entry_safe(
999 &group->blkvsc_req_list,
1001 list_del(&blkvsc_req->req_entry);
1002 kmem_cache_free(blkdev->request_pool, blkvsc_req);
1005 kmem_cache_free(blkdev->request_pool, group);
1009 memset(blkvsc_req, 0,
1010 sizeof(struct blkvsc_request));
1012 blkvsc_req->dev = blkdev;
1013 blkvsc_req->req = req;
1014 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1015 blkvsc_req->request.DataBuffer.Length = 0;
1017 /* Add to the group */
1018 blkvsc_req->group = group;
1019 blkvsc_req->group->outstanding++;
1020 list_add_tail(&blkvsc_req->req_entry,
1021 &blkvsc_req->group->blkvsc_req_list);
1023 start_sector += num_sectors;
1028 /* Add the curr bvec/segment to the curr blkvsc_req */
1029 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1030 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1035 num_sectors += bvec->bv_len >> 9;
1037 } /* bio_for_each_segment */
1039 } /* rq_for_each_bio */
1042 /* Handle the last one */
1044 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1045 blkdev, req, blkvsc_req->group,
1046 blkvsc_req->group->outstanding);
1048 blkvsc_req->sector_start = start_sector;
1049 sector_div(blkvsc_req->sector_start,
1050 (blkdev->sector_size >> 9));
1052 blkvsc_req->sector_count = num_sectors /
1053 (blkdev->sector_size >> 9);
1055 blkvsc_init_rw(blkvsc_req);
1058 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1060 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1061 "pending_list - blkvsc_req %p start_sect %lu"
1062 " sect_count %ld (%lu %ld)\n", blkvsc_req,
1063 (unsigned long)blkvsc_req->sector_start,
1064 blkvsc_req->sector_count,
1065 (unsigned long)start_sector,
1066 (unsigned long)num_sectors);
1068 list_add_tail(&blkvsc_req->pend_entry,
1069 &blkdev->pending_list);
1071 ret = blkvsc_submit_request(blkvsc_req,
1072 blkvsc_request_completion);
1075 list_add_tail(&blkvsc_req->pend_entry,
1076 &blkdev->pending_list);
1079 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1080 "start_sect %lu sect_count %ld (%lu %ld) "
1081 "ret %d\n", blkvsc_req,
1082 (unsigned long)blkvsc_req->sector_start,
1083 blkvsc_req->sector_count,
1084 (unsigned long)start_sector,
1092 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1094 struct blkvsc_request *blkvsc_req =
1095 (struct blkvsc_request *)request->Context;
1096 struct block_device_context *blkdev =
1097 (struct block_device_context *)blkvsc_req->dev;
1098 struct scsi_sense_hdr sense_hdr;
1100 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1103 blkdev->num_outstanding_reqs--;
1105 if (blkvsc_req->request.Status)
1106 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1107 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1108 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1110 blkvsc_req->cond = 1;
1111 wake_up_interruptible(&blkvsc_req->wevent);
1114 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1116 struct blkvsc_request *blkvsc_req =
1117 (struct blkvsc_request *)request->Context;
1118 struct block_device_context *blkdev =
1119 (struct block_device_context *)blkvsc_req->dev;
1120 unsigned long flags;
1121 struct blkvsc_request *comp_req, *tmp;
1123 /* ASSERT(blkvsc_req->group); */
1125 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1126 "sect_start %lu sect_count %ld len %d group outstd %d "
1127 "total outstd %d\n",
1128 blkdev, blkvsc_req, blkvsc_req->group,
1129 (blkvsc_req->write) ? "WRITE" : "READ",
1130 (unsigned long)blkvsc_req->sector_start,
1131 blkvsc_req->sector_count,
1132 blkvsc_req->request.DataBuffer.Length,
1133 blkvsc_req->group->outstanding,
1134 blkdev->num_outstanding_reqs);
1136 spin_lock_irqsave(&blkdev->lock, flags);
1138 blkdev->num_outstanding_reqs--;
1139 blkvsc_req->group->outstanding--;
1142 * Only start processing when all the blkvsc_reqs are
1143 * completed. This guarantees no out-of-order blkvsc_req
1144 * completion when calling end_that_request_first()
1146 if (blkvsc_req->group->outstanding == 0) {
1147 list_for_each_entry_safe(comp_req, tmp,
1148 &blkvsc_req->group->blkvsc_req_list,
1150 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1151 "sect_start %lu sect_count %ld\n",
1153 (unsigned long)comp_req->sector_start,
1154 comp_req->sector_count);
1156 list_del(&comp_req->req_entry);
1158 if (!__blk_end_request(comp_req->req,
1159 (!comp_req->request.Status ? 0 : -EIO),
1160 comp_req->sector_count * blkdev->sector_size)) {
1162 * All the sectors have been xferred ie the
1165 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1167 kmem_cache_free(blkdev->request_pool,
1171 kmem_cache_free(blkdev->request_pool, comp_req);
1174 if (!blkdev->shutting_down) {
1175 blkvsc_do_pending_reqs(blkdev);
1176 blk_start_queue(blkdev->gd->queue);
1177 blkvsc_request(blkdev->gd->queue);
1181 spin_unlock_irqrestore(&blkdev->lock, flags);
1184 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1186 struct blkvsc_request *pend_req, *tmp;
1187 struct blkvsc_request *comp_req, *tmp2;
1191 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1193 /* Flush the pending list first */
1194 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1197 * The pend_req could be part of a partially completed
1198 * request. If so, complete those req first until we
1201 list_for_each_entry_safe(comp_req, tmp2,
1202 &pend_req->group->blkvsc_req_list,
1204 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1205 "sect_start %lu sect_count %ld\n",
1207 (unsigned long) comp_req->sector_start,
1208 comp_req->sector_count);
1210 if (comp_req == pend_req)
1213 list_del(&comp_req->req_entry);
1215 if (comp_req->req) {
1216 ret = __blk_end_request(comp_req->req,
1217 (!comp_req->request.Status ? 0 : -EIO),
1218 comp_req->sector_count *
1219 blkdev->sector_size);
1221 /* FIXME: shouldn't this do more than return? */
1226 kmem_cache_free(blkdev->request_pool, comp_req);
1229 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1232 list_del(&pend_req->pend_entry);
1234 list_del(&pend_req->req_entry);
1236 if (comp_req->req) {
1237 if (!__blk_end_request(pend_req->req, -EIO,
1238 pend_req->sector_count *
1239 blkdev->sector_size)) {
1241 * All the sectors have been xferred ie the
1244 DPRINT_DBG(BLKVSC_DRV,
1245 "blkvsc_cancel_pending_reqs() - "
1246 "req %p COMPLETED\n", pend_req->req);
1247 kmem_cache_free(blkdev->request_pool,
1252 kmem_cache_free(blkdev->request_pool, pend_req);
1259 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1261 struct blkvsc_request *pend_req, *tmp;
1264 /* Flush the pending list first */
1265 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1267 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1270 ret = blkvsc_submit_request(pend_req,
1271 blkvsc_request_completion);
1275 list_del(&pend_req->pend_entry);
1281 static void blkvsc_request(struct request_queue *queue)
1283 struct block_device_context *blkdev = NULL;
1284 struct request *req;
1287 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1288 while ((req = blk_peek_request(queue)) != NULL) {
1289 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1291 blkdev = req->rq_disk->private_data;
1292 if (blkdev->shutting_down || req->cmd_type != REQ_TYPE_FS ||
1293 blkdev->media_not_present) {
1294 __blk_end_request_cur(req, 0);
1298 ret = blkvsc_do_pending_reqs(blkdev);
1301 DPRINT_DBG(BLKVSC_DRV,
1302 "- stop queue - pending_list not empty\n");
1303 blk_stop_queue(queue);
1307 blk_start_request(req);
1309 ret = blkvsc_do_request(blkdev, req);
1311 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1312 blk_stop_queue(queue);
1314 } else if (ret < 0) {
1315 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1316 blk_requeue_request(queue, req);
1317 blk_stop_queue(queue);
1323 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1325 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1327 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1328 blkdev->gd->disk_name);
1331 spin_lock(&blkdev->lock);
1333 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1334 spin_unlock(&blkdev->lock);
1335 check_disk_change(bdev);
1336 spin_lock(&blkdev->lock);
1341 spin_unlock(&blkdev->lock);
1346 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1348 struct block_device_context *blkdev = disk->private_data;
1350 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1351 blkdev->gd->disk_name);
1354 spin_lock(&blkdev->lock);
1355 if (blkdev->users == 1) {
1356 spin_unlock(&blkdev->lock);
1357 blkvsc_do_flush(blkdev);
1358 spin_lock(&blkdev->lock);
1363 spin_unlock(&blkdev->lock);
1368 static int blkvsc_media_changed(struct gendisk *gd)
1370 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1374 static int blkvsc_revalidate_disk(struct gendisk *gd)
1376 struct block_device_context *blkdev = gd->private_data;
1378 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1380 if (blkdev->device_type == DVD_TYPE) {
1381 blkvsc_do_read_capacity(blkdev);
1382 set_capacity(blkdev->gd, blkdev->capacity *
1383 (blkdev->sector_size/512));
1384 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1389 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1391 sector_t total_sectors = get_capacity(bd->bd_disk);
1392 sector_t cylinder_times_heads = 0;
1395 int sectors_per_track = 0;
1400 if (total_sectors > (65535 * 16 * 255))
1401 total_sectors = (65535 * 16 * 255);
1403 if (total_sectors >= (65535 * 16 * 63)) {
1404 sectors_per_track = 255;
1407 cylinder_times_heads = total_sectors;
1408 /* sector_div stores the quotient in cylinder_times_heads */
1409 rem = sector_div(cylinder_times_heads, sectors_per_track);
1411 sectors_per_track = 17;
1413 cylinder_times_heads = total_sectors;
1414 /* sector_div stores the quotient in cylinder_times_heads */
1415 rem = sector_div(cylinder_times_heads, sectors_per_track);
1417 temp = cylinder_times_heads + 1023;
1418 /* sector_div stores the quotient in temp */
1419 rem = sector_div(temp, 1024);
1427 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1428 sectors_per_track = 31;
1431 cylinder_times_heads = total_sectors;
1433 * sector_div stores the quotient in
1434 * cylinder_times_heads
1436 rem = sector_div(cylinder_times_heads,
1440 if (cylinder_times_heads >= (heads * 1024)) {
1441 sectors_per_track = 63;
1444 cylinder_times_heads = total_sectors;
1446 * sector_div stores the quotient in
1447 * cylinder_times_heads
1449 rem = sector_div(cylinder_times_heads,
1454 temp = cylinder_times_heads;
1455 /* sector_div stores the quotient in temp */
1456 rem = sector_div(temp, heads);
1460 hg->sectors = sectors_per_track;
1461 hg->cylinders = cylinders;
1463 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1469 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1470 unsigned cmd, unsigned long argument)
1472 /* struct block_device_context *blkdev = bd->bd_disk->private_data; */
1477 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1478 * than just a GUID. Commented it out for now.
1481 case HDIO_GET_IDENTITY:
1482 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1483 if (copy_to_user((void __user *)arg, blkdev->device_id,
1484 blkdev->device_id_len))
1496 static int __init blkvsc_init(void)
1500 BUILD_BUG_ON(sizeof(sector_t) != 8);
1502 DPRINT_ENTER(BLKVSC_DRV);
1504 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1506 ret = blkvsc_drv_init(BlkVscInitialize);
1508 DPRINT_EXIT(BLKVSC_DRV);
1513 static void __exit blkvsc_exit(void)
1515 DPRINT_ENTER(BLKVSC_DRV);
1517 DPRINT_ENTER(BLKVSC_DRV);
1520 MODULE_LICENSE("GPL");
1521 MODULE_VERSION(HV_DRV_VERSION);
1522 MODULE_DESCRIPTION("Microsoft Hyper-V virtual block driver");
1523 module_init(blkvsc_init);
1524 module_exit(blkvsc_exit);