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