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/uio.h>
22 #include <linux/dax.h>
23 #include <asm/extmem.h>
26 #define DCSSBLK_NAME "dcssblk"
27 #define DCSSBLK_MINORS_PER_DISK 1
28 #define DCSSBLK_PARM_LEN 400
29 #define DCSS_BUS_ID_SIZE 20
31 static int dcssblk_open(struct block_device
*bdev
, fmode_t mode
);
32 static void dcssblk_release(struct gendisk
*disk
, fmode_t mode
);
33 static blk_qc_t
dcssblk_make_request(struct request_queue
*q
,
35 static long dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
36 long nr_pages
, void **kaddr
, pfn_t
*pfn
);
38 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
40 static int dcssblk_major
;
41 static const struct block_device_operations dcssblk_devops
= {
44 .release
= dcssblk_release
,
47 static size_t dcssblk_dax_copy_from_iter(struct dax_device
*dax_dev
,
48 pgoff_t pgoff
, void *addr
, size_t bytes
, struct iov_iter
*i
)
50 return copy_from_iter(addr
, bytes
, i
);
53 static const struct dax_operations dcssblk_dax_ops
= {
54 .direct_access
= dcssblk_dax_direct_access
,
55 .copy_from_iter
= dcssblk_dax_copy_from_iter
,
58 struct dcssblk_dev_info
{
61 char segment_name
[DCSS_BUS_ID_SIZE
];
67 unsigned char save_pending
;
68 unsigned char is_shared
;
69 struct request_queue
*dcssblk_queue
;
71 struct list_head seg_list
;
72 struct dax_device
*dax_dev
;
77 char segment_name
[DCSS_BUS_ID_SIZE
];
83 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
85 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
88 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
89 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
91 static struct device
*dcssblk_root_dev
;
93 static LIST_HEAD(dcssblk_devices
);
94 static struct rw_semaphore dcssblk_devices_sem
;
97 * release function for segment device.
100 dcssblk_release_segment(struct device
*dev
)
102 struct dcssblk_dev_info
*dev_info
;
103 struct segment_info
*entry
, *temp
;
105 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
106 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
107 list_del(&entry
->lh
);
111 module_put(THIS_MODULE
);
115 * get a minor number. needs to be called with
116 * down_write(&dcssblk_devices_sem) and the
117 * device needs to be enqueued before the semaphore is
121 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
124 struct dcssblk_dev_info
*entry
;
126 if (dev_info
== NULL
)
128 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
130 // test if minor available
131 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
132 if (minor
== entry
->gd
->first_minor
)
134 if (!found
) break; // got unused minor
138 dev_info
->gd
->first_minor
= minor
;
143 * get the struct dcssblk_dev_info from dcssblk_devices
144 * for the given name.
145 * down_read(&dcssblk_devices_sem) must be held.
147 static struct dcssblk_dev_info
*
148 dcssblk_get_device_by_name(char *name
)
150 struct dcssblk_dev_info
*entry
;
152 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
153 if (!strcmp(name
, entry
->segment_name
)) {
161 * get the struct segment_info from seg_list
162 * for the given name.
163 * down_read(&dcssblk_devices_sem) must be held.
165 static struct segment_info
*
166 dcssblk_get_segment_by_name(char *name
)
168 struct dcssblk_dev_info
*dev_info
;
169 struct segment_info
*entry
;
171 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
172 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
173 if (!strcmp(name
, entry
->segment_name
))
181 * get the highest address of the multi-segment block.
184 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
186 unsigned long highest_addr
;
187 struct segment_info
*entry
;
190 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
191 if (highest_addr
< entry
->end
)
192 highest_addr
= entry
->end
;
198 * get the lowest address of the multi-segment block.
201 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
204 unsigned long lowest_addr
;
205 struct segment_info
*entry
;
209 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
210 if (set_first
== 0) {
211 lowest_addr
= entry
->start
;
214 if (lowest_addr
> entry
->start
)
215 lowest_addr
= entry
->start
;
222 * Check continuity of segments.
225 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
228 struct segment_info
*sort_list
, *entry
, temp
;
230 if (dev_info
->num_of_segments
<= 1)
234 sizeof(struct segment_info
) * dev_info
->num_of_segments
,
236 if (sort_list
== NULL
)
239 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
240 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
245 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
246 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
247 if (sort_list
[j
].start
> sort_list
[i
].start
) {
248 memcpy(&temp
, &sort_list
[i
],
249 sizeof(struct segment_info
));
250 memcpy(&sort_list
[i
], &sort_list
[j
],
251 sizeof(struct segment_info
));
252 memcpy(&sort_list
[j
], &temp
,
253 sizeof(struct segment_info
));
256 /* check continuity */
257 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
258 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
259 pr_err("Adjacent DCSSs %s and %s are not "
260 "contiguous\n", sort_list
[i
].segment_name
,
261 sort_list
[i
+1].segment_name
);
265 /* EN and EW are allowed in a block device */
266 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
267 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
268 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
269 !(sort_list
[i
+1].segment_type
&
270 SEGMENT_EXCLUSIVE
) ||
271 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
272 pr_err("DCSS %s and DCSS %s have "
273 "incompatible types\n",
274 sort_list
[i
].segment_name
,
275 sort_list
[i
+1].segment_name
);
291 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
295 /* already loaded? */
296 down_read(&dcssblk_devices_sem
);
297 *seg_info
= dcssblk_get_segment_by_name(name
);
298 up_read(&dcssblk_devices_sem
);
299 if (*seg_info
!= NULL
)
302 /* get a struct segment_info */
303 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
304 if (*seg_info
== NULL
)
307 strcpy((*seg_info
)->segment_name
, name
);
309 /* load the segment */
310 rc
= segment_load(name
, SEGMENT_SHARED
,
311 &(*seg_info
)->start
, &(*seg_info
)->end
);
313 segment_warning(rc
, (*seg_info
)->segment_name
);
316 INIT_LIST_HEAD(&(*seg_info
)->lh
);
317 (*seg_info
)->segment_type
= rc
;
323 * device attribute for switching shared/nonshared (exclusive)
324 * operation (show + store)
327 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
329 struct dcssblk_dev_info
*dev_info
;
331 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
332 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
336 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
338 struct dcssblk_dev_info
*dev_info
;
339 struct segment_info
*entry
, *temp
;
342 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
344 down_write(&dcssblk_devices_sem
);
345 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
346 if (atomic_read(&dev_info
->use_count
)) {
350 if (inbuf
[0] == '1') {
351 /* reload segments in shared mode */
352 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
353 rc
= segment_modify_shared(entry
->segment_name
,
356 BUG_ON(rc
== -EINVAL
);
361 dev_info
->is_shared
= 1;
362 switch (dev_info
->segment_type
) {
366 set_disk_ro(dev_info
->gd
, 1);
368 } else if (inbuf
[0] == '0') {
369 /* reload segments in exclusive mode */
370 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
371 pr_err("DCSS %s is of type SC and cannot be "
372 "loaded as exclusive-writable\n",
373 dev_info
->segment_name
);
377 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
378 rc
= segment_modify_shared(entry
->segment_name
,
381 BUG_ON(rc
== -EINVAL
);
386 dev_info
->is_shared
= 0;
387 set_disk_ro(dev_info
->gd
, 0);
396 pr_err("DCSS device %s is removed after a failed access mode "
397 "change\n", dev_info
->segment_name
);
399 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
401 segment_unload(entry
->segment_name
);
403 list_del(&dev_info
->lh
);
405 kill_dax(dev_info
->dax_dev
);
406 put_dax(dev_info
->dax_dev
);
407 del_gendisk(dev_info
->gd
);
408 blk_cleanup_queue(dev_info
->dcssblk_queue
);
409 dev_info
->gd
->queue
= NULL
;
410 put_disk(dev_info
->gd
);
411 up_write(&dcssblk_devices_sem
);
413 if (device_remove_file_self(dev
, attr
)) {
414 device_unregister(dev
);
419 up_write(&dcssblk_devices_sem
);
422 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
423 dcssblk_shared_store
);
426 * device attribute for save operation on current copy
427 * of the segment. If the segment is busy, saving will
428 * become pending until it gets released, which can be
429 * undone by storing a non-true value to this entry.
433 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
435 struct dcssblk_dev_info
*dev_info
;
437 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
438 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
442 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
444 struct dcssblk_dev_info
*dev_info
;
445 struct segment_info
*entry
;
447 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
449 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
451 down_write(&dcssblk_devices_sem
);
452 if (inbuf
[0] == '1') {
453 if (atomic_read(&dev_info
->use_count
) == 0) {
454 // device is idle => we save immediately
455 pr_info("All DCSSs that map to device %s are "
456 "saved\n", dev_info
->segment_name
);
457 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
458 if (entry
->segment_type
== SEG_TYPE_EN
||
459 entry
->segment_type
== SEG_TYPE_SN
)
460 pr_warn("DCSS %s is of type SN or EN"
461 " and cannot be saved\n",
462 entry
->segment_name
);
464 segment_save(entry
->segment_name
);
467 // device is busy => we save it when it becomes
468 // idle in dcssblk_release
469 pr_info("Device %s is in use, its DCSSs will be "
470 "saved when it becomes idle\n",
471 dev_info
->segment_name
);
472 dev_info
->save_pending
= 1;
474 } else if (inbuf
[0] == '0') {
475 if (dev_info
->save_pending
) {
476 // device is busy & the user wants to undo his save
478 dev_info
->save_pending
= 0;
479 pr_info("A pending save request for device %s "
480 "has been canceled\n",
481 dev_info
->segment_name
);
484 up_write(&dcssblk_devices_sem
);
487 up_write(&dcssblk_devices_sem
);
490 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
494 * device attribute for showing all segments in a device
497 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
502 struct dcssblk_dev_info
*dev_info
;
503 struct segment_info
*entry
;
505 down_read(&dcssblk_devices_sem
);
506 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
509 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
510 strcpy(&buf
[i
], entry
->segment_name
);
511 i
+= strlen(entry
->segment_name
);
515 up_read(&dcssblk_devices_sem
);
518 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
520 static struct attribute
*dcssblk_dev_attrs
[] = {
521 &dev_attr_shared
.attr
,
523 &dev_attr_seglist
.attr
,
526 static struct attribute_group dcssblk_dev_attr_group
= {
527 .attrs
= dcssblk_dev_attrs
,
529 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
530 &dcssblk_dev_attr_group
,
535 * device attribute for adding devices
538 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
540 int rc
, i
, j
, num_of_segments
;
541 struct dcssblk_dev_info
*dev_info
;
542 struct segment_info
*seg_info
, *temp
;
544 unsigned long seg_byte_size
;
548 if (dev
!= dcssblk_root_dev
) {
552 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
557 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
558 if (local_buf
== NULL
) {
567 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
568 for (j
= i
; j
< count
&&
571 (buf
[j
] != '\n'); j
++) {
572 local_buf
[j
-i
] = toupper(buf
[j
]);
574 local_buf
[j
-i
] = '\0';
575 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
580 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
584 * get a struct dcssblk_dev_info
586 if (num_of_segments
== 0) {
587 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
589 if (dev_info
== NULL
) {
593 strcpy(dev_info
->segment_name
, local_buf
);
594 dev_info
->segment_type
= seg_info
->segment_type
;
595 INIT_LIST_HEAD(&dev_info
->seg_list
);
597 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
601 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
605 /* no trailing colon at the end of the input */
606 if ((i
> 0) && (buf
[i
-1] == ':')) {
610 strlcpy(local_buf
, buf
, i
+ 1);
611 dev_info
->num_of_segments
= num_of_segments
;
612 rc
= dcssblk_is_continuous(dev_info
);
616 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
617 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
619 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
620 dev_info
->dev
.release
= dcssblk_release_segment
;
621 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
622 INIT_LIST_HEAD(&dev_info
->lh
);
623 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
624 if (dev_info
->gd
== NULL
) {
628 dev_info
->gd
->major
= dcssblk_major
;
629 dev_info
->gd
->fops
= &dcssblk_devops
;
630 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
631 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
632 dev_info
->gd
->private_data
= dev_info
;
633 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
634 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
635 queue_flag_set_unlocked(QUEUE_FLAG_DAX
, dev_info
->dcssblk_queue
);
637 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
638 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
639 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
640 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
642 dev_info
->save_pending
= 0;
643 dev_info
->is_shared
= 1;
644 dev_info
->dev
.parent
= dcssblk_root_dev
;
647 *get minor, add to list
649 down_write(&dcssblk_devices_sem
);
650 if (dcssblk_get_segment_by_name(local_buf
)) {
654 rc
= dcssblk_assign_free_minor(dev_info
);
657 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
658 dev_info
->gd
->first_minor
);
659 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
661 if (!try_module_get(THIS_MODULE
)) {
666 * register the device
668 rc
= device_register(&dev_info
->dev
);
672 dev_info
->dax_dev
= alloc_dax(dev_info
, dev_info
->gd
->disk_name
,
674 if (!dev_info
->dax_dev
) {
679 get_device(&dev_info
->dev
);
680 device_add_disk(&dev_info
->dev
, dev_info
->gd
);
682 switch (dev_info
->segment_type
) {
686 set_disk_ro(dev_info
->gd
,1);
689 set_disk_ro(dev_info
->gd
,0);
692 up_write(&dcssblk_devices_sem
);
697 list_del(&dev_info
->lh
);
698 blk_cleanup_queue(dev_info
->dcssblk_queue
);
699 dev_info
->gd
->queue
= NULL
;
700 put_disk(dev_info
->gd
);
701 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
702 segment_unload(seg_info
->segment_name
);
704 put_device(&dev_info
->dev
);
705 up_write(&dcssblk_devices_sem
);
708 list_del(&dev_info
->lh
);
710 blk_cleanup_queue(dev_info
->dcssblk_queue
);
711 dev_info
->gd
->queue
= NULL
;
712 put_disk(dev_info
->gd
);
713 up_write(&dcssblk_devices_sem
);
715 if (dev_info
== NULL
)
717 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
718 list_del(&seg_info
->lh
);
719 segment_unload(seg_info
->segment_name
);
730 * device attribute for removing devices
733 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
735 struct dcssblk_dev_info
*dev_info
;
736 struct segment_info
*entry
;
740 if (dev
!= dcssblk_root_dev
) {
743 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
744 if (local_buf
== NULL
) {
750 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
751 local_buf
[i
] = toupper(buf
[i
]);
754 if ((i
== 0) || (i
> 8)) {
759 down_write(&dcssblk_devices_sem
);
760 dev_info
= dcssblk_get_device_by_name(local_buf
);
761 if (dev_info
== NULL
) {
762 up_write(&dcssblk_devices_sem
);
763 pr_warn("Device %s cannot be removed because it is not a known device\n",
768 if (atomic_read(&dev_info
->use_count
) != 0) {
769 up_write(&dcssblk_devices_sem
);
770 pr_warn("Device %s cannot be removed while it is in use\n",
776 list_del(&dev_info
->lh
);
777 kill_dax(dev_info
->dax_dev
);
778 put_dax(dev_info
->dax_dev
);
779 del_gendisk(dev_info
->gd
);
780 blk_cleanup_queue(dev_info
->dcssblk_queue
);
781 dev_info
->gd
->queue
= NULL
;
782 put_disk(dev_info
->gd
);
784 /* unload all related segments */
785 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
786 segment_unload(entry
->segment_name
);
788 up_write(&dcssblk_devices_sem
);
790 device_unregister(&dev_info
->dev
);
791 put_device(&dev_info
->dev
);
800 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
802 struct dcssblk_dev_info
*dev_info
;
805 dev_info
= bdev
->bd_disk
->private_data
;
806 if (NULL
== dev_info
) {
810 atomic_inc(&dev_info
->use_count
);
811 bdev
->bd_block_size
= 4096;
818 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
820 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
821 struct segment_info
*entry
;
827 down_write(&dcssblk_devices_sem
);
828 if (atomic_dec_and_test(&dev_info
->use_count
)
829 && (dev_info
->save_pending
)) {
830 pr_info("Device %s has become idle and is being saved "
831 "now\n", dev_info
->segment_name
);
832 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
833 if (entry
->segment_type
== SEG_TYPE_EN
||
834 entry
->segment_type
== SEG_TYPE_SN
)
835 pr_warn("DCSS %s is of type SN or EN and cannot"
836 " be saved\n", entry
->segment_name
);
838 segment_save(entry
->segment_name
);
840 dev_info
->save_pending
= 0;
842 up_write(&dcssblk_devices_sem
);
846 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
848 struct dcssblk_dev_info
*dev_info
;
850 struct bvec_iter iter
;
852 unsigned long page_addr
;
853 unsigned long source_addr
;
854 unsigned long bytes_done
;
856 blk_queue_split(q
, &bio
);
859 dev_info
= bio
->bi_disk
->private_data
;
860 if (dev_info
== NULL
)
862 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
863 (bio
->bi_iter
.bi_size
& 4095) != 0)
864 /* Request is not page-aligned. */
866 if (bio_end_sector(bio
) > get_capacity(bio
->bi_disk
)) {
867 /* Request beyond end of DCSS segment. */
870 /* verify data transfer direction */
871 if (dev_info
->is_shared
) {
872 switch (dev_info
->segment_type
) {
876 /* cannot write to these segments */
877 if (bio_data_dir(bio
) == WRITE
) {
878 pr_warn("Writing to %s failed because it is a read-only device\n",
879 dev_name(&dev_info
->dev
));
885 index
= (bio
->bi_iter
.bi_sector
>> 3);
886 bio_for_each_segment(bvec
, bio
, iter
) {
887 page_addr
= (unsigned long)
888 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
889 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
890 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
893 if (bio_data_dir(bio
) == READ
) {
894 memcpy((void*)page_addr
, (void*)source_addr
,
897 memcpy((void*)source_addr
, (void*)page_addr
,
900 bytes_done
+= bvec
.bv_len
;
903 return BLK_QC_T_NONE
;
906 return BLK_QC_T_NONE
;
910 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
911 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
913 resource_size_t offset
= pgoff
* PAGE_SIZE
;
914 unsigned long dev_sz
;
916 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
917 *kaddr
= (void *) dev_info
->start
+ offset
;
918 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
), PFN_DEV
);
920 return (dev_sz
- offset
) / PAGE_SIZE
;
924 dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
925 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
927 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
929 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
933 dcssblk_check_params(void)
936 char buf
[DCSSBLK_PARM_LEN
+ 1];
937 struct dcssblk_dev_info
*dev_info
;
939 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
941 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
942 (dcssblk_segments
[j
] != ',') &&
943 (dcssblk_segments
[j
] != '\0') &&
944 (dcssblk_segments
[j
] != '('); j
++)
946 buf
[j
-i
] = dcssblk_segments
[j
];
949 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
950 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
951 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
952 buf
[k
] = toupper(buf
[k
]);
954 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
955 down_read(&dcssblk_devices_sem
);
956 dev_info
= dcssblk_get_device_by_name(buf
);
957 up_read(&dcssblk_devices_sem
);
959 dcssblk_shared_store(&dev_info
->dev
,
963 while ((dcssblk_segments
[j
] != ',') &&
964 (dcssblk_segments
[j
] != '\0'))
968 if (dcssblk_segments
[j
] == '\0')
977 static int dcssblk_freeze(struct device
*dev
)
979 struct dcssblk_dev_info
*dev_info
;
982 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
983 switch (dev_info
->segment_type
) {
987 if (!dev_info
->is_shared
)
998 pr_err("Suspending the system failed because DCSS device %s "
1000 dev_info
->segment_name
);
1004 static int dcssblk_restore(struct device
*dev
)
1006 struct dcssblk_dev_info
*dev_info
;
1007 struct segment_info
*entry
;
1008 unsigned long start
, end
;
1011 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
1012 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
1013 segment_unload(entry
->segment_name
);
1014 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
1017 // TODO in_use check ?
1018 segment_warning(rc
, entry
->segment_name
);
1021 if (start
!= entry
->start
|| end
!= entry
->end
) {
1022 pr_err("The address range of DCSS %s changed "
1023 "while the system was suspended\n",
1024 entry
->segment_name
);
1031 panic("fatal dcssblk resume error\n");
1034 static int dcssblk_thaw(struct device
*dev
)
1039 static const struct dev_pm_ops dcssblk_pm_ops
= {
1040 .freeze
= dcssblk_freeze
,
1041 .thaw
= dcssblk_thaw
,
1042 .restore
= dcssblk_restore
,
1045 static struct platform_driver dcssblk_pdrv
= {
1048 .pm
= &dcssblk_pm_ops
,
1052 static struct platform_device
*dcssblk_pdev
;
1056 * The init/exit functions.
1061 platform_device_unregister(dcssblk_pdev
);
1062 platform_driver_unregister(&dcssblk_pdrv
);
1063 root_device_unregister(dcssblk_root_dev
);
1064 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1072 rc
= platform_driver_register(&dcssblk_pdrv
);
1076 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1078 if (IS_ERR(dcssblk_pdev
)) {
1079 rc
= PTR_ERR(dcssblk_pdev
);
1083 dcssblk_root_dev
= root_device_register("dcssblk");
1084 if (IS_ERR(dcssblk_root_dev
)) {
1085 rc
= PTR_ERR(dcssblk_root_dev
);
1088 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1091 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1094 rc
= register_blkdev(0, DCSSBLK_NAME
);
1098 init_rwsem(&dcssblk_devices_sem
);
1100 dcssblk_check_params();
1104 root_device_unregister(dcssblk_root_dev
);
1106 platform_device_unregister(dcssblk_pdev
);
1108 platform_driver_unregister(&dcssblk_pdrv
);
1112 module_init(dcssblk_init
);
1113 module_exit(dcssblk_exit
);
1115 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1116 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1117 "comma-separated list, names in each set separated "
1118 "by commas are separated by colons, each set contains "
1119 "names of contiguous segments and each name max. 8 chars.\n"
1120 "Adding \"(local)\" to the end of each set equals echoing 0 "
1121 "to /sys/devices/dcssblk/<device name>/shared after loading "
1122 "the contiguous segments - \n"
1123 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1125 MODULE_LICENSE("GPL");