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>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/blkdev.h>
29 #include <linux/major.h>
30 #include <linux/delay.h>
31 #include <linux/hdreg.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_cmnd.h>
35 #include <scsi/scsi_eh.h>
36 #include <scsi/scsi_dbg.h>
38 #include "include/logging.h"
39 #include "include/vmbus.h"
41 #include "include/StorVscApi.h"
46 #define BLKVSC_MINORS 64
51 enum blkvsc_device_type {
57 // This request ties the struct request and struct blkvsc_request/STORVSC_REQUEST together
58 // A struct request may be represented by 1 or more struct blkvsc_request
59 struct blkvsc_request_group {
63 struct list_head blkvsc_req_list; // list of blkvsc_requests
67 struct blkvsc_request {
68 struct list_head req_entry; // blkvsc_request_group.blkvsc_req_list
70 struct list_head pend_entry; // block_device_context.pending_list
72 struct request *req; // This may be null if we generate a request internally
73 struct block_device_context *dev;
74 struct blkvsc_request_group *group; // The group this request is part of. Maybe null
76 wait_queue_head_t wevent;
80 sector_t sector_start;
81 unsigned long sector_count;
83 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
84 unsigned char cmd_len;
85 unsigned char cmnd[MAX_COMMAND_SIZE];
87 STORVSC_REQUEST request;
88 // !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap, because -
89 // The extension buffer falls right here and is pointed to by request.Extension;
92 // Per device structure
93 struct block_device_context {
94 struct device_context *device_ctx; // point back to our device context
95 struct kmem_cache *request_pool;
98 enum blkvsc_device_type device_type;
99 struct list_head pending_list;
101 unsigned char device_id[64];
102 unsigned int device_id_len;
103 int num_outstanding_reqs;
105 int media_not_present;
106 unsigned int sector_size;
110 unsigned char target;
115 struct blkvsc_driver_context {
116 // !! These must be the first 2 fields !!
117 struct driver_context drv_ctx;
118 STORVSC_DRIVER_OBJECT drv_obj;
122 static int blkvsc_probe(struct device *dev);
123 static int blkvsc_remove(struct device *device);
124 static void blkvsc_shutdown(struct device *device);
126 static int blkvsc_open(struct inode *inode, struct file *filep);
127 static int blkvsc_release(struct inode *inode, struct file *filep);
128 static int blkvsc_media_changed(struct gendisk *gd);
129 static int blkvsc_revalidate_disk(struct gendisk *gd);
130 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
131 static int blkvsc_ioctl(struct inode *inode, struct file *filep, unsigned cmd, unsigned long arg);
133 static void blkvsc_request(struct request_queue *queue);
134 static void blkvsc_request_completion(STORVSC_REQUEST* request);
135 static int blkvsc_do_request(struct block_device_context *blkdev, struct request *req);
136 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req, void (*request_completion)(STORVSC_REQUEST*) );
137 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
138 static void blkvsc_cmd_completion(STORVSC_REQUEST* request);
139 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
140 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
141 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
142 static int blkvsc_do_flush(struct block_device_context *blkdev);
143 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
144 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
147 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
149 // The one and only one
150 static struct blkvsc_driver_context g_blkvsc_drv;
153 static struct block_device_operations block_ops =
155 .owner = THIS_MODULE,
157 .release = blkvsc_release,
158 .media_changed = blkvsc_media_changed,
159 .revalidate_disk = blkvsc_revalidate_disk,
160 .getgeo = blkvsc_getgeo,
161 .ioctl = blkvsc_ioctl,
166 Name: blkvsc_drv_init()
168 Desc: BlkVsc driver initialization.
171 int blkvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
174 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_blkvsc_drv.drv_obj;
175 struct driver_context *drv_ctx=&g_blkvsc_drv.drv_ctx;
177 DPRINT_ENTER(BLKVSC_DRV);
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 pfn_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, sizeof(GUID));
189 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
190 drv_ctx->driver.probe = blkvsc_probe;
191 drv_ctx->driver.remove = blkvsc_remove;
193 drv_ctx->probe = blkvsc_probe;
194 drv_ctx->remove = blkvsc_remove;
195 drv_ctx->shutdown = blkvsc_shutdown;
198 // The driver belongs to vmbus
199 vmbus_child_driver_register(drv_ctx);
201 DPRINT_EXIT(BLKVSC_DRV);
207 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
209 struct device **curr = (struct device **)data;
211 return 1; // stop iterating
216 Name: blkvsc_drv_exit()
221 void blkvsc_drv_exit(void)
223 STORVSC_DRIVER_OBJECT *storvsc_drv_obj=&g_blkvsc_drv.drv_obj;
224 struct driver_context *drv_ctx=&g_blkvsc_drv.drv_ctx;
226 struct device *current_dev=NULL;
228 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
229 #define driver_for_each_device(drv, start, data, fn) \
230 struct list_head *ptr, *n; \
231 list_for_each_safe(ptr, n, &((drv)->devices)) {\
232 struct device *curr_dev;\
233 curr_dev = list_entry(ptr, struct device, driver_list);\
236 #endif // KERNEL_2_6_9
238 DPRINT_ENTER(BLKVSC_DRV);
245 driver_for_each_device(&drv_ctx->driver, NULL, (void*)¤t_dev, blkvsc_drv_exit_cb);
247 if (current_dev == NULL)
250 // Initiate removal from the top-down
251 device_unregister(current_dev);
254 if (storvsc_drv_obj->Base.OnCleanup)
255 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
257 vmbus_child_driver_unregister(drv_ctx);
259 DPRINT_EXIT(BLKVSC_DRV);
268 Desc: Add a new device for this driver
271 static int blkvsc_probe(struct device *device)
275 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
276 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
277 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
279 struct device_context *device_ctx = device_to_device_context(device);
280 DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
282 struct block_device_context *blkdev=NULL;
283 STORVSC_DEVICE_INFO device_info;
287 static int ide0_registered=0;
288 static int ide1_registered=0;
290 DPRINT_ENTER(BLKVSC_DRV);
292 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
294 if (!storvsc_drv_obj->Base.OnDeviceAdd)
296 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
302 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
309 INIT_LIST_HEAD(&blkdev->pending_list);
311 // Initialize what we can here
312 spin_lock_init(&blkdev->lock);
314 ASSERT(sizeof(struct blkvsc_request_group) <= sizeof(struct blkvsc_request));
317 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
318 sizeof(struct blkvsc_request) + storvsc_drv_obj->RequestExtSize, 0,
319 SLAB_HWCACHE_ALIGN, NULL);
321 blkdev->request_pool = kmem_cache_create(device_ctx->device.bus_id,
322 sizeof(struct blkvsc_request) + storvsc_drv_obj->RequestExtSize, 0,
323 SLAB_HWCACHE_ALIGN, NULL, NULL);
325 if (!blkdev->request_pool)
332 // Call to the vsc driver to add the device
333 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
336 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
340 blkdev->device_ctx = device_ctx;
341 blkdev->target = device_info.TargetId; // this identified the device 0 or 1
342 blkdev->path = device_info.PathId; // this identified the ide ctrl 0 or 1
344 dev_set_drvdata(device, blkdev);
346 // Calculate the major and device num
347 if (blkdev->path == 0)
350 devnum = blkdev->path + blkdev->target; // 0 or 1
352 if (!ide0_registered)
354 ret = register_blkdev(major, "ide");
357 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
364 else if (blkdev->path == 1)
367 devnum = blkdev->path + blkdev->target + 1; // 2 or 3
369 if (!ide1_registered)
371 ret = register_blkdev(major, "ide");
374 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
384 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
389 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
391 blkdev->gd = alloc_disk(BLKVSC_MINORS);
394 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
399 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
401 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
402 blk_queue_max_phys_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
403 blk_queue_max_hw_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
404 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
405 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
406 blk_queue_dma_alignment(blkdev->gd->queue, 511);
408 blkdev->gd->major = major;
409 if (devnum == 1 || devnum == 3)
410 blkdev->gd->first_minor = BLKVSC_MINORS;
412 blkdev->gd->first_minor = 0;
413 blkdev->gd->fops = &block_ops;
414 blkdev->gd->private_data = blkdev;
415 sprintf(blkdev->gd->disk_name, "hd%c", 'a'+ devnum);
417 blkvsc_do_inquiry(blkdev);
418 if (blkdev->device_type == DVD_TYPE)
420 set_disk_ro(blkdev->gd, 1);
421 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
422 blkvsc_do_read_capacity(blkdev);
426 blkvsc_do_read_capacity16(blkdev);
429 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
430 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
432 add_disk(blkdev->gd);
434 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %llu sector_size %d", blkdev->gd->disk_name, blkdev->capacity, blkdev->sector_size);
439 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
444 if (blkdev->request_pool)
446 kmem_cache_destroy(blkdev->request_pool);
447 blkdev->request_pool = NULL;
453 DPRINT_EXIT(BLKVSC_DRV);
458 static void blkvsc_shutdown(struct device *device)
460 struct block_device_context *blkdev = dev_get_drvdata(device);
466 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
468 spin_lock_irqsave(&blkdev->lock, flags);
470 blkdev->shutting_down = 1;
472 blk_stop_queue(blkdev->gd->queue);
474 spin_unlock_irqrestore(&blkdev->lock, flags);
476 while (blkdev->num_outstanding_reqs)
478 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", blkdev->num_outstanding_reqs);
483 blkvsc_do_flush(blkdev);
485 spin_lock_irqsave(&blkdev->lock, flags);
487 blkvsc_cancel_pending_reqs(blkdev);
489 spin_unlock_irqrestore(&blkdev->lock, flags);
492 static int blkvsc_do_flush(struct block_device_context *blkdev)
494 struct blkvsc_request *blkvsc_req=NULL;
496 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
498 if (blkdev->device_type != HARDDISK_TYPE)
501 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
507 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
508 init_waitqueue_head(&blkvsc_req->wevent);
509 blkvsc_req->dev = blkdev;
510 blkvsc_req->req = NULL;
511 blkvsc_req->write = 0;
513 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
514 blkvsc_req->request.DataBuffer.Offset = 0;
515 blkvsc_req->request.DataBuffer.Length = 0;
517 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
518 blkvsc_req->cmd_len = 10;
520 // Set this here since the completion routine may be invoked and completed before we return
522 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
524 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
526 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
531 // Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd)
532 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
534 struct blkvsc_request *blkvsc_req=NULL;
535 struct page *page_buf;
537 unsigned char device_type;
539 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
541 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
547 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
548 page_buf = alloc_page(GFP_KERNEL);
551 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
555 init_waitqueue_head(&blkvsc_req->wevent);
556 blkvsc_req->dev = blkdev;
557 blkvsc_req->req = NULL;
558 blkvsc_req->write = 0;
560 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
561 blkvsc_req->request.DataBuffer.Offset = 0;
562 blkvsc_req->request.DataBuffer.Length = 64;
564 blkvsc_req->cmnd[0] = INQUIRY;
565 blkvsc_req->cmnd[1] = 0x1; // Get product data
566 blkvsc_req->cmnd[2] = 0x83; // mode page 83
567 blkvsc_req->cmnd[4] = 64;
568 blkvsc_req->cmd_len = 6;
570 // Set this here since the completion routine may be invoked and completed before we return
573 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
575 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
577 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
579 buf = kmap(page_buf);
581 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
583 device_type = buf[0] & 0x1F;
585 if (device_type == 0x0)
587 blkdev->device_type = HARDDISK_TYPE;
589 else if (device_type == 0x5)
591 blkdev->device_type = DVD_TYPE;
595 // TODO: this is currently unsupported device type
596 blkdev->device_type = UNKNOWN_DEV_TYPE;
599 DPRINT_DBG(BLKVSC_DRV, "device type %d \n", device_type);
601 blkdev->device_id_len = buf[7];
602 if (blkdev->device_id_len > 64)
603 blkdev->device_id_len = 64;
605 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
606 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
607 * blkdev->device_id_len); */
611 __free_page(page_buf);
613 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
618 // Do a scsi READ_CAPACITY cmd here to get the size of the disk
619 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
621 struct blkvsc_request *blkvsc_req=NULL;
622 struct page *page_buf;
624 struct scsi_sense_hdr sense_hdr;
626 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
628 blkdev->sector_size = 0;
629 blkdev->capacity = 0;
630 blkdev->media_not_present = 0; // assume a disk is present
632 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
638 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
639 page_buf = alloc_page(GFP_KERNEL);
642 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
646 init_waitqueue_head(&blkvsc_req->wevent);
647 blkvsc_req->dev = blkdev;
648 blkvsc_req->req = NULL;
649 blkvsc_req->write = 0;
651 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
652 blkvsc_req->request.DataBuffer.Offset = 0;
653 blkvsc_req->request.DataBuffer.Length = 8;
655 blkvsc_req->cmnd[0] = READ_CAPACITY;
656 blkvsc_req->cmd_len = 16;
658 // Set this here since the completion routine may be invoked and completed before we return
661 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
663 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
665 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
668 if (blkvsc_req->request.Status)
670 scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
672 if (sense_hdr.asc == 0x3A) // Medium not present
674 blkdev->media_not_present = 1;
679 buf = kmap(page_buf);
682 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) + 1;
683 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
687 __free_page(page_buf);
689 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
695 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
697 struct blkvsc_request *blkvsc_req=NULL;
698 struct page *page_buf;
700 struct scsi_sense_hdr sense_hdr;
702 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
704 blkdev->sector_size = 0;
705 blkdev->capacity = 0;
706 blkdev->media_not_present = 0; // assume a disk is present
708 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
714 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
715 page_buf = alloc_page(GFP_KERNEL);
718 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
722 init_waitqueue_head(&blkvsc_req->wevent);
723 blkvsc_req->dev = blkdev;
724 blkvsc_req->req = NULL;
725 blkvsc_req->write = 0;
727 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
728 blkvsc_req->request.DataBuffer.Offset = 0;
729 blkvsc_req->request.DataBuffer.Length = 12;
731 blkvsc_req->cmnd[0] = 0x9E; //READ_CAPACITY16;
732 blkvsc_req->cmd_len = 16;
734 // Set this here since the completion routine may be invoked and completed before we return
737 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
739 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n", blkvsc_req, blkvsc_req->cond);
741 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
744 if (blkvsc_req->request.Status)
746 scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
748 if (sense_hdr.asc == 0x3A) // Medium not present
750 blkdev->media_not_present = 1;
755 buf = kmap(page_buf);
758 blkdev->capacity = be64_to_cpu(*(unsigned long long*) &buf[0]) + 1;
759 blkdev->sector_size = be32_to_cpu(*(unsigned int*)&buf[8]);
761 //blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) + 1;
762 //blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
766 __free_page(page_buf);
768 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
775 Name: blkvsc_remove()
777 Desc: Callback when our device is removed
780 static int blkvsc_remove(struct device *device)
784 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
785 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
786 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
788 struct device_context *device_ctx = device_to_device_context(device);
789 DEVICE_OBJECT* device_obj = &device_ctx->device_obj;
790 struct block_device_context *blkdev = dev_get_drvdata(device);
793 DPRINT_ENTER(BLKVSC_DRV);
795 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
797 if (!storvsc_drv_obj->Base.OnDeviceRemove)
799 DPRINT_EXIT(BLKVSC_DRV);
803 // Call to the vsc driver to let it know that the device is being removed
804 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
808 DPRINT_ERR(BLKVSC_DRV, "unable to remove blkvsc device (ret %d)", ret);
811 // Get to a known state
812 spin_lock_irqsave(&blkdev->lock, flags);
814 blkdev->shutting_down = 1;
816 blk_stop_queue(blkdev->gd->queue);
818 spin_unlock_irqrestore(&blkdev->lock, flags);
820 while (blkdev->num_outstanding_reqs)
822 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", blkdev->num_outstanding_reqs);
827 blkvsc_do_flush(blkdev);
829 spin_lock_irqsave(&blkdev->lock, flags);
831 blkvsc_cancel_pending_reqs(blkdev);
833 spin_unlock_irqrestore(&blkdev->lock, flags);
835 blk_cleanup_queue(blkdev->gd->queue);
837 del_gendisk(blkdev->gd);
839 kmem_cache_destroy(blkdev->request_pool);
843 DPRINT_EXIT(BLKVSC_DRV);
848 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
850 ASSERT(blkvsc_req->req);
851 ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8));
853 blkvsc_req->cmd_len = 16;
855 if (blkvsc_req->sector_start > 0xffffffff)
857 if (rq_data_dir(blkvsc_req->req))
859 blkvsc_req->write = 1;
860 blkvsc_req->cmnd[0] = WRITE_16;
864 blkvsc_req->write = 0;
865 blkvsc_req->cmnd[0] = READ_16;
868 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
870 *(unsigned long long*)&blkvsc_req->cmnd[2] = cpu_to_be64(blkvsc_req->sector_start);
871 *(unsigned int*)&blkvsc_req->cmnd[10] = cpu_to_be32(blkvsc_req->sector_count);
873 else if ((blkvsc_req->sector_count > 0xff) || (blkvsc_req->sector_start > 0x1fffff))
875 if (rq_data_dir(blkvsc_req->req))
877 blkvsc_req->write = 1;
878 blkvsc_req->cmnd[0] = WRITE_10;
882 blkvsc_req->write = 0;
883 blkvsc_req->cmnd[0] = READ_10;
886 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
888 *(unsigned int *)&blkvsc_req->cmnd[2] = cpu_to_be32(blkvsc_req->sector_start);
889 *(unsigned short*)&blkvsc_req->cmnd[7] = cpu_to_be16(blkvsc_req->sector_count);
893 if (rq_data_dir(blkvsc_req->req))
895 blkvsc_req->write = 1;
896 blkvsc_req->cmnd[0] = WRITE_6;
900 blkvsc_req->write = 0;
901 blkvsc_req->cmnd[0] = READ_6;
904 *(unsigned int *)&blkvsc_req->cmnd[1] = cpu_to_be32(blkvsc_req->sector_start) >> 8;
905 blkvsc_req->cmnd[1] &= 0x1f;
906 blkvsc_req->cmnd[4] = (unsigned char) blkvsc_req->sector_count;
910 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req, void (*request_completion)(STORVSC_REQUEST*) )
912 struct block_device_context *blkdev = blkvsc_req->dev;
913 struct device_context *device_ctx=blkdev->device_ctx;
914 struct driver_context *driver_ctx = driver_to_driver_context(device_ctx->device.driver);
915 struct blkvsc_driver_context *blkvsc_drv_ctx = (struct blkvsc_driver_context*)driver_ctx;
916 STORVSC_DRIVER_OBJECT* storvsc_drv_obj = &blkvsc_drv_ctx->drv_obj;
919 STORVSC_REQUEST *storvsc_req;
921 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - req %p type %s start_sector %llu count %d offset %d len %d\n",
923 (blkvsc_req->write)?"WRITE":"READ",
924 blkvsc_req->sector_start,
925 blkvsc_req->sector_count,
926 blkvsc_req->request.DataBuffer.Offset,
927 blkvsc_req->request.DataBuffer.Length);
929 /*for (i=0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++)
931 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - req %p pfn[%d] %llx\n",
934 blkvsc_req->request.DataBuffer.PfnArray[i]);
937 storvsc_req = &blkvsc_req->request;
938 storvsc_req->Extension = (void*)((unsigned long)blkvsc_req + sizeof(struct blkvsc_request));
940 storvsc_req->Type = blkvsc_req->write? WRITE_TYPE : READ_TYPE;
942 storvsc_req->OnIOCompletion = request_completion;
943 storvsc_req->Context = blkvsc_req;
945 storvsc_req->Host = blkdev->port;
946 storvsc_req->Bus = blkdev->path;
947 storvsc_req->TargetId = blkdev->target;
948 storvsc_req->LunId = 0; // this is not really used at all
950 storvsc_req->CdbLen = blkvsc_req->cmd_len;
951 storvsc_req->Cdb = blkvsc_req->cmnd;
953 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
954 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
956 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj, &blkvsc_req->request);
959 blkdev->num_outstanding_reqs++;
966 // We break the request into 1 or more blkvsc_requests and submit them.
967 // If we cant submit them all, we put them on the pending_list. The
968 // blkvsc_request() will work on the pending_list.
970 static int blkvsc_do_request(struct block_device_context *blkdev, struct request *req)
972 struct bio *bio=NULL;
973 struct bio_vec *bvec=NULL;
974 struct bio_vec *prev_bvec=NULL;
976 struct blkvsc_request *blkvsc_req=NULL;
977 struct blkvsc_request *tmp;
981 sector_t start_sector;
982 unsigned long num_sectors = 0;
985 struct blkvsc_request_group *group=NULL;
987 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %llu \n", blkdev, req, blk_rq_pos(req));
989 // Create a group to tie req to list of blkvsc_reqs
990 group = (struct blkvsc_request_group*)kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
996 INIT_LIST_HEAD(&group->blkvsc_req_list);
997 group->outstanding = group->status = 0;
999 start_sector = blk_rq_pos(req);
1001 // foreach bio in the request
1003 for (bio = req->bio; bio; bio = bio->bi_next)
1005 // Map this bio into an existing or new storvsc request
1006 bio_for_each_segment (bvec, bio, seg_idx)
1008 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() - req %p bio %p bvec %p seg_idx %d databuf_idx %d\n",
1009 req, bio, bvec, seg_idx, databuf_idx);
1011 // Get a new storvsc request
1012 if ( (!blkvsc_req) || // 1st-time
1013 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT) ||
1014 (bvec->bv_offset != 0) || // hole at the begin of page
1015 (prev_bvec && (prev_bvec->bv_len != PAGE_SIZE)) ) // hold at the end of page
1017 // submit the prev one
1020 blkvsc_req->sector_start = start_sector;
1021 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
1023 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
1025 blkvsc_init_rw(blkvsc_req);
1028 // Create new blkvsc_req to represent the current bvec
1029 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
1032 // free up everything
1033 list_for_each_entry_safe(blkvsc_req, tmp, &group->blkvsc_req_list, req_entry)
1035 list_del(&blkvsc_req->req_entry);
1036 kmem_cache_free(blkdev->request_pool, blkvsc_req);
1039 kmem_cache_free(blkdev->request_pool, group);
1043 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
1045 blkvsc_req->dev = blkdev;
1046 blkvsc_req->req = req;
1047 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1048 blkvsc_req->request.DataBuffer.Length = 0;
1051 blkvsc_req->group = group;
1052 blkvsc_req->group->outstanding++;
1053 list_add_tail(&blkvsc_req->req_entry, &blkvsc_req->group->blkvsc_req_list);
1055 start_sector += num_sectors;
1060 // Add the curr bvec/segment to the curr blkvsc_req
1061 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1062 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1067 num_sectors += bvec->bv_len >> 9;
1069 } // bio_for_each_segment
1071 } // rq_for_each_bio
1073 // Handle the last one
1076 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n", blkdev, req, blkvsc_req->group, blkvsc_req->group->outstanding);
1078 blkvsc_req->sector_start = start_sector;
1079 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
1081 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
1083 blkvsc_init_rw(blkvsc_req);
1086 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry)
1090 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to pending_list - blkvsc_req %p start_sect %llu sect_count %d (%llu %d)\n",
1091 blkvsc_req, blkvsc_req->sector_start, blkvsc_req->sector_count, start_sector, num_sectors);
1093 list_add_tail(&blkvsc_req->pend_entry, &blkdev->pending_list);
1097 ret = blkvsc_submit_request(blkvsc_req, blkvsc_request_completion);
1101 list_add_tail(&blkvsc_req->pend_entry, &blkdev->pending_list);
1104 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p start_sect %llu sect_count %d (%llu %d) ret %d\n",
1105 blkvsc_req, blkvsc_req->sector_start, blkvsc_req->sector_count, start_sector, num_sectors, ret);
1112 static void blkvsc_cmd_completion(STORVSC_REQUEST* request)
1114 struct blkvsc_request *blkvsc_req=(struct blkvsc_request*)request->Context;
1115 struct block_device_context *blkdev = (struct block_device_context*)blkvsc_req->dev;
1117 struct scsi_sense_hdr sense_hdr;
1119 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n", blkvsc_req);
1121 blkdev->num_outstanding_reqs--;
1123 if (blkvsc_req->request.Status)
1125 if (scsi_normalize_sense(blkvsc_req->sense_buffer, SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1127 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1131 blkvsc_req->cond =1;
1132 wake_up_interruptible(&blkvsc_req->wevent);
1135 static void blkvsc_request_completion(STORVSC_REQUEST* request)
1137 struct blkvsc_request *blkvsc_req=(struct blkvsc_request*)request->Context;
1138 struct block_device_context *blkdev = (struct block_device_context*)blkvsc_req->dev;
1139 unsigned long flags;
1140 struct blkvsc_request *comp_req, *tmp;
1142 ASSERT(blkvsc_req->group);
1144 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s sect_start %llu sect_count %d len %d group outstd %d total outstd %d\n",
1148 (blkvsc_req->write)?"WRITE":"READ",
1149 blkvsc_req->sector_start,
1150 blkvsc_req->sector_count,
1151 blkvsc_req->request.DataBuffer.Length,
1152 blkvsc_req->group->outstanding,
1153 blkdev->num_outstanding_reqs);
1155 spin_lock_irqsave(&blkdev->lock, flags);
1157 blkdev->num_outstanding_reqs--;
1158 blkvsc_req->group->outstanding--;
1160 // Only start processing when all the blkvsc_reqs are completed. This guarantees no out-of-order
1161 // blkvsc_req completion when calling end_that_request_first()
1162 if (blkvsc_req->group->outstanding == 0)
1164 list_for_each_entry_safe(comp_req, tmp, &blkvsc_req->group->blkvsc_req_list, req_entry)
1166 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p sect_start %llu sect_count %d \n",
1168 comp_req->sector_start,
1169 comp_req->sector_count);
1171 list_del(&comp_req->req_entry);
1173 #ifdef KERNEL_2_6_27
1174 if (!__blk_end_request(
1176 (!comp_req->request.Status ? 0: -EIO),
1177 comp_req->sector_count * blkdev->sector_size))
1179 //All the sectors have been xferred ie the request is done
1180 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n", comp_req->req);
1181 kmem_cache_free(blkdev->request_pool, comp_req->group);
1184 if (!end_that_request_first(comp_req->req, !comp_req->request.Status, (comp_req->sector_count * (blkdev->sector_size >> 9))))
1186 //All the sectors have been xferred ie the request is done
1187 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n", comp_req->req);
1189 end_that_request_last(comp_req->req, !comp_req->request.Status);
1191 kmem_cache_free(blkdev->request_pool, comp_req->group);
1195 kmem_cache_free(blkdev->request_pool, comp_req);
1198 if (!blkdev->shutting_down)
1200 blkvsc_do_pending_reqs(blkdev);
1201 blk_start_queue(blkdev->gd->queue);
1202 blkvsc_request(blkdev->gd->queue);
1206 spin_unlock_irqrestore(&blkdev->lock, flags);
1209 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1211 struct blkvsc_request *pend_req, *tmp;
1212 struct blkvsc_request *comp_req, *tmp2;
1216 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1218 // Flush the pending list first
1219 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list, pend_entry)
1221 // The pend_req could be part of a partially completed request. If so, complete those req first
1222 // until we hit the pend_req
1223 list_for_each_entry_safe(comp_req, tmp2, &pend_req->group->blkvsc_req_list, req_entry)
1225 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p sect_start %llu sect_count %d \n",
1227 comp_req->sector_start,
1228 comp_req->sector_count);
1230 if (comp_req == pend_req)
1233 list_del(&comp_req->req_entry);
1237 #ifdef KERNEL_2_6_27
1238 ret = __blk_end_request(
1240 (!comp_req->request.Status ? 0 : -EIO),
1241 comp_req->sector_count * blkdev->sector_size);
1243 ret = end_that_request_first(comp_req->req, !comp_req->request.Status, (comp_req->sector_count * (blkdev->sector_size >> 9)));
1248 kmem_cache_free(blkdev->request_pool, comp_req);
1251 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n", pend_req);
1253 list_del(&pend_req->pend_entry);
1255 list_del(&pend_req->req_entry);
1259 #ifdef KERNEL_2_6_27
1260 if (!__blk_end_request(
1263 pend_req->sector_count * blkdev->sector_size))
1265 //All the sectors have been xferred ie the request is done
1266 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs() - req %p COMPLETED\n", pend_req->req);
1267 kmem_cache_free(blkdev->request_pool, pend_req->group);
1270 if (!end_that_request_first(pend_req->req, 0, (pend_req->sector_count * (blkdev->sector_size >> 9))))
1272 //All the sectors have been xferred ie the request is done
1273 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs() - req %p COMPLETED\n", pend_req->req);
1275 end_that_request_last(pend_req->req, 0);
1277 kmem_cache_free(blkdev->request_pool, pend_req->group);
1282 kmem_cache_free(blkdev->request_pool, pend_req);
1288 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1290 struct blkvsc_request *pend_req, *tmp;
1293 // Flush the pending list first
1294 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list, pend_entry)
1296 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n", pend_req);
1298 ret = blkvsc_submit_request(pend_req, blkvsc_request_completion);
1305 list_del(&pend_req->pend_entry);
1312 static void blkvsc_request(struct request_queue *queue)
1314 struct block_device_context *blkdev = NULL;
1315 struct request *req;
1318 DPRINT_DBG(BLKVSC_DRV, "- enter \n");
1319 while ((req = blk_peek_request(queue)) != NULL)
1321 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1323 blkdev = req->rq_disk->private_data;
1324 if (blkdev->shutting_down || !blk_fs_request(req) || blkdev->media_not_present) {
1325 __blk_end_request_cur(req, 0);
1329 ret = blkvsc_do_pending_reqs(blkdev);
1333 DPRINT_DBG(BLKVSC_DRV, "- stop queue - pending_list not empty\n");
1334 blk_stop_queue(queue);
1338 blk_start_request(req);
1340 ret = blkvsc_do_request(blkdev, req);
1343 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1344 blk_stop_queue(queue);
1349 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1350 blk_requeue_request(queue, req);
1351 blk_stop_queue(queue);
1357 static int blkvsc_open(struct inode *inode, struct file *filep)
1359 struct block_device_context *blkdev = inode->i_bdev->bd_disk->private_data;
1361 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
1363 spin_lock(&blkdev->lock);
1365 if (!blkdev->users && blkdev->device_type == DVD_TYPE)
1367 spin_unlock(&blkdev->lock);
1368 check_disk_change(inode->i_bdev);
1369 spin_lock(&blkdev->lock);
1374 spin_unlock(&blkdev->lock);
1378 static int blkvsc_release(struct inode *inode, struct file *filep)
1380 struct block_device_context *blkdev = inode->i_bdev->bd_disk->private_data;
1382 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users, blkdev->gd->disk_name);
1384 spin_lock(&blkdev->lock);
1385 if (blkdev->users == 1)
1387 spin_unlock(&blkdev->lock);
1388 blkvsc_do_flush(blkdev);
1389 spin_lock(&blkdev->lock);
1394 spin_unlock(&blkdev->lock);
1398 static int blkvsc_media_changed(struct gendisk *gd)
1400 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1405 static int blkvsc_revalidate_disk(struct gendisk *gd)
1407 struct block_device_context *blkdev = gd->private_data;
1409 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1411 if (blkdev->device_type == DVD_TYPE)
1413 blkvsc_do_read_capacity(blkdev);
1414 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
1415 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1420 int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1422 sector_t total_sectors = get_capacity(bd->bd_disk);
1423 sector_t cylinder_times_heads=0;
1426 int sectors_per_track=0;
1431 if (total_sectors > (65535 * 16 * 255)) {
1432 total_sectors = (65535 * 16 * 255);
1435 if (total_sectors >= (65535 * 16 * 63)) {
1436 sectors_per_track = 255;
1439 cylinder_times_heads = total_sectors;
1440 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1444 sectors_per_track = 17;
1446 cylinder_times_heads = total_sectors;
1447 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1449 temp = cylinder_times_heads + 1023;
1450 rem = sector_div(temp, 1024); // sector_div stores the quotient in temp
1458 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1459 sectors_per_track = 31;
1462 cylinder_times_heads = total_sectors;
1463 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1466 if (cylinder_times_heads >= (heads * 1024)) {
1467 sectors_per_track = 63;
1470 cylinder_times_heads = total_sectors;
1471 rem = sector_div(cylinder_times_heads, sectors_per_track); // sector_div stores the quotient in cylinder_times_heads
1475 temp = cylinder_times_heads;
1476 rem = sector_div(temp, heads); // sector_div stores the quotient in temp
1480 hg->sectors = sectors_per_track;
1481 hg->cylinders = cylinders;
1483 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, sectors_per_track);
1488 static int blkvsc_ioctl(struct inode *inode, struct file *filep, unsigned cmd, unsigned long arg)
1490 struct block_device *bd = inode->i_bdev;
1491 struct block_device_context *blkdev = bd->bd_disk->private_data;
1496 // TODO: I think there is certain format for HDIO_GET_IDENTITY rather than just
1497 // a GUID. Commented it out for now.
1498 /*case HDIO_GET_IDENTITY:
1499 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1501 if (copy_to_user((void __user *)arg, blkdev->device_id, blkdev->device_id_len))
1516 MODULE_LICENSE("GPL");
1518 static int __init blkvsc_init(void)
1522 ASSERT(sizeof(sector_t) == 8); // Make sure CONFIG_LBD is set
1524 DPRINT_ENTER(BLKVSC_DRV);
1526 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1528 ret = blkvsc_drv_init(BlkVscInitialize);
1530 DPRINT_EXIT(BLKVSC_DRV);
1535 static void __exit blkvsc_exit(void)
1537 DPRINT_ENTER(BLKVSC_DRV);
1541 DPRINT_ENTER(BLKVSC_DRV);
1544 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
1546 module_init(blkvsc_init);
1547 module_exit(blkvsc_exit);