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