2 * dcssblk.c -- the S/390 block driver for dcss memory
4 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/ctype.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/blkdev.h>
14 #include <asm/extmem.h>
16 #include <linux/completion.h>
17 #include <linux/interrupt.h>
18 #include <asm/ccwdev.h> // for s390_root_dev_(un)register()
20 //#define DCSSBLK_DEBUG /* Debug messages on/off */
21 #define DCSSBLK_NAME "dcssblk"
22 #define DCSSBLK_MINORS_PER_DISK 1
23 #define DCSSBLK_PARM_LEN 400
26 #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSSBLK_NAME " debug: " x)
28 #define PRINT_DEBUG(x...) do {} while (0)
30 #define PRINT_INFO(x...) printk(KERN_INFO DCSSBLK_NAME " info: " x)
31 #define PRINT_WARN(x...) printk(KERN_WARNING DCSSBLK_NAME " warning: " x)
32 #define PRINT_ERR(x...) printk(KERN_ERR DCSSBLK_NAME " error: " x)
35 static int dcssblk_open(struct inode
*inode
, struct file
*filp
);
36 static int dcssblk_release(struct inode
*inode
, struct file
*filp
);
37 static int dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
);
39 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
41 static int dcssblk_major
;
42 static struct block_device_operations dcssblk_devops
= {
45 .release
= dcssblk_release
,
48 static ssize_t
dcssblk_add_store(struct device
* dev
, const char * buf
,
50 static ssize_t
dcssblk_remove_store(struct device
* dev
, const char * buf
,
52 static ssize_t
dcssblk_save_store(struct device
* dev
, const char * buf
,
54 static ssize_t
dcssblk_save_show(struct device
*dev
, char *buf
);
55 static ssize_t
dcssblk_shared_store(struct device
* dev
, const char * buf
,
57 static ssize_t
dcssblk_shared_show(struct device
*dev
, char *buf
);
59 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
60 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
61 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUGO
, dcssblk_save_show
,
63 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUGO
, dcssblk_shared_show
,
64 dcssblk_shared_store
);
66 static struct device
*dcssblk_root_dev
;
68 struct dcssblk_dev_info
{
71 char segment_name
[BUS_ID_SIZE
];
77 unsigned char save_pending
;
78 unsigned char is_shared
;
79 struct request_queue
*dcssblk_queue
;
82 static struct list_head dcssblk_devices
= LIST_HEAD_INIT(dcssblk_devices
);
83 static struct rw_semaphore dcssblk_devices_sem
;
86 * release function for segment device.
89 dcssblk_release_segment(struct device
*dev
)
91 PRINT_DEBUG("segment release fn called for %s\n", dev
->bus_id
);
92 kfree(container_of(dev
, struct dcssblk_dev_info
, dev
));
93 module_put(THIS_MODULE
);
97 * get a minor number. needs to be called with
98 * down_write(&dcssblk_devices_sem) and the
99 * device needs to be enqueued before the semaphore is
103 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
106 struct dcssblk_dev_info
*entry
;
108 if (dev_info
== NULL
)
110 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
112 // test if minor available
113 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
114 if (minor
== entry
->gd
->first_minor
)
116 if (!found
) break; // got unused minor
120 dev_info
->gd
->first_minor
= minor
;
125 * get the struct dcssblk_dev_info from dcssblk_devices
126 * for the given name.
127 * down_read(&dcssblk_devices_sem) must be held.
129 static struct dcssblk_dev_info
*
130 dcssblk_get_device_by_name(char *name
)
132 struct dcssblk_dev_info
*entry
;
134 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
135 if (!strcmp(name
, entry
->segment_name
)) {
143 * print appropriate error message for segment_load()/segment_type()
147 dcssblk_segment_warn(int rc
, char* seg_name
)
151 PRINT_WARN("cannot load/query segment %s, does not exist\n",
155 PRINT_WARN("cannot load/query segment %s, not running on VM\n",
159 PRINT_WARN("cannot load/query segment %s, hardware error\n",
163 PRINT_WARN("cannot load/query segment %s, is a multi-part "
164 "segment\n", seg_name
);
167 PRINT_WARN("cannot load/query segment %s, overlaps with "
168 "storage\n", seg_name
);
171 PRINT_WARN("cannot load/query segment %s, overlaps with "
172 "already loaded dcss\n", seg_name
);
175 PRINT_WARN("cannot load/query segment %s, already loaded in "
176 "incompatible mode\n", seg_name
);
179 PRINT_WARN("cannot load/query segment %s, out of memory\n",
183 PRINT_WARN("cannot load/query segment %s, exceeds kernel "
184 "mapping range\n", seg_name
);
187 PRINT_WARN("cannot load/query segment %s, return value %i\n",
194 * device attribute for switching shared/nonshared (exclusive)
195 * operation (show + store)
198 dcssblk_shared_show(struct device
*dev
, char *buf
)
200 struct dcssblk_dev_info
*dev_info
;
202 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
203 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
207 dcssblk_shared_store(struct device
*dev
, const char *inbuf
, size_t count
)
209 struct dcssblk_dev_info
*dev_info
;
212 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0')) {
213 PRINT_WARN("Invalid value, must be 0 or 1\n");
216 down_write(&dcssblk_devices_sem
);
217 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
218 if (atomic_read(&dev_info
->use_count
)) {
219 PRINT_ERR("share: segment %s is busy!\n",
220 dev_info
->segment_name
);
224 if (inbuf
[0] == '1') {
225 // reload segment in shared mode
226 rc
= segment_modify_shared(dev_info
->segment_name
,
229 BUG_ON(rc
== -EINVAL
);
230 if (rc
== -EIO
|| rc
== -ENOENT
)
233 dev_info
->is_shared
= 1;
234 switch (dev_info
->segment_type
) {
238 set_disk_ro(dev_info
->gd
,1);
241 } else if (inbuf
[0] == '0') {
242 // reload segment in exclusive mode
243 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
244 PRINT_ERR("Segment type SC (%s) cannot be loaded in "
245 "non-shared mode\n", dev_info
->segment_name
);
249 rc
= segment_modify_shared(dev_info
->segment_name
,
252 BUG_ON(rc
== -EINVAL
);
253 if (rc
== -EIO
|| rc
== -ENOENT
)
256 dev_info
->is_shared
= 0;
257 set_disk_ro(dev_info
->gd
, 0);
260 PRINT_WARN("Invalid value, must be 0 or 1\n");
268 PRINT_ERR("Could not reload segment %s, removing it now!\n",
269 dev_info
->segment_name
);
270 list_del(&dev_info
->lh
);
272 del_gendisk(dev_info
->gd
);
273 blk_put_queue(dev_info
->dcssblk_queue
);
274 dev_info
->gd
->queue
= NULL
;
275 put_disk(dev_info
->gd
);
276 device_unregister(dev
);
279 up_write(&dcssblk_devices_sem
);
284 * device attribute for save operation on current copy
285 * of the segment. If the segment is busy, saving will
286 * become pending until it gets released, which can be
287 * undone by storing a non-true value to this entry.
291 dcssblk_save_show(struct device
*dev
, char *buf
)
293 struct dcssblk_dev_info
*dev_info
;
295 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
296 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
300 dcssblk_save_store(struct device
*dev
, const char *inbuf
, size_t count
)
302 struct dcssblk_dev_info
*dev_info
;
304 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0')) {
305 PRINT_WARN("Invalid value, must be 0 or 1\n");
308 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
310 down_write(&dcssblk_devices_sem
);
311 if (inbuf
[0] == '1') {
312 if (atomic_read(&dev_info
->use_count
) == 0) {
313 // device is idle => we save immediately
314 PRINT_INFO("Saving segment %s\n",
315 dev_info
->segment_name
);
316 segment_save(dev_info
->segment_name
);
318 // device is busy => we save it when it becomes
319 // idle in dcssblk_release
320 PRINT_INFO("Segment %s is currently busy, it will "
321 "be saved when it becomes idle...\n",
322 dev_info
->segment_name
);
323 dev_info
->save_pending
= 1;
325 } else if (inbuf
[0] == '0') {
326 if (dev_info
->save_pending
) {
327 // device is busy & the user wants to undo his save
329 dev_info
->save_pending
= 0;
330 PRINT_INFO("Pending save for segment %s deactivated\n",
331 dev_info
->segment_name
);
334 up_write(&dcssblk_devices_sem
);
335 PRINT_WARN("Invalid value, must be 0 or 1\n");
338 up_write(&dcssblk_devices_sem
);
343 * device attribute for adding devices
346 dcssblk_add_store(struct device
*dev
, const char *buf
, size_t count
)
349 struct dcssblk_dev_info
*dev_info
;
351 unsigned long seg_byte_size
;
354 if (dev
!= dcssblk_root_dev
) {
358 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
359 if (local_buf
== NULL
) {
366 for (i
= 0; ((buf
[i
] != '\0') && (buf
[i
] != '\n') && i
< count
); i
++) {
367 local_buf
[i
] = toupper(buf
[i
]);
370 if ((i
== 0) || (i
> 8)) {
377 down_read(&dcssblk_devices_sem
);
378 dev_info
= dcssblk_get_device_by_name(local_buf
);
379 up_read(&dcssblk_devices_sem
);
380 if (dev_info
!= NULL
) {
381 PRINT_WARN("Segment %s already loaded!\n", local_buf
);
386 * get a struct dcssblk_dev_info
388 dev_info
= kmalloc(sizeof(struct dcssblk_dev_info
), GFP_KERNEL
);
389 if (dev_info
== NULL
) {
393 memset(dev_info
, 0, sizeof(struct dcssblk_dev_info
));
395 strcpy(dev_info
->segment_name
, local_buf
);
396 strlcpy(dev_info
->dev
.bus_id
, local_buf
, BUS_ID_SIZE
);
397 dev_info
->dev
.release
= dcssblk_release_segment
;
398 INIT_LIST_HEAD(&dev_info
->lh
);
400 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
401 if (dev_info
->gd
== NULL
) {
405 dev_info
->gd
->major
= dcssblk_major
;
406 dev_info
->gd
->fops
= &dcssblk_devops
;
407 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
408 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
409 dev_info
->gd
->private_data
= dev_info
;
410 dev_info
->gd
->driverfs_dev
= &dev_info
->dev
;
414 rc
= segment_load(local_buf
, SEGMENT_SHARED
,
415 &dev_info
->start
, &dev_info
->end
);
417 dcssblk_segment_warn(rc
, dev_info
->segment_name
);
418 goto dealloc_gendisk
;
420 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
421 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
422 PRINT_INFO("Loaded segment %s, size = %lu Byte, "
423 "capacity = %lu (512 Byte) sectors\n", local_buf
,
424 seg_byte_size
, seg_byte_size
>> 9);
426 dev_info
->segment_type
= rc
;
427 dev_info
->save_pending
= 0;
428 dev_info
->is_shared
= 1;
429 dev_info
->dev
.parent
= dcssblk_root_dev
;
432 * get minor, add to list
434 down_write(&dcssblk_devices_sem
);
435 rc
= dcssblk_assign_free_minor(dev_info
);
437 up_write(&dcssblk_devices_sem
);
438 PRINT_ERR("No free minor number available! "
439 "Unloading segment...\n");
442 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
443 dev_info
->gd
->first_minor
);
444 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
446 if (!try_module_get(THIS_MODULE
)) {
451 * register the device
453 rc
= device_register(&dev_info
->dev
);
455 PRINT_ERR("Segment %s could not be registered RC=%d\n",
457 module_put(THIS_MODULE
);
460 get_device(&dev_info
->dev
);
461 rc
= device_create_file(&dev_info
->dev
, &dev_attr_shared
);
464 rc
= device_create_file(&dev_info
->dev
, &dev_attr_save
);
468 add_disk(dev_info
->gd
);
470 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
471 blk_queue_hardsect_size(dev_info
->dcssblk_queue
, 4096);
473 switch (dev_info
->segment_type
) {
477 set_disk_ro(dev_info
->gd
,1);
480 set_disk_ro(dev_info
->gd
,0);
483 PRINT_DEBUG("Segment %s loaded successfully\n", local_buf
);
484 up_write(&dcssblk_devices_sem
);
489 PRINT_ERR("device_create_file() failed!\n");
490 list_del(&dev_info
->lh
);
491 blk_put_queue(dev_info
->dcssblk_queue
);
492 dev_info
->gd
->queue
= NULL
;
493 put_disk(dev_info
->gd
);
494 device_unregister(&dev_info
->dev
);
495 segment_unload(dev_info
->segment_name
);
496 put_device(&dev_info
->dev
);
497 up_write(&dcssblk_devices_sem
);
500 list_del(&dev_info
->lh
);
501 up_write(&dcssblk_devices_sem
);
503 segment_unload(local_buf
);
505 blk_put_queue(dev_info
->dcssblk_queue
);
506 dev_info
->gd
->queue
= NULL
;
507 put_disk(dev_info
->gd
);
517 * device attribute for removing devices
520 dcssblk_remove_store(struct device
*dev
, const char *buf
, size_t count
)
522 struct dcssblk_dev_info
*dev_info
;
526 if (dev
!= dcssblk_root_dev
) {
529 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
530 if (local_buf
== NULL
) {
536 for (i
= 0; ((*(buf
+i
)!='\0') && (*(buf
+i
)!='\n') && i
< count
); i
++) {
537 local_buf
[i
] = toupper(buf
[i
]);
540 if ((i
== 0) || (i
> 8)) {
545 down_write(&dcssblk_devices_sem
);
546 dev_info
= dcssblk_get_device_by_name(local_buf
);
547 if (dev_info
== NULL
) {
548 up_write(&dcssblk_devices_sem
);
549 PRINT_WARN("Segment %s is not loaded!\n", local_buf
);
553 if (atomic_read(&dev_info
->use_count
) != 0) {
554 up_write(&dcssblk_devices_sem
);
555 PRINT_WARN("Segment %s is in use!\n", local_buf
);
559 list_del(&dev_info
->lh
);
561 del_gendisk(dev_info
->gd
);
562 blk_put_queue(dev_info
->dcssblk_queue
);
563 dev_info
->gd
->queue
= NULL
;
564 put_disk(dev_info
->gd
);
565 device_unregister(&dev_info
->dev
);
566 segment_unload(dev_info
->segment_name
);
567 PRINT_DEBUG("Segment %s unloaded successfully\n",
568 dev_info
->segment_name
);
569 put_device(&dev_info
->dev
);
570 up_write(&dcssblk_devices_sem
);
579 dcssblk_open(struct inode
*inode
, struct file
*filp
)
581 struct dcssblk_dev_info
*dev_info
;
584 dev_info
= inode
->i_bdev
->bd_disk
->private_data
;
585 if (NULL
== dev_info
) {
589 atomic_inc(&dev_info
->use_count
);
590 inode
->i_bdev
->bd_block_size
= 4096;
597 dcssblk_release(struct inode
*inode
, struct file
*filp
)
599 struct dcssblk_dev_info
*dev_info
;
602 dev_info
= inode
->i_bdev
->bd_disk
->private_data
;
603 if (NULL
== dev_info
) {
607 down_write(&dcssblk_devices_sem
);
608 if (atomic_dec_and_test(&dev_info
->use_count
)
609 && (dev_info
->save_pending
)) {
610 PRINT_INFO("Segment %s became idle and is being saved now\n",
611 dev_info
->segment_name
);
612 segment_save(dev_info
->segment_name
);
613 dev_info
->save_pending
= 0;
615 up_write(&dcssblk_devices_sem
);
622 dcssblk_make_request(request_queue_t
*q
, struct bio
*bio
)
624 struct dcssblk_dev_info
*dev_info
;
625 struct bio_vec
*bvec
;
627 unsigned long page_addr
;
628 unsigned long source_addr
;
629 unsigned long bytes_done
;
633 dev_info
= bio
->bi_bdev
->bd_disk
->private_data
;
634 if (dev_info
== NULL
)
636 if ((bio
->bi_sector
& 7) != 0 || (bio
->bi_size
& 4095) != 0)
637 /* Request is not page-aligned. */
639 if (((bio
->bi_size
>> 9) + bio
->bi_sector
)
640 > get_capacity(bio
->bi_bdev
->bd_disk
)) {
641 /* Request beyond end of DCSS segment. */
644 index
= (bio
->bi_sector
>> 3);
645 bio_for_each_segment(bvec
, bio
, i
) {
646 page_addr
= (unsigned long)
647 page_address(bvec
->bv_page
) + bvec
->bv_offset
;
648 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
649 if (unlikely(page_addr
& 4095) != 0 || (bvec
->bv_len
& 4095) != 0)
652 if (bio_data_dir(bio
) == READ
) {
653 memcpy((void*)page_addr
, (void*)source_addr
,
656 memcpy((void*)source_addr
, (void*)page_addr
,
659 bytes_done
+= bvec
->bv_len
;
661 bio_endio(bio
, bytes_done
, 0);
664 bio_io_error(bio
, bytes_done
);
669 dcssblk_check_params(void)
673 struct dcssblk_dev_info
*dev_info
;
675 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
677 for (j
= i
; (dcssblk_segments
[j
] != ',') &&
678 (dcssblk_segments
[j
] != '\0') &&
679 (dcssblk_segments
[j
] != '(') &&
682 buf
[j
-i
] = dcssblk_segments
[j
];
685 rc
= dcssblk_add_store(dcssblk_root_dev
, buf
, j
-i
);
686 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
687 for (k
= 0; buf
[k
] != '\0'; k
++)
688 buf
[k
] = toupper(buf
[k
]);
689 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
690 down_read(&dcssblk_devices_sem
);
691 dev_info
= dcssblk_get_device_by_name(buf
);
692 up_read(&dcssblk_devices_sem
);
694 dcssblk_shared_store(&dev_info
->dev
,
698 while ((dcssblk_segments
[j
] != ',') &&
699 (dcssblk_segments
[j
] != '\0'))
703 if (dcssblk_segments
[j
] == '\0')
710 * The init/exit functions.
717 PRINT_DEBUG("DCSSBLOCK EXIT...\n");
718 s390_root_dev_unregister(dcssblk_root_dev
);
719 rc
= unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
721 PRINT_ERR("unregister_blkdev() failed!\n");
723 PRINT_DEBUG("...finished!\n");
731 PRINT_DEBUG("DCSSBLOCK INIT...\n");
732 dcssblk_root_dev
= s390_root_dev_register("dcssblk");
733 if (IS_ERR(dcssblk_root_dev
)) {
734 PRINT_ERR("device_register() failed!\n");
735 return PTR_ERR(dcssblk_root_dev
);
737 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
739 PRINT_ERR("device_create_file(add) failed!\n");
740 s390_root_dev_unregister(dcssblk_root_dev
);
743 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
745 PRINT_ERR("device_create_file(remove) failed!\n");
746 s390_root_dev_unregister(dcssblk_root_dev
);
749 rc
= register_blkdev(0, DCSSBLK_NAME
);
751 PRINT_ERR("Can't get dynamic major!\n");
752 s390_root_dev_unregister(dcssblk_root_dev
);
756 init_rwsem(&dcssblk_devices_sem
);
758 dcssblk_check_params();
760 PRINT_DEBUG("...finished!\n");
764 module_init(dcssblk_init
);
765 module_exit(dcssblk_exit
);
767 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
768 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
769 "comma-separated list, each name max. 8 chars.\n"
770 "Adding \"(local)\" to segment name equals echoing 0 to "
771 "/sys/devices/dcssblk/<segment name>/shared after loading "
773 "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
775 MODULE_LICENSE("GPL");