[S390] .align 4096 statements in head.S
[deliverable/linux.git] / drivers / s390 / char / tape_core.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
4 *
5 * S390 and zSeries version
4111796d 6 * Copyright (C) 2001,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
1da177e4
LT
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
4111796d 11 * Stefan Bader <shbader@de.ibm.com>
1da177e4
LT
12 */
13
1da177e4
LT
14#include <linux/module.h>
15#include <linux/init.h> // for kernel parameters
16#include <linux/kmod.h> // for requesting modules
17#include <linux/spinlock.h> // for locks
18#include <linux/vmalloc.h>
19#include <linux/list.h>
20
21#include <asm/types.h> // for variable types
22
23#define TAPE_DBF_AREA tape_core_dbf
24
25#include "tape.h"
26#include "tape_std.h"
27
28#define PRINTK_HEADER "TAPE_CORE: "
29
30static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
4111796d 31static void tape_delayed_next_request(void * data);
1da177e4
LT
32
33/*
34 * One list to contain all tape devices of all disciplines, so
35 * we can assign the devices to minor numbers of the same major
36 * The list is protected by the rwlock
37 */
38static struct list_head tape_device_list = LIST_HEAD_INIT(tape_device_list);
39static DEFINE_RWLOCK(tape_device_lock);
40
41/*
42 * Pointer to debug area.
43 */
44debug_info_t *TAPE_DBF_AREA = NULL;
45EXPORT_SYMBOL(TAPE_DBF_AREA);
46
47/*
48 * Printable strings for tape enumerations.
49 */
50const char *tape_state_verbose[TS_SIZE] =
51{
52 [TS_UNUSED] = "UNUSED",
53 [TS_IN_USE] = "IN_USE",
54 [TS_BLKUSE] = "BLKUSE",
55 [TS_INIT] = "INIT ",
56 [TS_NOT_OPER] = "NOT_OP"
57};
58
59const char *tape_op_verbose[TO_SIZE] =
60{
61 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
62 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
63 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
64 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
65 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
66 [TO_RFO] = "RFO", [TO_REW] = "REW",
67 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
68 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
69 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
70 [TO_READ_ATTMSG] = "RAT",
71 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
72 [TO_UNASSIGN] = "UAS"
73};
74
75static inline int
76busid_to_int(char *bus_id)
77{
78 int dec;
79 int d;
80 char * s;
81
82 for(s = bus_id, d = 0; *s != '\0' && *s != '.'; s++)
83 d = (d * 10) + (*s - '0');
84 dec = d;
85 for(s++, d = 0; *s != '\0' && *s != '.'; s++)
86 d = (d * 10) + (*s - '0');
87 dec = (dec << 8) + d;
88
89 for(s++; *s != '\0'; s++) {
90 if (*s >= '0' && *s <= '9') {
91 d = *s - '0';
92 } else if (*s >= 'a' && *s <= 'f') {
93 d = *s - 'a' + 10;
94 } else {
95 d = *s - 'A' + 10;
96 }
97 dec = (dec << 4) + d;
98 }
99
100 return dec;
101}
102
103/*
104 * Some channel attached tape specific attributes.
105 *
106 * FIXME: In the future the first_minor and blocksize attribute should be
107 * replaced by a link to the cdev tree.
108 */
109static ssize_t
3fd3c0a5 110tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
111{
112 struct tape_device *tdev;
113
114 tdev = (struct tape_device *) dev->driver_data;
115 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
116}
117
118static
119DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
120
121static ssize_t
3fd3c0a5 122tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
123{
124 struct tape_device *tdev;
125
126 tdev = (struct tape_device *) dev->driver_data;
127 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
128}
129
130static
131DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
132
133static ssize_t
3fd3c0a5 134tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
135{
136 struct tape_device *tdev;
137
138 tdev = (struct tape_device *) dev->driver_data;
139 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
140 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
141}
142
143static
144DEVICE_ATTR(state, 0444, tape_state_show, NULL);
145
146static ssize_t
3fd3c0a5 147tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
148{
149 struct tape_device *tdev;
150 ssize_t rc;
151
152 tdev = (struct tape_device *) dev->driver_data;
153 if (tdev->first_minor < 0)
154 return scnprintf(buf, PAGE_SIZE, "N/A\n");
155
156 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
157 if (list_empty(&tdev->req_queue))
158 rc = scnprintf(buf, PAGE_SIZE, "---\n");
159 else {
160 struct tape_request *req;
161
162 req = list_entry(tdev->req_queue.next, struct tape_request,
163 list);
164 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
165 }
166 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
167 return rc;
168}
169
170static
171DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
172
173static ssize_t
3fd3c0a5 174tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
175{
176 struct tape_device *tdev;
177
178 tdev = (struct tape_device *) dev->driver_data;
179
180 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
181}
182
183static
184DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
185
186static struct attribute *tape_attrs[] = {
187 &dev_attr_medium_state.attr,
188 &dev_attr_first_minor.attr,
189 &dev_attr_state.attr,
190 &dev_attr_operation.attr,
191 &dev_attr_blocksize.attr,
192 NULL
193};
194
195static struct attribute_group tape_attr_group = {
196 .attrs = tape_attrs,
197};
198
199/*
200 * Tape state functions
201 */
202void
203tape_state_set(struct tape_device *device, enum tape_state newstate)
204{
205 const char *str;
206
207 if (device->tape_state == TS_NOT_OPER) {
208 DBF_EVENT(3, "ts_set err: not oper\n");
209 return;
210 }
211 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
f976069a
PO
212 DBF_EVENT(4, "old ts:\t\n");
213 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
1da177e4
LT
214 str = tape_state_verbose[device->tape_state];
215 else
216 str = "UNKNOWN TS";
217 DBF_EVENT(4, "%s\n", str);
218 DBF_EVENT(4, "new ts:\t\n");
f976069a 219 if (newstate < TS_SIZE && newstate >= 0)
1da177e4
LT
220 str = tape_state_verbose[newstate];
221 else
222 str = "UNKNOWN TS";
223 DBF_EVENT(4, "%s\n", str);
224 device->tape_state = newstate;
225 wake_up(&device->state_change_wq);
226}
227
228void
229tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
230{
231 if (device->medium_state == newstate)
232 return;
233 switch(newstate){
234 case MS_UNLOADED:
235 device->tape_generic_status |= GMT_DR_OPEN(~0);
236 PRINT_INFO("(%s): Tape is unloaded\n",
237 device->cdev->dev.bus_id);
238 break;
239 case MS_LOADED:
240 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
241 PRINT_INFO("(%s): Tape has been mounted\n",
242 device->cdev->dev.bus_id);
243 break;
244 default:
245 // print nothing
246 break;
247 }
248 device->medium_state = newstate;
249 wake_up(&device->state_change_wq);
250}
251
252/*
253 * Stop running ccw. Has to be called with the device lock held.
254 */
255static inline int
4111796d 256__tape_cancel_io(struct tape_device *device, struct tape_request *request)
1da177e4
LT
257{
258 int retries;
259 int rc;
260
261 /* Check if interrupt has already been processed */
262 if (request->callback == NULL)
263 return 0;
264
265 rc = 0;
266 for (retries = 0; retries < 5; retries++) {
267 rc = ccw_device_clear(device->cdev, (long) request);
268
4111796d
SB
269 switch (rc) {
270 case 0:
271 request->status = TAPE_REQUEST_DONE;
272 return 0;
273 case -EBUSY:
274 request->status = TAPE_REQUEST_CANCEL;
275 schedule_work(&device->tape_dnr);
276 return 0;
277 case -ENODEV:
278 DBF_EXCEPTION(2, "device gone, retry\n");
279 break;
280 case -EIO:
281 DBF_EXCEPTION(2, "I/O error, retry\n");
282 break;
283 default:
284 BUG();
1da177e4 285 }
1da177e4
LT
286 }
287
288 return rc;
289}
290
291/*
292 * Add device into the sorted list, giving it the first
293 * available minor number.
294 */
295static int
296tape_assign_minor(struct tape_device *device)
297{
298 struct tape_device *tmp;
299 int minor;
300
301 minor = 0;
302 write_lock(&tape_device_lock);
303 list_for_each_entry(tmp, &tape_device_list, node) {
304 if (minor < tmp->first_minor)
305 break;
306 minor += TAPE_MINORS_PER_DEV;
307 }
308 if (minor >= 256) {
309 write_unlock(&tape_device_lock);
310 return -ENODEV;
311 }
312 device->first_minor = minor;
313 list_add_tail(&device->node, &tmp->node);
314 write_unlock(&tape_device_lock);
315 return 0;
316}
317
318/* remove device from the list */
319static void
320tape_remove_minor(struct tape_device *device)
321{
322 write_lock(&tape_device_lock);
323 list_del_init(&device->node);
324 device->first_minor = -1;
325 write_unlock(&tape_device_lock);
326}
327
328/*
329 * Set a device online.
330 *
331 * This function is called by the common I/O layer to move a device from the
332 * detected but offline into the online state.
333 * If we return an error (RC < 0) the device remains in the offline state. This
334 * can happen if the device is assigned somewhere else, for example.
335 */
336int
337tape_generic_online(struct tape_device *device,
338 struct tape_discipline *discipline)
339{
340 int rc;
341
342 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
343
344 if (device->tape_state != TS_INIT) {
345 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
346 return -EINVAL;
347 }
348
349 /* Let the discipline have a go at the device. */
350 device->discipline = discipline;
351 if (!try_module_get(discipline->owner)) {
352 PRINT_ERR("Cannot get module. Module gone.\n");
353 return -EINVAL;
354 }
355
356 rc = discipline->setup_device(device);
357 if (rc)
358 goto out;
359 rc = tape_assign_minor(device);
360 if (rc)
361 goto out_discipline;
362
363 rc = tapechar_setup_device(device);
364 if (rc)
365 goto out_minor;
366 rc = tapeblock_setup_device(device);
367 if (rc)
368 goto out_char;
369
370 tape_state_set(device, TS_UNUSED);
371
372 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
373
374 return 0;
375
376out_char:
377 tapechar_cleanup_device(device);
378out_discipline:
379 device->discipline->cleanup_device(device);
380 device->discipline = NULL;
381out_minor:
382 tape_remove_minor(device);
383out:
384 module_put(discipline->owner);
385 return rc;
386}
387
388static inline void
389tape_cleanup_device(struct tape_device *device)
390{
391 tapeblock_cleanup_device(device);
392 tapechar_cleanup_device(device);
393 device->discipline->cleanup_device(device);
394 module_put(device->discipline->owner);
395 tape_remove_minor(device);
396 tape_med_state_set(device, MS_UNKNOWN);
397}
398
399/*
400 * Set device offline.
401 *
402 * Called by the common I/O layer if the drive should set offline on user
403 * request. We may prevent this by returning an error.
404 * Manual offline is only allowed while the drive is not in use.
405 */
406int
407tape_generic_offline(struct tape_device *device)
408{
409 if (!device) {
410 PRINT_ERR("tape_generic_offline: no such device\n");
411 return -ENODEV;
412 }
413
414 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
415 device->cdev_id, device);
416
417 spin_lock_irq(get_ccwdev_lock(device->cdev));
418 switch (device->tape_state) {
419 case TS_INIT:
420 case TS_NOT_OPER:
421 spin_unlock_irq(get_ccwdev_lock(device->cdev));
422 break;
423 case TS_UNUSED:
424 tape_state_set(device, TS_INIT);
425 spin_unlock_irq(get_ccwdev_lock(device->cdev));
426 tape_cleanup_device(device);
427 break;
428 default:
429 DBF_EVENT(3, "(%08x): Set offline failed "
430 "- drive in use.\n",
431 device->cdev_id);
432 PRINT_WARN("(%s): Set offline failed "
433 "- drive in use.\n",
434 device->cdev->dev.bus_id);
435 spin_unlock_irq(get_ccwdev_lock(device->cdev));
436 return -EBUSY;
437 }
438
439 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
440 return 0;
441}
442
443/*
444 * Allocate memory for a new device structure.
445 */
446static struct tape_device *
447tape_alloc_device(void)
448{
449 struct tape_device *device;
450
88abaab4 451 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
1da177e4
LT
452 if (device == NULL) {
453 DBF_EXCEPTION(2, "ti:no mem\n");
454 PRINT_INFO ("can't allocate memory for "
455 "tape info structure\n");
456 return ERR_PTR(-ENOMEM);
457 }
88abaab4 458 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
1da177e4
LT
459 if (device->modeset_byte == NULL) {
460 DBF_EXCEPTION(2, "ti:no mem\n");
461 PRINT_INFO("can't allocate memory for modeset byte\n");
462 kfree(device);
463 return ERR_PTR(-ENOMEM);
464 }
465 INIT_LIST_HEAD(&device->req_queue);
466 INIT_LIST_HEAD(&device->node);
467 init_waitqueue_head(&device->state_change_wq);
468 device->tape_state = TS_INIT;
469 device->medium_state = MS_UNKNOWN;
470 *device->modeset_byte = 0;
471 device->first_minor = -1;
472 atomic_set(&device->ref_count, 1);
4111796d 473 INIT_WORK(&device->tape_dnr, tape_delayed_next_request, device);
1da177e4
LT
474
475 return device;
476}
477
478/*
479 * Get a reference to an existing device structure. This will automatically
480 * increment the reference count.
481 */
482struct tape_device *
483tape_get_device_reference(struct tape_device *device)
484{
485 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
486 atomic_inc_return(&device->ref_count));
487
488 return device;
489}
490
491/*
492 * Decrease the reference counter of a devices structure. If the
493 * reference counter reaches zero free the device structure.
494 * The function returns a NULL pointer to be used by the caller
495 * for clearing reference pointers.
496 */
497struct tape_device *
498tape_put_device(struct tape_device *device)
499{
500 int remain;
501
502 remain = atomic_dec_return(&device->ref_count);
503 if (remain > 0) {
504 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
505 } else {
506 if (remain < 0) {
507 DBF_EVENT(4, "put device without reference\n");
508 PRINT_ERR("put device without reference\n");
509 } else {
510 DBF_EVENT(4, "tape_free_device(%p)\n", device);
511 kfree(device->modeset_byte);
512 kfree(device);
513 }
514 }
515
516 return NULL;
517}
518
519/*
520 * Find tape device by a device index.
521 */
522struct tape_device *
523tape_get_device(int devindex)
524{
525 struct tape_device *device, *tmp;
526
527 device = ERR_PTR(-ENODEV);
528 read_lock(&tape_device_lock);
529 list_for_each_entry(tmp, &tape_device_list, node) {
530 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
531 device = tape_get_device_reference(tmp);
532 break;
533 }
534 }
535 read_unlock(&tape_device_lock);
536 return device;
537}
538
539/*
540 * Driverfs tape probe function.
541 */
542int
543tape_generic_probe(struct ccw_device *cdev)
544{
545 struct tape_device *device;
546
547 device = tape_alloc_device();
548 if (IS_ERR(device))
549 return -ENODEV;
550 PRINT_INFO("tape device %s found\n", cdev->dev.bus_id);
551 cdev->dev.driver_data = device;
552 device->cdev = cdev;
553 device->cdev_id = busid_to_int(cdev->dev.bus_id);
554 cdev->handler = __tape_do_irq;
555
556 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
557 sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
558
559 return 0;
560}
561
562static inline void
563__tape_discard_requests(struct tape_device *device)
564{
565 struct tape_request * request;
566 struct list_head * l, *n;
567
568 list_for_each_safe(l, n, &device->req_queue) {
569 request = list_entry(l, struct tape_request, list);
570 if (request->status == TAPE_REQUEST_IN_IO)
571 request->status = TAPE_REQUEST_DONE;
572 list_del(&request->list);
573
574 /* Decrease ref_count for removed request. */
575 request->device = tape_put_device(device);
576 request->rc = -EIO;
577 if (request->callback != NULL)
578 request->callback(request, request->callback_data);
579 }
580}
581
582/*
583 * Driverfs tape remove function.
584 *
585 * This function is called whenever the common I/O layer detects the device
586 * gone. This can happen at any time and we cannot refuse.
587 */
588void
589tape_generic_remove(struct ccw_device *cdev)
590{
591 struct tape_device * device;
592
593 device = cdev->dev.driver_data;
594 if (!device) {
595 PRINT_ERR("No device pointer in tape_generic_remove!\n");
596 return;
597 }
598 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
599
600 spin_lock_irq(get_ccwdev_lock(device->cdev));
601 switch (device->tape_state) {
602 case TS_INIT:
603 tape_state_set(device, TS_NOT_OPER);
604 case TS_NOT_OPER:
605 /*
606 * Nothing to do.
607 */
608 spin_unlock_irq(get_ccwdev_lock(device->cdev));
609 break;
610 case TS_UNUSED:
611 /*
612 * Need only to release the device.
613 */
614 tape_state_set(device, TS_NOT_OPER);
615 spin_unlock_irq(get_ccwdev_lock(device->cdev));
616 tape_cleanup_device(device);
617 break;
618 default:
619 /*
620 * There may be requests on the queue. We will not get
621 * an interrupt for a request that was running. So we
622 * just post them all as I/O errors.
623 */
624 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
625 device->cdev_id);
626 PRINT_WARN("(%s): Drive in use vanished - "
627 "expect trouble!\n",
628 device->cdev->dev.bus_id);
629 PRINT_WARN("State was %i\n", device->tape_state);
630 tape_state_set(device, TS_NOT_OPER);
631 __tape_discard_requests(device);
632 spin_unlock_irq(get_ccwdev_lock(device->cdev));
633 tape_cleanup_device(device);
634 }
635
636 if (cdev->dev.driver_data != NULL) {
637 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
638 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
639 }
640}
641
642/*
643 * Allocate a new tape ccw request
644 */
645struct tape_request *
646tape_alloc_request(int cplength, int datasize)
647{
648 struct tape_request *request;
649
650 if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
651 BUG();
652
653 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
654
88abaab4 655 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
1da177e4
LT
656 if (request == NULL) {
657 DBF_EXCEPTION(1, "cqra nomem\n");
658 return ERR_PTR(-ENOMEM);
659 }
1da177e4
LT
660 /* allocate channel program */
661 if (cplength > 0) {
88abaab4 662 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1da177e4
LT
663 GFP_ATOMIC | GFP_DMA);
664 if (request->cpaddr == NULL) {
665 DBF_EXCEPTION(1, "cqra nomem\n");
666 kfree(request);
667 return ERR_PTR(-ENOMEM);
668 }
1da177e4
LT
669 }
670 /* alloc small kernel buffer */
671 if (datasize > 0) {
88abaab4 672 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
1da177e4
LT
673 if (request->cpdata == NULL) {
674 DBF_EXCEPTION(1, "cqra nomem\n");
17fd682e 675 kfree(request->cpaddr);
1da177e4
LT
676 kfree(request);
677 return ERR_PTR(-ENOMEM);
678 }
1da177e4
LT
679 }
680 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
681 request->cpdata);
682
683 return request;
684}
685
686/*
687 * Free tape ccw request
688 */
689void
690tape_free_request (struct tape_request * request)
691{
692 DBF_LH(6, "Free request %p\n", request);
693
694 if (request->device != NULL) {
695 request->device = tape_put_device(request->device);
696 }
17fd682e
JJ
697 kfree(request->cpdata);
698 kfree(request->cpaddr);
1da177e4
LT
699 kfree(request);
700}
701
4111796d
SB
702static inline int
703__tape_start_io(struct tape_device *device, struct tape_request *request)
704{
705 int rc;
706
707#ifdef CONFIG_S390_TAPE_BLOCK
708 if (request->op == TO_BLOCK)
709 device->discipline->check_locate(device, request);
710#endif
711 rc = ccw_device_start(
712 device->cdev,
713 request->cpaddr,
714 (unsigned long) request,
715 0x00,
716 request->options
717 );
718 if (rc == 0) {
719 request->status = TAPE_REQUEST_IN_IO;
720 } else if (rc == -EBUSY) {
721 /* The common I/O subsystem is currently busy. Retry later. */
722 request->status = TAPE_REQUEST_QUEUED;
723 schedule_work(&device->tape_dnr);
724 rc = 0;
725 } else {
726 /* Start failed. Remove request and indicate failure. */
727 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
728 }
729 return rc;
730}
731
1da177e4 732static inline void
4111796d 733__tape_start_next_request(struct tape_device *device)
1da177e4
LT
734{
735 struct list_head *l, *n;
736 struct tape_request *request;
737 int rc;
738
4111796d 739 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
1da177e4
LT
740 /*
741 * Try to start each request on request queue until one is
742 * started successful.
743 */
744 list_for_each_safe(l, n, &device->req_queue) {
745 request = list_entry(l, struct tape_request, list);
4111796d
SB
746
747 /*
748 * Avoid race condition if bottom-half was triggered more than
749 * once.
750 */
751 if (request->status == TAPE_REQUEST_IN_IO)
752 return;
5f384338
MH
753 /*
754 * Request has already been stopped. We have to wait until
755 * the request is removed from the queue in the interrupt
756 * handling.
757 */
758 if (request->status == TAPE_REQUEST_DONE)
759 return;
4111796d
SB
760
761 /*
762 * We wanted to cancel the request but the common I/O layer
763 * was busy at that time. This can only happen if this
764 * function is called by delayed_next_request.
765 * Otherwise we start the next request on the queue.
766 */
767 if (request->status == TAPE_REQUEST_CANCEL) {
768 rc = __tape_cancel_io(device, request);
769 } else {
770 rc = __tape_start_io(device, request);
1da177e4 771 }
4111796d
SB
772 if (rc == 0)
773 return;
1da177e4 774
4111796d 775 /* Set ending status. */
1da177e4
LT
776 request->rc = rc;
777 request->status = TAPE_REQUEST_DONE;
4111796d
SB
778
779 /* Remove from request queue. */
780 list_del(&request->list);
781
782 /* Do callback. */
783 if (request->callback != NULL)
784 request->callback(request, request->callback_data);
1da177e4
LT
785 }
786}
787
788static void
4111796d 789tape_delayed_next_request(void *data)
1da177e4 790{
4111796d 791 struct tape_device * device;
1da177e4 792
4111796d
SB
793 device = (struct tape_device *) data;
794 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
795 spin_lock_irq(get_ccwdev_lock(device->cdev));
796 __tape_start_next_request(device);
797 spin_unlock_irq(get_ccwdev_lock(device->cdev));
798}
799
800static inline void
801__tape_end_request(
802 struct tape_device * device,
803 struct tape_request * request,
804 int rc)
805{
806 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
807 if (request) {
808 request->rc = rc;
809 request->status = TAPE_REQUEST_DONE;
810
811 /* Remove from request queue. */
812 list_del(&request->list);
813
814 /* Do callback. */
815 if (request->callback != NULL)
816 request->callback(request, request->callback_data);
817 }
1da177e4
LT
818
819 /* Start next request. */
820 if (!list_empty(&device->req_queue))
4111796d 821 __tape_start_next_request(device);
1da177e4
LT
822}
823
824/*
825 * Write sense data to console/dbf
826 */
827void
828tape_dump_sense(struct tape_device* device, struct tape_request *request,
829 struct irb *irb)
830{
831 unsigned int *sptr;
832
833 PRINT_INFO("-------------------------------------------------\n");
834 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
835 irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
836 PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
837 if (request != NULL)
838 PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]);
839
840 sptr = (unsigned int *) irb->ecw;
841 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
842 sptr[0], sptr[1], sptr[2], sptr[3]);
843 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
844 sptr[4], sptr[5], sptr[6], sptr[7]);
845 PRINT_INFO("--------------------------------------------------\n");
846}
847
848/*
849 * Write sense data to dbf
850 */
851void
852tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
853 struct irb *irb)
854{
855 unsigned int *sptr;
856 const char* op;
857
858 if (request != NULL)
859 op = tape_op_verbose[request->op];
860 else
861 op = "---";
862 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
863 irb->scsw.dstat,irb->scsw.cstat);
864 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
865 sptr = (unsigned int *) irb->ecw;
866 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
867 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
868 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
869 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
870}
871
872/*
873 * I/O helper function. Adds the request to the request queue
874 * and starts it if the tape is idle. Has to be called with
875 * the device lock held.
876 */
877static inline int
4111796d 878__tape_start_request(struct tape_device *device, struct tape_request *request)
1da177e4
LT
879{
880 int rc;
881
882 switch (request->op) {
883 case TO_MSEN:
884 case TO_ASSIGN:
885 case TO_UNASSIGN:
886 case TO_READ_ATTMSG:
887 if (device->tape_state == TS_INIT)
888 break;
889 if (device->tape_state == TS_UNUSED)
890 break;
891 default:
892 if (device->tape_state == TS_BLKUSE)
893 break;
894 if (device->tape_state != TS_IN_USE)
895 return -ENODEV;
896 }
897
898 /* Increase use count of device for the added request. */
899 request->device = tape_get_device_reference(device);
900
901 if (list_empty(&device->req_queue)) {
902 /* No other requests are on the queue. Start this one. */
4111796d
SB
903 rc = __tape_start_io(device, request);
904 if (rc)
1da177e4 905 return rc;
4111796d 906
1da177e4
LT
907 DBF_LH(5, "Request %p added for execution.\n", request);
908 list_add(&request->list, &device->req_queue);
1da177e4
LT
909 } else {
910 DBF_LH(5, "Request %p add to queue.\n", request);
1da177e4 911 request->status = TAPE_REQUEST_QUEUED;
4111796d 912 list_add_tail(&request->list, &device->req_queue);
1da177e4
LT
913 }
914 return 0;
915}
916
917/*
918 * Add the request to the request queue, try to start it if the
919 * tape is idle. Return without waiting for end of i/o.
920 */
921int
922tape_do_io_async(struct tape_device *device, struct tape_request *request)
923{
924 int rc;
925
926 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
927
928 spin_lock_irq(get_ccwdev_lock(device->cdev));
929 /* Add request to request queue and try to start it. */
4111796d 930 rc = __tape_start_request(device, request);
1da177e4
LT
931 spin_unlock_irq(get_ccwdev_lock(device->cdev));
932 return rc;
933}
934
935/*
936 * tape_do_io/__tape_wake_up
937 * Add the request to the request queue, try to start it if the
938 * tape is idle and wait uninterruptible for its completion.
939 */
940static void
941__tape_wake_up(struct tape_request *request, void *data)
942{
943 request->callback = NULL;
944 wake_up((wait_queue_head_t *) data);
945}
946
947int
948tape_do_io(struct tape_device *device, struct tape_request *request)
949{
950 wait_queue_head_t wq;
951 int rc;
952
953 init_waitqueue_head(&wq);
954 spin_lock_irq(get_ccwdev_lock(device->cdev));
955 /* Setup callback */
956 request->callback = __tape_wake_up;
957 request->callback_data = &wq;
958 /* Add request to request queue and try to start it. */
4111796d 959 rc = __tape_start_request(device, request);
1da177e4
LT
960 spin_unlock_irq(get_ccwdev_lock(device->cdev));
961 if (rc)
962 return rc;
963 /* Request added to the queue. Wait for its completion. */
964 wait_event(wq, (request->callback == NULL));
965 /* Get rc from request */
966 return request->rc;
967}
968
969/*
970 * tape_do_io_interruptible/__tape_wake_up_interruptible
971 * Add the request to the request queue, try to start it if the
972 * tape is idle and wait uninterruptible for its completion.
973 */
974static void
975__tape_wake_up_interruptible(struct tape_request *request, void *data)
976{
977 request->callback = NULL;
978 wake_up_interruptible((wait_queue_head_t *) data);
979}
980
981int
982tape_do_io_interruptible(struct tape_device *device,
983 struct tape_request *request)
984{
985 wait_queue_head_t wq;
986 int rc;
987
988 init_waitqueue_head(&wq);
989 spin_lock_irq(get_ccwdev_lock(device->cdev));
990 /* Setup callback */
991 request->callback = __tape_wake_up_interruptible;
992 request->callback_data = &wq;
4111796d 993 rc = __tape_start_request(device, request);
1da177e4
LT
994 spin_unlock_irq(get_ccwdev_lock(device->cdev));
995 if (rc)
996 return rc;
997 /* Request added to the queue. Wait for its completion. */
998 rc = wait_event_interruptible(wq, (request->callback == NULL));
999 if (rc != -ERESTARTSYS)
1000 /* Request finished normally. */
1001 return request->rc;
4111796d 1002
1da177e4
LT
1003 /* Interrupted by a signal. We have to stop the current request. */
1004 spin_lock_irq(get_ccwdev_lock(device->cdev));
4111796d
SB
1005 rc = __tape_cancel_io(device, request);
1006 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1da177e4 1007 if (rc == 0) {
4111796d
SB
1008 /* Wait for the interrupt that acknowledges the halt. */
1009 do {
1010 rc = wait_event_interruptible(
1011 wq,
1012 (request->callback == NULL)
1013 );
4cd190a7 1014 } while (rc == -ERESTARTSYS);
4111796d 1015
1da177e4
LT
1016 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1017 rc = -ERESTARTSYS;
1018 }
1da177e4
LT
1019 return rc;
1020}
1021
5f384338
MH
1022/*
1023 * Stop running ccw.
1024 */
1025int
1026tape_cancel_io(struct tape_device *device, struct tape_request *request)
1027{
1028 int rc;
1029
1030 spin_lock_irq(get_ccwdev_lock(device->cdev));
1031 rc = __tape_cancel_io(device, request);
1032 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1033 return rc;
1034}
1035
1da177e4
LT
1036/*
1037 * Tape interrupt routine, called from the ccw_device layer
1038 */
1039static void
1040__tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1041{
1042 struct tape_device *device;
1043 struct tape_request *request;
1da177e4
LT
1044 int rc;
1045
1046 device = (struct tape_device *) cdev->dev.driver_data;
1047 if (device == NULL) {
1048 PRINT_ERR("could not get device structure for %s "
1049 "in interrupt\n", cdev->dev.bus_id);
1050 return;
1051 }
1052 request = (struct tape_request *) intparm;
1053
1054 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1055
1056 /* On special conditions irb is an error pointer */
1057 if (IS_ERR(irb)) {
4111796d 1058 /* FIXME: What to do with the request? */
1da177e4
LT
1059 switch (PTR_ERR(irb)) {
1060 case -ETIMEDOUT:
1061 PRINT_WARN("(%s): Request timed out\n",
1062 cdev->dev.bus_id);
1063 case -EIO:
4111796d 1064 __tape_end_request(device, request, -EIO);
1da177e4
LT
1065 break;
1066 default:
1067 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1068 cdev->dev.bus_id,
1069 PTR_ERR(irb));
1070 }
1071 return;
1072 }
1073
4111796d
SB
1074 /*
1075 * If the condition code is not zero and the start function bit is
1076 * still set, this is an deferred error and the last start I/O did
842d3fba
SB
1077 * not succeed. At this point the condition that caused the deferred
1078 * error might still apply. So we just schedule the request to be
1079 * started later.
4111796d 1080 */
5f384338
MH
1081 if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC) &&
1082 (request->status == TAPE_REQUEST_IN_IO)) {
1083 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1084 device->cdev_id, irb->scsw.cc, irb->scsw.fctl);
842d3fba 1085 request->status = TAPE_REQUEST_QUEUED;
5f384338 1086 schedule_delayed_work(&device->tape_dnr, HZ);
4111796d
SB
1087 return;
1088 }
1089
1da177e4
LT
1090 /* May be an unsolicited irq */
1091 if(request != NULL)
1092 request->rescnt = irb->scsw.count;
1093
1094 if (irb->scsw.dstat != 0x0c) {
1095 /* Set the 'ONLINE' flag depending on sense byte 1 */
1096 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1097 device->tape_generic_status |= GMT_ONLINE(~0);
1098 else
1099 device->tape_generic_status &= ~GMT_ONLINE(~0);
1100
1101 /*
1102 * Any request that does not come back with channel end
1103 * and device end is unusual. Log the sense data.
1104 */
1105 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1106 tape_dump_sense_dbf(device, request, irb);
1107 } else {
1108 /* Upon normal completion the device _is_ online */
1109 device->tape_generic_status |= GMT_ONLINE(~0);
1110 }
1111 if (device->tape_state == TS_NOT_OPER) {
1112 DBF_EVENT(6, "tape:device is not operational\n");
1113 return;
1114 }
1115
1116 /*
1117 * Request that were canceled still come back with an interrupt.
1118 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1119 */
1120 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
4111796d 1121 __tape_end_request(device, request, -EIO);
1da177e4
LT
1122 return;
1123 }
1124
1125 rc = device->discipline->irq(device, request, irb);
1126 /*
1127 * rc < 0 : request finished unsuccessfully.
1128 * rc == TAPE_IO_SUCCESS: request finished successfully.
1129 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1130 * rc == TAPE_IO_RETRY: request finished but needs another go.
1131 * rc == TAPE_IO_STOP: request needs to get terminated.
1132 */
1da177e4 1133 switch (rc) {
4111796d
SB
1134 case TAPE_IO_SUCCESS:
1135 /* Upon normal completion the device _is_ online */
1136 device->tape_generic_status |= GMT_ONLINE(~0);
1137 __tape_end_request(device, request, rc);
1138 break;
1139 case TAPE_IO_PENDING:
1140 break;
1141 case TAPE_IO_RETRY:
1142 rc = __tape_start_io(device, request);
1143 if (rc)
1144 __tape_end_request(device, request, rc);
1145 break;
1146 case TAPE_IO_STOP:
1147 rc = __tape_cancel_io(device, request);
1148 if (rc)
1149 __tape_end_request(device, request, rc);
1150 break;
1151 default:
1152 if (rc > 0) {
1153 DBF_EVENT(6, "xunknownrc\n");
1154 PRINT_ERR("Invalid return code from discipline "
1155 "interrupt function.\n");
1156 __tape_end_request(device, request, -EIO);
1157 } else {
1158 __tape_end_request(device, request, rc);
1159 }
1160 break;
1da177e4
LT
1161 }
1162}
1163
1164/*
1165 * Tape device open function used by tape_char & tape_block frontends.
1166 */
1167int
1168tape_open(struct tape_device *device)
1169{
1170 int rc;
1171
1172 spin_lock(get_ccwdev_lock(device->cdev));
1173 if (device->tape_state == TS_NOT_OPER) {
1174 DBF_EVENT(6, "TAPE:nodev\n");
1175 rc = -ENODEV;
1176 } else if (device->tape_state == TS_IN_USE) {
1177 DBF_EVENT(6, "TAPE:dbusy\n");
1178 rc = -EBUSY;
1179 } else if (device->tape_state == TS_BLKUSE) {
1180 DBF_EVENT(6, "TAPE:dbusy\n");
1181 rc = -EBUSY;
1182 } else if (device->discipline != NULL &&
1183 !try_module_get(device->discipline->owner)) {
1184 DBF_EVENT(6, "TAPE:nodisc\n");
1185 rc = -ENODEV;
1186 } else {
1187 tape_state_set(device, TS_IN_USE);
1188 rc = 0;
1189 }
1190 spin_unlock(get_ccwdev_lock(device->cdev));
1191 return rc;
1192}
1193
1194/*
1195 * Tape device release function used by tape_char & tape_block frontends.
1196 */
1197int
1198tape_release(struct tape_device *device)
1199{
1200 spin_lock(get_ccwdev_lock(device->cdev));
1201 if (device->tape_state == TS_IN_USE)
1202 tape_state_set(device, TS_UNUSED);
1203 module_put(device->discipline->owner);
1204 spin_unlock(get_ccwdev_lock(device->cdev));
1205 return 0;
1206}
1207
1208/*
1209 * Execute a magnetic tape command a number of times.
1210 */
1211int
1212tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1213{
1214 tape_mtop_fn fn;
1215 int rc;
1216
1217 DBF_EVENT(6, "TAPE:mtio\n");
1218 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1219 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1220
1221 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1222 return -EINVAL;
1223 fn = device->discipline->mtop_array[mt_op];
1224 if (fn == NULL)
1225 return -EINVAL;
1226
1227 /* We assume that the backends can handle count up to 500. */
1228 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1229 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1230 rc = 0;
1231 for (; mt_count > 500; mt_count -= 500)
1232 if ((rc = fn(device, 500)) != 0)
1233 break;
1234 if (rc == 0)
1235 rc = fn(device, mt_count);
1236 } else
1237 rc = fn(device, mt_count);
1238 return rc;
1239
1240}
1241
1242/*
1243 * Tape init function.
1244 */
1245static int
1246tape_init (void)
1247{
66a464db 1248 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1da177e4
LT
1249 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1250#ifdef DBF_LIKE_HELL
1251 debug_set_level(TAPE_DBF_AREA, 6);
1252#endif
e018ba1f 1253 DBF_EVENT(3, "tape init\n");
1da177e4
LT
1254 tape_proc_init();
1255 tapechar_init ();
1256 tapeblock_init ();
1257 return 0;
1258}
1259
1260/*
1261 * Tape exit function.
1262 */
1263static void
1264tape_exit(void)
1265{
1266 DBF_EVENT(6, "tape exit\n");
1267
1268 /* Get rid of the frontends */
1269 tapechar_exit();
1270 tapeblock_exit();
1271 tape_proc_cleanup();
1272 debug_unregister (TAPE_DBF_AREA);
1273}
1274
1275MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1276 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
e018ba1f 1277MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1da177e4
LT
1278MODULE_LICENSE("GPL");
1279
1280module_init(tape_init);
1281module_exit(tape_exit);
1282
1283EXPORT_SYMBOL(tape_generic_remove);
1284EXPORT_SYMBOL(tape_generic_probe);
1285EXPORT_SYMBOL(tape_generic_online);
1286EXPORT_SYMBOL(tape_generic_offline);
1287EXPORT_SYMBOL(tape_put_device);
1288EXPORT_SYMBOL(tape_get_device_reference);
1289EXPORT_SYMBOL(tape_state_verbose);
1290EXPORT_SYMBOL(tape_op_verbose);
1291EXPORT_SYMBOL(tape_state_set);
1292EXPORT_SYMBOL(tape_med_state_set);
1293EXPORT_SYMBOL(tape_alloc_request);
1294EXPORT_SYMBOL(tape_free_request);
1295EXPORT_SYMBOL(tape_dump_sense);
1296EXPORT_SYMBOL(tape_dump_sense_dbf);
1297EXPORT_SYMBOL(tape_do_io);
1298EXPORT_SYMBOL(tape_do_io_async);
1299EXPORT_SYMBOL(tape_do_io_interruptible);
5f384338 1300EXPORT_SYMBOL(tape_cancel_io);
1da177e4 1301EXPORT_SYMBOL(tape_mtop);
This page took 0.22472 seconds and 5 git commands to generate.