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/s390_rdev.h>
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);
38 static int dcssblk_direct_access(struct block_device *bdev, sector_t secnum,
41 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
43 static int dcssblk_major;
44 static struct block_device_operations dcssblk_devops = {
47 .release = dcssblk_release,
48 .direct_access = dcssblk_direct_access,
51 static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
53 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
55 static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
57 static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
58 static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
60 static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
62 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
63 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
64 static DEVICE_ATTR(save, S_IWUSR | S_IRUGO, dcssblk_save_show,
66 static DEVICE_ATTR(shared, S_IWUSR | S_IRUGO, dcssblk_shared_show,
67 dcssblk_shared_store);
69 static struct device *dcssblk_root_dev;
71 struct dcssblk_dev_info {
74 char segment_name[BUS_ID_SIZE];
80 unsigned char save_pending;
81 unsigned char is_shared;
82 struct request_queue *dcssblk_queue;
85 static LIST_HEAD(dcssblk_devices);
86 static struct rw_semaphore dcssblk_devices_sem;
89 * release function for segment device.
92 dcssblk_release_segment(struct device *dev)
94 PRINT_DEBUG("segment release fn called for %s\n", dev->bus_id);
95 kfree(container_of(dev, struct dcssblk_dev_info, dev));
96 module_put(THIS_MODULE);
100 * get a minor number. needs to be called with
101 * down_write(&dcssblk_devices_sem) and the
102 * device needs to be enqueued before the semaphore is
106 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
109 struct dcssblk_dev_info *entry;
111 if (dev_info == NULL)
113 for (minor = 0; minor < (1<<MINORBITS); minor++) {
115 // test if minor available
116 list_for_each_entry(entry, &dcssblk_devices, lh)
117 if (minor == entry->gd->first_minor)
119 if (!found) break; // got unused minor
123 dev_info->gd->first_minor = minor;
128 * get the struct dcssblk_dev_info from dcssblk_devices
129 * for the given name.
130 * down_read(&dcssblk_devices_sem) must be held.
132 static struct dcssblk_dev_info *
133 dcssblk_get_device_by_name(char *name)
135 struct dcssblk_dev_info *entry;
137 list_for_each_entry(entry, &dcssblk_devices, lh) {
138 if (!strcmp(name, entry->segment_name)) {
145 static void dcssblk_unregister_callback(struct device *dev)
147 device_unregister(dev);
152 * device attribute for switching shared/nonshared (exclusive)
153 * operation (show + store)
156 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
158 struct dcssblk_dev_info *dev_info;
160 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
161 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
165 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
167 struct dcssblk_dev_info *dev_info;
170 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
171 PRINT_WARN("Invalid value, must be 0 or 1\n");
174 down_write(&dcssblk_devices_sem);
175 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
176 if (atomic_read(&dev_info->use_count)) {
177 PRINT_ERR("share: segment %s is busy!\n",
178 dev_info->segment_name);
182 if (inbuf[0] == '1') {
183 // reload segment in shared mode
184 rc = segment_modify_shared(dev_info->segment_name,
187 BUG_ON(rc == -EINVAL);
191 dev_info->is_shared = 1;
192 switch (dev_info->segment_type) {
196 set_disk_ro(dev_info->gd,1);
199 } else if (inbuf[0] == '0') {
200 // reload segment in exclusive mode
201 if (dev_info->segment_type == SEG_TYPE_SC) {
202 PRINT_ERR("Segment type SC (%s) cannot be loaded in "
203 "non-shared mode\n", dev_info->segment_name);
207 rc = segment_modify_shared(dev_info->segment_name,
210 BUG_ON(rc == -EINVAL);
214 dev_info->is_shared = 0;
215 set_disk_ro(dev_info->gd, 0);
218 PRINT_WARN("Invalid value, must be 0 or 1\n");
226 PRINT_ERR("Could not reload segment %s, removing it now!\n",
227 dev_info->segment_name);
228 list_del(&dev_info->lh);
230 del_gendisk(dev_info->gd);
231 blk_cleanup_queue(dev_info->dcssblk_queue);
232 dev_info->gd->queue = NULL;
233 put_disk(dev_info->gd);
234 rc = device_schedule_callback(dev, dcssblk_unregister_callback);
236 up_write(&dcssblk_devices_sem);
241 * device attribute for save operation on current copy
242 * of the segment. If the segment is busy, saving will
243 * become pending until it gets released, which can be
244 * undone by storing a non-true value to this entry.
248 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
250 struct dcssblk_dev_info *dev_info;
252 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
253 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
257 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
259 struct dcssblk_dev_info *dev_info;
261 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
262 PRINT_WARN("Invalid value, must be 0 or 1\n");
265 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
267 down_write(&dcssblk_devices_sem);
268 if (inbuf[0] == '1') {
269 if (atomic_read(&dev_info->use_count) == 0) {
270 // device is idle => we save immediately
271 PRINT_INFO("Saving segment %s\n",
272 dev_info->segment_name);
273 segment_save(dev_info->segment_name);
275 // device is busy => we save it when it becomes
276 // idle in dcssblk_release
277 PRINT_INFO("Segment %s is currently busy, it will "
278 "be saved when it becomes idle...\n",
279 dev_info->segment_name);
280 dev_info->save_pending = 1;
282 } else if (inbuf[0] == '0') {
283 if (dev_info->save_pending) {
284 // device is busy & the user wants to undo his save
286 dev_info->save_pending = 0;
287 PRINT_INFO("Pending save for segment %s deactivated\n",
288 dev_info->segment_name);
291 up_write(&dcssblk_devices_sem);
292 PRINT_WARN("Invalid value, must be 0 or 1\n");
295 up_write(&dcssblk_devices_sem);
300 * device attribute for adding devices
303 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
306 struct dcssblk_dev_info *dev_info;
308 unsigned long seg_byte_size;
311 if (dev != dcssblk_root_dev) {
315 local_buf = kmalloc(count + 1, GFP_KERNEL);
316 if (local_buf == NULL) {
323 for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
324 local_buf[i] = toupper(buf[i]);
327 if ((i == 0) || (i > 8)) {
334 down_read(&dcssblk_devices_sem);
335 dev_info = dcssblk_get_device_by_name(local_buf);
336 up_read(&dcssblk_devices_sem);
337 if (dev_info != NULL) {
338 PRINT_WARN("Segment %s already loaded!\n", local_buf);
343 * get a struct dcssblk_dev_info
345 dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
346 if (dev_info == NULL) {
351 strcpy(dev_info->segment_name, local_buf);
352 strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
353 dev_info->dev.release = dcssblk_release_segment;
354 INIT_LIST_HEAD(&dev_info->lh);
356 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
357 if (dev_info->gd == NULL) {
361 dev_info->gd->major = dcssblk_major;
362 dev_info->gd->fops = &dcssblk_devops;
363 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
364 dev_info->gd->queue = dev_info->dcssblk_queue;
365 dev_info->gd->private_data = dev_info;
366 dev_info->gd->driverfs_dev = &dev_info->dev;
367 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
368 blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
372 rc = segment_load(local_buf, SEGMENT_SHARED,
373 &dev_info->start, &dev_info->end);
375 segment_warning(rc, dev_info->segment_name);
376 goto dealloc_gendisk;
378 seg_byte_size = (dev_info->end - dev_info->start + 1);
379 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
380 PRINT_INFO("Loaded segment %s, size = %lu Byte, "
381 "capacity = %lu (512 Byte) sectors\n", local_buf,
382 seg_byte_size, seg_byte_size >> 9);
384 dev_info->segment_type = rc;
385 dev_info->save_pending = 0;
386 dev_info->is_shared = 1;
387 dev_info->dev.parent = dcssblk_root_dev;
390 * get minor, add to list
392 down_write(&dcssblk_devices_sem);
393 rc = dcssblk_assign_free_minor(dev_info);
395 up_write(&dcssblk_devices_sem);
396 PRINT_ERR("No free minor number available! "
397 "Unloading segment...\n");
400 sprintf(dev_info->gd->disk_name, "dcssblk%d",
401 dev_info->gd->first_minor);
402 list_add_tail(&dev_info->lh, &dcssblk_devices);
404 if (!try_module_get(THIS_MODULE)) {
409 * register the device
411 rc = device_register(&dev_info->dev);
413 PRINT_ERR("Segment %s could not be registered RC=%d\n",
415 module_put(THIS_MODULE);
418 get_device(&dev_info->dev);
419 rc = device_create_file(&dev_info->dev, &dev_attr_shared);
422 rc = device_create_file(&dev_info->dev, &dev_attr_save);
426 add_disk(dev_info->gd);
428 switch (dev_info->segment_type) {
432 set_disk_ro(dev_info->gd,1);
435 set_disk_ro(dev_info->gd,0);
438 PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
439 up_write(&dcssblk_devices_sem);
444 PRINT_ERR("device_create_file() failed!\n");
445 list_del(&dev_info->lh);
446 blk_cleanup_queue(dev_info->dcssblk_queue);
447 dev_info->gd->queue = NULL;
448 put_disk(dev_info->gd);
449 device_unregister(&dev_info->dev);
450 segment_unload(dev_info->segment_name);
451 put_device(&dev_info->dev);
452 up_write(&dcssblk_devices_sem);
455 list_del(&dev_info->lh);
456 up_write(&dcssblk_devices_sem);
458 segment_unload(local_buf);
460 blk_cleanup_queue(dev_info->dcssblk_queue);
461 dev_info->gd->queue = NULL;
462 put_disk(dev_info->gd);
472 * device attribute for removing devices
475 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
477 struct dcssblk_dev_info *dev_info;
481 if (dev != dcssblk_root_dev) {
484 local_buf = kmalloc(count + 1, GFP_KERNEL);
485 if (local_buf == NULL) {
491 for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
492 local_buf[i] = toupper(buf[i]);
495 if ((i == 0) || (i > 8)) {
500 down_write(&dcssblk_devices_sem);
501 dev_info = dcssblk_get_device_by_name(local_buf);
502 if (dev_info == NULL) {
503 up_write(&dcssblk_devices_sem);
504 PRINT_WARN("Segment %s is not loaded!\n", local_buf);
508 if (atomic_read(&dev_info->use_count) != 0) {
509 up_write(&dcssblk_devices_sem);
510 PRINT_WARN("Segment %s is in use!\n", local_buf);
514 list_del(&dev_info->lh);
516 del_gendisk(dev_info->gd);
517 blk_cleanup_queue(dev_info->dcssblk_queue);
518 dev_info->gd->queue = NULL;
519 put_disk(dev_info->gd);
520 device_unregister(&dev_info->dev);
521 segment_unload(dev_info->segment_name);
522 PRINT_DEBUG("Segment %s unloaded successfully\n",
523 dev_info->segment_name);
524 put_device(&dev_info->dev);
525 up_write(&dcssblk_devices_sem);
534 dcssblk_open(struct inode *inode, struct file *filp)
536 struct dcssblk_dev_info *dev_info;
539 dev_info = inode->i_bdev->bd_disk->private_data;
540 if (NULL == dev_info) {
544 atomic_inc(&dev_info->use_count);
545 inode->i_bdev->bd_block_size = 4096;
552 dcssblk_release(struct inode *inode, struct file *filp)
554 struct dcssblk_dev_info *dev_info;
557 dev_info = inode->i_bdev->bd_disk->private_data;
558 if (NULL == dev_info) {
562 down_write(&dcssblk_devices_sem);
563 if (atomic_dec_and_test(&dev_info->use_count)
564 && (dev_info->save_pending)) {
565 PRINT_INFO("Segment %s became idle and is being saved now\n",
566 dev_info->segment_name);
567 segment_save(dev_info->segment_name);
568 dev_info->save_pending = 0;
570 up_write(&dcssblk_devices_sem);
577 dcssblk_make_request(struct request_queue *q, struct bio *bio)
579 struct dcssblk_dev_info *dev_info;
580 struct bio_vec *bvec;
582 unsigned long page_addr;
583 unsigned long source_addr;
584 unsigned long bytes_done;
588 dev_info = bio->bi_bdev->bd_disk->private_data;
589 if (dev_info == NULL)
591 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
592 /* Request is not page-aligned. */
594 if (((bio->bi_size >> 9) + bio->bi_sector)
595 > get_capacity(bio->bi_bdev->bd_disk)) {
596 /* Request beyond end of DCSS segment. */
599 /* verify data transfer direction */
600 if (dev_info->is_shared) {
601 switch (dev_info->segment_type) {
605 /* cannot write to these segments */
606 if (bio_data_dir(bio) == WRITE) {
607 PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
613 index = (bio->bi_sector >> 3);
614 bio_for_each_segment(bvec, bio, i) {
615 page_addr = (unsigned long)
616 page_address(bvec->bv_page) + bvec->bv_offset;
617 source_addr = dev_info->start + (index<<12) + bytes_done;
618 if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
621 if (bio_data_dir(bio) == READ) {
622 memcpy((void*)page_addr, (void*)source_addr,
625 memcpy((void*)source_addr, (void*)page_addr,
628 bytes_done += bvec->bv_len;
638 dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
641 struct dcssblk_dev_info *dev_info;
644 dev_info = bdev->bd_disk->private_data;
647 if (secnum % (PAGE_SIZE/512))
649 pgoff = secnum / (PAGE_SIZE / 512);
650 if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
652 *data = (unsigned long) (dev_info->start+pgoff*PAGE_SIZE);
657 dcssblk_check_params(void)
661 struct dcssblk_dev_info *dev_info;
663 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
665 for (j = i; (dcssblk_segments[j] != ',') &&
666 (dcssblk_segments[j] != '\0') &&
667 (dcssblk_segments[j] != '(') &&
670 buf[j-i] = dcssblk_segments[j];
673 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
674 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
675 for (k = 0; buf[k] != '\0'; k++)
676 buf[k] = toupper(buf[k]);
677 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
678 down_read(&dcssblk_devices_sem);
679 dev_info = dcssblk_get_device_by_name(buf);
680 up_read(&dcssblk_devices_sem);
682 dcssblk_shared_store(&dev_info->dev,
686 while ((dcssblk_segments[j] != ',') &&
687 (dcssblk_segments[j] != '\0'))
691 if (dcssblk_segments[j] == '\0')
698 * The init/exit functions.
703 PRINT_DEBUG("DCSSBLOCK EXIT...\n");
704 s390_root_dev_unregister(dcssblk_root_dev);
705 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
706 PRINT_DEBUG("...finished!\n");
714 PRINT_DEBUG("DCSSBLOCK INIT...\n");
715 dcssblk_root_dev = s390_root_dev_register("dcssblk");
716 if (IS_ERR(dcssblk_root_dev)) {
717 PRINT_ERR("device_register() failed!\n");
718 return PTR_ERR(dcssblk_root_dev);
720 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
722 PRINT_ERR("device_create_file(add) failed!\n");
723 s390_root_dev_unregister(dcssblk_root_dev);
726 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
728 PRINT_ERR("device_create_file(remove) failed!\n");
729 s390_root_dev_unregister(dcssblk_root_dev);
732 rc = register_blkdev(0, DCSSBLK_NAME);
734 PRINT_ERR("Can't get dynamic major!\n");
735 s390_root_dev_unregister(dcssblk_root_dev);
739 init_rwsem(&dcssblk_devices_sem);
741 dcssblk_check_params();
743 PRINT_DEBUG("...finished!\n");
747 module_init(dcssblk_init);
748 module_exit(dcssblk_exit);
750 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
751 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
752 "comma-separated list, each name max. 8 chars.\n"
753 "Adding \"(local)\" to segment name equals echoing 0 to "
754 "/sys/devices/dcssblk/<segment name>/shared after loading "
756 "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
758 MODULE_LICENSE("GPL");