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