[S390] Avoid excessive inlining.
[deliverable/linux.git] / drivers / s390 / char / tape_core.c
1 /*
2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
4 *
5 * S390 and zSeries version
6 * Copyright IBM Corp. 2001,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",
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 if (device->tape_state == TS_INIT)
915 break;
916 if (device->tape_state == TS_UNUSED)
917 break;
918 default:
919 if (device->tape_state == TS_BLKUSE)
920 break;
921 if (device->tape_state != TS_IN_USE)
922 return -ENODEV;
923 }
924
925 /* Increase use count of device for the added request. */
926 request->device = tape_get_device_reference(device);
927
928 if (list_empty(&device->req_queue)) {
929 /* No other requests are on the queue. Start this one. */
930 rc = __tape_start_io(device, request);
931 if (rc)
932 return rc;
933
934 DBF_LH(5, "Request %p added for execution.\n", request);
935 list_add(&request->list, &device->req_queue);
936 } else {
937 DBF_LH(5, "Request %p add to queue.\n", request);
938 request->status = TAPE_REQUEST_QUEUED;
939 list_add_tail(&request->list, &device->req_queue);
940 }
941 return 0;
942 }
943
944 /*
945 * Add the request to the request queue, try to start it if the
946 * tape is idle. Return without waiting for end of i/o.
947 */
948 int
949 tape_do_io_async(struct tape_device *device, struct tape_request *request)
950 {
951 int rc;
952
953 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
954
955 spin_lock_irq(get_ccwdev_lock(device->cdev));
956 /* Add request to request queue and try to start it. */
957 rc = __tape_start_request(device, request);
958 spin_unlock_irq(get_ccwdev_lock(device->cdev));
959 return rc;
960 }
961
962 /*
963 * tape_do_io/__tape_wake_up
964 * Add the request to the request queue, try to start it if the
965 * tape is idle and wait uninterruptible for its completion.
966 */
967 static void
968 __tape_wake_up(struct tape_request *request, void *data)
969 {
970 request->callback = NULL;
971 wake_up((wait_queue_head_t *) data);
972 }
973
974 int
975 tape_do_io(struct tape_device *device, struct tape_request *request)
976 {
977 wait_queue_head_t wq;
978 int rc;
979
980 init_waitqueue_head(&wq);
981 spin_lock_irq(get_ccwdev_lock(device->cdev));
982 /* Setup callback */
983 request->callback = __tape_wake_up;
984 request->callback_data = &wq;
985 /* Add request to request queue and try to start it. */
986 rc = __tape_start_request(device, request);
987 spin_unlock_irq(get_ccwdev_lock(device->cdev));
988 if (rc)
989 return rc;
990 /* Request added to the queue. Wait for its completion. */
991 wait_event(wq, (request->callback == NULL));
992 /* Get rc from request */
993 return request->rc;
994 }
995
996 /*
997 * tape_do_io_interruptible/__tape_wake_up_interruptible
998 * Add the request to the request queue, try to start it if the
999 * tape is idle and wait uninterruptible for its completion.
1000 */
1001 static void
1002 __tape_wake_up_interruptible(struct tape_request *request, void *data)
1003 {
1004 request->callback = NULL;
1005 wake_up_interruptible((wait_queue_head_t *) data);
1006 }
1007
1008 int
1009 tape_do_io_interruptible(struct tape_device *device,
1010 struct tape_request *request)
1011 {
1012 wait_queue_head_t wq;
1013 int rc;
1014
1015 init_waitqueue_head(&wq);
1016 spin_lock_irq(get_ccwdev_lock(device->cdev));
1017 /* Setup callback */
1018 request->callback = __tape_wake_up_interruptible;
1019 request->callback_data = &wq;
1020 rc = __tape_start_request(device, request);
1021 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1022 if (rc)
1023 return rc;
1024 /* Request added to the queue. Wait for its completion. */
1025 rc = wait_event_interruptible(wq, (request->callback == NULL));
1026 if (rc != -ERESTARTSYS)
1027 /* Request finished normally. */
1028 return request->rc;
1029
1030 /* Interrupted by a signal. We have to stop the current request. */
1031 spin_lock_irq(get_ccwdev_lock(device->cdev));
1032 rc = __tape_cancel_io(device, request);
1033 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1034 if (rc == 0) {
1035 /* Wait for the interrupt that acknowledges the halt. */
1036 do {
1037 rc = wait_event_interruptible(
1038 wq,
1039 (request->callback == NULL)
1040 );
1041 } while (rc == -ERESTARTSYS);
1042
1043 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1044 rc = -ERESTARTSYS;
1045 }
1046 return rc;
1047 }
1048
1049 /*
1050 * Stop running ccw.
1051 */
1052 int
1053 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1054 {
1055 int rc;
1056
1057 spin_lock_irq(get_ccwdev_lock(device->cdev));
1058 rc = __tape_cancel_io(device, request);
1059 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1060 return rc;
1061 }
1062
1063 /*
1064 * Tape interrupt routine, called from the ccw_device layer
1065 */
1066 static void
1067 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1068 {
1069 struct tape_device *device;
1070 struct tape_request *request;
1071 int rc;
1072
1073 device = (struct tape_device *) cdev->dev.driver_data;
1074 if (device == NULL) {
1075 PRINT_ERR("could not get device structure for %s "
1076 "in interrupt\n", cdev->dev.bus_id);
1077 return;
1078 }
1079 request = (struct tape_request *) intparm;
1080
1081 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1082
1083 /* On special conditions irb is an error pointer */
1084 if (IS_ERR(irb)) {
1085 /* FIXME: What to do with the request? */
1086 switch (PTR_ERR(irb)) {
1087 case -ETIMEDOUT:
1088 PRINT_WARN("(%s): Request timed out\n",
1089 cdev->dev.bus_id);
1090 case -EIO:
1091 __tape_end_request(device, request, -EIO);
1092 break;
1093 default:
1094 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1095 cdev->dev.bus_id,
1096 PTR_ERR(irb));
1097 }
1098 return;
1099 }
1100
1101 /*
1102 * If the condition code is not zero and the start function bit is
1103 * still set, this is an deferred error and the last start I/O did
1104 * not succeed. At this point the condition that caused the deferred
1105 * error might still apply. So we just schedule the request to be
1106 * started later.
1107 */
1108 if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC) &&
1109 (request->status == TAPE_REQUEST_IN_IO)) {
1110 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1111 device->cdev_id, irb->scsw.cc, irb->scsw.fctl);
1112 request->status = TAPE_REQUEST_QUEUED;
1113 schedule_delayed_work(&device->tape_dnr, HZ);
1114 return;
1115 }
1116
1117 /* May be an unsolicited irq */
1118 if(request != NULL)
1119 request->rescnt = irb->scsw.count;
1120 else if ((irb->scsw.dstat == 0x85 || irb->scsw.dstat == 0x80) &&
1121 !list_empty(&device->req_queue)) {
1122 /* Not Ready to Ready after long busy ? */
1123 struct tape_request *req;
1124 req = list_entry(device->req_queue.next,
1125 struct tape_request, list);
1126 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1127 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1128 if (del_timer(&device->lb_timeout)) {
1129 device->lb_timeout.data = (unsigned long)
1130 tape_put_device(device);
1131 __tape_start_next_request(device);
1132 }
1133 return;
1134 }
1135 }
1136 if (irb->scsw.dstat != 0x0c) {
1137 /* Set the 'ONLINE' flag depending on sense byte 1 */
1138 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1139 device->tape_generic_status |= GMT_ONLINE(~0);
1140 else
1141 device->tape_generic_status &= ~GMT_ONLINE(~0);
1142
1143 /*
1144 * Any request that does not come back with channel end
1145 * and device end is unusual. Log the sense data.
1146 */
1147 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1148 tape_dump_sense_dbf(device, request, irb);
1149 } else {
1150 /* Upon normal completion the device _is_ online */
1151 device->tape_generic_status |= GMT_ONLINE(~0);
1152 }
1153 if (device->tape_state == TS_NOT_OPER) {
1154 DBF_EVENT(6, "tape:device is not operational\n");
1155 return;
1156 }
1157
1158 /*
1159 * Request that were canceled still come back with an interrupt.
1160 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1161 */
1162 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1163 __tape_end_request(device, request, -EIO);
1164 return;
1165 }
1166
1167 rc = device->discipline->irq(device, request, irb);
1168 /*
1169 * rc < 0 : request finished unsuccessfully.
1170 * rc == TAPE_IO_SUCCESS: request finished successfully.
1171 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1172 * rc == TAPE_IO_RETRY: request finished but needs another go.
1173 * rc == TAPE_IO_STOP: request needs to get terminated.
1174 */
1175 switch (rc) {
1176 case TAPE_IO_SUCCESS:
1177 /* Upon normal completion the device _is_ online */
1178 device->tape_generic_status |= GMT_ONLINE(~0);
1179 __tape_end_request(device, request, rc);
1180 break;
1181 case TAPE_IO_PENDING:
1182 break;
1183 case TAPE_IO_LONG_BUSY:
1184 device->lb_timeout.data =
1185 (unsigned long)tape_get_device_reference(device);
1186 device->lb_timeout.expires = jiffies +
1187 LONG_BUSY_TIMEOUT * HZ;
1188 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1189 add_timer(&device->lb_timeout);
1190 request->status = TAPE_REQUEST_LONG_BUSY;
1191 break;
1192 case TAPE_IO_RETRY:
1193 rc = __tape_start_io(device, request);
1194 if (rc)
1195 __tape_end_request(device, request, rc);
1196 break;
1197 case TAPE_IO_STOP:
1198 rc = __tape_cancel_io(device, request);
1199 if (rc)
1200 __tape_end_request(device, request, rc);
1201 break;
1202 default:
1203 if (rc > 0) {
1204 DBF_EVENT(6, "xunknownrc\n");
1205 PRINT_ERR("Invalid return code from discipline "
1206 "interrupt function.\n");
1207 __tape_end_request(device, request, -EIO);
1208 } else {
1209 __tape_end_request(device, request, rc);
1210 }
1211 break;
1212 }
1213 }
1214
1215 /*
1216 * Tape device open function used by tape_char & tape_block frontends.
1217 */
1218 int
1219 tape_open(struct tape_device *device)
1220 {
1221 int rc;
1222
1223 spin_lock(get_ccwdev_lock(device->cdev));
1224 if (device->tape_state == TS_NOT_OPER) {
1225 DBF_EVENT(6, "TAPE:nodev\n");
1226 rc = -ENODEV;
1227 } else if (device->tape_state == TS_IN_USE) {
1228 DBF_EVENT(6, "TAPE:dbusy\n");
1229 rc = -EBUSY;
1230 } else if (device->tape_state == TS_BLKUSE) {
1231 DBF_EVENT(6, "TAPE:dbusy\n");
1232 rc = -EBUSY;
1233 } else if (device->discipline != NULL &&
1234 !try_module_get(device->discipline->owner)) {
1235 DBF_EVENT(6, "TAPE:nodisc\n");
1236 rc = -ENODEV;
1237 } else {
1238 tape_state_set(device, TS_IN_USE);
1239 rc = 0;
1240 }
1241 spin_unlock(get_ccwdev_lock(device->cdev));
1242 return rc;
1243 }
1244
1245 /*
1246 * Tape device release function used by tape_char & tape_block frontends.
1247 */
1248 int
1249 tape_release(struct tape_device *device)
1250 {
1251 spin_lock(get_ccwdev_lock(device->cdev));
1252 if (device->tape_state == TS_IN_USE)
1253 tape_state_set(device, TS_UNUSED);
1254 module_put(device->discipline->owner);
1255 spin_unlock(get_ccwdev_lock(device->cdev));
1256 return 0;
1257 }
1258
1259 /*
1260 * Execute a magnetic tape command a number of times.
1261 */
1262 int
1263 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1264 {
1265 tape_mtop_fn fn;
1266 int rc;
1267
1268 DBF_EVENT(6, "TAPE:mtio\n");
1269 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1270 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1271
1272 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1273 return -EINVAL;
1274 fn = device->discipline->mtop_array[mt_op];
1275 if (fn == NULL)
1276 return -EINVAL;
1277
1278 /* We assume that the backends can handle count up to 500. */
1279 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1280 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1281 rc = 0;
1282 for (; mt_count > 500; mt_count -= 500)
1283 if ((rc = fn(device, 500)) != 0)
1284 break;
1285 if (rc == 0)
1286 rc = fn(device, mt_count);
1287 } else
1288 rc = fn(device, mt_count);
1289 return rc;
1290
1291 }
1292
1293 /*
1294 * Tape init function.
1295 */
1296 static int
1297 tape_init (void)
1298 {
1299 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1300 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1301 #ifdef DBF_LIKE_HELL
1302 debug_set_level(TAPE_DBF_AREA, 6);
1303 #endif
1304 DBF_EVENT(3, "tape init\n");
1305 tape_proc_init();
1306 tapechar_init ();
1307 tapeblock_init ();
1308 return 0;
1309 }
1310
1311 /*
1312 * Tape exit function.
1313 */
1314 static void
1315 tape_exit(void)
1316 {
1317 DBF_EVENT(6, "tape exit\n");
1318
1319 /* Get rid of the frontends */
1320 tapechar_exit();
1321 tapeblock_exit();
1322 tape_proc_cleanup();
1323 debug_unregister (TAPE_DBF_AREA);
1324 }
1325
1326 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1327 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1328 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1329 MODULE_LICENSE("GPL");
1330
1331 module_init(tape_init);
1332 module_exit(tape_exit);
1333
1334 EXPORT_SYMBOL(tape_generic_remove);
1335 EXPORT_SYMBOL(tape_generic_probe);
1336 EXPORT_SYMBOL(tape_generic_online);
1337 EXPORT_SYMBOL(tape_generic_offline);
1338 EXPORT_SYMBOL(tape_put_device);
1339 EXPORT_SYMBOL(tape_get_device_reference);
1340 EXPORT_SYMBOL(tape_state_verbose);
1341 EXPORT_SYMBOL(tape_op_verbose);
1342 EXPORT_SYMBOL(tape_state_set);
1343 EXPORT_SYMBOL(tape_med_state_set);
1344 EXPORT_SYMBOL(tape_alloc_request);
1345 EXPORT_SYMBOL(tape_free_request);
1346 EXPORT_SYMBOL(tape_dump_sense);
1347 EXPORT_SYMBOL(tape_dump_sense_dbf);
1348 EXPORT_SYMBOL(tape_do_io);
1349 EXPORT_SYMBOL(tape_do_io_async);
1350 EXPORT_SYMBOL(tape_do_io_interruptible);
1351 EXPORT_SYMBOL(tape_cancel_io);
1352 EXPORT_SYMBOL(tape_mtop);
This page took 0.061898 seconds and 5 git commands to generate.