]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/hv/blkvsc_drv.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / hv / blkvsc_drv.c
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
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/slab.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_dbg.h>
33 #include "osd.h"
34 #include "logging.h"
35 #include "VersionInfo.h"
36 #include "vmbus.h"
37 #include "StorVscApi.h"
38
39
40 #define BLKVSC_MINORS 64
41
42 enum blkvsc_device_type {
43 UNKNOWN_DEV_TYPE,
44 HARDDISK_TYPE,
45 DVD_TYPE,
46 };
47
48 /*
49 * This request ties the struct request and struct
50 * blkvsc_request/hv_storvsc_request together A struct request may be
51 * represented by 1 or more struct blkvsc_request
52 */
53 struct blkvsc_request_group {
54 int outstanding;
55 int status;
56 struct list_head blkvsc_req_list; /* list of blkvsc_requests */
57 };
58
59 struct blkvsc_request {
60 /* blkvsc_request_group.blkvsc_req_list */
61 struct list_head req_entry;
62
63 /* block_device_context.pending_list */
64 struct list_head pend_entry;
65
66 /* This may be null if we generate a request internally */
67 struct request *req;
68
69 struct block_device_context *dev;
70
71 /* The group this request is part of. Maybe null */
72 struct blkvsc_request_group *group;
73
74 wait_queue_head_t wevent;
75 int cond;
76
77 int write;
78 sector_t sector_start;
79 unsigned long sector_count;
80
81 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
82 unsigned char cmd_len;
83 unsigned char cmnd[MAX_COMMAND_SIZE];
84
85 struct hv_storvsc_request request;
86 /*
87 * !!!DO NOT ADD ANYTHING BELOW HERE!!! Otherwise, memory can overlap,
88 * because - The extension buffer falls right here and is pointed to by
89 * request.Extension;
90 * Which sounds like a horrible idea, who designed this?
91 */
92 };
93
94 /* Per device structure */
95 struct block_device_context {
96 /* point back to our device context */
97 struct vm_device *device_ctx;
98 struct kmem_cache *request_pool;
99 spinlock_t lock;
100 struct gendisk *gd;
101 enum blkvsc_device_type device_type;
102 struct list_head pending_list;
103
104 unsigned char device_id[64];
105 unsigned int device_id_len;
106 int num_outstanding_reqs;
107 int shutting_down;
108 int media_not_present;
109 unsigned int sector_size;
110 sector_t capacity;
111 unsigned int port;
112 unsigned char path;
113 unsigned char target;
114 int users;
115 };
116
117 /* Per driver */
118 struct blkvsc_driver_context {
119 /* !! These must be the first 2 fields !! */
120 /* FIXME this is a bug! */
121 struct driver_context drv_ctx;
122 struct storvsc_driver_object drv_obj;
123 };
124
125 /* Static decl */
126 static int blkvsc_probe(struct device *dev);
127 static int blkvsc_remove(struct device *device);
128 static void blkvsc_shutdown(struct device *device);
129
130 static int blkvsc_open(struct block_device *bdev, fmode_t mode);
131 static int blkvsc_release(struct gendisk *disk, fmode_t mode);
132 static int blkvsc_media_changed(struct gendisk *gd);
133 static int blkvsc_revalidate_disk(struct gendisk *gd);
134 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
135 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
136 unsigned cmd, unsigned long argument);
137 static void blkvsc_request(struct request_queue *queue);
138 static void blkvsc_request_completion(struct hv_storvsc_request *request);
139 static int blkvsc_do_request(struct block_device_context *blkdev,
140 struct request *req);
141 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
142 void (*request_completion)(struct hv_storvsc_request *));
143 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
144 static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
145 static int blkvsc_do_inquiry(struct block_device_context *blkdev);
146 static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
147 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
148 static int blkvsc_do_flush(struct block_device_context *blkdev);
149 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
150 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
151
152
153 static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
154
155 /* The one and only one */
156 static struct blkvsc_driver_context g_blkvsc_drv;
157
158 static struct block_device_operations block_ops = {
159 .owner = THIS_MODULE,
160 .open = blkvsc_open,
161 .release = blkvsc_release,
162 .media_changed = blkvsc_media_changed,
163 .revalidate_disk = blkvsc_revalidate_disk,
164 .getgeo = blkvsc_getgeo,
165 .ioctl = blkvsc_ioctl,
166 };
167
168 /**
169 * blkvsc_drv_init - BlkVsc driver initialization.
170 */
171 static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
172 {
173 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
174 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
175 int ret;
176
177 DPRINT_ENTER(BLKVSC_DRV);
178
179 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
180
181 storvsc_drv_obj->RingBufferSize = blkvsc_ringbuffer_size;
182
183 /* Callback to client driver to complete the initialization */
184 drv_init(&storvsc_drv_obj->Base);
185
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));
189
190 drv_ctx->probe = blkvsc_probe;
191 drv_ctx->remove = blkvsc_remove;
192 drv_ctx->shutdown = blkvsc_shutdown;
193
194 /* The driver belongs to vmbus */
195 ret = vmbus_child_driver_register(drv_ctx);
196
197 DPRINT_EXIT(BLKVSC_DRV);
198
199 return ret;
200 }
201
202 static int blkvsc_drv_exit_cb(struct device *dev, void *data)
203 {
204 struct device **curr = (struct device **)data;
205 *curr = dev;
206 return 1; /* stop iterating */
207 }
208
209 static void blkvsc_drv_exit(void)
210 {
211 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
212 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
213 struct device *current_dev;
214 int ret;
215
216 DPRINT_ENTER(BLKVSC_DRV);
217
218 while (1) {
219 current_dev = NULL;
220
221 /* Get the device */
222 ret = driver_for_each_device(&drv_ctx->driver, NULL,
223 (void *) &current_dev,
224 blkvsc_drv_exit_cb);
225
226 if (ret)
227 DPRINT_WARN(BLKVSC_DRV,
228 "driver_for_each_device returned %d", ret);
229
230
231 if (current_dev == NULL)
232 break;
233
234 /* Initiate removal from the top-down */
235 device_unregister(current_dev);
236 }
237
238 if (storvsc_drv_obj->Base.OnCleanup)
239 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
240
241 vmbus_child_driver_unregister(drv_ctx);
242
243 DPRINT_EXIT(BLKVSC_DRV);
244
245 return;
246 }
247
248 /**
249 * blkvsc_probe - Add a new device for this driver
250 */
251 static int blkvsc_probe(struct device *device)
252 {
253 struct driver_context *driver_ctx =
254 driver_to_driver_context(device->driver);
255 struct blkvsc_driver_context *blkvsc_drv_ctx =
256 (struct blkvsc_driver_context *)driver_ctx;
257 struct storvsc_driver_object *storvsc_drv_obj =
258 &blkvsc_drv_ctx->drv_obj;
259 struct vm_device *device_ctx = device_to_vm_device(device);
260 struct hv_device *device_obj = &device_ctx->device_obj;
261
262 struct block_device_context *blkdev = NULL;
263 struct storvsc_device_info device_info;
264 int major = 0;
265 int devnum = 0;
266 int ret = 0;
267 static int ide0_registered;
268 static int ide1_registered;
269
270 DPRINT_ENTER(BLKVSC_DRV);
271
272 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
273
274 if (!storvsc_drv_obj->Base.OnDeviceAdd) {
275 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
276 ret = -1;
277 goto Cleanup;
278 }
279
280 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
281 if (!blkdev) {
282 ret = -ENOMEM;
283 goto Cleanup;
284 }
285
286 INIT_LIST_HEAD(&blkdev->pending_list);
287
288 /* Initialize what we can here */
289 spin_lock_init(&blkdev->lock);
290
291 ASSERT(sizeof(struct blkvsc_request_group) <=
292 sizeof(struct blkvsc_request));
293
294 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
295 sizeof(struct blkvsc_request) +
296 storvsc_drv_obj->RequestExtSize, 0,
297 SLAB_HWCACHE_ALIGN, NULL);
298 if (!blkdev->request_pool) {
299 ret = -ENOMEM;
300 goto Cleanup;
301 }
302
303
304 /* Call to the vsc driver to add the device */
305 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
306 if (ret != 0) {
307 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
308 goto Cleanup;
309 }
310
311 blkdev->device_ctx = device_ctx;
312 /* this identified the device 0 or 1 */
313 blkdev->target = device_info.TargetId;
314 /* this identified the ide ctrl 0 or 1 */
315 blkdev->path = device_info.PathId;
316
317 dev_set_drvdata(device, blkdev);
318
319 /* Calculate the major and device num */
320 if (blkdev->path == 0) {
321 major = IDE0_MAJOR;
322 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
323
324 if (!ide0_registered) {
325 ret = register_blkdev(major, "ide");
326 if (ret != 0) {
327 DPRINT_ERR(BLKVSC_DRV,
328 "register_blkdev() failed! ret %d",
329 ret);
330 goto Remove;
331 }
332
333 ide0_registered = 1;
334 }
335 } else if (blkdev->path == 1) {
336 major = IDE1_MAJOR;
337 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
338
339 if (!ide1_registered) {
340 ret = register_blkdev(major, "ide");
341 if (ret != 0) {
342 DPRINT_ERR(BLKVSC_DRV,
343 "register_blkdev() failed! ret %d",
344 ret);
345 goto Remove;
346 }
347
348 ide1_registered = 1;
349 }
350 } else {
351 DPRINT_ERR(BLKVSC_DRV, "invalid pathid");
352 ret = -1;
353 goto Cleanup;
354 }
355
356 DPRINT_INFO(BLKVSC_DRV, "blkvsc registered for major %d!!", major);
357
358 blkdev->gd = alloc_disk(BLKVSC_MINORS);
359 if (!blkdev->gd) {
360 DPRINT_ERR(BLKVSC_DRV, "register_blkdev() failed! ret %d", ret);
361 ret = -1;
362 goto Cleanup;
363 }
364
365 blkdev->gd->queue = blk_init_queue(blkvsc_request, &blkdev->lock);
366
367 blk_queue_max_segment_size(blkdev->gd->queue, PAGE_SIZE);
368 blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
369 blk_queue_segment_boundary(blkdev->gd->queue, PAGE_SIZE-1);
370 blk_queue_bounce_limit(blkdev->gd->queue, BLK_BOUNCE_ANY);
371 blk_queue_dma_alignment(blkdev->gd->queue, 511);
372
373 blkdev->gd->major = major;
374 if (devnum == 1 || devnum == 3)
375 blkdev->gd->first_minor = BLKVSC_MINORS;
376 else
377 blkdev->gd->first_minor = 0;
378 blkdev->gd->fops = &block_ops;
379 blkdev->gd->private_data = blkdev;
380 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
381
382 blkvsc_do_inquiry(blkdev);
383 if (blkdev->device_type == DVD_TYPE) {
384 set_disk_ro(blkdev->gd, 1);
385 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
386 blkvsc_do_read_capacity(blkdev);
387 } else {
388 blkvsc_do_read_capacity16(blkdev);
389 }
390
391 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
392 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
393 /* go! */
394 add_disk(blkdev->gd);
395
396 DPRINT_INFO(BLKVSC_DRV, "%s added!! capacity %lu sector_size %d",
397 blkdev->gd->disk_name, (unsigned long)blkdev->capacity,
398 blkdev->sector_size);
399
400 return ret;
401
402 Remove:
403 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
404
405 Cleanup:
406 if (blkdev) {
407 if (blkdev->request_pool) {
408 kmem_cache_destroy(blkdev->request_pool);
409 blkdev->request_pool = NULL;
410 }
411 kfree(blkdev);
412 blkdev = NULL;
413 }
414
415 DPRINT_EXIT(BLKVSC_DRV);
416
417 return ret;
418 }
419
420 static void blkvsc_shutdown(struct device *device)
421 {
422 struct block_device_context *blkdev = dev_get_drvdata(device);
423 unsigned long flags;
424
425 if (!blkdev)
426 return;
427
428 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
429 blkdev->users, blkdev->gd->disk_name);
430
431 spin_lock_irqsave(&blkdev->lock, flags);
432
433 blkdev->shutting_down = 1;
434
435 blk_stop_queue(blkdev->gd->queue);
436
437 spin_unlock_irqrestore(&blkdev->lock, flags);
438
439 while (blkdev->num_outstanding_reqs) {
440 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
441 blkdev->num_outstanding_reqs);
442 udelay(100);
443 }
444
445 blkvsc_do_flush(blkdev);
446
447 spin_lock_irqsave(&blkdev->lock, flags);
448
449 blkvsc_cancel_pending_reqs(blkdev);
450
451 spin_unlock_irqrestore(&blkdev->lock, flags);
452 }
453
454 static int blkvsc_do_flush(struct block_device_context *blkdev)
455 {
456 struct blkvsc_request *blkvsc_req;
457
458 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_flush()\n");
459
460 if (blkdev->device_type != HARDDISK_TYPE)
461 return 0;
462
463 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
464 if (!blkvsc_req)
465 return -ENOMEM;
466
467 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
468 init_waitqueue_head(&blkvsc_req->wevent);
469 blkvsc_req->dev = blkdev;
470 blkvsc_req->req = NULL;
471 blkvsc_req->write = 0;
472
473 blkvsc_req->request.DataBuffer.PfnArray[0] = 0;
474 blkvsc_req->request.DataBuffer.Offset = 0;
475 blkvsc_req->request.DataBuffer.Length = 0;
476
477 blkvsc_req->cmnd[0] = SYNCHRONIZE_CACHE;
478 blkvsc_req->cmd_len = 10;
479
480 /*
481 * Set this here since the completion routine may be invoked and
482 * completed before we return
483 */
484 blkvsc_req->cond = 0;
485 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
486
487 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
488
489 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
490
491 return 0;
492 }
493
494 /* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
495 static int blkvsc_do_inquiry(struct block_device_context *blkdev)
496 {
497 struct blkvsc_request *blkvsc_req;
498 struct page *page_buf;
499 unsigned char *buf;
500 unsigned char device_type;
501
502 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_inquiry()\n");
503
504 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
505 if (!blkvsc_req)
506 return -ENOMEM;
507
508 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
509 page_buf = alloc_page(GFP_KERNEL);
510 if (!page_buf) {
511 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
512 return -ENOMEM;
513 }
514
515 init_waitqueue_head(&blkvsc_req->wevent);
516 blkvsc_req->dev = blkdev;
517 blkvsc_req->req = NULL;
518 blkvsc_req->write = 0;
519
520 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
521 blkvsc_req->request.DataBuffer.Offset = 0;
522 blkvsc_req->request.DataBuffer.Length = 64;
523
524 blkvsc_req->cmnd[0] = INQUIRY;
525 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
526 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
527 blkvsc_req->cmnd[4] = 64;
528 blkvsc_req->cmd_len = 6;
529
530 /*
531 * Set this here since the completion routine may be invoked and
532 * completed before we return
533 */
534 blkvsc_req->cond = 0;
535
536 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
537
538 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
539 blkvsc_req, blkvsc_req->cond);
540
541 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
542
543 buf = kmap(page_buf);
544
545 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
546 /* be to le */
547 device_type = buf[0] & 0x1F;
548
549 if (device_type == 0x0) {
550 blkdev->device_type = HARDDISK_TYPE;
551 } else if (device_type == 0x5) {
552 blkdev->device_type = DVD_TYPE;
553 } else {
554 /* TODO: this is currently unsupported device type */
555 blkdev->device_type = UNKNOWN_DEV_TYPE;
556 }
557
558 DPRINT_DBG(BLKVSC_DRV, "device type %d \n", device_type);
559
560 blkdev->device_id_len = buf[7];
561 if (blkdev->device_id_len > 64)
562 blkdev->device_id_len = 64;
563
564 memcpy(blkdev->device_id, &buf[8], blkdev->device_id_len);
565 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
566 * blkdev->device_id_len); */
567
568 kunmap(page_buf);
569
570 __free_page(page_buf);
571
572 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
573
574 return 0;
575 }
576
577 /* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
578 static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
579 {
580 struct blkvsc_request *blkvsc_req;
581 struct page *page_buf;
582 unsigned char *buf;
583 struct scsi_sense_hdr sense_hdr;
584
585 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity()\n");
586
587 blkdev->sector_size = 0;
588 blkdev->capacity = 0;
589 blkdev->media_not_present = 0; /* assume a disk is present */
590
591 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
592 if (!blkvsc_req)
593 return -ENOMEM;
594
595 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
596 page_buf = alloc_page(GFP_KERNEL);
597 if (!page_buf) {
598 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
599 return -ENOMEM;
600 }
601
602 init_waitqueue_head(&blkvsc_req->wevent);
603 blkvsc_req->dev = blkdev;
604 blkvsc_req->req = NULL;
605 blkvsc_req->write = 0;
606
607 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
608 blkvsc_req->request.DataBuffer.Offset = 0;
609 blkvsc_req->request.DataBuffer.Length = 8;
610
611 blkvsc_req->cmnd[0] = READ_CAPACITY;
612 blkvsc_req->cmd_len = 16;
613
614 /*
615 * Set this here since the completion routine may be invoked
616 * and completed before we return
617 */
618 blkvsc_req->cond = 0;
619
620 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
621
622 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
623 blkvsc_req, blkvsc_req->cond);
624
625 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
626
627 /* check error */
628 if (blkvsc_req->request.Status) {
629 scsi_normalize_sense(blkvsc_req->sense_buffer,
630 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
631
632 if (sense_hdr.asc == 0x3A) {
633 /* Medium not present */
634 blkdev->media_not_present = 1;
635 }
636 return 0;
637 }
638 buf = kmap(page_buf);
639
640 /* be to le */
641 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
642 (buf[2] << 8) | buf[3]) + 1;
643 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
644 (buf[6] << 8) | buf[7];
645
646 kunmap(page_buf);
647
648 __free_page(page_buf);
649
650 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
651
652 return 0;
653 }
654
655 static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
656 {
657 struct blkvsc_request *blkvsc_req;
658 struct page *page_buf;
659 unsigned char *buf;
660 struct scsi_sense_hdr sense_hdr;
661
662 DPRINT_DBG(BLKVSC_DRV, "blkvsc_do_read_capacity16()\n");
663
664 blkdev->sector_size = 0;
665 blkdev->capacity = 0;
666 blkdev->media_not_present = 0; /* assume a disk is present */
667
668 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
669 if (!blkvsc_req)
670 return -ENOMEM;
671
672 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
673 page_buf = alloc_page(GFP_KERNEL);
674 if (!page_buf) {
675 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
676 return -ENOMEM;
677 }
678
679 init_waitqueue_head(&blkvsc_req->wevent);
680 blkvsc_req->dev = blkdev;
681 blkvsc_req->req = NULL;
682 blkvsc_req->write = 0;
683
684 blkvsc_req->request.DataBuffer.PfnArray[0] = page_to_pfn(page_buf);
685 blkvsc_req->request.DataBuffer.Offset = 0;
686 blkvsc_req->request.DataBuffer.Length = 12;
687
688 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
689 blkvsc_req->cmd_len = 16;
690
691 /*
692 * Set this here since the completion routine may be invoked
693 * and completed before we return
694 */
695 blkvsc_req->cond = 0;
696
697 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
698
699 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
700 blkvsc_req, blkvsc_req->cond);
701
702 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
703
704 /* check error */
705 if (blkvsc_req->request.Status) {
706 scsi_normalize_sense(blkvsc_req->sense_buffer,
707 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
708 if (sense_hdr.asc == 0x3A) {
709 /* Medium not present */
710 blkdev->media_not_present = 1;
711 }
712 return 0;
713 }
714 buf = kmap(page_buf);
715
716 /* be to le */
717 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
718 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
719
720 #if 0
721 blkdev->capacity = ((buf[0] << 24) | (buf[1] << 16) |
722 (buf[2] << 8) | buf[3]) + 1;
723 blkdev->sector_size = (buf[4] << 24) | (buf[5] << 16) |
724 (buf[6] << 8) | buf[7];
725 #endif
726
727 kunmap(page_buf);
728
729 __free_page(page_buf);
730
731 kmem_cache_free(blkvsc_req->dev->request_pool, blkvsc_req);
732
733 return 0;
734 }
735
736 /**
737 * blkvsc_remove() - Callback when our device is removed
738 */
739 static int blkvsc_remove(struct device *device)
740 {
741 struct driver_context *driver_ctx =
742 driver_to_driver_context(device->driver);
743 struct blkvsc_driver_context *blkvsc_drv_ctx =
744 (struct blkvsc_driver_context *)driver_ctx;
745 struct storvsc_driver_object *storvsc_drv_obj =
746 &blkvsc_drv_ctx->drv_obj;
747 struct vm_device *device_ctx = device_to_vm_device(device);
748 struct hv_device *device_obj = &device_ctx->device_obj;
749 struct block_device_context *blkdev = dev_get_drvdata(device);
750 unsigned long flags;
751 int ret;
752
753 DPRINT_ENTER(BLKVSC_DRV);
754
755 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
756
757 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
758 DPRINT_EXIT(BLKVSC_DRV);
759 return -1;
760 }
761
762 /*
763 * Call to the vsc driver to let it know that the device is being
764 * removed
765 */
766 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
767 if (ret != 0) {
768 /* TODO: */
769 DPRINT_ERR(BLKVSC_DRV,
770 "unable to remove blkvsc device (ret %d)", ret);
771 }
772
773 /* Get to a known state */
774 spin_lock_irqsave(&blkdev->lock, flags);
775
776 blkdev->shutting_down = 1;
777
778 blk_stop_queue(blkdev->gd->queue);
779
780 spin_unlock_irqrestore(&blkdev->lock, flags);
781
782 while (blkdev->num_outstanding_reqs) {
783 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
784 blkdev->num_outstanding_reqs);
785 udelay(100);
786 }
787
788 blkvsc_do_flush(blkdev);
789
790 spin_lock_irqsave(&blkdev->lock, flags);
791
792 blkvsc_cancel_pending_reqs(blkdev);
793
794 spin_unlock_irqrestore(&blkdev->lock, flags);
795
796 blk_cleanup_queue(blkdev->gd->queue);
797
798 del_gendisk(blkdev->gd);
799
800 kmem_cache_destroy(blkdev->request_pool);
801
802 kfree(blkdev);
803
804 DPRINT_EXIT(BLKVSC_DRV);
805
806 return ret;
807 }
808
809 static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req)
810 {
811 ASSERT(blkvsc_req->req);
812 ASSERT(blkvsc_req->sector_count <= (MAX_MULTIPAGE_BUFFER_COUNT*8));
813
814 blkvsc_req->cmd_len = 16;
815
816 if (blkvsc_req->sector_start > 0xffffffff) {
817 if (rq_data_dir(blkvsc_req->req)) {
818 blkvsc_req->write = 1;
819 blkvsc_req->cmnd[0] = WRITE_16;
820 } else {
821 blkvsc_req->write = 0;
822 blkvsc_req->cmnd[0] = READ_16;
823 }
824
825 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
826
827 *(unsigned long long *)&blkvsc_req->cmnd[2] =
828 cpu_to_be64(blkvsc_req->sector_start);
829 *(unsigned int *)&blkvsc_req->cmnd[10] =
830 cpu_to_be32(blkvsc_req->sector_count);
831 } else if ((blkvsc_req->sector_count > 0xff) ||
832 (blkvsc_req->sector_start > 0x1fffff)) {
833 if (rq_data_dir(blkvsc_req->req)) {
834 blkvsc_req->write = 1;
835 blkvsc_req->cmnd[0] = WRITE_10;
836 } else {
837 blkvsc_req->write = 0;
838 blkvsc_req->cmnd[0] = READ_10;
839 }
840
841 blkvsc_req->cmnd[1] |= blk_fua_rq(blkvsc_req->req) ? 0x8 : 0;
842
843 *(unsigned int *)&blkvsc_req->cmnd[2] =
844 cpu_to_be32(blkvsc_req->sector_start);
845 *(unsigned short *)&blkvsc_req->cmnd[7] =
846 cpu_to_be16(blkvsc_req->sector_count);
847 } else {
848 if (rq_data_dir(blkvsc_req->req)) {
849 blkvsc_req->write = 1;
850 blkvsc_req->cmnd[0] = WRITE_6;
851 } else {
852 blkvsc_req->write = 0;
853 blkvsc_req->cmnd[0] = READ_6;
854 }
855
856 *(unsigned int *)&blkvsc_req->cmnd[1] =
857 cpu_to_be32(blkvsc_req->sector_start) >> 8;
858 blkvsc_req->cmnd[1] &= 0x1f;
859 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
860 }
861 }
862
863 static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
864 void (*request_completion)(struct hv_storvsc_request *))
865 {
866 struct block_device_context *blkdev = blkvsc_req->dev;
867 struct vm_device *device_ctx = blkdev->device_ctx;
868 struct driver_context *driver_ctx =
869 driver_to_driver_context(device_ctx->device.driver);
870 struct blkvsc_driver_context *blkvsc_drv_ctx =
871 (struct blkvsc_driver_context *)driver_ctx;
872 struct storvsc_driver_object *storvsc_drv_obj =
873 &blkvsc_drv_ctx->drv_obj;
874 struct hv_storvsc_request *storvsc_req;
875 int ret;
876
877 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
878 "req %p type %s start_sector %lu count %ld offset %d "
879 "len %d\n", blkvsc_req,
880 (blkvsc_req->write) ? "WRITE" : "READ",
881 (unsigned long) blkvsc_req->sector_start,
882 blkvsc_req->sector_count,
883 blkvsc_req->request.DataBuffer.Offset,
884 blkvsc_req->request.DataBuffer.Length);
885 #if 0
886 for (i = 0; i < (blkvsc_req->request.DataBuffer.Length >> 12); i++) {
887 DPRINT_DBG(BLKVSC_DRV, "blkvsc_submit_request() - "
888 "req %p pfn[%d] %llx\n",
889 blkvsc_req, i,
890 blkvsc_req->request.DataBuffer.PfnArray[i]);
891 }
892 #endif
893
894 storvsc_req = &blkvsc_req->request;
895 storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
896 sizeof(struct blkvsc_request));
897
898 storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
899
900 storvsc_req->OnIOCompletion = request_completion;
901 storvsc_req->Context = blkvsc_req;
902
903 storvsc_req->Host = blkdev->port;
904 storvsc_req->Bus = blkdev->path;
905 storvsc_req->TargetId = blkdev->target;
906 storvsc_req->LunId = 0; /* this is not really used at all */
907
908 storvsc_req->CdbLen = blkvsc_req->cmd_len;
909 storvsc_req->Cdb = blkvsc_req->cmnd;
910
911 storvsc_req->SenseBuffer = blkvsc_req->sense_buffer;
912 storvsc_req->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
913
914 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
915 &blkvsc_req->request);
916 if (ret == 0)
917 blkdev->num_outstanding_reqs++;
918
919 return ret;
920 }
921
922 /*
923 * We break the request into 1 or more blkvsc_requests and submit
924 * them. If we cant submit them all, we put them on the
925 * pending_list. The blkvsc_request() will work on the pending_list.
926 */
927 static int blkvsc_do_request(struct block_device_context *blkdev,
928 struct request *req)
929 {
930 struct bio *bio = NULL;
931 struct bio_vec *bvec = NULL;
932 struct bio_vec *prev_bvec = NULL;
933 struct blkvsc_request *blkvsc_req = NULL;
934 struct blkvsc_request *tmp;
935 int databuf_idx = 0;
936 int seg_idx = 0;
937 sector_t start_sector;
938 unsigned long num_sectors = 0;
939 int ret = 0;
940 int pending = 0;
941 struct blkvsc_request_group *group = NULL;
942
943 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu \n", blkdev, req,
944 (unsigned long)blk_rq_pos(req));
945
946 /* Create a group to tie req to list of blkvsc_reqs */
947 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
948 if (!group)
949 return -ENOMEM;
950
951 INIT_LIST_HEAD(&group->blkvsc_req_list);
952 group->outstanding = group->status = 0;
953
954 start_sector = blk_rq_pos(req);
955
956 /* foreach bio in the request */
957 if (req->bio) {
958 for (bio = req->bio; bio; bio = bio->bi_next) {
959 /*
960 * Map this bio into an existing or new storvsc request
961 */
962 bio_for_each_segment(bvec, bio, seg_idx) {
963 DPRINT_DBG(BLKVSC_DRV, "bio_for_each_segment() "
964 "- req %p bio %p bvec %p seg_idx %d "
965 "databuf_idx %d\n", req, bio, bvec,
966 seg_idx, databuf_idx);
967
968 /* Get a new storvsc request */
969 /* 1st-time */
970 if ((!blkvsc_req) ||
971 (databuf_idx >= MAX_MULTIPAGE_BUFFER_COUNT)
972 /* hole at the begin of page */
973 || (bvec->bv_offset != 0) ||
974 /* hold at the end of page */
975 (prev_bvec &&
976 (prev_bvec->bv_len != PAGE_SIZE))) {
977 /* submit the prev one */
978 if (blkvsc_req) {
979 blkvsc_req->sector_start = start_sector;
980 sector_div(blkvsc_req->sector_start, (blkdev->sector_size >> 9));
981
982 blkvsc_req->sector_count = num_sectors / (blkdev->sector_size >> 9);
983 blkvsc_init_rw(blkvsc_req);
984 }
985
986 /*
987 * Create new blkvsc_req to represent
988 * the current bvec
989 */
990 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
991 if (!blkvsc_req) {
992 /* free up everything */
993 list_for_each_entry_safe(
994 blkvsc_req, tmp,
995 &group->blkvsc_req_list,
996 req_entry) {
997 list_del(&blkvsc_req->req_entry);
998 kmem_cache_free(blkdev->request_pool, blkvsc_req);
999 }
1000
1001 kmem_cache_free(blkdev->request_pool, group);
1002 return -ENOMEM;
1003 }
1004
1005 memset(blkvsc_req, 0,
1006 sizeof(struct blkvsc_request));
1007
1008 blkvsc_req->dev = blkdev;
1009 blkvsc_req->req = req;
1010 blkvsc_req->request.DataBuffer.Offset = bvec->bv_offset;
1011 blkvsc_req->request.DataBuffer.Length = 0;
1012
1013 /* Add to the group */
1014 blkvsc_req->group = group;
1015 blkvsc_req->group->outstanding++;
1016 list_add_tail(&blkvsc_req->req_entry,
1017 &blkvsc_req->group->blkvsc_req_list);
1018
1019 start_sector += num_sectors;
1020 num_sectors = 0;
1021 databuf_idx = 0;
1022 }
1023
1024 /* Add the curr bvec/segment to the curr blkvsc_req */
1025 blkvsc_req->request.DataBuffer.PfnArray[databuf_idx] = page_to_pfn(bvec->bv_page);
1026 blkvsc_req->request.DataBuffer.Length += bvec->bv_len;
1027
1028 prev_bvec = bvec;
1029
1030 databuf_idx++;
1031 num_sectors += bvec->bv_len >> 9;
1032
1033 } /* bio_for_each_segment */
1034
1035 } /* rq_for_each_bio */
1036 }
1037
1038 /* Handle the last one */
1039 if (blkvsc_req) {
1040 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p group %p count %d\n",
1041 blkdev, req, blkvsc_req->group,
1042 blkvsc_req->group->outstanding);
1043
1044 blkvsc_req->sector_start = start_sector;
1045 sector_div(blkvsc_req->sector_start,
1046 (blkdev->sector_size >> 9));
1047
1048 blkvsc_req->sector_count = num_sectors /
1049 (blkdev->sector_size >> 9);
1050
1051 blkvsc_init_rw(blkvsc_req);
1052 }
1053
1054 list_for_each_entry(blkvsc_req, &group->blkvsc_req_list, req_entry) {
1055 if (pending) {
1056 DPRINT_DBG(BLKVSC_DRV, "adding blkvsc_req to "
1057 "pending_list - blkvsc_req %p start_sect %lu"
1058 " sect_count %ld (%lu %ld)\n", blkvsc_req,
1059 (unsigned long)blkvsc_req->sector_start,
1060 blkvsc_req->sector_count,
1061 (unsigned long)start_sector,
1062 (unsigned long)num_sectors);
1063
1064 list_add_tail(&blkvsc_req->pend_entry,
1065 &blkdev->pending_list);
1066 } else {
1067 ret = blkvsc_submit_request(blkvsc_req,
1068 blkvsc_request_completion);
1069 if (ret == -1) {
1070 pending = 1;
1071 list_add_tail(&blkvsc_req->pend_entry,
1072 &blkdev->pending_list);
1073 }
1074
1075 DPRINT_DBG(BLKVSC_DRV, "submitted blkvsc_req %p "
1076 "start_sect %lu sect_count %ld (%lu %ld) "
1077 "ret %d\n", blkvsc_req,
1078 (unsigned long)blkvsc_req->sector_start,
1079 blkvsc_req->sector_count,
1080 (unsigned long)start_sector,
1081 num_sectors, ret);
1082 }
1083 }
1084
1085 return pending;
1086 }
1087
1088 static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
1089 {
1090 struct blkvsc_request *blkvsc_req =
1091 (struct blkvsc_request *)request->Context;
1092 struct block_device_context *blkdev =
1093 (struct block_device_context *)blkvsc_req->dev;
1094 struct scsi_sense_hdr sense_hdr;
1095
1096 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1097 blkvsc_req);
1098
1099 blkdev->num_outstanding_reqs--;
1100
1101 if (blkvsc_req->request.Status)
1102 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1103 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1104 scsi_print_sense_hdr("blkvsc", &sense_hdr);
1105
1106 blkvsc_req->cond = 1;
1107 wake_up_interruptible(&blkvsc_req->wevent);
1108 }
1109
1110 static void blkvsc_request_completion(struct hv_storvsc_request *request)
1111 {
1112 struct blkvsc_request *blkvsc_req =
1113 (struct blkvsc_request *)request->Context;
1114 struct block_device_context *blkdev =
1115 (struct block_device_context *)blkvsc_req->dev;
1116 unsigned long flags;
1117 struct blkvsc_request *comp_req, *tmp;
1118
1119 ASSERT(blkvsc_req->group);
1120
1121 DPRINT_DBG(BLKVSC_DRV, "blkdev %p blkvsc_req %p group %p type %s "
1122 "sect_start %lu sect_count %ld len %d group outstd %d "
1123 "total outstd %d\n",
1124 blkdev, blkvsc_req, blkvsc_req->group,
1125 (blkvsc_req->write) ? "WRITE" : "READ",
1126 (unsigned long)blkvsc_req->sector_start,
1127 blkvsc_req->sector_count,
1128 blkvsc_req->request.DataBuffer.Length,
1129 blkvsc_req->group->outstanding,
1130 blkdev->num_outstanding_reqs);
1131
1132 spin_lock_irqsave(&blkdev->lock, flags);
1133
1134 blkdev->num_outstanding_reqs--;
1135 blkvsc_req->group->outstanding--;
1136
1137 /*
1138 * Only start processing when all the blkvsc_reqs are
1139 * completed. This guarantees no out-of-order blkvsc_req
1140 * completion when calling end_that_request_first()
1141 */
1142 if (blkvsc_req->group->outstanding == 0) {
1143 list_for_each_entry_safe(comp_req, tmp,
1144 &blkvsc_req->group->blkvsc_req_list,
1145 req_entry) {
1146 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1147 "sect_start %lu sect_count %ld \n",
1148 comp_req,
1149 (unsigned long)comp_req->sector_start,
1150 comp_req->sector_count);
1151
1152 list_del(&comp_req->req_entry);
1153
1154 if (!__blk_end_request(comp_req->req,
1155 (!comp_req->request.Status ? 0 : -EIO),
1156 comp_req->sector_count * blkdev->sector_size)) {
1157 /*
1158 * All the sectors have been xferred ie the
1159 * request is done
1160 */
1161 DPRINT_DBG(BLKVSC_DRV, "req %p COMPLETED\n",
1162 comp_req->req);
1163 kmem_cache_free(blkdev->request_pool,
1164 comp_req->group);
1165 }
1166
1167 kmem_cache_free(blkdev->request_pool, comp_req);
1168 }
1169
1170 if (!blkdev->shutting_down) {
1171 blkvsc_do_pending_reqs(blkdev);
1172 blk_start_queue(blkdev->gd->queue);
1173 blkvsc_request(blkdev->gd->queue);
1174 }
1175 }
1176
1177 spin_unlock_irqrestore(&blkdev->lock, flags);
1178 }
1179
1180 static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev)
1181 {
1182 struct blkvsc_request *pend_req, *tmp;
1183 struct blkvsc_request *comp_req, *tmp2;
1184
1185 int ret = 0;
1186
1187 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1188
1189 /* Flush the pending list first */
1190 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1191 pend_entry) {
1192 /*
1193 * The pend_req could be part of a partially completed
1194 * request. If so, complete those req first until we
1195 * hit the pend_req
1196 */
1197 list_for_each_entry_safe(comp_req, tmp2,
1198 &pend_req->group->blkvsc_req_list,
1199 req_entry) {
1200 DPRINT_DBG(BLKVSC_DRV, "completing blkvsc_req %p "
1201 "sect_start %lu sect_count %ld \n",
1202 comp_req,
1203 (unsigned long) comp_req->sector_start,
1204 comp_req->sector_count);
1205
1206 if (comp_req == pend_req)
1207 break;
1208
1209 list_del(&comp_req->req_entry);
1210
1211 if (comp_req->req) {
1212 ret = __blk_end_request(comp_req->req,
1213 (!comp_req->request.Status ? 0 : -EIO),
1214 comp_req->sector_count *
1215 blkdev->sector_size);
1216 ASSERT(ret != 0);
1217 }
1218
1219 kmem_cache_free(blkdev->request_pool, comp_req);
1220 }
1221
1222 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1223 pend_req);
1224
1225 list_del(&pend_req->pend_entry);
1226
1227 list_del(&pend_req->req_entry);
1228
1229 if (comp_req->req) {
1230 if (!__blk_end_request(pend_req->req, -EIO,
1231 pend_req->sector_count *
1232 blkdev->sector_size)) {
1233 /*
1234 * All the sectors have been xferred ie the
1235 * request is done
1236 */
1237 DPRINT_DBG(BLKVSC_DRV,
1238 "blkvsc_cancel_pending_reqs() - "
1239 "req %p COMPLETED\n", pend_req->req);
1240 kmem_cache_free(blkdev->request_pool,
1241 pend_req->group);
1242 }
1243 }
1244
1245 kmem_cache_free(blkdev->request_pool, pend_req);
1246 }
1247
1248 return ret;
1249 }
1250
1251 static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1252 {
1253 struct blkvsc_request *pend_req, *tmp;
1254 int ret = 0;
1255
1256 /* Flush the pending list first */
1257 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1258 pend_entry) {
1259 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1260 pend_req);
1261
1262 ret = blkvsc_submit_request(pend_req,
1263 blkvsc_request_completion);
1264 if (ret != 0)
1265 break;
1266 else
1267 list_del(&pend_req->pend_entry);
1268 }
1269
1270 return ret;
1271 }
1272
1273 static void blkvsc_request(struct request_queue *queue)
1274 {
1275 struct block_device_context *blkdev = NULL;
1276 struct request *req;
1277 int ret = 0;
1278
1279 DPRINT_DBG(BLKVSC_DRV, "- enter \n");
1280 while ((req = blk_peek_request(queue)) != NULL) {
1281 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1282
1283 blkdev = req->rq_disk->private_data;
1284 if (blkdev->shutting_down || !blk_fs_request(req) ||
1285 blkdev->media_not_present) {
1286 __blk_end_request_cur(req, 0);
1287 continue;
1288 }
1289
1290 ret = blkvsc_do_pending_reqs(blkdev);
1291
1292 if (ret != 0) {
1293 DPRINT_DBG(BLKVSC_DRV,
1294 "- stop queue - pending_list not empty\n");
1295 blk_stop_queue(queue);
1296 break;
1297 }
1298
1299 blk_start_request(req);
1300
1301 ret = blkvsc_do_request(blkdev, req);
1302 if (ret > 0) {
1303 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1304 blk_stop_queue(queue);
1305 break;
1306 } else if (ret < 0) {
1307 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1308 blk_requeue_request(queue, req);
1309 blk_stop_queue(queue);
1310 break;
1311 }
1312 }
1313 }
1314
1315 static int blkvsc_open(struct block_device *bdev, fmode_t mode)
1316 {
1317 struct block_device_context *blkdev = bdev->bd_disk->private_data;
1318
1319 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1320 blkdev->gd->disk_name);
1321
1322 spin_lock(&blkdev->lock);
1323
1324 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
1325 spin_unlock(&blkdev->lock);
1326 check_disk_change(bdev);
1327 spin_lock(&blkdev->lock);
1328 }
1329
1330 blkdev->users++;
1331
1332 spin_unlock(&blkdev->lock);
1333 return 0;
1334 }
1335
1336 static int blkvsc_release(struct gendisk *disk, fmode_t mode)
1337 {
1338 struct block_device_context *blkdev = disk->private_data;
1339
1340 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1341 blkdev->gd->disk_name);
1342
1343 spin_lock(&blkdev->lock);
1344 if (blkdev->users == 1) {
1345 spin_unlock(&blkdev->lock);
1346 blkvsc_do_flush(blkdev);
1347 spin_lock(&blkdev->lock);
1348 }
1349
1350 blkdev->users--;
1351
1352 spin_unlock(&blkdev->lock);
1353 return 0;
1354 }
1355
1356 static int blkvsc_media_changed(struct gendisk *gd)
1357 {
1358 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1359 return 1;
1360 }
1361
1362 static int blkvsc_revalidate_disk(struct gendisk *gd)
1363 {
1364 struct block_device_context *blkdev = gd->private_data;
1365
1366 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1367
1368 if (blkdev->device_type == DVD_TYPE) {
1369 blkvsc_do_read_capacity(blkdev);
1370 set_capacity(blkdev->gd, blkdev->capacity *
1371 (blkdev->sector_size/512));
1372 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
1373 }
1374 return 0;
1375 }
1376
1377 static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
1378 {
1379 sector_t total_sectors = get_capacity(bd->bd_disk);
1380 sector_t cylinder_times_heads = 0;
1381 sector_t temp = 0;
1382
1383 int sectors_per_track = 0;
1384 int heads = 0;
1385 int cylinders = 0;
1386 int rem = 0;
1387
1388 if (total_sectors > (65535 * 16 * 255))
1389 total_sectors = (65535 * 16 * 255);
1390
1391 if (total_sectors >= (65535 * 16 * 63)) {
1392 sectors_per_track = 255;
1393 heads = 16;
1394
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);
1398 } else {
1399 sectors_per_track = 17;
1400
1401 cylinder_times_heads = total_sectors;
1402 /* sector_div stores the quotient in cylinder_times_heads */
1403 rem = sector_div(cylinder_times_heads, sectors_per_track);
1404
1405 temp = cylinder_times_heads + 1023;
1406 /* sector_div stores the quotient in temp */
1407 rem = sector_div(temp, 1024);
1408
1409 heads = temp;
1410
1411 if (heads < 4)
1412 heads = 4;
1413
1414
1415 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1416 sectors_per_track = 31;
1417 heads = 16;
1418
1419 cylinder_times_heads = total_sectors;
1420 /*
1421 * sector_div stores the quotient in
1422 * cylinder_times_heads
1423 */
1424 rem = sector_div(cylinder_times_heads,
1425 sectors_per_track);
1426 }
1427
1428 if (cylinder_times_heads >= (heads * 1024)) {
1429 sectors_per_track = 63;
1430 heads = 16;
1431
1432 cylinder_times_heads = total_sectors;
1433 /*
1434 * sector_div stores the quotient in
1435 * cylinder_times_heads
1436 */
1437 rem = sector_div(cylinder_times_heads,
1438 sectors_per_track);
1439 }
1440 }
1441
1442 temp = cylinder_times_heads;
1443 /* sector_div stores the quotient in temp */
1444 rem = sector_div(temp, heads);
1445 cylinders = temp;
1446
1447 hg->heads = heads;
1448 hg->sectors = sectors_per_track;
1449 hg->cylinders = cylinders;
1450
1451 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1452 sectors_per_track);
1453
1454 return 0;
1455 }
1456
1457 static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1458 unsigned cmd, unsigned long argument)
1459 {
1460 /* struct block_device_context *blkdev = bd->bd_disk->private_data; */
1461 int ret;
1462
1463 switch (cmd) {
1464 /*
1465 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1466 * than just a GUID. Commented it out for now.
1467 */
1468 #if 0
1469 case HDIO_GET_IDENTITY:
1470 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
1471 if (copy_to_user((void __user *)arg, blkdev->device_id,
1472 blkdev->device_id_len))
1473 ret = -EFAULT;
1474 break;
1475 #endif
1476 default:
1477 ret = -EINVAL;
1478 break;
1479 }
1480
1481 return ret;
1482 }
1483
1484 static int __init blkvsc_init(void)
1485 {
1486 int ret;
1487
1488 ASSERT(sizeof(sector_t) == 8); /* Make sure CONFIG_LBD is set */
1489
1490 DPRINT_ENTER(BLKVSC_DRV);
1491
1492 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1493
1494 ret = blkvsc_drv_init(BlkVscInitialize);
1495
1496 DPRINT_EXIT(BLKVSC_DRV);
1497
1498 return ret;
1499 }
1500
1501 static void __exit blkvsc_exit(void)
1502 {
1503 DPRINT_ENTER(BLKVSC_DRV);
1504 blkvsc_drv_exit();
1505 DPRINT_ENTER(BLKVSC_DRV);
1506 }
1507
1508 MODULE_LICENSE("GPL");
1509 MODULE_VERSION(HV_DRV_VERSION);
1510 module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
1511 module_init(blkvsc_init);
1512 module_exit(blkvsc_exit);