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 .copy_from_iter
= dcssblk_dax_copy_from_iter
,
63 .copy_to_iter
= dcssblk_dax_copy_to_iter
,
66 struct dcssblk_dev_info
{
69 char segment_name
[DCSS_BUS_ID_SIZE
];
75 unsigned char save_pending
;
76 unsigned char is_shared
;
77 struct request_queue
*dcssblk_queue
;
79 struct list_head seg_list
;
80 struct dax_device
*dax_dev
;
85 char segment_name
[DCSS_BUS_ID_SIZE
];
91 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
93 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
96 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
97 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
99 static struct device
*dcssblk_root_dev
;
101 static LIST_HEAD(dcssblk_devices
);
102 static struct rw_semaphore dcssblk_devices_sem
;
105 * release function for segment device.
108 dcssblk_release_segment(struct device
*dev
)
110 struct dcssblk_dev_info
*dev_info
;
111 struct segment_info
*entry
, *temp
;
113 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
114 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
115 list_del(&entry
->lh
);
119 module_put(THIS_MODULE
);
123 * get a minor number. needs to be called with
124 * down_write(&dcssblk_devices_sem) and the
125 * device needs to be enqueued before the semaphore is
129 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
132 struct dcssblk_dev_info
*entry
;
134 if (dev_info
== NULL
)
136 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
138 // test if minor available
139 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
140 if (minor
== entry
->gd
->first_minor
)
142 if (!found
) break; // got unused minor
146 dev_info
->gd
->first_minor
= minor
;
151 * get the struct dcssblk_dev_info from dcssblk_devices
152 * for the given name.
153 * down_read(&dcssblk_devices_sem) must be held.
155 static struct dcssblk_dev_info
*
156 dcssblk_get_device_by_name(char *name
)
158 struct dcssblk_dev_info
*entry
;
160 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
161 if (!strcmp(name
, entry
->segment_name
)) {
169 * get the struct segment_info from seg_list
170 * for the given name.
171 * down_read(&dcssblk_devices_sem) must be held.
173 static struct segment_info
*
174 dcssblk_get_segment_by_name(char *name
)
176 struct dcssblk_dev_info
*dev_info
;
177 struct segment_info
*entry
;
179 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
180 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
181 if (!strcmp(name
, entry
->segment_name
))
189 * get the highest address of the multi-segment block.
192 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
194 unsigned long highest_addr
;
195 struct segment_info
*entry
;
198 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
199 if (highest_addr
< entry
->end
)
200 highest_addr
= entry
->end
;
206 * get the lowest address of the multi-segment block.
209 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
212 unsigned long lowest_addr
;
213 struct segment_info
*entry
;
217 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
218 if (set_first
== 0) {
219 lowest_addr
= entry
->start
;
222 if (lowest_addr
> entry
->start
)
223 lowest_addr
= entry
->start
;
230 * Check continuity of segments.
233 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
236 struct segment_info
*sort_list
, *entry
, temp
;
238 if (dev_info
->num_of_segments
<= 1)
241 sort_list
= kcalloc(dev_info
->num_of_segments
,
242 sizeof(struct segment_info
),
244 if (sort_list
== NULL
)
247 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
248 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
253 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
254 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
255 if (sort_list
[j
].start
> sort_list
[i
].start
) {
256 memcpy(&temp
, &sort_list
[i
],
257 sizeof(struct segment_info
));
258 memcpy(&sort_list
[i
], &sort_list
[j
],
259 sizeof(struct segment_info
));
260 memcpy(&sort_list
[j
], &temp
,
261 sizeof(struct segment_info
));
264 /* check continuity */
265 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
266 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
267 pr_err("Adjacent DCSSs %s and %s are not "
268 "contiguous\n", sort_list
[i
].segment_name
,
269 sort_list
[i
+1].segment_name
);
273 /* EN and EW are allowed in a block device */
274 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
275 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
276 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
277 !(sort_list
[i
+1].segment_type
&
278 SEGMENT_EXCLUSIVE
) ||
279 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
280 pr_err("DCSS %s and DCSS %s have "
281 "incompatible types\n",
282 sort_list
[i
].segment_name
,
283 sort_list
[i
+1].segment_name
);
299 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
303 /* already loaded? */
304 down_read(&dcssblk_devices_sem
);
305 *seg_info
= dcssblk_get_segment_by_name(name
);
306 up_read(&dcssblk_devices_sem
);
307 if (*seg_info
!= NULL
)
310 /* get a struct segment_info */
311 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
312 if (*seg_info
== NULL
)
315 strcpy((*seg_info
)->segment_name
, name
);
317 /* load the segment */
318 rc
= segment_load(name
, SEGMENT_SHARED
,
319 &(*seg_info
)->start
, &(*seg_info
)->end
);
321 segment_warning(rc
, (*seg_info
)->segment_name
);
324 INIT_LIST_HEAD(&(*seg_info
)->lh
);
325 (*seg_info
)->segment_type
= rc
;
331 * device attribute for switching shared/nonshared (exclusive)
332 * operation (show + store)
335 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
337 struct dcssblk_dev_info
*dev_info
;
339 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
340 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
344 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
346 struct dcssblk_dev_info
*dev_info
;
347 struct segment_info
*entry
, *temp
;
350 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
352 down_write(&dcssblk_devices_sem
);
353 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
354 if (atomic_read(&dev_info
->use_count
)) {
358 if (inbuf
[0] == '1') {
359 /* reload segments in shared mode */
360 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
361 rc
= segment_modify_shared(entry
->segment_name
,
364 BUG_ON(rc
== -EINVAL
);
369 dev_info
->is_shared
= 1;
370 switch (dev_info
->segment_type
) {
374 set_disk_ro(dev_info
->gd
, 1);
376 } else if (inbuf
[0] == '0') {
377 /* reload segments in exclusive mode */
378 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
379 pr_err("DCSS %s is of type SC and cannot be "
380 "loaded as exclusive-writable\n",
381 dev_info
->segment_name
);
385 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
386 rc
= segment_modify_shared(entry
->segment_name
,
389 BUG_ON(rc
== -EINVAL
);
394 dev_info
->is_shared
= 0;
395 set_disk_ro(dev_info
->gd
, 0);
404 pr_err("DCSS device %s is removed after a failed access mode "
405 "change\n", dev_info
->segment_name
);
407 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
409 segment_unload(entry
->segment_name
);
411 list_del(&dev_info
->lh
);
413 kill_dax(dev_info
->dax_dev
);
414 put_dax(dev_info
->dax_dev
);
415 del_gendisk(dev_info
->gd
);
416 blk_cleanup_queue(dev_info
->dcssblk_queue
);
417 dev_info
->gd
->queue
= NULL
;
418 put_disk(dev_info
->gd
);
419 up_write(&dcssblk_devices_sem
);
421 if (device_remove_file_self(dev
, attr
)) {
422 device_unregister(dev
);
427 up_write(&dcssblk_devices_sem
);
430 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
431 dcssblk_shared_store
);
434 * device attribute for save operation on current copy
435 * of the segment. If the segment is busy, saving will
436 * become pending until it gets released, which can be
437 * undone by storing a non-true value to this entry.
441 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
443 struct dcssblk_dev_info
*dev_info
;
445 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
446 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
450 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
452 struct dcssblk_dev_info
*dev_info
;
453 struct segment_info
*entry
;
455 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
457 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
459 down_write(&dcssblk_devices_sem
);
460 if (inbuf
[0] == '1') {
461 if (atomic_read(&dev_info
->use_count
) == 0) {
462 // device is idle => we save immediately
463 pr_info("All DCSSs that map to device %s are "
464 "saved\n", dev_info
->segment_name
);
465 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
466 if (entry
->segment_type
== SEG_TYPE_EN
||
467 entry
->segment_type
== SEG_TYPE_SN
)
468 pr_warn("DCSS %s is of type SN or EN"
469 " and cannot be saved\n",
470 entry
->segment_name
);
472 segment_save(entry
->segment_name
);
475 // device is busy => we save it when it becomes
476 // idle in dcssblk_release
477 pr_info("Device %s is in use, its DCSSs will be "
478 "saved when it becomes idle\n",
479 dev_info
->segment_name
);
480 dev_info
->save_pending
= 1;
482 } else if (inbuf
[0] == '0') {
483 if (dev_info
->save_pending
) {
484 // device is busy & the user wants to undo his save
486 dev_info
->save_pending
= 0;
487 pr_info("A pending save request for device %s "
488 "has been canceled\n",
489 dev_info
->segment_name
);
492 up_write(&dcssblk_devices_sem
);
495 up_write(&dcssblk_devices_sem
);
498 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
502 * device attribute for showing all segments in a device
505 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
510 struct dcssblk_dev_info
*dev_info
;
511 struct segment_info
*entry
;
513 down_read(&dcssblk_devices_sem
);
514 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
517 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
518 strcpy(&buf
[i
], entry
->segment_name
);
519 i
+= strlen(entry
->segment_name
);
523 up_read(&dcssblk_devices_sem
);
526 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
528 static struct attribute
*dcssblk_dev_attrs
[] = {
529 &dev_attr_shared
.attr
,
531 &dev_attr_seglist
.attr
,
534 static struct attribute_group dcssblk_dev_attr_group
= {
535 .attrs
= dcssblk_dev_attrs
,
537 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
538 &dcssblk_dev_attr_group
,
543 * device attribute for adding devices
546 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
548 int rc
, i
, j
, num_of_segments
;
549 struct dcssblk_dev_info
*dev_info
;
550 struct segment_info
*seg_info
, *temp
;
552 unsigned long seg_byte_size
;
556 if (dev
!= dcssblk_root_dev
) {
560 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
565 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
566 if (local_buf
== NULL
) {
575 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
576 for (j
= i
; j
< count
&&
579 (buf
[j
] != '\n'); j
++) {
580 local_buf
[j
-i
] = toupper(buf
[j
]);
582 local_buf
[j
-i
] = '\0';
583 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
588 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
592 * get a struct dcssblk_dev_info
594 if (num_of_segments
== 0) {
595 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
597 if (dev_info
== NULL
) {
601 strcpy(dev_info
->segment_name
, local_buf
);
602 dev_info
->segment_type
= seg_info
->segment_type
;
603 INIT_LIST_HEAD(&dev_info
->seg_list
);
605 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
609 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
613 /* no trailing colon at the end of the input */
614 if ((i
> 0) && (buf
[i
-1] == ':')) {
618 strlcpy(local_buf
, buf
, i
+ 1);
619 dev_info
->num_of_segments
= num_of_segments
;
620 rc
= dcssblk_is_continuous(dev_info
);
624 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
625 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
627 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
628 dev_info
->dev
.release
= dcssblk_release_segment
;
629 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
630 INIT_LIST_HEAD(&dev_info
->lh
);
631 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
632 if (dev_info
->gd
== NULL
) {
636 dev_info
->gd
->major
= dcssblk_major
;
637 dev_info
->gd
->fops
= &dcssblk_devops
;
638 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
639 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
640 dev_info
->gd
->private_data
= dev_info
;
641 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
642 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
643 blk_queue_flag_set(QUEUE_FLAG_DAX
, dev_info
->dcssblk_queue
);
645 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
646 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
647 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
648 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
650 dev_info
->save_pending
= 0;
651 dev_info
->is_shared
= 1;
652 dev_info
->dev
.parent
= dcssblk_root_dev
;
655 *get minor, add to list
657 down_write(&dcssblk_devices_sem
);
658 if (dcssblk_get_segment_by_name(local_buf
)) {
662 rc
= dcssblk_assign_free_minor(dev_info
);
665 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
666 dev_info
->gd
->first_minor
);
667 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
669 if (!try_module_get(THIS_MODULE
)) {
674 * register the device
676 rc
= device_register(&dev_info
->dev
);
680 dev_info
->dax_dev
= alloc_dax(dev_info
, dev_info
->gd
->disk_name
,
682 if (!dev_info
->dax_dev
) {
687 get_device(&dev_info
->dev
);
688 device_add_disk(&dev_info
->dev
, dev_info
->gd
, NULL
);
690 switch (dev_info
->segment_type
) {
694 set_disk_ro(dev_info
->gd
,1);
697 set_disk_ro(dev_info
->gd
,0);
700 up_write(&dcssblk_devices_sem
);
705 list_del(&dev_info
->lh
);
706 blk_cleanup_queue(dev_info
->dcssblk_queue
);
707 dev_info
->gd
->queue
= NULL
;
708 put_disk(dev_info
->gd
);
709 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
710 segment_unload(seg_info
->segment_name
);
712 put_device(&dev_info
->dev
);
713 up_write(&dcssblk_devices_sem
);
716 list_del(&dev_info
->lh
);
718 blk_cleanup_queue(dev_info
->dcssblk_queue
);
719 dev_info
->gd
->queue
= NULL
;
720 put_disk(dev_info
->gd
);
721 up_write(&dcssblk_devices_sem
);
723 if (dev_info
== NULL
)
725 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
726 list_del(&seg_info
->lh
);
727 segment_unload(seg_info
->segment_name
);
738 * device attribute for removing devices
741 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
743 struct dcssblk_dev_info
*dev_info
;
744 struct segment_info
*entry
;
748 if (dev
!= dcssblk_root_dev
) {
751 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
752 if (local_buf
== NULL
) {
758 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
759 local_buf
[i
] = toupper(buf
[i
]);
762 if ((i
== 0) || (i
> 8)) {
767 down_write(&dcssblk_devices_sem
);
768 dev_info
= dcssblk_get_device_by_name(local_buf
);
769 if (dev_info
== NULL
) {
770 up_write(&dcssblk_devices_sem
);
771 pr_warn("Device %s cannot be removed because it is not a known device\n",
776 if (atomic_read(&dev_info
->use_count
) != 0) {
777 up_write(&dcssblk_devices_sem
);
778 pr_warn("Device %s cannot be removed while it is in use\n",
784 list_del(&dev_info
->lh
);
785 kill_dax(dev_info
->dax_dev
);
786 put_dax(dev_info
->dax_dev
);
787 del_gendisk(dev_info
->gd
);
788 blk_cleanup_queue(dev_info
->dcssblk_queue
);
789 dev_info
->gd
->queue
= NULL
;
790 put_disk(dev_info
->gd
);
792 /* unload all related segments */
793 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
794 segment_unload(entry
->segment_name
);
796 up_write(&dcssblk_devices_sem
);
798 device_unregister(&dev_info
->dev
);
799 put_device(&dev_info
->dev
);
808 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
810 struct dcssblk_dev_info
*dev_info
;
813 dev_info
= bdev
->bd_disk
->private_data
;
814 if (NULL
== dev_info
) {
818 atomic_inc(&dev_info
->use_count
);
819 bdev
->bd_block_size
= 4096;
826 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
828 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
829 struct segment_info
*entry
;
835 down_write(&dcssblk_devices_sem
);
836 if (atomic_dec_and_test(&dev_info
->use_count
)
837 && (dev_info
->save_pending
)) {
838 pr_info("Device %s has become idle and is being saved "
839 "now\n", dev_info
->segment_name
);
840 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
841 if (entry
->segment_type
== SEG_TYPE_EN
||
842 entry
->segment_type
== SEG_TYPE_SN
)
843 pr_warn("DCSS %s is of type SN or EN and cannot"
844 " be saved\n", entry
->segment_name
);
846 segment_save(entry
->segment_name
);
848 dev_info
->save_pending
= 0;
850 up_write(&dcssblk_devices_sem
);
854 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
856 struct dcssblk_dev_info
*dev_info
;
858 struct bvec_iter iter
;
860 unsigned long page_addr
;
861 unsigned long source_addr
;
862 unsigned long bytes_done
;
864 blk_queue_split(q
, &bio
);
867 dev_info
= bio
->bi_disk
->private_data
;
868 if (dev_info
== NULL
)
870 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
871 (bio
->bi_iter
.bi_size
& 4095) != 0)
872 /* Request is not page-aligned. */
874 if (bio_end_sector(bio
) > get_capacity(bio
->bi_disk
)) {
875 /* Request beyond end of DCSS segment. */
878 /* verify data transfer direction */
879 if (dev_info
->is_shared
) {
880 switch (dev_info
->segment_type
) {
884 /* cannot write to these segments */
885 if (bio_data_dir(bio
) == WRITE
) {
886 pr_warn("Writing to %s failed because it is a read-only device\n",
887 dev_name(&dev_info
->dev
));
893 index
= (bio
->bi_iter
.bi_sector
>> 3);
894 bio_for_each_segment(bvec
, bio
, iter
) {
895 page_addr
= (unsigned long)
896 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
897 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
898 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
901 if (bio_data_dir(bio
) == READ
) {
902 memcpy((void*)page_addr
, (void*)source_addr
,
905 memcpy((void*)source_addr
, (void*)page_addr
,
908 bytes_done
+= bvec
.bv_len
;
911 return BLK_QC_T_NONE
;
914 return BLK_QC_T_NONE
;
918 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
919 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
921 resource_size_t offset
= pgoff
* PAGE_SIZE
;
922 unsigned long dev_sz
;
924 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
926 *kaddr
= (void *) dev_info
->start
+ offset
;
928 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
),
929 PFN_DEV
|PFN_SPECIAL
);
931 return (dev_sz
- offset
) / PAGE_SIZE
;
935 dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
936 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
938 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
940 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
944 dcssblk_check_params(void)
947 char buf
[DCSSBLK_PARM_LEN
+ 1];
948 struct dcssblk_dev_info
*dev_info
;
950 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
952 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
953 (dcssblk_segments
[j
] != ',') &&
954 (dcssblk_segments
[j
] != '\0') &&
955 (dcssblk_segments
[j
] != '('); j
++)
957 buf
[j
-i
] = dcssblk_segments
[j
];
960 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
961 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
962 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
963 buf
[k
] = toupper(buf
[k
]);
965 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
966 down_read(&dcssblk_devices_sem
);
967 dev_info
= dcssblk_get_device_by_name(buf
);
968 up_read(&dcssblk_devices_sem
);
970 dcssblk_shared_store(&dev_info
->dev
,
974 while ((dcssblk_segments
[j
] != ',') &&
975 (dcssblk_segments
[j
] != '\0'))
979 if (dcssblk_segments
[j
] == '\0')
988 static int dcssblk_freeze(struct device
*dev
)
990 struct dcssblk_dev_info
*dev_info
;
993 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
994 switch (dev_info
->segment_type
) {
998 if (!dev_info
->is_shared
)
1009 pr_err("Suspending the system failed because DCSS device %s "
1011 dev_info
->segment_name
);
1015 static int dcssblk_restore(struct device
*dev
)
1017 struct dcssblk_dev_info
*dev_info
;
1018 struct segment_info
*entry
;
1019 unsigned long start
, end
;
1022 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1023 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
1024 segment_unload(entry
->segment_name
);
1025 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
1028 // TODO in_use check ?
1029 segment_warning(rc
, entry
->segment_name
);
1032 if (start
!= entry
->start
|| end
!= entry
->end
) {
1033 pr_err("The address range of DCSS %s changed "
1034 "while the system was suspended\n",
1035 entry
->segment_name
);
1042 panic("fatal dcssblk resume error\n");
1045 static int dcssblk_thaw(struct device
*dev
)
1050 static const struct dev_pm_ops dcssblk_pm_ops
= {
1051 .freeze
= dcssblk_freeze
,
1052 .thaw
= dcssblk_thaw
,
1053 .restore
= dcssblk_restore
,
1056 static struct platform_driver dcssblk_pdrv
= {
1059 .pm
= &dcssblk_pm_ops
,
1063 static struct platform_device
*dcssblk_pdev
;
1067 * The init/exit functions.
1072 platform_device_unregister(dcssblk_pdev
);
1073 platform_driver_unregister(&dcssblk_pdrv
);
1074 root_device_unregister(dcssblk_root_dev
);
1075 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1083 rc
= platform_driver_register(&dcssblk_pdrv
);
1087 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1089 if (IS_ERR(dcssblk_pdev
)) {
1090 rc
= PTR_ERR(dcssblk_pdev
);
1094 dcssblk_root_dev
= root_device_register("dcssblk");
1095 if (IS_ERR(dcssblk_root_dev
)) {
1096 rc
= PTR_ERR(dcssblk_root_dev
);
1099 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1102 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1105 rc
= register_blkdev(0, DCSSBLK_NAME
);
1109 init_rwsem(&dcssblk_devices_sem
);
1111 dcssblk_check_params();
1115 root_device_unregister(dcssblk_root_dev
);
1117 platform_device_unregister(dcssblk_pdev
);
1119 platform_driver_unregister(&dcssblk_pdrv
);
1123 module_init(dcssblk_init
);
1124 module_exit(dcssblk_exit
);
1126 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1127 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1128 "comma-separated list, names in each set separated "
1129 "by commas are separated by colons, each set contains "
1130 "names of contiguous segments and each name max. 8 chars.\n"
1131 "Adding \"(local)\" to the end of each set equals echoing 0 "
1132 "to /sys/devices/dcssblk/<device name>/shared after loading "
1133 "the contiguous segments - \n"
1134 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1136 MODULE_LICENSE("GPL");