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,
39 void **kaddr, unsigned long *pfn);
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'))
172 down_write(&dcssblk_devices_sem);
173 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
174 if (atomic_read(&dev_info->use_count)) {
175 PRINT_ERR("share: segment %s is busy!\n",
176 dev_info->segment_name);
180 if (inbuf[0] == '1') {
181 // reload segment in shared mode
182 rc = segment_modify_shared(dev_info->segment_name,
185 BUG_ON(rc == -EINVAL);
189 dev_info->is_shared = 1;
190 switch (dev_info->segment_type) {
194 set_disk_ro(dev_info->gd,1);
197 } else if (inbuf[0] == '0') {
198 // reload segment in exclusive mode
199 if (dev_info->segment_type == SEG_TYPE_SC) {
200 PRINT_ERR("Segment type SC (%s) cannot be loaded in "
201 "non-shared mode\n", dev_info->segment_name);
205 rc = segment_modify_shared(dev_info->segment_name,
208 BUG_ON(rc == -EINVAL);
212 dev_info->is_shared = 0;
213 set_disk_ro(dev_info->gd, 0);
223 PRINT_ERR("Could not reload segment %s, removing it now!\n",
224 dev_info->segment_name);
225 list_del(&dev_info->lh);
227 del_gendisk(dev_info->gd);
228 blk_cleanup_queue(dev_info->dcssblk_queue);
229 dev_info->gd->queue = NULL;
230 put_disk(dev_info->gd);
231 rc = device_schedule_callback(dev, dcssblk_unregister_callback);
233 up_write(&dcssblk_devices_sem);
238 * device attribute for save operation on current copy
239 * of the segment. If the segment is busy, saving will
240 * become pending until it gets released, which can be
241 * undone by storing a non-true value to this entry.
245 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
247 struct dcssblk_dev_info *dev_info;
249 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
250 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
254 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
256 struct dcssblk_dev_info *dev_info;
258 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
260 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
262 down_write(&dcssblk_devices_sem);
263 if (inbuf[0] == '1') {
264 if (atomic_read(&dev_info->use_count) == 0) {
265 // device is idle => we save immediately
266 PRINT_INFO("Saving segment %s\n",
267 dev_info->segment_name);
268 segment_save(dev_info->segment_name);
270 // device is busy => we save it when it becomes
271 // idle in dcssblk_release
272 PRINT_INFO("Segment %s is currently busy, it will "
273 "be saved when it becomes idle...\n",
274 dev_info->segment_name);
275 dev_info->save_pending = 1;
277 } else if (inbuf[0] == '0') {
278 if (dev_info->save_pending) {
279 // device is busy & the user wants to undo his save
281 dev_info->save_pending = 0;
282 PRINT_INFO("Pending save for segment %s deactivated\n",
283 dev_info->segment_name);
286 up_write(&dcssblk_devices_sem);
289 up_write(&dcssblk_devices_sem);
294 * device attribute for adding devices
297 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
300 struct dcssblk_dev_info *dev_info;
302 unsigned long seg_byte_size;
305 if (dev != dcssblk_root_dev) {
309 local_buf = kmalloc(count + 1, GFP_KERNEL);
310 if (local_buf == NULL) {
317 for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
318 local_buf[i] = toupper(buf[i]);
321 if ((i == 0) || (i > 8)) {
328 down_read(&dcssblk_devices_sem);
329 dev_info = dcssblk_get_device_by_name(local_buf);
330 up_read(&dcssblk_devices_sem);
331 if (dev_info != NULL) {
332 PRINT_WARN("Segment %s already loaded!\n", local_buf);
337 * get a struct dcssblk_dev_info
339 dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
340 if (dev_info == NULL) {
345 strcpy(dev_info->segment_name, local_buf);
346 strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
347 dev_info->dev.release = dcssblk_release_segment;
348 INIT_LIST_HEAD(&dev_info->lh);
350 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
351 if (dev_info->gd == NULL) {
355 dev_info->gd->major = dcssblk_major;
356 dev_info->gd->fops = &dcssblk_devops;
357 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
358 dev_info->gd->queue = dev_info->dcssblk_queue;
359 dev_info->gd->private_data = dev_info;
360 dev_info->gd->driverfs_dev = &dev_info->dev;
361 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
362 blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
366 rc = segment_load(local_buf, SEGMENT_SHARED,
367 &dev_info->start, &dev_info->end);
369 segment_warning(rc, dev_info->segment_name);
370 goto dealloc_gendisk;
372 seg_byte_size = (dev_info->end - dev_info->start + 1);
373 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
374 PRINT_INFO("Loaded segment %s, size = %lu Byte, "
375 "capacity = %lu (512 Byte) sectors\n", local_buf,
376 seg_byte_size, seg_byte_size >> 9);
378 dev_info->segment_type = rc;
379 dev_info->save_pending = 0;
380 dev_info->is_shared = 1;
381 dev_info->dev.parent = dcssblk_root_dev;
384 * get minor, add to list
386 down_write(&dcssblk_devices_sem);
387 if (dcssblk_get_segment_by_name(local_buf)) {
391 rc = dcssblk_assign_free_minor(dev_info);
393 up_write(&dcssblk_devices_sem);
394 PRINT_ERR("No free minor number available! "
395 "Unloading segment...\n");
398 sprintf(dev_info->gd->disk_name, "dcssblk%d",
399 dev_info->gd->first_minor);
400 list_add_tail(&dev_info->lh, &dcssblk_devices);
402 if (!try_module_get(THIS_MODULE)) {
407 * register the device
409 rc = device_register(&dev_info->dev);
411 PRINT_ERR("Segment %s could not be registered RC=%d\n",
413 module_put(THIS_MODULE);
416 get_device(&dev_info->dev);
417 rc = device_create_file(&dev_info->dev, &dev_attr_shared);
420 rc = device_create_file(&dev_info->dev, &dev_attr_save);
424 add_disk(dev_info->gd);
426 switch (dev_info->segment_type) {
430 set_disk_ro(dev_info->gd,1);
433 set_disk_ro(dev_info->gd,0);
436 PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
437 up_write(&dcssblk_devices_sem);
442 list_del(&dev_info->lh);
443 blk_cleanup_queue(dev_info->dcssblk_queue);
444 dev_info->gd->queue = NULL;
445 put_disk(dev_info->gd);
446 device_unregister(&dev_info->dev);
447 segment_unload(dev_info->segment_name);
448 put_device(&dev_info->dev);
449 up_write(&dcssblk_devices_sem);
452 list_del(&dev_info->lh);
453 up_write(&dcssblk_devices_sem);
455 segment_unload(local_buf);
457 blk_cleanup_queue(dev_info->dcssblk_queue);
458 dev_info->gd->queue = NULL;
459 put_disk(dev_info->gd);
469 * device attribute for removing devices
472 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
474 struct dcssblk_dev_info *dev_info;
478 if (dev != dcssblk_root_dev) {
481 local_buf = kmalloc(count + 1, GFP_KERNEL);
482 if (local_buf == NULL) {
488 for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
489 local_buf[i] = toupper(buf[i]);
492 if ((i == 0) || (i > 8)) {
497 down_write(&dcssblk_devices_sem);
498 dev_info = dcssblk_get_device_by_name(local_buf);
499 if (dev_info == NULL) {
500 up_write(&dcssblk_devices_sem);
501 PRINT_WARN("Segment %s is not loaded!\n", local_buf);
505 if (atomic_read(&dev_info->use_count) != 0) {
506 up_write(&dcssblk_devices_sem);
507 PRINT_WARN("Segment %s is in use!\n", local_buf);
511 list_del(&dev_info->lh);
513 del_gendisk(dev_info->gd);
514 blk_cleanup_queue(dev_info->dcssblk_queue);
515 dev_info->gd->queue = NULL;
516 put_disk(dev_info->gd);
517 device_unregister(&dev_info->dev);
518 segment_unload(dev_info->segment_name);
519 PRINT_DEBUG("Segment %s unloaded successfully\n",
520 dev_info->segment_name);
521 put_device(&dev_info->dev);
522 up_write(&dcssblk_devices_sem);
531 dcssblk_open(struct inode *inode, struct file *filp)
533 struct dcssblk_dev_info *dev_info;
536 dev_info = inode->i_bdev->bd_disk->private_data;
537 if (NULL == dev_info) {
541 atomic_inc(&dev_info->use_count);
542 inode->i_bdev->bd_block_size = 4096;
549 dcssblk_release(struct inode *inode, struct file *filp)
551 struct dcssblk_dev_info *dev_info;
554 dev_info = inode->i_bdev->bd_disk->private_data;
555 if (NULL == dev_info) {
559 down_write(&dcssblk_devices_sem);
560 if (atomic_dec_and_test(&dev_info->use_count)
561 && (dev_info->save_pending)) {
562 PRINT_INFO("Segment %s became idle and is being saved now\n",
563 dev_info->segment_name);
564 segment_save(dev_info->segment_name);
565 dev_info->save_pending = 0;
567 up_write(&dcssblk_devices_sem);
574 dcssblk_make_request(struct request_queue *q, struct bio *bio)
576 struct dcssblk_dev_info *dev_info;
577 struct bio_vec *bvec;
579 unsigned long page_addr;
580 unsigned long source_addr;
581 unsigned long bytes_done;
585 dev_info = bio->bi_bdev->bd_disk->private_data;
586 if (dev_info == NULL)
588 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
589 /* Request is not page-aligned. */
591 if (((bio->bi_size >> 9) + bio->bi_sector)
592 > get_capacity(bio->bi_bdev->bd_disk)) {
593 /* Request beyond end of DCSS segment. */
596 /* verify data transfer direction */
597 if (dev_info->is_shared) {
598 switch (dev_info->segment_type) {
602 /* cannot write to these segments */
603 if (bio_data_dir(bio) == WRITE) {
604 PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
610 index = (bio->bi_sector >> 3);
611 bio_for_each_segment(bvec, bio, i) {
612 page_addr = (unsigned long)
613 page_address(bvec->bv_page) + bvec->bv_offset;
614 source_addr = dev_info->start + (index<<12) + bytes_done;
615 if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
618 if (bio_data_dir(bio) == READ) {
619 memcpy((void*)page_addr, (void*)source_addr,
622 memcpy((void*)source_addr, (void*)page_addr,
625 bytes_done += bvec->bv_len;
635 dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
636 void **kaddr, unsigned long *pfn)
638 struct dcssblk_dev_info *dev_info;
641 dev_info = bdev->bd_disk->private_data;
644 if (secnum % (PAGE_SIZE/512))
646 pgoff = secnum / (PAGE_SIZE / 512);
647 if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
649 *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
650 *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
656 dcssblk_check_params(void)
660 struct dcssblk_dev_info *dev_info;
662 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
664 for (j = i; (dcssblk_segments[j] != ',') &&
665 (dcssblk_segments[j] != '\0') &&
666 (dcssblk_segments[j] != '(') &&
669 buf[j-i] = dcssblk_segments[j];
672 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
673 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
674 for (k = 0; buf[k] != '\0'; k++)
675 buf[k] = toupper(buf[k]);
676 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
677 down_read(&dcssblk_devices_sem);
678 dev_info = dcssblk_get_device_by_name(buf);
679 up_read(&dcssblk_devices_sem);
681 dcssblk_shared_store(&dev_info->dev,
685 while ((dcssblk_segments[j] != ',') &&
686 (dcssblk_segments[j] != '\0'))
690 if (dcssblk_segments[j] == '\0')
697 * The init/exit functions.
702 s390_root_dev_unregister(dcssblk_root_dev);
703 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
711 dcssblk_root_dev = s390_root_dev_register("dcssblk");
712 if (IS_ERR(dcssblk_root_dev))
713 return PTR_ERR(dcssblk_root_dev);
714 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
716 s390_root_dev_unregister(dcssblk_root_dev);
719 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
721 s390_root_dev_unregister(dcssblk_root_dev);
724 rc = register_blkdev(0, DCSSBLK_NAME);
726 s390_root_dev_unregister(dcssblk_root_dev);
730 init_rwsem(&dcssblk_devices_sem);
732 dcssblk_check_params();
737 module_init(dcssblk_init);
738 module_exit(dcssblk_exit);
740 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
741 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
742 "comma-separated list, each name max. 8 chars.\n"
743 "Adding \"(local)\" to segment name equals echoing 0 to "
744 "/sys/devices/dcssblk/<segment name>/shared after loading "
746 "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
748 MODULE_LICENSE("GPL");