]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/hv/blkvsc_drv.c
staging: hv: make the block driver depend on LBDAF
[net-next-2.6.git] / drivers / staging / hv / blkvsc_drv.c
CommitLineData
f82bd046 1/*
f82bd046
HJ
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:
d0e94d17 18 * Haiyang Zhang <haiyangz@microsoft.com>
f82bd046 19 * Hank Janssen <hjanssen@microsoft.com>
f82bd046 20 */
f82bd046
HJ
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>
5a0e3ad6 28#include <linux/slab.h>
f82bd046
HJ
29#include <scsi/scsi.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_dbg.h>
4983b39a 33#include "osd.h"
645954c5 34#include "logging.h"
26c14cc1 35#include "VersionInfo.h"
870cde80 36#include "vmbus.h"
731a7884 37#include "StorVscApi.h"
f82bd046 38
454f18a9 39
f82bd046
HJ
40#define BLKVSC_MINORS 64
41
f82bd046
HJ
42enum blkvsc_device_type {
43 UNKNOWN_DEV_TYPE,
44 HARDDISK_TYPE,
45 DVD_TYPE,
46};
47
454f18a9
BP
48/*
49 * This request ties the struct request and struct
0b3f6834 50 * blkvsc_request/hv_storvsc_request together A struct request may be
454f18a9
BP
51 * represented by 1 or more struct blkvsc_request
52 */
f82bd046 53struct blkvsc_request_group {
8a280399
GKH
54 int outstanding;
55 int status;
56 struct list_head blkvsc_req_list; /* list of blkvsc_requests */
f82bd046
HJ
57};
58
f82bd046 59struct blkvsc_request {
8a280399
GKH
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;
f82bd046 68
8a280399 69 struct block_device_context *dev;
f82bd046 70
8a280399
GKH
71 /* The group this request is part of. Maybe null */
72 struct blkvsc_request_group *group;
f82bd046 73
8a280399 74 wait_queue_head_t wevent;
f82bd046
HJ
75 int cond;
76
8a280399
GKH
77 int write;
78 sector_t sector_start;
79 unsigned long sector_count;
f82bd046
HJ
80
81 unsigned char sense_buffer[SCSI_SENSE_BUFFERSIZE];
82 unsigned char cmd_len;
83 unsigned char cmnd[MAX_COMMAND_SIZE];
84
0b3f6834 85 struct hv_storvsc_request request;
8a280399
GKH
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 */
f82bd046
HJ
92};
93
454f18a9 94/* Per device structure */
f82bd046 95struct block_device_context {
8a280399 96 /* point back to our device context */
f916a34d 97 struct vm_device *device_ctx;
8a280399
GKH
98 struct kmem_cache *request_pool;
99 spinlock_t lock;
100 struct gendisk *gd;
f82bd046 101 enum blkvsc_device_type device_type;
8a280399
GKH
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;
f82bd046
HJ
115};
116
454f18a9 117/* Per driver */
f82bd046 118struct blkvsc_driver_context {
454f18a9 119 /* !! These must be the first 2 fields !! */
8a280399
GKH
120 /* FIXME this is a bug! */
121 struct driver_context drv_ctx;
9f0c7d2c 122 struct storvsc_driver_object drv_obj;
f82bd046
HJ
123};
124
454f18a9 125/* Static decl */
f82bd046
HJ
126static int blkvsc_probe(struct device *dev);
127static int blkvsc_remove(struct device *device);
128static void blkvsc_shutdown(struct device *device);
129
39635f7d 130static int blkvsc_open(struct block_device *bdev, fmode_t mode);
77d2d9da 131static int blkvsc_release(struct gendisk *disk, fmode_t mode);
f82bd046
HJ
132static int blkvsc_media_changed(struct gendisk *gd);
133static int blkvsc_revalidate_disk(struct gendisk *gd);
134static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg);
dfe8b2d9
BP
135static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
136 unsigned cmd, unsigned long argument);
f82bd046 137static void blkvsc_request(struct request_queue *queue);
0b3f6834 138static void blkvsc_request_completion(struct hv_storvsc_request *request);
8a280399
GKH
139static int blkvsc_do_request(struct block_device_context *blkdev,
140 struct request *req);
141static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
142 void (*request_completion)(struct hv_storvsc_request *));
f82bd046 143static void blkvsc_init_rw(struct blkvsc_request *blkvsc_req);
0b3f6834 144static void blkvsc_cmd_completion(struct hv_storvsc_request *request);
f82bd046
HJ
145static int blkvsc_do_inquiry(struct block_device_context *blkdev);
146static int blkvsc_do_read_capacity(struct block_device_context *blkdev);
147static int blkvsc_do_read_capacity16(struct block_device_context *blkdev);
148static int blkvsc_do_flush(struct block_device_context *blkdev);
149static int blkvsc_cancel_pending_reqs(struct block_device_context *blkdev);
150static int blkvsc_do_pending_reqs(struct block_device_context *blkdev);
151
152
153static int blkvsc_ringbuffer_size = BLKVSC_RING_BUFFER_SIZE;
154
454f18a9 155/* The one and only one */
f82bd046
HJ
156static struct blkvsc_driver_context g_blkvsc_drv;
157
8a280399 158static struct block_device_operations block_ops = {
f82bd046
HJ
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
3e189519 168/*
8a280399
GKH
169 * blkvsc_drv_init - BlkVsc driver initialization.
170 */
21707bed 171static int blkvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
f82bd046 172{
9f0c7d2c 173 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
8a280399
GKH
174 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
175 int ret;
f82bd046
HJ
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
454f18a9 183 /* Callback to client driver to complete the initialization */
21707bed 184 drv_init(&storvsc_drv_obj->Base);
f82bd046
HJ
185
186 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
8a280399
GKH
187 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
188 sizeof(struct hv_guid));
f82bd046 189
f82bd046
HJ
190 drv_ctx->probe = blkvsc_probe;
191 drv_ctx->remove = blkvsc_remove;
192 drv_ctx->shutdown = blkvsc_shutdown;
f82bd046 193
454f18a9 194 /* The driver belongs to vmbus */
5d48a1c2 195 ret = vmbus_child_driver_register(drv_ctx);
f82bd046
HJ
196
197 DPRINT_EXIT(BLKVSC_DRV);
198
199 return ret;
200}
201
f82bd046
HJ
202static int blkvsc_drv_exit_cb(struct device *dev, void *data)
203{
204 struct device **curr = (struct device **)data;
205 *curr = dev;
454f18a9 206 return 1; /* stop iterating */
f82bd046
HJ
207}
208
bd1de709 209static void blkvsc_drv_exit(void)
f82bd046 210{
9f0c7d2c 211 struct storvsc_driver_object *storvsc_drv_obj = &g_blkvsc_drv.drv_obj;
8a280399
GKH
212 struct driver_context *drv_ctx = &g_blkvsc_drv.drv_ctx;
213 struct device *current_dev;
2295ba2e 214 int ret;
f82bd046 215
f82bd046
HJ
216 DPRINT_ENTER(BLKVSC_DRV);
217
8a280399 218 while (1) {
f82bd046
HJ
219 current_dev = NULL;
220
454f18a9 221 /* Get the device */
2295ba2e
BP
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
f82bd046
HJ
230
231 if (current_dev == NULL)
232 break;
233
454f18a9 234 /* Initiate removal from the top-down */
f82bd046
HJ
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
3e189519 248/*
8a280399
GKH
249 * blkvsc_probe - Add a new device for this driver
250 */
f82bd046
HJ
251static int blkvsc_probe(struct device *device)
252{
8a280399
GKH
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;
f916a34d 259 struct vm_device *device_ctx = device_to_vm_device(device);
3d3b5518 260 struct hv_device *device_obj = &device_ctx->device_obj;
f82bd046 261
8a280399 262 struct block_device_context *blkdev = NULL;
9f0c7d2c 263 struct storvsc_device_info device_info;
8a280399
GKH
264 int major = 0;
265 int devnum = 0;
266 int ret = 0;
267 static int ide0_registered;
268 static int ide1_registered;
f82bd046
HJ
269
270 DPRINT_ENTER(BLKVSC_DRV);
271
272 DPRINT_DBG(BLKVSC_DRV, "blkvsc_probe - enter");
273
8a280399 274 if (!storvsc_drv_obj->Base.OnDeviceAdd) {
f82bd046 275 DPRINT_ERR(BLKVSC_DRV, "OnDeviceAdd() not set");
f82bd046
HJ
276 ret = -1;
277 goto Cleanup;
278 }
279
280 blkdev = kzalloc(sizeof(struct block_device_context), GFP_KERNEL);
8a280399 281 if (!blkdev) {
f82bd046
HJ
282 ret = -ENOMEM;
283 goto Cleanup;
284 }
285
286 INIT_LIST_HEAD(&blkdev->pending_list);
287
454f18a9 288 /* Initialize what we can here */
f82bd046
HJ
289 spin_lock_init(&blkdev->lock);
290
8a280399
GKH
291 ASSERT(sizeof(struct blkvsc_request_group) <=
292 sizeof(struct blkvsc_request));
f82bd046 293
454f18a9 294 blkdev->request_pool = kmem_cache_create(dev_name(&device_ctx->device),
8a280399
GKH
295 sizeof(struct blkvsc_request) +
296 storvsc_drv_obj->RequestExtSize, 0,
297 SLAB_HWCACHE_ALIGN, NULL);
298 if (!blkdev->request_pool) {
f82bd046
HJ
299 ret = -ENOMEM;
300 goto Cleanup;
301 }
302
303
454f18a9 304 /* Call to the vsc driver to add the device */
f82bd046 305 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
8a280399 306 if (ret != 0) {
f82bd046
HJ
307 DPRINT_ERR(BLKVSC_DRV, "unable to add blkvsc device");
308 goto Cleanup;
309 }
310
311 blkdev->device_ctx = device_ctx;
8a280399
GKH
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;
f82bd046 316
b57a68dc 317 dev_set_drvdata(device, blkdev);
f82bd046 318
454f18a9 319 /* Calculate the major and device num */
8a280399 320 if (blkdev->path == 0) {
f82bd046 321 major = IDE0_MAJOR;
454f18a9 322 devnum = blkdev->path + blkdev->target; /* 0 or 1 */
f82bd046 323
8a280399 324 if (!ide0_registered) {
f82bd046 325 ret = register_blkdev(major, "ide");
8a280399
GKH
326 if (ret != 0) {
327 DPRINT_ERR(BLKVSC_DRV,
328 "register_blkdev() failed! ret %d",
329 ret);
f82bd046
HJ
330 goto Remove;
331 }
332
333 ide0_registered = 1;
334 }
8a280399 335 } else if (blkdev->path == 1) {
f82bd046 336 major = IDE1_MAJOR;
454f18a9 337 devnum = blkdev->path + blkdev->target + 1; /* 2 or 3 */
f82bd046 338
8a280399 339 if (!ide1_registered) {
f82bd046 340 ret = register_blkdev(major, "ide");
8a280399
GKH
341 if (ret != 0) {
342 DPRINT_ERR(BLKVSC_DRV,
343 "register_blkdev() failed! ret %d",
344 ret);
f82bd046
HJ
345 goto Remove;
346 }
347
348 ide1_registered = 1;
349 }
8a280399 350 } else {
f82bd046
HJ
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);
8a280399 359 if (!blkdev->gd) {
f82bd046
HJ
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);
8a78362c 368 blk_queue_max_segments(blkdev->gd->queue, MAX_MULTIPAGE_BUFFER_COUNT);
f82bd046
HJ
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;
8a280399 380 sprintf(blkdev->gd->disk_name, "hd%c", 'a' + devnum);
f82bd046
HJ
381
382 blkvsc_do_inquiry(blkdev);
8a280399 383 if (blkdev->device_type == DVD_TYPE) {
f82bd046
HJ
384 set_disk_ro(blkdev->gd, 1);
385 blkdev->gd->flags |= GENHD_FL_REMOVABLE;
386 blkvsc_do_read_capacity(blkdev);
8a280399 387 } else {
f82bd046
HJ
388 blkvsc_do_read_capacity16(blkdev);
389 }
390
391 set_capacity(blkdev->gd, blkdev->capacity * (blkdev->sector_size/512));
0fce4c2f 392 blk_queue_logical_block_size(blkdev->gd->queue, blkdev->sector_size);
454f18a9 393 /* go! */
f82bd046
HJ
394 add_disk(blkdev->gd);
395
8a280399
GKH
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);
f82bd046
HJ
399
400 return ret;
401
402Remove:
403 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
404
405Cleanup:
8a280399
GKH
406 if (blkdev) {
407 if (blkdev->request_pool) {
f82bd046
HJ
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
420static void blkvsc_shutdown(struct device *device)
421{
b57a68dc 422 struct block_device_context *blkdev = dev_get_drvdata(device);
f82bd046
HJ
423 unsigned long flags;
424
425 if (!blkdev)
426 return;
427
8a280399
GKH
428 DPRINT_DBG(BLKVSC_DRV, "blkvsc_shutdown - users %d disk %s\n",
429 blkdev->users, blkdev->gd->disk_name);
f82bd046
HJ
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
8a280399
GKH
439 while (blkdev->num_outstanding_reqs) {
440 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
441 blkdev->num_outstanding_reqs);
f82bd046
HJ
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
454static int blkvsc_do_flush(struct block_device_context *blkdev)
455{
8a280399 456 struct blkvsc_request *blkvsc_req;
f82bd046
HJ
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)
f82bd046 465 return -ENOMEM;
f82bd046
HJ
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
8a280399
GKH
480 /*
481 * Set this here since the completion routine may be invoked and
482 * completed before we return
483 */
484 blkvsc_req->cond = 0;
f82bd046
HJ
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
454f18a9 494/* Do a scsi INQUIRY cmd here to get the device type (ie disk or dvd) */
f82bd046
HJ
495static int blkvsc_do_inquiry(struct block_device_context *blkdev)
496{
8a280399 497 struct blkvsc_request *blkvsc_req;
f82bd046
HJ
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)
f82bd046 506 return -ENOMEM;
f82bd046
HJ
507
508 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
509 page_buf = alloc_page(GFP_KERNEL);
8a280399 510 if (!page_buf) {
f82bd046
HJ
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;
454f18a9
BP
525 blkvsc_req->cmnd[1] = 0x1; /* Get product data */
526 blkvsc_req->cmnd[2] = 0x83; /* mode page 83 */
f82bd046
HJ
527 blkvsc_req->cmnd[4] = 64;
528 blkvsc_req->cmd_len = 6;
529
8a280399
GKH
530 /*
531 * Set this here since the completion routine may be invoked and
532 * completed before we return
533 */
534 blkvsc_req->cond = 0;
f82bd046
HJ
535
536 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
537
8a280399
GKH
538 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
539 blkvsc_req, blkvsc_req->cond);
f82bd046
HJ
540
541 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
542
543 buf = kmap(page_buf);
544
04f50c4d 545 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, 64); */
454f18a9 546 /* be to le */
f82bd046
HJ
547 device_type = buf[0] & 0x1F;
548
8a280399 549 if (device_type == 0x0) {
f82bd046 550 blkdev->device_type = HARDDISK_TYPE;
8a280399 551 } else if (device_type == 0x5) {
f82bd046 552 blkdev->device_type = DVD_TYPE;
8a280399 553 } else {
454f18a9 554 /* TODO: this is currently unsupported device type */
f82bd046
HJ
555 blkdev->device_type = UNKNOWN_DEV_TYPE;
556 }
557
0686e4f4 558 DPRINT_DBG(BLKVSC_DRV, "device type %d\n", device_type);
f82bd046
HJ
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);
04f50c4d 565 /* printk_hex_dump_bytes("", DUMP_PREFIX_NONE, blkdev->device_id,
454f18a9 566 * blkdev->device_id_len); */
f82bd046
HJ
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
454f18a9 577/* Do a scsi READ_CAPACITY cmd here to get the size of the disk */
f82bd046
HJ
578static int blkvsc_do_read_capacity(struct block_device_context *blkdev)
579{
8a280399 580 struct blkvsc_request *blkvsc_req;
f82bd046
HJ
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;
454f18a9 589 blkdev->media_not_present = 0; /* assume a disk is present */
f82bd046
HJ
590
591 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
592 if (!blkvsc_req)
f82bd046 593 return -ENOMEM;
f82bd046
HJ
594
595 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
596 page_buf = alloc_page(GFP_KERNEL);
8a280399 597 if (!page_buf) {
f82bd046
HJ
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
454f18a9
BP
614 /*
615 * Set this here since the completion routine may be invoked
616 * and completed before we return
617 */
8a280399 618 blkvsc_req->cond = 0;
f82bd046
HJ
619
620 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
621
8a280399
GKH
622 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
623 blkvsc_req, blkvsc_req->cond);
f82bd046
HJ
624
625 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
626
454f18a9 627 /* check error */
8a280399
GKH
628 if (blkvsc_req->request.Status) {
629 scsi_normalize_sense(blkvsc_req->sense_buffer,
630 SCSI_SENSE_BUFFERSIZE, &sense_hdr);
f82bd046 631
8a280399
GKH
632 if (sense_hdr.asc == 0x3A) {
633 /* Medium not present */
f82bd046
HJ
634 blkdev->media_not_present = 1;
635 }
f82bd046
HJ
636 return 0;
637 }
638 buf = kmap(page_buf);
639
454f18a9 640 /* be to le */
8a280399
GKH
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];
f82bd046
HJ
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
f82bd046
HJ
655static int blkvsc_do_read_capacity16(struct block_device_context *blkdev)
656{
8a280399 657 struct blkvsc_request *blkvsc_req;
f82bd046
HJ
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;
454f18a9 666 blkdev->media_not_present = 0; /* assume a disk is present */
f82bd046
HJ
667
668 blkvsc_req = kmem_cache_alloc(blkdev->request_pool, GFP_KERNEL);
669 if (!blkvsc_req)
f82bd046 670 return -ENOMEM;
f82bd046
HJ
671
672 memset(blkvsc_req, 0, sizeof(struct blkvsc_request));
673 page_buf = alloc_page(GFP_KERNEL);
8a280399 674 if (!page_buf) {
f82bd046
HJ
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
454f18a9 688 blkvsc_req->cmnd[0] = 0x9E; /* READ_CAPACITY16; */
f82bd046
HJ
689 blkvsc_req->cmd_len = 16;
690
454f18a9
BP
691 /*
692 * Set this here since the completion routine may be invoked
693 * and completed before we return
694 */
8a280399 695 blkvsc_req->cond = 0;
f82bd046
HJ
696
697 blkvsc_submit_request(blkvsc_req, blkvsc_cmd_completion);
698
8a280399
GKH
699 DPRINT_DBG(BLKVSC_DRV, "waiting %p to complete - cond %d\n",
700 blkvsc_req, blkvsc_req->cond);
f82bd046
HJ
701
702 wait_event_interruptible(blkvsc_req->wevent, blkvsc_req->cond);
703
454f18a9 704 /* check error */
8a280399
GKH
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 */
f82bd046
HJ
710 blkdev->media_not_present = 1;
711 }
f82bd046
HJ
712 return 0;
713 }
714 buf = kmap(page_buf);
715
454f18a9 716 /* be to le */
8a280399
GKH
717 blkdev->capacity = be64_to_cpu(*(unsigned long long *) &buf[0]) + 1;
718 blkdev->sector_size = be32_to_cpu(*(unsigned int *)&buf[8]);
f82bd046 719
8a280399
GKH
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
f82bd046
HJ
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
3e189519 736/*
8a280399
GKH
737 * blkvsc_remove() - Callback when our device is removed
738 */
f82bd046
HJ
739static int blkvsc_remove(struct device *device)
740{
8a280399
GKH
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;
f916a34d 747 struct vm_device *device_ctx = device_to_vm_device(device);
3d3b5518 748 struct hv_device *device_obj = &device_ctx->device_obj;
b57a68dc 749 struct block_device_context *blkdev = dev_get_drvdata(device);
f82bd046 750 unsigned long flags;
8a280399 751 int ret;
f82bd046
HJ
752
753 DPRINT_ENTER(BLKVSC_DRV);
754
755 DPRINT_DBG(BLKVSC_DRV, "blkvsc_remove()\n");
756
8a280399 757 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
f82bd046
HJ
758 DPRINT_EXIT(BLKVSC_DRV);
759 return -1;
760 }
761
8a280399
GKH
762 /*
763 * Call to the vsc driver to let it know that the device is being
764 * removed
765 */
f82bd046 766 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
8a280399 767 if (ret != 0) {
454f18a9 768 /* TODO: */
8a280399
GKH
769 DPRINT_ERR(BLKVSC_DRV,
770 "unable to remove blkvsc device (ret %d)", ret);
f82bd046
HJ
771 }
772
454f18a9 773 /* Get to a known state */
f82bd046
HJ
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
8a280399
GKH
782 while (blkdev->num_outstanding_reqs) {
783 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
784 blkdev->num_outstanding_reqs);
f82bd046
HJ
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
809static 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
8a280399
GKH
816 if (blkvsc_req->sector_start > 0xffffffff) {
817 if (rq_data_dir(blkvsc_req->req)) {
f82bd046
HJ
818 blkvsc_req->write = 1;
819 blkvsc_req->cmnd[0] = WRITE_16;
8a280399 820 } else {
f82bd046
HJ
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
8a280399
GKH
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)) {
f82bd046
HJ
834 blkvsc_req->write = 1;
835 blkvsc_req->cmnd[0] = WRITE_10;
8a280399 836 } else {
f82bd046
HJ
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
8a280399
GKH
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)) {
f82bd046
HJ
849 blkvsc_req->write = 1;
850 blkvsc_req->cmnd[0] = WRITE_6;
8a280399 851 } else {
f82bd046
HJ
852 blkvsc_req->write = 0;
853 blkvsc_req->cmnd[0] = READ_6;
854 }
855
8a280399
GKH
856 *(unsigned int *)&blkvsc_req->cmnd[1] =
857 cpu_to_be32(blkvsc_req->sector_start) >> 8;
f82bd046 858 blkvsc_req->cmnd[1] &= 0x1f;
8a280399 859 blkvsc_req->cmnd[4] = (unsigned char)blkvsc_req->sector_count;
f82bd046
HJ
860 }
861}
862
8a280399
GKH
863static int blkvsc_submit_request(struct blkvsc_request *blkvsc_req,
864 void (*request_completion)(struct hv_storvsc_request *))
f82bd046
HJ
865{
866 struct block_device_context *blkdev = blkvsc_req->dev;
f916a34d 867 struct vm_device *device_ctx = blkdev->device_ctx;
8a280399
GKH
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;
0b3f6834 874 struct hv_storvsc_request *storvsc_req;
8a280399 875 int ret;
f82bd046 876
8a280399
GKH
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
f82bd046
HJ
893
894 storvsc_req = &blkvsc_req->request;
8a280399
GKH
895 storvsc_req->Extension = (void *)((unsigned long)blkvsc_req +
896 sizeof(struct blkvsc_request));
f82bd046 897
8a280399 898 storvsc_req->Type = blkvsc_req->write ? WRITE_TYPE : READ_TYPE;
f82bd046
HJ
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;
454f18a9 906 storvsc_req->LunId = 0; /* this is not really used at all */
f82bd046
HJ
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
8a280399
GKH
914 ret = storvsc_drv_obj->OnIORequest(&blkdev->device_ctx->device_obj,
915 &blkvsc_req->request);
f82bd046 916 if (ret == 0)
f82bd046 917 blkdev->num_outstanding_reqs++;
f82bd046
HJ
918
919 return ret;
920}
921
454f18a9
BP
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 */
8a280399
GKH
927static int blkvsc_do_request(struct block_device_context *blkdev,
928 struct request *req)
f82bd046 929{
8a280399
GKH
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;
f82bd046 934 struct blkvsc_request *tmp;
8a280399
GKH
935 int databuf_idx = 0;
936 int seg_idx = 0;
f82bd046
HJ
937 sector_t start_sector;
938 unsigned long num_sectors = 0;
8a280399
GKH
939 int ret = 0;
940 int pending = 0;
941 struct blkvsc_request_group *group = NULL;
f82bd046 942
0686e4f4 943 DPRINT_DBG(BLKVSC_DRV, "blkdev %p req %p sect %lu\n", blkdev, req,
8a280399 944 (unsigned long)blk_rq_pos(req));
f82bd046 945
454f18a9 946 /* Create a group to tie req to list of blkvsc_reqs */
8a280399 947 group = kmem_cache_alloc(blkdev->request_pool, GFP_ATOMIC);
f82bd046 948 if (!group)
f82bd046 949 return -ENOMEM;
f82bd046
HJ
950
951 INIT_LIST_HEAD(&group->blkvsc_req_list);
952 group->outstanding = group->status = 0;
953
0fce4c2f 954 start_sector = blk_rq_pos(req);
f82bd046 955
454f18a9 956 /* foreach bio in the request */
8a280399
GKH
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);
f82bd046
HJ
984 }
985
8a280399
GKH
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 }
f82bd046 1004
8a280399
GKH
1005 memset(blkvsc_req, 0,
1006 sizeof(struct blkvsc_request));
f82bd046 1007
8a280399
GKH
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;
f82bd046 1012
8a280399
GKH
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);
f82bd046 1018
8a280399
GKH
1019 start_sector += num_sectors;
1020 num_sectors = 0;
1021 databuf_idx = 0;
1022 }
f82bd046 1023
8a280399
GKH
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;
f82bd046 1027
8a280399 1028 prev_bvec = bvec;
f82bd046 1029
8a280399
GKH
1030 databuf_idx++;
1031 num_sectors += bvec->bv_len >> 9;
f82bd046 1032
8a280399 1033 } /* bio_for_each_segment */
f82bd046 1034
8a280399
GKH
1035 } /* rq_for_each_bio */
1036 }
f82bd046 1037
454f18a9 1038 /* Handle the last one */
8a280399
GKH
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);
f82bd046
HJ
1043
1044 blkvsc_req->sector_start = start_sector;
8a280399
GKH
1045 sector_div(blkvsc_req->sector_start,
1046 (blkdev->sector_size >> 9));
f82bd046 1047
8a280399
GKH
1048 blkvsc_req->sector_count = num_sectors /
1049 (blkdev->sector_size >> 9);
f82bd046
HJ
1050
1051 blkvsc_init_rw(blkvsc_req);
1052 }
1053
8a280399
GKH
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) {
f82bd046 1070 pending = 1;
8a280399
GKH
1071 list_add_tail(&blkvsc_req->pend_entry,
1072 &blkdev->pending_list);
f82bd046
HJ
1073 }
1074
8a280399
GKH
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);
f82bd046
HJ
1082 }
1083 }
1084
1085 return pending;
1086}
1087
0b3f6834 1088static void blkvsc_cmd_completion(struct hv_storvsc_request *request)
f82bd046 1089{
8a280399
GKH
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;
f82bd046
HJ
1094 struct scsi_sense_hdr sense_hdr;
1095
8a280399
GKH
1096 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cmd_completion() - req %p\n",
1097 blkvsc_req);
f82bd046
HJ
1098
1099 blkdev->num_outstanding_reqs--;
1100
1101 if (blkvsc_req->request.Status)
8a280399
GKH
1102 if (scsi_normalize_sense(blkvsc_req->sense_buffer,
1103 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
f82bd046 1104 scsi_print_sense_hdr("blkvsc", &sense_hdr);
f82bd046 1105
8a280399 1106 blkvsc_req->cond = 1;
f82bd046
HJ
1107 wake_up_interruptible(&blkvsc_req->wevent);
1108}
1109
0b3f6834 1110static void blkvsc_request_completion(struct hv_storvsc_request *request)
f82bd046 1111{
8a280399
GKH
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;
f82bd046
HJ
1116 unsigned long flags;
1117 struct blkvsc_request *comp_req, *tmp;
1118
1119 ASSERT(blkvsc_req->group);
1120
8a280399
GKH
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);
f82bd046
HJ
1131
1132 spin_lock_irqsave(&blkdev->lock, flags);
1133
1134 blkdev->num_outstanding_reqs--;
1135 blkvsc_req->group->outstanding--;
1136
454f18a9
BP
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 */
8a280399
GKH
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 "
0686e4f4 1147 "sect_start %lu sect_count %ld\n",
8a280399
GKH
1148 comp_req,
1149 (unsigned long)comp_req->sector_start,
1150 comp_req->sector_count);
f82bd046
HJ
1151
1152 list_del(&comp_req->req_entry);
1153
8a280399
GKH
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);
f82bd046 1165 }
f82bd046
HJ
1166
1167 kmem_cache_free(blkdev->request_pool, comp_req);
1168 }
1169
8a280399 1170 if (!blkdev->shutting_down) {
f82bd046
HJ
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
1180static 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
8a280399 1185 int ret = 0;
f82bd046
HJ
1186
1187 DPRINT_DBG(BLKVSC_DRV, "blkvsc_cancel_pending_reqs()");
1188
454f18a9 1189 /* Flush the pending list first */
8a280399
GKH
1190 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1191 pend_entry) {
454f18a9
BP
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 */
8a280399
GKH
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 "
0686e4f4 1201 "sect_start %lu sect_count %ld\n",
8a280399
GKH
1202 comp_req,
1203 (unsigned long) comp_req->sector_start,
1204 comp_req->sector_count);
f82bd046
HJ
1205
1206 if (comp_req == pend_req)
1207 break;
1208
1209 list_del(&comp_req->req_entry);
1210
8a280399
GKH
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);
ee350376
BP
1216
1217 /* FIXME: shouldn't this do more than return? */
1218 if (ret)
1219 goto out;
f82bd046
HJ
1220 }
1221
1222 kmem_cache_free(blkdev->request_pool, comp_req);
1223 }
1224
8a280399
GKH
1225 DPRINT_DBG(BLKVSC_DRV, "cancelling pending request - %p\n",
1226 pend_req);
f82bd046
HJ
1227
1228 list_del(&pend_req->pend_entry);
1229
1230 list_del(&pend_req->req_entry);
1231
8a280399
GKH
1232 if (comp_req->req) {
1233 if (!__blk_end_request(pend_req->req, -EIO,
1234 pend_req->sector_count *
1235 blkdev->sector_size)) {
1236 /*
1237 * All the sectors have been xferred ie the
1238 * request is done
1239 */
1240 DPRINT_DBG(BLKVSC_DRV,
1241 "blkvsc_cancel_pending_reqs() - "
1242 "req %p COMPLETED\n", pend_req->req);
1243 kmem_cache_free(blkdev->request_pool,
1244 pend_req->group);
1245 }
f82bd046
HJ
1246 }
1247
1248 kmem_cache_free(blkdev->request_pool, pend_req);
1249 }
1250
ee350376 1251out:
f82bd046
HJ
1252 return ret;
1253}
1254
1255static int blkvsc_do_pending_reqs(struct block_device_context *blkdev)
1256{
1257 struct blkvsc_request *pend_req, *tmp;
8a280399 1258 int ret = 0;
f82bd046 1259
454f18a9 1260 /* Flush the pending list first */
8a280399
GKH
1261 list_for_each_entry_safe(pend_req, tmp, &blkdev->pending_list,
1262 pend_entry) {
1263 DPRINT_DBG(BLKVSC_DRV, "working off pending_list - %p\n",
1264 pend_req);
f82bd046 1265
8a280399
GKH
1266 ret = blkvsc_submit_request(pend_req,
1267 blkvsc_request_completion);
f82bd046 1268 if (ret != 0)
f82bd046 1269 break;
f82bd046 1270 else
f82bd046 1271 list_del(&pend_req->pend_entry);
f82bd046
HJ
1272 }
1273
1274 return ret;
1275}
1276
1277static void blkvsc_request(struct request_queue *queue)
1278{
1279 struct block_device_context *blkdev = NULL;
1280 struct request *req;
8a280399 1281 int ret = 0;
f82bd046 1282
0686e4f4 1283 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
8a280399 1284 while ((req = blk_peek_request(queue)) != NULL) {
f82bd046
HJ
1285 DPRINT_DBG(BLKVSC_DRV, "- req %p\n", req);
1286
1287 blkdev = req->rq_disk->private_data;
8a280399
GKH
1288 if (blkdev->shutting_down || !blk_fs_request(req) ||
1289 blkdev->media_not_present) {
0fce4c2f 1290 __blk_end_request_cur(req, 0);
f82bd046
HJ
1291 continue;
1292 }
1293
1294 ret = blkvsc_do_pending_reqs(blkdev);
1295
8a280399
GKH
1296 if (ret != 0) {
1297 DPRINT_DBG(BLKVSC_DRV,
1298 "- stop queue - pending_list not empty\n");
f82bd046
HJ
1299 blk_stop_queue(queue);
1300 break;
1301 }
1302
0fce4c2f 1303 blk_start_request(req);
f82bd046
HJ
1304
1305 ret = blkvsc_do_request(blkdev, req);
8a280399 1306 if (ret > 0) {
f82bd046
HJ
1307 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no room\n");
1308 blk_stop_queue(queue);
1309 break;
8a280399 1310 } else if (ret < 0) {
f82bd046
HJ
1311 DPRINT_DBG(BLKVSC_DRV, "- stop queue - no mem\n");
1312 blk_requeue_request(queue, req);
1313 blk_stop_queue(queue);
1314 break;
1315 }
1316 }
1317}
1318
8a280399 1319static int blkvsc_open(struct block_device *bdev, fmode_t mode)
f82bd046 1320{
39635f7d 1321 struct block_device_context *blkdev = bdev->bd_disk->private_data;
f82bd046 1322
8a280399
GKH
1323 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1324 blkdev->gd->disk_name);
f82bd046
HJ
1325
1326 spin_lock(&blkdev->lock);
1327
8a280399 1328 if (!blkdev->users && blkdev->device_type == DVD_TYPE) {
f82bd046 1329 spin_unlock(&blkdev->lock);
39635f7d 1330 check_disk_change(bdev);
f82bd046
HJ
1331 spin_lock(&blkdev->lock);
1332 }
1333
1334 blkdev->users++;
1335
1336 spin_unlock(&blkdev->lock);
1337 return 0;
1338}
1339
77d2d9da 1340static int blkvsc_release(struct gendisk *disk, fmode_t mode)
f82bd046 1341{
77d2d9da 1342 struct block_device_context *blkdev = disk->private_data;
f82bd046 1343
8a280399
GKH
1344 DPRINT_DBG(BLKVSC_DRV, "- users %d disk %s\n", blkdev->users,
1345 blkdev->gd->disk_name);
f82bd046
HJ
1346
1347 spin_lock(&blkdev->lock);
8a280399 1348 if (blkdev->users == 1) {
f82bd046
HJ
1349 spin_unlock(&blkdev->lock);
1350 blkvsc_do_flush(blkdev);
1351 spin_lock(&blkdev->lock);
1352 }
1353
1354 blkdev->users--;
1355
1356 spin_unlock(&blkdev->lock);
1357 return 0;
1358}
1359
1360static int blkvsc_media_changed(struct gendisk *gd)
1361{
1362 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
f82bd046
HJ
1363 return 1;
1364}
1365
1366static int blkvsc_revalidate_disk(struct gendisk *gd)
1367{
1368 struct block_device_context *blkdev = gd->private_data;
1369
1370 DPRINT_DBG(BLKVSC_DRV, "- enter\n");
1371
8a280399 1372 if (blkdev->device_type == DVD_TYPE) {
f82bd046 1373 blkvsc_do_read_capacity(blkdev);
8a280399
GKH
1374 set_capacity(blkdev->gd, blkdev->capacity *
1375 (blkdev->sector_size/512));
0fce4c2f 1376 blk_queue_logical_block_size(gd->queue, blkdev->sector_size);
f82bd046
HJ
1377 }
1378 return 0;
1379}
1380
bd1de709 1381static int blkvsc_getgeo(struct block_device *bd, struct hd_geometry *hg)
f82bd046
HJ
1382{
1383 sector_t total_sectors = get_capacity(bd->bd_disk);
8a280399
GKH
1384 sector_t cylinder_times_heads = 0;
1385 sector_t temp = 0;
f82bd046 1386
8a280399
GKH
1387 int sectors_per_track = 0;
1388 int heads = 0;
1389 int cylinders = 0;
1390 int rem = 0;
f82bd046 1391
8a280399
GKH
1392 if (total_sectors > (65535 * 16 * 255))
1393 total_sectors = (65535 * 16 * 255);
f82bd046 1394
8a280399
GKH
1395 if (total_sectors >= (65535 * 16 * 63)) {
1396 sectors_per_track = 255;
1397 heads = 16;
f82bd046
HJ
1398
1399 cylinder_times_heads = total_sectors;
8a280399
GKH
1400 /* sector_div stores the quotient in cylinder_times_heads */
1401 rem = sector_div(cylinder_times_heads, sectors_per_track);
1402 } else {
1403 sectors_per_track = 17;
f82bd046
HJ
1404
1405 cylinder_times_heads = total_sectors;
8a280399
GKH
1406 /* sector_div stores the quotient in cylinder_times_heads */
1407 rem = sector_div(cylinder_times_heads, sectors_per_track);
f82bd046
HJ
1408
1409 temp = cylinder_times_heads + 1023;
8a280399
GKH
1410 /* sector_div stores the quotient in temp */
1411 rem = sector_div(temp, 1024);
f82bd046
HJ
1412
1413 heads = temp;
1414
8a280399
GKH
1415 if (heads < 4)
1416 heads = 4;
1417
f82bd046 1418
8a280399
GKH
1419 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1420 sectors_per_track = 31;
1421 heads = 16;
f82bd046
HJ
1422
1423 cylinder_times_heads = total_sectors;
8a280399
GKH
1424 /*
1425 * sector_div stores the quotient in
1426 * cylinder_times_heads
1427 */
1428 rem = sector_div(cylinder_times_heads,
1429 sectors_per_track);
1430 }
f82bd046 1431
8a280399
GKH
1432 if (cylinder_times_heads >= (heads * 1024)) {
1433 sectors_per_track = 63;
1434 heads = 16;
f82bd046
HJ
1435
1436 cylinder_times_heads = total_sectors;
8a280399
GKH
1437 /*
1438 * sector_div stores the quotient in
1439 * cylinder_times_heads
1440 */
1441 rem = sector_div(cylinder_times_heads,
1442 sectors_per_track);
1443 }
454f18a9 1444 }
f82bd046
HJ
1445
1446 temp = cylinder_times_heads;
8a280399
GKH
1447 /* sector_div stores the quotient in temp */
1448 rem = sector_div(temp, heads);
f82bd046
HJ
1449 cylinders = temp;
1450
1451 hg->heads = heads;
8a280399
GKH
1452 hg->sectors = sectors_per_track;
1453 hg->cylinders = cylinders;
f82bd046 1454
8a280399
GKH
1455 DPRINT_INFO(BLKVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1456 sectors_per_track);
f82bd046
HJ
1457
1458 return 0;
1459}
1460
dfe8b2d9
BP
1461static int blkvsc_ioctl(struct block_device *bd, fmode_t mode,
1462 unsigned cmd, unsigned long argument)
f82bd046 1463{
b5788529 1464/* struct block_device_context *blkdev = bd->bd_disk->private_data; */
8a280399 1465 int ret;
f82bd046 1466
8a280399
GKH
1467 switch (cmd) {
1468 /*
1469 * TODO: I think there is certain format for HDIO_GET_IDENTITY rather
1470 * than just a GUID. Commented it out for now.
1471 */
1472#if 0
1473 case HDIO_GET_IDENTITY:
f82bd046 1474 DPRINT_INFO(BLKVSC_DRV, "HDIO_GET_IDENTITY\n");
8a280399
GKH
1475 if (copy_to_user((void __user *)arg, blkdev->device_id,
1476 blkdev->device_id_len))
f82bd046 1477 ret = -EFAULT;
8a280399
GKH
1478 break;
1479#endif
f82bd046
HJ
1480 default:
1481 ret = -EINVAL;
1482 break;
1483 }
1484
1485 return ret;
1486}
1487
f82bd046
HJ
1488static int __init blkvsc_init(void)
1489{
1490 int ret;
1491
5afd06cc 1492 BUILD_BUG_ON(sizeof(sector_t) != 8);
f82bd046
HJ
1493
1494 DPRINT_ENTER(BLKVSC_DRV);
1495
1496 DPRINT_INFO(BLKVSC_DRV, "Blkvsc initializing....");
1497
1498 ret = blkvsc_drv_init(BlkVscInitialize);
1499
1500 DPRINT_EXIT(BLKVSC_DRV);
1501
1502 return ret;
1503}
1504
1505static void __exit blkvsc_exit(void)
1506{
1507 DPRINT_ENTER(BLKVSC_DRV);
f82bd046 1508 blkvsc_drv_exit();
f82bd046
HJ
1509 DPRINT_ENTER(BLKVSC_DRV);
1510}
1511
8a280399 1512MODULE_LICENSE("GPL");
26c14cc1 1513MODULE_VERSION(HV_DRV_VERSION);
f82bd046 1514module_param(blkvsc_ringbuffer_size, int, S_IRUGO);
f82bd046
HJ
1515module_init(blkvsc_init);
1516module_exit(blkvsc_exit);