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