1 // SPDX-License-Identifier: GPL-2.0
3 * dcssblk.c -- the S/390 block driver for dcss memory
5 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
8 #define KMSG_COMPONENT "dcssblk"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/ctype.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/blkdev.h>
18 #include <linux/completion.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/pfn_t.h>
22 #include <linux/uio.h>
23 #include <linux/dax.h>
24 #include <asm/extmem.h>
27 #define DCSSBLK_NAME "dcssblk"
28 #define DCSSBLK_MINORS_PER_DISK 1
29 #define DCSSBLK_PARM_LEN 400
30 #define DCSS_BUS_ID_SIZE 20
32 static int dcssblk_open(struct block_device
*bdev
, fmode_t mode
);
33 static void dcssblk_release(struct gendisk
*disk
, fmode_t mode
);
34 static blk_qc_t
dcssblk_submit_bio(struct bio
*bio
);
35 static long dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
36 long nr_pages
, void **kaddr
, pfn_t
*pfn
);
38 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
40 static int dcssblk_major
;
41 static const struct block_device_operations dcssblk_devops
= {
43 .submit_bio
= dcssblk_submit_bio
,
45 .release
= dcssblk_release
,
48 static size_t dcssblk_dax_copy_from_iter(struct dax_device
*dax_dev
,
49 pgoff_t pgoff
, void *addr
, size_t bytes
, struct iov_iter
*i
)
51 return copy_from_iter(addr
, bytes
, i
);
54 static size_t dcssblk_dax_copy_to_iter(struct dax_device
*dax_dev
,
55 pgoff_t pgoff
, void *addr
, size_t bytes
, struct iov_iter
*i
)
57 return copy_to_iter(addr
, bytes
, i
);
60 static int dcssblk_dax_zero_page_range(struct dax_device
*dax_dev
,
61 pgoff_t pgoff
, size_t nr_pages
)
66 rc
= dax_direct_access(dax_dev
, pgoff
, nr_pages
, &kaddr
, NULL
);
69 memset(kaddr
, 0, nr_pages
<< PAGE_SHIFT
);
70 dax_flush(dax_dev
, kaddr
, nr_pages
<< PAGE_SHIFT
);
74 static const struct dax_operations dcssblk_dax_ops
= {
75 .direct_access
= dcssblk_dax_direct_access
,
76 .dax_supported
= generic_fsdax_supported
,
77 .copy_from_iter
= dcssblk_dax_copy_from_iter
,
78 .copy_to_iter
= dcssblk_dax_copy_to_iter
,
79 .zero_page_range
= dcssblk_dax_zero_page_range
,
82 struct dcssblk_dev_info
{
85 char segment_name
[DCSS_BUS_ID_SIZE
];
91 unsigned char save_pending
;
92 unsigned char is_shared
;
93 struct request_queue
*dcssblk_queue
;
95 struct list_head seg_list
;
96 struct dax_device
*dax_dev
;
101 char segment_name
[DCSS_BUS_ID_SIZE
];
107 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
109 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
112 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
113 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
115 static struct device
*dcssblk_root_dev
;
117 static LIST_HEAD(dcssblk_devices
);
118 static struct rw_semaphore dcssblk_devices_sem
;
121 * release function for segment device.
124 dcssblk_release_segment(struct device
*dev
)
126 struct dcssblk_dev_info
*dev_info
;
127 struct segment_info
*entry
, *temp
;
129 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
130 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
131 list_del(&entry
->lh
);
135 module_put(THIS_MODULE
);
139 * get a minor number. needs to be called with
140 * down_write(&dcssblk_devices_sem) and the
141 * device needs to be enqueued before the semaphore is
145 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
148 struct dcssblk_dev_info
*entry
;
150 if (dev_info
== NULL
)
152 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
154 // test if minor available
155 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
156 if (minor
== entry
->gd
->first_minor
)
158 if (!found
) break; // got unused minor
162 dev_info
->gd
->first_minor
= minor
;
167 * get the struct dcssblk_dev_info from dcssblk_devices
168 * for the given name.
169 * down_read(&dcssblk_devices_sem) must be held.
171 static struct dcssblk_dev_info
*
172 dcssblk_get_device_by_name(char *name
)
174 struct dcssblk_dev_info
*entry
;
176 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
177 if (!strcmp(name
, entry
->segment_name
)) {
185 * get the struct segment_info from seg_list
186 * for the given name.
187 * down_read(&dcssblk_devices_sem) must be held.
189 static struct segment_info
*
190 dcssblk_get_segment_by_name(char *name
)
192 struct dcssblk_dev_info
*dev_info
;
193 struct segment_info
*entry
;
195 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
196 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
197 if (!strcmp(name
, entry
->segment_name
))
205 * get the highest address of the multi-segment block.
208 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
210 unsigned long highest_addr
;
211 struct segment_info
*entry
;
214 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
215 if (highest_addr
< entry
->end
)
216 highest_addr
= entry
->end
;
222 * get the lowest address of the multi-segment block.
225 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
228 unsigned long lowest_addr
;
229 struct segment_info
*entry
;
233 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
234 if (set_first
== 0) {
235 lowest_addr
= entry
->start
;
238 if (lowest_addr
> entry
->start
)
239 lowest_addr
= entry
->start
;
246 * Check continuity of segments.
249 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
252 struct segment_info
*sort_list
, *entry
, temp
;
254 if (dev_info
->num_of_segments
<= 1)
257 sort_list
= kcalloc(dev_info
->num_of_segments
,
258 sizeof(struct segment_info
),
260 if (sort_list
== NULL
)
263 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
264 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
269 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
270 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
271 if (sort_list
[j
].start
> sort_list
[i
].start
) {
272 memcpy(&temp
, &sort_list
[i
],
273 sizeof(struct segment_info
));
274 memcpy(&sort_list
[i
], &sort_list
[j
],
275 sizeof(struct segment_info
));
276 memcpy(&sort_list
[j
], &temp
,
277 sizeof(struct segment_info
));
280 /* check continuity */
281 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
282 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
283 pr_err("Adjacent DCSSs %s and %s are not "
284 "contiguous\n", sort_list
[i
].segment_name
,
285 sort_list
[i
+1].segment_name
);
289 /* EN and EW are allowed in a block device */
290 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
291 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
292 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
293 !(sort_list
[i
+1].segment_type
&
294 SEGMENT_EXCLUSIVE
) ||
295 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
296 pr_err("DCSS %s and DCSS %s have "
297 "incompatible types\n",
298 sort_list
[i
].segment_name
,
299 sort_list
[i
+1].segment_name
);
315 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
319 /* already loaded? */
320 down_read(&dcssblk_devices_sem
);
321 *seg_info
= dcssblk_get_segment_by_name(name
);
322 up_read(&dcssblk_devices_sem
);
323 if (*seg_info
!= NULL
)
326 /* get a struct segment_info */
327 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
328 if (*seg_info
== NULL
)
331 strcpy((*seg_info
)->segment_name
, name
);
333 /* load the segment */
334 rc
= segment_load(name
, SEGMENT_SHARED
,
335 &(*seg_info
)->start
, &(*seg_info
)->end
);
337 segment_warning(rc
, (*seg_info
)->segment_name
);
340 INIT_LIST_HEAD(&(*seg_info
)->lh
);
341 (*seg_info
)->segment_type
= rc
;
347 * device attribute for switching shared/nonshared (exclusive)
348 * operation (show + store)
351 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
353 struct dcssblk_dev_info
*dev_info
;
355 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
356 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
360 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
362 struct dcssblk_dev_info
*dev_info
;
363 struct segment_info
*entry
, *temp
;
366 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
368 down_write(&dcssblk_devices_sem
);
369 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
370 if (atomic_read(&dev_info
->use_count
)) {
374 if (inbuf
[0] == '1') {
375 /* reload segments in shared mode */
376 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
377 rc
= segment_modify_shared(entry
->segment_name
,
380 BUG_ON(rc
== -EINVAL
);
385 dev_info
->is_shared
= 1;
386 switch (dev_info
->segment_type
) {
390 set_disk_ro(dev_info
->gd
, 1);
392 } else if (inbuf
[0] == '0') {
393 /* reload segments in exclusive mode */
394 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
395 pr_err("DCSS %s is of type SC and cannot be "
396 "loaded as exclusive-writable\n",
397 dev_info
->segment_name
);
401 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
402 rc
= segment_modify_shared(entry
->segment_name
,
405 BUG_ON(rc
== -EINVAL
);
410 dev_info
->is_shared
= 0;
411 set_disk_ro(dev_info
->gd
, 0);
420 pr_err("DCSS device %s is removed after a failed access mode "
421 "change\n", dev_info
->segment_name
);
423 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
425 segment_unload(entry
->segment_name
);
427 list_del(&dev_info
->lh
);
429 kill_dax(dev_info
->dax_dev
);
430 put_dax(dev_info
->dax_dev
);
431 del_gendisk(dev_info
->gd
);
432 blk_cleanup_queue(dev_info
->dcssblk_queue
);
433 dev_info
->gd
->queue
= NULL
;
434 put_disk(dev_info
->gd
);
435 up_write(&dcssblk_devices_sem
);
437 if (device_remove_file_self(dev
, attr
)) {
438 device_unregister(dev
);
443 up_write(&dcssblk_devices_sem
);
446 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
447 dcssblk_shared_store
);
450 * device attribute for save operation on current copy
451 * of the segment. If the segment is busy, saving will
452 * become pending until it gets released, which can be
453 * undone by storing a non-true value to this entry.
457 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
459 struct dcssblk_dev_info
*dev_info
;
461 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
462 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
466 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
468 struct dcssblk_dev_info
*dev_info
;
469 struct segment_info
*entry
;
471 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
473 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
475 down_write(&dcssblk_devices_sem
);
476 if (inbuf
[0] == '1') {
477 if (atomic_read(&dev_info
->use_count
) == 0) {
478 // device is idle => we save immediately
479 pr_info("All DCSSs that map to device %s are "
480 "saved\n", dev_info
->segment_name
);
481 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
482 if (entry
->segment_type
== SEG_TYPE_EN
||
483 entry
->segment_type
== SEG_TYPE_SN
)
484 pr_warn("DCSS %s is of type SN or EN"
485 " and cannot be saved\n",
486 entry
->segment_name
);
488 segment_save(entry
->segment_name
);
491 // device is busy => we save it when it becomes
492 // idle in dcssblk_release
493 pr_info("Device %s is in use, its DCSSs will be "
494 "saved when it becomes idle\n",
495 dev_info
->segment_name
);
496 dev_info
->save_pending
= 1;
498 } else if (inbuf
[0] == '0') {
499 if (dev_info
->save_pending
) {
500 // device is busy & the user wants to undo his save
502 dev_info
->save_pending
= 0;
503 pr_info("A pending save request for device %s "
504 "has been canceled\n",
505 dev_info
->segment_name
);
508 up_write(&dcssblk_devices_sem
);
511 up_write(&dcssblk_devices_sem
);
514 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
518 * device attribute for showing all segments in a device
521 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
526 struct dcssblk_dev_info
*dev_info
;
527 struct segment_info
*entry
;
529 down_read(&dcssblk_devices_sem
);
530 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
533 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
534 strcpy(&buf
[i
], entry
->segment_name
);
535 i
+= strlen(entry
->segment_name
);
539 up_read(&dcssblk_devices_sem
);
542 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
544 static struct attribute
*dcssblk_dev_attrs
[] = {
545 &dev_attr_shared
.attr
,
547 &dev_attr_seglist
.attr
,
550 static struct attribute_group dcssblk_dev_attr_group
= {
551 .attrs
= dcssblk_dev_attrs
,
553 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
554 &dcssblk_dev_attr_group
,
559 * device attribute for adding devices
562 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
564 int rc
, i
, j
, num_of_segments
;
565 struct dcssblk_dev_info
*dev_info
;
566 struct segment_info
*seg_info
, *temp
;
568 unsigned long seg_byte_size
;
572 if (dev
!= dcssblk_root_dev
) {
576 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
581 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
582 if (local_buf
== NULL
) {
591 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
592 for (j
= i
; j
< count
&&
595 (buf
[j
] != '\n'); j
++) {
596 local_buf
[j
-i
] = toupper(buf
[j
]);
598 local_buf
[j
-i
] = '\0';
599 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
604 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
608 * get a struct dcssblk_dev_info
610 if (num_of_segments
== 0) {
611 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
613 if (dev_info
== NULL
) {
617 strcpy(dev_info
->segment_name
, local_buf
);
618 dev_info
->segment_type
= seg_info
->segment_type
;
619 INIT_LIST_HEAD(&dev_info
->seg_list
);
621 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
625 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
629 /* no trailing colon at the end of the input */
630 if ((i
> 0) && (buf
[i
-1] == ':')) {
634 strlcpy(local_buf
, buf
, i
+ 1);
635 dev_info
->num_of_segments
= num_of_segments
;
636 rc
= dcssblk_is_continuous(dev_info
);
640 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
641 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
643 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
644 dev_info
->dev
.release
= dcssblk_release_segment
;
645 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
646 INIT_LIST_HEAD(&dev_info
->lh
);
647 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
648 if (dev_info
->gd
== NULL
) {
652 dev_info
->gd
->major
= dcssblk_major
;
653 dev_info
->gd
->fops
= &dcssblk_devops
;
654 dev_info
->dcssblk_queue
= blk_alloc_queue(NUMA_NO_NODE
);
655 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
656 dev_info
->gd
->private_data
= dev_info
;
657 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
658 blk_queue_flag_set(QUEUE_FLAG_DAX
, dev_info
->dcssblk_queue
);
660 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
661 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
662 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
663 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
665 dev_info
->save_pending
= 0;
666 dev_info
->is_shared
= 1;
667 dev_info
->dev
.parent
= dcssblk_root_dev
;
670 *get minor, add to list
672 down_write(&dcssblk_devices_sem
);
673 if (dcssblk_get_segment_by_name(local_buf
)) {
677 rc
= dcssblk_assign_free_minor(dev_info
);
680 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
681 dev_info
->gd
->first_minor
);
682 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
684 if (!try_module_get(THIS_MODULE
)) {
689 * register the device
691 rc
= device_register(&dev_info
->dev
);
695 dev_info
->dax_dev
= alloc_dax(dev_info
, dev_info
->gd
->disk_name
,
696 &dcssblk_dax_ops
, DAXDEV_F_SYNC
);
697 if (IS_ERR(dev_info
->dax_dev
)) {
698 rc
= PTR_ERR(dev_info
->dax_dev
);
699 dev_info
->dax_dev
= NULL
;
703 get_device(&dev_info
->dev
);
704 device_add_disk(&dev_info
->dev
, dev_info
->gd
, NULL
);
706 switch (dev_info
->segment_type
) {
710 set_disk_ro(dev_info
->gd
,1);
713 set_disk_ro(dev_info
->gd
,0);
716 up_write(&dcssblk_devices_sem
);
721 list_del(&dev_info
->lh
);
722 blk_cleanup_queue(dev_info
->dcssblk_queue
);
723 dev_info
->gd
->queue
= NULL
;
724 put_disk(dev_info
->gd
);
725 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
726 segment_unload(seg_info
->segment_name
);
728 put_device(&dev_info
->dev
);
729 up_write(&dcssblk_devices_sem
);
732 list_del(&dev_info
->lh
);
734 blk_cleanup_queue(dev_info
->dcssblk_queue
);
735 dev_info
->gd
->queue
= NULL
;
736 put_disk(dev_info
->gd
);
737 up_write(&dcssblk_devices_sem
);
739 if (dev_info
== NULL
)
741 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
742 list_del(&seg_info
->lh
);
743 segment_unload(seg_info
->segment_name
);
754 * device attribute for removing devices
757 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
759 struct dcssblk_dev_info
*dev_info
;
760 struct segment_info
*entry
;
764 if (dev
!= dcssblk_root_dev
) {
767 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
768 if (local_buf
== NULL
) {
774 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
775 local_buf
[i
] = toupper(buf
[i
]);
778 if ((i
== 0) || (i
> 8)) {
783 down_write(&dcssblk_devices_sem
);
784 dev_info
= dcssblk_get_device_by_name(local_buf
);
785 if (dev_info
== NULL
) {
786 up_write(&dcssblk_devices_sem
);
787 pr_warn("Device %s cannot be removed because it is not a known device\n",
792 if (atomic_read(&dev_info
->use_count
) != 0) {
793 up_write(&dcssblk_devices_sem
);
794 pr_warn("Device %s cannot be removed while it is in use\n",
800 list_del(&dev_info
->lh
);
801 kill_dax(dev_info
->dax_dev
);
802 put_dax(dev_info
->dax_dev
);
803 del_gendisk(dev_info
->gd
);
804 blk_cleanup_queue(dev_info
->dcssblk_queue
);
805 dev_info
->gd
->queue
= NULL
;
806 put_disk(dev_info
->gd
);
808 /* unload all related segments */
809 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
810 segment_unload(entry
->segment_name
);
812 up_write(&dcssblk_devices_sem
);
814 device_unregister(&dev_info
->dev
);
815 put_device(&dev_info
->dev
);
824 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
826 struct dcssblk_dev_info
*dev_info
;
829 dev_info
= bdev
->bd_disk
->private_data
;
830 if (NULL
== dev_info
) {
834 atomic_inc(&dev_info
->use_count
);
841 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
843 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
844 struct segment_info
*entry
;
850 down_write(&dcssblk_devices_sem
);
851 if (atomic_dec_and_test(&dev_info
->use_count
)
852 && (dev_info
->save_pending
)) {
853 pr_info("Device %s has become idle and is being saved "
854 "now\n", dev_info
->segment_name
);
855 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
856 if (entry
->segment_type
== SEG_TYPE_EN
||
857 entry
->segment_type
== SEG_TYPE_SN
)
858 pr_warn("DCSS %s is of type SN or EN and cannot"
859 " be saved\n", entry
->segment_name
);
861 segment_save(entry
->segment_name
);
863 dev_info
->save_pending
= 0;
865 up_write(&dcssblk_devices_sem
);
869 dcssblk_submit_bio(struct bio
*bio
)
871 struct dcssblk_dev_info
*dev_info
;
873 struct bvec_iter iter
;
875 unsigned long page_addr
;
876 unsigned long source_addr
;
877 unsigned long bytes_done
;
879 blk_queue_split(&bio
);
882 dev_info
= bio
->bi_disk
->private_data
;
883 if (dev_info
== NULL
)
885 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
886 (bio
->bi_iter
.bi_size
& 4095) != 0)
887 /* Request is not page-aligned. */
889 if (bio_end_sector(bio
) > get_capacity(bio
->bi_disk
)) {
890 /* Request beyond end of DCSS segment. */
893 /* verify data transfer direction */
894 if (dev_info
->is_shared
) {
895 switch (dev_info
->segment_type
) {
899 /* cannot write to these segments */
900 if (bio_data_dir(bio
) == WRITE
) {
901 pr_warn("Writing to %s failed because it is a read-only device\n",
902 dev_name(&dev_info
->dev
));
908 index
= (bio
->bi_iter
.bi_sector
>> 3);
909 bio_for_each_segment(bvec
, bio
, iter
) {
910 page_addr
= (unsigned long)
911 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
912 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
913 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
916 if (bio_data_dir(bio
) == READ
) {
917 memcpy((void*)page_addr
, (void*)source_addr
,
920 memcpy((void*)source_addr
, (void*)page_addr
,
923 bytes_done
+= bvec
.bv_len
;
926 return BLK_QC_T_NONE
;
929 return BLK_QC_T_NONE
;
933 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
934 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
936 resource_size_t offset
= pgoff
* PAGE_SIZE
;
937 unsigned long dev_sz
;
939 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
941 *kaddr
= (void *) dev_info
->start
+ offset
;
943 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
),
944 PFN_DEV
|PFN_SPECIAL
);
946 return (dev_sz
- offset
) / PAGE_SIZE
;
950 dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
951 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
953 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
955 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
959 dcssblk_check_params(void)
962 char buf
[DCSSBLK_PARM_LEN
+ 1];
963 struct dcssblk_dev_info
*dev_info
;
965 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
967 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
968 (dcssblk_segments
[j
] != ',') &&
969 (dcssblk_segments
[j
] != '\0') &&
970 (dcssblk_segments
[j
] != '('); j
++)
972 buf
[j
-i
] = dcssblk_segments
[j
];
975 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
976 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
977 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
978 buf
[k
] = toupper(buf
[k
]);
980 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
981 down_read(&dcssblk_devices_sem
);
982 dev_info
= dcssblk_get_device_by_name(buf
);
983 up_read(&dcssblk_devices_sem
);
985 dcssblk_shared_store(&dev_info
->dev
,
989 while ((dcssblk_segments
[j
] != ',') &&
990 (dcssblk_segments
[j
] != '\0'))
994 if (dcssblk_segments
[j
] == '\0')
1003 static int dcssblk_freeze(struct device
*dev
)
1005 struct dcssblk_dev_info
*dev_info
;
1008 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1009 switch (dev_info
->segment_type
) {
1013 if (!dev_info
->is_shared
)
1024 pr_err("Suspending the system failed because DCSS device %s "
1026 dev_info
->segment_name
);
1030 static int dcssblk_restore(struct device
*dev
)
1032 struct dcssblk_dev_info
*dev_info
;
1033 struct segment_info
*entry
;
1034 unsigned long start
, end
;
1037 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1038 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
1039 segment_unload(entry
->segment_name
);
1040 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
1043 // TODO in_use check ?
1044 segment_warning(rc
, entry
->segment_name
);
1047 if (start
!= entry
->start
|| end
!= entry
->end
) {
1048 pr_err("The address range of DCSS %s changed "
1049 "while the system was suspended\n",
1050 entry
->segment_name
);
1057 panic("fatal dcssblk resume error\n");
1060 static int dcssblk_thaw(struct device
*dev
)
1065 static const struct dev_pm_ops dcssblk_pm_ops
= {
1066 .freeze
= dcssblk_freeze
,
1067 .thaw
= dcssblk_thaw
,
1068 .restore
= dcssblk_restore
,
1071 static struct platform_driver dcssblk_pdrv
= {
1074 .pm
= &dcssblk_pm_ops
,
1078 static struct platform_device
*dcssblk_pdev
;
1082 * The init/exit functions.
1087 platform_device_unregister(dcssblk_pdev
);
1088 platform_driver_unregister(&dcssblk_pdrv
);
1089 root_device_unregister(dcssblk_root_dev
);
1090 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1098 rc
= platform_driver_register(&dcssblk_pdrv
);
1102 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1104 if (IS_ERR(dcssblk_pdev
)) {
1105 rc
= PTR_ERR(dcssblk_pdev
);
1109 dcssblk_root_dev
= root_device_register("dcssblk");
1110 if (IS_ERR(dcssblk_root_dev
)) {
1111 rc
= PTR_ERR(dcssblk_root_dev
);
1114 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1117 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1120 rc
= register_blkdev(0, DCSSBLK_NAME
);
1124 init_rwsem(&dcssblk_devices_sem
);
1126 dcssblk_check_params();
1130 root_device_unregister(dcssblk_root_dev
);
1132 platform_device_unregister(dcssblk_pdev
);
1134 platform_driver_unregister(&dcssblk_pdrv
);
1138 module_init(dcssblk_init
);
1139 module_exit(dcssblk_exit
);
1141 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1142 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1143 "comma-separated list, names in each set separated "
1144 "by commas are separated by colons, each set contains "
1145 "names of contiguous segments and each name max. 8 chars.\n"
1146 "Adding \"(local)\" to the end of each set equals echoing 0 "
1147 "to /sys/devices/dcssblk/<device name>/shared after loading "
1148 "the contiguous segments - \n"
1149 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1151 MODULE_LICENSE("GPL");