blob: 08c09d3503cfd5857de8b205d7f561a86f76dada [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
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100627 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
630
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800631 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (request == NULL) {
633 DBF_EXCEPTION(1, "cqra nomem\n");
634 return ERR_PTR(-ENOMEM);
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* allocate channel program */
637 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800638 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 GFP_ATOMIC | GFP_DMA);
640 if (request->cpaddr == NULL) {
641 DBF_EXCEPTION(1, "cqra nomem\n");
642 kfree(request);
643 return ERR_PTR(-ENOMEM);
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 /* alloc small kernel buffer */
647 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800648 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (request->cpdata == NULL) {
650 DBF_EXCEPTION(1, "cqra nomem\n");
Jesper Juhl17fd6822005-11-07 01:01:30 -0800651 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 kfree(request);
653 return ERR_PTR(-ENOMEM);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
657 request->cpdata);
658
659 return request;
660}
661
662/*
663 * Free tape ccw request
664 */
665void
666tape_free_request (struct tape_request * request)
667{
668 DBF_LH(6, "Free request %p\n", request);
669
670 if (request->device != NULL) {
671 request->device = tape_put_device(request->device);
672 }
Jesper Juhl17fd6822005-11-07 01:01:30 -0800673 kfree(request->cpdata);
674 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 kfree(request);
676}
677
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100678static int
Stefan Bader41117962005-07-27 11:45:04 -0700679__tape_start_io(struct tape_device *device, struct tape_request *request)
680{
681 int rc;
682
683#ifdef CONFIG_S390_TAPE_BLOCK
684 if (request->op == TO_BLOCK)
685 device->discipline->check_locate(device, request);
686#endif
687 rc = ccw_device_start(
688 device->cdev,
689 request->cpaddr,
690 (unsigned long) request,
691 0x00,
692 request->options
693 );
694 if (rc == 0) {
695 request->status = TAPE_REQUEST_IN_IO;
696 } else if (rc == -EBUSY) {
697 /* The common I/O subsystem is currently busy. Retry later. */
698 request->status = TAPE_REQUEST_QUEUED;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100699 schedule_delayed_work(&device->tape_dnr, 0);
Stefan Bader41117962005-07-27 11:45:04 -0700700 rc = 0;
701 } else {
702 /* Start failed. Remove request and indicate failure. */
703 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
704 }
705 return rc;
706}
707
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100708static void
Stefan Bader41117962005-07-27 11:45:04 -0700709__tape_start_next_request(struct tape_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct list_head *l, *n;
712 struct tape_request *request;
713 int rc;
714
Stefan Bader41117962005-07-27 11:45:04 -0700715 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * Try to start each request on request queue until one is
718 * started successful.
719 */
720 list_for_each_safe(l, n, &device->req_queue) {
721 request = list_entry(l, struct tape_request, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Stefan Bader41117962005-07-27 11:45:04 -0700723 /*
724 * Avoid race condition if bottom-half was triggered more than
725 * once.
726 */
727 if (request->status == TAPE_REQUEST_IN_IO)
728 return;
Michael Holzheu5f384332006-03-24 03:15:28 -0800729 /*
730 * Request has already been stopped. We have to wait until
731 * the request is removed from the queue in the interrupt
732 * handling.
733 */
734 if (request->status == TAPE_REQUEST_DONE)
735 return;
Stefan Bader41117962005-07-27 11:45:04 -0700736
737 /*
738 * We wanted to cancel the request but the common I/O layer
739 * was busy at that time. This can only happen if this
740 * function is called by delayed_next_request.
741 * Otherwise we start the next request on the queue.
742 */
743 if (request->status == TAPE_REQUEST_CANCEL) {
744 rc = __tape_cancel_io(device, request);
745 } else {
746 rc = __tape_start_io(device, request);
747 }
748 if (rc == 0)
749 return;
750
751 /* Set ending status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 request->rc = rc;
753 request->status = TAPE_REQUEST_DONE;
Stefan Bader41117962005-07-27 11:45:04 -0700754
755 /* Remove from request queue. */
756 list_del(&request->list);
757
758 /* Do callback. */
759 if (request->callback != NULL)
760 request->callback(request, request->callback_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762}
763
764static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100765tape_delayed_next_request(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100767 struct tape_device *device =
768 container_of(work, struct tape_device, tape_dnr.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Stefan Bader41117962005-07-27 11:45:04 -0700770 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
771 spin_lock_irq(get_ccwdev_lock(device->cdev));
772 __tape_start_next_request(device);
773 spin_unlock_irq(get_ccwdev_lock(device->cdev));
774}
775
Michael Holzheucced1dd2007-02-05 21:18:26 +0100776static void tape_long_busy_timeout(unsigned long data)
777{
778 struct tape_request *request;
779 struct tape_device *device;
780
781 device = (struct tape_device *) data;
782 spin_lock_irq(get_ccwdev_lock(device->cdev));
783 request = list_entry(device->req_queue.next, struct tape_request, list);
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100784 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100785 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
786 __tape_start_next_request(device);
787 device->lb_timeout.data = (unsigned long) tape_put_device(device);
788 spin_unlock_irq(get_ccwdev_lock(device->cdev));
789}
790
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100791static void
Stefan Bader41117962005-07-27 11:45:04 -0700792__tape_end_request(
793 struct tape_device * device,
794 struct tape_request * request,
795 int rc)
796{
797 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
798 if (request) {
799 request->rc = rc;
800 request->status = TAPE_REQUEST_DONE;
801
802 /* Remove from request queue. */
803 list_del(&request->list);
804
805 /* Do callback. */
806 if (request->callback != NULL)
807 request->callback(request, request->callback_data);
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* Start next request. */
811 if (!list_empty(&device->req_queue))
Stefan Bader41117962005-07-27 11:45:04 -0700812 __tape_start_next_request(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * Write sense data to dbf
817 */
818void
819tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
820 struct irb *irb)
821{
822 unsigned int *sptr;
823 const char* op;
824
825 if (request != NULL)
826 op = tape_op_verbose[request->op];
827 else
828 op = "---";
829 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200830 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
832 sptr = (unsigned int *) irb->ecw;
833 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
834 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
835 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
836 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
837}
838
839/*
840 * I/O helper function. Adds the request to the request queue
841 * and starts it if the tape is idle. Has to be called with
842 * the device lock held.
843 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100844static int
Stefan Bader41117962005-07-27 11:45:04 -0700845__tape_start_request(struct tape_device *device, struct tape_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 int rc;
848
849 switch (request->op) {
850 case TO_MSEN:
851 case TO_ASSIGN:
852 case TO_UNASSIGN:
853 case TO_READ_ATTMSG:
Michael Holzheue2963062007-05-04 18:47:53 +0200854 case TO_RDC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (device->tape_state == TS_INIT)
856 break;
857 if (device->tape_state == TS_UNUSED)
858 break;
859 default:
860 if (device->tape_state == TS_BLKUSE)
861 break;
862 if (device->tape_state != TS_IN_USE)
863 return -ENODEV;
864 }
865
866 /* Increase use count of device for the added request. */
867 request->device = tape_get_device_reference(device);
868
869 if (list_empty(&device->req_queue)) {
870 /* No other requests are on the queue. Start this one. */
Stefan Bader41117962005-07-27 11:45:04 -0700871 rc = __tape_start_io(device, request);
872 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return rc;
Stefan Bader41117962005-07-27 11:45:04 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 DBF_LH(5, "Request %p added for execution.\n", request);
876 list_add(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 } else {
878 DBF_LH(5, "Request %p add to queue.\n", request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 request->status = TAPE_REQUEST_QUEUED;
Stefan Bader41117962005-07-27 11:45:04 -0700880 list_add_tail(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882 return 0;
883}
884
885/*
886 * Add the request to the request queue, try to start it if the
887 * tape is idle. Return without waiting for end of i/o.
888 */
889int
890tape_do_io_async(struct tape_device *device, struct tape_request *request)
891{
892 int rc;
893
894 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
895
896 spin_lock_irq(get_ccwdev_lock(device->cdev));
897 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -0700898 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 spin_unlock_irq(get_ccwdev_lock(device->cdev));
900 return rc;
901}
902
903/*
904 * tape_do_io/__tape_wake_up
905 * Add the request to the request queue, try to start it if the
906 * tape is idle and wait uninterruptible for its completion.
907 */
908static void
909__tape_wake_up(struct tape_request *request, void *data)
910{
911 request->callback = NULL;
912 wake_up((wait_queue_head_t *) data);
913}
914
915int
916tape_do_io(struct tape_device *device, struct tape_request *request)
917{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int rc;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 spin_lock_irq(get_ccwdev_lock(device->cdev));
921 /* Setup callback */
922 request->callback = __tape_wake_up;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200923 request->callback_data = &device->wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -0700925 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 spin_unlock_irq(get_ccwdev_lock(device->cdev));
927 if (rc)
928 return rc;
929 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200930 wait_event(device->wait_queue, (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Get rc from request */
932 return request->rc;
933}
934
935/*
936 * tape_do_io_interruptible/__tape_wake_up_interruptible
937 * Add the request to the request queue, try to start it if the
938 * tape is idle and wait uninterruptible for its completion.
939 */
940static void
941__tape_wake_up_interruptible(struct tape_request *request, void *data)
942{
943 request->callback = NULL;
944 wake_up_interruptible((wait_queue_head_t *) data);
945}
946
947int
948tape_do_io_interruptible(struct tape_device *device,
949 struct tape_request *request)
950{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int rc;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 spin_lock_irq(get_ccwdev_lock(device->cdev));
954 /* Setup callback */
955 request->callback = __tape_wake_up_interruptible;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200956 request->callback_data = &device->wait_queue;
Stefan Bader41117962005-07-27 11:45:04 -0700957 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 spin_unlock_irq(get_ccwdev_lock(device->cdev));
959 if (rc)
960 return rc;
961 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200962 rc = wait_event_interruptible(device->wait_queue,
963 (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (rc != -ERESTARTSYS)
965 /* Request finished normally. */
966 return request->rc;
Stefan Bader41117962005-07-27 11:45:04 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* Interrupted by a signal. We have to stop the current request. */
969 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Bader41117962005-07-27 11:45:04 -0700970 rc = __tape_cancel_io(device, request);
971 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (rc == 0) {
Stefan Bader41117962005-07-27 11:45:04 -0700973 /* Wait for the interrupt that acknowledges the halt. */
974 do {
975 rc = wait_event_interruptible(
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200976 device->wait_queue,
Stefan Bader41117962005-07-27 11:45:04 -0700977 (request->callback == NULL)
978 );
Michael Holzheu4cd190a2006-03-24 03:15:27 -0800979 } while (rc == -ERESTARTSYS);
Stefan Bader41117962005-07-27 11:45:04 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
982 rc = -ERESTARTSYS;
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return rc;
985}
986
987/*
Michael Holzheu5f384332006-03-24 03:15:28 -0800988 * Stop running ccw.
989 */
990int
991tape_cancel_io(struct tape_device *device, struct tape_request *request)
992{
993 int rc;
994
995 spin_lock_irq(get_ccwdev_lock(device->cdev));
996 rc = __tape_cancel_io(device, request);
997 spin_unlock_irq(get_ccwdev_lock(device->cdev));
998 return rc;
999}
1000
1001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * Tape interrupt routine, called from the ccw_device layer
1003 */
1004static void
1005__tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1006{
1007 struct tape_device *device;
1008 struct tape_request *request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int rc;
1010
1011 device = (struct tape_device *) cdev->dev.driver_data;
1012 if (device == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return;
1014 }
1015 request = (struct tape_request *) intparm;
1016
1017 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1018
1019 /* On special conditions irb is an error pointer */
1020 if (IS_ERR(irb)) {
Stefan Bader41117962005-07-27 11:45:04 -07001021 /* FIXME: What to do with the request? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 switch (PTR_ERR(irb)) {
1023 case -ETIMEDOUT:
Carsten Otteab640db2009-03-26 15:24:38 +01001024 DBF_LH(1, "(%s): Request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001025 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 case -EIO:
Stefan Bader41117962005-07-27 11:45:04 -07001027 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
1029 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001030 DBF_LH(1, "(%s): Unexpected i/o error %li\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001031 dev_name(&cdev->dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 PTR_ERR(irb));
1033 }
1034 return;
1035 }
1036
Stefan Bader41117962005-07-27 11:45:04 -07001037 /*
1038 * If the condition code is not zero and the start function bit is
1039 * still set, this is an deferred error and the last start I/O did
Stefan Bader842d3fb2006-03-24 03:15:26 -08001040 * not succeed. At this point the condition that caused the deferred
1041 * error might still apply. So we just schedule the request to be
1042 * started later.
Stefan Bader41117962005-07-27 11:45:04 -07001043 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001044 if (irb->scsw.cmd.cc != 0 &&
1045 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
Michael Holzheu5f384332006-03-24 03:15:28 -08001046 (request->status == TAPE_REQUEST_IN_IO)) {
1047 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001048 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
Stefan Bader842d3fb2006-03-24 03:15:26 -08001049 request->status = TAPE_REQUEST_QUEUED;
Michael Holzheu5f384332006-03-24 03:15:28 -08001050 schedule_delayed_work(&device->tape_dnr, HZ);
Stefan Bader41117962005-07-27 11:45:04 -07001051 return;
1052 }
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* May be an unsolicited irq */
1055 if(request != NULL)
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001056 request->rescnt = irb->scsw.cmd.count;
1057 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
Michael Holzheucced1dd2007-02-05 21:18:26 +01001058 !list_empty(&device->req_queue)) {
1059 /* Not Ready to Ready after long busy ? */
1060 struct tape_request *req;
1061 req = list_entry(device->req_queue.next,
1062 struct tape_request, list);
1063 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1064 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1065 if (del_timer(&device->lb_timeout)) {
1066 device->lb_timeout.data = (unsigned long)
1067 tape_put_device(device);
1068 __tape_start_next_request(device);
1069 }
1070 return;
1071 }
1072 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001073 if (irb->scsw.cmd.dstat != 0x0c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Set the 'ONLINE' flag depending on sense byte 1 */
1075 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1076 device->tape_generic_status |= GMT_ONLINE(~0);
1077 else
1078 device->tape_generic_status &= ~GMT_ONLINE(~0);
1079
1080 /*
1081 * Any request that does not come back with channel end
1082 * and device end is unusual. Log the sense data.
1083 */
1084 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1085 tape_dump_sense_dbf(device, request, irb);
1086 } else {
1087 /* Upon normal completion the device _is_ online */
1088 device->tape_generic_status |= GMT_ONLINE(~0);
1089 }
1090 if (device->tape_state == TS_NOT_OPER) {
1091 DBF_EVENT(6, "tape:device is not operational\n");
1092 return;
1093 }
1094
1095 /*
1096 * Request that were canceled still come back with an interrupt.
1097 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1098 */
1099 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
Stefan Bader41117962005-07-27 11:45:04 -07001100 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return;
1102 }
1103
1104 rc = device->discipline->irq(device, request, irb);
1105 /*
1106 * rc < 0 : request finished unsuccessfully.
1107 * rc == TAPE_IO_SUCCESS: request finished successfully.
1108 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1109 * rc == TAPE_IO_RETRY: request finished but needs another go.
1110 * rc == TAPE_IO_STOP: request needs to get terminated.
1111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 switch (rc) {
Stefan Bader41117962005-07-27 11:45:04 -07001113 case TAPE_IO_SUCCESS:
1114 /* Upon normal completion the device _is_ online */
1115 device->tape_generic_status |= GMT_ONLINE(~0);
1116 __tape_end_request(device, request, rc);
1117 break;
1118 case TAPE_IO_PENDING:
1119 break;
Michael Holzheucced1dd2007-02-05 21:18:26 +01001120 case TAPE_IO_LONG_BUSY:
1121 device->lb_timeout.data =
1122 (unsigned long)tape_get_device_reference(device);
1123 device->lb_timeout.expires = jiffies +
1124 LONG_BUSY_TIMEOUT * HZ;
1125 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1126 add_timer(&device->lb_timeout);
1127 request->status = TAPE_REQUEST_LONG_BUSY;
1128 break;
Stefan Bader41117962005-07-27 11:45:04 -07001129 case TAPE_IO_RETRY:
1130 rc = __tape_start_io(device, request);
1131 if (rc)
1132 __tape_end_request(device, request, rc);
1133 break;
1134 case TAPE_IO_STOP:
1135 rc = __tape_cancel_io(device, request);
1136 if (rc)
1137 __tape_end_request(device, request, rc);
1138 break;
1139 default:
1140 if (rc > 0) {
1141 DBF_EVENT(6, "xunknownrc\n");
Stefan Bader41117962005-07-27 11:45:04 -07001142 __tape_end_request(device, request, -EIO);
1143 } else {
1144 __tape_end_request(device, request, rc);
1145 }
1146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148}
1149
1150/*
1151 * Tape device open function used by tape_char & tape_block frontends.
1152 */
1153int
1154tape_open(struct tape_device *device)
1155{
1156 int rc;
1157
Frank Munzertb3c21e42008-10-28 11:10:19 +01001158 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (device->tape_state == TS_NOT_OPER) {
1160 DBF_EVENT(6, "TAPE:nodev\n");
1161 rc = -ENODEV;
1162 } else if (device->tape_state == TS_IN_USE) {
1163 DBF_EVENT(6, "TAPE:dbusy\n");
1164 rc = -EBUSY;
1165 } else if (device->tape_state == TS_BLKUSE) {
1166 DBF_EVENT(6, "TAPE:dbusy\n");
1167 rc = -EBUSY;
1168 } else if (device->discipline != NULL &&
1169 !try_module_get(device->discipline->owner)) {
1170 DBF_EVENT(6, "TAPE:nodisc\n");
1171 rc = -ENODEV;
1172 } else {
1173 tape_state_set(device, TS_IN_USE);
1174 rc = 0;
1175 }
Frank Munzertb3c21e42008-10-28 11:10:19 +01001176 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return rc;
1178}
1179
1180/*
1181 * Tape device release function used by tape_char & tape_block frontends.
1182 */
1183int
1184tape_release(struct tape_device *device)
1185{
Frank Munzertb3c21e42008-10-28 11:10:19 +01001186 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (device->tape_state == TS_IN_USE)
1188 tape_state_set(device, TS_UNUSED);
1189 module_put(device->discipline->owner);
Frank Munzertb3c21e42008-10-28 11:10:19 +01001190 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
1194/*
1195 * Execute a magnetic tape command a number of times.
1196 */
1197int
1198tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1199{
1200 tape_mtop_fn fn;
1201 int rc;
1202
1203 DBF_EVENT(6, "TAPE:mtio\n");
1204 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1205 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1206
1207 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1208 return -EINVAL;
1209 fn = device->discipline->mtop_array[mt_op];
1210 if (fn == NULL)
1211 return -EINVAL;
1212
1213 /* We assume that the backends can handle count up to 500. */
1214 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1215 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1216 rc = 0;
1217 for (; mt_count > 500; mt_count -= 500)
1218 if ((rc = fn(device, 500)) != 0)
1219 break;
1220 if (rc == 0)
1221 rc = fn(device, mt_count);
1222 } else
1223 rc = fn(device, mt_count);
1224 return rc;
1225
1226}
1227
1228/*
1229 * Tape init function.
1230 */
1231static int
1232tape_init (void)
1233{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001234 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1236#ifdef DBF_LIKE_HELL
1237 debug_set_level(TAPE_DBF_AREA, 6);
1238#endif
Heiko Carstense018ba12006-02-01 03:06:31 -08001239 DBF_EVENT(3, "tape init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 tape_proc_init();
1241 tapechar_init ();
1242 tapeblock_init ();
1243 return 0;
1244}
1245
1246/*
1247 * Tape exit function.
1248 */
1249static void
1250tape_exit(void)
1251{
1252 DBF_EVENT(6, "tape exit\n");
1253
1254 /* Get rid of the frontends */
1255 tapechar_exit();
1256 tapeblock_exit();
1257 tape_proc_cleanup();
1258 debug_unregister (TAPE_DBF_AREA);
1259}
1260
1261MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1262 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
Heiko Carstense018ba12006-02-01 03:06:31 -08001263MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264MODULE_LICENSE("GPL");
1265
1266module_init(tape_init);
1267module_exit(tape_exit);
1268
1269EXPORT_SYMBOL(tape_generic_remove);
1270EXPORT_SYMBOL(tape_generic_probe);
1271EXPORT_SYMBOL(tape_generic_online);
1272EXPORT_SYMBOL(tape_generic_offline);
1273EXPORT_SYMBOL(tape_put_device);
1274EXPORT_SYMBOL(tape_get_device_reference);
1275EXPORT_SYMBOL(tape_state_verbose);
1276EXPORT_SYMBOL(tape_op_verbose);
1277EXPORT_SYMBOL(tape_state_set);
1278EXPORT_SYMBOL(tape_med_state_set);
1279EXPORT_SYMBOL(tape_alloc_request);
1280EXPORT_SYMBOL(tape_free_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281EXPORT_SYMBOL(tape_dump_sense_dbf);
1282EXPORT_SYMBOL(tape_do_io);
1283EXPORT_SYMBOL(tape_do_io_async);
1284EXPORT_SYMBOL(tape_do_io_interruptible);
Michael Holzheu5f384332006-03-24 03:15:28 -08001285EXPORT_SYMBOL(tape_cancel_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286EXPORT_SYMBOL(tape_mtop);