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