dcssblk.c 27.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7
/*
 * dcssblk.c -- the S/390 block driver for dcss memory
 *
 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
 */

8 9 10
#define KMSG_COMPONENT "dcssblk"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/blkdev.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
20
#include <linux/platform_device.h>
D
Dan Williams 已提交
21
#include <linux/pfn_t.h>
22
#include <linux/uio.h>
23
#include <linux/dax.h>
24 25
#include <asm/extmem.h>
#include <asm/io.h>
L
Linus Torvalds 已提交
26 27 28 29

#define DCSSBLK_NAME "dcssblk"
#define DCSSBLK_MINORS_PER_DISK 1
#define DCSSBLK_PARM_LEN 400
30
#define DCSS_BUS_ID_SIZE 20
L
Linus Torvalds 已提交
31

A
Al Viro 已提交
32
static int dcssblk_open(struct block_device *bdev, fmode_t mode);
33
static void dcssblk_release(struct gendisk *disk, fmode_t mode);
34 35
static blk_qc_t dcssblk_make_request(struct request_queue *q,
						struct bio *bio);
36 37
static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
		long nr_pages, void **kaddr, pfn_t *pfn);
L
Linus Torvalds 已提交
38 39 40 41

static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";

static int dcssblk_major;
42
static const struct block_device_operations dcssblk_devops = {
43
	.owner   	= THIS_MODULE,
A
Al Viro 已提交
44 45
	.open    	= dcssblk_open,
	.release 	= dcssblk_release,
46 47
};

48 49 50 51 52 53
static size_t dcssblk_dax_copy_from_iter(struct dax_device *dax_dev,
		pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i)
{
	return copy_from_iter(addr, bytes, i);
}

54 55
static const struct dax_operations dcssblk_dax_ops = {
	.direct_access = dcssblk_dax_direct_access,
56
	.copy_from_iter = dcssblk_dax_copy_from_iter,
L
Linus Torvalds 已提交
57 58
};

59 60 61
struct dcssblk_dev_info {
	struct list_head lh;
	struct device dev;
62
	char segment_name[DCSS_BUS_ID_SIZE];
63 64 65 66 67 68 69 70 71 72
	atomic_t use_count;
	struct gendisk *gd;
	unsigned long start;
	unsigned long end;
	int segment_type;
	unsigned char save_pending;
	unsigned char is_shared;
	struct request_queue *dcssblk_queue;
	int num_of_segments;
	struct list_head seg_list;
73
	struct dax_device *dax_dev;
74 75 76 77
};

struct segment_info {
	struct list_head lh;
78
	char segment_name[DCSS_BUS_ID_SIZE];
79 80 81 82 83
	unsigned long start;
	unsigned long end;
	int segment_type;
};

84
static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
L
Linus Torvalds 已提交
85
				  size_t count);
86
static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
L
Linus Torvalds 已提交
87 88 89 90 91 92 93
				  size_t count);

static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);

static struct device *dcssblk_root_dev;

94
static LIST_HEAD(dcssblk_devices);
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102
static struct rw_semaphore dcssblk_devices_sem;

/*
 * release function for segment device.
 */
static void
dcssblk_release_segment(struct device *dev)
{
103 104 105 106 107 108 109 110 111
	struct dcssblk_dev_info *dev_info;
	struct segment_info *entry, *temp;

	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
	list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
		list_del(&entry->lh);
		kfree(entry);
	}
	kfree(dev_info);
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120
	module_put(THIS_MODULE);
}

/*
 * get a minor number. needs to be called with
 * down_write(&dcssblk_devices_sem) and the
 * device needs to be enqueued before the semaphore is
 * freed.
 */
121
static int
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132
dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
{
	int minor, found;
	struct dcssblk_dev_info *entry;

	if (dev_info == NULL)
		return -EINVAL;
	for (minor = 0; minor < (1<<MINORBITS); minor++) {
		found = 0;
		// test if minor available
		list_for_each_entry(entry, &dcssblk_devices, lh)
133
			if (minor == entry->gd->first_minor)
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
				found++;
		if (!found) break; // got unused minor
	}
	if (found)
		return -EBUSY;
	dev_info->gd->first_minor = minor;
	return 0;
}

/*
 * get the struct dcssblk_dev_info from dcssblk_devices
 * for the given name.
 * down_read(&dcssblk_devices_sem) must be held.
 */
static struct dcssblk_dev_info *
dcssblk_get_device_by_name(char *name)
{
	struct dcssblk_dev_info *entry;

	list_for_each_entry(entry, &dcssblk_devices, lh) {
		if (!strcmp(name, entry->segment_name)) {
			return entry;
		}
	}
	return NULL;
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/*
 * get the struct segment_info from seg_list
 * for the given name.
 * down_read(&dcssblk_devices_sem) must be held.
 */
static struct segment_info *
dcssblk_get_segment_by_name(char *name)
{
	struct dcssblk_dev_info *dev_info;
	struct segment_info *entry;

	list_for_each_entry(dev_info, &dcssblk_devices, lh) {
		list_for_each_entry(entry, &dev_info->seg_list, lh) {
			if (!strcmp(name, entry->segment_name))
				return entry;
		}
	}
	return NULL;
}

/*
 * get the highest address of the multi-segment block.
 */
static unsigned long
dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
{
	unsigned long highest_addr;
	struct segment_info *entry;

	highest_addr = 0;
	list_for_each_entry(entry, &dev_info->seg_list, lh) {
		if (highest_addr < entry->end)
			highest_addr = entry->end;
	}
	return highest_addr;
}

/*
 * get the lowest address of the multi-segment block.
 */
static unsigned long
dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
{
	int set_first;
	unsigned long lowest_addr;
	struct segment_info *entry;

	set_first = 0;
	lowest_addr = 0;
	list_for_each_entry(entry, &dev_info->seg_list, lh) {
		if (set_first == 0) {
			lowest_addr = entry->start;
			set_first = 1;
		} else {
			if (lowest_addr > entry->start)
				lowest_addr = entry->start;
		}
	}
	return lowest_addr;
}

/*
 * Check continuity of segments.
 */
static int
dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
{
	int i, j, rc;
	struct segment_info *sort_list, *entry, temp;

	if (dev_info->num_of_segments <= 1)
		return 0;

	sort_list = kzalloc(
			sizeof(struct segment_info) * dev_info->num_of_segments,
			GFP_KERNEL);
	if (sort_list == NULL)
		return -ENOMEM;
	i = 0;
	list_for_each_entry(entry, &dev_info->seg_list, lh) {
		memcpy(&sort_list[i], entry, sizeof(struct segment_info));
		i++;
	}

	/* sort segments */
	for (i = 0; i < dev_info->num_of_segments; i++)
		for (j = 0; j < dev_info->num_of_segments; j++)
			if (sort_list[j].start > sort_list[i].start) {
				memcpy(&temp, &sort_list[i],
					sizeof(struct segment_info));
				memcpy(&sort_list[i], &sort_list[j],
					sizeof(struct segment_info));
				memcpy(&sort_list[j], &temp,
					sizeof(struct segment_info));
			}

	/* check continuity */
	for (i = 0; i < dev_info->num_of_segments - 1; i++) {
		if ((sort_list[i].end + 1) != sort_list[i+1].start) {
260 261 262
			pr_err("Adjacent DCSSs %s and %s are not "
			       "contiguous\n", sort_list[i].segment_name,
			       sort_list[i+1].segment_name);
263 264 265 266 267 268 269 270 271 272
			rc = -EINVAL;
			goto out;
		}
		/* EN and EW are allowed in a block device */
		if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
			if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
				(sort_list[i].segment_type == SEG_TYPE_ER) ||
				!(sort_list[i+1].segment_type &
				SEGMENT_EXCLUSIVE) ||
				(sort_list[i+1].segment_type == SEG_TYPE_ER)) {
273 274 275 276
				pr_err("DCSS %s and DCSS %s have "
				       "incompatible types\n",
				       sort_list[i].segment_name,
				       sort_list[i+1].segment_name);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
				rc = -EINVAL;
				goto out;
			}
		}
	}
	rc = 0;
out:
	kfree(sort_list);
	return rc;
}

/*
 * Load a segment
 */
static int
dcssblk_load_segment(char *name, struct segment_info **seg_info)
{
	int rc;

	/* already loaded? */
	down_read(&dcssblk_devices_sem);
	*seg_info = dcssblk_get_segment_by_name(name);
	up_read(&dcssblk_devices_sem);
	if (*seg_info != NULL)
		return -EEXIST;

	/* get a struct segment_info */
	*seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
	if (*seg_info == NULL)
		return -ENOMEM;

	strcpy((*seg_info)->segment_name, name);

	/* load the segment */
	rc = segment_load(name, SEGMENT_SHARED,
			&(*seg_info)->start, &(*seg_info)->end);
	if (rc < 0) {
		segment_warning(rc, (*seg_info)->segment_name);
		kfree(*seg_info);
	} else {
		INIT_LIST_HEAD(&(*seg_info)->lh);
		(*seg_info)->segment_type = rc;
	}
	return rc;
}

L
Linus Torvalds 已提交
323 324 325 326 327
/*
 * device attribute for switching shared/nonshared (exclusive)
 * operation (show + store)
 */
static ssize_t
328
dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336
{
	struct dcssblk_dev_info *dev_info;

	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
	return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
}

static ssize_t
337
dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
L
Linus Torvalds 已提交
338 339
{
	struct dcssblk_dev_info *dev_info;
340
	struct segment_info *entry, *temp;
L
Linus Torvalds 已提交
341 342
	int rc;

343
	if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351
		return -EINVAL;
	down_write(&dcssblk_devices_sem);
	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
	if (atomic_read(&dev_info->use_count)) {
		rc = -EBUSY;
		goto out;
	}
	if (inbuf[0] == '1') {
352 353 354 355 356 357 358 359
		/* reload segments in shared mode */
		list_for_each_entry(entry, &dev_info->seg_list, lh) {
			rc = segment_modify_shared(entry->segment_name,
						SEGMENT_SHARED);
			if (rc < 0) {
				BUG_ON(rc == -EINVAL);
				if (rc != -EAGAIN)
					goto removeseg;
L
Linus Torvalds 已提交
360 361
			}
		}
362 363 364 365 366 367 368
		dev_info->is_shared = 1;
		switch (dev_info->segment_type) {
		case SEG_TYPE_SR:
		case SEG_TYPE_ER:
		case SEG_TYPE_SC:
			set_disk_ro(dev_info->gd, 1);
		}
L
Linus Torvalds 已提交
369
	} else if (inbuf[0] == '0') {
370
		/* reload segments in exclusive mode */
L
Linus Torvalds 已提交
371
		if (dev_info->segment_type == SEG_TYPE_SC) {
372 373 374
			pr_err("DCSS %s is of type SC and cannot be "
			       "loaded as exclusive-writable\n",
			       dev_info->segment_name);
L
Linus Torvalds 已提交
375 376 377
			rc = -EINVAL;
			goto out;
		}
378 379 380 381 382 383 384 385
		list_for_each_entry(entry, &dev_info->seg_list, lh) {
			rc = segment_modify_shared(entry->segment_name,
						   SEGMENT_EXCLUSIVE);
			if (rc < 0) {
				BUG_ON(rc == -EINVAL);
				if (rc != -EAGAIN)
					goto removeseg;
			}
L
Linus Torvalds 已提交
386
		}
387 388
		dev_info->is_shared = 0;
		set_disk_ro(dev_info->gd, 0);
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396
	} else {
		rc = -EINVAL;
		goto out;
	}
	rc = count;
	goto out;

removeseg:
397 398
	pr_err("DCSS device %s is removed after a failed access mode "
	       "change\n", dev_info->segment_name);
399 400 401 402 403
	temp = entry;
	list_for_each_entry(entry, &dev_info->seg_list, lh) {
		if (entry != temp)
			segment_unload(entry->segment_name);
	}
L
Linus Torvalds 已提交
404 405
	list_del(&dev_info->lh);

406 407
	kill_dax(dev_info->dax_dev);
	put_dax(dev_info->dax_dev);
L
Linus Torvalds 已提交
408
	del_gendisk(dev_info->gd);
409
	blk_cleanup_queue(dev_info->dcssblk_queue);
L
Linus Torvalds 已提交
410 411
	dev_info->gd->queue = NULL;
	put_disk(dev_info->gd);
412 413 414 415 416 417 418
	up_write(&dcssblk_devices_sem);

	if (device_remove_file_self(dev, attr)) {
		device_unregister(dev);
		put_device(dev);
	}
	return rc;
L
Linus Torvalds 已提交
419 420 421 422
out:
	up_write(&dcssblk_devices_sem);
	return rc;
}
423 424
static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
		   dcssblk_shared_store);
L
Linus Torvalds 已提交
425 426 427 428 429 430 431 432 433

/*
 * device attribute for save operation on current copy
 * of the segment. If the segment is busy, saving will
 * become pending until it gets released, which can be
 * undone by storing a non-true value to this entry.
 * (show + store)
 */
static ssize_t
434
dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442
{
	struct dcssblk_dev_info *dev_info;

	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
	return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
}

static ssize_t
443
dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
L
Linus Torvalds 已提交
444 445
{
	struct dcssblk_dev_info *dev_info;
446
	struct segment_info *entry;
L
Linus Torvalds 已提交
447

448
	if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
L
Linus Torvalds 已提交
449 450 451 452 453 454 455
		return -EINVAL;
	dev_info = container_of(dev, struct dcssblk_dev_info, dev);

	down_write(&dcssblk_devices_sem);
	if (inbuf[0] == '1') {
		if (atomic_read(&dev_info->use_count) == 0) {
			// device is idle => we save immediately
456 457
			pr_info("All DCSSs that map to device %s are "
				"saved\n", dev_info->segment_name);
458
			list_for_each_entry(entry, &dev_info->seg_list, lh) {
459 460 461 462 463 464 465
				if (entry->segment_type == SEG_TYPE_EN ||
				    entry->segment_type == SEG_TYPE_SN)
					pr_warn("DCSS %s is of type SN or EN"
						" and cannot be saved\n",
						entry->segment_name);
				else
					segment_save(entry->segment_name);
466
			}
L
Linus Torvalds 已提交
467 468 469
		}  else {
			// device is busy => we save it when it becomes
			// idle in dcssblk_release
470 471 472
			pr_info("Device %s is in use, its DCSSs will be "
				"saved when it becomes idle\n",
				dev_info->segment_name);
L
Linus Torvalds 已提交
473 474 475 476 477 478 479
			dev_info->save_pending = 1;
		}
	} else if (inbuf[0] == '0') {
		if (dev_info->save_pending) {
			// device is busy & the user wants to undo his save
			// request
			dev_info->save_pending = 0;
480 481 482
			pr_info("A pending save request for device %s "
				"has been canceled\n",
				dev_info->segment_name);
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490
		}
	} else {
		up_write(&dcssblk_devices_sem);
		return -EINVAL;
	}
	up_write(&dcssblk_devices_sem);
	return count;
}
491 492
static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
		   dcssblk_save_store);
L
Linus Torvalds 已提交
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
/*
 * device attribute for showing all segments in a device
 */
static ssize_t
dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	int i;

	struct dcssblk_dev_info *dev_info;
	struct segment_info *entry;

	down_read(&dcssblk_devices_sem);
	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
	i = 0;
	buf[0] = '\0';
	list_for_each_entry(entry, &dev_info->seg_list, lh) {
		strcpy(&buf[i], entry->segment_name);
		i += strlen(entry->segment_name);
		buf[i] = '\n';
		i++;
	}
	up_read(&dcssblk_devices_sem);
	return i;
}
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);

static struct attribute *dcssblk_dev_attrs[] = {
	&dev_attr_shared.attr,
	&dev_attr_save.attr,
	&dev_attr_seglist.attr,
	NULL,
};
static struct attribute_group dcssblk_dev_attr_group = {
	.attrs = dcssblk_dev_attrs,
};
static const struct attribute_group *dcssblk_dev_attr_groups[] = {
	&dcssblk_dev_attr_group,
	NULL,
};
534

L
Linus Torvalds 已提交
535 536 537 538
/*
 * device attribute for adding devices
 */
static ssize_t
539
dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
540
{
541
	int rc, i, j, num_of_segments;
L
Linus Torvalds 已提交
542
	struct dcssblk_dev_info *dev_info;
543
	struct segment_info *seg_info, *temp;
L
Linus Torvalds 已提交
544 545 546 547
	char *local_buf;
	unsigned long seg_byte_size;

	dev_info = NULL;
548
	seg_info = NULL;
L
Linus Torvalds 已提交
549 550 551 552
	if (dev != dcssblk_root_dev) {
		rc = -EINVAL;
		goto out_nobuf;
	}
553 554 555 556 557
	if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
		rc = -ENAMETOOLONG;
		goto out_nobuf;
	}

L
Linus Torvalds 已提交
558 559 560 561 562
	local_buf = kmalloc(count + 1, GFP_KERNEL);
	if (local_buf == NULL) {
		rc = -ENOMEM;
		goto out_nobuf;
	}
563

L
Linus Torvalds 已提交
564 565 566
	/*
	 * parse input
	 */
567
	num_of_segments = 0;
568
	for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
569 570
		for (j = i; j < count &&
			(buf[j] != ':') &&
571
			(buf[j] != '\0') &&
572
			(buf[j] != '\n'); j++) {
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
			local_buf[j-i] = toupper(buf[j]);
		}
		local_buf[j-i] = '\0';
		if (((j - i) == 0) || ((j - i) > 8)) {
			rc = -ENAMETOOLONG;
			goto seg_list_del;
		}

		rc = dcssblk_load_segment(local_buf, &seg_info);
		if (rc < 0)
			goto seg_list_del;
		/*
		 * get a struct dcssblk_dev_info
		 */
		if (num_of_segments == 0) {
			dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
					GFP_KERNEL);
			if (dev_info == NULL) {
				rc = -ENOMEM;
				goto out;
			}
			strcpy(dev_info->segment_name, local_buf);
			dev_info->segment_type = seg_info->segment_type;
			INIT_LIST_HEAD(&dev_info->seg_list);
		}
		list_add_tail(&seg_info->lh, &dev_info->seg_list);
		num_of_segments++;
		i = j;

		if ((buf[j] == '\0') || (buf[j] == '\n'))
			break;
L
Linus Torvalds 已提交
604
	}
605 606 607

	/* no trailing colon at the end of the input */
	if ((i > 0) && (buf[i-1] == ':')) {
L
Linus Torvalds 已提交
608
		rc = -ENAMETOOLONG;
609
		goto seg_list_del;
L
Linus Torvalds 已提交
610
	}
611 612 613 614 615 616 617 618
	strlcpy(local_buf, buf, i + 1);
	dev_info->num_of_segments = num_of_segments;
	rc = dcssblk_is_continuous(dev_info);
	if (rc < 0)
		goto seg_list_del;

	dev_info->start = dcssblk_find_lowest_addr(dev_info);
	dev_info->end = dcssblk_find_highest_addr(dev_info);
L
Linus Torvalds 已提交
619

620
	dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
L
Linus Torvalds 已提交
621
	dev_info->dev.release = dcssblk_release_segment;
622
	dev_info->dev.groups = dcssblk_dev_attr_groups;
L
Linus Torvalds 已提交
623 624 625 626
	INIT_LIST_HEAD(&dev_info->lh);
	dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
	if (dev_info->gd == NULL) {
		rc = -ENOMEM;
627
		goto seg_list_del;
L
Linus Torvalds 已提交
628 629 630 631 632 633
	}
	dev_info->gd->major = dcssblk_major;
	dev_info->gd->fops = &dcssblk_devops;
	dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
	dev_info->gd->queue = dev_info->dcssblk_queue;
	dev_info->gd->private_data = dev_info;
634
	blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
635
	blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
636
	queue_flag_set_unlocked(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
637

L
Linus Torvalds 已提交
638 639
	seg_byte_size = (dev_info->end - dev_info->start + 1);
	set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
640 641
	pr_info("Loaded %s with total size %lu bytes and capacity %lu "
		"sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
L
Linus Torvalds 已提交
642 643 644 645 646 647

	dev_info->save_pending = 0;
	dev_info->is_shared = 1;
	dev_info->dev.parent = dcssblk_root_dev;

	/*
648
	 *get minor, add to list
L
Linus Torvalds 已提交
649 650
	 */
	down_write(&dcssblk_devices_sem);
651
	if (dcssblk_get_segment_by_name(local_buf)) {
652
		rc = -EEXIST;
653
		goto release_gd;
654
	}
L
Linus Torvalds 已提交
655
	rc = dcssblk_assign_free_minor(dev_info);
656 657
	if (rc)
		goto release_gd;
L
Linus Torvalds 已提交
658
	sprintf(dev_info->gd->disk_name, "dcssblk%d",
659
		dev_info->gd->first_minor);
L
Linus Torvalds 已提交
660 661 662 663
	list_add_tail(&dev_info->lh, &dcssblk_devices);

	if (!try_module_get(THIS_MODULE)) {
		rc = -ENODEV;
664
		goto dev_list_del;
L
Linus Torvalds 已提交
665 666 667 668 669
	}
	/*
	 * register the device
	 */
	rc = device_register(&dev_info->dev);
670
	if (rc)
671
		goto put_dev;
L
Linus Torvalds 已提交
672

673 674 675 676 677 678 679
	dev_info->dax_dev = alloc_dax(dev_info, dev_info->gd->disk_name,
			&dcssblk_dax_ops);
	if (!dev_info->dax_dev) {
		rc = -ENOMEM;
		goto put_dev;
	}

680
	get_device(&dev_info->dev);
681
	device_add_disk(&dev_info->dev, dev_info->gd);
682

L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696
	switch (dev_info->segment_type) {
		case SEG_TYPE_SR:
		case SEG_TYPE_ER:
		case SEG_TYPE_SC:
			set_disk_ro(dev_info->gd,1);
			break;
		default:
			set_disk_ro(dev_info->gd,0);
			break;
	}
	up_write(&dcssblk_devices_sem);
	rc = count;
	goto out;

697
put_dev:
L
Linus Torvalds 已提交
698
	list_del(&dev_info->lh);
699
	blk_cleanup_queue(dev_info->dcssblk_queue);
L
Linus Torvalds 已提交
700 701
	dev_info->gd->queue = NULL;
	put_disk(dev_info->gd);
702 703 704
	list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
		segment_unload(seg_info->segment_name);
	}
L
Linus Torvalds 已提交
705 706 707
	put_device(&dev_info->dev);
	up_write(&dcssblk_devices_sem);
	goto out;
708
dev_list_del:
L
Linus Torvalds 已提交
709
	list_del(&dev_info->lh);
710
release_gd:
711
	blk_cleanup_queue(dev_info->dcssblk_queue);
L
Linus Torvalds 已提交
712 713
	dev_info->gd->queue = NULL;
	put_disk(dev_info->gd);
714 715 716 717 718 719 720 721 722
	up_write(&dcssblk_devices_sem);
seg_list_del:
	if (dev_info == NULL)
		goto out;
	list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
		list_del(&seg_info->lh);
		segment_unload(seg_info->segment_name);
		kfree(seg_info);
	}
L
Linus Torvalds 已提交
723 724 725 726 727 728 729 730 731 732 733
	kfree(dev_info);
out:
	kfree(local_buf);
out_nobuf:
	return rc;
}

/*
 * device attribute for removing devices
 */
static ssize_t
734
dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
735 736
{
	struct dcssblk_dev_info *dev_info;
737
	struct segment_info *entry;
L
Linus Torvalds 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750
	int rc, i;
	char *local_buf;

	if (dev != dcssblk_root_dev) {
		return -EINVAL;
	}
	local_buf = kmalloc(count + 1, GFP_KERNEL);
	if (local_buf == NULL) {
		return -ENOMEM;
	}
	/*
	 * parse input
	 */
751
	for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760 761 762 763
		local_buf[i] = toupper(buf[i]);
	}
	local_buf[i] = '\0';
	if ((i == 0) || (i > 8)) {
		rc = -ENAMETOOLONG;
		goto out_buf;
	}

	down_write(&dcssblk_devices_sem);
	dev_info = dcssblk_get_device_by_name(local_buf);
	if (dev_info == NULL) {
		up_write(&dcssblk_devices_sem);
764 765
		pr_warn("Device %s cannot be removed because it is not a known device\n",
			local_buf);
L
Linus Torvalds 已提交
766 767 768 769 770
		rc = -ENODEV;
		goto out_buf;
	}
	if (atomic_read(&dev_info->use_count) != 0) {
		up_write(&dcssblk_devices_sem);
771 772
		pr_warn("Device %s cannot be removed while it is in use\n",
			local_buf);
L
Linus Torvalds 已提交
773 774 775 776
		rc = -EBUSY;
		goto out_buf;
	}

777
	list_del(&dev_info->lh);
778 779
	kill_dax(dev_info->dax_dev);
	put_dax(dev_info->dax_dev);
L
Linus Torvalds 已提交
780
	del_gendisk(dev_info->gd);
781
	blk_cleanup_queue(dev_info->dcssblk_queue);
L
Linus Torvalds 已提交
782 783
	dev_info->gd->queue = NULL;
	put_disk(dev_info->gd);
784 785 786 787 788

	/* unload all related segments */
	list_for_each_entry(entry, &dev_info->seg_list, lh)
		segment_unload(entry->segment_name);

L
Linus Torvalds 已提交
789 790
	up_write(&dcssblk_devices_sem);

791 792 793
	device_unregister(&dev_info->dev);
	put_device(&dev_info->dev);

L
Linus Torvalds 已提交
794 795 796 797 798 799 800
	rc = count;
out_buf:
	kfree(local_buf);
	return rc;
}

static int
A
Al Viro 已提交
801
dcssblk_open(struct block_device *bdev, fmode_t mode)
L
Linus Torvalds 已提交
802 803 804 805
{
	struct dcssblk_dev_info *dev_info;
	int rc;

A
Al Viro 已提交
806
	dev_info = bdev->bd_disk->private_data;
L
Linus Torvalds 已提交
807 808 809 810 811
	if (NULL == dev_info) {
		rc = -ENODEV;
		goto out;
	}
	atomic_inc(&dev_info->use_count);
A
Al Viro 已提交
812
	bdev->bd_block_size = 4096;
L
Linus Torvalds 已提交
813 814 815 816 817
	rc = 0;
out:
	return rc;
}

818
static void
A
Al Viro 已提交
819
dcssblk_release(struct gendisk *disk, fmode_t mode)
L
Linus Torvalds 已提交
820
{
A
Al Viro 已提交
821
	struct dcssblk_dev_info *dev_info = disk->private_data;
822
	struct segment_info *entry;
L
Linus Torvalds 已提交
823

A
Al Viro 已提交
824
	if (!dev_info) {
825 826
		WARN_ON(1);
		return;
L
Linus Torvalds 已提交
827 828 829 830
	}
	down_write(&dcssblk_devices_sem);
	if (atomic_dec_and_test(&dev_info->use_count)
	    && (dev_info->save_pending)) {
831 832
		pr_info("Device %s has become idle and is being saved "
			"now\n", dev_info->segment_name);
833
		list_for_each_entry(entry, &dev_info->seg_list, lh) {
834 835 836 837 838 839
			if (entry->segment_type == SEG_TYPE_EN ||
			    entry->segment_type == SEG_TYPE_SN)
				pr_warn("DCSS %s is of type SN or EN and cannot"
					" be saved\n", entry->segment_name);
			else
				segment_save(entry->segment_name);
840
		}
L
Linus Torvalds 已提交
841 842 843 844 845
		dev_info->save_pending = 0;
	}
	up_write(&dcssblk_devices_sem);
}

846
static blk_qc_t
847
dcssblk_make_request(struct request_queue *q, struct bio *bio)
L
Linus Torvalds 已提交
848 849
{
	struct dcssblk_dev_info *dev_info;
850 851
	struct bio_vec bvec;
	struct bvec_iter iter;
L
Linus Torvalds 已提交
852 853 854 855 856
	unsigned long index;
	unsigned long page_addr;
	unsigned long source_addr;
	unsigned long bytes_done;

857
	blk_queue_split(q, &bio);
858

L
Linus Torvalds 已提交
859
	bytes_done = 0;
860
	dev_info = bio->bi_disk->private_data;
L
Linus Torvalds 已提交
861 862
	if (dev_info == NULL)
		goto fail;
863 864
	if ((bio->bi_iter.bi_sector & 7) != 0 ||
	    (bio->bi_iter.bi_size & 4095) != 0)
L
Linus Torvalds 已提交
865 866
		/* Request is not page-aligned. */
		goto fail;
867
	if (bio_end_sector(bio) > get_capacity(bio->bi_disk)) {
L
Linus Torvalds 已提交
868 869 870
		/* Request beyond end of DCSS segment. */
		goto fail;
	}
871 872 873 874 875 876 877 878
	/* verify data transfer direction */
	if (dev_info->is_shared) {
		switch (dev_info->segment_type) {
		case SEG_TYPE_SR:
		case SEG_TYPE_ER:
		case SEG_TYPE_SC:
			/* cannot write to these segments */
			if (bio_data_dir(bio) == WRITE) {
879 880
				pr_warn("Writing to %s failed because it is a read-only device\n",
					dev_name(&dev_info->dev));
881 882 883 884 885
				goto fail;
			}
		}
	}

886
	index = (bio->bi_iter.bi_sector >> 3);
887
	bio_for_each_segment(bvec, bio, iter) {
L
Linus Torvalds 已提交
888
		page_addr = (unsigned long)
889
			page_address(bvec.bv_page) + bvec.bv_offset;
L
Linus Torvalds 已提交
890
		source_addr = dev_info->start + (index<<12) + bytes_done;
891
		if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
L
Linus Torvalds 已提交
892 893 894 895
			// More paranoia.
			goto fail;
		if (bio_data_dir(bio) == READ) {
			memcpy((void*)page_addr, (void*)source_addr,
896
				bvec.bv_len);
L
Linus Torvalds 已提交
897 898
		} else {
			memcpy((void*)source_addr, (void*)page_addr,
899
				bvec.bv_len);
L
Linus Torvalds 已提交
900
		}
901
		bytes_done += bvec.bv_len;
L
Linus Torvalds 已提交
902
	}
903
	bio_endio(bio);
904
	return BLK_QC_T_NONE;
L
Linus Torvalds 已提交
905
fail:
906
	bio_io_error(bio);
907
	return BLK_QC_T_NONE;
908 909
}

910
static long
911 912 913 914 915 916 917 918
__dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
		long nr_pages, void **kaddr, pfn_t *pfn)
{
	resource_size_t offset = pgoff * PAGE_SIZE;
	unsigned long dev_sz;

	dev_sz = dev_info->end - dev_info->start + 1;
	*kaddr = (void *) dev_info->start + offset;
919 920
	*pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
			PFN_DEV|PFN_SPECIAL);
921 922 923 924 925 926 927 928 929

	return (dev_sz - offset) / PAGE_SIZE;
}

static long
dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
		long nr_pages, void **kaddr, pfn_t *pfn)
{
	struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
930

931
	return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
L
Linus Torvalds 已提交
932 933 934 935 936 937
}

static void
dcssblk_check_params(void)
{
	int rc, i, j, k;
938
	char buf[DCSSBLK_PARM_LEN + 1];
L
Linus Torvalds 已提交
939 940 941 942
	struct dcssblk_dev_info *dev_info;

	for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
	     i++) {
943 944
		for (j = i; (j < DCSSBLK_PARM_LEN) &&
			    (dcssblk_segments[j] != ',')  &&
L
Linus Torvalds 已提交
945
			    (dcssblk_segments[j] != '\0') &&
946
			    (dcssblk_segments[j] != '('); j++)
L
Linus Torvalds 已提交
947 948 949 950
		{
			buf[j-i] = dcssblk_segments[j];
		}
		buf[j-i] = '\0';
951
		rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
L
Linus Torvalds 已提交
952
		if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
953
			for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
L
Linus Torvalds 已提交
954
				buf[k] = toupper(buf[k]);
955
			buf[k] = '\0';
L
Linus Torvalds 已提交
956 957 958 959 960 961
			if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
				down_read(&dcssblk_devices_sem);
				dev_info = dcssblk_get_device_by_name(buf);
				up_read(&dcssblk_devices_sem);
				if (dev_info)
					dcssblk_shared_store(&dev_info->dev,
962
							     NULL, "0\n", 2);
L
Linus Torvalds 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975
			}
		}
		while ((dcssblk_segments[j] != ',') &&
		       (dcssblk_segments[j] != '\0'))
		{
			j++;
		}
		if (dcssblk_segments[j] == '\0')
			break;
		i = j;
	}
}

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
/*
 * Suspend / Resume
 */
static int dcssblk_freeze(struct device *dev)
{
	struct dcssblk_dev_info *dev_info;
	int rc = 0;

	list_for_each_entry(dev_info, &dcssblk_devices, lh) {
		switch (dev_info->segment_type) {
			case SEG_TYPE_SR:
			case SEG_TYPE_ER:
			case SEG_TYPE_SC:
				if (!dev_info->is_shared)
					rc = -EINVAL;
				break;
			default:
				rc = -EINVAL;
				break;
		}
		if (rc)
			break;
	}
	if (rc)
1000 1001
		pr_err("Suspending the system failed because DCSS device %s "
		       "is writable\n",
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
		       dev_info->segment_name);
	return rc;
}

static int dcssblk_restore(struct device *dev)
{
	struct dcssblk_dev_info *dev_info;
	struct segment_info *entry;
	unsigned long start, end;
	int rc = 0;

	list_for_each_entry(dev_info, &dcssblk_devices, lh) {
		list_for_each_entry(entry, &dev_info->seg_list, lh) {
			segment_unload(entry->segment_name);
			rc = segment_load(entry->segment_name, SEGMENT_SHARED,
					  &start, &end);
			if (rc < 0) {
// TODO in_use check ?
				segment_warning(rc, entry->segment_name);
				goto out_panic;
			}
			if (start != entry->start || end != entry->end) {
1024 1025
				pr_err("The address range of DCSS %s changed "
				       "while the system was suspended\n",
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
				       entry->segment_name);
				goto out_panic;
			}
		}
	}
	return 0;
out_panic:
	panic("fatal dcssblk resume error\n");
}

static int dcssblk_thaw(struct device *dev)
{
	return 0;
}

1041
static const struct dev_pm_ops dcssblk_pm_ops = {
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	.freeze		= dcssblk_freeze,
	.thaw		= dcssblk_thaw,
	.restore	= dcssblk_restore,
};

static struct platform_driver dcssblk_pdrv = {
	.driver = {
		.name	= "dcssblk",
		.pm	= &dcssblk_pm_ops,
	},
};

static struct platform_device *dcssblk_pdev;


L
Linus Torvalds 已提交
1057 1058 1059 1060 1061 1062
/*
 * The init/exit functions.
 */
static void __exit
dcssblk_exit(void)
{
1063 1064
	platform_device_unregister(dcssblk_pdev);
	platform_driver_unregister(&dcssblk_pdrv);
M
Mark McLoughlin 已提交
1065
	root_device_unregister(dcssblk_root_dev);
1066
	unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073
}

static int __init
dcssblk_init(void)
{
	int rc;

1074 1075
	rc = platform_driver_register(&dcssblk_pdrv);
	if (rc)
L
Linus Torvalds 已提交
1076
		return rc;
1077 1078 1079 1080 1081 1082

	dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
							0);
	if (IS_ERR(dcssblk_pdev)) {
		rc = PTR_ERR(dcssblk_pdev);
		goto out_pdrv;
L
Linus Torvalds 已提交
1083
	}
1084 1085 1086 1087 1088

	dcssblk_root_dev = root_device_register("dcssblk");
	if (IS_ERR(dcssblk_root_dev)) {
		rc = PTR_ERR(dcssblk_root_dev);
		goto out_pdev;
L
Linus Torvalds 已提交
1089
	}
1090 1091 1092 1093 1094 1095
	rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
	if (rc)
		goto out_root;
	rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
	if (rc)
		goto out_root;
L
Linus Torvalds 已提交
1096
	rc = register_blkdev(0, DCSSBLK_NAME);
1097 1098
	if (rc < 0)
		goto out_root;
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103
	dcssblk_major = rc;
	init_rwsem(&dcssblk_devices_sem);

	dcssblk_check_params();
	return 0;
1104 1105 1106 1107 1108 1109 1110 1111

out_root:
	root_device_unregister(dcssblk_root_dev);
out_pdev:
	platform_device_unregister(dcssblk_pdev);
out_pdrv:
	platform_driver_unregister(&dcssblk_pdrv);
	return rc;
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118
}

module_init(dcssblk_init);
module_exit(dcssblk_exit);

module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
1119 1120 1121 1122 1123 1124 1125
		 "comma-separated list, names in each set separated "
		 "by commas are separated by colons, each set contains "
		 "names of contiguous segments and each name max. 8 chars.\n"
		 "Adding \"(local)\" to the end of each set equals echoing 0 "
		 "to /sys/devices/dcssblk/<device name>/shared after loading "
		 "the contiguous segments - \n"
		 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
L
Linus Torvalds 已提交
1126 1127

MODULE_LICENSE("GPL");