3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Hank Janssen <hjanssen@microsoft.com>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/blkdev.h>
27 #include <linux/major.h>
28 #include <linux/delay.h>
29 #include <linux/hdreg.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_dbg.h>
36 #include "include/logging.h"
37 #include "include/vmbus.h"
39 #include "include/StorVscApi.h"
44 #define BLKVSC_MINORS 64
49 enum blkvsc_device_type {
56 * This request ties the struct request and struct
57 * blkvsc_request/STORVSC_REQUEST together A struct request may be
58 * represented by 1 or more struct blkvsc_request
60 struct blkvsc_request_group {
64 struct list_head blkvsc_req_list; /* list of blkvsc_requests */
68 struct blkvsc_request {
69 struct list_head req_entry; /* blkvsc_request_group.blkvsc_req_list */
71 struct list_head pend_entry; /* block_device_context.pending_list */
73 struct request *req; /* This may be null if we generate a request internally */
74 struct block_device_context *dev;
75 struct blkvsc_request_group *group; /* The group this request is part of. Maybe null */
77 wait_queue_head_t wevent;
81 sector_t sector_start;
82 unsigned long sector_count;
84 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
85 unsigned char cmd_len;
86 unsigned char cmnd[MAX_COMMAND_SIZE];
88 STORVSC_REQUEST request;
89 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap, because - */
90 /* The extension buffer falls right here and is pointed to by request.Extension; */
93 /* Per device structure */
94 struct block_device_context {
95 struct device_context *device_ctx; /* point back to our device context */
96 struct kmem_cache *request_pool;
99 enum blkvsc_device_type device_type;
100 struct list_head pending_list;
102 unsigned char device_id[64];
103 unsigned int device_id_len;
104 int num_outstanding_reqs;
106 int media_not_present;
107 unsigned int sector_size;
111 unsigned char target;
116 struct blkvsc_driver_context {
117 /* !! These must be the first 2 fields !! */
118 struct driver_context drv_ctx;
119 STORVSC_DRIVER_OBJECT drv_obj;
123 static int blkvsc_probe(struct device *dev);
124 static int blkvsc_remove(struct device *device);
125 static void blkvsc_shutdown(struct device *device);
127 static int blkvsc_open(struct block_device *bdev, fmode_t mode);
128 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
129 static int blkvsc_media_changed(struct gendisk *gd);
130 static int blkvsc_revalidate_disk(struct gendisk *gd);
131 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
132 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
133 unsigned cmd, unsigned long argument);
134 static void blkvsc_request(struct request_queue *queue);
135 static void blkvsc_request_completion(STORVSC_REQUEST* request);
136 static int blkvsc_do_request(struct block_device_context *blkdev, struct request *req);
137 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req, void (*request_completion)(STORVSC_REQUEST*) );
138 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
139 static void blkvsc_cmd_completion(STORVSC_REQUEST* request);
140 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
141 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
142 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
143 static int blkvsc_do_flush(struct block_device_context *blkdev);
144 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
145 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
148 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
150 /* The one and only one */
151 static struct blkvsc_driver_context g_blkvsc_drv;
154 static struct block_device_operations block_ops =
156 .owner = THIS_MODULE,
158 .release = blkvsc_release,
159 .media_changed = blkvsc_media_changed,
160 .revalidate_disk = blkvsc_revalidate_disk,
161 .getgeo = blkvsc_getgeo,
162 .ioctl = blkvsc_ioctl,
167 Name: blkvsc_drv_init()
169 Desc: BlkVsc driver initialization.
172 int blkvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
175 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_blkvsc_drv.drv_obj;
176 struct driver_context *drv_ctx=&g_blkvsc_drv.drv_ctx;
178 DPRINT_ENTER(BLKVSC_DRV);
180 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
182 storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
184 /* Callback to client driver to complete the initialization */
185 pfn_drv_init(&storvsc_drv_obj->Base);
187 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
188 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, sizeof(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);
197 DPRINT_EXIT(BLKVSC_DRV);
203 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
205 struct device **curr = (struct device **)data;
207 return 1; /* stop iterating */
212 Name: blkvsc_drv_exit()
217 void blkvsc_drv_exit(void)
219 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_blkvsc_drv.drv_obj;
220 struct driver_context *drv_ctx=&g_blkvsc_drv.drv_ctx;
221 struct device *current_dev=NULL;
224 DPRINT_ENTER(BLKVSC_DRV);
231 ret = driver_for_each_device(&drv_ctx->driver, NULL,
232 (void *) ¤t_dev,
236 DPRINT_WARN(BLKVSC_DRV,
237 "driver_for_each_device returned %d", ret);
240 if (current_dev == NULL)
243 /* Initiate removal from the top-down */
244 device_unregister(current_dev);
247 if (storvsc_drv_obj->Base.OnCleanup)
248 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
250 vmbus_child_driver_unregister(drv_ctx);
252 DPRINT_EXIT(BLKVSC_DRV);
261 Desc: Add a new device for this driver
264 static int blkvsc_probe(struct device *device)
268 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
269 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
270 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
272 struct device_context *device_ctx = device_to_device_context(device);
273 struct hv_device *device_obj = &device_ctx->device_obj;
275 struct block_device_context *blkdev=NULL;
276 STORVSC_DEVICE_INFO device_info;
280 static int ide0_registered=0;
281 static int ide1_registered=0;
283 DPRINT_ENTER(BLKVSC_DRV);
285 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
287 if (!storvsc_drv_obj->Base.OnDeviceAdd)
289 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
295 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
302 INIT_LIST_HEAD(&blkdev->pending_list);
304 /* Initialize what we can here */
305 spin_lock_init(&blkdev->lock);
307 ASSERT(sizeof(struct blkvsc_request_group) <= sizeof(struct blkvsc_request));
309 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
310 sizeof(struct blkvsc_request) + storvsc_drv_obj->RequestExtSize, 0,
311 SLAB_HWCACHE_ALIGN, NULL);
312 if (!blkdev->request_pool)
319 /* Call to the vsc driver to add the device */
320 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
323 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
327 blkdev->device_ctx = device_ctx;
328 blkdev->target = device_info.TargetId; /* this identified the device 0 or 1 */
329 blkdev->path = device_info.PathId; /* this identified the ide ctrl 0 or 1 */
331 dev_set_drvdata(device, blkdev);
333 /* Calculate the major and device num */
334 if (blkdev->path == 0)
337 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
339 if (!ide0_registered)
341 ret = register_blkdev(major, "ide");
344 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
351 else if (blkdev->path == 1)
354 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
356 if (!ide1_registered)
358 ret = register_blkdev(major, "ide");
361 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
371 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
376 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
378 blkdev->gd = alloc_disk(BLKVSC_MINORS);
381 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
386 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
388 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
389 blk_queue_max_phys_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
390 blk_queue_max_hw_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
391 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
392 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
393 blk_queue_dma_alignment(blkdev->gd->queue, 511);
395 blkdev->gd->major = major;
396 if (devnum == 1 || devnum == 3)
397 blkdev->gd->first_minor = BLKVSC_MINORS;
399 blkdev->gd->first_minor = 0;
400 blkdev->gd->fops = &block_ops;
401 blkdev->gd->private_data = blkdev;
402 sprintf(blkdev->gd->disk_name, "hd%c", 'a'+ devnum);
404 blkvsc_do_inquiry(blkdev);
405 if (blkdev->device_type == DVD_TYPE)
407 set_disk_ro(blkdev->gd, 1);
408 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
409 blkvsc_do_read_capacity(blkdev);
413 blkvsc_do_read_capacity16(blkdev);
416 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
417 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
419 add_disk(blkdev->gd);
421 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d", blkdev->gd->disk_name, (unsigned long) blkdev->capacity, blkdev->sector_size);
426 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
431 if (blkdev->request_pool)
433 kmem_cache_destroy(blkdev->request_pool);
434 blkdev->request_pool = NULL;
440 DPRINT_EXIT(BLKVSC_DRV);
445 static void blkvsc_shutdown(struct device *device)
447 struct block_device_context *blkdev = dev_get_drvdata(device);
453 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
455 spin_lock_irqsave(&blkdev->lock, flags);
457 blkdev->shutting_down = 1;
459 blk_stop_queue(blkdev->gd->queue);
461 spin_unlock_irqrestore(&blkdev->lock, flags);
463 while (blkdev->num_outstanding_reqs)
465 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", blkdev->num_outstanding_reqs);
470 blkvsc_do_flush(blkdev);
472 spin_lock_irqsave(&blkdev->lock, flags);
474 blkvsc_cancel_pending_reqs(blkdev);
476 spin_unlock_irqrestore(&blkdev->lock, flags);
479 static int blkvsc_do_flush(struct block_device_context *blkdev)
481 struct blkvsc_request *blkvsc_req=NULL;
483 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
485 if (blkdev->device_type != HARDDISK_TYPE)
488 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
494 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
495 init_waitqueue_head(&blkvsc_req->wevent);
496 blkvsc_req->dev = blkdev;
497 blkvsc_req->req = NULL;
498 blkvsc_req->write = 0;
500 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
501 blkvsc_req->request.DataBuffer.Offset = 0;
502 blkvsc_req->request.DataBuffer.Length = 0;
504 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
505 blkvsc_req->cmd_len = 10;
507 /* Set this here since the completion routine may be invoked and completed before we return */
509 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
511 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
513 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
518 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
519 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
521 struct blkvsc_request *blkvsc_req=NULL;
522 struct page *page_buf;
524 unsigned char device_type;
526 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
528 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
534 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
535 page_buf = alloc_page(GFP_KERNEL);
538 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
542 init_waitqueue_head(&blkvsc_req->wevent);
543 blkvsc_req->dev = blkdev;
544 blkvsc_req->req = NULL;
545 blkvsc_req->write = 0;
547 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
548 blkvsc_req->request.DataBuffer.Offset = 0;
549 blkvsc_req->request.DataBuffer.Length = 64;
551 blkvsc_req->cmnd[0] = INQUIRY;
552 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
553 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
554 blkvsc_req->cmnd[4] = 64;
555 blkvsc_req->cmd_len = 6;
557 /* Set this here since the completion routine may be invoked and completed before we return */
560 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
562 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
564 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
566 buf = kmap(page_buf);
568 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
570 device_type = buf[0] & 0x1F;
572 if (device_type == 0x0)
574 blkdev->device_type = HARDDISK_TYPE;
576 else if (device_type == 0x5)
578 blkdev->device_type = DVD_TYPE;
582 /* TODO: this is currently unsupported device type */
583 blkdev->device_type = UNKNOWN_DEV_TYPE;
586 DPRINT_DBG(BLKVSC_DRV, "device type %d \n", device_type);
588 blkdev->device_id_len = buf[7];
589 if (blkdev->device_id_len > 64)
590 blkdev->device_id_len = 64;
592 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
593 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
594 * blkdev->device_id_len); */
598 __free_page(page_buf);
600 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
605 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
606 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
608 struct blkvsc_request *blkvsc_req=NULL;
609 struct page *page_buf;
611 struct scsi_sense_hdr sense_hdr;
613 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
615 blkdev->sector_size = 0;
616 blkdev->capacity = 0;
617 blkdev->media_not_present = 0; /* assume a disk is present */
619 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
625 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
626 page_buf = alloc_page(GFP_KERNEL);
629 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
633 init_waitqueue_head(&blkvsc_req->wevent);
634 blkvsc_req->dev = blkdev;
635 blkvsc_req->req = NULL;
636 blkvsc_req->write = 0;
638 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
639 blkvsc_req->request.DataBuffer.Offset = 0;
640 blkvsc_req->request.DataBuffer.Length = 8;
642 blkvsc_req->cmnd[0] = READ_CAPACITY;
643 blkvsc_req->cmd_len = 16;
646 * Set this here since the completion routine may be invoked
647 * and completed before we return
651 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
653 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
655 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
658 if (blkvsc_req->request.Status)
660 scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
662 if (sense_hdr.asc == 0x3A) /* Medium not present */
664 blkdev->media_not_present = 1;
669 buf = kmap(page_buf);
672 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) + 1;
673 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
677 __free_page(page_buf);
679 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
685 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
687 struct blkvsc_request *blkvsc_req=NULL;
688 struct page *page_buf;
690 struct scsi_sense_hdr sense_hdr;
692 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
694 blkdev->sector_size = 0;
695 blkdev->capacity = 0;
696 blkdev->media_not_present = 0; /* assume a disk is present */
698 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
704 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
705 page_buf = alloc_page(GFP_KERNEL);
708 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
712 init_waitqueue_head(&blkvsc_req->wevent);
713 blkvsc_req->dev = blkdev;
714 blkvsc_req->req = NULL;
715 blkvsc_req->write = 0;
717 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
718 blkvsc_req->request.DataBuffer.Offset = 0;
719 blkvsc_req->request.DataBuffer.Length = 12;
721 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
722 blkvsc_req->cmd_len = 16;
725 * Set this here since the completion routine may be invoked
726 * and completed before we return
730 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
732 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
734 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
737 if (blkvsc_req->request.Status)
739 scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
741 if (sense_hdr.asc == 0x3A) /* Medium not present */
743 blkdev->media_not_present = 1;
748 buf = kmap(page_buf);
751 blkdev->capacity = be64_to_cpu(*(unsigned long long*) &buf[0]) + 1;
752 blkdev->sector_size = be32_to_cpu(*(unsigned int*)&buf[8]);
754 /* blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) + 1; */
755 /* blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; */
759 __free_page(page_buf);
761 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
768 Name: blkvsc_remove()
770 Desc: Callback when our device is removed
773 static int blkvsc_remove(struct device *device)
777 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
778 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
779 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
781 struct device_context *device_ctx = device_to_device_context(device);
782 struct hv_device *device_obj = &device_ctx->device_obj;
783 struct block_device_context *blkdev = dev_get_drvdata(device);
786 DPRINT_ENTER(BLKVSC_DRV);
788 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
790 if (!storvsc_drv_obj->Base.OnDeviceRemove)
792 DPRINT_EXIT(BLKVSC_DRV);
796 /* Call to the vsc driver to let it know that the device is being removed */
797 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
801 DPRINT_ERR(BLKVSC_DRV, "unable to remove blkvsc device (ret %d)", ret);
804 /* Get to a known state */
805 spin_lock_irqsave(&blkdev->lock, flags);
807 blkdev->shutting_down = 1;
809 blk_stop_queue(blkdev->gd->queue);
811 spin_unlock_irqrestore(&blkdev->lock, flags);
813 while (blkdev->num_outstanding_reqs)
815 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", blkdev->num_outstanding_reqs);
820 blkvsc_do_flush(blkdev);
822 spin_lock_irqsave(&blkdev->lock, flags);
824 blkvsc_cancel_pending_reqs(blkdev);
826 spin_unlock_irqrestore(&blkdev->lock, flags);
828 blk_cleanup_queue(blkdev->gd->queue);
830 del_gendisk(blkdev->gd);
832 kmem_cache_destroy(blkdev->request_pool);
836 DPRINT_EXIT(BLKVSC_DRV);
841 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
843 ASSERT(blkvsc_req->req);
844 ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8));
846 blkvsc_req->cmd_len = 16;
848 if (blkvsc_req->sector_start > 0xffffffff)
850 if (rq_data_dir(blkvsc_req->req))
852 blkvsc_req->write = 1;
853 blkvsc_req->cmnd[0] = WRITE_16;
857 blkvsc_req->write = 0;
858 blkvsc_req->cmnd[0] = READ_16;
861 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
863 *(unsigned long long*)&blkvsc_req->cmnd[2] = cpu_to_be64(blkvsc_req->sector_start);
864 *(unsigned int*)&blkvsc_req->cmnd[10] = cpu_to_be32(blkvsc_req->sector_count);
866 else if ((blkvsc_req->sector_count > 0xff) || (blkvsc_req->sector_start > 0x1fffff))
868 if (rq_data_dir(blkvsc_req->req))
870 blkvsc_req->write = 1;
871 blkvsc_req->cmnd[0] = WRITE_10;
875 blkvsc_req->write = 0;
876 blkvsc_req->cmnd[0] = READ_10;
879 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
881 *(unsigned int *)&blkvsc_req->cmnd[2] = cpu_to_be32(blkvsc_req->sector_start);
882 *(unsigned short*)&blkvsc_req->cmnd[7] = cpu_to_be16(blkvsc_req->sector_count);
886 if (rq_data_dir(blkvsc_req->req))
888 blkvsc_req->write = 1;
889 blkvsc_req->cmnd[0] = WRITE_6;
893 blkvsc_req->write = 0;
894 blkvsc_req->cmnd[0] = READ_6;
897 *(unsigned int *)&blkvsc_req->cmnd[1] = cpu_to_be32(blkvsc_req->sector_start) >> 8;
898 blkvsc_req->cmnd[1] &= 0x1f;
899 blkvsc_req->cmnd[4] = (unsigned char) blkvsc_req->sector_count;
903 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req, void (*request_completion)(STORVSC_REQUEST*) )
905 struct block_device_context *blkdev = blkvsc_req->dev;
906 struct device_context *device_ctx=blkdev->device_ctx;
907 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
908 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
909 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
912 STORVSC_REQUEST *storvsc_req;
914 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - req %p type %s start_sector %lu count %ld offset %d len %d\n",
916 (blkvsc_req->write)?"WRITE":"READ",
917 (unsigned long) blkvsc_req->sector_start,
918 blkvsc_req->sector_count,
919 blkvsc_req->request.DataBuffer.Offset,
920 blkvsc_req->request.DataBuffer.Length);
922 /*for (i=0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++)
924 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - req %p pfn[%d] %llx\n",
927 blkvsc_req->request.DataBuffer.PfnArray[i]);
930 storvsc_req = &blkvsc_req->request;
931 storvsc_req->Extension = (void*)((unsigned long)blkvsc_req + sizeof(struct blkvsc_request));
933 storvsc_req->Type = blkvsc_req->write? WRITE_TYPE : READ_TYPE;
935 storvsc_req->OnIOCompletion = request_completion;
936 storvsc_req->Context = blkvsc_req;
938 storvsc_req->Host = blkdev->port;
939 storvsc_req->Bus = blkdev->path;
940 storvsc_req->TargetId = blkdev->target;
941 storvsc_req->LunId = 0; /* this is not really used at all */
943 storvsc_req->CdbLen = blkvsc_req->cmd_len;
944 storvsc_req->Cdb = blkvsc_req->cmnd;
946 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
947 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
949 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj, &blkvsc_req->request);
952 blkdev->num_outstanding_reqs++;
960 * We break the request into 1 or more blkvsc_requests and submit
961 * them. If we cant submit them all, we put them on the
962 * pending_list. The blkvsc_request() will work on the pending_list.
965 static int blkvsc_do_request(struct block_device_context *blkdev, struct request *req)
967 struct bio *bio=NULL;
968 struct bio_vec *bvec=NULL;
969 struct bio_vec *prev_bvec=NULL;
971 struct blkvsc_request *blkvsc_req=NULL;
972 struct blkvsc_request *tmp;
976 sector_t start_sector;
977 unsigned long num_sectors = 0;
980 struct blkvsc_request_group *group=NULL;
982 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu \n", blkdev, req, (unsigned long) blk_rq_pos(req));
984 /* Create a group to tie req to list of blkvsc_reqs */
985 group = (struct blkvsc_request_group*)kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
991 INIT_LIST_HEAD(&group->blkvsc_req_list);
992 group->outstanding = group->status = 0;
994 start_sector = blk_rq_pos(req);
996 /* foreach bio in the request */
998 for (bio = req->bio; bio; bio = bio->bi_next)
1000 /* Map this bio into an existing or new storvsc request */
1001 bio_for_each_segment (bvec, bio, seg_idx)
1003 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() - req %p bio %p bvec %p seg_idx %d databuf_idx %d\n",
1004 req, bio, bvec, seg_idx, databuf_idx);
1006 /* Get a new storvsc request */
1007 if ( (!blkvsc_req) || /* 1st-time */
1008 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT) ||
1009 (bvec->bv_offset != 0) || /* hole at the begin of page */
1010 (prev_bvec && (prev_bvec->bv_len != PAGE_SIZE)) ) /* hold at the end of page */
1012 /* submit the prev one */
1015 blkvsc_req->sector_start = start_sector;
1016 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
1018 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
1020 blkvsc_init_rw(blkvsc_req);
1023 /* Create new blkvsc_req to represent the current bvec */
1024 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
1027 /* free up everything */
1028 list_for_each_entry_safe(blkvsc_req, tmp, &group->blkvsc_req_list, req_entry)
1030 list_del(&blkvsc_req->req_entry);
1031 kmem_cache_free(blkdev->request_pool, blkvsc_req);
1034 kmem_cache_free(blkdev->request_pool, group);
1038 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
1040 blkvsc_req->dev = blkdev;
1041 blkvsc_req->req = req;
1042 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1043 blkvsc_req->request.DataBuffer.Length = 0;
1045 /* Add to the group */
1046 blkvsc_req->group = group;
1047 blkvsc_req->group->outstanding++;
1048 list_add_tail(&blkvsc_req->req_entry, &blkvsc_req->group->blkvsc_req_list);
1050 start_sector += num_sectors;
1055 /* Add the curr bvec/segment to the curr blkvsc_req */
1056 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1057 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1062 num_sectors += bvec->bv_len >> 9;
1064 } /* bio_for_each_segment */
1066 } /* rq_for_each_bio */
1068 /* Handle the last one */
1071 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n", blkdev, req, blkvsc_req->group, blkvsc_req->group->outstanding);
1073 blkvsc_req->sector_start = start_sector;
1074 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
1076 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
1078 blkvsc_init_rw(blkvsc_req);
1081 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry)
1085 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to pending_list - blkvsc_req %p start_sect %lu sect_count %ld (%lu %ld)\n",
1086 blkvsc_req, blkvsc_req->sector_start, blkvsc_req->sector_count, (unsigned long) start_sector, (unsigned long) num_sectors);
1088 list_add_tail(&blkvsc_req->pend_entry, &blkdev->pending_list);
1092 ret = blkvsc_submit_request(blkvsc_req, blkvsc_request_completion);
1096 list_add_tail(&blkvsc_req->pend_entry, &blkdev->pending_list);
1099 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p start_sect %lu sect_count %ld (%lu %ld) ret %d\n",
1100 blkvsc_req, (unsigned long) blkvsc_req->sector_start, blkvsc_req->sector_count, (unsigned long) start_sector, num_sectors, ret);
1107 static void blkvsc_cmd_completion(STORVSC_REQUEST* request)
1109 struct blkvsc_request *blkvsc_req=(struct blkvsc_request*)request->Context;
1110 struct block_device_context *blkdev = (struct block_device_context*)blkvsc_req->dev;
1112 struct scsi_sense_hdr sense_hdr;
1114 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n", blkvsc_req);
1116 blkdev->num_outstanding_reqs--;
1118 if (blkvsc_req->request.Status)
1120 if (scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1122 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1126 blkvsc_req->cond =1;
1127 wake_up_interruptible(&blkvsc_req->wevent);
1130 static void blkvsc_request_completion(STORVSC_REQUEST* request)
1132 struct blkvsc_request *blkvsc_req=(struct blkvsc_request*)request->Context;
1133 struct block_device_context *blkdev = (struct block_device_context*)blkvsc_req->dev;
1134 unsigned long flags;
1135 struct blkvsc_request *comp_req, *tmp;
1137 ASSERT(blkvsc_req->group);
1139 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s sect_start %lu sect_count %ld len %d group outstd %d total outstd %d\n",
1143 (blkvsc_req->write)?"WRITE":"READ",
1144 (unsigned long) blkvsc_req->sector_start,
1145 blkvsc_req->sector_count,
1146 blkvsc_req->request.DataBuffer.Length,
1147 blkvsc_req->group->outstanding,
1148 blkdev->num_outstanding_reqs);
1150 spin_lock_irqsave(&blkdev->lock, flags);
1152 blkdev->num_outstanding_reqs--;
1153 blkvsc_req->group->outstanding--;
1156 * Only start processing when all the blkvsc_reqs are
1157 * completed. This guarantees no out-of-order blkvsc_req
1158 * completion when calling end_that_request_first()
1160 if (blkvsc_req->group->outstanding == 0)
1162 list_for_each_entry_safe(comp_req, tmp, &blkvsc_req->group->blkvsc_req_list, req_entry)
1164 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p sect_start %lu sect_count %ld \n",
1166 (unsigned long) comp_req->sector_start,
1167 comp_req->sector_count);
1169 list_del(&comp_req->req_entry);
1171 if (!__blk_end_request(
1173 (!comp_req->request.Status ? 0: -EIO),
1174 comp_req->sector_count * blkdev->sector_size))
1176 /* All the sectors have been xferred ie the request is done */
1177 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n", comp_req->req);
1178 kmem_cache_free(blkdev->request_pool, comp_req->group);
1181 kmem_cache_free(blkdev->request_pool, comp_req);
1184 if (!blkdev->shutting_down)
1186 blkvsc_do_pending_reqs(blkdev);
1187 blk_start_queue(blkdev->gd->queue);
1188 blkvsc_request(blkdev->gd->queue);
1192 spin_unlock_irqrestore(&blkdev->lock, flags);
1195 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1197 struct blkvsc_request *pend_req, *tmp;
1198 struct blkvsc_request *comp_req, *tmp2;
1202 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1204 /* Flush the pending list first */
1205 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list, pend_entry)
1208 * The pend_req could be part of a partially completed
1209 * request. If so, complete those req first until we
1212 list_for_each_entry_safe(comp_req, tmp2, &pend_req->group->blkvsc_req_list, req_entry)
1214 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p sect_start %lu sect_count %ld \n",
1216 (unsigned long) comp_req->sector_start,
1217 comp_req->sector_count);
1219 if (comp_req == pend_req)
1222 list_del(&comp_req->req_entry);
1226 ret = __blk_end_request(
1228 (!comp_req->request.Status ? 0 : -EIO),
1229 comp_req->sector_count * blkdev->sector_size);
1233 kmem_cache_free(blkdev->request_pool, comp_req);
1236 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n", pend_req);
1238 list_del(&pend_req->pend_entry);
1240 list_del(&pend_req->req_entry);
1244 if (!__blk_end_request(
1247 pend_req->sector_count * blkdev->sector_size))
1249 /* All the sectors have been xferred ie the request is done */
1250 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs() - req %p COMPLETED\n", pend_req->req);
1251 kmem_cache_free(blkdev->request_pool, pend_req->group);
1255 kmem_cache_free(blkdev->request_pool, pend_req);
1261 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1263 struct blkvsc_request *pend_req, *tmp;
1266 /* Flush the pending list first */
1267 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list, pend_entry)
1269 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n", pend_req);
1271 ret = blkvsc_submit_request(pend_req, blkvsc_request_completion);
1278 list_del(&pend_req->pend_entry);
1285 static void blkvsc_request(struct request_queue *queue)
1287 struct block_device_context *blkdev = NULL;
1288 struct request *req;
1291 DPRINT_DBG(BLKVSC_DRV, "- enter \n");
1292 while ((req = blk_peek_request(queue)) != NULL)
1294 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1296 blkdev = req->rq_disk->private_data;
1297 if (blkdev->shutting_down || !blk_fs_request(req) || blkdev->media_not_present) {
1298 __blk_end_request_cur(req, 0);
1302 ret = blkvsc_do_pending_reqs(blkdev);
1306 DPRINT_DBG(BLKVSC_DRV, "- stop queue - pending_list not empty\n");
1307 blk_stop_queue(queue);
1311 blk_start_request(req);
1313 ret = blkvsc_do_request(blkdev, req);
1316 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1317 blk_stop_queue(queue);
1322 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1323 blk_requeue_request(queue, req);
1324 blk_stop_queue(queue);
1330 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1332 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1334 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
1336 spin_lock(&blkdev->lock);
1338 if (!blkdev->users && blkdev->device_type == DVD_TYPE)
1340 spin_unlock(&blkdev->lock);
1341 check_disk_change(bdev);
1342 spin_lock(&blkdev->lock);
1347 spin_unlock(&blkdev->lock);
1351 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1353 struct block_device_context *blkdev = disk->private_data;
1355 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
1357 spin_lock(&blkdev->lock);
1358 if (blkdev->users == 1)
1360 spin_unlock(&blkdev->lock);
1361 blkvsc_do_flush(blkdev);
1362 spin_lock(&blkdev->lock);
1367 spin_unlock(&blkdev->lock);
1371 static int blkvsc_media_changed(struct gendisk *gd)
1373 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1378 static int blkvsc_revalidate_disk(struct gendisk *gd)
1380 struct block_device_context *blkdev = gd->private_data;
1382 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1384 if (blkdev->device_type == DVD_TYPE)
1386 blkvsc_do_read_capacity(blkdev);
1387 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
1388 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1393 int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1395 sector_t total_sectors = get_capacity(bd->bd_disk);
1396 sector_t cylinder_times_heads=0;
1399 int sectors_per_track=0;
1404 if (total_sectors > (65535 * 16 * 255)) {
1405 total_sectors = (65535 * 16 * 255);
1408 if (total_sectors >= (65535 * 16 * 63)) {
1409 sectors_per_track = 255;
1412 cylinder_times_heads = total_sectors;
1413 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1417 sectors_per_track = 17;
1419 cylinder_times_heads = total_sectors;
1420 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1422 temp = cylinder_times_heads + 1023;
1423 rem = sector_div(temp, 1024); /* sector_div stores the quotient in temp */
1431 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1432 sectors_per_track = 31;
1435 cylinder_times_heads = total_sectors;
1436 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1439 if (cylinder_times_heads >= (heads * 1024)) {
1440 sectors_per_track = 63;
1443 cylinder_times_heads = total_sectors;
1444 rem = sector_div(cylinder_times_heads, sectors_per_track); /* sector_div stores the quotient in cylinder_times_heads */
1448 temp = cylinder_times_heads;
1449 rem = sector_div(temp, heads); /* sector_div stores the quotient in temp */
1453 hg->sectors = sectors_per_track;
1454 hg->cylinders = cylinders;
1456 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
1461 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1462 unsigned cmd, unsigned long argument)
1464 struct block_device_context *blkdev = bd->bd_disk->private_data;
1469 /* TODO: I think there is certain format for HDIO_GET_IDENTITY rather than just */
1470 /* a GUID. Commented it out for now. */
1471 /*case HDIO_GET_IDENTITY:
1472 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1474 if (copy_to_user((void __user *)arg, blkdev->device_id, blkdev->device_id_len))
1489 MODULE_LICENSE("GPL");
1491 static int __init blkvsc_init(void)
1495 ASSERT(sizeof(sector_t) == 8); /* Make sure CONFIG_LBD is set */
1497 DPRINT_ENTER(BLKVSC_DRV);
1499 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1501 ret = blkvsc_drv_init(BlkVscInitialize);
1503 DPRINT_EXIT(BLKVSC_DRV);
1508 static void __exit blkvsc_exit(void)
1510 DPRINT_ENTER(BLKVSC_DRV);
1514 DPRINT_ENTER(BLKVSC_DRV);
1517 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
1519 module_init(blkvsc_init);
1520 module_exit(blkvsc_exit);