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