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