blob: 1b6a244124656b11b7ea33832791811f62dafbf1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
4 *
5 * S390 and zSeries version
Michael Holzheucced1dd2007-02-05 21:18:26 +01006 * Copyright IBM Corp. 2001,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Stefan Bader41117962005-07-27 11:45:04 -070011 * Stefan Bader <shbader@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Carsten Otteab640db2009-03-26 15:24:38 +010014#define KMSG_COMPONENT "tape"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/init.h> // for kernel parameters
17#include <linux/kmod.h> // for requesting modules
18#include <linux/spinlock.h> // for locks
19#include <linux/vmalloc.h>
20#include <linux/list.h>
21
22#include <asm/types.h> // for variable types
23
24#define TAPE_DBF_AREA tape_core_dbf
25
26#include "tape.h"
27#include "tape_std.h"
28
Michael Holzheucced1dd2007-02-05 21:18:26 +010029#define LONG_BUSY_TIMEOUT 180 /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
Martin Schwidefskyc1637532006-12-08 15:53:57 +010032static void tape_delayed_next_request(struct work_struct *);
Michael Holzheucced1dd2007-02-05 21:18:26 +010033static void tape_long_busy_timeout(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
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 */
Denis Chengc11ca972008-01-26 14:11:13 +010040static LIST_HEAD(tape_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static DEFINE_RWLOCK(tape_device_lock);
42
43/*
44 * Pointer to debug area.
45 */
46debug_info_t *TAPE_DBF_AREA = NULL;
47EXPORT_SYMBOL(TAPE_DBF_AREA);
48
49/*
50 * Printable strings for tape enumerations.
51 */
52const 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
61const 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",
Michael Holzheucced1dd2007-02-05 21:18:26 +010074 [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON",
75 [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS",
Michael Holzheue2963062007-05-04 18:47:53 +020076 [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Cornelia Huckf455adc2008-05-15 16:52:37 +020079static int devid_to_int(struct ccw_dev_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Cornelia Huckf455adc2008-05-15 16:52:37 +020081 return dev_id->devno + (dev_id->ssid << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/*
85 * Some channel attached tape specific attributes.
86 *
87 * FIXME: In the future the first_minor and blocksize attribute should be
88 * replaced by a link to the cdev tree.
89 */
90static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040091tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct tape_device *tdev;
94
95 tdev = (struct tape_device *) dev->driver_data;
96 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
97}
98
99static
100DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
101
102static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400103tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct tape_device *tdev;
106
107 tdev = (struct tape_device *) dev->driver_data;
108 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
109}
110
111static
112DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
113
114static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400115tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct tape_device *tdev;
118
119 tdev = (struct tape_device *) dev->driver_data;
120 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
121 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
122}
123
124static
125DEVICE_ATTR(state, 0444, tape_state_show, NULL);
126
127static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400128tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct tape_device *tdev;
131 ssize_t rc;
132
133 tdev = (struct tape_device *) dev->driver_data;
134 if (tdev->first_minor < 0)
135 return scnprintf(buf, PAGE_SIZE, "N/A\n");
136
137 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
138 if (list_empty(&tdev->req_queue))
139 rc = scnprintf(buf, PAGE_SIZE, "---\n");
140 else {
141 struct tape_request *req;
142
143 req = list_entry(tdev->req_queue.next, struct tape_request,
144 list);
145 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
146 }
147 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
148 return rc;
149}
150
151static
152DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
153
154static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400155tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct tape_device *tdev;
158
159 tdev = (struct tape_device *) dev->driver_data;
160
161 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
162}
163
164static
165DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
166
167static struct attribute *tape_attrs[] = {
168 &dev_attr_medium_state.attr,
169 &dev_attr_first_minor.attr,
170 &dev_attr_state.attr,
171 &dev_attr_operation.attr,
172 &dev_attr_blocksize.attr,
173 NULL
174};
175
176static struct attribute_group tape_attr_group = {
177 .attrs = tape_attrs,
178};
179
180/*
181 * Tape state functions
182 */
183void
184tape_state_set(struct tape_device *device, enum tape_state newstate)
185{
186 const char *str;
187
188 if (device->tape_state == TS_NOT_OPER) {
189 DBF_EVENT(3, "ts_set err: not oper\n");
190 return;
191 }
192 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
Peter Oberparleiterf9760692006-04-10 22:53:49 -0700193 DBF_EVENT(4, "old ts:\t\n");
194 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 str = tape_state_verbose[device->tape_state];
196 else
197 str = "UNKNOWN TS";
198 DBF_EVENT(4, "%s\n", str);
199 DBF_EVENT(4, "new ts:\t\n");
Peter Oberparleiterf9760692006-04-10 22:53:49 -0700200 if (newstate < TS_SIZE && newstate >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 str = tape_state_verbose[newstate];
202 else
203 str = "UNKNOWN TS";
204 DBF_EVENT(4, "%s\n", str);
205 device->tape_state = newstate;
206 wake_up(&device->state_change_wq);
207}
208
209void
210tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
211{
212 if (device->medium_state == newstate)
213 return;
214 switch(newstate){
215 case MS_UNLOADED:
216 device->tape_generic_status |= GMT_DR_OPEN(~0);
Carsten Otteab640db2009-03-26 15:24:38 +0100217 dev_info(&device->cdev->dev, "The tape cartridge has been "
218 "successfully unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
220 case MS_LOADED:
221 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
Carsten Otteab640db2009-03-26 15:24:38 +0100222 dev_info(&device->cdev->dev, "A tape cartridge has been "
223 "mounted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 default:
226 // print nothing
227 break;
228 }
229 device->medium_state = newstate;
230 wake_up(&device->state_change_wq);
231}
232
233/*
234 * Stop running ccw. Has to be called with the device lock held.
235 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100236static int
Stefan Bader41117962005-07-27 11:45:04 -0700237__tape_cancel_io(struct tape_device *device, struct tape_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int retries;
240 int rc;
241
242 /* Check if interrupt has already been processed */
243 if (request->callback == NULL)
244 return 0;
245
246 rc = 0;
247 for (retries = 0; retries < 5; retries++) {
248 rc = ccw_device_clear(device->cdev, (long) request);
249
Stefan Bader41117962005-07-27 11:45:04 -0700250 switch (rc) {
251 case 0:
252 request->status = TAPE_REQUEST_DONE;
253 return 0;
254 case -EBUSY:
255 request->status = TAPE_REQUEST_CANCEL;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100256 schedule_delayed_work(&device->tape_dnr, 0);
Stefan Bader41117962005-07-27 11:45:04 -0700257 return 0;
258 case -ENODEV:
259 DBF_EXCEPTION(2, "device gone, retry\n");
260 break;
261 case -EIO:
262 DBF_EXCEPTION(2, "I/O error, retry\n");
263 break;
264 default:
265 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
269 return rc;
270}
271
272/*
273 * Add device into the sorted list, giving it the first
274 * available minor number.
275 */
276static int
277tape_assign_minor(struct tape_device *device)
278{
279 struct tape_device *tmp;
280 int minor;
281
282 minor = 0;
283 write_lock(&tape_device_lock);
284 list_for_each_entry(tmp, &tape_device_list, node) {
285 if (minor < tmp->first_minor)
286 break;
287 minor += TAPE_MINORS_PER_DEV;
288 }
289 if (minor >= 256) {
290 write_unlock(&tape_device_lock);
291 return -ENODEV;
292 }
293 device->first_minor = minor;
294 list_add_tail(&device->node, &tmp->node);
295 write_unlock(&tape_device_lock);
296 return 0;
297}
298
299/* remove device from the list */
300static void
301tape_remove_minor(struct tape_device *device)
302{
303 write_lock(&tape_device_lock);
304 list_del_init(&device->node);
305 device->first_minor = -1;
306 write_unlock(&tape_device_lock);
307}
308
309/*
310 * Set a device online.
311 *
312 * This function is called by the common I/O layer to move a device from the
313 * detected but offline into the online state.
314 * If we return an error (RC < 0) the device remains in the offline state. This
315 * can happen if the device is assigned somewhere else, for example.
316 */
317int
318tape_generic_online(struct tape_device *device,
319 struct tape_discipline *discipline)
320{
321 int rc;
322
323 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
324
325 if (device->tape_state != TS_INIT) {
326 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
327 return -EINVAL;
328 }
329
Michael Holzheucced1dd2007-02-05 21:18:26 +0100330 init_timer(&device->lb_timeout);
331 device->lb_timeout.function = tape_long_busy_timeout;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Let the discipline have a go at the device. */
334 device->discipline = discipline;
335 if (!try_module_get(discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -EINVAL;
337 }
338
339 rc = discipline->setup_device(device);
340 if (rc)
341 goto out;
342 rc = tape_assign_minor(device);
343 if (rc)
344 goto out_discipline;
345
346 rc = tapechar_setup_device(device);
347 if (rc)
348 goto out_minor;
349 rc = tapeblock_setup_device(device);
350 if (rc)
351 goto out_char;
352
353 tape_state_set(device, TS_UNUSED);
354
355 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
356
357 return 0;
358
359out_char:
360 tapechar_cleanup_device(device);
361out_discipline:
362 device->discipline->cleanup_device(device);
363 device->discipline = NULL;
364out_minor:
365 tape_remove_minor(device);
366out:
367 module_put(discipline->owner);
368 return rc;
369}
370
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100371static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372tape_cleanup_device(struct tape_device *device)
373{
374 tapeblock_cleanup_device(device);
375 tapechar_cleanup_device(device);
376 device->discipline->cleanup_device(device);
377 module_put(device->discipline->owner);
378 tape_remove_minor(device);
379 tape_med_state_set(device, MS_UNKNOWN);
380}
381
382/*
383 * Set device offline.
384 *
385 * Called by the common I/O layer if the drive should set offline on user
386 * request. We may prevent this by returning an error.
387 * Manual offline is only allowed while the drive is not in use.
388 */
389int
390tape_generic_offline(struct tape_device *device)
391{
392 if (!device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -ENODEV;
394 }
395
396 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
397 device->cdev_id, device);
398
399 spin_lock_irq(get_ccwdev_lock(device->cdev));
400 switch (device->tape_state) {
401 case TS_INIT:
402 case TS_NOT_OPER:
403 spin_unlock_irq(get_ccwdev_lock(device->cdev));
404 break;
405 case TS_UNUSED:
406 tape_state_set(device, TS_INIT);
407 spin_unlock_irq(get_ccwdev_lock(device->cdev));
408 tape_cleanup_device(device);
409 break;
410 default:
411 DBF_EVENT(3, "(%08x): Set offline failed "
412 "- drive in use.\n",
413 device->cdev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 spin_unlock_irq(get_ccwdev_lock(device->cdev));
415 return -EBUSY;
416 }
417
418 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
419 return 0;
420}
421
422/*
423 * Allocate memory for a new device structure.
424 */
425static struct tape_device *
426tape_alloc_device(void)
427{
428 struct tape_device *device;
429
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800430 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (device == NULL) {
432 DBF_EXCEPTION(2, "ti:no mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return ERR_PTR(-ENOMEM);
434 }
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800435 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (device->modeset_byte == NULL) {
437 DBF_EXCEPTION(2, "ti:no mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 kfree(device);
439 return ERR_PTR(-ENOMEM);
440 }
441 INIT_LIST_HEAD(&device->req_queue);
442 INIT_LIST_HEAD(&device->node);
443 init_waitqueue_head(&device->state_change_wq);
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200444 init_waitqueue_head(&device->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 device->tape_state = TS_INIT;
446 device->medium_state = MS_UNKNOWN;
447 *device->modeset_byte = 0;
448 device->first_minor = -1;
449 atomic_set(&device->ref_count, 1);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100450 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return device;
453}
454
455/*
456 * Get a reference to an existing device structure. This will automatically
457 * increment the reference count.
458 */
459struct tape_device *
460tape_get_device_reference(struct tape_device *device)
461{
462 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
463 atomic_inc_return(&device->ref_count));
464
465 return device;
466}
467
468/*
469 * Decrease the reference counter of a devices structure. If the
470 * reference counter reaches zero free the device structure.
471 * The function returns a NULL pointer to be used by the caller
472 * for clearing reference pointers.
473 */
474struct tape_device *
475tape_put_device(struct tape_device *device)
476{
477 int remain;
478
479 remain = atomic_dec_return(&device->ref_count);
480 if (remain > 0) {
481 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
482 } else {
483 if (remain < 0) {
484 DBF_EVENT(4, "put device without reference\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else {
486 DBF_EVENT(4, "tape_free_device(%p)\n", device);
487 kfree(device->modeset_byte);
488 kfree(device);
489 }
490 }
491
492 return NULL;
493}
494
495/*
496 * Find tape device by a device index.
497 */
498struct tape_device *
499tape_get_device(int devindex)
500{
501 struct tape_device *device, *tmp;
502
503 device = ERR_PTR(-ENODEV);
504 read_lock(&tape_device_lock);
505 list_for_each_entry(tmp, &tape_device_list, node) {
506 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
507 device = tape_get_device_reference(tmp);
508 break;
509 }
510 }
511 read_unlock(&tape_device_lock);
512 return device;
513}
514
515/*
516 * Driverfs tape probe function.
517 */
518int
519tape_generic_probe(struct ccw_device *cdev)
520{
521 struct tape_device *device;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200522 int ret;
Cornelia Huckf455adc2008-05-15 16:52:37 +0200523 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 device = tape_alloc_device();
526 if (IS_ERR(device))
527 return -ENODEV;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200528 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
529 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
530 if (ret) {
531 tape_put_device(device);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200532 return ret;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 cdev->dev.driver_data = device;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200535 cdev->handler = __tape_do_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 device->cdev = cdev;
Cornelia Huckf455adc2008-05-15 16:52:37 +0200537 ccw_device_get_id(cdev, &dev_id);
538 device->cdev_id = devid_to_int(&dev_id);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100542static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543__tape_discard_requests(struct tape_device *device)
544{
545 struct tape_request * request;
546 struct list_head * l, *n;
547
548 list_for_each_safe(l, n, &device->req_queue) {
549 request = list_entry(l, struct tape_request, list);
550 if (request->status == TAPE_REQUEST_IN_IO)
551 request->status = TAPE_REQUEST_DONE;
552 list_del(&request->list);
553
554 /* Decrease ref_count for removed request. */
555 request->device = tape_put_device(device);
556 request->rc = -EIO;
557 if (request->callback != NULL)
558 request->callback(request, request->callback_data);
559 }
560}
561
562/*
563 * Driverfs tape remove function.
564 *
565 * This function is called whenever the common I/O layer detects the device
566 * gone. This can happen at any time and we cannot refuse.
567 */
568void
569tape_generic_remove(struct ccw_device *cdev)
570{
571 struct tape_device * device;
572
573 device = cdev->dev.driver_data;
574 if (!device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return;
576 }
577 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
578
579 spin_lock_irq(get_ccwdev_lock(device->cdev));
580 switch (device->tape_state) {
581 case TS_INIT:
582 tape_state_set(device, TS_NOT_OPER);
583 case TS_NOT_OPER:
584 /*
585 * Nothing to do.
586 */
587 spin_unlock_irq(get_ccwdev_lock(device->cdev));
588 break;
589 case TS_UNUSED:
590 /*
591 * Need only to release the device.
592 */
593 tape_state_set(device, TS_NOT_OPER);
594 spin_unlock_irq(get_ccwdev_lock(device->cdev));
595 tape_cleanup_device(device);
596 break;
597 default:
598 /*
599 * There may be requests on the queue. We will not get
600 * an interrupt for a request that was running. So we
601 * just post them all as I/O errors.
602 */
603 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
604 device->cdev_id);
Carsten Otteab640db2009-03-26 15:24:38 +0100605 dev_warn(&device->cdev->dev, "A tape unit was detached"
606 " while in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 tape_state_set(device, TS_NOT_OPER);
608 __tape_discard_requests(device);
609 spin_unlock_irq(get_ccwdev_lock(device->cdev));
610 tape_cleanup_device(device);
611 }
612
613 if (cdev->dev.driver_data != NULL) {
614 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
615 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
616 }
617}
618
619/*
620 * Allocate a new tape ccw request
621 */
622struct tape_request *
623tape_alloc_request(int cplength, int datasize)
624{
625 struct tape_request *request;
626
627 if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
628 BUG();
629
630 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
631
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800632 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (request == NULL) {
634 DBF_EXCEPTION(1, "cqra nomem\n");
635 return ERR_PTR(-ENOMEM);
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* allocate channel program */
638 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800639 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 GFP_ATOMIC | GFP_DMA);
641 if (request->cpaddr == NULL) {
642 DBF_EXCEPTION(1, "cqra nomem\n");
643 kfree(request);
644 return ERR_PTR(-ENOMEM);
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 /* alloc small kernel buffer */
648 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800649 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (request->cpdata == NULL) {
651 DBF_EXCEPTION(1, "cqra nomem\n");
Jesper Juhl17fd6822005-11-07 01:01:30 -0800652 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 kfree(request);
654 return ERR_PTR(-ENOMEM);
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
658 request->cpdata);
659
660 return request;
661}
662
663/*
664 * Free tape ccw request
665 */
666void
667tape_free_request (struct tape_request * request)
668{
669 DBF_LH(6, "Free request %p\n", request);
670
671 if (request->device != NULL) {
672 request->device = tape_put_device(request->device);
673 }
Jesper Juhl17fd6822005-11-07 01:01:30 -0800674 kfree(request->cpdata);
675 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 kfree(request);
677}
678
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100679static int
Stefan Bader41117962005-07-27 11:45:04 -0700680__tape_start_io(struct tape_device *device, struct tape_request *request)
681{
682 int rc;
683
684#ifdef CONFIG_S390_TAPE_BLOCK
685 if (request->op == TO_BLOCK)
686 device->discipline->check_locate(device, request);
687#endif
688 rc = ccw_device_start(
689 device->cdev,
690 request->cpaddr,
691 (unsigned long) request,
692 0x00,
693 request->options
694 );
695 if (rc == 0) {
696 request->status = TAPE_REQUEST_IN_IO;
697 } else if (rc == -EBUSY) {
698 /* The common I/O subsystem is currently busy. Retry later. */
699 request->status = TAPE_REQUEST_QUEUED;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100700 schedule_delayed_work(&device->tape_dnr, 0);
Stefan Bader41117962005-07-27 11:45:04 -0700701 rc = 0;
702 } else {
703 /* Start failed. Remove request and indicate failure. */
704 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
705 }
706 return rc;
707}
708
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100709static void
Stefan Bader41117962005-07-27 11:45:04 -0700710__tape_start_next_request(struct tape_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct list_head *l, *n;
713 struct tape_request *request;
714 int rc;
715
Stefan Bader41117962005-07-27 11:45:04 -0700716 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * Try to start each request on request queue until one is
719 * started successful.
720 */
721 list_for_each_safe(l, n, &device->req_queue) {
722 request = list_entry(l, struct tape_request, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Stefan Bader41117962005-07-27 11:45:04 -0700724 /*
725 * Avoid race condition if bottom-half was triggered more than
726 * once.
727 */
728 if (request->status == TAPE_REQUEST_IN_IO)
729 return;
Michael Holzheu5f384332006-03-24 03:15:28 -0800730 /*
731 * Request has already been stopped. We have to wait until
732 * the request is removed from the queue in the interrupt
733 * handling.
734 */
735 if (request->status == TAPE_REQUEST_DONE)
736 return;
Stefan Bader41117962005-07-27 11:45:04 -0700737
738 /*
739 * We wanted to cancel the request but the common I/O layer
740 * was busy at that time. This can only happen if this
741 * function is called by delayed_next_request.
742 * Otherwise we start the next request on the queue.
743 */
744 if (request->status == TAPE_REQUEST_CANCEL) {
745 rc = __tape_cancel_io(device, request);
746 } else {
747 rc = __tape_start_io(device, request);
748 }
749 if (rc == 0)
750 return;
751
752 /* Set ending status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 request->rc = rc;
754 request->status = TAPE_REQUEST_DONE;
Stefan Bader41117962005-07-27 11:45:04 -0700755
756 /* Remove from request queue. */
757 list_del(&request->list);
758
759 /* Do callback. */
760 if (request->callback != NULL)
761 request->callback(request, request->callback_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763}
764
765static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100766tape_delayed_next_request(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100768 struct tape_device *device =
769 container_of(work, struct tape_device, tape_dnr.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Stefan Bader41117962005-07-27 11:45:04 -0700771 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
772 spin_lock_irq(get_ccwdev_lock(device->cdev));
773 __tape_start_next_request(device);
774 spin_unlock_irq(get_ccwdev_lock(device->cdev));
775}
776
Michael Holzheucced1dd2007-02-05 21:18:26 +0100777static void tape_long_busy_timeout(unsigned long data)
778{
779 struct tape_request *request;
780 struct tape_device *device;
781
782 device = (struct tape_device *) data;
783 spin_lock_irq(get_ccwdev_lock(device->cdev));
784 request = list_entry(device->req_queue.next, struct tape_request, list);
785 if (request->status != TAPE_REQUEST_LONG_BUSY)
786 BUG();
787 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
788 __tape_start_next_request(device);
789 device->lb_timeout.data = (unsigned long) tape_put_device(device);
790 spin_unlock_irq(get_ccwdev_lock(device->cdev));
791}
792
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100793static void
Stefan Bader41117962005-07-27 11:45:04 -0700794__tape_end_request(
795 struct tape_device * device,
796 struct tape_request * request,
797 int rc)
798{
799 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
800 if (request) {
801 request->rc = rc;
802 request->status = TAPE_REQUEST_DONE;
803
804 /* Remove from request queue. */
805 list_del(&request->list);
806
807 /* Do callback. */
808 if (request->callback != NULL)
809 request->callback(request, request->callback_data);
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Start next request. */
813 if (!list_empty(&device->req_queue))
Stefan Bader41117962005-07-27 11:45:04 -0700814 __tape_start_next_request(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * Write sense data to dbf
819 */
820void
821tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
822 struct irb *irb)
823{
824 unsigned int *sptr;
825 const char* op;
826
827 if (request != NULL)
828 op = tape_op_verbose[request->op];
829 else
830 op = "---";
831 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200832 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
834 sptr = (unsigned int *) irb->ecw;
835 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
836 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
837 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
838 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
839}
840
841/*
842 * I/O helper function. Adds the request to the request queue
843 * and starts it if the tape is idle. Has to be called with
844 * the device lock held.
845 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100846static int
Stefan Bader41117962005-07-27 11:45:04 -0700847__tape_start_request(struct tape_device *device, struct tape_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 int rc;
850
851 switch (request->op) {
852 case TO_MSEN:
853 case TO_ASSIGN:
854 case TO_UNASSIGN:
855 case TO_READ_ATTMSG:
Michael Holzheue2963062007-05-04 18:47:53 +0200856 case TO_RDC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (device->tape_state == TS_INIT)
858 break;
859 if (device->tape_state == TS_UNUSED)
860 break;
861 default:
862 if (device->tape_state == TS_BLKUSE)
863 break;
864 if (device->tape_state != TS_IN_USE)
865 return -ENODEV;
866 }
867
868 /* Increase use count of device for the added request. */
869 request->device = tape_get_device_reference(device);
870
871 if (list_empty(&device->req_queue)) {
872 /* No other requests are on the queue. Start this one. */
Stefan Bader41117962005-07-27 11:45:04 -0700873 rc = __tape_start_io(device, request);
874 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return rc;
Stefan Bader41117962005-07-27 11:45:04 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 DBF_LH(5, "Request %p added for execution.\n", request);
878 list_add(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else {
880 DBF_LH(5, "Request %p add to queue.\n", request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 request->status = TAPE_REQUEST_QUEUED;
Stefan Bader41117962005-07-27 11:45:04 -0700882 list_add_tail(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 return 0;
885}
886
887/*
888 * Add the request to the request queue, try to start it if the
889 * tape is idle. Return without waiting for end of i/o.
890 */
891int
892tape_do_io_async(struct tape_device *device, struct tape_request *request)
893{
894 int rc;
895
896 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
897
898 spin_lock_irq(get_ccwdev_lock(device->cdev));
899 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -0700900 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 spin_unlock_irq(get_ccwdev_lock(device->cdev));
902 return rc;
903}
904
905/*
906 * tape_do_io/__tape_wake_up
907 * Add the request to the request queue, try to start it if the
908 * tape is idle and wait uninterruptible for its completion.
909 */
910static void
911__tape_wake_up(struct tape_request *request, void *data)
912{
913 request->callback = NULL;
914 wake_up((wait_queue_head_t *) data);
915}
916
917int
918tape_do_io(struct tape_device *device, struct tape_request *request)
919{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int rc;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 spin_lock_irq(get_ccwdev_lock(device->cdev));
923 /* Setup callback */
924 request->callback = __tape_wake_up;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200925 request->callback_data = &device->wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -0700927 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 spin_unlock_irq(get_ccwdev_lock(device->cdev));
929 if (rc)
930 return rc;
931 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200932 wait_event(device->wait_queue, (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Get rc from request */
934 return request->rc;
935}
936
937/*
938 * tape_do_io_interruptible/__tape_wake_up_interruptible
939 * Add the request to the request queue, try to start it if the
940 * tape is idle and wait uninterruptible for its completion.
941 */
942static void
943__tape_wake_up_interruptible(struct tape_request *request, void *data)
944{
945 request->callback = NULL;
946 wake_up_interruptible((wait_queue_head_t *) data);
947}
948
949int
950tape_do_io_interruptible(struct tape_device *device,
951 struct tape_request *request)
952{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int rc;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 spin_lock_irq(get_ccwdev_lock(device->cdev));
956 /* Setup callback */
957 request->callback = __tape_wake_up_interruptible;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200958 request->callback_data = &device->wait_queue;
Stefan Bader41117962005-07-27 11:45:04 -0700959 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_unlock_irq(get_ccwdev_lock(device->cdev));
961 if (rc)
962 return rc;
963 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200964 rc = wait_event_interruptible(device->wait_queue,
965 (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (rc != -ERESTARTSYS)
967 /* Request finished normally. */
968 return request->rc;
Stefan Bader41117962005-07-27 11:45:04 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Interrupted by a signal. We have to stop the current request. */
971 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Bader41117962005-07-27 11:45:04 -0700972 rc = __tape_cancel_io(device, request);
973 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (rc == 0) {
Stefan Bader41117962005-07-27 11:45:04 -0700975 /* Wait for the interrupt that acknowledges the halt. */
976 do {
977 rc = wait_event_interruptible(
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200978 device->wait_queue,
Stefan Bader41117962005-07-27 11:45:04 -0700979 (request->callback == NULL)
980 );
Michael Holzheu4cd190a2006-03-24 03:15:27 -0800981 } while (rc == -ERESTARTSYS);
Stefan Bader41117962005-07-27 11:45:04 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
984 rc = -ERESTARTSYS;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return rc;
987}
988
989/*
Michael Holzheu5f384332006-03-24 03:15:28 -0800990 * Stop running ccw.
991 */
992int
993tape_cancel_io(struct tape_device *device, struct tape_request *request)
994{
995 int rc;
996
997 spin_lock_irq(get_ccwdev_lock(device->cdev));
998 rc = __tape_cancel_io(device, request);
999 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1000 return rc;
1001}
1002
1003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * Tape interrupt routine, called from the ccw_device layer
1005 */
1006static void
1007__tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1008{
1009 struct tape_device *device;
1010 struct tape_request *request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int rc;
1012
1013 device = (struct tape_device *) cdev->dev.driver_data;
1014 if (device == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return;
1016 }
1017 request = (struct tape_request *) intparm;
1018
1019 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1020
1021 /* On special conditions irb is an error pointer */
1022 if (IS_ERR(irb)) {
Stefan Bader41117962005-07-27 11:45:04 -07001023 /* FIXME: What to do with the request? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 switch (PTR_ERR(irb)) {
1025 case -ETIMEDOUT:
Carsten Otteab640db2009-03-26 15:24:38 +01001026 DBF_LH(1, "(%s): Request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001027 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 case -EIO:
Stefan Bader41117962005-07-27 11:45:04 -07001029 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001032 DBF_LH(1, "(%s): Unexpected i/o error %li\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001033 dev_name(&cdev->dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 PTR_ERR(irb));
1035 }
1036 return;
1037 }
1038
Stefan Bader41117962005-07-27 11:45:04 -07001039 /*
1040 * If the condition code is not zero and the start function bit is
1041 * still set, this is an deferred error and the last start I/O did
Stefan Bader842d3fb2006-03-24 03:15:26 -08001042 * not succeed. At this point the condition that caused the deferred
1043 * error might still apply. So we just schedule the request to be
1044 * started later.
Stefan Bader41117962005-07-27 11:45:04 -07001045 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001046 if (irb->scsw.cmd.cc != 0 &&
1047 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
Michael Holzheu5f384332006-03-24 03:15:28 -08001048 (request->status == TAPE_REQUEST_IN_IO)) {
1049 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001050 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
Stefan Bader842d3fb2006-03-24 03:15:26 -08001051 request->status = TAPE_REQUEST_QUEUED;
Michael Holzheu5f384332006-03-24 03:15:28 -08001052 schedule_delayed_work(&device->tape_dnr, HZ);
Stefan Bader41117962005-07-27 11:45:04 -07001053 return;
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* May be an unsolicited irq */
1057 if(request != NULL)
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001058 request->rescnt = irb->scsw.cmd.count;
1059 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
Michael Holzheucced1dd2007-02-05 21:18:26 +01001060 !list_empty(&device->req_queue)) {
1061 /* Not Ready to Ready after long busy ? */
1062 struct tape_request *req;
1063 req = list_entry(device->req_queue.next,
1064 struct tape_request, list);
1065 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1066 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1067 if (del_timer(&device->lb_timeout)) {
1068 device->lb_timeout.data = (unsigned long)
1069 tape_put_device(device);
1070 __tape_start_next_request(device);
1071 }
1072 return;
1073 }
1074 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001075 if (irb->scsw.cmd.dstat != 0x0c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* Set the 'ONLINE' flag depending on sense byte 1 */
1077 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1078 device->tape_generic_status |= GMT_ONLINE(~0);
1079 else
1080 device->tape_generic_status &= ~GMT_ONLINE(~0);
1081
1082 /*
1083 * Any request that does not come back with channel end
1084 * and device end is unusual. Log the sense data.
1085 */
1086 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1087 tape_dump_sense_dbf(device, request, irb);
1088 } else {
1089 /* Upon normal completion the device _is_ online */
1090 device->tape_generic_status |= GMT_ONLINE(~0);
1091 }
1092 if (device->tape_state == TS_NOT_OPER) {
1093 DBF_EVENT(6, "tape:device is not operational\n");
1094 return;
1095 }
1096
1097 /*
1098 * Request that were canceled still come back with an interrupt.
1099 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1100 */
1101 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
Stefan Bader41117962005-07-27 11:45:04 -07001102 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
1105
1106 rc = device->discipline->irq(device, request, irb);
1107 /*
1108 * rc < 0 : request finished unsuccessfully.
1109 * rc == TAPE_IO_SUCCESS: request finished successfully.
1110 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1111 * rc == TAPE_IO_RETRY: request finished but needs another go.
1112 * rc == TAPE_IO_STOP: request needs to get terminated.
1113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 switch (rc) {
Stefan Bader41117962005-07-27 11:45:04 -07001115 case TAPE_IO_SUCCESS:
1116 /* Upon normal completion the device _is_ online */
1117 device->tape_generic_status |= GMT_ONLINE(~0);
1118 __tape_end_request(device, request, rc);
1119 break;
1120 case TAPE_IO_PENDING:
1121 break;
Michael Holzheucced1dd2007-02-05 21:18:26 +01001122 case TAPE_IO_LONG_BUSY:
1123 device->lb_timeout.data =
1124 (unsigned long)tape_get_device_reference(device);
1125 device->lb_timeout.expires = jiffies +
1126 LONG_BUSY_TIMEOUT * HZ;
1127 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1128 add_timer(&device->lb_timeout);
1129 request->status = TAPE_REQUEST_LONG_BUSY;
1130 break;
Stefan Bader41117962005-07-27 11:45:04 -07001131 case TAPE_IO_RETRY:
1132 rc = __tape_start_io(device, request);
1133 if (rc)
1134 __tape_end_request(device, request, rc);
1135 break;
1136 case TAPE_IO_STOP:
1137 rc = __tape_cancel_io(device, request);
1138 if (rc)
1139 __tape_end_request(device, request, rc);
1140 break;
1141 default:
1142 if (rc > 0) {
1143 DBF_EVENT(6, "xunknownrc\n");
Stefan Bader41117962005-07-27 11:45:04 -07001144 __tape_end_request(device, request, -EIO);
1145 } else {
1146 __tape_end_request(device, request, rc);
1147 }
1148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150}
1151
1152/*
1153 * Tape device open function used by tape_char & tape_block frontends.
1154 */
1155int
1156tape_open(struct tape_device *device)
1157{
1158 int rc;
1159
Frank Munzertb3c21e42008-10-28 11:10:19 +01001160 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (device->tape_state == TS_NOT_OPER) {
1162 DBF_EVENT(6, "TAPE:nodev\n");
1163 rc = -ENODEV;
1164 } else if (device->tape_state == TS_IN_USE) {
1165 DBF_EVENT(6, "TAPE:dbusy\n");
1166 rc = -EBUSY;
1167 } else if (device->tape_state == TS_BLKUSE) {
1168 DBF_EVENT(6, "TAPE:dbusy\n");
1169 rc = -EBUSY;
1170 } else if (device->discipline != NULL &&
1171 !try_module_get(device->discipline->owner)) {
1172 DBF_EVENT(6, "TAPE:nodisc\n");
1173 rc = -ENODEV;
1174 } else {
1175 tape_state_set(device, TS_IN_USE);
1176 rc = 0;
1177 }
Frank Munzertb3c21e42008-10-28 11:10:19 +01001178 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return rc;
1180}
1181
1182/*
1183 * Tape device release function used by tape_char & tape_block frontends.
1184 */
1185int
1186tape_release(struct tape_device *device)
1187{
Frank Munzertb3c21e42008-10-28 11:10:19 +01001188 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (device->tape_state == TS_IN_USE)
1190 tape_state_set(device, TS_UNUSED);
1191 module_put(device->discipline->owner);
Frank Munzertb3c21e42008-10-28 11:10:19 +01001192 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return 0;
1194}
1195
1196/*
1197 * Execute a magnetic tape command a number of times.
1198 */
1199int
1200tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1201{
1202 tape_mtop_fn fn;
1203 int rc;
1204
1205 DBF_EVENT(6, "TAPE:mtio\n");
1206 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1207 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1208
1209 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1210 return -EINVAL;
1211 fn = device->discipline->mtop_array[mt_op];
1212 if (fn == NULL)
1213 return -EINVAL;
1214
1215 /* We assume that the backends can handle count up to 500. */
1216 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1217 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1218 rc = 0;
1219 for (; mt_count > 500; mt_count -= 500)
1220 if ((rc = fn(device, 500)) != 0)
1221 break;
1222 if (rc == 0)
1223 rc = fn(device, mt_count);
1224 } else
1225 rc = fn(device, mt_count);
1226 return rc;
1227
1228}
1229
1230/*
1231 * Tape init function.
1232 */
1233static int
1234tape_init (void)
1235{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001236 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1238#ifdef DBF_LIKE_HELL
1239 debug_set_level(TAPE_DBF_AREA, 6);
1240#endif
Heiko Carstense018ba12006-02-01 03:06:31 -08001241 DBF_EVENT(3, "tape init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 tape_proc_init();
1243 tapechar_init ();
1244 tapeblock_init ();
1245 return 0;
1246}
1247
1248/*
1249 * Tape exit function.
1250 */
1251static void
1252tape_exit(void)
1253{
1254 DBF_EVENT(6, "tape exit\n");
1255
1256 /* Get rid of the frontends */
1257 tapechar_exit();
1258 tapeblock_exit();
1259 tape_proc_cleanup();
1260 debug_unregister (TAPE_DBF_AREA);
1261}
1262
1263MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1264 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
Heiko Carstense018ba12006-02-01 03:06:31 -08001265MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266MODULE_LICENSE("GPL");
1267
1268module_init(tape_init);
1269module_exit(tape_exit);
1270
1271EXPORT_SYMBOL(tape_generic_remove);
1272EXPORT_SYMBOL(tape_generic_probe);
1273EXPORT_SYMBOL(tape_generic_online);
1274EXPORT_SYMBOL(tape_generic_offline);
1275EXPORT_SYMBOL(tape_put_device);
1276EXPORT_SYMBOL(tape_get_device_reference);
1277EXPORT_SYMBOL(tape_state_verbose);
1278EXPORT_SYMBOL(tape_op_verbose);
1279EXPORT_SYMBOL(tape_state_set);
1280EXPORT_SYMBOL(tape_med_state_set);
1281EXPORT_SYMBOL(tape_alloc_request);
1282EXPORT_SYMBOL(tape_free_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283EXPORT_SYMBOL(tape_dump_sense_dbf);
1284EXPORT_SYMBOL(tape_do_io);
1285EXPORT_SYMBOL(tape_do_io_async);
1286EXPORT_SYMBOL(tape_do_io_interruptible);
Michael Holzheu5f384332006-03-24 03:15:28 -08001287EXPORT_SYMBOL(tape_cancel_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288EXPORT_SYMBOL(tape_mtop);