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 struct list_head dcssblk_devices = LIST_HEAD_INIT(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)) {
146 * print appropriate error message for segment_load()/segment_type()
150 dcssblk_segment_warn(int rc, char* seg_name)
154 PRINT_WARN("cannot load/query segment %s, does not exist\n",
158 PRINT_WARN("cannot load/query segment %s, not running on VM\n",
162 PRINT_WARN("cannot load/query segment %s, hardware error\n",
166 PRINT_WARN("cannot load/query segment %s, is a multi-part "
167 "segment\n", seg_name);
170 PRINT_WARN("cannot load/query segment %s, overlaps with "
171 "storage\n", seg_name);
174 PRINT_WARN("cannot load/query segment %s, overlaps with "
175 "already loaded dcss\n", seg_name);
178 PRINT_WARN("cannot load/query segment %s, already loaded in "
179 "incompatible mode\n", seg_name);
182 PRINT_WARN("cannot load/query segment %s, out of memory\n",
186 PRINT_WARN("cannot load/query segment %s, exceeds kernel "
187 "mapping range\n", seg_name);
190 PRINT_WARN("cannot load/query segment %s, return value %i\n",
197 * device attribute for switching shared/nonshared (exclusive)
198 * operation (show + store)
201 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
203 struct dcssblk_dev_info *dev_info;
205 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
206 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
210 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
212 struct dcssblk_dev_info *dev_info;
215 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
216 PRINT_WARN("Invalid value, must be 0 or 1\n");
219 down_write(&dcssblk_devices_sem);
220 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
221 if (atomic_read(&dev_info->use_count)) {
222 PRINT_ERR("share: segment %s is busy!\n",
223 dev_info->segment_name);
227 if (inbuf[0] == '1') {
228 // reload segment in shared mode
229 rc = segment_modify_shared(dev_info->segment_name,
232 BUG_ON(rc == -EINVAL);
236 dev_info->is_shared = 1;
237 switch (dev_info->segment_type) {
241 set_disk_ro(dev_info->gd,1);
244 } else if (inbuf[0] == '0') {
245 // reload segment in exclusive mode
246 if (dev_info->segment_type == SEG_TYPE_SC) {
247 PRINT_ERR("Segment type SC (%s) cannot be loaded in "
248 "non-shared mode\n", dev_info->segment_name);
252 rc = segment_modify_shared(dev_info->segment_name,
255 BUG_ON(rc == -EINVAL);
259 dev_info->is_shared = 0;
260 set_disk_ro(dev_info->gd, 0);
263 PRINT_WARN("Invalid value, must be 0 or 1\n");
271 PRINT_ERR("Could not reload segment %s, removing it now!\n",
272 dev_info->segment_name);
273 list_del(&dev_info->lh);
275 del_gendisk(dev_info->gd);
276 blk_cleanup_queue(dev_info->dcssblk_queue);
277 dev_info->gd->queue = NULL;
278 put_disk(dev_info->gd);
279 device_unregister(dev);
282 up_write(&dcssblk_devices_sem);
287 * device attribute for save operation on current copy
288 * of the segment. If the segment is busy, saving will
289 * become pending until it gets released, which can be
290 * undone by storing a non-true value to this entry.
294 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
296 struct dcssblk_dev_info *dev_info;
298 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
299 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
303 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
305 struct dcssblk_dev_info *dev_info;
307 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
308 PRINT_WARN("Invalid value, must be 0 or 1\n");
311 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
313 down_write(&dcssblk_devices_sem);
314 if (inbuf[0] == '1') {
315 if (atomic_read(&dev_info->use_count) == 0) {
316 // device is idle => we save immediately
317 PRINT_INFO("Saving segment %s\n",
318 dev_info->segment_name);
319 segment_save(dev_info->segment_name);
321 // device is busy => we save it when it becomes
322 // idle in dcssblk_release
323 PRINT_INFO("Segment %s is currently busy, it will "
324 "be saved when it becomes idle...\n",
325 dev_info->segment_name);
326 dev_info->save_pending = 1;
328 } else if (inbuf[0] == '0') {
329 if (dev_info->save_pending) {
330 // device is busy & the user wants to undo his save
332 dev_info->save_pending = 0;
333 PRINT_INFO("Pending save for segment %s deactivated\n",
334 dev_info->segment_name);
337 up_write(&dcssblk_devices_sem);
338 PRINT_WARN("Invalid value, must be 0 or 1\n");
341 up_write(&dcssblk_devices_sem);
346 * device attribute for adding devices
349 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
352 struct dcssblk_dev_info *dev_info;
354 unsigned long seg_byte_size;
357 if (dev != dcssblk_root_dev) {
361 local_buf = kmalloc(count + 1, GFP_KERNEL);
362 if (local_buf == NULL) {
369 for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
370 local_buf[i] = toupper(buf[i]);
373 if ((i == 0) || (i > 8)) {
380 down_read(&dcssblk_devices_sem);
381 dev_info = dcssblk_get_device_by_name(local_buf);
382 up_read(&dcssblk_devices_sem);
383 if (dev_info != NULL) {
384 PRINT_WARN("Segment %s already loaded!\n", local_buf);
389 * get a struct dcssblk_dev_info
391 dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
392 if (dev_info == NULL) {
397 strcpy(dev_info->segment_name, local_buf);
398 strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
399 dev_info->dev.release = dcssblk_release_segment;
400 INIT_LIST_HEAD(&dev_info->lh);
402 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
403 if (dev_info->gd == NULL) {
407 dev_info->gd->major = dcssblk_major;
408 dev_info->gd->fops = &dcssblk_devops;
409 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
410 dev_info->gd->queue = dev_info->dcssblk_queue;
411 dev_info->gd->private_data = dev_info;
412 dev_info->gd->driverfs_dev = &dev_info->dev;
416 rc = segment_load(local_buf, SEGMENT_SHARED,
417 &dev_info->start, &dev_info->end);
419 dcssblk_segment_warn(rc, dev_info->segment_name);
420 goto dealloc_gendisk;
422 seg_byte_size = (dev_info->end - dev_info->start + 1);
423 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
424 PRINT_INFO("Loaded segment %s, size = %lu Byte, "
425 "capacity = %lu (512 Byte) sectors\n", local_buf,
426 seg_byte_size, seg_byte_size >> 9);
428 dev_info->segment_type = rc;
429 dev_info->save_pending = 0;
430 dev_info->is_shared = 1;
431 dev_info->dev.parent = dcssblk_root_dev;
434 * get minor, add to list
436 down_write(&dcssblk_devices_sem);
437 rc = dcssblk_assign_free_minor(dev_info);
439 up_write(&dcssblk_devices_sem);
440 PRINT_ERR("No free minor number available! "
441 "Unloading segment...\n");
444 sprintf(dev_info->gd->disk_name, "dcssblk%d",
445 dev_info->gd->first_minor);
446 list_add_tail(&dev_info->lh, &dcssblk_devices);
448 if (!try_module_get(THIS_MODULE)) {
453 * register the device
455 rc = device_register(&dev_info->dev);
457 PRINT_ERR("Segment %s could not be registered RC=%d\n",
459 module_put(THIS_MODULE);
462 get_device(&dev_info->dev);
463 rc = device_create_file(&dev_info->dev, &dev_attr_shared);
466 rc = device_create_file(&dev_info->dev, &dev_attr_save);
470 add_disk(dev_info->gd);
472 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
473 blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
475 switch (dev_info->segment_type) {
479 set_disk_ro(dev_info->gd,1);
482 set_disk_ro(dev_info->gd,0);
485 PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
486 up_write(&dcssblk_devices_sem);
491 PRINT_ERR("device_create_file() failed!\n");
492 list_del(&dev_info->lh);
493 blk_cleanup_queue(dev_info->dcssblk_queue);
494 dev_info->gd->queue = NULL;
495 put_disk(dev_info->gd);
496 device_unregister(&dev_info->dev);
497 segment_unload(dev_info->segment_name);
498 put_device(&dev_info->dev);
499 up_write(&dcssblk_devices_sem);
502 list_del(&dev_info->lh);
503 up_write(&dcssblk_devices_sem);
505 segment_unload(local_buf);
507 blk_cleanup_queue(dev_info->dcssblk_queue);
508 dev_info->gd->queue = NULL;
509 put_disk(dev_info->gd);
519 * device attribute for removing devices
522 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
524 struct dcssblk_dev_info *dev_info;
528 if (dev != dcssblk_root_dev) {
531 local_buf = kmalloc(count + 1, GFP_KERNEL);
532 if (local_buf == NULL) {
538 for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
539 local_buf[i] = toupper(buf[i]);
542 if ((i == 0) || (i > 8)) {
547 down_write(&dcssblk_devices_sem);
548 dev_info = dcssblk_get_device_by_name(local_buf);
549 if (dev_info == NULL) {
550 up_write(&dcssblk_devices_sem);
551 PRINT_WARN("Segment %s is not loaded!\n", local_buf);
555 if (atomic_read(&dev_info->use_count) != 0) {
556 up_write(&dcssblk_devices_sem);
557 PRINT_WARN("Segment %s is in use!\n", local_buf);
561 list_del(&dev_info->lh);
563 del_gendisk(dev_info->gd);
564 blk_cleanup_queue(dev_info->dcssblk_queue);
565 dev_info->gd->queue = NULL;
566 put_disk(dev_info->gd);
567 device_unregister(&dev_info->dev);
568 segment_unload(dev_info->segment_name);
569 PRINT_DEBUG("Segment %s unloaded successfully\n",
570 dev_info->segment_name);
571 put_device(&dev_info->dev);
572 up_write(&dcssblk_devices_sem);
581 dcssblk_open(struct inode *inode, struct file *filp)
583 struct dcssblk_dev_info *dev_info;
586 dev_info = inode->i_bdev->bd_disk->private_data;
587 if (NULL == dev_info) {
591 atomic_inc(&dev_info->use_count);
592 inode->i_bdev->bd_block_size = 4096;
599 dcssblk_release(struct inode *inode, struct file *filp)
601 struct dcssblk_dev_info *dev_info;
604 dev_info = inode->i_bdev->bd_disk->private_data;
605 if (NULL == dev_info) {
609 down_write(&dcssblk_devices_sem);
610 if (atomic_dec_and_test(&dev_info->use_count)
611 && (dev_info->save_pending)) {
612 PRINT_INFO("Segment %s became idle and is being saved now\n",
613 dev_info->segment_name);
614 segment_save(dev_info->segment_name);
615 dev_info->save_pending = 0;
617 up_write(&dcssblk_devices_sem);
624 dcssblk_make_request(struct request_queue *q, struct bio *bio)
626 struct dcssblk_dev_info *dev_info;
627 struct bio_vec *bvec;
629 unsigned long page_addr;
630 unsigned long source_addr;
631 unsigned long bytes_done;
635 dev_info = bio->bi_bdev->bd_disk->private_data;
636 if (dev_info == NULL)
638 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
639 /* Request is not page-aligned. */
641 if (((bio->bi_size >> 9) + bio->bi_sector)
642 > get_capacity(bio->bi_bdev->bd_disk)) {
643 /* Request beyond end of DCSS segment. */
646 /* verify data transfer direction */
647 if (dev_info->is_shared) {
648 switch (dev_info->segment_type) {
652 /* cannot write to these segments */
653 if (bio_data_dir(bio) == WRITE) {
654 PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
660 index = (bio->bi_sector >> 3);
661 bio_for_each_segment(bvec, bio, i) {
662 page_addr = (unsigned long)
663 page_address(bvec->bv_page) + bvec->bv_offset;
664 source_addr = dev_info->start + (index<<12) + bytes_done;
665 if (unlikely(page_addr & 4095) != 0 || (bvec->bv_len & 4095) != 0)
668 if (bio_data_dir(bio) == READ) {
669 memcpy((void*)page_addr, (void*)source_addr,
672 memcpy((void*)source_addr, (void*)page_addr,
675 bytes_done += bvec->bv_len;
677 bio_endio(bio, bytes_done, 0);
680 bio_io_error(bio, bio->bi_size);
685 dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
688 struct dcssblk_dev_info *dev_info;
691 dev_info = bdev->bd_disk->private_data;
694 if (secnum % (PAGE_SIZE/512))
696 pgoff = secnum / (PAGE_SIZE / 512);
697 if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
699 *data = (unsigned long) (dev_info->start+pgoff*PAGE_SIZE);
704 dcssblk_check_params(void)
708 struct dcssblk_dev_info *dev_info;
710 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
712 for (j = i; (dcssblk_segments[j] != ',') &&
713 (dcssblk_segments[j] != '\0') &&
714 (dcssblk_segments[j] != '(') &&
717 buf[j-i] = dcssblk_segments[j];
720 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
721 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
722 for (k = 0; buf[k] != '\0'; k++)
723 buf[k] = toupper(buf[k]);
724 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
725 down_read(&dcssblk_devices_sem);
726 dev_info = dcssblk_get_device_by_name(buf);
727 up_read(&dcssblk_devices_sem);
729 dcssblk_shared_store(&dev_info->dev,
733 while ((dcssblk_segments[j] != ',') &&
734 (dcssblk_segments[j] != '\0'))
738 if (dcssblk_segments[j] == '\0')
745 * The init/exit functions.
750 PRINT_DEBUG("DCSSBLOCK EXIT...\n");
751 s390_root_dev_unregister(dcssblk_root_dev);
752 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
753 PRINT_DEBUG("...finished!\n");
761 PRINT_DEBUG("DCSSBLOCK INIT...\n");
762 dcssblk_root_dev = s390_root_dev_register("dcssblk");
763 if (IS_ERR(dcssblk_root_dev)) {
764 PRINT_ERR("device_register() failed!\n");
765 return PTR_ERR(dcssblk_root_dev);
767 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
769 PRINT_ERR("device_create_file(add) failed!\n");
770 s390_root_dev_unregister(dcssblk_root_dev);
773 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
775 PRINT_ERR("device_create_file(remove) failed!\n");
776 s390_root_dev_unregister(dcssblk_root_dev);
779 rc = register_blkdev(0, DCSSBLK_NAME);
781 PRINT_ERR("Can't get dynamic major!\n");
782 s390_root_dev_unregister(dcssblk_root_dev);
786 init_rwsem(&dcssblk_devices_sem);
788 dcssblk_check_params();
790 PRINT_DEBUG("...finished!\n");
794 module_init(dcssblk_init);
795 module_exit(dcssblk_exit);
797 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
798 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
799 "comma-separated list, each name max. 8 chars.\n"
800 "Adding \"(local)\" to segment name equals echoing 0 to "
801 "/sys/devices/dcssblk/<segment name>/shared after loading "
803 "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
805 MODULE_LICENSE("GPL");