tape_core.c 34.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 *  drivers/s390/char/tape_core.c
 *    basic function of the tape device driver
 *
 *  S390 and zSeries version
6
 *    Copyright IBM Corp. 2001, 2009
L
Linus Torvalds 已提交
7 8 9 10
 *    Author(s): Carsten Otte <cotte@de.ibm.com>
 *		 Michael Holzheu <holzheu@de.ibm.com>
 *		 Tuan Ngo-Anh <ngoanh@de.ibm.com>
 *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
S
Stefan Bader 已提交
11
 *		 Stefan Bader <shbader@de.ibm.com>
L
Linus Torvalds 已提交
12 13
 */

C
Carsten Otte 已提交
14
#define KMSG_COMPONENT "tape"
15 16
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
17 18 19 20 21 22
#include <linux/module.h>
#include <linux/init.h>	     // for kernel parameters
#include <linux/kmod.h>	     // for requesting modules
#include <linux/spinlock.h>  // for locks
#include <linux/vmalloc.h>
#include <linux/list.h>
23
#include <linux/slab.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31

#include <asm/types.h>	     // for variable types

#define TAPE_DBF_AREA	tape_core_dbf

#include "tape.h"
#include "tape_std.h"

32
#define LONG_BUSY_TIMEOUT 180 /* seconds */
L
Linus Torvalds 已提交
33 34

static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
M
Martin Schwidefsky 已提交
35
static void tape_delayed_next_request(struct work_struct *);
36
static void tape_long_busy_timeout(unsigned long data);
L
Linus Torvalds 已提交
37 38 39 40 41 42

/*
 * One list to contain all tape devices of all disciplines, so
 * we can assign the devices to minor numbers of the same major
 * The list is protected by the rwlock
 */
43
static LIST_HEAD(tape_device_list);
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static DEFINE_RWLOCK(tape_device_lock);

/*
 * Pointer to debug area.
 */
debug_info_t *TAPE_DBF_AREA = NULL;
EXPORT_SYMBOL(TAPE_DBF_AREA);

/*
 * Printable strings for tape enumerations.
 */
const char *tape_state_verbose[TS_SIZE] =
{
	[TS_UNUSED]   = "UNUSED",
	[TS_IN_USE]   = "IN_USE",
	[TS_BLKUSE]   = "BLKUSE",
	[TS_INIT]     = "INIT  ",
	[TS_NOT_OPER] = "NOT_OP"
};

const char *tape_op_verbose[TO_SIZE] =
{
	[TO_BLOCK] = "BLK",	[TO_BSB] = "BSB",
	[TO_BSF] = "BSF",	[TO_DSE] = "DSE",
	[TO_FSB] = "FSB",	[TO_FSF] = "FSF",
	[TO_LBL] = "LBL",	[TO_NOP] = "NOP",
	[TO_RBA] = "RBA",	[TO_RBI] = "RBI",
	[TO_RFO] = "RFO",	[TO_REW] = "REW",
	[TO_RUN] = "RUN",	[TO_WRI] = "WRI",
	[TO_WTM] = "WTM",	[TO_MSEN] = "MSN",
	[TO_LOAD] = "LOA",	[TO_READ_CONFIG] = "RCF",
	[TO_READ_ATTMSG] = "RAT",
	[TO_DIS] = "DIS",	[TO_ASSIGN] = "ASS",
77 78
	[TO_UNASSIGN] = "UAS",  [TO_CRYPT_ON] = "CON",
	[TO_CRYPT_OFF] = "COF",	[TO_KEKL_SET] = "KLS",
79
	[TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
L
Linus Torvalds 已提交
80 81
};

82
static int devid_to_int(struct ccw_dev_id *dev_id)
L
Linus Torvalds 已提交
83
{
84
	return dev_id->devno + (dev_id->ssid << 16);
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93
}

/*
 * Some channel attached tape specific attributes.
 *
 * FIXME: In the future the first_minor and blocksize attribute should be
 *        replaced by a link to the cdev tree.
 */
static ssize_t
94
tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
95 96 97
{
	struct tape_device *tdev;

98
	tdev = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
99 100 101 102 103 104 105
	return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
}

static
DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);

static ssize_t
106
tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
107 108 109
{
	struct tape_device *tdev;

110
	tdev = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
111 112 113 114 115 116 117
	return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
}

static
DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);

static ssize_t
118
tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
119 120 121
{
	struct tape_device *tdev;

122
	tdev = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130
	return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
		"OFFLINE" : tape_state_verbose[tdev->tape_state]);
}

static
DEVICE_ATTR(state, 0444, tape_state_show, NULL);

static ssize_t
131
tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
132 133 134 135
{
	struct tape_device *tdev;
	ssize_t rc;

136
	tdev = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	if (tdev->first_minor < 0)
		return scnprintf(buf, PAGE_SIZE, "N/A\n");

	spin_lock_irq(get_ccwdev_lock(tdev->cdev));
	if (list_empty(&tdev->req_queue))
		rc = scnprintf(buf, PAGE_SIZE, "---\n");
	else {
		struct tape_request *req;

		req = list_entry(tdev->req_queue.next, struct tape_request,
			list);
		rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
	}
	spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
	return rc;
}

static
DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);

static ssize_t
158
tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
159 160 161
{
	struct tape_device *tdev;

162
	tdev = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
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

	return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
}

static
DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);

static struct attribute *tape_attrs[] = {
	&dev_attr_medium_state.attr,
	&dev_attr_first_minor.attr,
	&dev_attr_state.attr,
	&dev_attr_operation.attr,
	&dev_attr_blocksize.attr,
	NULL
};

static struct attribute_group tape_attr_group = {
	.attrs = tape_attrs,
};

/*
 * Tape state functions
 */
void
tape_state_set(struct tape_device *device, enum tape_state newstate)
{
	const char *str;

	if (device->tape_state == TS_NOT_OPER) {
		DBF_EVENT(3, "ts_set err: not oper\n");
		return;
	}
	DBF_EVENT(4, "ts. dev:	%x\n", device->first_minor);
196 197
	DBF_EVENT(4, "old ts:\t\n");
	if (device->tape_state < TS_SIZE && device->tape_state >=0 )
L
Linus Torvalds 已提交
198 199 200 201 202
		str = tape_state_verbose[device->tape_state];
	else
		str = "UNKNOWN TS";
	DBF_EVENT(4, "%s\n", str);
	DBF_EVENT(4, "new ts:\t\n");
203
	if (newstate < TS_SIZE && newstate >= 0)
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211
		str = tape_state_verbose[newstate];
	else
		str = "UNKNOWN TS";
	DBF_EVENT(4, "%s\n", str);
	device->tape_state = newstate;
	wake_up(&device->state_change_wq);
}

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 260 261
struct tape_med_state_work_data {
	struct tape_device *device;
	enum tape_medium_state state;
	struct work_struct  work;
};

static void
tape_med_state_work_handler(struct work_struct *work)
{
	static char env_state_loaded[] = "MEDIUM_STATE=LOADED";
	static char env_state_unloaded[] = "MEDIUM_STATE=UNLOADED";
	struct tape_med_state_work_data *p =
		container_of(work, struct tape_med_state_work_data, work);
	struct tape_device *device = p->device;
	char *envp[] = { NULL, NULL };

	switch (p->state) {
	case MS_UNLOADED:
		pr_info("%s: The tape cartridge has been successfully "
			"unloaded\n", dev_name(&device->cdev->dev));
		envp[0] = env_state_unloaded;
		kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
		break;
	case MS_LOADED:
		pr_info("%s: A tape cartridge has been mounted\n",
			dev_name(&device->cdev->dev));
		envp[0] = env_state_loaded;
		kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
		break;
	default:
		break;
	}
	tape_put_device(device);
	kfree(p);
}

static void
tape_med_state_work(struct tape_device *device, enum tape_medium_state state)
{
	struct tape_med_state_work_data *p;

	p = kzalloc(sizeof(*p), GFP_ATOMIC);
	if (p) {
		INIT_WORK(&p->work, tape_med_state_work_handler);
		p->device = tape_get_device(device);
		p->state = state;
		schedule_work(&p->work);
	}
}

L
Linus Torvalds 已提交
262 263 264
void
tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
{
265 266 267 268
	enum tape_medium_state oldstate;

	oldstate = device->medium_state;
	if (oldstate == newstate)
L
Linus Torvalds 已提交
269
		return;
270
	device->medium_state = newstate;
L
Linus Torvalds 已提交
271 272 273
	switch(newstate){
	case MS_UNLOADED:
		device->tape_generic_status |= GMT_DR_OPEN(~0);
274 275
		if (oldstate == MS_LOADED)
			tape_med_state_work(device, MS_UNLOADED);
L
Linus Torvalds 已提交
276 277 278
		break;
	case MS_LOADED:
		device->tape_generic_status &= ~GMT_DR_OPEN(~0);
279 280
		if (oldstate == MS_UNLOADED)
			tape_med_state_work(device, MS_LOADED);
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288 289 290
		break;
	default:
		break;
	}
	wake_up(&device->state_change_wq);
}

/*
 * Stop running ccw. Has to be called with the device lock held.
 */
291
static int
S
Stefan Bader 已提交
292
__tape_cancel_io(struct tape_device *device, struct tape_request *request)
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300 301 302 303 304
{
	int retries;
	int rc;

	/* Check if interrupt has already been processed */
	if (request->callback == NULL)
		return 0;

	rc = 0;
	for (retries = 0; retries < 5; retries++) {
		rc = ccw_device_clear(device->cdev, (long) request);

S
Stefan Bader 已提交
305 306 307 308 309 310
		switch (rc) {
			case 0:
				request->status	= TAPE_REQUEST_DONE;
				return 0;
			case -EBUSY:
				request->status	= TAPE_REQUEST_CANCEL;
M
Martin Schwidefsky 已提交
311
				schedule_delayed_work(&device->tape_dnr, 0);
S
Stefan Bader 已提交
312 313 314 315 316 317 318 319 320
				return 0;
			case -ENODEV:
				DBF_EXCEPTION(2, "device gone, retry\n");
				break;
			case -EIO:
				DBF_EXCEPTION(2, "I/O error, retry\n");
				break;
			default:
				BUG();
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
		}
	}

	return rc;
}

/*
 * Add device into the sorted list, giving it the first
 * available minor number.
 */
static int
tape_assign_minor(struct tape_device *device)
{
	struct tape_device *tmp;
	int minor;

	minor = 0;
	write_lock(&tape_device_lock);
	list_for_each_entry(tmp, &tape_device_list, node) {
		if (minor < tmp->first_minor)
			break;
		minor += TAPE_MINORS_PER_DEV;
	}
	if (minor >= 256) {
		write_unlock(&tape_device_lock);
		return -ENODEV;
	}
	device->first_minor = minor;
	list_add_tail(&device->node, &tmp->node);
	write_unlock(&tape_device_lock);
	return 0;
}

/* remove device from the list */
static void
tape_remove_minor(struct tape_device *device)
{
	write_lock(&tape_device_lock);
	list_del_init(&device->node);
	device->first_minor = -1;
	write_unlock(&tape_device_lock);
}

/*
 * Set a device online.
 *
 * This function is called by the common I/O layer to move a device from the
 * detected but offline into the online state.
 * If we return an error (RC < 0) the device remains in the offline state. This
 * can happen if the device is assigned somewhere else, for example.
 */
int
tape_generic_online(struct tape_device *device,
		   struct tape_discipline *discipline)
{
	int rc;

	DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);

	if (device->tape_state != TS_INIT) {
		DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
		return -EINVAL;
	}

385 386 387
	init_timer(&device->lb_timeout);
	device->lb_timeout.function = tape_long_busy_timeout;

L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	/* Let the discipline have a go at the device. */
	device->discipline = discipline;
	if (!try_module_get(discipline->owner)) {
		return -EINVAL;
	}

	rc = discipline->setup_device(device);
	if (rc)
		goto out;
	rc = tape_assign_minor(device);
	if (rc)
		goto out_discipline;

	rc = tapechar_setup_device(device);
	if (rc)
		goto out_minor;
	rc = tapeblock_setup_device(device);
	if (rc)
		goto out_char;

	tape_state_set(device, TS_UNUSED);

	DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);

	return 0;

out_char:
	tapechar_cleanup_device(device);
416 417
out_minor:
	tape_remove_minor(device);
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425
out_discipline:
	device->discipline->cleanup_device(device);
	device->discipline = NULL;
out:
	module_put(discipline->owner);
	return rc;
}

426
static void
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436
tape_cleanup_device(struct tape_device *device)
{
	tapeblock_cleanup_device(device);
	tapechar_cleanup_device(device);
	device->discipline->cleanup_device(device);
	module_put(device->discipline->owner);
	tape_remove_minor(device);
	tape_med_state_set(device, MS_UNKNOWN);
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/*
 * Suspend device.
 *
 * Called by the common I/O layer if the drive should be suspended on user
 * request. We refuse to suspend if the device is loaded or in use for the
 * following reason:
 * While the Linux guest is suspended, it might be logged off which causes
 * devices to be detached. Tape devices are automatically rewound and unloaded
 * during DETACH processing (unless the tape device was attached with the
 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
 * resume the original state of the tape device, since we would need to
 * manually re-load the cartridge which was active at suspend time.
 */
int tape_generic_pm_suspend(struct ccw_device *cdev)
{
	struct tape_device *device;

M
Martin Schwidefsky 已提交
454
	device = dev_get_drvdata(&cdev->dev);
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	if (!device) {
		return -ENODEV;
	}

	DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
		device->cdev_id, device);

	if (device->medium_state != MS_UNLOADED) {
		pr_err("A cartridge is loaded in tape device %s, "
		       "refusing to suspend\n", dev_name(&cdev->dev));
		return -EBUSY;
	}

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	switch (device->tape_state) {
		case TS_INIT:
		case TS_NOT_OPER:
		case TS_UNUSED:
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			break;
		default:
			pr_err("Tape device %s is busy, refusing to "
			       "suspend\n", dev_name(&cdev->dev));
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			return -EBUSY;
	}

	DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
	return 0;
}

L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493
/*
 * Set device offline.
 *
 * Called by the common I/O layer if the drive should set offline on user
 * request. We may prevent this by returning an error.
 * Manual offline is only allowed while the drive is not in use.
 */
int
494
tape_generic_offline(struct ccw_device *cdev)
L
Linus Torvalds 已提交
495
{
496 497
	struct tape_device *device;

498
	device = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	if (!device) {
		return -ENODEV;
	}

	DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
		device->cdev_id, device);

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	switch (device->tape_state) {
		case TS_INIT:
		case TS_NOT_OPER:
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			break;
		case TS_UNUSED:
			tape_state_set(device, TS_INIT);
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			tape_cleanup_device(device);
			break;
		default:
			DBF_EVENT(3, "(%08x): Set offline failed "
				"- drive in use.\n",
				device->cdev_id);
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			return -EBUSY;
	}

	DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
	return 0;
}

/*
 * Allocate memory for a new device structure.
 */
static struct tape_device *
tape_alloc_device(void)
{
	struct tape_device *device;

537
	device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
L
Linus Torvalds 已提交
538 539 540 541
	if (device == NULL) {
		DBF_EXCEPTION(2, "ti:no mem\n");
		return ERR_PTR(-ENOMEM);
	}
542
	device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
543 544 545 546 547
	if (device->modeset_byte == NULL) {
		DBF_EXCEPTION(2, "ti:no mem\n");
		kfree(device);
		return ERR_PTR(-ENOMEM);
	}
548
	mutex_init(&device->mutex);
L
Linus Torvalds 已提交
549 550 551
	INIT_LIST_HEAD(&device->req_queue);
	INIT_LIST_HEAD(&device->node);
	init_waitqueue_head(&device->state_change_wq);
552
	init_waitqueue_head(&device->wait_queue);
L
Linus Torvalds 已提交
553 554 555 556 557
	device->tape_state = TS_INIT;
	device->medium_state = MS_UNKNOWN;
	*device->modeset_byte = 0;
	device->first_minor = -1;
	atomic_set(&device->ref_count, 1);
M
Martin Schwidefsky 已提交
558
	INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
L
Linus Torvalds 已提交
559 560 561 562 563 564 565 566 567

	return device;
}

/*
 * Get a reference to an existing device structure. This will automatically
 * increment the reference count.
 */
struct tape_device *
568
tape_get_device(struct tape_device *device)
L
Linus Torvalds 已提交
569
{
570
	int count;
L
Linus Torvalds 已提交
571

572 573
	count = atomic_inc_return(&device->ref_count);
	DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582
	return device;
}

/*
 * Decrease the reference counter of a devices structure. If the
 * reference counter reaches zero free the device structure.
 * The function returns a NULL pointer to be used by the caller
 * for clearing reference pointers.
 */
583
void
L
Linus Torvalds 已提交
584 585
tape_put_device(struct tape_device *device)
{
586
	int count;
L
Linus Torvalds 已提交
587

588 589 590 591 592 593
	count = atomic_dec_return(&device->ref_count);
	DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
	BUG_ON(count < 0);
	if (count == 0) {
		kfree(device->modeset_byte);
		kfree(device);
L
Linus Torvalds 已提交
594 595 596 597 598 599 600
	}
}

/*
 * Find tape device by a device index.
 */
struct tape_device *
601
tape_find_device(int devindex)
L
Linus Torvalds 已提交
602 603 604 605 606 607 608
{
	struct tape_device *device, *tmp;

	device = ERR_PTR(-ENODEV);
	read_lock(&tape_device_lock);
	list_for_each_entry(tmp, &tape_device_list, node) {
		if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
609
			device = tape_get_device(tmp);
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623
			break;
		}
	}
	read_unlock(&tape_device_lock);
	return device;
}

/*
 * Driverfs tape probe function.
 */
int
tape_generic_probe(struct ccw_device *cdev)
{
	struct tape_device *device;
624
	int ret;
625
	struct ccw_dev_id dev_id;
L
Linus Torvalds 已提交
626 627 628 629

	device = tape_alloc_device();
	if (IS_ERR(device))
		return -ENODEV;
630 631
	ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
				     CCWDEV_DO_MULTIPATH);
632 633 634 635 636
	ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
	if (ret) {
		tape_put_device(device);
		return ret;
	}
637
	dev_set_drvdata(&cdev->dev, device);
638
	cdev->handler = __tape_do_irq;
L
Linus Torvalds 已提交
639
	device->cdev = cdev;
640 641
	ccw_device_get_id(cdev, &dev_id);
	device->cdev_id = devid_to_int(&dev_id);
642
	return ret;
L
Linus Torvalds 已提交
643 644
}

645
static void
L
Linus Torvalds 已提交
646 647 648 649 650 651 652 653 654 655 656 657
__tape_discard_requests(struct tape_device *device)
{
	struct tape_request *	request;
	struct list_head *	l, *n;

	list_for_each_safe(l, n, &device->req_queue) {
		request = list_entry(l, struct tape_request, list);
		if (request->status == TAPE_REQUEST_IN_IO)
			request->status = TAPE_REQUEST_DONE;
		list_del(&request->list);

		/* Decrease ref_count for removed request. */
658 659
		request->device = NULL;
		tape_put_device(device);
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
		request->rc = -EIO;
		if (request->callback != NULL)
			request->callback(request, request->callback_data);
	}
}

/*
 * Driverfs tape remove function.
 *
 * This function is called whenever the common I/O layer detects the device
 * gone. This can happen at any time and we cannot refuse.
 */
void
tape_generic_remove(struct ccw_device *cdev)
{
	struct tape_device *	device;

677
	device = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
	if (!device) {
		return;
	}
	DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	switch (device->tape_state) {
		case TS_INIT:
			tape_state_set(device, TS_NOT_OPER);
		case TS_NOT_OPER:
			/*
			 * Nothing to do.
			 */
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			break;
		case TS_UNUSED:
			/*
			 * Need only to release the device.
			 */
			tape_state_set(device, TS_NOT_OPER);
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			tape_cleanup_device(device);
			break;
		default:
			/*
			 * There may be requests on the queue. We will not get
			 * an interrupt for a request that was running. So we
			 * just post them all as I/O errors.
			 */
			DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
				device->cdev_id);
709 710
			pr_warning("%s: A tape unit was detached while in "
				   "use\n", dev_name(&device->cdev->dev));
L
Linus Torvalds 已提交
711 712 713 714 715 716
			tape_state_set(device, TS_NOT_OPER);
			__tape_discard_requests(device);
			spin_unlock_irq(get_ccwdev_lock(device->cdev));
			tape_cleanup_device(device);
	}

717 718
	device = dev_get_drvdata(&cdev->dev);
	if (device) {
L
Linus Torvalds 已提交
719
		sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
720 721
		dev_set_drvdata(&cdev->dev, NULL);
		tape_put_device(device);
L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729 730 731 732
	}
}

/*
 * Allocate a new tape ccw request
 */
struct tape_request *
tape_alloc_request(int cplength, int datasize)
{
	struct tape_request *request;

S
Stoyan Gaydarov 已提交
733
	BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
L
Linus Torvalds 已提交
734 735 736

	DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);

737
	request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
L
Linus Torvalds 已提交
738 739 740 741 742 743
	if (request == NULL) {
		DBF_EXCEPTION(1, "cqra nomem\n");
		return ERR_PTR(-ENOMEM);
	}
	/* allocate channel program */
	if (cplength > 0) {
744
		request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753
					  GFP_ATOMIC | GFP_DMA);
		if (request->cpaddr == NULL) {
			DBF_EXCEPTION(1, "cqra nomem\n");
			kfree(request);
			return ERR_PTR(-ENOMEM);
		}
	}
	/* alloc small kernel buffer */
	if (datasize > 0) {
754
		request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
755 756
		if (request->cpdata == NULL) {
			DBF_EXCEPTION(1, "cqra nomem\n");
J
Jesper Juhl 已提交
757
			kfree(request->cpaddr);
L
Linus Torvalds 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
			kfree(request);
			return ERR_PTR(-ENOMEM);
		}
	}
	DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
		request->cpdata);

	return request;
}

/*
 * Free tape ccw request
 */
void
tape_free_request (struct tape_request * request)
{
	DBF_LH(6, "Free request %p\n", request);

776 777
	if (request->device)
		tape_put_device(request->device);
J
Jesper Juhl 已提交
778 779
	kfree(request->cpdata);
	kfree(request->cpaddr);
L
Linus Torvalds 已提交
780 781 782
	kfree(request);
}

783
static int
S
Stefan Bader 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
__tape_start_io(struct tape_device *device, struct tape_request *request)
{
	int rc;

#ifdef CONFIG_S390_TAPE_BLOCK
	if (request->op == TO_BLOCK)
		device->discipline->check_locate(device, request);
#endif
	rc = ccw_device_start(
		device->cdev,
		request->cpaddr,
		(unsigned long) request,
		0x00,
		request->options
	);
	if (rc == 0) {
		request->status = TAPE_REQUEST_IN_IO;
	} else if (rc == -EBUSY) {
		/* The common I/O subsystem is currently busy. Retry later. */
		request->status = TAPE_REQUEST_QUEUED;
M
Martin Schwidefsky 已提交
804
		schedule_delayed_work(&device->tape_dnr, 0);
S
Stefan Bader 已提交
805 806 807 808 809 810 811 812
		rc = 0;
	} else {
		/* Start failed. Remove request and indicate failure. */
		DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
	}
	return rc;
}

813
static void
S
Stefan Bader 已提交
814
__tape_start_next_request(struct tape_device *device)
L
Linus Torvalds 已提交
815 816 817 818 819
{
	struct list_head *l, *n;
	struct tape_request *request;
	int rc;

S
Stefan Bader 已提交
820
	DBF_LH(6, "__tape_start_next_request(%p)\n", device);
L
Linus Torvalds 已提交
821 822 823 824 825 826
	/*
	 * Try to start each request on request queue until one is
	 * started successful.
	 */
	list_for_each_safe(l, n, &device->req_queue) {
		request = list_entry(l, struct tape_request, list);
S
Stefan Bader 已提交
827 828 829 830 831 832 833

		/*
		 * Avoid race condition if bottom-half was triggered more than
		 * once.
		 */
		if (request->status == TAPE_REQUEST_IN_IO)
			return;
834 835 836 837 838 839 840
		/*
		 * Request has already been stopped. We have to wait until
		 * the request is removed from the queue in the interrupt
		 * handling.
		 */
		if (request->status == TAPE_REQUEST_DONE)
			return;
S
Stefan Bader 已提交
841 842 843 844 845 846 847 848 849 850 851

		/*
		 * We wanted to cancel the request but the common I/O layer
		 * was busy at that time. This can only happen if this
		 * function is called by delayed_next_request.
		 * Otherwise we start the next request on the queue.
		 */
		if (request->status == TAPE_REQUEST_CANCEL) {
			rc = __tape_cancel_io(device, request);
		} else {
			rc = __tape_start_io(device, request);
L
Linus Torvalds 已提交
852
		}
S
Stefan Bader 已提交
853 854
		if (rc == 0)
			return;
L
Linus Torvalds 已提交
855

S
Stefan Bader 已提交
856
		/* Set ending status. */
L
Linus Torvalds 已提交
857 858
		request->rc = rc;
		request->status = TAPE_REQUEST_DONE;
S
Stefan Bader 已提交
859 860 861 862 863 864 865

		/* Remove from request queue. */
		list_del(&request->list);

		/* Do callback. */
		if (request->callback != NULL)
			request->callback(request, request->callback_data);
L
Linus Torvalds 已提交
866 867 868 869
	}
}

static void
M
Martin Schwidefsky 已提交
870
tape_delayed_next_request(struct work_struct *work)
L
Linus Torvalds 已提交
871
{
M
Martin Schwidefsky 已提交
872 873
	struct tape_device *device =
		container_of(work, struct tape_device, tape_dnr.work);
L
Linus Torvalds 已提交
874

S
Stefan Bader 已提交
875 876 877 878 879 880
	DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
	spin_lock_irq(get_ccwdev_lock(device->cdev));
	__tape_start_next_request(device);
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
}

881 882 883 884 885 886 887 888
static void tape_long_busy_timeout(unsigned long data)
{
	struct tape_request *request;
	struct tape_device *device;

	device = (struct tape_device *) data;
	spin_lock_irq(get_ccwdev_lock(device->cdev));
	request = list_entry(device->req_queue.next, struct tape_request, list);
S
Stoyan Gaydarov 已提交
889
	BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
890 891
	DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
	__tape_start_next_request(device);
892 893
	device->lb_timeout.data = 0UL;
	tape_put_device(device);
894 895 896
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
}

897
static void
S
Stefan Bader 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
__tape_end_request(
	struct tape_device *	device,
	struct tape_request *	request,
	int			rc)
{
	DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
	if (request) {
		request->rc = rc;
		request->status = TAPE_REQUEST_DONE;

		/* Remove from request queue. */
		list_del(&request->list);

		/* Do callback. */
		if (request->callback != NULL)
			request->callback(request, request->callback_data);
	}
L
Linus Torvalds 已提交
915 916 917

	/* Start next request. */
	if (!list_empty(&device->req_queue))
S
Stefan Bader 已提交
918
		__tape_start_next_request(device);
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
}

/*
 * Write sense data to dbf
 */
void
tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
		    struct irb *irb)
{
	unsigned int *sptr;
	const char* op;

	if (request != NULL)
		op = tape_op_verbose[request->op];
	else
		op = "---";
	DBF_EVENT(3, "DSTAT : %02x   CSTAT: %02x\n",
936
		  irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949
	DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
	sptr = (unsigned int *) irb->ecw;
	DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
	DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
	DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
	DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
}

/*
 * I/O helper function. Adds the request to the request queue
 * and starts it if the tape is idle. Has to be called with
 * the device lock held.
 */
950
static int
S
Stefan Bader 已提交
951
__tape_start_request(struct tape_device *device, struct tape_request *request)
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959
{
	int rc;

	switch (request->op) {
		case TO_MSEN:
		case TO_ASSIGN:
		case TO_UNASSIGN:
		case TO_READ_ATTMSG:
960
		case TO_RDC:
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968 969 970 971 972
			if (device->tape_state == TS_INIT)
				break;
			if (device->tape_state == TS_UNUSED)
				break;
		default:
			if (device->tape_state == TS_BLKUSE)
				break;
			if (device->tape_state != TS_IN_USE)
				return -ENODEV;
	}

	/* Increase use count of device for the added request. */
973
	request->device = tape_get_device(device);
L
Linus Torvalds 已提交
974 975 976

	if (list_empty(&device->req_queue)) {
		/* No other requests are on the queue. Start this one. */
S
Stefan Bader 已提交
977 978
		rc = __tape_start_io(device, request);
		if (rc)
L
Linus Torvalds 已提交
979
			return rc;
S
Stefan Bader 已提交
980

L
Linus Torvalds 已提交
981 982 983 984 985
		DBF_LH(5, "Request %p added for execution.\n", request);
		list_add(&request->list, &device->req_queue);
	} else {
		DBF_LH(5, "Request %p add to queue.\n", request);
		request->status = TAPE_REQUEST_QUEUED;
S
Stefan Bader 已提交
986
		list_add_tail(&request->list, &device->req_queue);
L
Linus Torvalds 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	}
	return 0;
}

/*
 * Add the request to the request queue, try to start it if the
 * tape is idle. Return without waiting for end of i/o.
 */
int
tape_do_io_async(struct tape_device *device, struct tape_request *request)
{
	int rc;

	DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	/* Add request to request queue and try to start it. */
S
Stefan Bader 已提交
1004
	rc = __tape_start_request(device, request);
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
	return rc;
}

/*
 * tape_do_io/__tape_wake_up
 * Add the request to the request queue, try to start it if the
 * tape is idle and wait uninterruptible for its completion.
 */
static void
__tape_wake_up(struct tape_request *request, void *data)
{
	request->callback = NULL;
	wake_up((wait_queue_head_t *) data);
}

int
tape_do_io(struct tape_device *device, struct tape_request *request)
{
	int rc;

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	/* Setup callback */
	request->callback = __tape_wake_up;
1029
	request->callback_data = &device->wait_queue;
L
Linus Torvalds 已提交
1030
	/* Add request to request queue and try to start it. */
S
Stefan Bader 已提交
1031
	rc = __tape_start_request(device, request);
L
Linus Torvalds 已提交
1032 1033 1034 1035
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
	if (rc)
		return rc;
	/* Request added to the queue. Wait for its completion. */
1036
	wait_event(device->wait_queue, (request->callback == NULL));
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	/* Get rc from request */
	return request->rc;
}

/*
 * tape_do_io_interruptible/__tape_wake_up_interruptible
 * Add the request to the request queue, try to start it if the
 * tape is idle and wait uninterruptible for its completion.
 */
static void
__tape_wake_up_interruptible(struct tape_request *request, void *data)
{
	request->callback = NULL;
	wake_up_interruptible((wait_queue_head_t *) data);
}

int
tape_do_io_interruptible(struct tape_device *device,
			 struct tape_request *request)
{
	int rc;

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	/* Setup callback */
	request->callback = __tape_wake_up_interruptible;
1062
	request->callback_data = &device->wait_queue;
S
Stefan Bader 已提交
1063
	rc = __tape_start_request(device, request);
L
Linus Torvalds 已提交
1064 1065 1066 1067
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
	if (rc)
		return rc;
	/* Request added to the queue. Wait for its completion. */
1068 1069
	rc = wait_event_interruptible(device->wait_queue,
				      (request->callback == NULL));
L
Linus Torvalds 已提交
1070 1071 1072
	if (rc != -ERESTARTSYS)
		/* Request finished normally. */
		return request->rc;
S
Stefan Bader 已提交
1073

L
Linus Torvalds 已提交
1074 1075
	/* Interrupted by a signal. We have to stop the current request. */
	spin_lock_irq(get_ccwdev_lock(device->cdev));
S
Stefan Bader 已提交
1076 1077
	rc = __tape_cancel_io(device, request);
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
L
Linus Torvalds 已提交
1078
	if (rc == 0) {
S
Stefan Bader 已提交
1079 1080 1081
		/* Wait for the interrupt that acknowledges the halt. */
		do {
			rc = wait_event_interruptible(
1082
				device->wait_queue,
S
Stefan Bader 已提交
1083 1084
				(request->callback == NULL)
			);
1085
		} while (rc == -ERESTARTSYS);
S
Stefan Bader 已提交
1086

L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092
		DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
		rc = -ERESTARTSYS;
	}
	return rc;
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
/*
 * Stop running ccw.
 */
int
tape_cancel_io(struct tape_device *device, struct tape_request *request)
{
	int rc;

	spin_lock_irq(get_ccwdev_lock(device->cdev));
	rc = __tape_cancel_io(device, request);
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
	return rc;
}

L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
/*
 * Tape interrupt routine, called from the ccw_device layer
 */
static void
__tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
{
	struct tape_device *device;
	struct tape_request *request;
	int rc;

1117
	device = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126
	if (device == NULL) {
		return;
	}
	request = (struct tape_request *) intparm;

	DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);

	/* On special conditions irb is an error pointer */
	if (IS_ERR(irb)) {
S
Stefan Bader 已提交
1127
		/* FIXME: What to do with the request? */
L
Linus Torvalds 已提交
1128 1129
		switch (PTR_ERR(irb)) {
			case -ETIMEDOUT:
S
Sebastian Ott 已提交
1130 1131
				DBF_LH(1, "(%08x): Request timed out\n",
				       device->cdev_id);
L
Linus Torvalds 已提交
1132
			case -EIO:
S
Stefan Bader 已提交
1133
				__tape_end_request(device, request, -EIO);
L
Linus Torvalds 已提交
1134 1135
				break;
			default:
S
Sebastian Ott 已提交
1136 1137
				DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
				       device->cdev_id,	PTR_ERR(irb));
L
Linus Torvalds 已提交
1138 1139 1140 1141
		}
		return;
	}

S
Stefan Bader 已提交
1142 1143 1144
	/*
	 * If the condition code is not zero and the start function bit is
	 * still set, this is an deferred error and the last start I/O did
1145 1146 1147
	 * not succeed. At this point the condition that caused the deferred
	 * error might still apply. So we just schedule the request to be
	 * started later.
S
Stefan Bader 已提交
1148
	 */
1149 1150
	if (irb->scsw.cmd.cc != 0 &&
	    (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1151 1152
	    (request->status == TAPE_REQUEST_IN_IO)) {
		DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1153
			device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
1154
		request->status = TAPE_REQUEST_QUEUED;
1155
		schedule_delayed_work(&device->tape_dnr, HZ);
S
Stefan Bader 已提交
1156 1157 1158
		return;
	}

L
Linus Torvalds 已提交
1159 1160
	/* May be an unsolicited irq */
	if(request != NULL)
1161 1162
		request->rescnt = irb->scsw.cmd.count;
	else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
1163 1164 1165 1166 1167 1168 1169 1170
		 !list_empty(&device->req_queue)) {
		/* Not Ready to Ready after long busy ? */
		struct tape_request *req;
		req = list_entry(device->req_queue.next,
				 struct tape_request, list);
		if (req->status == TAPE_REQUEST_LONG_BUSY) {
			DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
			if (del_timer(&device->lb_timeout)) {
1171 1172
				device->lb_timeout.data = 0UL;
				tape_put_device(device);
1173 1174 1175 1176 1177
				__tape_start_next_request(device);
			}
			return;
		}
	}
1178
	if (irb->scsw.cmd.dstat != 0x0c) {
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
		/* Set the 'ONLINE' flag depending on sense byte 1 */
		if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
			device->tape_generic_status |= GMT_ONLINE(~0);
		else
			device->tape_generic_status &= ~GMT_ONLINE(~0);

		/*
		 * Any request that does not come back with channel end
		 * and device end is unusual. Log the sense data.
		 */
		DBF_EVENT(3,"-- Tape Interrupthandler --\n");
		tape_dump_sense_dbf(device, request, irb);
	} else {
		/* Upon normal completion the device _is_ online */
		device->tape_generic_status |= GMT_ONLINE(~0);
	}
	if (device->tape_state == TS_NOT_OPER) {
		DBF_EVENT(6, "tape:device is not operational\n");
		return;
	}

	/*
	 * Request that were canceled still come back with an interrupt.
	 * To detect these request the state will be set to TAPE_REQUEST_DONE.
	 */
	if(request != NULL && request->status == TAPE_REQUEST_DONE) {
S
Stefan Bader 已提交
1205
		__tape_end_request(device, request, -EIO);
L
Linus Torvalds 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
		return;
	}

	rc = device->discipline->irq(device, request, irb);
	/*
	 * rc < 0 : request finished unsuccessfully.
	 * rc == TAPE_IO_SUCCESS: request finished successfully.
	 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
	 * rc == TAPE_IO_RETRY: request finished but needs another go.
	 * rc == TAPE_IO_STOP: request needs to get terminated.
	 */
	switch (rc) {
S
Stefan Bader 已提交
1218 1219 1220 1221 1222 1223 1224
		case TAPE_IO_SUCCESS:
			/* Upon normal completion the device _is_ online */
			device->tape_generic_status |= GMT_ONLINE(~0);
			__tape_end_request(device, request, rc);
			break;
		case TAPE_IO_PENDING:
			break;
1225 1226
		case TAPE_IO_LONG_BUSY:
			device->lb_timeout.data =
1227
				(unsigned long) tape_get_device(device);
1228 1229 1230 1231 1232 1233
			device->lb_timeout.expires = jiffies +
				LONG_BUSY_TIMEOUT * HZ;
			DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
			add_timer(&device->lb_timeout);
			request->status = TAPE_REQUEST_LONG_BUSY;
			break;
S
Stefan Bader 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		case TAPE_IO_RETRY:
			rc = __tape_start_io(device, request);
			if (rc)
				__tape_end_request(device, request, rc);
			break;
		case TAPE_IO_STOP:
			rc = __tape_cancel_io(device, request);
			if (rc)
				__tape_end_request(device, request, rc);
			break;
		default:
			if (rc > 0) {
				DBF_EVENT(6, "xunknownrc\n");
				__tape_end_request(device, request, -EIO);
			} else {
				__tape_end_request(device, request, rc);
			}
			break;
L
Linus Torvalds 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	}
}

/*
 * Tape device open function used by tape_char & tape_block frontends.
 */
int
tape_open(struct tape_device *device)
{
	int rc;

1263
	spin_lock_irq(get_ccwdev_lock(device->cdev));
L
Linus Torvalds 已提交
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	if (device->tape_state == TS_NOT_OPER) {
		DBF_EVENT(6, "TAPE:nodev\n");
		rc = -ENODEV;
	} else if (device->tape_state == TS_IN_USE) {
		DBF_EVENT(6, "TAPE:dbusy\n");
		rc = -EBUSY;
	} else if (device->tape_state == TS_BLKUSE) {
		DBF_EVENT(6, "TAPE:dbusy\n");
		rc = -EBUSY;
	} else if (device->discipline != NULL &&
		   !try_module_get(device->discipline->owner)) {
		DBF_EVENT(6, "TAPE:nodisc\n");
		rc = -ENODEV;
	} else {
		tape_state_set(device, TS_IN_USE);
		rc = 0;
	}
1281
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
L
Linus Torvalds 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290
	return rc;
}

/*
 * Tape device release function used by tape_char & tape_block frontends.
 */
int
tape_release(struct tape_device *device)
{
1291
	spin_lock_irq(get_ccwdev_lock(device->cdev));
L
Linus Torvalds 已提交
1292 1293 1294
	if (device->tape_state == TS_IN_USE)
		tape_state_set(device, TS_UNUSED);
	module_put(device->discipline->owner);
1295
	spin_unlock_irq(get_ccwdev_lock(device->cdev));
L
Linus Torvalds 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
	return 0;
}

/*
 * Execute a magnetic tape command a number of times.
 */
int
tape_mtop(struct tape_device *device, int mt_op, int mt_count)
{
	tape_mtop_fn fn;
	int rc;

	DBF_EVENT(6, "TAPE:mtio\n");
	DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
	DBF_EVENT(6, "TAPE:arg:	 %x\n", mt_count);

	if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
		return -EINVAL;
	fn = device->discipline->mtop_array[mt_op];
	if (fn == NULL)
		return -EINVAL;

	/* We assume that the backends can handle count up to 500. */
	if (mt_op == MTBSR  || mt_op == MTFSR  || mt_op == MTFSF  ||
	    mt_op == MTBSF  || mt_op == MTFSFM || mt_op == MTBSFM) {
		rc = 0;
		for (; mt_count > 500; mt_count -= 500)
			if ((rc = fn(device, 500)) != 0)
				break;
		if (rc == 0)
			rc = fn(device, mt_count);
	} else
		rc = fn(device, mt_count);
	return rc;

}

/*
 * Tape init function.
 */
static int
tape_init (void)
{
1339
	TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
L
Linus Torvalds 已提交
1340 1341 1342 1343
	debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
#ifdef DBF_LIKE_HELL
	debug_set_level(TAPE_DBF_AREA, 6);
#endif
1344
	DBF_EVENT(3, "tape init\n");
L
Linus Torvalds 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
	tape_proc_init();
	tapechar_init ();
	tapeblock_init ();
	return 0;
}

/*
 * Tape exit function.
 */
static void
tape_exit(void)
{
	DBF_EVENT(6, "tape exit\n");

	/* Get rid of the frontends */
	tapechar_exit();
	tapeblock_exit();
	tape_proc_cleanup();
	debug_unregister (TAPE_DBF_AREA);
}

MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
	      "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1368
MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
L
Linus Torvalds 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377
MODULE_LICENSE("GPL");

module_init(tape_init);
module_exit(tape_exit);

EXPORT_SYMBOL(tape_generic_remove);
EXPORT_SYMBOL(tape_generic_probe);
EXPORT_SYMBOL(tape_generic_online);
EXPORT_SYMBOL(tape_generic_offline);
1378
EXPORT_SYMBOL(tape_generic_pm_suspend);
L
Linus Torvalds 已提交
1379
EXPORT_SYMBOL(tape_put_device);
1380
EXPORT_SYMBOL(tape_get_device);
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
EXPORT_SYMBOL(tape_state_verbose);
EXPORT_SYMBOL(tape_op_verbose);
EXPORT_SYMBOL(tape_state_set);
EXPORT_SYMBOL(tape_med_state_set);
EXPORT_SYMBOL(tape_alloc_request);
EXPORT_SYMBOL(tape_free_request);
EXPORT_SYMBOL(tape_dump_sense_dbf);
EXPORT_SYMBOL(tape_do_io);
EXPORT_SYMBOL(tape_do_io_async);
EXPORT_SYMBOL(tape_do_io_interruptible);
1391
EXPORT_SYMBOL(tape_cancel_io);
L
Linus Torvalds 已提交
1392
EXPORT_SYMBOL(tape_mtop);