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 <asm/extmem.h>
24 #define DCSSBLK_NAME "dcssblk"
25 #define DCSSBLK_MINORS_PER_DISK 1
26 #define DCSSBLK_PARM_LEN 400
27 #define DCSS_BUS_ID_SIZE 20
29 static int dcssblk_open(struct block_device
*bdev
, fmode_t mode
);
30 static void dcssblk_release(struct gendisk
*disk
, fmode_t mode
);
31 static blk_qc_t
dcssblk_make_request(struct request_queue
*q
,
33 static long dcssblk_direct_access(struct block_device
*bdev
, sector_t secnum
,
34 void __pmem
**kaddr
, pfn_t
*pfn
);
36 static char dcssblk_segments
[DCSSBLK_PARM_LEN
] = "\0";
38 static int dcssblk_major
;
39 static const struct block_device_operations dcssblk_devops
= {
42 .release
= dcssblk_release
,
43 .direct_access
= dcssblk_direct_access
,
46 struct dcssblk_dev_info
{
49 char segment_name
[DCSS_BUS_ID_SIZE
];
55 unsigned char save_pending
;
56 unsigned char is_shared
;
57 struct request_queue
*dcssblk_queue
;
59 struct list_head seg_list
;
64 char segment_name
[DCSS_BUS_ID_SIZE
];
70 static ssize_t
dcssblk_add_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
72 static ssize_t
dcssblk_remove_store(struct device
* dev
, struct device_attribute
*attr
, const char * buf
,
75 static DEVICE_ATTR(add
, S_IWUSR
, NULL
, dcssblk_add_store
);
76 static DEVICE_ATTR(remove
, S_IWUSR
, NULL
, dcssblk_remove_store
);
78 static struct device
*dcssblk_root_dev
;
80 static LIST_HEAD(dcssblk_devices
);
81 static struct rw_semaphore dcssblk_devices_sem
;
84 * release function for segment device.
87 dcssblk_release_segment(struct device
*dev
)
89 struct dcssblk_dev_info
*dev_info
;
90 struct segment_info
*entry
, *temp
;
92 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
93 list_for_each_entry_safe(entry
, temp
, &dev_info
->seg_list
, lh
) {
98 module_put(THIS_MODULE
);
102 * get a minor number. needs to be called with
103 * down_write(&dcssblk_devices_sem) and the
104 * device needs to be enqueued before the semaphore is
108 dcssblk_assign_free_minor(struct dcssblk_dev_info
*dev_info
)
111 struct dcssblk_dev_info
*entry
;
113 if (dev_info
== NULL
)
115 for (minor
= 0; minor
< (1<<MINORBITS
); minor
++) {
117 // test if minor available
118 list_for_each_entry(entry
, &dcssblk_devices
, lh
)
119 if (minor
== entry
->gd
->first_minor
)
121 if (!found
) break; // got unused minor
125 dev_info
->gd
->first_minor
= minor
;
130 * get the struct dcssblk_dev_info from dcssblk_devices
131 * for the given name.
132 * down_read(&dcssblk_devices_sem) must be held.
134 static struct dcssblk_dev_info
*
135 dcssblk_get_device_by_name(char *name
)
137 struct dcssblk_dev_info
*entry
;
139 list_for_each_entry(entry
, &dcssblk_devices
, lh
) {
140 if (!strcmp(name
, entry
->segment_name
)) {
148 * get the struct segment_info from seg_list
149 * for the given name.
150 * down_read(&dcssblk_devices_sem) must be held.
152 static struct segment_info
*
153 dcssblk_get_segment_by_name(char *name
)
155 struct dcssblk_dev_info
*dev_info
;
156 struct segment_info
*entry
;
158 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
159 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
160 if (!strcmp(name
, entry
->segment_name
))
168 * get the highest address of the multi-segment block.
171 dcssblk_find_highest_addr(struct dcssblk_dev_info
*dev_info
)
173 unsigned long highest_addr
;
174 struct segment_info
*entry
;
177 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
178 if (highest_addr
< entry
->end
)
179 highest_addr
= entry
->end
;
185 * get the lowest address of the multi-segment block.
188 dcssblk_find_lowest_addr(struct dcssblk_dev_info
*dev_info
)
191 unsigned long lowest_addr
;
192 struct segment_info
*entry
;
196 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
197 if (set_first
== 0) {
198 lowest_addr
= entry
->start
;
201 if (lowest_addr
> entry
->start
)
202 lowest_addr
= entry
->start
;
209 * Check continuity of segments.
212 dcssblk_is_continuous(struct dcssblk_dev_info
*dev_info
)
215 struct segment_info
*sort_list
, *entry
, temp
;
217 if (dev_info
->num_of_segments
<= 1)
221 sizeof(struct segment_info
) * dev_info
->num_of_segments
,
223 if (sort_list
== NULL
)
226 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
227 memcpy(&sort_list
[i
], entry
, sizeof(struct segment_info
));
232 for (i
= 0; i
< dev_info
->num_of_segments
; i
++)
233 for (j
= 0; j
< dev_info
->num_of_segments
; j
++)
234 if (sort_list
[j
].start
> sort_list
[i
].start
) {
235 memcpy(&temp
, &sort_list
[i
],
236 sizeof(struct segment_info
));
237 memcpy(&sort_list
[i
], &sort_list
[j
],
238 sizeof(struct segment_info
));
239 memcpy(&sort_list
[j
], &temp
,
240 sizeof(struct segment_info
));
243 /* check continuity */
244 for (i
= 0; i
< dev_info
->num_of_segments
- 1; i
++) {
245 if ((sort_list
[i
].end
+ 1) != sort_list
[i
+1].start
) {
246 pr_err("Adjacent DCSSs %s and %s are not "
247 "contiguous\n", sort_list
[i
].segment_name
,
248 sort_list
[i
+1].segment_name
);
252 /* EN and EW are allowed in a block device */
253 if (sort_list
[i
].segment_type
!= sort_list
[i
+1].segment_type
) {
254 if (!(sort_list
[i
].segment_type
& SEGMENT_EXCLUSIVE
) ||
255 (sort_list
[i
].segment_type
== SEG_TYPE_ER
) ||
256 !(sort_list
[i
+1].segment_type
&
257 SEGMENT_EXCLUSIVE
) ||
258 (sort_list
[i
+1].segment_type
== SEG_TYPE_ER
)) {
259 pr_err("DCSS %s and DCSS %s have "
260 "incompatible types\n",
261 sort_list
[i
].segment_name
,
262 sort_list
[i
+1].segment_name
);
278 dcssblk_load_segment(char *name
, struct segment_info
**seg_info
)
282 /* already loaded? */
283 down_read(&dcssblk_devices_sem
);
284 *seg_info
= dcssblk_get_segment_by_name(name
);
285 up_read(&dcssblk_devices_sem
);
286 if (*seg_info
!= NULL
)
289 /* get a struct segment_info */
290 *seg_info
= kzalloc(sizeof(struct segment_info
), GFP_KERNEL
);
291 if (*seg_info
== NULL
)
294 strcpy((*seg_info
)->segment_name
, name
);
296 /* load the segment */
297 rc
= segment_load(name
, SEGMENT_SHARED
,
298 &(*seg_info
)->start
, &(*seg_info
)->end
);
300 segment_warning(rc
, (*seg_info
)->segment_name
);
303 INIT_LIST_HEAD(&(*seg_info
)->lh
);
304 (*seg_info
)->segment_type
= rc
;
310 * device attribute for switching shared/nonshared (exclusive)
311 * operation (show + store)
314 dcssblk_shared_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
316 struct dcssblk_dev_info
*dev_info
;
318 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
319 return sprintf(buf
, dev_info
->is_shared
? "1\n" : "0\n");
323 dcssblk_shared_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
325 struct dcssblk_dev_info
*dev_info
;
326 struct segment_info
*entry
, *temp
;
329 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
331 down_write(&dcssblk_devices_sem
);
332 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
333 if (atomic_read(&dev_info
->use_count
)) {
337 if (inbuf
[0] == '1') {
338 /* reload segments in shared mode */
339 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
340 rc
= segment_modify_shared(entry
->segment_name
,
343 BUG_ON(rc
== -EINVAL
);
348 dev_info
->is_shared
= 1;
349 switch (dev_info
->segment_type
) {
353 set_disk_ro(dev_info
->gd
, 1);
355 } else if (inbuf
[0] == '0') {
356 /* reload segments in exclusive mode */
357 if (dev_info
->segment_type
== SEG_TYPE_SC
) {
358 pr_err("DCSS %s is of type SC and cannot be "
359 "loaded as exclusive-writable\n",
360 dev_info
->segment_name
);
364 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
365 rc
= segment_modify_shared(entry
->segment_name
,
368 BUG_ON(rc
== -EINVAL
);
373 dev_info
->is_shared
= 0;
374 set_disk_ro(dev_info
->gd
, 0);
383 pr_err("DCSS device %s is removed after a failed access mode "
384 "change\n", dev_info
->segment_name
);
386 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
388 segment_unload(entry
->segment_name
);
390 list_del(&dev_info
->lh
);
392 del_gendisk(dev_info
->gd
);
393 blk_cleanup_queue(dev_info
->dcssblk_queue
);
394 dev_info
->gd
->queue
= NULL
;
395 put_disk(dev_info
->gd
);
396 up_write(&dcssblk_devices_sem
);
398 if (device_remove_file_self(dev
, attr
)) {
399 device_unregister(dev
);
404 up_write(&dcssblk_devices_sem
);
407 static DEVICE_ATTR(shared
, S_IWUSR
| S_IRUSR
, dcssblk_shared_show
,
408 dcssblk_shared_store
);
411 * device attribute for save operation on current copy
412 * of the segment. If the segment is busy, saving will
413 * become pending until it gets released, which can be
414 * undone by storing a non-true value to this entry.
418 dcssblk_save_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
420 struct dcssblk_dev_info
*dev_info
;
422 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
423 return sprintf(buf
, dev_info
->save_pending
? "1\n" : "0\n");
427 dcssblk_save_store(struct device
*dev
, struct device_attribute
*attr
, const char *inbuf
, size_t count
)
429 struct dcssblk_dev_info
*dev_info
;
430 struct segment_info
*entry
;
432 if ((count
> 1) && (inbuf
[1] != '\n') && (inbuf
[1] != '\0'))
434 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
436 down_write(&dcssblk_devices_sem
);
437 if (inbuf
[0] == '1') {
438 if (atomic_read(&dev_info
->use_count
) == 0) {
439 // device is idle => we save immediately
440 pr_info("All DCSSs that map to device %s are "
441 "saved\n", dev_info
->segment_name
);
442 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
443 if (entry
->segment_type
== SEG_TYPE_EN
||
444 entry
->segment_type
== SEG_TYPE_SN
)
445 pr_warn("DCSS %s is of type SN or EN"
446 " and cannot be saved\n",
447 entry
->segment_name
);
449 segment_save(entry
->segment_name
);
452 // device is busy => we save it when it becomes
453 // idle in dcssblk_release
454 pr_info("Device %s is in use, its DCSSs will be "
455 "saved when it becomes idle\n",
456 dev_info
->segment_name
);
457 dev_info
->save_pending
= 1;
459 } else if (inbuf
[0] == '0') {
460 if (dev_info
->save_pending
) {
461 // device is busy & the user wants to undo his save
463 dev_info
->save_pending
= 0;
464 pr_info("A pending save request for device %s "
465 "has been canceled\n",
466 dev_info
->segment_name
);
469 up_write(&dcssblk_devices_sem
);
472 up_write(&dcssblk_devices_sem
);
475 static DEVICE_ATTR(save
, S_IWUSR
| S_IRUSR
, dcssblk_save_show
,
479 * device attribute for showing all segments in a device
482 dcssblk_seglist_show(struct device
*dev
, struct device_attribute
*attr
,
487 struct dcssblk_dev_info
*dev_info
;
488 struct segment_info
*entry
;
490 down_read(&dcssblk_devices_sem
);
491 dev_info
= container_of(dev
, struct dcssblk_dev_info
, dev
);
494 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
495 strcpy(&buf
[i
], entry
->segment_name
);
496 i
+= strlen(entry
->segment_name
);
500 up_read(&dcssblk_devices_sem
);
503 static DEVICE_ATTR(seglist
, S_IRUSR
, dcssblk_seglist_show
, NULL
);
505 static struct attribute
*dcssblk_dev_attrs
[] = {
506 &dev_attr_shared
.attr
,
508 &dev_attr_seglist
.attr
,
511 static struct attribute_group dcssblk_dev_attr_group
= {
512 .attrs
= dcssblk_dev_attrs
,
514 static const struct attribute_group
*dcssblk_dev_attr_groups
[] = {
515 &dcssblk_dev_attr_group
,
520 * device attribute for adding devices
523 dcssblk_add_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
525 int rc
, i
, j
, num_of_segments
;
526 struct dcssblk_dev_info
*dev_info
;
527 struct segment_info
*seg_info
, *temp
;
529 unsigned long seg_byte_size
;
533 if (dev
!= dcssblk_root_dev
) {
537 if ((count
< 1) || (buf
[0] == '\0') || (buf
[0] == '\n')) {
542 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
543 if (local_buf
== NULL
) {
552 for (i
= 0; (i
< count
&& (buf
[i
] != '\0') && (buf
[i
] != '\n')); i
++) {
553 for (j
= i
; j
< count
&&
556 (buf
[j
] != '\n'); j
++) {
557 local_buf
[j
-i
] = toupper(buf
[j
]);
559 local_buf
[j
-i
] = '\0';
560 if (((j
- i
) == 0) || ((j
- i
) > 8)) {
565 rc
= dcssblk_load_segment(local_buf
, &seg_info
);
569 * get a struct dcssblk_dev_info
571 if (num_of_segments
== 0) {
572 dev_info
= kzalloc(sizeof(struct dcssblk_dev_info
),
574 if (dev_info
== NULL
) {
578 strcpy(dev_info
->segment_name
, local_buf
);
579 dev_info
->segment_type
= seg_info
->segment_type
;
580 INIT_LIST_HEAD(&dev_info
->seg_list
);
582 list_add_tail(&seg_info
->lh
, &dev_info
->seg_list
);
586 if ((buf
[j
] == '\0') || (buf
[j
] == '\n'))
590 /* no trailing colon at the end of the input */
591 if ((i
> 0) && (buf
[i
-1] == ':')) {
595 strlcpy(local_buf
, buf
, i
+ 1);
596 dev_info
->num_of_segments
= num_of_segments
;
597 rc
= dcssblk_is_continuous(dev_info
);
601 dev_info
->start
= dcssblk_find_lowest_addr(dev_info
);
602 dev_info
->end
= dcssblk_find_highest_addr(dev_info
);
604 dev_set_name(&dev_info
->dev
, "%s", dev_info
->segment_name
);
605 dev_info
->dev
.release
= dcssblk_release_segment
;
606 dev_info
->dev
.groups
= dcssblk_dev_attr_groups
;
607 INIT_LIST_HEAD(&dev_info
->lh
);
608 dev_info
->gd
= alloc_disk(DCSSBLK_MINORS_PER_DISK
);
609 if (dev_info
->gd
== NULL
) {
613 dev_info
->gd
->major
= dcssblk_major
;
614 dev_info
->gd
->fops
= &dcssblk_devops
;
615 dev_info
->dcssblk_queue
= blk_alloc_queue(GFP_KERNEL
);
616 dev_info
->gd
->queue
= dev_info
->dcssblk_queue
;
617 dev_info
->gd
->private_data
= dev_info
;
618 dev_info
->gd
->driverfs_dev
= &dev_info
->dev
;
619 blk_queue_make_request(dev_info
->dcssblk_queue
, dcssblk_make_request
);
620 blk_queue_logical_block_size(dev_info
->dcssblk_queue
, 4096);
622 seg_byte_size
= (dev_info
->end
- dev_info
->start
+ 1);
623 set_capacity(dev_info
->gd
, seg_byte_size
>> 9); // size in sectors
624 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
625 "sectors\n", local_buf
, seg_byte_size
, seg_byte_size
>> 9);
627 dev_info
->save_pending
= 0;
628 dev_info
->is_shared
= 1;
629 dev_info
->dev
.parent
= dcssblk_root_dev
;
632 *get minor, add to list
634 down_write(&dcssblk_devices_sem
);
635 if (dcssblk_get_segment_by_name(local_buf
)) {
639 rc
= dcssblk_assign_free_minor(dev_info
);
642 sprintf(dev_info
->gd
->disk_name
, "dcssblk%d",
643 dev_info
->gd
->first_minor
);
644 list_add_tail(&dev_info
->lh
, &dcssblk_devices
);
646 if (!try_module_get(THIS_MODULE
)) {
651 * register the device
653 rc
= device_register(&dev_info
->dev
);
657 get_device(&dev_info
->dev
);
658 add_disk(dev_info
->gd
);
660 switch (dev_info
->segment_type
) {
664 set_disk_ro(dev_info
->gd
,1);
667 set_disk_ro(dev_info
->gd
,0);
670 up_write(&dcssblk_devices_sem
);
675 list_del(&dev_info
->lh
);
676 blk_cleanup_queue(dev_info
->dcssblk_queue
);
677 dev_info
->gd
->queue
= NULL
;
678 put_disk(dev_info
->gd
);
679 list_for_each_entry(seg_info
, &dev_info
->seg_list
, lh
) {
680 segment_unload(seg_info
->segment_name
);
682 put_device(&dev_info
->dev
);
683 up_write(&dcssblk_devices_sem
);
686 list_del(&dev_info
->lh
);
688 blk_cleanup_queue(dev_info
->dcssblk_queue
);
689 dev_info
->gd
->queue
= NULL
;
690 put_disk(dev_info
->gd
);
691 up_write(&dcssblk_devices_sem
);
693 if (dev_info
== NULL
)
695 list_for_each_entry_safe(seg_info
, temp
, &dev_info
->seg_list
, lh
) {
696 list_del(&seg_info
->lh
);
697 segment_unload(seg_info
->segment_name
);
708 * device attribute for removing devices
711 dcssblk_remove_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
713 struct dcssblk_dev_info
*dev_info
;
714 struct segment_info
*entry
;
718 if (dev
!= dcssblk_root_dev
) {
721 local_buf
= kmalloc(count
+ 1, GFP_KERNEL
);
722 if (local_buf
== NULL
) {
728 for (i
= 0; (i
< count
&& (*(buf
+i
)!='\0') && (*(buf
+i
)!='\n')); i
++) {
729 local_buf
[i
] = toupper(buf
[i
]);
732 if ((i
== 0) || (i
> 8)) {
737 down_write(&dcssblk_devices_sem
);
738 dev_info
= dcssblk_get_device_by_name(local_buf
);
739 if (dev_info
== NULL
) {
740 up_write(&dcssblk_devices_sem
);
741 pr_warn("Device %s cannot be removed because it is not a known device\n",
746 if (atomic_read(&dev_info
->use_count
) != 0) {
747 up_write(&dcssblk_devices_sem
);
748 pr_warn("Device %s cannot be removed while it is in use\n",
754 list_del(&dev_info
->lh
);
755 del_gendisk(dev_info
->gd
);
756 blk_cleanup_queue(dev_info
->dcssblk_queue
);
757 dev_info
->gd
->queue
= NULL
;
758 put_disk(dev_info
->gd
);
760 /* unload all related segments */
761 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
)
762 segment_unload(entry
->segment_name
);
764 up_write(&dcssblk_devices_sem
);
766 device_unregister(&dev_info
->dev
);
767 put_device(&dev_info
->dev
);
776 dcssblk_open(struct block_device
*bdev
, fmode_t mode
)
778 struct dcssblk_dev_info
*dev_info
;
781 dev_info
= bdev
->bd_disk
->private_data
;
782 if (NULL
== dev_info
) {
786 atomic_inc(&dev_info
->use_count
);
787 bdev
->bd_block_size
= 4096;
794 dcssblk_release(struct gendisk
*disk
, fmode_t mode
)
796 struct dcssblk_dev_info
*dev_info
= disk
->private_data
;
797 struct segment_info
*entry
;
803 down_write(&dcssblk_devices_sem
);
804 if (atomic_dec_and_test(&dev_info
->use_count
)
805 && (dev_info
->save_pending
)) {
806 pr_info("Device %s has become idle and is being saved "
807 "now\n", dev_info
->segment_name
);
808 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
809 if (entry
->segment_type
== SEG_TYPE_EN
||
810 entry
->segment_type
== SEG_TYPE_SN
)
811 pr_warn("DCSS %s is of type SN or EN and cannot"
812 " be saved\n", entry
->segment_name
);
814 segment_save(entry
->segment_name
);
816 dev_info
->save_pending
= 0;
818 up_write(&dcssblk_devices_sem
);
822 dcssblk_make_request(struct request_queue
*q
, struct bio
*bio
)
824 struct dcssblk_dev_info
*dev_info
;
826 struct bvec_iter iter
;
828 unsigned long page_addr
;
829 unsigned long source_addr
;
830 unsigned long bytes_done
;
832 blk_queue_split(q
, &bio
, q
->bio_split
);
835 dev_info
= bio
->bi_bdev
->bd_disk
->private_data
;
836 if (dev_info
== NULL
)
838 if ((bio
->bi_iter
.bi_sector
& 7) != 0 ||
839 (bio
->bi_iter
.bi_size
& 4095) != 0)
840 /* Request is not page-aligned. */
842 if (bio_end_sector(bio
) > get_capacity(bio
->bi_bdev
->bd_disk
)) {
843 /* Request beyond end of DCSS segment. */
846 /* verify data transfer direction */
847 if (dev_info
->is_shared
) {
848 switch (dev_info
->segment_type
) {
852 /* cannot write to these segments */
853 if (bio_data_dir(bio
) == WRITE
) {
854 pr_warn("Writing to %s failed because it is a read-only device\n",
855 dev_name(&dev_info
->dev
));
861 index
= (bio
->bi_iter
.bi_sector
>> 3);
862 bio_for_each_segment(bvec
, bio
, iter
) {
863 page_addr
= (unsigned long)
864 page_address(bvec
.bv_page
) + bvec
.bv_offset
;
865 source_addr
= dev_info
->start
+ (index
<<12) + bytes_done
;
866 if (unlikely((page_addr
& 4095) != 0) || (bvec
.bv_len
& 4095) != 0)
869 if (bio_data_dir(bio
) == READ
) {
870 memcpy((void*)page_addr
, (void*)source_addr
,
873 memcpy((void*)source_addr
, (void*)page_addr
,
876 bytes_done
+= bvec
.bv_len
;
879 return BLK_QC_T_NONE
;
882 return BLK_QC_T_NONE
;
886 dcssblk_direct_access (struct block_device
*bdev
, sector_t secnum
,
887 void __pmem
**kaddr
, pfn_t
*pfn
)
889 struct dcssblk_dev_info
*dev_info
;
890 unsigned long offset
, dev_sz
;
892 dev_info
= bdev
->bd_disk
->private_data
;
895 dev_sz
= dev_info
->end
- dev_info
->start
;
896 offset
= secnum
* 512;
897 *kaddr
= (void __pmem
*) (dev_info
->start
+ offset
);
898 *pfn
= __pfn_to_pfn_t(PFN_DOWN(dev_info
->start
+ offset
), PFN_DEV
);
900 return dev_sz
- offset
;
904 dcssblk_check_params(void)
907 char buf
[DCSSBLK_PARM_LEN
+ 1];
908 struct dcssblk_dev_info
*dev_info
;
910 for (i
= 0; (i
< DCSSBLK_PARM_LEN
) && (dcssblk_segments
[i
] != '\0');
912 for (j
= i
; (j
< DCSSBLK_PARM_LEN
) &&
913 (dcssblk_segments
[j
] != ',') &&
914 (dcssblk_segments
[j
] != '\0') &&
915 (dcssblk_segments
[j
] != '('); j
++)
917 buf
[j
-i
] = dcssblk_segments
[j
];
920 rc
= dcssblk_add_store(dcssblk_root_dev
, NULL
, buf
, j
-i
);
921 if ((rc
>= 0) && (dcssblk_segments
[j
] == '(')) {
922 for (k
= 0; (buf
[k
] != ':') && (buf
[k
] != '\0'); k
++)
923 buf
[k
] = toupper(buf
[k
]);
925 if (!strncmp(&dcssblk_segments
[j
], "(local)", 7)) {
926 down_read(&dcssblk_devices_sem
);
927 dev_info
= dcssblk_get_device_by_name(buf
);
928 up_read(&dcssblk_devices_sem
);
930 dcssblk_shared_store(&dev_info
->dev
,
934 while ((dcssblk_segments
[j
] != ',') &&
935 (dcssblk_segments
[j
] != '\0'))
939 if (dcssblk_segments
[j
] == '\0')
948 static int dcssblk_freeze(struct device
*dev
)
950 struct dcssblk_dev_info
*dev_info
;
953 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
954 switch (dev_info
->segment_type
) {
958 if (!dev_info
->is_shared
)
969 pr_err("Suspending the system failed because DCSS device %s "
971 dev_info
->segment_name
);
975 static int dcssblk_restore(struct device
*dev
)
977 struct dcssblk_dev_info
*dev_info
;
978 struct segment_info
*entry
;
979 unsigned long start
, end
;
982 list_for_each_entry(dev_info
, &dcssblk_devices
, lh
) {
983 list_for_each_entry(entry
, &dev_info
->seg_list
, lh
) {
984 segment_unload(entry
->segment_name
);
985 rc
= segment_load(entry
->segment_name
, SEGMENT_SHARED
,
988 // TODO in_use check ?
989 segment_warning(rc
, entry
->segment_name
);
992 if (start
!= entry
->start
|| end
!= entry
->end
) {
993 pr_err("The address range of DCSS %s changed "
994 "while the system was suspended\n",
995 entry
->segment_name
);
1002 panic("fatal dcssblk resume error\n");
1005 static int dcssblk_thaw(struct device
*dev
)
1010 static const struct dev_pm_ops dcssblk_pm_ops
= {
1011 .freeze
= dcssblk_freeze
,
1012 .thaw
= dcssblk_thaw
,
1013 .restore
= dcssblk_restore
,
1016 static struct platform_driver dcssblk_pdrv
= {
1019 .pm
= &dcssblk_pm_ops
,
1023 static struct platform_device
*dcssblk_pdev
;
1027 * The init/exit functions.
1032 platform_device_unregister(dcssblk_pdev
);
1033 platform_driver_unregister(&dcssblk_pdrv
);
1034 root_device_unregister(dcssblk_root_dev
);
1035 unregister_blkdev(dcssblk_major
, DCSSBLK_NAME
);
1043 rc
= platform_driver_register(&dcssblk_pdrv
);
1047 dcssblk_pdev
= platform_device_register_simple("dcssblk", -1, NULL
,
1049 if (IS_ERR(dcssblk_pdev
)) {
1050 rc
= PTR_ERR(dcssblk_pdev
);
1054 dcssblk_root_dev
= root_device_register("dcssblk");
1055 if (IS_ERR(dcssblk_root_dev
)) {
1056 rc
= PTR_ERR(dcssblk_root_dev
);
1059 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_add
);
1062 rc
= device_create_file(dcssblk_root_dev
, &dev_attr_remove
);
1065 rc
= register_blkdev(0, DCSSBLK_NAME
);
1069 init_rwsem(&dcssblk_devices_sem
);
1071 dcssblk_check_params();
1075 root_device_unregister(dcssblk_root_dev
);
1077 platform_device_unregister(dcssblk_pdev
);
1079 platform_driver_unregister(&dcssblk_pdrv
);
1083 module_init(dcssblk_init
);
1084 module_exit(dcssblk_exit
);
1086 module_param_string(segments
, dcssblk_segments
, DCSSBLK_PARM_LEN
, 0444);
1087 MODULE_PARM_DESC(segments
, "Name of DCSS segment(s) to be loaded, "
1088 "comma-separated list, names in each set separated "
1089 "by commas are separated by colons, each set contains "
1090 "names of contiguous segments and each name max. 8 chars.\n"
1091 "Adding \"(local)\" to the end of each set equals echoing 0 "
1092 "to /sys/devices/dcssblk/<device name>/shared after loading "
1093 "the contiguous segments - \n"
1094 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1096 MODULE_LICENSE("GPL");