2 * dcssblk.c -- the S/390 block driver for dcss memory
4 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
7 #define KMSG_COMPONENT "dcssblk"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/ctype.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/blkdev.h>
17 #include <linux/completion.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <asm/extmem.h>
23 #define DCSSBLK_NAME "dcssblk"
24 #define DCSSBLK_MINORS_PER_DISK 1
25 #define DCSSBLK_PARM_LEN 400
26 #define DCSS_BUS_ID_SIZE 20
28 static int dcssblk_open(struct block_device
*bdev
, fmode_t mode
);
29 static void dcssblk_release(struct gendisk
*disk
, fmode_t mode
);
30 static blk_qc_t
dcssblk_make_request(struct request_queue
*q
,
32 static long dcssblk_direct_access(struct block_device
*bdev
, sector_t secnum
,
33 void __pmem
**kaddr
, unsigned long *pfn
);
35 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
37 static int dcssblk_major
;
38 static const struct block_device_operations dcssblk_devops
= {
41 .release
= dcssblk_release
,
42 .direct_access
= dcssblk_direct_access
,
45 struct dcssblk_dev_info
{
48 char segment_name
[DCSS_BUS_ID_SIZE
];
54 unsigned char save_pending
;
55 unsigned char is_shared
;
56 struct request_queue
*dcssblk_queue
;
58 struct list_head seg_list
;
63 char segment_name
[DCSS_BUS_ID_SIZE
];
69 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
71 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
74 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
75 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
77 static struct device
*dcssblk_root_dev
;
79 static LIST_HEAD(dcssblk_devices
);
80 static struct rw_semaphore dcssblk_devices_sem
;
83 * release function for segment device.
86 dcssblk_release_segment(struct device
*dev
)
88 struct dcssblk_dev_info
*dev_info
;
89 struct segment_info
*entry
, *temp
;
91 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
92 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
97 module_put(THIS_MODULE
);
101 * get a minor number. needs to be called with
102 * down_write(&dcssblk_devices_sem) and the
103 * device needs to be enqueued before the semaphore is
107 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
110 struct dcssblk_dev_info
*entry
;
112 if (dev_info
== NULL
)
114 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
116 // test if minor available
117 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
118 if (minor
== entry
->gd
->first_minor
)
120 if (!found
) break; // got unused minor
124 dev_info
->gd
->first_minor
= minor
;
129 * get the struct dcssblk_dev_info from dcssblk_devices
130 * for the given name.
131 * down_read(&dcssblk_devices_sem) must be held.
133 static struct dcssblk_dev_info
*
134 dcssblk_get_device_by_name(char *name
)
136 struct dcssblk_dev_info
*entry
;
138 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
139 if (!strcmp(name
, entry
->segment_name
)) {
147 * get the struct segment_info from seg_list
148 * for the given name.
149 * down_read(&dcssblk_devices_sem) must be held.
151 static struct segment_info
*
152 dcssblk_get_segment_by_name(char *name
)
154 struct dcssblk_dev_info
*dev_info
;
155 struct segment_info
*entry
;
157 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
158 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
159 if (!strcmp(name
, entry
->segment_name
))
167 * get the highest address of the multi-segment block.
170 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
172 unsigned long highest_addr
;
173 struct segment_info
*entry
;
176 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
177 if (highest_addr
< entry
->end
)
178 highest_addr
= entry
->end
;
184 * get the lowest address of the multi-segment block.
187 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
190 unsigned long lowest_addr
;
191 struct segment_info
*entry
;
195 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
196 if (set_first
== 0) {
197 lowest_addr
= entry
->start
;
200 if (lowest_addr
> entry
->start
)
201 lowest_addr
= entry
->start
;
208 * Check continuity of segments.
211 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
214 struct segment_info
*sort_list
, *entry
, temp
;
216 if (dev_info
->num_of_segments
<= 1)
220 sizeof(struct segment_info
) * dev_info
->num_of_segments
,
222 if (sort_list
== NULL
)
225 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
226 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
231 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
232 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
233 if (sort_list
[j
].start
> sort_list
[i
].start
) {
234 memcpy(&temp
, &sort_list
[i
],
235 sizeof(struct segment_info
));
236 memcpy(&sort_list
[i
], &sort_list
[j
],
237 sizeof(struct segment_info
));
238 memcpy(&sort_list
[j
], &temp
,
239 sizeof(struct segment_info
));
242 /* check continuity */
243 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
244 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
245 pr_err("Adjacent DCSSs %s and %s are not "
246 "contiguous\n", sort_list
[i
].segment_name
,
247 sort_list
[i
+1].segment_name
);
251 /* EN and EW are allowed in a block device */
252 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
253 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
254 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
255 !(sort_list
[i
+1].segment_type
&
256 SEGMENT_EXCLUSIVE
) ||
257 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
258 pr_err("DCSS %s and DCSS %s have "
259 "incompatible types\n",
260 sort_list
[i
].segment_name
,
261 sort_list
[i
+1].segment_name
);
277 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
281 /* already loaded? */
282 down_read(&dcssblk_devices_sem
);
283 *seg_info
= dcssblk_get_segment_by_name(name
);
284 up_read(&dcssblk_devices_sem
);
285 if (*seg_info
!= NULL
)
288 /* get a struct segment_info */
289 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
290 if (*seg_info
== NULL
)
293 strcpy((*seg_info
)->segment_name
, name
);
295 /* load the segment */
296 rc
= segment_load(name
, SEGMENT_SHARED
,
297 &(*seg_info
)->start
, &(*seg_info
)->end
);
299 segment_warning(rc
, (*seg_info
)->segment_name
);
302 INIT_LIST_HEAD(&(*seg_info
)->lh
);
303 (*seg_info
)->segment_type
= rc
;
309 * device attribute for switching shared/nonshared (exclusive)
310 * operation (show + store)
313 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
315 struct dcssblk_dev_info
*dev_info
;
317 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
318 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
322 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
324 struct dcssblk_dev_info
*dev_info
;
325 struct segment_info
*entry
, *temp
;
328 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
330 down_write(&dcssblk_devices_sem
);
331 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
332 if (atomic_read(&dev_info
->use_count
)) {
336 if (inbuf
[0] == '1') {
337 /* reload segments in shared mode */
338 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
339 rc
= segment_modify_shared(entry
->segment_name
,
342 BUG_ON(rc
== -EINVAL
);
347 dev_info
->is_shared
= 1;
348 switch (dev_info
->segment_type
) {
352 set_disk_ro(dev_info
->gd
, 1);
354 } else if (inbuf
[0] == '0') {
355 /* reload segments in exclusive mode */
356 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
357 pr_err("DCSS %s is of type SC and cannot be "
358 "loaded as exclusive-writable\n",
359 dev_info
->segment_name
);
363 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
364 rc
= segment_modify_shared(entry
->segment_name
,
367 BUG_ON(rc
== -EINVAL
);
372 dev_info
->is_shared
= 0;
373 set_disk_ro(dev_info
->gd
, 0);
382 pr_err("DCSS device %s is removed after a failed access mode "
383 "change\n", dev_info
->segment_name
);
385 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
387 segment_unload(entry
->segment_name
);
389 list_del(&dev_info
->lh
);
391 del_gendisk(dev_info
->gd
);
392 blk_cleanup_queue(dev_info
->dcssblk_queue
);
393 dev_info
->gd
->queue
= NULL
;
394 put_disk(dev_info
->gd
);
395 up_write(&dcssblk_devices_sem
);
397 if (device_remove_file_self(dev
, attr
)) {
398 device_unregister(dev
);
403 up_write(&dcssblk_devices_sem
);
406 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
407 dcssblk_shared_store
);
410 * device attribute for save operation on current copy
411 * of the segment. If the segment is busy, saving will
412 * become pending until it gets released, which can be
413 * undone by storing a non-true value to this entry.
417 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
419 struct dcssblk_dev_info
*dev_info
;
421 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
422 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
426 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
428 struct dcssblk_dev_info
*dev_info
;
429 struct segment_info
*entry
;
431 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
433 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
435 down_write(&dcssblk_devices_sem
);
436 if (inbuf
[0] == '1') {
437 if (atomic_read(&dev_info
->use_count
) == 0) {
438 // device is idle => we save immediately
439 pr_info("All DCSSs that map to device %s are "
440 "saved\n", dev_info
->segment_name
);
441 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
442 if (entry
->segment_type
== SEG_TYPE_EN
||
443 entry
->segment_type
== SEG_TYPE_SN
)
444 pr_warn("DCSS %s is of type SN or EN"
445 " and cannot be saved\n",
446 entry
->segment_name
);
448 segment_save(entry
->segment_name
);
451 // device is busy => we save it when it becomes
452 // idle in dcssblk_release
453 pr_info("Device %s is in use, its DCSSs will be "
454 "saved when it becomes idle\n",
455 dev_info
->segment_name
);
456 dev_info
->save_pending
= 1;
458 } else if (inbuf
[0] == '0') {
459 if (dev_info
->save_pending
) {
460 // device is busy & the user wants to undo his save
462 dev_info
->save_pending
= 0;
463 pr_info("A pending save request for device %s "
464 "has been canceled\n",
465 dev_info
->segment_name
);
468 up_write(&dcssblk_devices_sem
);
471 up_write(&dcssblk_devices_sem
);
474 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
478 * device attribute for showing all segments in a device
481 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
486 struct dcssblk_dev_info
*dev_info
;
487 struct segment_info
*entry
;
489 down_read(&dcssblk_devices_sem
);
490 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
493 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
494 strcpy(&buf
[i
], entry
->segment_name
);
495 i
+= strlen(entry
->segment_name
);
499 up_read(&dcssblk_devices_sem
);
502 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
504 static struct attribute
*dcssblk_dev_attrs
[] = {
505 &dev_attr_shared
.attr
,
507 &dev_attr_seglist
.attr
,
510 static struct attribute_group dcssblk_dev_attr_group
= {
511 .attrs
= dcssblk_dev_attrs
,
513 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
514 &dcssblk_dev_attr_group
,
519 * device attribute for adding devices
522 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
524 int rc
, i
, j
, num_of_segments
;
525 struct dcssblk_dev_info
*dev_info
;
526 struct segment_info
*seg_info
, *temp
;
528 unsigned long seg_byte_size
;
532 if (dev
!= dcssblk_root_dev
) {
536 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
541 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
542 if (local_buf
== NULL
) {
551 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
552 for (j
= i
; j
< count
&&
555 (buf
[j
] != '\n'); j
++) {
556 local_buf
[j
-i
] = toupper(buf
[j
]);
558 local_buf
[j
-i
] = '\0';
559 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
564 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
568 * get a struct dcssblk_dev_info
570 if (num_of_segments
== 0) {
571 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
573 if (dev_info
== NULL
) {
577 strcpy(dev_info
->segment_name
, local_buf
);
578 dev_info
->segment_type
= seg_info
->segment_type
;
579 INIT_LIST_HEAD(&dev_info
->seg_list
);
581 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
585 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
589 /* no trailing colon at the end of the input */
590 if ((i
> 0) && (buf
[i
-1] == ':')) {
594 strlcpy(local_buf
, buf
, i
+ 1);
595 dev_info
->num_of_segments
= num_of_segments
;
596 rc
= dcssblk_is_continuous(dev_info
);
600 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
601 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
603 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
604 dev_info
->dev
.release
= dcssblk_release_segment
;
605 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
606 INIT_LIST_HEAD(&dev_info
->lh
);
607 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
608 if (dev_info
->gd
== NULL
) {
612 dev_info
->gd
->major
= dcssblk_major
;
613 dev_info
->gd
->fops
= &dcssblk_devops
;
614 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
615 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
616 dev_info
->gd
->private_data
= dev_info
;
617 dev_info
->gd
->driverfs_dev
= &dev_info
->dev
;
618 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
619 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
621 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
622 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
623 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
624 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
626 dev_info
->save_pending
= 0;
627 dev_info
->is_shared
= 1;
628 dev_info
->dev
.parent
= dcssblk_root_dev
;
631 *get minor, add to list
633 down_write(&dcssblk_devices_sem
);
634 if (dcssblk_get_segment_by_name(local_buf
)) {
638 rc
= dcssblk_assign_free_minor(dev_info
);
641 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
642 dev_info
->gd
->first_minor
);
643 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
645 if (!try_module_get(THIS_MODULE
)) {
650 * register the device
652 rc
= device_register(&dev_info
->dev
);
656 get_device(&dev_info
->dev
);
657 add_disk(dev_info
->gd
);
659 switch (dev_info
->segment_type
) {
663 set_disk_ro(dev_info
->gd
,1);
666 set_disk_ro(dev_info
->gd
,0);
669 up_write(&dcssblk_devices_sem
);
674 list_del(&dev_info
->lh
);
675 blk_cleanup_queue(dev_info
->dcssblk_queue
);
676 dev_info
->gd
->queue
= NULL
;
677 put_disk(dev_info
->gd
);
678 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
679 segment_unload(seg_info
->segment_name
);
681 put_device(&dev_info
->dev
);
682 up_write(&dcssblk_devices_sem
);
685 list_del(&dev_info
->lh
);
687 blk_cleanup_queue(dev_info
->dcssblk_queue
);
688 dev_info
->gd
->queue
= NULL
;
689 put_disk(dev_info
->gd
);
690 up_write(&dcssblk_devices_sem
);
692 if (dev_info
== NULL
)
694 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
695 list_del(&seg_info
->lh
);
696 segment_unload(seg_info
->segment_name
);
707 * device attribute for removing devices
710 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
712 struct dcssblk_dev_info
*dev_info
;
713 struct segment_info
*entry
;
717 if (dev
!= dcssblk_root_dev
) {
720 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
721 if (local_buf
== NULL
) {
727 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
728 local_buf
[i
] = toupper(buf
[i
]);
731 if ((i
== 0) || (i
> 8)) {
736 down_write(&dcssblk_devices_sem
);
737 dev_info
= dcssblk_get_device_by_name(local_buf
);
738 if (dev_info
== NULL
) {
739 up_write(&dcssblk_devices_sem
);
740 pr_warning("Device %s cannot be removed because it is not a "
741 "known device\n", local_buf
);
745 if (atomic_read(&dev_info
->use_count
) != 0) {
746 up_write(&dcssblk_devices_sem
);
747 pr_warning("Device %s cannot be removed while it is in "
753 list_del(&dev_info
->lh
);
754 del_gendisk(dev_info
->gd
);
755 blk_cleanup_queue(dev_info
->dcssblk_queue
);
756 dev_info
->gd
->queue
= NULL
;
757 put_disk(dev_info
->gd
);
758 device_unregister(&dev_info
->dev
);
760 /* unload all related segments */
761 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
762 segment_unload(entry
->segment_name
);
764 put_device(&dev_info
->dev
);
765 up_write(&dcssblk_devices_sem
);
774 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
776 struct dcssblk_dev_info
*dev_info
;
779 dev_info
= bdev
->bd_disk
->private_data
;
780 if (NULL
== dev_info
) {
784 atomic_inc(&dev_info
->use_count
);
785 bdev
->bd_block_size
= 4096;
792 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
794 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
795 struct segment_info
*entry
;
801 down_write(&dcssblk_devices_sem
);
802 if (atomic_dec_and_test(&dev_info
->use_count
)
803 && (dev_info
->save_pending
)) {
804 pr_info("Device %s has become idle and is being saved "
805 "now\n", dev_info
->segment_name
);
806 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
807 if (entry
->segment_type
== SEG_TYPE_EN
||
808 entry
->segment_type
== SEG_TYPE_SN
)
809 pr_warn("DCSS %s is of type SN or EN and cannot"
810 " be saved\n", entry
->segment_name
);
812 segment_save(entry
->segment_name
);
814 dev_info
->save_pending
= 0;
816 up_write(&dcssblk_devices_sem
);
820 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
822 struct dcssblk_dev_info
*dev_info
;
824 struct bvec_iter iter
;
826 unsigned long page_addr
;
827 unsigned long source_addr
;
828 unsigned long bytes_done
;
830 blk_queue_split(q
, &bio
, q
->bio_split
);
833 dev_info
= bio
->bi_bdev
->bd_disk
->private_data
;
834 if (dev_info
== NULL
)
836 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
837 (bio
->bi_iter
.bi_size
& 4095) != 0)
838 /* Request is not page-aligned. */
840 if (bio_end_sector(bio
) > get_capacity(bio
->bi_bdev
->bd_disk
)) {
841 /* Request beyond end of DCSS segment. */
844 /* verify data transfer direction */
845 if (dev_info
->is_shared
) {
846 switch (dev_info
->segment_type
) {
850 /* cannot write to these segments */
851 if (bio_data_dir(bio
) == WRITE
) {
852 pr_warning("Writing to %s failed because it "
853 "is a read-only device\n",
854 dev_name(&dev_info
->dev
));
860 index
= (bio
->bi_iter
.bi_sector
>> 3);
861 bio_for_each_segment(bvec
, bio
, iter
) {
862 page_addr
= (unsigned long)
863 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
864 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
865 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
868 if (bio_data_dir(bio
) == READ
) {
869 memcpy((void*)page_addr
, (void*)source_addr
,
872 memcpy((void*)source_addr
, (void*)page_addr
,
875 bytes_done
+= bvec
.bv_len
;
878 return BLK_QC_T_NONE
;
881 return BLK_QC_T_NONE
;
885 dcssblk_direct_access (struct block_device
*bdev
, sector_t secnum
,
886 void __pmem
**kaddr
, unsigned long *pfn
)
888 struct dcssblk_dev_info
*dev_info
;
889 unsigned long offset
, dev_sz
;
892 dev_info
= bdev
->bd_disk
->private_data
;
895 dev_sz
= dev_info
->end
- dev_info
->start
;
896 offset
= secnum
* 512;
897 addr
= (void *) (dev_info
->start
+ offset
);
898 *pfn
= virt_to_phys(addr
) >> PAGE_SHIFT
;
899 *kaddr
= (void __pmem
*) addr
;
901 return dev_sz
- offset
;
905 dcssblk_check_params(void)
908 char buf
[DCSSBLK_PARM_LEN
+ 1];
909 struct dcssblk_dev_info
*dev_info
;
911 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
913 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
914 (dcssblk_segments
[j
] != ',') &&
915 (dcssblk_segments
[j
] != '\0') &&
916 (dcssblk_segments
[j
] != '('); j
++)
918 buf
[j
-i
] = dcssblk_segments
[j
];
921 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
922 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
923 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
924 buf
[k
] = toupper(buf
[k
]);
926 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
927 down_read(&dcssblk_devices_sem
);
928 dev_info
= dcssblk_get_device_by_name(buf
);
929 up_read(&dcssblk_devices_sem
);
931 dcssblk_shared_store(&dev_info
->dev
,
935 while ((dcssblk_segments
[j
] != ',') &&
936 (dcssblk_segments
[j
] != '\0'))
940 if (dcssblk_segments
[j
] == '\0')
949 static int dcssblk_freeze(struct device
*dev
)
951 struct dcssblk_dev_info
*dev_info
;
954 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
955 switch (dev_info
->segment_type
) {
959 if (!dev_info
->is_shared
)
970 pr_err("Suspending the system failed because DCSS device %s "
972 dev_info
->segment_name
);
976 static int dcssblk_restore(struct device
*dev
)
978 struct dcssblk_dev_info
*dev_info
;
979 struct segment_info
*entry
;
980 unsigned long start
, end
;
983 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
984 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
985 segment_unload(entry
->segment_name
);
986 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
989 // TODO in_use check ?
990 segment_warning(rc
, entry
->segment_name
);
993 if (start
!= entry
->start
|| end
!= entry
->end
) {
994 pr_err("The address range of DCSS %s changed "
995 "while the system was suspended\n",
996 entry
->segment_name
);
1003 panic("fatal dcssblk resume error\n");
1006 static int dcssblk_thaw(struct device
*dev
)
1011 static const struct dev_pm_ops dcssblk_pm_ops
= {
1012 .freeze
= dcssblk_freeze
,
1013 .thaw
= dcssblk_thaw
,
1014 .restore
= dcssblk_restore
,
1017 static struct platform_driver dcssblk_pdrv
= {
1020 .pm
= &dcssblk_pm_ops
,
1024 static struct platform_device
*dcssblk_pdev
;
1028 * The init/exit functions.
1033 platform_device_unregister(dcssblk_pdev
);
1034 platform_driver_unregister(&dcssblk_pdrv
);
1035 root_device_unregister(dcssblk_root_dev
);
1036 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1044 rc
= platform_driver_register(&dcssblk_pdrv
);
1048 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1050 if (IS_ERR(dcssblk_pdev
)) {
1051 rc
= PTR_ERR(dcssblk_pdev
);
1055 dcssblk_root_dev
= root_device_register("dcssblk");
1056 if (IS_ERR(dcssblk_root_dev
)) {
1057 rc
= PTR_ERR(dcssblk_root_dev
);
1060 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1063 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1066 rc
= register_blkdev(0, DCSSBLK_NAME
);
1070 init_rwsem(&dcssblk_devices_sem
);
1072 dcssblk_check_params();
1076 root_device_unregister(dcssblk_root_dev
);
1078 platform_device_unregister(dcssblk_pdev
);
1080 platform_driver_unregister(&dcssblk_pdrv
);
1084 module_init(dcssblk_init
);
1085 module_exit(dcssblk_exit
);
1087 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1088 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1089 "comma-separated list, names in each set separated "
1090 "by commas are separated by colons, each set contains "
1091 "names of contiguous segments and each name max. 8 chars.\n"
1092 "Adding \"(local)\" to the end of each set equals echoing 0 "
1093 "to /sys/devices/dcssblk/<device name>/shared after loading "
1094 "the contiguous segments - \n"
1095 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1097 MODULE_LICENSE("GPL");