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