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