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