]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/s390/block/dcssblk.c
dcssblk: add dax_operations support
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / block / dcssblk.c
1 /*
2 * dcssblk.c -- the S/390 block driver for dcss memory
3 *
4 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
5 */
6
7 #define KMSG_COMPONENT "dcssblk"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
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>
23 #include <asm/io.h>
24
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
29
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,
33 struct bio *bio);
34 static long dcssblk_blk_direct_access(struct block_device *bdev, sector_t secnum,
35 void **kaddr, pfn_t *pfn, long size);
36 static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
37 long nr_pages, void **kaddr, pfn_t *pfn);
38
39 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
40
41 static int dcssblk_major;
42 static const struct block_device_operations dcssblk_devops = {
43 .owner = THIS_MODULE,
44 .open = dcssblk_open,
45 .release = dcssblk_release,
46 .direct_access = dcssblk_blk_direct_access,
47 };
48
49 static const struct dax_operations dcssblk_dax_ops = {
50 .direct_access = dcssblk_dax_direct_access,
51 };
52
53 struct dcssblk_dev_info {
54 struct list_head lh;
55 struct device dev;
56 char segment_name[DCSS_BUS_ID_SIZE];
57 atomic_t use_count;
58 struct gendisk *gd;
59 unsigned long start;
60 unsigned long end;
61 int segment_type;
62 unsigned char save_pending;
63 unsigned char is_shared;
64 struct request_queue *dcssblk_queue;
65 int num_of_segments;
66 struct list_head seg_list;
67 struct dax_device *dax_dev;
68 };
69
70 struct segment_info {
71 struct list_head lh;
72 char segment_name[DCSS_BUS_ID_SIZE];
73 unsigned long start;
74 unsigned long end;
75 int segment_type;
76 };
77
78 static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
79 size_t count);
80 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
81 size_t count);
82
83 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
84 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
85
86 static struct device *dcssblk_root_dev;
87
88 static LIST_HEAD(dcssblk_devices);
89 static struct rw_semaphore dcssblk_devices_sem;
90
91 /*
92 * release function for segment device.
93 */
94 static void
95 dcssblk_release_segment(struct device *dev)
96 {
97 struct dcssblk_dev_info *dev_info;
98 struct segment_info *entry, *temp;
99
100 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
101 list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
102 list_del(&entry->lh);
103 kfree(entry);
104 }
105 kfree(dev_info);
106 module_put(THIS_MODULE);
107 }
108
109 /*
110 * get a minor number. needs to be called with
111 * down_write(&dcssblk_devices_sem) and the
112 * device needs to be enqueued before the semaphore is
113 * freed.
114 */
115 static int
116 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
117 {
118 int minor, found;
119 struct dcssblk_dev_info *entry;
120
121 if (dev_info == NULL)
122 return -EINVAL;
123 for (minor = 0; minor < (1<<MINORBITS); minor++) {
124 found = 0;
125 // test if minor available
126 list_for_each_entry(entry, &dcssblk_devices, lh)
127 if (minor == entry->gd->first_minor)
128 found++;
129 if (!found) break; // got unused minor
130 }
131 if (found)
132 return -EBUSY;
133 dev_info->gd->first_minor = minor;
134 return 0;
135 }
136
137 /*
138 * get the struct dcssblk_dev_info from dcssblk_devices
139 * for the given name.
140 * down_read(&dcssblk_devices_sem) must be held.
141 */
142 static struct dcssblk_dev_info *
143 dcssblk_get_device_by_name(char *name)
144 {
145 struct dcssblk_dev_info *entry;
146
147 list_for_each_entry(entry, &dcssblk_devices, lh) {
148 if (!strcmp(name, entry->segment_name)) {
149 return entry;
150 }
151 }
152 return NULL;
153 }
154
155 /*
156 * get the struct segment_info from seg_list
157 * for the given name.
158 * down_read(&dcssblk_devices_sem) must be held.
159 */
160 static struct segment_info *
161 dcssblk_get_segment_by_name(char *name)
162 {
163 struct dcssblk_dev_info *dev_info;
164 struct segment_info *entry;
165
166 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
167 list_for_each_entry(entry, &dev_info->seg_list, lh) {
168 if (!strcmp(name, entry->segment_name))
169 return entry;
170 }
171 }
172 return NULL;
173 }
174
175 /*
176 * get the highest address of the multi-segment block.
177 */
178 static unsigned long
179 dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
180 {
181 unsigned long highest_addr;
182 struct segment_info *entry;
183
184 highest_addr = 0;
185 list_for_each_entry(entry, &dev_info->seg_list, lh) {
186 if (highest_addr < entry->end)
187 highest_addr = entry->end;
188 }
189 return highest_addr;
190 }
191
192 /*
193 * get the lowest address of the multi-segment block.
194 */
195 static unsigned long
196 dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
197 {
198 int set_first;
199 unsigned long lowest_addr;
200 struct segment_info *entry;
201
202 set_first = 0;
203 lowest_addr = 0;
204 list_for_each_entry(entry, &dev_info->seg_list, lh) {
205 if (set_first == 0) {
206 lowest_addr = entry->start;
207 set_first = 1;
208 } else {
209 if (lowest_addr > entry->start)
210 lowest_addr = entry->start;
211 }
212 }
213 return lowest_addr;
214 }
215
216 /*
217 * Check continuity of segments.
218 */
219 static int
220 dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
221 {
222 int i, j, rc;
223 struct segment_info *sort_list, *entry, temp;
224
225 if (dev_info->num_of_segments <= 1)
226 return 0;
227
228 sort_list = kzalloc(
229 sizeof(struct segment_info) * dev_info->num_of_segments,
230 GFP_KERNEL);
231 if (sort_list == NULL)
232 return -ENOMEM;
233 i = 0;
234 list_for_each_entry(entry, &dev_info->seg_list, lh) {
235 memcpy(&sort_list[i], entry, sizeof(struct segment_info));
236 i++;
237 }
238
239 /* sort segments */
240 for (i = 0; i < dev_info->num_of_segments; i++)
241 for (j = 0; j < dev_info->num_of_segments; j++)
242 if (sort_list[j].start > sort_list[i].start) {
243 memcpy(&temp, &sort_list[i],
244 sizeof(struct segment_info));
245 memcpy(&sort_list[i], &sort_list[j],
246 sizeof(struct segment_info));
247 memcpy(&sort_list[j], &temp,
248 sizeof(struct segment_info));
249 }
250
251 /* check continuity */
252 for (i = 0; i < dev_info->num_of_segments - 1; i++) {
253 if ((sort_list[i].end + 1) != sort_list[i+1].start) {
254 pr_err("Adjacent DCSSs %s and %s are not "
255 "contiguous\n", sort_list[i].segment_name,
256 sort_list[i+1].segment_name);
257 rc = -EINVAL;
258 goto out;
259 }
260 /* EN and EW are allowed in a block device */
261 if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
262 if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
263 (sort_list[i].segment_type == SEG_TYPE_ER) ||
264 !(sort_list[i+1].segment_type &
265 SEGMENT_EXCLUSIVE) ||
266 (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
267 pr_err("DCSS %s and DCSS %s have "
268 "incompatible types\n",
269 sort_list[i].segment_name,
270 sort_list[i+1].segment_name);
271 rc = -EINVAL;
272 goto out;
273 }
274 }
275 }
276 rc = 0;
277 out:
278 kfree(sort_list);
279 return rc;
280 }
281
282 /*
283 * Load a segment
284 */
285 static int
286 dcssblk_load_segment(char *name, struct segment_info **seg_info)
287 {
288 int rc;
289
290 /* already loaded? */
291 down_read(&dcssblk_devices_sem);
292 *seg_info = dcssblk_get_segment_by_name(name);
293 up_read(&dcssblk_devices_sem);
294 if (*seg_info != NULL)
295 return -EEXIST;
296
297 /* get a struct segment_info */
298 *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
299 if (*seg_info == NULL)
300 return -ENOMEM;
301
302 strcpy((*seg_info)->segment_name, name);
303
304 /* load the segment */
305 rc = segment_load(name, SEGMENT_SHARED,
306 &(*seg_info)->start, &(*seg_info)->end);
307 if (rc < 0) {
308 segment_warning(rc, (*seg_info)->segment_name);
309 kfree(*seg_info);
310 } else {
311 INIT_LIST_HEAD(&(*seg_info)->lh);
312 (*seg_info)->segment_type = rc;
313 }
314 return rc;
315 }
316
317 /*
318 * device attribute for switching shared/nonshared (exclusive)
319 * operation (show + store)
320 */
321 static ssize_t
322 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
323 {
324 struct dcssblk_dev_info *dev_info;
325
326 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
327 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
328 }
329
330 static ssize_t
331 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
332 {
333 struct dcssblk_dev_info *dev_info;
334 struct segment_info *entry, *temp;
335 int rc;
336
337 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
338 return -EINVAL;
339 down_write(&dcssblk_devices_sem);
340 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
341 if (atomic_read(&dev_info->use_count)) {
342 rc = -EBUSY;
343 goto out;
344 }
345 if (inbuf[0] == '1') {
346 /* reload segments in shared mode */
347 list_for_each_entry(entry, &dev_info->seg_list, lh) {
348 rc = segment_modify_shared(entry->segment_name,
349 SEGMENT_SHARED);
350 if (rc < 0) {
351 BUG_ON(rc == -EINVAL);
352 if (rc != -EAGAIN)
353 goto removeseg;
354 }
355 }
356 dev_info->is_shared = 1;
357 switch (dev_info->segment_type) {
358 case SEG_TYPE_SR:
359 case SEG_TYPE_ER:
360 case SEG_TYPE_SC:
361 set_disk_ro(dev_info->gd, 1);
362 }
363 } else if (inbuf[0] == '0') {
364 /* reload segments in exclusive mode */
365 if (dev_info->segment_type == SEG_TYPE_SC) {
366 pr_err("DCSS %s is of type SC and cannot be "
367 "loaded as exclusive-writable\n",
368 dev_info->segment_name);
369 rc = -EINVAL;
370 goto out;
371 }
372 list_for_each_entry(entry, &dev_info->seg_list, lh) {
373 rc = segment_modify_shared(entry->segment_name,
374 SEGMENT_EXCLUSIVE);
375 if (rc < 0) {
376 BUG_ON(rc == -EINVAL);
377 if (rc != -EAGAIN)
378 goto removeseg;
379 }
380 }
381 dev_info->is_shared = 0;
382 set_disk_ro(dev_info->gd, 0);
383 } else {
384 rc = -EINVAL;
385 goto out;
386 }
387 rc = count;
388 goto out;
389
390 removeseg:
391 pr_err("DCSS device %s is removed after a failed access mode "
392 "change\n", dev_info->segment_name);
393 temp = entry;
394 list_for_each_entry(entry, &dev_info->seg_list, lh) {
395 if (entry != temp)
396 segment_unload(entry->segment_name);
397 }
398 list_del(&dev_info->lh);
399
400 kill_dax(dev_info->dax_dev);
401 put_dax(dev_info->dax_dev);
402 del_gendisk(dev_info->gd);
403 blk_cleanup_queue(dev_info->dcssblk_queue);
404 dev_info->gd->queue = NULL;
405 put_disk(dev_info->gd);
406 up_write(&dcssblk_devices_sem);
407
408 if (device_remove_file_self(dev, attr)) {
409 device_unregister(dev);
410 put_device(dev);
411 }
412 return rc;
413 out:
414 up_write(&dcssblk_devices_sem);
415 return rc;
416 }
417 static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
418 dcssblk_shared_store);
419
420 /*
421 * device attribute for save operation on current copy
422 * of the segment. If the segment is busy, saving will
423 * become pending until it gets released, which can be
424 * undone by storing a non-true value to this entry.
425 * (show + store)
426 */
427 static ssize_t
428 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
429 {
430 struct dcssblk_dev_info *dev_info;
431
432 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
433 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
434 }
435
436 static ssize_t
437 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
438 {
439 struct dcssblk_dev_info *dev_info;
440 struct segment_info *entry;
441
442 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
443 return -EINVAL;
444 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
445
446 down_write(&dcssblk_devices_sem);
447 if (inbuf[0] == '1') {
448 if (atomic_read(&dev_info->use_count) == 0) {
449 // device is idle => we save immediately
450 pr_info("All DCSSs that map to device %s are "
451 "saved\n", dev_info->segment_name);
452 list_for_each_entry(entry, &dev_info->seg_list, lh) {
453 if (entry->segment_type == SEG_TYPE_EN ||
454 entry->segment_type == SEG_TYPE_SN)
455 pr_warn("DCSS %s is of type SN or EN"
456 " and cannot be saved\n",
457 entry->segment_name);
458 else
459 segment_save(entry->segment_name);
460 }
461 } else {
462 // device is busy => we save it when it becomes
463 // idle in dcssblk_release
464 pr_info("Device %s is in use, its DCSSs will be "
465 "saved when it becomes idle\n",
466 dev_info->segment_name);
467 dev_info->save_pending = 1;
468 }
469 } else if (inbuf[0] == '0') {
470 if (dev_info->save_pending) {
471 // device is busy & the user wants to undo his save
472 // request
473 dev_info->save_pending = 0;
474 pr_info("A pending save request for device %s "
475 "has been canceled\n",
476 dev_info->segment_name);
477 }
478 } else {
479 up_write(&dcssblk_devices_sem);
480 return -EINVAL;
481 }
482 up_write(&dcssblk_devices_sem);
483 return count;
484 }
485 static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
486 dcssblk_save_store);
487
488 /*
489 * device attribute for showing all segments in a device
490 */
491 static ssize_t
492 dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
493 char *buf)
494 {
495 int i;
496
497 struct dcssblk_dev_info *dev_info;
498 struct segment_info *entry;
499
500 down_read(&dcssblk_devices_sem);
501 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
502 i = 0;
503 buf[0] = '\0';
504 list_for_each_entry(entry, &dev_info->seg_list, lh) {
505 strcpy(&buf[i], entry->segment_name);
506 i += strlen(entry->segment_name);
507 buf[i] = '\n';
508 i++;
509 }
510 up_read(&dcssblk_devices_sem);
511 return i;
512 }
513 static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
514
515 static struct attribute *dcssblk_dev_attrs[] = {
516 &dev_attr_shared.attr,
517 &dev_attr_save.attr,
518 &dev_attr_seglist.attr,
519 NULL,
520 };
521 static struct attribute_group dcssblk_dev_attr_group = {
522 .attrs = dcssblk_dev_attrs,
523 };
524 static const struct attribute_group *dcssblk_dev_attr_groups[] = {
525 &dcssblk_dev_attr_group,
526 NULL,
527 };
528
529 /*
530 * device attribute for adding devices
531 */
532 static ssize_t
533 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
534 {
535 int rc, i, j, num_of_segments;
536 struct dcssblk_dev_info *dev_info;
537 struct segment_info *seg_info, *temp;
538 char *local_buf;
539 unsigned long seg_byte_size;
540
541 dev_info = NULL;
542 seg_info = NULL;
543 if (dev != dcssblk_root_dev) {
544 rc = -EINVAL;
545 goto out_nobuf;
546 }
547 if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
548 rc = -ENAMETOOLONG;
549 goto out_nobuf;
550 }
551
552 local_buf = kmalloc(count + 1, GFP_KERNEL);
553 if (local_buf == NULL) {
554 rc = -ENOMEM;
555 goto out_nobuf;
556 }
557
558 /*
559 * parse input
560 */
561 num_of_segments = 0;
562 for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
563 for (j = i; j < count &&
564 (buf[j] != ':') &&
565 (buf[j] != '\0') &&
566 (buf[j] != '\n'); j++) {
567 local_buf[j-i] = toupper(buf[j]);
568 }
569 local_buf[j-i] = '\0';
570 if (((j - i) == 0) || ((j - i) > 8)) {
571 rc = -ENAMETOOLONG;
572 goto seg_list_del;
573 }
574
575 rc = dcssblk_load_segment(local_buf, &seg_info);
576 if (rc < 0)
577 goto seg_list_del;
578 /*
579 * get a struct dcssblk_dev_info
580 */
581 if (num_of_segments == 0) {
582 dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
583 GFP_KERNEL);
584 if (dev_info == NULL) {
585 rc = -ENOMEM;
586 goto out;
587 }
588 strcpy(dev_info->segment_name, local_buf);
589 dev_info->segment_type = seg_info->segment_type;
590 INIT_LIST_HEAD(&dev_info->seg_list);
591 }
592 list_add_tail(&seg_info->lh, &dev_info->seg_list);
593 num_of_segments++;
594 i = j;
595
596 if ((buf[j] == '\0') || (buf[j] == '\n'))
597 break;
598 }
599
600 /* no trailing colon at the end of the input */
601 if ((i > 0) && (buf[i-1] == ':')) {
602 rc = -ENAMETOOLONG;
603 goto seg_list_del;
604 }
605 strlcpy(local_buf, buf, i + 1);
606 dev_info->num_of_segments = num_of_segments;
607 rc = dcssblk_is_continuous(dev_info);
608 if (rc < 0)
609 goto seg_list_del;
610
611 dev_info->start = dcssblk_find_lowest_addr(dev_info);
612 dev_info->end = dcssblk_find_highest_addr(dev_info);
613
614 dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
615 dev_info->dev.release = dcssblk_release_segment;
616 dev_info->dev.groups = dcssblk_dev_attr_groups;
617 INIT_LIST_HEAD(&dev_info->lh);
618 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
619 if (dev_info->gd == NULL) {
620 rc = -ENOMEM;
621 goto seg_list_del;
622 }
623 dev_info->gd->major = dcssblk_major;
624 dev_info->gd->fops = &dcssblk_devops;
625 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
626 dev_info->gd->queue = dev_info->dcssblk_queue;
627 dev_info->gd->private_data = dev_info;
628 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
629 blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
630 queue_flag_set_unlocked(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
631
632 seg_byte_size = (dev_info->end - dev_info->start + 1);
633 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
634 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
635 "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
636
637 dev_info->save_pending = 0;
638 dev_info->is_shared = 1;
639 dev_info->dev.parent = dcssblk_root_dev;
640
641 /*
642 *get minor, add to list
643 */
644 down_write(&dcssblk_devices_sem);
645 if (dcssblk_get_segment_by_name(local_buf)) {
646 rc = -EEXIST;
647 goto release_gd;
648 }
649 rc = dcssblk_assign_free_minor(dev_info);
650 if (rc)
651 goto release_gd;
652 sprintf(dev_info->gd->disk_name, "dcssblk%d",
653 dev_info->gd->first_minor);
654 list_add_tail(&dev_info->lh, &dcssblk_devices);
655
656 if (!try_module_get(THIS_MODULE)) {
657 rc = -ENODEV;
658 goto dev_list_del;
659 }
660 /*
661 * register the device
662 */
663 rc = device_register(&dev_info->dev);
664 if (rc)
665 goto put_dev;
666
667 dev_info->dax_dev = alloc_dax(dev_info, dev_info->gd->disk_name,
668 &dcssblk_dax_ops);
669 if (!dev_info->dax_dev) {
670 rc = -ENOMEM;
671 goto put_dev;
672 }
673
674 get_device(&dev_info->dev);
675 device_add_disk(&dev_info->dev, dev_info->gd);
676
677 switch (dev_info->segment_type) {
678 case SEG_TYPE_SR:
679 case SEG_TYPE_ER:
680 case SEG_TYPE_SC:
681 set_disk_ro(dev_info->gd,1);
682 break;
683 default:
684 set_disk_ro(dev_info->gd,0);
685 break;
686 }
687 up_write(&dcssblk_devices_sem);
688 rc = count;
689 goto out;
690
691 put_dev:
692 list_del(&dev_info->lh);
693 blk_cleanup_queue(dev_info->dcssblk_queue);
694 dev_info->gd->queue = NULL;
695 put_disk(dev_info->gd);
696 list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
697 segment_unload(seg_info->segment_name);
698 }
699 put_device(&dev_info->dev);
700 up_write(&dcssblk_devices_sem);
701 goto out;
702 dev_list_del:
703 list_del(&dev_info->lh);
704 release_gd:
705 blk_cleanup_queue(dev_info->dcssblk_queue);
706 dev_info->gd->queue = NULL;
707 put_disk(dev_info->gd);
708 up_write(&dcssblk_devices_sem);
709 seg_list_del:
710 if (dev_info == NULL)
711 goto out;
712 list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
713 list_del(&seg_info->lh);
714 segment_unload(seg_info->segment_name);
715 kfree(seg_info);
716 }
717 kfree(dev_info);
718 out:
719 kfree(local_buf);
720 out_nobuf:
721 return rc;
722 }
723
724 /*
725 * device attribute for removing devices
726 */
727 static ssize_t
728 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
729 {
730 struct dcssblk_dev_info *dev_info;
731 struct segment_info *entry;
732 int rc, i;
733 char *local_buf;
734
735 if (dev != dcssblk_root_dev) {
736 return -EINVAL;
737 }
738 local_buf = kmalloc(count + 1, GFP_KERNEL);
739 if (local_buf == NULL) {
740 return -ENOMEM;
741 }
742 /*
743 * parse input
744 */
745 for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
746 local_buf[i] = toupper(buf[i]);
747 }
748 local_buf[i] = '\0';
749 if ((i == 0) || (i > 8)) {
750 rc = -ENAMETOOLONG;
751 goto out_buf;
752 }
753
754 down_write(&dcssblk_devices_sem);
755 dev_info = dcssblk_get_device_by_name(local_buf);
756 if (dev_info == NULL) {
757 up_write(&dcssblk_devices_sem);
758 pr_warn("Device %s cannot be removed because it is not a known device\n",
759 local_buf);
760 rc = -ENODEV;
761 goto out_buf;
762 }
763 if (atomic_read(&dev_info->use_count) != 0) {
764 up_write(&dcssblk_devices_sem);
765 pr_warn("Device %s cannot be removed while it is in use\n",
766 local_buf);
767 rc = -EBUSY;
768 goto out_buf;
769 }
770
771 list_del(&dev_info->lh);
772 kill_dax(dev_info->dax_dev);
773 put_dax(dev_info->dax_dev);
774 del_gendisk(dev_info->gd);
775 blk_cleanup_queue(dev_info->dcssblk_queue);
776 dev_info->gd->queue = NULL;
777 put_disk(dev_info->gd);
778
779 /* unload all related segments */
780 list_for_each_entry(entry, &dev_info->seg_list, lh)
781 segment_unload(entry->segment_name);
782
783 up_write(&dcssblk_devices_sem);
784
785 device_unregister(&dev_info->dev);
786 put_device(&dev_info->dev);
787
788 rc = count;
789 out_buf:
790 kfree(local_buf);
791 return rc;
792 }
793
794 static int
795 dcssblk_open(struct block_device *bdev, fmode_t mode)
796 {
797 struct dcssblk_dev_info *dev_info;
798 int rc;
799
800 dev_info = bdev->bd_disk->private_data;
801 if (NULL == dev_info) {
802 rc = -ENODEV;
803 goto out;
804 }
805 atomic_inc(&dev_info->use_count);
806 bdev->bd_block_size = 4096;
807 rc = 0;
808 out:
809 return rc;
810 }
811
812 static void
813 dcssblk_release(struct gendisk *disk, fmode_t mode)
814 {
815 struct dcssblk_dev_info *dev_info = disk->private_data;
816 struct segment_info *entry;
817
818 if (!dev_info) {
819 WARN_ON(1);
820 return;
821 }
822 down_write(&dcssblk_devices_sem);
823 if (atomic_dec_and_test(&dev_info->use_count)
824 && (dev_info->save_pending)) {
825 pr_info("Device %s has become idle and is being saved "
826 "now\n", dev_info->segment_name);
827 list_for_each_entry(entry, &dev_info->seg_list, lh) {
828 if (entry->segment_type == SEG_TYPE_EN ||
829 entry->segment_type == SEG_TYPE_SN)
830 pr_warn("DCSS %s is of type SN or EN and cannot"
831 " be saved\n", entry->segment_name);
832 else
833 segment_save(entry->segment_name);
834 }
835 dev_info->save_pending = 0;
836 }
837 up_write(&dcssblk_devices_sem);
838 }
839
840 static blk_qc_t
841 dcssblk_make_request(struct request_queue *q, struct bio *bio)
842 {
843 struct dcssblk_dev_info *dev_info;
844 struct bio_vec bvec;
845 struct bvec_iter iter;
846 unsigned long index;
847 unsigned long page_addr;
848 unsigned long source_addr;
849 unsigned long bytes_done;
850
851 blk_queue_split(q, &bio, q->bio_split);
852
853 bytes_done = 0;
854 dev_info = bio->bi_bdev->bd_disk->private_data;
855 if (dev_info == NULL)
856 goto fail;
857 if ((bio->bi_iter.bi_sector & 7) != 0 ||
858 (bio->bi_iter.bi_size & 4095) != 0)
859 /* Request is not page-aligned. */
860 goto fail;
861 if (bio_end_sector(bio) > get_capacity(bio->bi_bdev->bd_disk)) {
862 /* Request beyond end of DCSS segment. */
863 goto fail;
864 }
865 /* verify data transfer direction */
866 if (dev_info->is_shared) {
867 switch (dev_info->segment_type) {
868 case SEG_TYPE_SR:
869 case SEG_TYPE_ER:
870 case SEG_TYPE_SC:
871 /* cannot write to these segments */
872 if (bio_data_dir(bio) == WRITE) {
873 pr_warn("Writing to %s failed because it is a read-only device\n",
874 dev_name(&dev_info->dev));
875 goto fail;
876 }
877 }
878 }
879
880 index = (bio->bi_iter.bi_sector >> 3);
881 bio_for_each_segment(bvec, bio, iter) {
882 page_addr = (unsigned long)
883 page_address(bvec.bv_page) + bvec.bv_offset;
884 source_addr = dev_info->start + (index<<12) + bytes_done;
885 if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
886 // More paranoia.
887 goto fail;
888 if (bio_data_dir(bio) == READ) {
889 memcpy((void*)page_addr, (void*)source_addr,
890 bvec.bv_len);
891 } else {
892 memcpy((void*)source_addr, (void*)page_addr,
893 bvec.bv_len);
894 }
895 bytes_done += bvec.bv_len;
896 }
897 bio_endio(bio);
898 return BLK_QC_T_NONE;
899 fail:
900 bio_io_error(bio);
901 return BLK_QC_T_NONE;
902 }
903
904 static long
905 __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
906 long nr_pages, void **kaddr, pfn_t *pfn)
907 {
908 resource_size_t offset = pgoff * PAGE_SIZE;
909 unsigned long dev_sz;
910
911 dev_sz = dev_info->end - dev_info->start + 1;
912 *kaddr = (void *) dev_info->start + offset;
913 *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset), PFN_DEV);
914
915 return (dev_sz - offset) / PAGE_SIZE;
916 }
917
918 static long
919 dcssblk_blk_direct_access(struct block_device *bdev, sector_t secnum,
920 void **kaddr, pfn_t *pfn, long size)
921 {
922 struct dcssblk_dev_info *dev_info;
923
924 dev_info = bdev->bd_disk->private_data;
925 if (!dev_info)
926 return -ENODEV;
927 return __dcssblk_direct_access(dev_info, PHYS_PFN(secnum * 512),
928 PHYS_PFN(size), kaddr, pfn) * PAGE_SIZE;
929 }
930
931 static long
932 dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
933 long nr_pages, void **kaddr, pfn_t *pfn)
934 {
935 struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
936
937 return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
938 }
939
940 static void
941 dcssblk_check_params(void)
942 {
943 int rc, i, j, k;
944 char buf[DCSSBLK_PARM_LEN + 1];
945 struct dcssblk_dev_info *dev_info;
946
947 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
948 i++) {
949 for (j = i; (j < DCSSBLK_PARM_LEN) &&
950 (dcssblk_segments[j] != ',') &&
951 (dcssblk_segments[j] != '\0') &&
952 (dcssblk_segments[j] != '('); j++)
953 {
954 buf[j-i] = dcssblk_segments[j];
955 }
956 buf[j-i] = '\0';
957 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
958 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
959 for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
960 buf[k] = toupper(buf[k]);
961 buf[k] = '\0';
962 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
963 down_read(&dcssblk_devices_sem);
964 dev_info = dcssblk_get_device_by_name(buf);
965 up_read(&dcssblk_devices_sem);
966 if (dev_info)
967 dcssblk_shared_store(&dev_info->dev,
968 NULL, "0\n", 2);
969 }
970 }
971 while ((dcssblk_segments[j] != ',') &&
972 (dcssblk_segments[j] != '\0'))
973 {
974 j++;
975 }
976 if (dcssblk_segments[j] == '\0')
977 break;
978 i = j;
979 }
980 }
981
982 /*
983 * Suspend / Resume
984 */
985 static int dcssblk_freeze(struct device *dev)
986 {
987 struct dcssblk_dev_info *dev_info;
988 int rc = 0;
989
990 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
991 switch (dev_info->segment_type) {
992 case SEG_TYPE_SR:
993 case SEG_TYPE_ER:
994 case SEG_TYPE_SC:
995 if (!dev_info->is_shared)
996 rc = -EINVAL;
997 break;
998 default:
999 rc = -EINVAL;
1000 break;
1001 }
1002 if (rc)
1003 break;
1004 }
1005 if (rc)
1006 pr_err("Suspending the system failed because DCSS device %s "
1007 "is writable\n",
1008 dev_info->segment_name);
1009 return rc;
1010 }
1011
1012 static int dcssblk_restore(struct device *dev)
1013 {
1014 struct dcssblk_dev_info *dev_info;
1015 struct segment_info *entry;
1016 unsigned long start, end;
1017 int rc = 0;
1018
1019 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
1020 list_for_each_entry(entry, &dev_info->seg_list, lh) {
1021 segment_unload(entry->segment_name);
1022 rc = segment_load(entry->segment_name, SEGMENT_SHARED,
1023 &start, &end);
1024 if (rc < 0) {
1025 // TODO in_use check ?
1026 segment_warning(rc, entry->segment_name);
1027 goto out_panic;
1028 }
1029 if (start != entry->start || end != entry->end) {
1030 pr_err("The address range of DCSS %s changed "
1031 "while the system was suspended\n",
1032 entry->segment_name);
1033 goto out_panic;
1034 }
1035 }
1036 }
1037 return 0;
1038 out_panic:
1039 panic("fatal dcssblk resume error\n");
1040 }
1041
1042 static int dcssblk_thaw(struct device *dev)
1043 {
1044 return 0;
1045 }
1046
1047 static const struct dev_pm_ops dcssblk_pm_ops = {
1048 .freeze = dcssblk_freeze,
1049 .thaw = dcssblk_thaw,
1050 .restore = dcssblk_restore,
1051 };
1052
1053 static struct platform_driver dcssblk_pdrv = {
1054 .driver = {
1055 .name = "dcssblk",
1056 .pm = &dcssblk_pm_ops,
1057 },
1058 };
1059
1060 static struct platform_device *dcssblk_pdev;
1061
1062
1063 /*
1064 * The init/exit functions.
1065 */
1066 static void __exit
1067 dcssblk_exit(void)
1068 {
1069 platform_device_unregister(dcssblk_pdev);
1070 platform_driver_unregister(&dcssblk_pdrv);
1071 root_device_unregister(dcssblk_root_dev);
1072 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
1073 }
1074
1075 static int __init
1076 dcssblk_init(void)
1077 {
1078 int rc;
1079
1080 rc = platform_driver_register(&dcssblk_pdrv);
1081 if (rc)
1082 return rc;
1083
1084 dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
1085 0);
1086 if (IS_ERR(dcssblk_pdev)) {
1087 rc = PTR_ERR(dcssblk_pdev);
1088 goto out_pdrv;
1089 }
1090
1091 dcssblk_root_dev = root_device_register("dcssblk");
1092 if (IS_ERR(dcssblk_root_dev)) {
1093 rc = PTR_ERR(dcssblk_root_dev);
1094 goto out_pdev;
1095 }
1096 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
1097 if (rc)
1098 goto out_root;
1099 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
1100 if (rc)
1101 goto out_root;
1102 rc = register_blkdev(0, DCSSBLK_NAME);
1103 if (rc < 0)
1104 goto out_root;
1105 dcssblk_major = rc;
1106 init_rwsem(&dcssblk_devices_sem);
1107
1108 dcssblk_check_params();
1109 return 0;
1110
1111 out_root:
1112 root_device_unregister(dcssblk_root_dev);
1113 out_pdev:
1114 platform_device_unregister(dcssblk_pdev);
1115 out_pdrv:
1116 platform_driver_unregister(&dcssblk_pdrv);
1117 return rc;
1118 }
1119
1120 module_init(dcssblk_init);
1121 module_exit(dcssblk_exit);
1122
1123 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1124 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
1125 "comma-separated list, names in each set separated "
1126 "by commas are separated by colons, each set contains "
1127 "names of contiguous segments and each name max. 8 chars.\n"
1128 "Adding \"(local)\" to the end of each set equals echoing 0 "
1129 "to /sys/devices/dcssblk/<device name>/shared after loading "
1130 "the contiguous segments - \n"
1131 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1132
1133 MODULE_LICENSE("GPL");