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 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
181 storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
183 /* Callback to client driver to complete the initialization */
184 drv_init(&storvsc_drv_obj->Base);
186 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
187 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
188 sizeof(struct hv_guid));
190 drv_ctx->probe = blkvsc_probe;
191 drv_ctx->remove = blkvsc_remove;
192 drv_ctx->shutdown = blkvsc_shutdown;
194 /* The driver belongs to vmbus */
195 ret = vmbus_child_driver_register(drv_ctx);
200 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
202 struct device **curr = (struct device **)data;
204 return 1; /* stop iterating */
207 static void blkvsc_drv_exit(void)
209 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
210 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
211 struct device *current_dev;
218 ret = driver_for_each_device(&drv_ctx->driver, NULL,
219 (void *) ¤t_dev,
223 DPRINT_WARN(BLKVSC_DRV,
224 "driver_for_each_device returned %d", ret);
227 if (current_dev == NULL)
230 /* Initiate removal from the top-down */
231 device_unregister(current_dev);
234 if (storvsc_drv_obj->Base.OnCleanup)
235 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
237 vmbus_child_driver_unregister(drv_ctx);
243 * blkvsc_probe - Add a new device for this driver
245 static int blkvsc_probe(struct device *device)
247 struct driver_context *driver_ctx =
248 driver_to_driver_context(device->driver);
249 struct blkvsc_driver_context *blkvsc_drv_ctx =
250 (struct blkvsc_driver_context *)driver_ctx;
251 struct storvsc_driver_object *storvsc_drv_obj =
252 &blkvsc_drv_ctx->drv_obj;
253 struct vm_device *device_ctx = device_to_vm_device(device);
254 struct hv_device *device_obj = &device_ctx->device_obj;
256 struct block_device_context *blkdev = NULL;
257 struct storvsc_device_info device_info;
261 static int ide0_registered;
262 static int ide1_registered;
264 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
266 if (!storvsc_drv_obj->Base.OnDeviceAdd) {
267 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
272 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
278 INIT_LIST_HEAD(&blkdev->pending_list);
280 /* Initialize what we can here */
281 spin_lock_init(&blkdev->lock);
283 /* ASSERT(sizeof(struct blkvsc_request_group) <= */
284 /* sizeof(struct blkvsc_request)); */
286 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
287 sizeof(struct blkvsc_request) +
288 storvsc_drv_obj->RequestExtSize, 0,
289 SLAB_HWCACHE_ALIGN, NULL);
290 if (!blkdev->request_pool) {
296 /* Call to the vsc driver to add the device */
297 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
299 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
303 blkdev->device_ctx = device_ctx;
304 /* this identified the device 0 or 1 */
305 blkdev->target = device_info.TargetId;
306 /* this identified the ide ctrl 0 or 1 */
307 blkdev->path = device_info.PathId;
309 dev_set_drvdata(device, blkdev);
311 /* Calculate the major and device num */
312 if (blkdev->path == 0) {
314 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
316 if (!ide0_registered) {
317 ret = register_blkdev(major, "ide");
319 DPRINT_ERR(BLKVSC_DRV,
320 "register_blkdev() failed! ret %d",
327 } else if (blkdev->path == 1) {
329 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
331 if (!ide1_registered) {
332 ret = register_blkdev(major, "ide");
334 DPRINT_ERR(BLKVSC_DRV,
335 "register_blkdev() failed! ret %d",
343 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
348 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
350 blkdev->gd = alloc_disk(BLKVSC_MINORS);
352 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
357 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
359 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
360 blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
361 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
362 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
363 blk_queue_dma_alignment(blkdev->gd->queue, 511);
365 blkdev->gd->major = major;
366 if (devnum == 1 || devnum == 3)
367 blkdev->gd->first_minor = BLKVSC_MINORS;
369 blkdev->gd->first_minor = 0;
370 blkdev->gd->fops = &block_ops;
371 blkdev->gd->private_data = blkdev;
372 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
374 blkvsc_do_inquiry(blkdev);
375 if (blkdev->device_type == DVD_TYPE) {
376 set_disk_ro(blkdev->gd, 1);
377 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
378 blkvsc_do_read_capacity(blkdev);
380 blkvsc_do_read_capacity16(blkdev);
383 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
384 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
386 add_disk(blkdev->gd);
388 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
389 blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
390 blkdev->sector_size);
395 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
399 if (blkdev->request_pool) {
400 kmem_cache_destroy(blkdev->request_pool);
401 blkdev->request_pool = NULL;
410 static void blkvsc_shutdown(struct device *device)
412 struct block_device_context *blkdev = dev_get_drvdata(device);
418 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
419 blkdev->users, blkdev->gd->disk_name);
421 spin_lock_irqsave(&blkdev->lock, flags);
423 blkdev->shutting_down = 1;
425 blk_stop_queue(blkdev->gd->queue);
427 spin_unlock_irqrestore(&blkdev->lock, flags);
429 while (blkdev->num_outstanding_reqs) {
430 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
431 blkdev->num_outstanding_reqs);
435 blkvsc_do_flush(blkdev);
437 spin_lock_irqsave(&blkdev->lock, flags);
439 blkvsc_cancel_pending_reqs(blkdev);
441 spin_unlock_irqrestore(&blkdev->lock, flags);
444 static int blkvsc_do_flush(struct block_device_context *blkdev)
446 struct blkvsc_request *blkvsc_req;
448 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
450 if (blkdev->device_type != HARDDISK_TYPE)
453 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
457 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
458 init_waitqueue_head(&blkvsc_req->wevent);
459 blkvsc_req->dev = blkdev;
460 blkvsc_req->req = NULL;
461 blkvsc_req->write = 0;
463 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
464 blkvsc_req->request.DataBuffer.Offset = 0;
465 blkvsc_req->request.DataBuffer.Length = 0;
467 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
468 blkvsc_req->cmd_len = 10;
471 * Set this here since the completion routine may be invoked and
472 * completed before we return
474 blkvsc_req->cond = 0;
475 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
477 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
479 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
484 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
485 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
487 struct blkvsc_request *blkvsc_req;
488 struct page *page_buf;
490 unsigned char device_type;
492 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
494 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
498 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
499 page_buf = alloc_page(GFP_KERNEL);
501 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
505 init_waitqueue_head(&blkvsc_req->wevent);
506 blkvsc_req->dev = blkdev;
507 blkvsc_req->req = NULL;
508 blkvsc_req->write = 0;
510 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
511 blkvsc_req->request.DataBuffer.Offset = 0;
512 blkvsc_req->request.DataBuffer.Length = 64;
514 blkvsc_req->cmnd[0] = INQUIRY;
515 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
516 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
517 blkvsc_req->cmnd[4] = 64;
518 blkvsc_req->cmd_len = 6;
521 * Set this here since the completion routine may be invoked and
522 * completed before we return
524 blkvsc_req->cond = 0;
526 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
528 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
529 blkvsc_req, blkvsc_req->cond);
531 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
533 buf = kmap(page_buf);
535 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
537 device_type = buf[0] & 0x1F;
539 if (device_type == 0x0) {
540 blkdev->device_type = HARDDISK_TYPE;
541 } else if (device_type == 0x5) {
542 blkdev->device_type = DVD_TYPE;
544 /* TODO: this is currently unsupported device type */
545 blkdev->device_type = UNKNOWN_DEV_TYPE;
548 DPRINT_DBG(BLKVSC_DRV, "device type %d\n", device_type);
550 blkdev->device_id_len = buf[7];
551 if (blkdev->device_id_len > 64)
552 blkdev->device_id_len = 64;
554 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
555 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
556 * blkdev->device_id_len); */
560 __free_page(page_buf);
562 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
567 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
568 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
570 struct blkvsc_request *blkvsc_req;
571 struct page *page_buf;
573 struct scsi_sense_hdr sense_hdr;
575 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
577 blkdev->sector_size = 0;
578 blkdev->capacity = 0;
579 blkdev->media_not_present = 0; /* assume a disk is present */
581 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
585 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
586 page_buf = alloc_page(GFP_KERNEL);
588 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
592 init_waitqueue_head(&blkvsc_req->wevent);
593 blkvsc_req->dev = blkdev;
594 blkvsc_req->req = NULL;
595 blkvsc_req->write = 0;
597 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
598 blkvsc_req->request.DataBuffer.Offset = 0;
599 blkvsc_req->request.DataBuffer.Length = 8;
601 blkvsc_req->cmnd[0] = READ_CAPACITY;
602 blkvsc_req->cmd_len = 16;
605 * Set this here since the completion routine may be invoked
606 * and completed before we return
608 blkvsc_req->cond = 0;
610 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
612 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
613 blkvsc_req, blkvsc_req->cond);
615 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
618 if (blkvsc_req->request.Status) {
619 scsi_normalize_sense(blkvsc_req->sense_buffer,
620 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
622 if (sense_hdr.asc == 0x3A) {
623 /* Medium not present */
624 blkdev->media_not_present = 1;
628 buf = kmap(page_buf);
631 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
632 (buf[2] << 8) | buf[3]) + 1;
633 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
634 (buf[6] << 8) | buf[7];
638 __free_page(page_buf);
640 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
645 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
647 struct blkvsc_request *blkvsc_req;
648 struct page *page_buf;
650 struct scsi_sense_hdr sense_hdr;
652 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
654 blkdev->sector_size = 0;
655 blkdev->capacity = 0;
656 blkdev->media_not_present = 0; /* assume a disk is present */
658 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
662 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
663 page_buf = alloc_page(GFP_KERNEL);
665 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
669 init_waitqueue_head(&blkvsc_req->wevent);
670 blkvsc_req->dev = blkdev;
671 blkvsc_req->req = NULL;
672 blkvsc_req->write = 0;
674 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
675 blkvsc_req->request.DataBuffer.Offset = 0;
676 blkvsc_req->request.DataBuffer.Length = 12;
678 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
679 blkvsc_req->cmd_len = 16;
682 * Set this here since the completion routine may be invoked
683 * and completed before we return
685 blkvsc_req->cond = 0;
687 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
689 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
690 blkvsc_req, blkvsc_req->cond);
692 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
695 if (blkvsc_req->request.Status) {
696 scsi_normalize_sense(blkvsc_req->sense_buffer,
697 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
698 if (sense_hdr.asc == 0x3A) {
699 /* Medium not present */
700 blkdev->media_not_present = 1;
704 buf = kmap(page_buf);
707 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
708 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
711 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
712 (buf[2] << 8) | buf[3]) + 1;
713 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
714 (buf[6] << 8) | buf[7];
719 __free_page(page_buf);
721 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
727 * blkvsc_remove() - Callback when our device is removed
729 static int blkvsc_remove(struct device *device)
731 struct driver_context *driver_ctx =
732 driver_to_driver_context(device->driver);
733 struct blkvsc_driver_context *blkvsc_drv_ctx =
734 (struct blkvsc_driver_context *)driver_ctx;
735 struct storvsc_driver_object *storvsc_drv_obj =
736 &blkvsc_drv_ctx->drv_obj;
737 struct vm_device *device_ctx = device_to_vm_device(device);
738 struct hv_device *device_obj = &device_ctx->device_obj;
739 struct block_device_context *blkdev = dev_get_drvdata(device);
743 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
745 if (!storvsc_drv_obj->Base.OnDeviceRemove)
749 * Call to the vsc driver to let it know that the device is being
752 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
755 DPRINT_ERR(BLKVSC_DRV,
756 "unable to remove blkvsc device (ret %d)", ret);
759 /* Get to a known state */
760 spin_lock_irqsave(&blkdev->lock, flags);
762 blkdev->shutting_down = 1;
764 blk_stop_queue(blkdev->gd->queue);
766 spin_unlock_irqrestore(&blkdev->lock, flags);
768 while (blkdev->num_outstanding_reqs) {
769 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
770 blkdev->num_outstanding_reqs);
774 blkvsc_do_flush(blkdev);
776 spin_lock_irqsave(&blkdev->lock, flags);
778 blkvsc_cancel_pending_reqs(blkdev);
780 spin_unlock_irqrestore(&blkdev->lock, flags);
782 blk_cleanup_queue(blkdev->gd->queue);
784 del_gendisk(blkdev->gd);
786 kmem_cache_destroy(blkdev->request_pool);
793 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
795 /* ASSERT(blkvsc_req->req); */
796 /* ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8)); */
798 blkvsc_req->cmd_len = 16;
800 if (blkvsc_req->sector_start > 0xffffffff) {
801 if (rq_data_dir(blkvsc_req->req)) {
802 blkvsc_req->write = 1;
803 blkvsc_req->cmnd[0] = WRITE_16;
805 blkvsc_req->write = 0;
806 blkvsc_req->cmnd[0] = READ_16;
809 blkvsc_req->cmnd[1] |=
810 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
812 *(unsigned long long *)&blkvsc_req->cmnd[2] =
813 cpu_to_be64(blkvsc_req->sector_start);
814 *(unsigned int *)&blkvsc_req->cmnd[10] =
815 cpu_to_be32(blkvsc_req->sector_count);
816 } else if ((blkvsc_req->sector_count > 0xff) ||
817 (blkvsc_req->sector_start > 0x1fffff)) {
818 if (rq_data_dir(blkvsc_req->req)) {
819 blkvsc_req->write = 1;
820 blkvsc_req->cmnd[0] = WRITE_10;
822 blkvsc_req->write = 0;
823 blkvsc_req->cmnd[0] = READ_10;
826 blkvsc_req->cmnd[1] |=
827 (blkvsc_req->req->cmd_flags & REQ_FUA) ? 0x8 : 0;
829 *(unsigned int *)&blkvsc_req->cmnd[2] =
830 cpu_to_be32(blkvsc_req->sector_start);
831 *(unsigned short *)&blkvsc_req->cmnd[7] =
832 cpu_to_be16(blkvsc_req->sector_count);
834 if (rq_data_dir(blkvsc_req->req)) {
835 blkvsc_req->write = 1;
836 blkvsc_req->cmnd[0] = WRITE_6;
838 blkvsc_req->write = 0;
839 blkvsc_req->cmnd[0] = READ_6;
842 *(unsigned int *)&blkvsc_req->cmnd[1] =
843 cpu_to_be32(blkvsc_req->sector_start) >> 8;
844 blkvsc_req->cmnd[1] &= 0x1f;
845 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
849 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
850 void (*request_completion)(struct hv_storvsc_request *))
852 struct block_device_context *blkdev = blkvsc_req->dev;
853 struct vm_device *device_ctx = blkdev->device_ctx;
854 struct driver_context *driver_ctx =
855 driver_to_driver_context(device_ctx->device.driver);
856 struct blkvsc_driver_context *blkvsc_drv_ctx =
857 (struct blkvsc_driver_context *)driver_ctx;
858 struct storvsc_driver_object *storvsc_drv_obj =
859 &blkvsc_drv_ctx->drv_obj;
860 struct hv_storvsc_request *storvsc_req;
863 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
864 "req %p type %s start_sector %lu count %ld offset %d "
865 "len %d\n", blkvsc_req,
866 (blkvsc_req->write) ? "WRITE" : "READ",
867 (unsigned long) blkvsc_req->sector_start,
868 blkvsc_req->sector_count,
869 blkvsc_req->request.DataBuffer.Offset,
870 blkvsc_req->request.DataBuffer.Length);
872 for (i = 0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++) {
873 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
874 "req %p pfn[%d] %llx\n",
876 blkvsc_req->request.DataBuffer.PfnArray[i]);
880 storvsc_req = &blkvsc_req->request;
881 storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
882 sizeof(struct blkvsc_request));
884 storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
886 storvsc_req->OnIOCompletion = request_completion;
887 storvsc_req->Context = blkvsc_req;
889 storvsc_req->Host = blkdev->port;
890 storvsc_req->Bus = blkdev->path;
891 storvsc_req->TargetId = blkdev->target;
892 storvsc_req->LunId = 0; /* this is not really used at all */
894 storvsc_req->CdbLen = blkvsc_req->cmd_len;
895 storvsc_req->Cdb = blkvsc_req->cmnd;
897 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
898 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
900 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
901 &blkvsc_req->request);
903 blkdev->num_outstanding_reqs++;
909 * We break the request into 1 or more blkvsc_requests and submit
910 * them. If we cant submit them all, we put them on the
911 * pending_list. The blkvsc_request() will work on the pending_list.
913 static int blkvsc_do_request(struct block_device_context *blkdev,
916 struct bio *bio = NULL;
917 struct bio_vec *bvec = NULL;
918 struct bio_vec *prev_bvec = NULL;
919 struct blkvsc_request *blkvsc_req = NULL;
920 struct blkvsc_request *tmp;
923 sector_t start_sector;
924 unsigned long num_sectors = 0;
927 struct blkvsc_request_group *group = NULL;
929 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu\n", blkdev, req,
930 (unsigned long)blk_rq_pos(req));
932 /* Create a group to tie req to list of blkvsc_reqs */
933 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
937 INIT_LIST_HEAD(&group->blkvsc_req_list);
938 group->outstanding = group->status = 0;
940 start_sector = blk_rq_pos(req);
942 /* foreach bio in the request */
944 for (bio = req->bio; bio; bio = bio->bi_next) {
946 * Map this bio into an existing or new storvsc request
948 bio_for_each_segment(bvec, bio, seg_idx) {
949 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
950 "- req %p bio %p bvec %p seg_idx %d "
951 "databuf_idx %d\n", req, bio, bvec,
952 seg_idx, databuf_idx);
954 /* Get a new storvsc request */
957 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
958 /* hole at the begin of page */
959 || (bvec->bv_offset != 0) ||
960 /* hold at the end of page */
962 (prev_bvec->bv_len != PAGE_SIZE))) {
963 /* submit the prev one */
965 blkvsc_req->sector_start = start_sector;
966 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
968 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
969 blkvsc_init_rw(blkvsc_req);
973 * Create new blkvsc_req to represent
976 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
978 /* free up everything */
979 list_for_each_entry_safe(
981 &group->blkvsc_req_list,
983 list_del(&blkvsc_req->req_entry);
984 kmem_cache_free(blkdev->request_pool, blkvsc_req);
987 kmem_cache_free(blkdev->request_pool, group);
991 memset(blkvsc_req, 0,
992 sizeof(struct blkvsc_request));
994 blkvsc_req->dev = blkdev;
995 blkvsc_req->req = req;
996 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
997 blkvsc_req->request.DataBuffer.Length = 0;
999 /* Add to the group */
1000 blkvsc_req->group = group;
1001 blkvsc_req->group->outstanding++;
1002 list_add_tail(&blkvsc_req->req_entry,
1003 &blkvsc_req->group->blkvsc_req_list);
1005 start_sector += num_sectors;
1010 /* Add the curr bvec/segment to the curr blkvsc_req */
1011 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1012 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1017 num_sectors += bvec->bv_len >> 9;
1019 } /* bio_for_each_segment */
1021 } /* rq_for_each_bio */
1024 /* Handle the last one */
1026 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1027 blkdev, req, blkvsc_req->group,
1028 blkvsc_req->group->outstanding);
1030 blkvsc_req->sector_start = start_sector;
1031 sector_div(blkvsc_req->sector_start,
1032 (blkdev->sector_size >> 9));
1034 blkvsc_req->sector_count = num_sectors /
1035 (blkdev->sector_size >> 9);
1037 blkvsc_init_rw(blkvsc_req);
1040 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1042 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1043 "pending_list - blkvsc_req %p start_sect %lu"
1044 " sect_count %ld (%lu %ld)\n", blkvsc_req,
1045 (unsigned long)blkvsc_req->sector_start,
1046 blkvsc_req->sector_count,
1047 (unsigned long)start_sector,
1048 (unsigned long)num_sectors);
1050 list_add_tail(&blkvsc_req->pend_entry,
1051 &blkdev->pending_list);
1053 ret = blkvsc_submit_request(blkvsc_req,
1054 blkvsc_request_completion);
1057 list_add_tail(&blkvsc_req->pend_entry,
1058 &blkdev->pending_list);
1061 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1062 "start_sect %lu sect_count %ld (%lu %ld) "
1063 "ret %d\n", blkvsc_req,
1064 (unsigned long)blkvsc_req->sector_start,
1065 blkvsc_req->sector_count,
1066 (unsigned long)start_sector,
1074 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1076 struct blkvsc_request *blkvsc_req =
1077 (struct blkvsc_request *)request->Context;
1078 struct block_device_context *blkdev =
1079 (struct block_device_context *)blkvsc_req->dev;
1080 struct scsi_sense_hdr sense_hdr;
1082 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1085 blkdev->num_outstanding_reqs--;
1087 if (blkvsc_req->request.Status)
1088 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1089 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1090 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1092 blkvsc_req->cond = 1;
1093 wake_up_interruptible(&blkvsc_req->wevent);
1096 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1098 struct blkvsc_request *blkvsc_req =
1099 (struct blkvsc_request *)request->Context;
1100 struct block_device_context *blkdev =
1101 (struct block_device_context *)blkvsc_req->dev;
1102 unsigned long flags;
1103 struct blkvsc_request *comp_req, *tmp;
1105 /* ASSERT(blkvsc_req->group); */
1107 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1108 "sect_start %lu sect_count %ld len %d group outstd %d "
1109 "total outstd %d\n",
1110 blkdev, blkvsc_req, blkvsc_req->group,
1111 (blkvsc_req->write) ? "WRITE" : "READ",
1112 (unsigned long)blkvsc_req->sector_start,
1113 blkvsc_req->sector_count,
1114 blkvsc_req->request.DataBuffer.Length,
1115 blkvsc_req->group->outstanding,
1116 blkdev->num_outstanding_reqs);
1118 spin_lock_irqsave(&blkdev->lock, flags);
1120 blkdev->num_outstanding_reqs--;
1121 blkvsc_req->group->outstanding--;
1124 * Only start processing when all the blkvsc_reqs are
1125 * completed. This guarantees no out-of-order blkvsc_req
1126 * completion when calling end_that_request_first()
1128 if (blkvsc_req->group->outstanding == 0) {
1129 list_for_each_entry_safe(comp_req, tmp,
1130 &blkvsc_req->group->blkvsc_req_list,
1132 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1133 "sect_start %lu sect_count %ld\n",
1135 (unsigned long)comp_req->sector_start,
1136 comp_req->sector_count);
1138 list_del(&comp_req->req_entry);
1140 if (!__blk_end_request(comp_req->req,
1141 (!comp_req->request.Status ? 0 : -EIO),
1142 comp_req->sector_count * blkdev->sector_size)) {
1144 * All the sectors have been xferred ie the
1147 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1149 kmem_cache_free(blkdev->request_pool,
1153 kmem_cache_free(blkdev->request_pool, comp_req);
1156 if (!blkdev->shutting_down) {
1157 blkvsc_do_pending_reqs(blkdev);
1158 blk_start_queue(blkdev->gd->queue);
1159 blkvsc_request(blkdev->gd->queue);
1163 spin_unlock_irqrestore(&blkdev->lock, flags);
1166 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1168 struct blkvsc_request *pend_req, *tmp;
1169 struct blkvsc_request *comp_req, *tmp2;
1173 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1175 /* Flush the pending list first */
1176 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1179 * The pend_req could be part of a partially completed
1180 * request. If so, complete those req first until we
1183 list_for_each_entry_safe(comp_req, tmp2,
1184 &pend_req->group->blkvsc_req_list,
1186 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1187 "sect_start %lu sect_count %ld\n",
1189 (unsigned long) comp_req->sector_start,
1190 comp_req->sector_count);
1192 if (comp_req == pend_req)
1195 list_del(&comp_req->req_entry);
1197 if (comp_req->req) {
1198 ret = __blk_end_request(comp_req->req,
1199 (!comp_req->request.Status ? 0 : -EIO),
1200 comp_req->sector_count *
1201 blkdev->sector_size);
1203 /* FIXME: shouldn't this do more than return? */
1208 kmem_cache_free(blkdev->request_pool, comp_req);
1211 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1214 list_del(&pend_req->pend_entry);
1216 list_del(&pend_req->req_entry);
1218 if (comp_req->req) {
1219 if (!__blk_end_request(pend_req->req, -EIO,
1220 pend_req->sector_count *
1221 blkdev->sector_size)) {
1223 * All the sectors have been xferred ie the
1226 DPRINT_DBG(BLKVSC_DRV,
1227 "blkvsc_cancel_pending_reqs() - "
1228 "req %p COMPLETED\n", pend_req->req);
1229 kmem_cache_free(blkdev->request_pool,
1234 kmem_cache_free(blkdev->request_pool, pend_req);
1241 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1243 struct blkvsc_request *pend_req, *tmp;
1246 /* Flush the pending list first */
1247 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1249 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1252 ret = blkvsc_submit_request(pend_req,
1253 blkvsc_request_completion);
1257 list_del(&pend_req->pend_entry);
1263 static void blkvsc_request(struct request_queue *queue)
1265 struct block_device_context *blkdev = NULL;
1266 struct request *req;
1269 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1270 while ((req = blk_peek_request(queue)) != NULL) {
1271 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1273 blkdev = req->rq_disk->private_data;
1274 if (blkdev->shutting_down || req->cmd_type != REQ_TYPE_FS ||
1275 blkdev->media_not_present) {
1276 __blk_end_request_cur(req, 0);
1280 ret = blkvsc_do_pending_reqs(blkdev);
1283 DPRINT_DBG(BLKVSC_DRV,
1284 "- stop queue - pending_list not empty\n");
1285 blk_stop_queue(queue);
1289 blk_start_request(req);
1291 ret = blkvsc_do_request(blkdev, req);
1293 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1294 blk_stop_queue(queue);
1296 } else if (ret < 0) {
1297 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1298 blk_requeue_request(queue, req);
1299 blk_stop_queue(queue);
1305 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1307 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1309 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1310 blkdev->gd->disk_name);
1313 spin_lock(&blkdev->lock);
1315 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1316 spin_unlock(&blkdev->lock);
1317 check_disk_change(bdev);
1318 spin_lock(&blkdev->lock);
1323 spin_unlock(&blkdev->lock);
1328 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1330 struct block_device_context *blkdev = disk->private_data;
1332 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1333 blkdev->gd->disk_name);
1336 spin_lock(&blkdev->lock);
1337 if (blkdev->users == 1) {
1338 spin_unlock(&blkdev->lock);
1339 blkvsc_do_flush(blkdev);
1340 spin_lock(&blkdev->lock);
1345 spin_unlock(&blkdev->lock);
1350 static int blkvsc_media_changed(struct gendisk *gd)
1352 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1356 static int blkvsc_revalidate_disk(struct gendisk *gd)
1358 struct block_device_context *blkdev = gd->private_data;
1360 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1362 if (blkdev->device_type == DVD_TYPE) {
1363 blkvsc_do_read_capacity(blkdev);
1364 set_capacity(blkdev->gd, blkdev->capacity *
1365 (blkdev->sector_size/512));
1366 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1371 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1373 sector_t total_sectors = get_capacity(bd->bd_disk);
1374 sector_t cylinder_times_heads = 0;
1377 int sectors_per_track = 0;
1382 if (total_sectors > (65535 * 16 * 255))
1383 total_sectors = (65535 * 16 * 255);
1385 if (total_sectors >= (65535 * 16 * 63)) {
1386 sectors_per_track = 255;
1389 cylinder_times_heads = total_sectors;
1390 /* sector_div stores the quotient in cylinder_times_heads */
1391 rem = sector_div(cylinder_times_heads, sectors_per_track);
1393 sectors_per_track = 17;
1395 cylinder_times_heads = total_sectors;
1396 /* sector_div stores the quotient in cylinder_times_heads */
1397 rem = sector_div(cylinder_times_heads, sectors_per_track);
1399 temp = cylinder_times_heads + 1023;
1400 /* sector_div stores the quotient in temp */
1401 rem = sector_div(temp, 1024);
1409 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1410 sectors_per_track = 31;
1413 cylinder_times_heads = total_sectors;
1415 * sector_div stores the quotient in
1416 * cylinder_times_heads
1418 rem = sector_div(cylinder_times_heads,
1422 if (cylinder_times_heads >= (heads * 1024)) {
1423 sectors_per_track = 63;
1426 cylinder_times_heads = total_sectors;
1428 * sector_div stores the quotient in
1429 * cylinder_times_heads
1431 rem = sector_div(cylinder_times_heads,
1436 temp = cylinder_times_heads;
1437 /* sector_div stores the quotient in temp */
1438 rem = sector_div(temp, heads);
1442 hg->sectors = sectors_per_track;
1443 hg->cylinders = cylinders;
1445 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1451 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1452 unsigned cmd, unsigned long argument)
1454 /* struct block_device_context *blkdev = bd->bd_disk->private_data; */
1459 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1460 * than just a GUID. Commented it out for now.
1463 case HDIO_GET_IDENTITY:
1464 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1465 if (copy_to_user((void __user *)arg, blkdev->device_id,
1466 blkdev->device_id_len))
1478 static int __init blkvsc_init(void)
1482 BUILD_BUG_ON(sizeof(sector_t) != 8);
1484 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1486 ret = blkvsc_drv_init(BlkVscInitialize);
1491 static void __exit blkvsc_exit(void)
1496 MODULE_LICENSE("GPL");
1497 MODULE_VERSION(HV_DRV_VERSION);
1498 MODULE_DESCRIPTION("Microsoft Hyper-V virtual block driver");
1499 module_init(blkvsc_init);
1500 module_exit(blkvsc_exit);