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/pfn_t.h>
21 #include <linux/uio.h>
22 #include <linux/dax.h>
24 #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 gendisk
*disk
, blk_mode_t mode
);
32 static void dcssblk_release(struct gendisk
*disk
);
33 static void dcssblk_submit_bio(struct bio
*bio
);
34 static long dcssblk_dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
,
35 long nr_pages
, enum dax_access_mode mode
, void **kaddr
,
38 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
40 static int dcssblk_major
;
41 static const struct block_device_operations dcssblk_devops
= {
43 .submit_bio
= dcssblk_submit_bio
,
45 .release
= dcssblk_release
,
48 static int dcssblk_dax_zero_page_range(struct dax_device
*dax_dev
,
49 pgoff_t pgoff
, size_t nr_pages
)
54 rc
= dax_direct_access(dax_dev
, pgoff
, nr_pages
, DAX_ACCESS
,
57 return dax_mem2blk_err(rc
);
59 memset(kaddr
, 0, nr_pages
<< PAGE_SHIFT
);
60 dax_flush(dax_dev
, kaddr
, nr_pages
<< PAGE_SHIFT
);
64 static const struct dax_operations dcssblk_dax_ops
= {
65 .direct_access
= dcssblk_dax_direct_access
,
66 .zero_page_range
= dcssblk_dax_zero_page_range
,
69 struct dcssblk_dev_info
{
72 char segment_name
[DCSS_BUS_ID_SIZE
];
78 unsigned char save_pending
;
79 unsigned char is_shared
;
81 struct list_head seg_list
;
82 struct dax_device
*dax_dev
;
87 char segment_name
[DCSS_BUS_ID_SIZE
];
93 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
95 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
98 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
99 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
101 static struct device
*dcssblk_root_dev
;
103 static LIST_HEAD(dcssblk_devices
);
104 static struct rw_semaphore dcssblk_devices_sem
;
107 * release function for segment device.
110 dcssblk_release_segment(struct device
*dev
)
112 struct dcssblk_dev_info
*dev_info
;
113 struct segment_info
*entry
, *temp
;
115 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
116 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
117 list_del(&entry
->lh
);
121 module_put(THIS_MODULE
);
125 * get a minor number. needs to be called with
126 * down_write(&dcssblk_devices_sem) and the
127 * device needs to be enqueued before the semaphore is
131 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
134 struct dcssblk_dev_info
*entry
;
136 if (dev_info
== NULL
)
138 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
140 // test if minor available
141 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
142 if (minor
== entry
->gd
->first_minor
)
144 if (!found
) break; // got unused minor
148 dev_info
->gd
->first_minor
= minor
;
153 * get the struct dcssblk_dev_info from dcssblk_devices
154 * for the given name.
155 * down_read(&dcssblk_devices_sem) must be held.
157 static struct dcssblk_dev_info
*
158 dcssblk_get_device_by_name(char *name
)
160 struct dcssblk_dev_info
*entry
;
162 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
163 if (!strcmp(name
, entry
->segment_name
)) {
171 * get the struct segment_info from seg_list
172 * for the given name.
173 * down_read(&dcssblk_devices_sem) must be held.
175 static struct segment_info
*
176 dcssblk_get_segment_by_name(char *name
)
178 struct dcssblk_dev_info
*dev_info
;
179 struct segment_info
*entry
;
181 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
182 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
183 if (!strcmp(name
, entry
->segment_name
))
191 * get the highest address of the multi-segment block.
194 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
196 unsigned long highest_addr
;
197 struct segment_info
*entry
;
200 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
201 if (highest_addr
< entry
->end
)
202 highest_addr
= entry
->end
;
208 * get the lowest address of the multi-segment block.
211 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
214 unsigned long lowest_addr
;
215 struct segment_info
*entry
;
219 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
220 if (set_first
== 0) {
221 lowest_addr
= entry
->start
;
224 if (lowest_addr
> entry
->start
)
225 lowest_addr
= entry
->start
;
232 * Check continuity of segments.
235 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
238 struct segment_info
*sort_list
, *entry
, temp
;
240 if (dev_info
->num_of_segments
<= 1)
243 sort_list
= kcalloc(dev_info
->num_of_segments
,
244 sizeof(struct segment_info
),
246 if (sort_list
== NULL
)
249 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
250 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
255 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
256 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
257 if (sort_list
[j
].start
> sort_list
[i
].start
) {
258 memcpy(&temp
, &sort_list
[i
],
259 sizeof(struct segment_info
));
260 memcpy(&sort_list
[i
], &sort_list
[j
],
261 sizeof(struct segment_info
));
262 memcpy(&sort_list
[j
], &temp
,
263 sizeof(struct segment_info
));
266 /* check continuity */
267 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
268 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
269 pr_err("Adjacent DCSSs %s and %s are not "
270 "contiguous\n", sort_list
[i
].segment_name
,
271 sort_list
[i
+1].segment_name
);
275 /* EN and EW are allowed in a block device */
276 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
277 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
278 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
279 !(sort_list
[i
+1].segment_type
&
280 SEGMENT_EXCLUSIVE
) ||
281 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
282 pr_err("DCSS %s and DCSS %s have "
283 "incompatible types\n",
284 sort_list
[i
].segment_name
,
285 sort_list
[i
+1].segment_name
);
301 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
305 /* already loaded? */
306 down_read(&dcssblk_devices_sem
);
307 *seg_info
= dcssblk_get_segment_by_name(name
);
308 up_read(&dcssblk_devices_sem
);
309 if (*seg_info
!= NULL
)
312 /* get a struct segment_info */
313 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
314 if (*seg_info
== NULL
)
317 strcpy((*seg_info
)->segment_name
, name
);
319 /* load the segment */
320 rc
= segment_load(name
, SEGMENT_SHARED
,
321 &(*seg_info
)->start
, &(*seg_info
)->end
);
323 segment_warning(rc
, (*seg_info
)->segment_name
);
326 INIT_LIST_HEAD(&(*seg_info
)->lh
);
327 (*seg_info
)->segment_type
= rc
;
333 * device attribute for switching shared/nonshared (exclusive)
334 * operation (show + store)
337 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
339 struct dcssblk_dev_info
*dev_info
;
341 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
342 return sysfs_emit(buf
, dev_info
->is_shared
? "1\n" : "0\n");
346 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
348 struct dcssblk_dev_info
*dev_info
;
349 struct segment_info
*entry
, *temp
;
352 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
354 down_write(&dcssblk_devices_sem
);
355 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
356 if (atomic_read(&dev_info
->use_count
)) {
360 if (inbuf
[0] == '1') {
361 /* reload segments in shared mode */
362 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
363 rc
= segment_modify_shared(entry
->segment_name
,
366 BUG_ON(rc
== -EINVAL
);
371 dev_info
->is_shared
= 1;
372 switch (dev_info
->segment_type
) {
376 set_disk_ro(dev_info
->gd
, 1);
378 } else if (inbuf
[0] == '0') {
379 /* reload segments in exclusive mode */
380 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
381 pr_err("DCSS %s is of type SC and cannot be "
382 "loaded as exclusive-writable\n",
383 dev_info
->segment_name
);
387 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
388 rc
= segment_modify_shared(entry
->segment_name
,
391 BUG_ON(rc
== -EINVAL
);
396 dev_info
->is_shared
= 0;
397 set_disk_ro(dev_info
->gd
, 0);
406 pr_err("DCSS device %s is removed after a failed access mode "
407 "change\n", dev_info
->segment_name
);
409 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
411 segment_unload(entry
->segment_name
);
413 list_del(&dev_info
->lh
);
414 up_write(&dcssblk_devices_sem
);
416 dax_remove_host(dev_info
->gd
);
417 kill_dax(dev_info
->dax_dev
);
418 put_dax(dev_info
->dax_dev
);
419 del_gendisk(dev_info
->gd
);
420 put_disk(dev_info
->gd
);
422 if (device_remove_file_self(dev
, attr
)) {
423 device_unregister(dev
);
428 up_write(&dcssblk_devices_sem
);
431 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
432 dcssblk_shared_store
);
435 * device attribute for save operation on current copy
436 * of the segment. If the segment is busy, saving will
437 * become pending until it gets released, which can be
438 * undone by storing a non-true value to this entry.
442 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
444 struct dcssblk_dev_info
*dev_info
;
446 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
447 return sysfs_emit(buf
, dev_info
->save_pending
? "1\n" : "0\n");
451 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
453 struct dcssblk_dev_info
*dev_info
;
454 struct segment_info
*entry
;
456 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
458 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
460 down_write(&dcssblk_devices_sem
);
461 if (inbuf
[0] == '1') {
462 if (atomic_read(&dev_info
->use_count
) == 0) {
463 // device is idle => we save immediately
464 pr_info("All DCSSs that map to device %s are "
465 "saved\n", dev_info
->segment_name
);
466 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
467 if (entry
->segment_type
== SEG_TYPE_EN
||
468 entry
->segment_type
== SEG_TYPE_SN
)
469 pr_warn("DCSS %s is of type SN or EN"
470 " and cannot be saved\n",
471 entry
->segment_name
);
473 segment_save(entry
->segment_name
);
476 // device is busy => we save it when it becomes
477 // idle in dcssblk_release
478 pr_info("Device %s is in use, its DCSSs will be "
479 "saved when it becomes idle\n",
480 dev_info
->segment_name
);
481 dev_info
->save_pending
= 1;
483 } else if (inbuf
[0] == '0') {
484 if (dev_info
->save_pending
) {
485 // device is busy & the user wants to undo his save
487 dev_info
->save_pending
= 0;
488 pr_info("A pending save request for device %s "
489 "has been canceled\n",
490 dev_info
->segment_name
);
493 up_write(&dcssblk_devices_sem
);
496 up_write(&dcssblk_devices_sem
);
499 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
503 * device attribute for showing all segments in a device
506 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
509 struct dcssblk_dev_info
*dev_info
;
510 struct segment_info
*entry
;
514 down_read(&dcssblk_devices_sem
);
515 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
516 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
517 i
+= sysfs_emit_at(buf
, i
, "%s\n", entry
->segment_name
);
518 up_read(&dcssblk_devices_sem
);
521 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
523 static struct attribute
*dcssblk_dev_attrs
[] = {
524 &dev_attr_shared
.attr
,
526 &dev_attr_seglist
.attr
,
529 static struct attribute_group dcssblk_dev_attr_group
= {
530 .attrs
= dcssblk_dev_attrs
,
532 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
533 &dcssblk_dev_attr_group
,
538 * device attribute for adding devices
541 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
543 struct queue_limits lim
= {
544 .logical_block_size
= 4096,
545 .features
= BLK_FEAT_DAX
,
547 int rc
, i
, j
, num_of_segments
;
548 struct dcssblk_dev_info
*dev_info
;
549 struct segment_info
*seg_info
, *temp
;
550 struct dax_device
*dax_dev
;
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 strscpy(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
= blk_alloc_disk(&lim
, NUMA_NO_NODE
);
632 if (IS_ERR(dev_info
->gd
)) {
633 rc
= PTR_ERR(dev_info
->gd
);
636 dev_info
->gd
->major
= dcssblk_major
;
637 dev_info
->gd
->minors
= DCSSBLK_MINORS_PER_DISK
;
638 dev_info
->gd
->fops
= &dcssblk_devops
;
639 dev_info
->gd
->private_data
= dev_info
;
640 dev_info
->gd
->flags
|= GENHD_FL_NO_PART
;
642 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
643 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
644 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
645 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
647 dev_info
->save_pending
= 0;
648 dev_info
->is_shared
= 1;
649 dev_info
->dev
.parent
= dcssblk_root_dev
;
652 *get minor, add to list
654 down_write(&dcssblk_devices_sem
);
655 if (dcssblk_get_segment_by_name(local_buf
)) {
659 rc
= dcssblk_assign_free_minor(dev_info
);
662 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
663 dev_info
->gd
->first_minor
);
664 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
666 if (!try_module_get(THIS_MODULE
)) {
671 * register the device
673 rc
= device_register(&dev_info
->dev
);
677 dax_dev
= alloc_dax(dev_info
, &dcssblk_dax_ops
);
678 if (IS_ERR(dax_dev
)) {
679 rc
= PTR_ERR(dax_dev
);
682 set_dax_synchronous(dax_dev
);
683 dev_info
->dax_dev
= dax_dev
;
684 rc
= dax_add_host(dev_info
->dax_dev
, dev_info
->gd
);
688 get_device(&dev_info
->dev
);
689 rc
= device_add_disk(&dev_info
->dev
, dev_info
->gd
, NULL
);
693 switch (dev_info
->segment_type
) {
697 set_disk_ro(dev_info
->gd
,1);
700 set_disk_ro(dev_info
->gd
,0);
703 up_write(&dcssblk_devices_sem
);
708 put_device(&dev_info
->dev
);
709 dax_remove_host(dev_info
->gd
);
711 kill_dax(dev_info
->dax_dev
);
712 put_dax(dev_info
->dax_dev
);
714 list_del(&dev_info
->lh
);
715 put_disk(dev_info
->gd
);
716 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
717 segment_unload(seg_info
->segment_name
);
719 put_device(&dev_info
->dev
);
720 up_write(&dcssblk_devices_sem
);
723 list_del(&dev_info
->lh
);
725 put_disk(dev_info
->gd
);
726 up_write(&dcssblk_devices_sem
);
728 if (dev_info
== NULL
)
730 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
731 list_del(&seg_info
->lh
);
732 segment_unload(seg_info
->segment_name
);
743 * device attribute for removing devices
746 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
748 struct dcssblk_dev_info
*dev_info
;
749 struct segment_info
*entry
;
753 if (dev
!= dcssblk_root_dev
) {
756 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
757 if (local_buf
== NULL
) {
763 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
764 local_buf
[i
] = toupper(buf
[i
]);
767 if ((i
== 0) || (i
> 8)) {
772 down_write(&dcssblk_devices_sem
);
773 dev_info
= dcssblk_get_device_by_name(local_buf
);
774 if (dev_info
== NULL
) {
775 up_write(&dcssblk_devices_sem
);
776 pr_warn("Device %s cannot be removed because it is not a known device\n",
781 if (atomic_read(&dev_info
->use_count
) != 0) {
782 up_write(&dcssblk_devices_sem
);
783 pr_warn("Device %s cannot be removed while it is in use\n",
789 list_del(&dev_info
->lh
);
790 /* unload all related segments */
791 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
792 segment_unload(entry
->segment_name
);
793 up_write(&dcssblk_devices_sem
);
795 dax_remove_host(dev_info
->gd
);
796 kill_dax(dev_info
->dax_dev
);
797 put_dax(dev_info
->dax_dev
);
798 del_gendisk(dev_info
->gd
);
799 put_disk(dev_info
->gd
);
801 device_unregister(&dev_info
->dev
);
802 put_device(&dev_info
->dev
);
811 dcssblk_open(struct gendisk
*disk
, blk_mode_t mode
)
813 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
816 if (NULL
== dev_info
) {
820 atomic_inc(&dev_info
->use_count
);
827 dcssblk_release(struct gendisk
*disk
)
829 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
830 struct segment_info
*entry
;
836 down_write(&dcssblk_devices_sem
);
837 if (atomic_dec_and_test(&dev_info
->use_count
)
838 && (dev_info
->save_pending
)) {
839 pr_info("Device %s has become idle and is being saved "
840 "now\n", dev_info
->segment_name
);
841 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
842 if (entry
->segment_type
== SEG_TYPE_EN
||
843 entry
->segment_type
== SEG_TYPE_SN
)
844 pr_warn("DCSS %s is of type SN or EN and cannot"
845 " be saved\n", entry
->segment_name
);
847 segment_save(entry
->segment_name
);
849 dev_info
->save_pending
= 0;
851 up_write(&dcssblk_devices_sem
);
855 dcssblk_submit_bio(struct bio
*bio
)
857 struct dcssblk_dev_info
*dev_info
;
859 struct bvec_iter iter
;
862 unsigned long source_addr
;
863 unsigned long bytes_done
;
866 dev_info
= bio
->bi_bdev
->bd_disk
->private_data
;
867 if (dev_info
== NULL
)
869 if (!IS_ALIGNED(bio
->bi_iter
.bi_sector
, 8) ||
870 !IS_ALIGNED(bio
->bi_iter
.bi_size
, PAGE_SIZE
))
871 /* Request is not page-aligned. */
873 /* verify data transfer direction */
874 if (dev_info
->is_shared
) {
875 switch (dev_info
->segment_type
) {
879 /* cannot write to these segments */
880 if (bio_data_dir(bio
) == WRITE
) {
881 pr_warn("Writing to %s failed because it is a read-only device\n",
882 dev_name(&dev_info
->dev
));
888 index
= (bio
->bi_iter
.bi_sector
>> 3);
889 bio_for_each_segment(bvec
, bio
, iter
) {
890 page_addr
= bvec_virt(&bvec
);
891 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
892 if (unlikely(!IS_ALIGNED((unsigned long)page_addr
, PAGE_SIZE
) ||
893 !IS_ALIGNED(bvec
.bv_len
, PAGE_SIZE
)))
896 if (bio_data_dir(bio
) == READ
)
897 memcpy(page_addr
, __va(source_addr
), bvec
.bv_len
);
899 memcpy(__va(source_addr
), page_addr
, bvec
.bv_len
);
900 bytes_done
+= bvec
.bv_len
;
909 __dcssblk_direct_access(struct dcssblk_dev_info
*dev_info
, pgoff_t pgoff
,
910 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
912 resource_size_t offset
= pgoff
* PAGE_SIZE
;
913 unsigned long dev_sz
;
915 dev_sz
= dev_info
->end
- dev_info
->start
+ 1;
917 *kaddr
= __va(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
, enum dax_access_mode mode
, void **kaddr
,
930 struct dcssblk_dev_info
*dev_info
= dax_get_private(dax_dev
);
932 return __dcssblk_direct_access(dev_info
, pgoff
, nr_pages
, kaddr
, pfn
);
936 dcssblk_check_params(void)
939 char buf
[DCSSBLK_PARM_LEN
+ 1];
940 struct dcssblk_dev_info
*dev_info
;
942 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
944 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
945 (dcssblk_segments
[j
] != ',') &&
946 (dcssblk_segments
[j
] != '\0') &&
947 (dcssblk_segments
[j
] != '('); j
++)
949 buf
[j
-i
] = dcssblk_segments
[j
];
952 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
953 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
954 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
955 buf
[k
] = toupper(buf
[k
]);
957 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
958 down_read(&dcssblk_devices_sem
);
959 dev_info
= dcssblk_get_device_by_name(buf
);
960 up_read(&dcssblk_devices_sem
);
962 dcssblk_shared_store(&dev_info
->dev
,
966 while ((dcssblk_segments
[j
] != ',') &&
967 (dcssblk_segments
[j
] != '\0'))
971 if (dcssblk_segments
[j
] == '\0')
978 * The init/exit functions.
983 root_device_unregister(dcssblk_root_dev
);
984 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
992 dcssblk_root_dev
= root_device_register("dcssblk");
993 if (IS_ERR(dcssblk_root_dev
))
994 return PTR_ERR(dcssblk_root_dev
);
995 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
998 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1001 rc
= register_blkdev(0, DCSSBLK_NAME
);
1005 init_rwsem(&dcssblk_devices_sem
);
1007 dcssblk_check_params();
1011 root_device_unregister(dcssblk_root_dev
);
1016 module_init(dcssblk_init
);
1017 module_exit(dcssblk_exit
);
1019 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1020 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1021 "comma-separated list, names in each set separated "
1022 "by commas are separated by colons, each set contains "
1023 "names of contiguous segments and each name max. 8 chars.\n"
1024 "Adding \"(local)\" to the end of each set equals echoing 0 "
1025 "to /sys/devices/dcssblk/<device name>/shared after loading "
1026 "the contiguous segments - \n"
1027 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1029 MODULE_DESCRIPTION("S/390 block driver for DCSS memory");
1030 MODULE_LICENSE("GPL");