2 * dcssblk.c -- the S/390 block driver for dcss memory
4 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
7 #define KMSG_COMPONENT "dcssblk"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/ctype.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/blkdev.h>
17 #include <linux/completion.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/pfn_t.h>
21 #include <linux/dax.h>
22 #include <asm/extmem.h>
25 #define DCSSBLK_NAME "dcssblk"
26 #define DCSSBLK_MINORS_PER_DISK 1
27 #define DCSSBLK_PARM_LEN 400
28 #define DCSS_BUS_ID_SIZE 20
30 static int dcssblk_open(struct block_device
*bdev
, fmode_t mode
);
31 static void dcssblk_release(struct gendisk
*disk
, fmode_t mode
);
32 static blk_qc_t
dcssblk_make_request(struct request_queue
*q
,
34 static long dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
35 long nr_pages
, void **kaddr
, pfn_t
*pfn
);
37 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
39 static int dcssblk_major
;
40 static const struct block_device_operations dcssblk_devops
= {
43 .release
= dcssblk_release
,
46 static const struct dax_operations dcssblk_dax_ops
= {
47 .direct_access
= dcssblk_dax_direct_access
,
50 struct dcssblk_dev_info
{
53 char segment_name
[DCSS_BUS_ID_SIZE
];
59 unsigned char save_pending
;
60 unsigned char is_shared
;
61 struct request_queue
*dcssblk_queue
;
63 struct list_head seg_list
;
64 struct dax_device
*dax_dev
;
69 char segment_name
[DCSS_BUS_ID_SIZE
];
75 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
77 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
80 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
81 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
83 static struct device
*dcssblk_root_dev
;
85 static LIST_HEAD(dcssblk_devices
);
86 static struct rw_semaphore dcssblk_devices_sem
;
89 * release function for segment device.
92 dcssblk_release_segment(struct device
*dev
)
94 struct dcssblk_dev_info
*dev_info
;
95 struct segment_info
*entry
, *temp
;
97 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
98 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
103 module_put(THIS_MODULE
);
107 * get a minor number. needs to be called with
108 * down_write(&dcssblk_devices_sem) and the
109 * device needs to be enqueued before the semaphore is
113 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
116 struct dcssblk_dev_info
*entry
;
118 if (dev_info
== NULL
)
120 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
122 // test if minor available
123 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
124 if (minor
== entry
->gd
->first_minor
)
126 if (!found
) break; // got unused minor
130 dev_info
->gd
->first_minor
= minor
;
135 * get the struct dcssblk_dev_info from dcssblk_devices
136 * for the given name.
137 * down_read(&dcssblk_devices_sem) must be held.
139 static struct dcssblk_dev_info
*
140 dcssblk_get_device_by_name(char *name
)
142 struct dcssblk_dev_info
*entry
;
144 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
145 if (!strcmp(name
, entry
->segment_name
)) {
153 * get the struct segment_info from seg_list
154 * for the given name.
155 * down_read(&dcssblk_devices_sem) must be held.
157 static struct segment_info
*
158 dcssblk_get_segment_by_name(char *name
)
160 struct dcssblk_dev_info
*dev_info
;
161 struct segment_info
*entry
;
163 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
164 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
165 if (!strcmp(name
, entry
->segment_name
))
173 * get the highest address of the multi-segment block.
176 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
178 unsigned long highest_addr
;
179 struct segment_info
*entry
;
182 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
183 if (highest_addr
< entry
->end
)
184 highest_addr
= entry
->end
;
190 * get the lowest address of the multi-segment block.
193 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
196 unsigned long lowest_addr
;
197 struct segment_info
*entry
;
201 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
202 if (set_first
== 0) {
203 lowest_addr
= entry
->start
;
206 if (lowest_addr
> entry
->start
)
207 lowest_addr
= entry
->start
;
214 * Check continuity of segments.
217 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
220 struct segment_info
*sort_list
, *entry
, temp
;
222 if (dev_info
->num_of_segments
<= 1)
226 sizeof(struct segment_info
) * dev_info
->num_of_segments
,
228 if (sort_list
== NULL
)
231 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
232 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
237 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
238 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
239 if (sort_list
[j
].start
> sort_list
[i
].start
) {
240 memcpy(&temp
, &sort_list
[i
],
241 sizeof(struct segment_info
));
242 memcpy(&sort_list
[i
], &sort_list
[j
],
243 sizeof(struct segment_info
));
244 memcpy(&sort_list
[j
], &temp
,
245 sizeof(struct segment_info
));
248 /* check continuity */
249 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
250 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
251 pr_err("Adjacent DCSSs %s and %s are not "
252 "contiguous\n", sort_list
[i
].segment_name
,
253 sort_list
[i
+1].segment_name
);
257 /* EN and EW are allowed in a block device */
258 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
259 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
260 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
261 !(sort_list
[i
+1].segment_type
&
262 SEGMENT_EXCLUSIVE
) ||
263 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
264 pr_err("DCSS %s and DCSS %s have "
265 "incompatible types\n",
266 sort_list
[i
].segment_name
,
267 sort_list
[i
+1].segment_name
);
283 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
287 /* already loaded? */
288 down_read(&dcssblk_devices_sem
);
289 *seg_info
= dcssblk_get_segment_by_name(name
);
290 up_read(&dcssblk_devices_sem
);
291 if (*seg_info
!= NULL
)
294 /* get a struct segment_info */
295 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
296 if (*seg_info
== NULL
)
299 strcpy((*seg_info
)->segment_name
, name
);
301 /* load the segment */
302 rc
= segment_load(name
, SEGMENT_SHARED
,
303 &(*seg_info
)->start
, &(*seg_info
)->end
);
305 segment_warning(rc
, (*seg_info
)->segment_name
);
308 INIT_LIST_HEAD(&(*seg_info
)->lh
);
309 (*seg_info
)->segment_type
= rc
;
315 * device attribute for switching shared/nonshared (exclusive)
316 * operation (show + store)
319 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
321 struct dcssblk_dev_info
*dev_info
;
323 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
324 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
328 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
330 struct dcssblk_dev_info
*dev_info
;
331 struct segment_info
*entry
, *temp
;
334 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
336 down_write(&dcssblk_devices_sem
);
337 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
338 if (atomic_read(&dev_info
->use_count
)) {
342 if (inbuf
[0] == '1') {
343 /* reload segments in shared mode */
344 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
345 rc
= segment_modify_shared(entry
->segment_name
,
348 BUG_ON(rc
== -EINVAL
);
353 dev_info
->is_shared
= 1;
354 switch (dev_info
->segment_type
) {
358 set_disk_ro(dev_info
->gd
, 1);
360 } else if (inbuf
[0] == '0') {
361 /* reload segments in exclusive mode */
362 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
363 pr_err("DCSS %s is of type SC and cannot be "
364 "loaded as exclusive-writable\n",
365 dev_info
->segment_name
);
369 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
370 rc
= segment_modify_shared(entry
->segment_name
,
373 BUG_ON(rc
== -EINVAL
);
378 dev_info
->is_shared
= 0;
379 set_disk_ro(dev_info
->gd
, 0);
388 pr_err("DCSS device %s is removed after a failed access mode "
389 "change\n", dev_info
->segment_name
);
391 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
393 segment_unload(entry
->segment_name
);
395 list_del(&dev_info
->lh
);
397 kill_dax(dev_info
->dax_dev
);
398 put_dax(dev_info
->dax_dev
);
399 del_gendisk(dev_info
->gd
);
400 blk_cleanup_queue(dev_info
->dcssblk_queue
);
401 dev_info
->gd
->queue
= NULL
;
402 put_disk(dev_info
->gd
);
403 up_write(&dcssblk_devices_sem
);
405 if (device_remove_file_self(dev
, attr
)) {
406 device_unregister(dev
);
411 up_write(&dcssblk_devices_sem
);
414 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
415 dcssblk_shared_store
);
418 * device attribute for save operation on current copy
419 * of the segment. If the segment is busy, saving will
420 * become pending until it gets released, which can be
421 * undone by storing a non-true value to this entry.
425 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
427 struct dcssblk_dev_info
*dev_info
;
429 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
430 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
434 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
436 struct dcssblk_dev_info
*dev_info
;
437 struct segment_info
*entry
;
439 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
441 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
443 down_write(&dcssblk_devices_sem
);
444 if (inbuf
[0] == '1') {
445 if (atomic_read(&dev_info
->use_count
) == 0) {
446 // device is idle => we save immediately
447 pr_info("All DCSSs that map to device %s are "
448 "saved\n", dev_info
->segment_name
);
449 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
450 if (entry
->segment_type
== SEG_TYPE_EN
||
451 entry
->segment_type
== SEG_TYPE_SN
)
452 pr_warn("DCSS %s is of type SN or EN"
453 " and cannot be saved\n",
454 entry
->segment_name
);
456 segment_save(entry
->segment_name
);
459 // device is busy => we save it when it becomes
460 // idle in dcssblk_release
461 pr_info("Device %s is in use, its DCSSs will be "
462 "saved when it becomes idle\n",
463 dev_info
->segment_name
);
464 dev_info
->save_pending
= 1;
466 } else if (inbuf
[0] == '0') {
467 if (dev_info
->save_pending
) {
468 // device is busy & the user wants to undo his save
470 dev_info
->save_pending
= 0;
471 pr_info("A pending save request for device %s "
472 "has been canceled\n",
473 dev_info
->segment_name
);
476 up_write(&dcssblk_devices_sem
);
479 up_write(&dcssblk_devices_sem
);
482 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
486 * device attribute for showing all segments in a device
489 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
494 struct dcssblk_dev_info
*dev_info
;
495 struct segment_info
*entry
;
497 down_read(&dcssblk_devices_sem
);
498 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
501 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
502 strcpy(&buf
[i
], entry
->segment_name
);
503 i
+= strlen(entry
->segment_name
);
507 up_read(&dcssblk_devices_sem
);
510 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
512 static struct attribute
*dcssblk_dev_attrs
[] = {
513 &dev_attr_shared
.attr
,
515 &dev_attr_seglist
.attr
,
518 static struct attribute_group dcssblk_dev_attr_group
= {
519 .attrs
= dcssblk_dev_attrs
,
521 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
522 &dcssblk_dev_attr_group
,
527 * device attribute for adding devices
530 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
532 int rc
, i
, j
, num_of_segments
;
533 struct dcssblk_dev_info
*dev_info
;
534 struct segment_info
*seg_info
, *temp
;
536 unsigned long seg_byte_size
;
540 if (dev
!= dcssblk_root_dev
) {
544 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
549 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
550 if (local_buf
== NULL
) {
559 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
560 for (j
= i
; j
< count
&&
563 (buf
[j
] != '\n'); j
++) {
564 local_buf
[j
-i
] = toupper(buf
[j
]);
566 local_buf
[j
-i
] = '\0';
567 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
572 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
576 * get a struct dcssblk_dev_info
578 if (num_of_segments
== 0) {
579 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
581 if (dev_info
== NULL
) {
585 strcpy(dev_info
->segment_name
, local_buf
);
586 dev_info
->segment_type
= seg_info
->segment_type
;
587 INIT_LIST_HEAD(&dev_info
->seg_list
);
589 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
593 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
597 /* no trailing colon at the end of the input */
598 if ((i
> 0) && (buf
[i
-1] == ':')) {
602 strlcpy(local_buf
, buf
, i
+ 1);
603 dev_info
->num_of_segments
= num_of_segments
;
604 rc
= dcssblk_is_continuous(dev_info
);
608 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
609 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
611 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
612 dev_info
->dev
.release
= dcssblk_release_segment
;
613 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
614 INIT_LIST_HEAD(&dev_info
->lh
);
615 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
616 if (dev_info
->gd
== NULL
) {
620 dev_info
->gd
->major
= dcssblk_major
;
621 dev_info
->gd
->fops
= &dcssblk_devops
;
622 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
623 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
624 dev_info
->gd
->private_data
= dev_info
;
625 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
626 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
627 queue_flag_set_unlocked(QUEUE_FLAG_DAX
, dev_info
->dcssblk_queue
);
629 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
630 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
631 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
632 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
634 dev_info
->save_pending
= 0;
635 dev_info
->is_shared
= 1;
636 dev_info
->dev
.parent
= dcssblk_root_dev
;
639 *get minor, add to list
641 down_write(&dcssblk_devices_sem
);
642 if (dcssblk_get_segment_by_name(local_buf
)) {
646 rc
= dcssblk_assign_free_minor(dev_info
);
649 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
650 dev_info
->gd
->first_minor
);
651 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
653 if (!try_module_get(THIS_MODULE
)) {
658 * register the device
660 rc
= device_register(&dev_info
->dev
);
664 dev_info
->dax_dev
= alloc_dax(dev_info
, dev_info
->gd
->disk_name
,
666 if (!dev_info
->dax_dev
) {
671 get_device(&dev_info
->dev
);
672 device_add_disk(&dev_info
->dev
, dev_info
->gd
);
674 switch (dev_info
->segment_type
) {
678 set_disk_ro(dev_info
->gd
,1);
681 set_disk_ro(dev_info
->gd
,0);
684 up_write(&dcssblk_devices_sem
);
689 list_del(&dev_info
->lh
);
690 blk_cleanup_queue(dev_info
->dcssblk_queue
);
691 dev_info
->gd
->queue
= NULL
;
692 put_disk(dev_info
->gd
);
693 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
694 segment_unload(seg_info
->segment_name
);
696 put_device(&dev_info
->dev
);
697 up_write(&dcssblk_devices_sem
);
700 list_del(&dev_info
->lh
);
702 blk_cleanup_queue(dev_info
->dcssblk_queue
);
703 dev_info
->gd
->queue
= NULL
;
704 put_disk(dev_info
->gd
);
705 up_write(&dcssblk_devices_sem
);
707 if (dev_info
== NULL
)
709 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
710 list_del(&seg_info
->lh
);
711 segment_unload(seg_info
->segment_name
);
722 * device attribute for removing devices
725 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
727 struct dcssblk_dev_info
*dev_info
;
728 struct segment_info
*entry
;
732 if (dev
!= dcssblk_root_dev
) {
735 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
736 if (local_buf
== NULL
) {
742 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
743 local_buf
[i
] = toupper(buf
[i
]);
746 if ((i
== 0) || (i
> 8)) {
751 down_write(&dcssblk_devices_sem
);
752 dev_info
= dcssblk_get_device_by_name(local_buf
);
753 if (dev_info
== NULL
) {
754 up_write(&dcssblk_devices_sem
);
755 pr_warn("Device %s cannot be removed because it is not a known device\n",
760 if (atomic_read(&dev_info
->use_count
) != 0) {
761 up_write(&dcssblk_devices_sem
);
762 pr_warn("Device %s cannot be removed while it is in use\n",
768 list_del(&dev_info
->lh
);
769 kill_dax(dev_info
->dax_dev
);
770 put_dax(dev_info
->dax_dev
);
771 del_gendisk(dev_info
->gd
);
772 blk_cleanup_queue(dev_info
->dcssblk_queue
);
773 dev_info
->gd
->queue
= NULL
;
774 put_disk(dev_info
->gd
);
776 /* unload all related segments */
777 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
778 segment_unload(entry
->segment_name
);
780 up_write(&dcssblk_devices_sem
);
782 device_unregister(&dev_info
->dev
);
783 put_device(&dev_info
->dev
);
792 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
794 struct dcssblk_dev_info
*dev_info
;
797 dev_info
= bdev
->bd_disk
->private_data
;
798 if (NULL
== dev_info
) {
802 atomic_inc(&dev_info
->use_count
);
803 bdev
->bd_block_size
= 4096;
810 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
812 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
813 struct segment_info
*entry
;
819 down_write(&dcssblk_devices_sem
);
820 if (atomic_dec_and_test(&dev_info
->use_count
)
821 && (dev_info
->save_pending
)) {
822 pr_info("Device %s has become idle and is being saved "
823 "now\n", dev_info
->segment_name
);
824 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
825 if (entry
->segment_type
== SEG_TYPE_EN
||
826 entry
->segment_type
== SEG_TYPE_SN
)
827 pr_warn("DCSS %s is of type SN or EN and cannot"
828 " be saved\n", entry
->segment_name
);
830 segment_save(entry
->segment_name
);
832 dev_info
->save_pending
= 0;
834 up_write(&dcssblk_devices_sem
);
838 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
840 struct dcssblk_dev_info
*dev_info
;
842 struct bvec_iter iter
;
844 unsigned long page_addr
;
845 unsigned long source_addr
;
846 unsigned long bytes_done
;
848 blk_queue_split(q
, &bio
);
851 dev_info
= bio
->bi_bdev
->bd_disk
->private_data
;
852 if (dev_info
== NULL
)
854 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
855 (bio
->bi_iter
.bi_size
& 4095) != 0)
856 /* Request is not page-aligned. */
858 if (bio_end_sector(bio
) > get_capacity(bio
->bi_bdev
->bd_disk
)) {
859 /* Request beyond end of DCSS segment. */
862 /* verify data transfer direction */
863 if (dev_info
->is_shared
) {
864 switch (dev_info
->segment_type
) {
868 /* cannot write to these segments */
869 if (bio_data_dir(bio
) == WRITE
) {
870 pr_warn("Writing to %s failed because it is a read-only device\n",
871 dev_name(&dev_info
->dev
));
877 index
= (bio
->bi_iter
.bi_sector
>> 3);
878 bio_for_each_segment(bvec
, bio
, iter
) {
879 page_addr
= (unsigned long)
880 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
881 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
882 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
885 if (bio_data_dir(bio
) == READ
) {
886 memcpy((void*)page_addr
, (void*)source_addr
,
889 memcpy((void*)source_addr
, (void*)page_addr
,
892 bytes_done
+= bvec
.bv_len
;
895 return BLK_QC_T_NONE
;
898 return BLK_QC_T_NONE
;
902 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
903 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
905 resource_size_t offset
= pgoff
* PAGE_SIZE
;
906 unsigned long dev_sz
;
908 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
909 *kaddr
= (void *) dev_info
->start
+ offset
;
910 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
), PFN_DEV
);
912 return (dev_sz
- offset
) / PAGE_SIZE
;
916 dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
917 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
919 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
921 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
925 dcssblk_check_params(void)
928 char buf
[DCSSBLK_PARM_LEN
+ 1];
929 struct dcssblk_dev_info
*dev_info
;
931 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
933 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
934 (dcssblk_segments
[j
] != ',') &&
935 (dcssblk_segments
[j
] != '\0') &&
936 (dcssblk_segments
[j
] != '('); j
++)
938 buf
[j
-i
] = dcssblk_segments
[j
];
941 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
942 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
943 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
944 buf
[k
] = toupper(buf
[k
]);
946 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
947 down_read(&dcssblk_devices_sem
);
948 dev_info
= dcssblk_get_device_by_name(buf
);
949 up_read(&dcssblk_devices_sem
);
951 dcssblk_shared_store(&dev_info
->dev
,
955 while ((dcssblk_segments
[j
] != ',') &&
956 (dcssblk_segments
[j
] != '\0'))
960 if (dcssblk_segments
[j
] == '\0')
969 static int dcssblk_freeze(struct device
*dev
)
971 struct dcssblk_dev_info
*dev_info
;
974 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
975 switch (dev_info
->segment_type
) {
979 if (!dev_info
->is_shared
)
990 pr_err("Suspending the system failed because DCSS device %s "
992 dev_info
->segment_name
);
996 static int dcssblk_restore(struct device
*dev
)
998 struct dcssblk_dev_info
*dev_info
;
999 struct segment_info
*entry
;
1000 unsigned long start
, end
;
1003 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1004 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
1005 segment_unload(entry
->segment_name
);
1006 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
1009 // TODO in_use check ?
1010 segment_warning(rc
, entry
->segment_name
);
1013 if (start
!= entry
->start
|| end
!= entry
->end
) {
1014 pr_err("The address range of DCSS %s changed "
1015 "while the system was suspended\n",
1016 entry
->segment_name
);
1023 panic("fatal dcssblk resume error\n");
1026 static int dcssblk_thaw(struct device
*dev
)
1031 static const struct dev_pm_ops dcssblk_pm_ops
= {
1032 .freeze
= dcssblk_freeze
,
1033 .thaw
= dcssblk_thaw
,
1034 .restore
= dcssblk_restore
,
1037 static struct platform_driver dcssblk_pdrv
= {
1040 .pm
= &dcssblk_pm_ops
,
1044 static struct platform_device
*dcssblk_pdev
;
1048 * The init/exit functions.
1053 platform_device_unregister(dcssblk_pdev
);
1054 platform_driver_unregister(&dcssblk_pdrv
);
1055 root_device_unregister(dcssblk_root_dev
);
1056 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1064 rc
= platform_driver_register(&dcssblk_pdrv
);
1068 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1070 if (IS_ERR(dcssblk_pdev
)) {
1071 rc
= PTR_ERR(dcssblk_pdev
);
1075 dcssblk_root_dev
= root_device_register("dcssblk");
1076 if (IS_ERR(dcssblk_root_dev
)) {
1077 rc
= PTR_ERR(dcssblk_root_dev
);
1080 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1083 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1086 rc
= register_blkdev(0, DCSSBLK_NAME
);
1090 init_rwsem(&dcssblk_devices_sem
);
1092 dcssblk_check_params();
1096 root_device_unregister(dcssblk_root_dev
);
1098 platform_device_unregister(dcssblk_pdev
);
1100 platform_driver_unregister(&dcssblk_pdrv
);
1104 module_init(dcssblk_init
);
1105 module_exit(dcssblk_exit
);
1107 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1108 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1109 "comma-separated list, names in each set separated "
1110 "by commas are separated by colons, each set contains "
1111 "names of contiguous segments and each name max. 8 chars.\n"
1112 "Adding \"(local)\" to the end of each set equals echoing 0 "
1113 "to /sys/devices/dcssblk/<device name>/shared after loading "
1114 "the contiguous segments - \n"
1115 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1117 MODULE_LICENSE("GPL");