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);
233 if (rc == -EIO || rc == -ENOENT)
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);
256 if (rc == -EIO || rc == -ENOENT)
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_put_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 = kmalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
392 if (dev_info == NULL) {
396 memset(dev_info, 0, sizeof(struct dcssblk_dev_info));
398 strcpy(dev_info->segment_name, local_buf);
399 strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
400 dev_info->dev.release = dcssblk_release_segment;
401 INIT_LIST_HEAD(&dev_info->lh);
403 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
404 if (dev_info->gd == NULL) {
408 dev_info->gd->major = dcssblk_major;
409 dev_info->gd->fops = &dcssblk_devops;
410 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
411 dev_info->gd->queue = dev_info->dcssblk_queue;
412 dev_info->gd->private_data = dev_info;
413 dev_info->gd->driverfs_dev = &dev_info->dev;
417 rc = segment_load(local_buf, SEGMENT_SHARED,
418 &dev_info->start, &dev_info->end);
420 dcssblk_segment_warn(rc, dev_info->segment_name);
421 goto dealloc_gendisk;
423 seg_byte_size = (dev_info->end - dev_info->start + 1);
424 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
425 PRINT_INFO("Loaded segment %s, size = %lu Byte, "
426 "capacity = %lu (512 Byte) sectors\n", local_buf,
427 seg_byte_size, seg_byte_size >> 9);
429 dev_info->segment_type = rc;
430 dev_info->save_pending = 0;
431 dev_info->is_shared = 1;
432 dev_info->dev.parent = dcssblk_root_dev;
435 * get minor, add to list
437 down_write(&dcssblk_devices_sem);
438 rc = dcssblk_assign_free_minor(dev_info);
440 up_write(&dcssblk_devices_sem);
441 PRINT_ERR("No free minor number available! "
442 "Unloading segment...\n");
445 sprintf(dev_info->gd->disk_name, "dcssblk%d",
446 dev_info->gd->first_minor);
447 list_add_tail(&dev_info->lh, &dcssblk_devices);
449 if (!try_module_get(THIS_MODULE)) {
454 * register the device
456 rc = device_register(&dev_info->dev);
458 PRINT_ERR("Segment %s could not be registered RC=%d\n",
460 module_put(THIS_MODULE);
463 get_device(&dev_info->dev);
464 rc = device_create_file(&dev_info->dev, &dev_attr_shared);
467 rc = device_create_file(&dev_info->dev, &dev_attr_save);
471 add_disk(dev_info->gd);
473 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
474 blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
476 switch (dev_info->segment_type) {
480 set_disk_ro(dev_info->gd,1);
483 set_disk_ro(dev_info->gd,0);
486 PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
487 up_write(&dcssblk_devices_sem);
492 PRINT_ERR("device_create_file() failed!\n");
493 list_del(&dev_info->lh);
494 blk_put_queue(dev_info->dcssblk_queue);
495 dev_info->gd->queue = NULL;
496 put_disk(dev_info->gd);
497 device_unregister(&dev_info->dev);
498 segment_unload(dev_info->segment_name);
499 put_device(&dev_info->dev);
500 up_write(&dcssblk_devices_sem);
503 list_del(&dev_info->lh);
504 up_write(&dcssblk_devices_sem);
506 segment_unload(local_buf);
508 blk_put_queue(dev_info->dcssblk_queue);
509 dev_info->gd->queue = NULL;
510 put_disk(dev_info->gd);
520 * device attribute for removing devices
523 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
525 struct dcssblk_dev_info *dev_info;
529 if (dev != dcssblk_root_dev) {
532 local_buf = kmalloc(count + 1, GFP_KERNEL);
533 if (local_buf == NULL) {
539 for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
540 local_buf[i] = toupper(buf[i]);
543 if ((i == 0) || (i > 8)) {
548 down_write(&dcssblk_devices_sem);
549 dev_info = dcssblk_get_device_by_name(local_buf);
550 if (dev_info == NULL) {
551 up_write(&dcssblk_devices_sem);
552 PRINT_WARN("Segment %s is not loaded!\n", local_buf);
556 if (atomic_read(&dev_info->use_count) != 0) {
557 up_write(&dcssblk_devices_sem);
558 PRINT_WARN("Segment %s is in use!\n", local_buf);
562 list_del(&dev_info->lh);
564 del_gendisk(dev_info->gd);
565 blk_put_queue(dev_info->dcssblk_queue);
566 dev_info->gd->queue = NULL;
567 put_disk(dev_info->gd);
568 device_unregister(&dev_info->dev);
569 segment_unload(dev_info->segment_name);
570 PRINT_DEBUG("Segment %s unloaded successfully\n",
571 dev_info->segment_name);
572 put_device(&dev_info->dev);
573 up_write(&dcssblk_devices_sem);
582 dcssblk_open(struct inode *inode, struct file *filp)
584 struct dcssblk_dev_info *dev_info;
587 dev_info = inode->i_bdev->bd_disk->private_data;
588 if (NULL == dev_info) {
592 atomic_inc(&dev_info->use_count);
593 inode->i_bdev->bd_block_size = 4096;
600 dcssblk_release(struct inode *inode, struct file *filp)
602 struct dcssblk_dev_info *dev_info;
605 dev_info = inode->i_bdev->bd_disk->private_data;
606 if (NULL == dev_info) {
610 down_write(&dcssblk_devices_sem);
611 if (atomic_dec_and_test(&dev_info->use_count)
612 && (dev_info->save_pending)) {
613 PRINT_INFO("Segment %s became idle and is being saved now\n",
614 dev_info->segment_name);
615 segment_save(dev_info->segment_name);
616 dev_info->save_pending = 0;
618 up_write(&dcssblk_devices_sem);
625 dcssblk_make_request(request_queue_t *q, struct bio *bio)
627 struct dcssblk_dev_info *dev_info;
628 struct bio_vec *bvec;
630 unsigned long page_addr;
631 unsigned long source_addr;
632 unsigned long bytes_done;
636 dev_info = bio->bi_bdev->bd_disk->private_data;
637 if (dev_info == NULL)
639 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
640 /* Request is not page-aligned. */
642 if (((bio->bi_size >> 9) + bio->bi_sector)
643 > get_capacity(bio->bi_bdev->bd_disk)) {
644 /* Request beyond end of DCSS segment. */
647 /* verify data transfer direction */
648 if (dev_info->is_shared) {
649 switch (dev_info->segment_type) {
653 /* cannot write to these segments */
654 if (bio_data_dir(bio) == WRITE) {
655 PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
661 index = (bio->bi_sector >> 3);
662 bio_for_each_segment(bvec, bio, i) {
663 page_addr = (unsigned long)
664 page_address(bvec->bv_page) + bvec->bv_offset;
665 source_addr = dev_info->start + (index<<12) + bytes_done;
666 if (unlikely(page_addr & 4095) != 0 || (bvec->bv_len & 4095) != 0)
669 if (bio_data_dir(bio) == READ) {
670 memcpy((void*)page_addr, (void*)source_addr,
673 memcpy((void*)source_addr, (void*)page_addr,
676 bytes_done += bvec->bv_len;
678 bio_endio(bio, bytes_done, 0);
681 bio_io_error(bio, bio->bi_size);
686 dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
689 struct dcssblk_dev_info *dev_info;
692 dev_info = bdev->bd_disk->private_data;
695 if (secnum % (PAGE_SIZE/512))
697 pgoff = secnum / (PAGE_SIZE / 512);
698 if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
700 *data = (unsigned long) (dev_info->start+pgoff*PAGE_SIZE);
705 dcssblk_check_params(void)
709 struct dcssblk_dev_info *dev_info;
711 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
713 for (j = i; (dcssblk_segments[j] != ',') &&
714 (dcssblk_segments[j] != '\0') &&
715 (dcssblk_segments[j] != '(') &&
718 buf[j-i] = dcssblk_segments[j];
721 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
722 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
723 for (k = 0; buf[k] != '\0'; k++)
724 buf[k] = toupper(buf[k]);
725 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
726 down_read(&dcssblk_devices_sem);
727 dev_info = dcssblk_get_device_by_name(buf);
728 up_read(&dcssblk_devices_sem);
730 dcssblk_shared_store(&dev_info->dev,
734 while ((dcssblk_segments[j] != ',') &&
735 (dcssblk_segments[j] != '\0'))
739 if (dcssblk_segments[j] == '\0')
746 * The init/exit functions.
753 PRINT_DEBUG("DCSSBLOCK EXIT...\n");
754 s390_root_dev_unregister(dcssblk_root_dev);
755 rc = unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
757 PRINT_ERR("unregister_blkdev() failed!\n");
759 PRINT_DEBUG("...finished!\n");
767 PRINT_DEBUG("DCSSBLOCK INIT...\n");
768 dcssblk_root_dev = s390_root_dev_register("dcssblk");
769 if (IS_ERR(dcssblk_root_dev)) {
770 PRINT_ERR("device_register() failed!\n");
771 return PTR_ERR(dcssblk_root_dev);
773 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
775 PRINT_ERR("device_create_file(add) failed!\n");
776 s390_root_dev_unregister(dcssblk_root_dev);
779 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
781 PRINT_ERR("device_create_file(remove) failed!\n");
782 s390_root_dev_unregister(dcssblk_root_dev);
785 rc = register_blkdev(0, DCSSBLK_NAME);
787 PRINT_ERR("Can't get dynamic major!\n");
788 s390_root_dev_unregister(dcssblk_root_dev);
792 init_rwsem(&dcssblk_devices_sem);
794 dcssblk_check_params();
796 PRINT_DEBUG("...finished!\n");
800 module_init(dcssblk_init);
801 module_exit(dcssblk_exit);
803 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
804 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
805 "comma-separated list, each name max. 8 chars.\n"
806 "Adding \"(local)\" to segment name equals echoing 0 to "
807 "/sys/devices/dcssblk/<segment name>/shared after loading "
809 "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
811 MODULE_LICENSE("GPL");