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_make_request(struct request_queue
*q
,
36 static long dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
37 long nr_pages
, void **kaddr
, pfn_t
*pfn
);
39 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
41 static int dcssblk_major
;
42 static const struct block_device_operations dcssblk_devops
= {
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 const struct dax_operations dcssblk_dax_ops
= {
61 .direct_access
= dcssblk_dax_direct_access
,
62 .dax_supported
= generic_fsdax_supported
,
63 .copy_from_iter
= dcssblk_dax_copy_from_iter
,
64 .copy_to_iter
= dcssblk_dax_copy_to_iter
,
67 struct dcssblk_dev_info
{
70 char segment_name
[DCSS_BUS_ID_SIZE
];
76 unsigned char save_pending
;
77 unsigned char is_shared
;
78 struct request_queue
*dcssblk_queue
;
80 struct list_head seg_list
;
81 struct dax_device
*dax_dev
;
86 char segment_name
[DCSS_BUS_ID_SIZE
];
92 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
94 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
97 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
98 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
100 static struct device
*dcssblk_root_dev
;
102 static LIST_HEAD(dcssblk_devices
);
103 static struct rw_semaphore dcssblk_devices_sem
;
106 * release function for segment device.
109 dcssblk_release_segment(struct device
*dev
)
111 struct dcssblk_dev_info
*dev_info
;
112 struct segment_info
*entry
, *temp
;
114 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
115 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
116 list_del(&entry
->lh
);
120 module_put(THIS_MODULE
);
124 * get a minor number. needs to be called with
125 * down_write(&dcssblk_devices_sem) and the
126 * device needs to be enqueued before the semaphore is
130 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
133 struct dcssblk_dev_info
*entry
;
135 if (dev_info
== NULL
)
137 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
139 // test if minor available
140 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
141 if (minor
== entry
->gd
->first_minor
)
143 if (!found
) break; // got unused minor
147 dev_info
->gd
->first_minor
= minor
;
152 * get the struct dcssblk_dev_info from dcssblk_devices
153 * for the given name.
154 * down_read(&dcssblk_devices_sem) must be held.
156 static struct dcssblk_dev_info
*
157 dcssblk_get_device_by_name(char *name
)
159 struct dcssblk_dev_info
*entry
;
161 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
162 if (!strcmp(name
, entry
->segment_name
)) {
170 * get the struct segment_info from seg_list
171 * for the given name.
172 * down_read(&dcssblk_devices_sem) must be held.
174 static struct segment_info
*
175 dcssblk_get_segment_by_name(char *name
)
177 struct dcssblk_dev_info
*dev_info
;
178 struct segment_info
*entry
;
180 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
181 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
182 if (!strcmp(name
, entry
->segment_name
))
190 * get the highest address of the multi-segment block.
193 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
195 unsigned long highest_addr
;
196 struct segment_info
*entry
;
199 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
200 if (highest_addr
< entry
->end
)
201 highest_addr
= entry
->end
;
207 * get the lowest address of the multi-segment block.
210 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
213 unsigned long lowest_addr
;
214 struct segment_info
*entry
;
218 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
219 if (set_first
== 0) {
220 lowest_addr
= entry
->start
;
223 if (lowest_addr
> entry
->start
)
224 lowest_addr
= entry
->start
;
231 * Check continuity of segments.
234 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
237 struct segment_info
*sort_list
, *entry
, temp
;
239 if (dev_info
->num_of_segments
<= 1)
242 sort_list
= kcalloc(dev_info
->num_of_segments
,
243 sizeof(struct segment_info
),
245 if (sort_list
== NULL
)
248 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
249 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
254 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
255 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
256 if (sort_list
[j
].start
> sort_list
[i
].start
) {
257 memcpy(&temp
, &sort_list
[i
],
258 sizeof(struct segment_info
));
259 memcpy(&sort_list
[i
], &sort_list
[j
],
260 sizeof(struct segment_info
));
261 memcpy(&sort_list
[j
], &temp
,
262 sizeof(struct segment_info
));
265 /* check continuity */
266 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
267 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
268 pr_err("Adjacent DCSSs %s and %s are not "
269 "contiguous\n", sort_list
[i
].segment_name
,
270 sort_list
[i
+1].segment_name
);
274 /* EN and EW are allowed in a block device */
275 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
276 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
277 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
278 !(sort_list
[i
+1].segment_type
&
279 SEGMENT_EXCLUSIVE
) ||
280 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
281 pr_err("DCSS %s and DCSS %s have "
282 "incompatible types\n",
283 sort_list
[i
].segment_name
,
284 sort_list
[i
+1].segment_name
);
300 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
304 /* already loaded? */
305 down_read(&dcssblk_devices_sem
);
306 *seg_info
= dcssblk_get_segment_by_name(name
);
307 up_read(&dcssblk_devices_sem
);
308 if (*seg_info
!= NULL
)
311 /* get a struct segment_info */
312 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
313 if (*seg_info
== NULL
)
316 strcpy((*seg_info
)->segment_name
, name
);
318 /* load the segment */
319 rc
= segment_load(name
, SEGMENT_SHARED
,
320 &(*seg_info
)->start
, &(*seg_info
)->end
);
322 segment_warning(rc
, (*seg_info
)->segment_name
);
325 INIT_LIST_HEAD(&(*seg_info
)->lh
);
326 (*seg_info
)->segment_type
= rc
;
332 * device attribute for switching shared/nonshared (exclusive)
333 * operation (show + store)
336 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
338 struct dcssblk_dev_info
*dev_info
;
340 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
341 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
345 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
347 struct dcssblk_dev_info
*dev_info
;
348 struct segment_info
*entry
, *temp
;
351 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
353 down_write(&dcssblk_devices_sem
);
354 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
355 if (atomic_read(&dev_info
->use_count
)) {
359 if (inbuf
[0] == '1') {
360 /* reload segments in shared mode */
361 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
362 rc
= segment_modify_shared(entry
->segment_name
,
365 BUG_ON(rc
== -EINVAL
);
370 dev_info
->is_shared
= 1;
371 switch (dev_info
->segment_type
) {
375 set_disk_ro(dev_info
->gd
, 1);
377 } else if (inbuf
[0] == '0') {
378 /* reload segments in exclusive mode */
379 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
380 pr_err("DCSS %s is of type SC and cannot be "
381 "loaded as exclusive-writable\n",
382 dev_info
->segment_name
);
386 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
387 rc
= segment_modify_shared(entry
->segment_name
,
390 BUG_ON(rc
== -EINVAL
);
395 dev_info
->is_shared
= 0;
396 set_disk_ro(dev_info
->gd
, 0);
405 pr_err("DCSS device %s is removed after a failed access mode "
406 "change\n", dev_info
->segment_name
);
408 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
410 segment_unload(entry
->segment_name
);
412 list_del(&dev_info
->lh
);
414 kill_dax(dev_info
->dax_dev
);
415 put_dax(dev_info
->dax_dev
);
416 del_gendisk(dev_info
->gd
);
417 blk_cleanup_queue(dev_info
->dcssblk_queue
);
418 dev_info
->gd
->queue
= NULL
;
419 put_disk(dev_info
->gd
);
420 up_write(&dcssblk_devices_sem
);
422 if (device_remove_file_self(dev
, attr
)) {
423 device_unregister(dev
);
428 up_write(&dcssblk_devices_sem
);
431 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
432 dcssblk_shared_store
);
435 * device attribute for save operation on current copy
436 * of the segment. If the segment is busy, saving will
437 * become pending until it gets released, which can be
438 * undone by storing a non-true value to this entry.
442 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
444 struct dcssblk_dev_info
*dev_info
;
446 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
447 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
451 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
453 struct dcssblk_dev_info
*dev_info
;
454 struct segment_info
*entry
;
456 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
458 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
460 down_write(&dcssblk_devices_sem
);
461 if (inbuf
[0] == '1') {
462 if (atomic_read(&dev_info
->use_count
) == 0) {
463 // device is idle => we save immediately
464 pr_info("All DCSSs that map to device %s are "
465 "saved\n", dev_info
->segment_name
);
466 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
467 if (entry
->segment_type
== SEG_TYPE_EN
||
468 entry
->segment_type
== SEG_TYPE_SN
)
469 pr_warn("DCSS %s is of type SN or EN"
470 " and cannot be saved\n",
471 entry
->segment_name
);
473 segment_save(entry
->segment_name
);
476 // device is busy => we save it when it becomes
477 // idle in dcssblk_release
478 pr_info("Device %s is in use, its DCSSs will be "
479 "saved when it becomes idle\n",
480 dev_info
->segment_name
);
481 dev_info
->save_pending
= 1;
483 } else if (inbuf
[0] == '0') {
484 if (dev_info
->save_pending
) {
485 // device is busy & the user wants to undo his save
487 dev_info
->save_pending
= 0;
488 pr_info("A pending save request for device %s "
489 "has been canceled\n",
490 dev_info
->segment_name
);
493 up_write(&dcssblk_devices_sem
);
496 up_write(&dcssblk_devices_sem
);
499 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
503 * device attribute for showing all segments in a device
506 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
511 struct dcssblk_dev_info
*dev_info
;
512 struct segment_info
*entry
;
514 down_read(&dcssblk_devices_sem
);
515 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
518 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
519 strcpy(&buf
[i
], entry
->segment_name
);
520 i
+= strlen(entry
->segment_name
);
524 up_read(&dcssblk_devices_sem
);
527 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
529 static struct attribute
*dcssblk_dev_attrs
[] = {
530 &dev_attr_shared
.attr
,
532 &dev_attr_seglist
.attr
,
535 static struct attribute_group dcssblk_dev_attr_group
= {
536 .attrs
= dcssblk_dev_attrs
,
538 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
539 &dcssblk_dev_attr_group
,
544 * device attribute for adding devices
547 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
549 int rc
, i
, j
, num_of_segments
;
550 struct dcssblk_dev_info
*dev_info
;
551 struct segment_info
*seg_info
, *temp
;
553 unsigned long seg_byte_size
;
557 if (dev
!= dcssblk_root_dev
) {
561 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
566 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
567 if (local_buf
== NULL
) {
576 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
577 for (j
= i
; j
< count
&&
580 (buf
[j
] != '\n'); j
++) {
581 local_buf
[j
-i
] = toupper(buf
[j
]);
583 local_buf
[j
-i
] = '\0';
584 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
589 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
593 * get a struct dcssblk_dev_info
595 if (num_of_segments
== 0) {
596 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
598 if (dev_info
== NULL
) {
602 strcpy(dev_info
->segment_name
, local_buf
);
603 dev_info
->segment_type
= seg_info
->segment_type
;
604 INIT_LIST_HEAD(&dev_info
->seg_list
);
606 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
610 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
614 /* no trailing colon at the end of the input */
615 if ((i
> 0) && (buf
[i
-1] == ':')) {
619 strlcpy(local_buf
, buf
, i
+ 1);
620 dev_info
->num_of_segments
= num_of_segments
;
621 rc
= dcssblk_is_continuous(dev_info
);
625 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
626 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
628 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
629 dev_info
->dev
.release
= dcssblk_release_segment
;
630 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
631 INIT_LIST_HEAD(&dev_info
->lh
);
632 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
633 if (dev_info
->gd
== NULL
) {
637 dev_info
->gd
->major
= dcssblk_major
;
638 dev_info
->gd
->fops
= &dcssblk_devops
;
639 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
640 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
641 dev_info
->gd
->private_data
= dev_info
;
642 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
643 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
644 blk_queue_flag_set(QUEUE_FLAG_DAX
, dev_info
->dcssblk_queue
);
646 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
647 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
648 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
649 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
651 dev_info
->save_pending
= 0;
652 dev_info
->is_shared
= 1;
653 dev_info
->dev
.parent
= dcssblk_root_dev
;
656 *get minor, add to list
658 down_write(&dcssblk_devices_sem
);
659 if (dcssblk_get_segment_by_name(local_buf
)) {
663 rc
= dcssblk_assign_free_minor(dev_info
);
666 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
667 dev_info
->gd
->first_minor
);
668 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
670 if (!try_module_get(THIS_MODULE
)) {
675 * register the device
677 rc
= device_register(&dev_info
->dev
);
681 dev_info
->dax_dev
= alloc_dax(dev_info
, dev_info
->gd
->disk_name
,
682 &dcssblk_dax_ops
, DAXDEV_F_SYNC
);
683 if (!dev_info
->dax_dev
) {
688 get_device(&dev_info
->dev
);
689 device_add_disk(&dev_info
->dev
, dev_info
->gd
, NULL
);
691 switch (dev_info
->segment_type
) {
695 set_disk_ro(dev_info
->gd
,1);
698 set_disk_ro(dev_info
->gd
,0);
701 up_write(&dcssblk_devices_sem
);
706 list_del(&dev_info
->lh
);
707 blk_cleanup_queue(dev_info
->dcssblk_queue
);
708 dev_info
->gd
->queue
= NULL
;
709 put_disk(dev_info
->gd
);
710 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
711 segment_unload(seg_info
->segment_name
);
713 put_device(&dev_info
->dev
);
714 up_write(&dcssblk_devices_sem
);
717 list_del(&dev_info
->lh
);
719 blk_cleanup_queue(dev_info
->dcssblk_queue
);
720 dev_info
->gd
->queue
= NULL
;
721 put_disk(dev_info
->gd
);
722 up_write(&dcssblk_devices_sem
);
724 if (dev_info
== NULL
)
726 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
727 list_del(&seg_info
->lh
);
728 segment_unload(seg_info
->segment_name
);
739 * device attribute for removing devices
742 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
744 struct dcssblk_dev_info
*dev_info
;
745 struct segment_info
*entry
;
749 if (dev
!= dcssblk_root_dev
) {
752 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
753 if (local_buf
== NULL
) {
759 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
760 local_buf
[i
] = toupper(buf
[i
]);
763 if ((i
== 0) || (i
> 8)) {
768 down_write(&dcssblk_devices_sem
);
769 dev_info
= dcssblk_get_device_by_name(local_buf
);
770 if (dev_info
== NULL
) {
771 up_write(&dcssblk_devices_sem
);
772 pr_warn("Device %s cannot be removed because it is not a known device\n",
777 if (atomic_read(&dev_info
->use_count
) != 0) {
778 up_write(&dcssblk_devices_sem
);
779 pr_warn("Device %s cannot be removed while it is in use\n",
785 list_del(&dev_info
->lh
);
786 kill_dax(dev_info
->dax_dev
);
787 put_dax(dev_info
->dax_dev
);
788 del_gendisk(dev_info
->gd
);
789 blk_cleanup_queue(dev_info
->dcssblk_queue
);
790 dev_info
->gd
->queue
= NULL
;
791 put_disk(dev_info
->gd
);
793 /* unload all related segments */
794 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
795 segment_unload(entry
->segment_name
);
797 up_write(&dcssblk_devices_sem
);
799 device_unregister(&dev_info
->dev
);
800 put_device(&dev_info
->dev
);
809 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
811 struct dcssblk_dev_info
*dev_info
;
814 dev_info
= bdev
->bd_disk
->private_data
;
815 if (NULL
== dev_info
) {
819 atomic_inc(&dev_info
->use_count
);
820 bdev
->bd_block_size
= 4096;
827 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
829 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
830 struct segment_info
*entry
;
836 down_write(&dcssblk_devices_sem
);
837 if (atomic_dec_and_test(&dev_info
->use_count
)
838 && (dev_info
->save_pending
)) {
839 pr_info("Device %s has become idle and is being saved "
840 "now\n", dev_info
->segment_name
);
841 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
842 if (entry
->segment_type
== SEG_TYPE_EN
||
843 entry
->segment_type
== SEG_TYPE_SN
)
844 pr_warn("DCSS %s is of type SN or EN and cannot"
845 " be saved\n", entry
->segment_name
);
847 segment_save(entry
->segment_name
);
849 dev_info
->save_pending
= 0;
851 up_write(&dcssblk_devices_sem
);
855 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
857 struct dcssblk_dev_info
*dev_info
;
859 struct bvec_iter iter
;
861 unsigned long page_addr
;
862 unsigned long source_addr
;
863 unsigned long bytes_done
;
865 blk_queue_split(q
, &bio
);
868 dev_info
= bio
->bi_disk
->private_data
;
869 if (dev_info
== NULL
)
871 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
872 (bio
->bi_iter
.bi_size
& 4095) != 0)
873 /* Request is not page-aligned. */
875 if (bio_end_sector(bio
) > get_capacity(bio
->bi_disk
)) {
876 /* Request beyond end of DCSS segment. */
879 /* verify data transfer direction */
880 if (dev_info
->is_shared
) {
881 switch (dev_info
->segment_type
) {
885 /* cannot write to these segments */
886 if (bio_data_dir(bio
) == WRITE
) {
887 pr_warn("Writing to %s failed because it is a read-only device\n",
888 dev_name(&dev_info
->dev
));
894 index
= (bio
->bi_iter
.bi_sector
>> 3);
895 bio_for_each_segment(bvec
, bio
, iter
) {
896 page_addr
= (unsigned long)
897 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
898 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
899 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
902 if (bio_data_dir(bio
) == READ
) {
903 memcpy((void*)page_addr
, (void*)source_addr
,
906 memcpy((void*)source_addr
, (void*)page_addr
,
909 bytes_done
+= bvec
.bv_len
;
912 return BLK_QC_T_NONE
;
915 return BLK_QC_T_NONE
;
919 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
920 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
922 resource_size_t offset
= pgoff
* PAGE_SIZE
;
923 unsigned long dev_sz
;
925 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
927 *kaddr
= (void *) dev_info
->start
+ offset
;
929 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
),
930 PFN_DEV
|PFN_SPECIAL
);
932 return (dev_sz
- offset
) / PAGE_SIZE
;
936 dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
937 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
939 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
941 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
945 dcssblk_check_params(void)
948 char buf
[DCSSBLK_PARM_LEN
+ 1];
949 struct dcssblk_dev_info
*dev_info
;
951 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
953 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
954 (dcssblk_segments
[j
] != ',') &&
955 (dcssblk_segments
[j
] != '\0') &&
956 (dcssblk_segments
[j
] != '('); j
++)
958 buf
[j
-i
] = dcssblk_segments
[j
];
961 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
962 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
963 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
964 buf
[k
] = toupper(buf
[k
]);
966 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
967 down_read(&dcssblk_devices_sem
);
968 dev_info
= dcssblk_get_device_by_name(buf
);
969 up_read(&dcssblk_devices_sem
);
971 dcssblk_shared_store(&dev_info
->dev
,
975 while ((dcssblk_segments
[j
] != ',') &&
976 (dcssblk_segments
[j
] != '\0'))
980 if (dcssblk_segments
[j
] == '\0')
989 static int dcssblk_freeze(struct device
*dev
)
991 struct dcssblk_dev_info
*dev_info
;
994 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
995 switch (dev_info
->segment_type
) {
999 if (!dev_info
->is_shared
)
1010 pr_err("Suspending the system failed because DCSS device %s "
1012 dev_info
->segment_name
);
1016 static int dcssblk_restore(struct device
*dev
)
1018 struct dcssblk_dev_info
*dev_info
;
1019 struct segment_info
*entry
;
1020 unsigned long start
, end
;
1023 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1024 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
1025 segment_unload(entry
->segment_name
);
1026 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
1029 // TODO in_use check ?
1030 segment_warning(rc
, entry
->segment_name
);
1033 if (start
!= entry
->start
|| end
!= entry
->end
) {
1034 pr_err("The address range of DCSS %s changed "
1035 "while the system was suspended\n",
1036 entry
->segment_name
);
1043 panic("fatal dcssblk resume error\n");
1046 static int dcssblk_thaw(struct device
*dev
)
1051 static const struct dev_pm_ops dcssblk_pm_ops
= {
1052 .freeze
= dcssblk_freeze
,
1053 .thaw
= dcssblk_thaw
,
1054 .restore
= dcssblk_restore
,
1057 static struct platform_driver dcssblk_pdrv
= {
1060 .pm
= &dcssblk_pm_ops
,
1064 static struct platform_device
*dcssblk_pdev
;
1068 * The init/exit functions.
1073 platform_device_unregister(dcssblk_pdev
);
1074 platform_driver_unregister(&dcssblk_pdrv
);
1075 root_device_unregister(dcssblk_root_dev
);
1076 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1084 rc
= platform_driver_register(&dcssblk_pdrv
);
1088 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1090 if (IS_ERR(dcssblk_pdev
)) {
1091 rc
= PTR_ERR(dcssblk_pdev
);
1095 dcssblk_root_dev
= root_device_register("dcssblk");
1096 if (IS_ERR(dcssblk_root_dev
)) {
1097 rc
= PTR_ERR(dcssblk_root_dev
);
1100 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1103 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1106 rc
= register_blkdev(0, DCSSBLK_NAME
);
1110 init_rwsem(&dcssblk_devices_sem
);
1112 dcssblk_check_params();
1116 root_device_unregister(dcssblk_root_dev
);
1118 platform_device_unregister(dcssblk_pdev
);
1120 platform_driver_unregister(&dcssblk_pdrv
);
1124 module_init(dcssblk_init
);
1125 module_exit(dcssblk_exit
);
1127 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1128 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1129 "comma-separated list, names in each set separated "
1130 "by commas are separated by colons, each set contains "
1131 "names of contiguous segments and each name max. 8 chars.\n"
1132 "Adding \"(local)\" to the end of each set equals echoing 0 "
1133 "to /sys/devices/dcssblk/<device name>/shared after loading "
1134 "the contiguous segments - \n"
1135 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1137 MODULE_LICENSE("GPL");