blob: 7abb240847c07dd0b24f3f2e7f03d221a1416f5f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dcssblk.c -- the S/390 block driver for dcss memory
3 *
4 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
5 */
6
Hongjie Yang93098bf2008-12-25 13:39:45 +01007#define KMSG_COMPONENT "dcssblk"
8#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/completion.h>
18#include <linux/interrupt.h>
Gerald Schaeferc3695272009-06-16 10:30:51 +020019#include <linux/platform_device.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080020#include <linux/pfn_t.h>
Dan Williams5d61e432017-06-27 13:06:22 -070021#include <linux/uio.h>
Dan Williams7a2765f2017-01-26 13:54:35 -080022#include <linux/dax.h>
Gerald Schaeferc3695272009-06-16 10:30:51 +020023#include <asm/extmem.h>
24#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define DCSSBLK_NAME "dcssblk"
27#define DCSSBLK_MINORS_PER_DISK 1
28#define DCSSBLK_PARM_LEN 400
Kay Sievers98df67b2008-12-25 13:38:55 +010029#define DCSS_BUS_ID_SIZE 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Al Viro46d74322008-03-02 10:37:41 -050031static int dcssblk_open(struct block_device *bdev, fmode_t mode);
Al Virodb2a1442013-05-05 21:52:57 -040032static void dcssblk_release(struct gendisk *disk, fmode_t mode);
Jens Axboedece1632015-11-05 10:41:16 -070033static blk_qc_t dcssblk_make_request(struct request_queue *q,
34 struct bio *bio);
Dan Williams7a2765f2017-01-26 13:54:35 -080035static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
36 long nr_pages, void **kaddr, pfn_t *pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
39
40static int dcssblk_major;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070041static const struct block_device_operations dcssblk_devops = {
Carsten Otte420edbc2005-06-23 22:05:23 -070042 .owner = THIS_MODULE,
Al Viro46d74322008-03-02 10:37:41 -050043 .open = dcssblk_open,
44 .release = dcssblk_release,
Dan Williams7a2765f2017-01-26 13:54:35 -080045};
46
Dan Williams5d61e432017-06-27 13:06:22 -070047static 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
Dan Williams7a2765f2017-01-26 13:54:35 -080053static const struct dax_operations dcssblk_dax_ops = {
54 .direct_access = dcssblk_dax_direct_access,
Dan Williams5d61e432017-06-27 13:06:22 -070055 .copy_from_iter = dcssblk_dax_copy_from_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct dcssblk_dev_info {
59 struct list_head lh;
60 struct device dev;
Kay Sievers98df67b2008-12-25 13:38:55 +010061 char segment_name[DCSS_BUS_ID_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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;
Hongjie Yangb2300b92008-10-10 21:33:21 +020070 int num_of_segments;
71 struct list_head seg_list;
Dan Williams7a2765f2017-01-26 13:54:35 -080072 struct dax_device *dax_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Hongjie Yangb2300b92008-10-10 21:33:21 +020075struct segment_info {
76 struct list_head lh;
Kay Sievers98df67b2008-12-25 13:38:55 +010077 char segment_name[DCSS_BUS_ID_SIZE];
Hongjie Yangb2300b92008-10-10 21:33:21 +020078 unsigned long start;
79 unsigned long end;
80 int segment_type;
81};
82
83static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
84 size_t count);
85static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
86 size_t count);
Hongjie Yangb2300b92008-10-10 21:33:21 +020087
88static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
89static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
Hongjie Yangb2300b92008-10-10 21:33:21 +020090
91static struct device *dcssblk_root_dev;
92
Denis Chengc11ca972008-01-26 14:11:13 +010093static LIST_HEAD(dcssblk_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct rw_semaphore dcssblk_devices_sem;
95
96/*
97 * release function for segment device.
98 */
99static void
100dcssblk_release_segment(struct device *dev)
101{
Hongjie Yangb2300b92008-10-10 21:33:21 +0200102 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100120static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121dcssblk_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)
Gerald Schaeferd0591482009-06-12 10:26:35 +0200132 if (minor == entry->gd->first_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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
Hongjie Yangb2300b92008-10-10 21:33:21 +0200160/*
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) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100259 pr_err("Adjacent DCSSs %s and %s are not "
260 "contiguous\n", sort_list[i].segment_name,
261 sort_list[i+1].segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200262 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)) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100272 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);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200276 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
323 * device attribute for switching shared/nonshared (exclusive)
324 * operation (show + store)
325 */
326static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400327dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
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
Yani Ioannoue404e272005-05-17 06:42:58 -0400336dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200339 struct segment_info *entry, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int rc;
341
Hongjie Yangded77fb2008-07-14 09:59:39 +0200342 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 down_write(&dcssblk_devices_sem);
345 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
346 if (atomic_read(&dev_info->use_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 rc = -EBUSY;
348 goto out;
349 }
350 if (inbuf[0] == '1') {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200351 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200361 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 } else if (inbuf[0] == '0') {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200369 /* reload segments in exclusive mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (dev_info->segment_type == SEG_TYPE_SC) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100371 pr_err("DCSS %s is of type SC and cannot be "
372 "loaded as exclusive-writable\n",
373 dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rc = -EINVAL;
375 goto out;
376 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200377 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200386 dev_info->is_shared = 0;
387 set_disk_ro(dev_info->gd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 rc = -EINVAL;
390 goto out;
391 }
392 rc = count;
393 goto out;
394
395removeseg:
Hongjie Yang93098bf2008-12-25 13:39:45 +0100396 pr_err("DCSS device %s is removed after a failed access mode "
397 "change\n", dev_info->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200398 temp = entry;
399 list_for_each_entry(entry, &dev_info->seg_list, lh) {
400 if (entry != temp)
401 segment_unload(entry->segment_name);
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 list_del(&dev_info->lh);
404
Dan Williams7a2765f2017-01-26 13:54:35 -0800405 kill_dax(dev_info->dax_dev);
406 put_dax(dev_info->dax_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 del_gendisk(dev_info->gd);
Al Viro1312f402006-03-12 11:02:03 -0500408 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dev_info->gd->queue = NULL;
410 put_disk(dev_info->gd);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500411 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418out:
419 up_write(&dcssblk_devices_sem);
420 return rc;
421}
Sebastian Ott521b3d72012-10-01 09:12:01 +0200422static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
423 dcssblk_shared_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
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
Yani Ioannoue404e272005-05-17 06:42:58 -0400433dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
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
Yani Ioannoue404e272005-05-17 06:42:58 -0400442dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200445 struct segment_info *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Hongjie Yangded77fb2008-07-14 09:59:39 +0200447 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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
Hongjie Yang93098bf2008-12-25 13:39:45 +0100455 pr_info("All DCSSs that map to device %s are "
456 "saved\n", dev_info->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200457 list_for_each_entry(entry, &dev_info->seg_list, lh) {
Gerald Schaefer5be6fdc2015-01-16 18:29:04 +0100458 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);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else {
467 // device is busy => we save it when it becomes
468 // idle in dcssblk_release
Hongjie Yang93098bf2008-12-25 13:39:45 +0100469 pr_info("Device %s is in use, its DCSSs will be "
470 "saved when it becomes idle\n",
471 dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 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;
Hongjie Yang93098bf2008-12-25 13:39:45 +0100479 pr_info("A pending save request for device %s "
480 "has been canceled\n",
481 dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 } else {
484 up_write(&dcssblk_devices_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -EINVAL;
486 }
487 up_write(&dcssblk_devices_sem);
488 return count;
489}
Sebastian Ott521b3d72012-10-01 09:12:01 +0200490static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
491 dcssblk_save_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/*
Hongjie Yangb2300b92008-10-10 21:33:21 +0200494 * 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}
Sebastian Ott521b3d72012-10-01 09:12:01 +0200518static 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};
Hongjie Yangb2300b92008-10-10 21:33:21 +0200533
534/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * device attribute for adding devices
536 */
537static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400538dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Hongjie Yangb2300b92008-10-10 21:33:21 +0200540 int rc, i, j, num_of_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200542 struct segment_info *seg_info, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 char *local_buf;
544 unsigned long seg_byte_size;
545
546 dev_info = NULL;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200547 seg_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (dev != dcssblk_root_dev) {
549 rc = -EINVAL;
550 goto out_nobuf;
551 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200552 if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
553 rc = -ENAMETOOLONG;
554 goto out_nobuf;
555 }
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 local_buf = kmalloc(count + 1, GFP_KERNEL);
558 if (local_buf == NULL) {
559 rc = -ENOMEM;
560 goto out_nobuf;
561 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * parse input
565 */
Hongjie Yangb2300b92008-10-10 21:33:21 +0200566 num_of_segments = 0;
Ameen Ali3a9f9182015-02-24 18:41:50 +0200567 for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200568 for (j = i; j < count &&
569 (buf[j] != ':') &&
Hongjie Yangb2300b92008-10-10 21:33:21 +0200570 (buf[j] != '\0') &&
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200571 (buf[j] != '\n'); j++) {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200572 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Hongjie Yangb2300b92008-10-10 21:33:21 +0200605 /* no trailing colon at the end of the input */
606 if ((i > 0) && (buf[i-1] == ':')) {
607 rc = -ENAMETOOLONG;
608 goto seg_list_del;
609 }
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);
618
Kees Cookef283688f2014-06-10 10:46:20 -0700619 dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 dev_info->dev.release = dcssblk_release_segment;
Sebastian Ott521b3d72012-10-01 09:12:01 +0200621 dev_info->dev.groups = dcssblk_dev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 INIT_LIST_HEAD(&dev_info->lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
624 if (dev_info->gd == NULL) {
625 rc = -ENOMEM;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200626 goto seg_list_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
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;
Heiko Carstensc5411db2008-02-05 16:50:50 +0100633 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
Martin K. Petersene1defc42009-05-22 17:17:49 -0400634 blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
Toshi Kani163d4ba2016-06-23 17:05:50 -0400635 queue_flag_set_unlocked(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 seg_byte_size = (dev_info->end - dev_info->start + 1);
638 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
Hongjie Yang93098bf2008-12-25 13:39:45 +0100639 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
640 "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 dev_info->save_pending = 0;
643 dev_info->is_shared = 1;
644 dev_info->dev.parent = dcssblk_root_dev;
645
646 /*
Hongjie Yangb2300b92008-10-10 21:33:21 +0200647 *get minor, add to list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
649 down_write(&dcssblk_devices_sem);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200650 if (dcssblk_get_segment_by_name(local_buf)) {
Gerald Schaefer04f64b52008-08-21 19:46:40 +0200651 rc = -EEXIST;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200652 goto release_gd;
Gerald Schaefer04f64b52008-08-21 19:46:40 +0200653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 rc = dcssblk_assign_free_minor(dev_info);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200655 if (rc)
656 goto release_gd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 sprintf(dev_info->gd->disk_name, "dcssblk%d",
Gerald Schaeferd0591482009-06-12 10:26:35 +0200658 dev_info->gd->first_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 list_add_tail(&dev_info->lh, &dcssblk_devices);
660
661 if (!try_module_get(THIS_MODULE)) {
662 rc = -ENODEV;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200663 goto dev_list_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 /*
666 * register the device
667 */
668 rc = device_register(&dev_info->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (rc)
Sebastian Ott521b3d72012-10-01 09:12:01 +0200670 goto put_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Dan Williams7a2765f2017-01-26 13:54:35 -0800672 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
Sebastian Ott521b3d72012-10-01 09:12:01 +0200679 get_device(&dev_info->dev);
Dan Williams0d52c7562016-06-15 19:44:20 -0700680 device_add_disk(&dev_info->dev, dev_info->gd);
Christian Borntraeger436d1bc2007-12-04 16:09:03 +0100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 up_write(&dcssblk_devices_sem);
693 rc = count;
694 goto out;
695
Sebastian Ott521b3d72012-10-01 09:12:01 +0200696put_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 list_del(&dev_info->lh);
Al Viro1312f402006-03-12 11:02:03 -0500698 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dev_info->gd->queue = NULL;
700 put_disk(dev_info->gd);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200701 list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
702 segment_unload(seg_info->segment_name);
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 put_device(&dev_info->dev);
705 up_write(&dcssblk_devices_sem);
706 goto out;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200707dev_list_del:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 list_del(&dev_info->lh);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200709release_gd:
Al Viro1312f402006-03-12 11:02:03 -0500710 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dev_info->gd->queue = NULL;
712 put_disk(dev_info->gd);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200713 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 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
Yani Ioannoue404e272005-05-17 06:42:58 -0400733dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200736 struct segment_info *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 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 */
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200750 for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 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);
Joe Perchesbaebc702016-03-03 20:49:57 -0800763 pr_warn("Device %s cannot be removed because it is not a known device\n",
764 local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 rc = -ENODEV;
766 goto out_buf;
767 }
768 if (atomic_read(&dev_info->use_count) != 0) {
769 up_write(&dcssblk_devices_sem);
Joe Perchesbaebc702016-03-03 20:49:57 -0800770 pr_warn("Device %s cannot be removed while it is in use\n",
771 local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 rc = -EBUSY;
773 goto out_buf;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Hongjie Yangb2300b92008-10-10 21:33:21 +0200776 list_del(&dev_info->lh);
Dan Williams7a2765f2017-01-26 13:54:35 -0800777 kill_dax(dev_info->dax_dev);
778 put_dax(dev_info->dax_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 del_gendisk(dev_info->gd);
Al Viro1312f402006-03-12 11:02:03 -0500780 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 dev_info->gd->queue = NULL;
782 put_disk(dev_info->gd);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200783
784 /* unload all related segments */
785 list_for_each_entry(entry, &dev_info->seg_list, lh)
786 segment_unload(entry->segment_name);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 up_write(&dcssblk_devices_sem);
789
Gerald Schaefer1378a682016-04-08 13:23:52 +0200790 device_unregister(&dev_info->dev);
791 put_device(&dev_info->dev);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 rc = count;
794out_buf:
795 kfree(local_buf);
796 return rc;
797}
798
799static int
Al Viro46d74322008-03-02 10:37:41 -0500800dcssblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct dcssblk_dev_info *dev_info;
803 int rc;
804
Al Viro46d74322008-03-02 10:37:41 -0500805 dev_info = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (NULL == dev_info) {
807 rc = -ENODEV;
808 goto out;
809 }
810 atomic_inc(&dev_info->use_count);
Al Viro46d74322008-03-02 10:37:41 -0500811 bdev->bd_block_size = 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 rc = 0;
813out:
814 return rc;
815}
816
Al Virodb2a1442013-05-05 21:52:57 -0400817static void
Al Viro46d74322008-03-02 10:37:41 -0500818dcssblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Al Viro46d74322008-03-02 10:37:41 -0500820 struct dcssblk_dev_info *dev_info = disk->private_data;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200821 struct segment_info *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Al Viro46d74322008-03-02 10:37:41 -0500823 if (!dev_info) {
Al Virodb2a1442013-05-05 21:52:57 -0400824 WARN_ON(1);
825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 down_write(&dcssblk_devices_sem);
828 if (atomic_dec_and_test(&dev_info->use_count)
829 && (dev_info->save_pending)) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100830 pr_info("Device %s has become idle and is being saved "
831 "now\n", dev_info->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200832 list_for_each_entry(entry, &dev_info->seg_list, lh) {
Gerald Schaefer5be6fdc2015-01-16 18:29:04 +0100833 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);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 dev_info->save_pending = 0;
841 }
842 up_write(&dcssblk_devices_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Jens Axboedece1632015-11-05 10:41:16 -0700845static blk_qc_t
Jens Axboe165125e2007-07-24 09:28:11 +0200846dcssblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct dcssblk_dev_info *dev_info;
Kent Overstreet79886132013-11-23 17:19:00 -0800849 struct bio_vec bvec;
850 struct bvec_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned long index;
852 unsigned long page_addr;
853 unsigned long source_addr;
854 unsigned long bytes_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
NeilBrownaf67c312017-06-18 14:38:57 +1000856 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 bytes_done = 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200859 dev_info = bio->bi_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (dev_info == NULL)
861 goto fail;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700862 if ((bio->bi_iter.bi_sector & 7) != 0 ||
863 (bio->bi_iter.bi_size & 4095) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* Request is not page-aligned. */
865 goto fail;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200866 if (bio_end_sector(bio) > get_capacity(bio->bi_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Request beyond end of DCSS segment. */
868 goto fail;
869 }
Carsten Otte420edbc2005-06-23 22:05:23 -0700870 /* 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) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800878 pr_warn("Writing to %s failed because it is a read-only device\n",
879 dev_name(&dev_info->dev));
Carsten Otte420edbc2005-06-23 22:05:23 -0700880 goto fail;
881 }
882 }
883 }
884
Kent Overstreet4f024f32013-10-11 15:44:27 -0700885 index = (bio->bi_iter.bi_sector >> 3);
Kent Overstreet79886132013-11-23 17:19:00 -0800886 bio_for_each_segment(bvec, bio, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 page_addr = (unsigned long)
Kent Overstreet79886132013-11-23 17:19:00 -0800888 page_address(bvec.bv_page) + bvec.bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 source_addr = dev_info->start + (index<<12) + bytes_done;
Kent Overstreet79886132013-11-23 17:19:00 -0800890 if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 // More paranoia.
892 goto fail;
893 if (bio_data_dir(bio) == READ) {
894 memcpy((void*)page_addr, (void*)source_addr,
Kent Overstreet79886132013-11-23 17:19:00 -0800895 bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 memcpy((void*)source_addr, (void*)page_addr,
Kent Overstreet79886132013-11-23 17:19:00 -0800898 bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Kent Overstreet79886132013-11-23 17:19:00 -0800900 bytes_done += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200902 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700903 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904fail:
NeilBrown6712ecf2007-09-27 12:47:43 +0200905 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700906 return BLK_QC_T_NONE;
Carsten Otte420edbc2005-06-23 22:05:23 -0700907}
908
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200909static long
Dan Williams7a2765f2017-01-26 13:54:35 -0800910__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
923static long
Dan Williams7a2765f2017-01-26 13:54:35 -0800924dcssblk_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);
928
929 return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932static void
933dcssblk_check_params(void)
934{
935 int rc, i, j, k;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200936 char buf[DCSSBLK_PARM_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct dcssblk_dev_info *dev_info;
938
939 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
940 i++) {
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200941 for (j = i; (j < DCSSBLK_PARM_LEN) &&
942 (dcssblk_segments[j] != ',') &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 (dcssblk_segments[j] != '\0') &&
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200944 (dcssblk_segments[j] != '('); j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 {
946 buf[j-i] = dcssblk_segments[j];
947 }
948 buf[j-i] = '\0';
Cornelia Huckf901e5d12005-06-25 14:55:29 -0700949 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200951 for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 buf[k] = toupper(buf[k]);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200953 buf[k] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 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,
Cornelia Huckf901e5d12005-06-25 14:55:29 -0700960 NULL, "0\n", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
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
974/*
Gerald Schaeferc3695272009-06-16 10:30:51 +0200975 * 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)
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200998 pr_err("Suspending the system failed because DCSS device %s "
999 "is writable\n",
Gerald Schaeferc3695272009-06-16 10:30:51 +02001000 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) {
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +02001022 pr_err("The address range of DCSS %s changed "
1023 "while the system was suspended\n",
Gerald Schaeferc3695272009-06-16 10:30:51 +02001024 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
Alexey Dobriyan47145212009-12-14 18:00:08 -08001039static const struct dev_pm_ops dcssblk_pm_ops = {
Gerald Schaeferc3695272009-06-16 10:30:51 +02001040 .freeze = dcssblk_freeze,
1041 .thaw = dcssblk_thaw,
1042 .restore = dcssblk_restore,
1043};
1044
1045static struct platform_driver dcssblk_pdrv = {
1046 .driver = {
1047 .name = "dcssblk",
Gerald Schaeferc3695272009-06-16 10:30:51 +02001048 .pm = &dcssblk_pm_ops,
1049 },
1050};
1051
1052static struct platform_device *dcssblk_pdev;
1053
1054
1055/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * The init/exit functions.
1057 */
1058static void __exit
1059dcssblk_exit(void)
1060{
Gerald Schaeferc3695272009-06-16 10:30:51 +02001061 platform_device_unregister(dcssblk_pdev);
1062 platform_driver_unregister(&dcssblk_pdrv);
Mark McLoughlin035da162008-12-15 12:58:29 +00001063 root_device_unregister(dcssblk_root_dev);
Akinobu Mita00d59402007-07-17 04:03:46 -07001064 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static int __init
1068dcssblk_init(void)
1069{
1070 int rc;
1071
Gerald Schaeferc3695272009-06-16 10:30:51 +02001072 rc = platform_driver_register(&dcssblk_pdrv);
1073 if (rc)
1074 return rc;
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;
1081 }
1082
Mark McLoughlin035da162008-12-15 12:58:29 +00001083 dcssblk_root_dev = root_device_register("dcssblk");
Gerald Schaeferc3695272009-06-16 10:30:51 +02001084 if (IS_ERR(dcssblk_root_dev)) {
1085 rc = PTR_ERR(dcssblk_root_dev);
1086 goto out_pdev;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
Gerald Schaeferc3695272009-06-16 10:30:51 +02001089 if (rc)
1090 goto out_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
Gerald Schaeferc3695272009-06-16 10:30:51 +02001092 if (rc)
1093 goto out_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 rc = register_blkdev(0, DCSSBLK_NAME);
Gerald Schaeferc3695272009-06-16 10:30:51 +02001095 if (rc < 0)
1096 goto out_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 dcssblk_major = rc;
1098 init_rwsem(&dcssblk_devices_sem);
1099
1100 dcssblk_check_params();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return 0;
Gerald Schaeferc3695272009-06-16 10:30:51 +02001102
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
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, "
Hongjie Yangb2300b92008-10-10 21:33:21 +02001117 "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)\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125MODULE_LICENSE("GPL");