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