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 const struct dax_operations dcssblk_dax_ops
= {
55 .direct_access
= dcssblk_dax_direct_access
,
56 .copy_from_iter
= dcssblk_dax_copy_from_iter
,
59 struct dcssblk_dev_info
{
62 char segment_name
[DCSS_BUS_ID_SIZE
];
68 unsigned char save_pending
;
69 unsigned char is_shared
;
70 struct request_queue
*dcssblk_queue
;
72 struct list_head seg_list
;
73 struct dax_device
*dax_dev
;
78 char segment_name
[DCSS_BUS_ID_SIZE
];
84 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
86 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
89 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
90 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
92 static struct device
*dcssblk_root_dev
;
94 static LIST_HEAD(dcssblk_devices
);
95 static struct rw_semaphore dcssblk_devices_sem
;
98 * release function for segment device.
101 dcssblk_release_segment(struct device
*dev
)
103 struct dcssblk_dev_info
*dev_info
;
104 struct segment_info
*entry
, *temp
;
106 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
107 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
108 list_del(&entry
->lh
);
112 module_put(THIS_MODULE
);
116 * get a minor number. needs to be called with
117 * down_write(&dcssblk_devices_sem) and the
118 * device needs to be enqueued before the semaphore is
122 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
125 struct dcssblk_dev_info
*entry
;
127 if (dev_info
== NULL
)
129 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
131 // test if minor available
132 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
133 if (minor
== entry
->gd
->first_minor
)
135 if (!found
) break; // got unused minor
139 dev_info
->gd
->first_minor
= minor
;
144 * get the struct dcssblk_dev_info from dcssblk_devices
145 * for the given name.
146 * down_read(&dcssblk_devices_sem) must be held.
148 static struct dcssblk_dev_info
*
149 dcssblk_get_device_by_name(char *name
)
151 struct dcssblk_dev_info
*entry
;
153 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
154 if (!strcmp(name
, entry
->segment_name
)) {
162 * get the struct segment_info from seg_list
163 * for the given name.
164 * down_read(&dcssblk_devices_sem) must be held.
166 static struct segment_info
*
167 dcssblk_get_segment_by_name(char *name
)
169 struct dcssblk_dev_info
*dev_info
;
170 struct segment_info
*entry
;
172 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
173 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
174 if (!strcmp(name
, entry
->segment_name
))
182 * get the highest address of the multi-segment block.
185 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
187 unsigned long highest_addr
;
188 struct segment_info
*entry
;
191 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
192 if (highest_addr
< entry
->end
)
193 highest_addr
= entry
->end
;
199 * get the lowest address of the multi-segment block.
202 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
205 unsigned long lowest_addr
;
206 struct segment_info
*entry
;
210 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
211 if (set_first
== 0) {
212 lowest_addr
= entry
->start
;
215 if (lowest_addr
> entry
->start
)
216 lowest_addr
= entry
->start
;
223 * Check continuity of segments.
226 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
229 struct segment_info
*sort_list
, *entry
, temp
;
231 if (dev_info
->num_of_segments
<= 1)
235 sizeof(struct segment_info
) * dev_info
->num_of_segments
,
237 if (sort_list
== NULL
)
240 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
241 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
246 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
247 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
248 if (sort_list
[j
].start
> sort_list
[i
].start
) {
249 memcpy(&temp
, &sort_list
[i
],
250 sizeof(struct segment_info
));
251 memcpy(&sort_list
[i
], &sort_list
[j
],
252 sizeof(struct segment_info
));
253 memcpy(&sort_list
[j
], &temp
,
254 sizeof(struct segment_info
));
257 /* check continuity */
258 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
259 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
260 pr_err("Adjacent DCSSs %s and %s are not "
261 "contiguous\n", sort_list
[i
].segment_name
,
262 sort_list
[i
+1].segment_name
);
266 /* EN and EW are allowed in a block device */
267 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
268 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
269 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
270 !(sort_list
[i
+1].segment_type
&
271 SEGMENT_EXCLUSIVE
) ||
272 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
273 pr_err("DCSS %s and DCSS %s have "
274 "incompatible types\n",
275 sort_list
[i
].segment_name
,
276 sort_list
[i
+1].segment_name
);
292 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
296 /* already loaded? */
297 down_read(&dcssblk_devices_sem
);
298 *seg_info
= dcssblk_get_segment_by_name(name
);
299 up_read(&dcssblk_devices_sem
);
300 if (*seg_info
!= NULL
)
303 /* get a struct segment_info */
304 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
305 if (*seg_info
== NULL
)
308 strcpy((*seg_info
)->segment_name
, name
);
310 /* load the segment */
311 rc
= segment_load(name
, SEGMENT_SHARED
,
312 &(*seg_info
)->start
, &(*seg_info
)->end
);
314 segment_warning(rc
, (*seg_info
)->segment_name
);
317 INIT_LIST_HEAD(&(*seg_info
)->lh
);
318 (*seg_info
)->segment_type
= rc
;
324 * device attribute for switching shared/nonshared (exclusive)
325 * operation (show + store)
328 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
330 struct dcssblk_dev_info
*dev_info
;
332 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
333 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
337 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
339 struct dcssblk_dev_info
*dev_info
;
340 struct segment_info
*entry
, *temp
;
343 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
345 down_write(&dcssblk_devices_sem
);
346 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
347 if (atomic_read(&dev_info
->use_count
)) {
351 if (inbuf
[0] == '1') {
352 /* reload segments in shared mode */
353 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
354 rc
= segment_modify_shared(entry
->segment_name
,
357 BUG_ON(rc
== -EINVAL
);
362 dev_info
->is_shared
= 1;
363 switch (dev_info
->segment_type
) {
367 set_disk_ro(dev_info
->gd
, 1);
369 } else if (inbuf
[0] == '0') {
370 /* reload segments in exclusive mode */
371 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
372 pr_err("DCSS %s is of type SC and cannot be "
373 "loaded as exclusive-writable\n",
374 dev_info
->segment_name
);
378 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
379 rc
= segment_modify_shared(entry
->segment_name
,
382 BUG_ON(rc
== -EINVAL
);
387 dev_info
->is_shared
= 0;
388 set_disk_ro(dev_info
->gd
, 0);
397 pr_err("DCSS device %s is removed after a failed access mode "
398 "change\n", dev_info
->segment_name
);
400 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
402 segment_unload(entry
->segment_name
);
404 list_del(&dev_info
->lh
);
406 kill_dax(dev_info
->dax_dev
);
407 put_dax(dev_info
->dax_dev
);
408 del_gendisk(dev_info
->gd
);
409 blk_cleanup_queue(dev_info
->dcssblk_queue
);
410 dev_info
->gd
->queue
= NULL
;
411 put_disk(dev_info
->gd
);
412 up_write(&dcssblk_devices_sem
);
414 if (device_remove_file_self(dev
, attr
)) {
415 device_unregister(dev
);
420 up_write(&dcssblk_devices_sem
);
423 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
424 dcssblk_shared_store
);
427 * device attribute for save operation on current copy
428 * of the segment. If the segment is busy, saving will
429 * become pending until it gets released, which can be
430 * undone by storing a non-true value to this entry.
434 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
436 struct dcssblk_dev_info
*dev_info
;
438 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
439 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
443 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
445 struct dcssblk_dev_info
*dev_info
;
446 struct segment_info
*entry
;
448 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
450 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
452 down_write(&dcssblk_devices_sem
);
453 if (inbuf
[0] == '1') {
454 if (atomic_read(&dev_info
->use_count
) == 0) {
455 // device is idle => we save immediately
456 pr_info("All DCSSs that map to device %s are "
457 "saved\n", dev_info
->segment_name
);
458 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
459 if (entry
->segment_type
== SEG_TYPE_EN
||
460 entry
->segment_type
== SEG_TYPE_SN
)
461 pr_warn("DCSS %s is of type SN or EN"
462 " and cannot be saved\n",
463 entry
->segment_name
);
465 segment_save(entry
->segment_name
);
468 // device is busy => we save it when it becomes
469 // idle in dcssblk_release
470 pr_info("Device %s is in use, its DCSSs will be "
471 "saved when it becomes idle\n",
472 dev_info
->segment_name
);
473 dev_info
->save_pending
= 1;
475 } else if (inbuf
[0] == '0') {
476 if (dev_info
->save_pending
) {
477 // device is busy & the user wants to undo his save
479 dev_info
->save_pending
= 0;
480 pr_info("A pending save request for device %s "
481 "has been canceled\n",
482 dev_info
->segment_name
);
485 up_write(&dcssblk_devices_sem
);
488 up_write(&dcssblk_devices_sem
);
491 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
495 * device attribute for showing all segments in a device
498 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
503 struct dcssblk_dev_info
*dev_info
;
504 struct segment_info
*entry
;
506 down_read(&dcssblk_devices_sem
);
507 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
510 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
511 strcpy(&buf
[i
], entry
->segment_name
);
512 i
+= strlen(entry
->segment_name
);
516 up_read(&dcssblk_devices_sem
);
519 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
521 static struct attribute
*dcssblk_dev_attrs
[] = {
522 &dev_attr_shared
.attr
,
524 &dev_attr_seglist
.attr
,
527 static struct attribute_group dcssblk_dev_attr_group
= {
528 .attrs
= dcssblk_dev_attrs
,
530 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
531 &dcssblk_dev_attr_group
,
536 * device attribute for adding devices
539 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
541 int rc
, i
, j
, num_of_segments
;
542 struct dcssblk_dev_info
*dev_info
;
543 struct segment_info
*seg_info
, *temp
;
545 unsigned long seg_byte_size
;
549 if (dev
!= dcssblk_root_dev
) {
553 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
558 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
559 if (local_buf
== NULL
) {
568 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
569 for (j
= i
; j
< count
&&
572 (buf
[j
] != '\n'); j
++) {
573 local_buf
[j
-i
] = toupper(buf
[j
]);
575 local_buf
[j
-i
] = '\0';
576 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
581 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
585 * get a struct dcssblk_dev_info
587 if (num_of_segments
== 0) {
588 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
590 if (dev_info
== NULL
) {
594 strcpy(dev_info
->segment_name
, local_buf
);
595 dev_info
->segment_type
= seg_info
->segment_type
;
596 INIT_LIST_HEAD(&dev_info
->seg_list
);
598 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
602 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
606 /* no trailing colon at the end of the input */
607 if ((i
> 0) && (buf
[i
-1] == ':')) {
611 strlcpy(local_buf
, buf
, i
+ 1);
612 dev_info
->num_of_segments
= num_of_segments
;
613 rc
= dcssblk_is_continuous(dev_info
);
617 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
618 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
620 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
621 dev_info
->dev
.release
= dcssblk_release_segment
;
622 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
623 INIT_LIST_HEAD(&dev_info
->lh
);
624 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
625 if (dev_info
->gd
== NULL
) {
629 dev_info
->gd
->major
= dcssblk_major
;
630 dev_info
->gd
->fops
= &dcssblk_devops
;
631 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
632 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
633 dev_info
->gd
->private_data
= dev_info
;
634 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
635 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
636 queue_flag_set_unlocked(QUEUE_FLAG_DAX
, dev_info
->dcssblk_queue
);
638 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
639 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
640 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
641 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
643 dev_info
->save_pending
= 0;
644 dev_info
->is_shared
= 1;
645 dev_info
->dev
.parent
= dcssblk_root_dev
;
648 *get minor, add to list
650 down_write(&dcssblk_devices_sem
);
651 if (dcssblk_get_segment_by_name(local_buf
)) {
655 rc
= dcssblk_assign_free_minor(dev_info
);
658 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
659 dev_info
->gd
->first_minor
);
660 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
662 if (!try_module_get(THIS_MODULE
)) {
667 * register the device
669 rc
= device_register(&dev_info
->dev
);
673 dev_info
->dax_dev
= alloc_dax(dev_info
, dev_info
->gd
->disk_name
,
675 if (!dev_info
->dax_dev
) {
680 get_device(&dev_info
->dev
);
681 device_add_disk(&dev_info
->dev
, dev_info
->gd
);
683 switch (dev_info
->segment_type
) {
687 set_disk_ro(dev_info
->gd
,1);
690 set_disk_ro(dev_info
->gd
,0);
693 up_write(&dcssblk_devices_sem
);
698 list_del(&dev_info
->lh
);
699 blk_cleanup_queue(dev_info
->dcssblk_queue
);
700 dev_info
->gd
->queue
= NULL
;
701 put_disk(dev_info
->gd
);
702 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
703 segment_unload(seg_info
->segment_name
);
705 put_device(&dev_info
->dev
);
706 up_write(&dcssblk_devices_sem
);
709 list_del(&dev_info
->lh
);
711 blk_cleanup_queue(dev_info
->dcssblk_queue
);
712 dev_info
->gd
->queue
= NULL
;
713 put_disk(dev_info
->gd
);
714 up_write(&dcssblk_devices_sem
);
716 if (dev_info
== NULL
)
718 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
719 list_del(&seg_info
->lh
);
720 segment_unload(seg_info
->segment_name
);
731 * device attribute for removing devices
734 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
736 struct dcssblk_dev_info
*dev_info
;
737 struct segment_info
*entry
;
741 if (dev
!= dcssblk_root_dev
) {
744 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
745 if (local_buf
== NULL
) {
751 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
752 local_buf
[i
] = toupper(buf
[i
]);
755 if ((i
== 0) || (i
> 8)) {
760 down_write(&dcssblk_devices_sem
);
761 dev_info
= dcssblk_get_device_by_name(local_buf
);
762 if (dev_info
== NULL
) {
763 up_write(&dcssblk_devices_sem
);
764 pr_warn("Device %s cannot be removed because it is not a known device\n",
769 if (atomic_read(&dev_info
->use_count
) != 0) {
770 up_write(&dcssblk_devices_sem
);
771 pr_warn("Device %s cannot be removed while it is in use\n",
777 list_del(&dev_info
->lh
);
778 kill_dax(dev_info
->dax_dev
);
779 put_dax(dev_info
->dax_dev
);
780 del_gendisk(dev_info
->gd
);
781 blk_cleanup_queue(dev_info
->dcssblk_queue
);
782 dev_info
->gd
->queue
= NULL
;
783 put_disk(dev_info
->gd
);
785 /* unload all related segments */
786 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
787 segment_unload(entry
->segment_name
);
789 up_write(&dcssblk_devices_sem
);
791 device_unregister(&dev_info
->dev
);
792 put_device(&dev_info
->dev
);
801 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
803 struct dcssblk_dev_info
*dev_info
;
806 dev_info
= bdev
->bd_disk
->private_data
;
807 if (NULL
== dev_info
) {
811 atomic_inc(&dev_info
->use_count
);
812 bdev
->bd_block_size
= 4096;
819 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
821 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
822 struct segment_info
*entry
;
828 down_write(&dcssblk_devices_sem
);
829 if (atomic_dec_and_test(&dev_info
->use_count
)
830 && (dev_info
->save_pending
)) {
831 pr_info("Device %s has become idle and is being saved "
832 "now\n", dev_info
->segment_name
);
833 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
834 if (entry
->segment_type
== SEG_TYPE_EN
||
835 entry
->segment_type
== SEG_TYPE_SN
)
836 pr_warn("DCSS %s is of type SN or EN and cannot"
837 " be saved\n", entry
->segment_name
);
839 segment_save(entry
->segment_name
);
841 dev_info
->save_pending
= 0;
843 up_write(&dcssblk_devices_sem
);
847 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
849 struct dcssblk_dev_info
*dev_info
;
851 struct bvec_iter iter
;
853 unsigned long page_addr
;
854 unsigned long source_addr
;
855 unsigned long bytes_done
;
857 blk_queue_split(q
, &bio
);
860 dev_info
= bio
->bi_disk
->private_data
;
861 if (dev_info
== NULL
)
863 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
864 (bio
->bi_iter
.bi_size
& 4095) != 0)
865 /* Request is not page-aligned. */
867 if (bio_end_sector(bio
) > get_capacity(bio
->bi_disk
)) {
868 /* Request beyond end of DCSS segment. */
871 /* verify data transfer direction */
872 if (dev_info
->is_shared
) {
873 switch (dev_info
->segment_type
) {
877 /* cannot write to these segments */
878 if (bio_data_dir(bio
) == WRITE
) {
879 pr_warn("Writing to %s failed because it is a read-only device\n",
880 dev_name(&dev_info
->dev
));
886 index
= (bio
->bi_iter
.bi_sector
>> 3);
887 bio_for_each_segment(bvec
, bio
, iter
) {
888 page_addr
= (unsigned long)
889 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
890 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
891 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
894 if (bio_data_dir(bio
) == READ
) {
895 memcpy((void*)page_addr
, (void*)source_addr
,
898 memcpy((void*)source_addr
, (void*)page_addr
,
901 bytes_done
+= bvec
.bv_len
;
904 return BLK_QC_T_NONE
;
907 return BLK_QC_T_NONE
;
911 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
912 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
914 resource_size_t offset
= pgoff
* PAGE_SIZE
;
915 unsigned long dev_sz
;
917 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
918 *kaddr
= (void *) dev_info
->start
+ offset
;
919 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
),
920 PFN_DEV
|PFN_SPECIAL
);
922 return (dev_sz
- offset
) / PAGE_SIZE
;
926 dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
927 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
929 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
931 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
935 dcssblk_check_params(void)
938 char buf
[DCSSBLK_PARM_LEN
+ 1];
939 struct dcssblk_dev_info
*dev_info
;
941 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
943 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
944 (dcssblk_segments
[j
] != ',') &&
945 (dcssblk_segments
[j
] != '\0') &&
946 (dcssblk_segments
[j
] != '('); j
++)
948 buf
[j
-i
] = dcssblk_segments
[j
];
951 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
952 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
953 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
954 buf
[k
] = toupper(buf
[k
]);
956 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
957 down_read(&dcssblk_devices_sem
);
958 dev_info
= dcssblk_get_device_by_name(buf
);
959 up_read(&dcssblk_devices_sem
);
961 dcssblk_shared_store(&dev_info
->dev
,
965 while ((dcssblk_segments
[j
] != ',') &&
966 (dcssblk_segments
[j
] != '\0'))
970 if (dcssblk_segments
[j
] == '\0')
979 static int dcssblk_freeze(struct device
*dev
)
981 struct dcssblk_dev_info
*dev_info
;
984 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
985 switch (dev_info
->segment_type
) {
989 if (!dev_info
->is_shared
)
1000 pr_err("Suspending the system failed because DCSS device %s "
1002 dev_info
->segment_name
);
1006 static int dcssblk_restore(struct device
*dev
)
1008 struct dcssblk_dev_info
*dev_info
;
1009 struct segment_info
*entry
;
1010 unsigned long start
, end
;
1013 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1014 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
1015 segment_unload(entry
->segment_name
);
1016 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
1019 // TODO in_use check ?
1020 segment_warning(rc
, entry
->segment_name
);
1023 if (start
!= entry
->start
|| end
!= entry
->end
) {
1024 pr_err("The address range of DCSS %s changed "
1025 "while the system was suspended\n",
1026 entry
->segment_name
);
1033 panic("fatal dcssblk resume error\n");
1036 static int dcssblk_thaw(struct device
*dev
)
1041 static const struct dev_pm_ops dcssblk_pm_ops
= {
1042 .freeze
= dcssblk_freeze
,
1043 .thaw
= dcssblk_thaw
,
1044 .restore
= dcssblk_restore
,
1047 static struct platform_driver dcssblk_pdrv
= {
1050 .pm
= &dcssblk_pm_ops
,
1054 static struct platform_device
*dcssblk_pdev
;
1058 * The init/exit functions.
1063 platform_device_unregister(dcssblk_pdev
);
1064 platform_driver_unregister(&dcssblk_pdrv
);
1065 root_device_unregister(dcssblk_root_dev
);
1066 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1074 rc
= platform_driver_register(&dcssblk_pdrv
);
1078 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1080 if (IS_ERR(dcssblk_pdev
)) {
1081 rc
= PTR_ERR(dcssblk_pdev
);
1085 dcssblk_root_dev
= root_device_register("dcssblk");
1086 if (IS_ERR(dcssblk_root_dev
)) {
1087 rc
= PTR_ERR(dcssblk_root_dev
);
1090 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1093 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1096 rc
= register_blkdev(0, DCSSBLK_NAME
);
1100 init_rwsem(&dcssblk_devices_sem
);
1102 dcssblk_check_params();
1106 root_device_unregister(dcssblk_root_dev
);
1108 platform_device_unregister(dcssblk_pdev
);
1110 platform_driver_unregister(&dcssblk_pdrv
);
1114 module_init(dcssblk_init
);
1115 module_exit(dcssblk_exit
);
1117 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1118 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1119 "comma-separated list, names in each set separated "
1120 "by commas are separated by colons, each set contains "
1121 "names of contiguous segments and each name max. 8 chars.\n"
1122 "Adding \"(local)\" to the end of each set equals echoing 0 "
1123 "to /sys/devices/dcssblk/<device name>/shared after loading "
1124 "the contiguous segments - \n"
1125 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1127 MODULE_LICENSE("GPL");