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