blob: 1d420d9475961719ecd625a5fab7c06f5a2713bd [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
Frank Munzert3ef32e622009-06-16 10:30:39 +02006 * Copyright IBM Corp. 2001, 2009
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
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -070095 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 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
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700107 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700119 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 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
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700133 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 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
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700159 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
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/*
Frank Munzert3ef32e622009-06-16 10:30:39 +0200383 * Suspend device.
384 *
385 * Called by the common I/O layer if the drive should be suspended on user
386 * request. We refuse to suspend if the device is loaded or in use for the
387 * following reason:
388 * While the Linux guest is suspended, it might be logged off which causes
389 * devices to be detached. Tape devices are automatically rewound and unloaded
390 * during DETACH processing (unless the tape device was attached with the
391 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
392 * resume the original state of the tape device, since we would need to
393 * manually re-load the cartridge which was active at suspend time.
394 */
395int tape_generic_pm_suspend(struct ccw_device *cdev)
396{
397 struct tape_device *device;
398
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200399 device = dev_get_drvdata(&cdev->dev);
Frank Munzert3ef32e622009-06-16 10:30:39 +0200400 if (!device) {
401 return -ENODEV;
402 }
403
404 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
405 device->cdev_id, device);
406
407 if (device->medium_state != MS_UNLOADED) {
408 pr_err("A cartridge is loaded in tape device %s, "
409 "refusing to suspend\n", dev_name(&cdev->dev));
410 return -EBUSY;
411 }
412
413 spin_lock_irq(get_ccwdev_lock(device->cdev));
414 switch (device->tape_state) {
415 case TS_INIT:
416 case TS_NOT_OPER:
417 case TS_UNUSED:
418 spin_unlock_irq(get_ccwdev_lock(device->cdev));
419 break;
420 default:
421 pr_err("Tape device %s is busy, refusing to "
422 "suspend\n", dev_name(&cdev->dev));
423 spin_unlock_irq(get_ccwdev_lock(device->cdev));
424 return -EBUSY;
425 }
426
427 DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
428 return 0;
429}
430
431/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * Set device offline.
433 *
434 * Called by the common I/O layer if the drive should set offline on user
435 * request. We may prevent this by returning an error.
436 * Manual offline is only allowed while the drive is not in use.
437 */
438int
Frank Munzert4d7a3cd2009-04-23 13:58:09 +0200439tape_generic_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Frank Munzert4d7a3cd2009-04-23 13:58:09 +0200441 struct tape_device *device;
442
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700443 device = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -ENODEV;
446 }
447
448 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
449 device->cdev_id, device);
450
451 spin_lock_irq(get_ccwdev_lock(device->cdev));
452 switch (device->tape_state) {
453 case TS_INIT:
454 case TS_NOT_OPER:
455 spin_unlock_irq(get_ccwdev_lock(device->cdev));
456 break;
457 case TS_UNUSED:
458 tape_state_set(device, TS_INIT);
459 spin_unlock_irq(get_ccwdev_lock(device->cdev));
460 tape_cleanup_device(device);
461 break;
462 default:
463 DBF_EVENT(3, "(%08x): Set offline failed "
464 "- drive in use.\n",
465 device->cdev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 spin_unlock_irq(get_ccwdev_lock(device->cdev));
467 return -EBUSY;
468 }
469
470 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
471 return 0;
472}
473
474/*
475 * Allocate memory for a new device structure.
476 */
477static struct tape_device *
478tape_alloc_device(void)
479{
480 struct tape_device *device;
481
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800482 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (device == NULL) {
484 DBF_EXCEPTION(2, "ti:no mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return ERR_PTR(-ENOMEM);
486 }
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800487 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (device->modeset_byte == NULL) {
489 DBF_EXCEPTION(2, "ti:no mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 kfree(device);
491 return ERR_PTR(-ENOMEM);
492 }
493 INIT_LIST_HEAD(&device->req_queue);
494 INIT_LIST_HEAD(&device->node);
495 init_waitqueue_head(&device->state_change_wq);
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200496 init_waitqueue_head(&device->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 device->tape_state = TS_INIT;
498 device->medium_state = MS_UNKNOWN;
499 *device->modeset_byte = 0;
500 device->first_minor = -1;
501 atomic_set(&device->ref_count, 1);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100502 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return device;
505}
506
507/*
508 * Get a reference to an existing device structure. This will automatically
509 * increment the reference count.
510 */
511struct tape_device *
512tape_get_device_reference(struct tape_device *device)
513{
514 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
515 atomic_inc_return(&device->ref_count));
516
517 return device;
518}
519
520/*
521 * Decrease the reference counter of a devices structure. If the
522 * reference counter reaches zero free the device structure.
523 * The function returns a NULL pointer to be used by the caller
524 * for clearing reference pointers.
525 */
526struct tape_device *
527tape_put_device(struct tape_device *device)
528{
529 int remain;
530
531 remain = atomic_dec_return(&device->ref_count);
532 if (remain > 0) {
533 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
534 } else {
535 if (remain < 0) {
536 DBF_EVENT(4, "put device without reference\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else {
538 DBF_EVENT(4, "tape_free_device(%p)\n", device);
539 kfree(device->modeset_byte);
540 kfree(device);
541 }
542 }
543
544 return NULL;
545}
546
547/*
548 * Find tape device by a device index.
549 */
550struct tape_device *
551tape_get_device(int devindex)
552{
553 struct tape_device *device, *tmp;
554
555 device = ERR_PTR(-ENODEV);
556 read_lock(&tape_device_lock);
557 list_for_each_entry(tmp, &tape_device_list, node) {
558 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
559 device = tape_get_device_reference(tmp);
560 break;
561 }
562 }
563 read_unlock(&tape_device_lock);
564 return device;
565}
566
567/*
568 * Driverfs tape probe function.
569 */
570int
571tape_generic_probe(struct ccw_device *cdev)
572{
573 struct tape_device *device;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200574 int ret;
Cornelia Huckf455adc2008-05-15 16:52:37 +0200575 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 device = tape_alloc_device();
578 if (IS_ERR(device))
579 return -ENODEV;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200580 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
581 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
582 if (ret) {
583 tape_put_device(device);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200584 return ret;
585 }
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700586 dev_set_drvdata(&cdev->dev, device);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200587 cdev->handler = __tape_do_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 device->cdev = cdev;
Cornelia Huckf455adc2008-05-15 16:52:37 +0200589 ccw_device_get_id(cdev, &dev_id);
590 device->cdev_id = devid_to_int(&dev_id);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100594static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595__tape_discard_requests(struct tape_device *device)
596{
597 struct tape_request * request;
598 struct list_head * l, *n;
599
600 list_for_each_safe(l, n, &device->req_queue) {
601 request = list_entry(l, struct tape_request, list);
602 if (request->status == TAPE_REQUEST_IN_IO)
603 request->status = TAPE_REQUEST_DONE;
604 list_del(&request->list);
605
606 /* Decrease ref_count for removed request. */
607 request->device = tape_put_device(device);
608 request->rc = -EIO;
609 if (request->callback != NULL)
610 request->callback(request, request->callback_data);
611 }
612}
613
614/*
615 * Driverfs tape remove function.
616 *
617 * This function is called whenever the common I/O layer detects the device
618 * gone. This can happen at any time and we cannot refuse.
619 */
620void
621tape_generic_remove(struct ccw_device *cdev)
622{
623 struct tape_device * device;
624
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700625 device = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return;
628 }
629 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
630
631 spin_lock_irq(get_ccwdev_lock(device->cdev));
632 switch (device->tape_state) {
633 case TS_INIT:
634 tape_state_set(device, TS_NOT_OPER);
635 case TS_NOT_OPER:
636 /*
637 * Nothing to do.
638 */
639 spin_unlock_irq(get_ccwdev_lock(device->cdev));
640 break;
641 case TS_UNUSED:
642 /*
643 * Need only to release the device.
644 */
645 tape_state_set(device, TS_NOT_OPER);
646 spin_unlock_irq(get_ccwdev_lock(device->cdev));
647 tape_cleanup_device(device);
648 break;
649 default:
650 /*
651 * There may be requests on the queue. We will not get
652 * an interrupt for a request that was running. So we
653 * just post them all as I/O errors.
654 */
655 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
656 device->cdev_id);
Carsten Otteab640db2009-03-26 15:24:38 +0100657 dev_warn(&device->cdev->dev, "A tape unit was detached"
658 " while in use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 tape_state_set(device, TS_NOT_OPER);
660 __tape_discard_requests(device);
661 spin_unlock_irq(get_ccwdev_lock(device->cdev));
662 tape_cleanup_device(device);
663 }
664
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700665 if (!dev_get_drvdata(&cdev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700667 dev_set_drvdata(&cdev->dev, tape_put_device(dev_get_drvdata(&cdev->dev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669}
670
671/*
672 * Allocate a new tape ccw request
673 */
674struct tape_request *
675tape_alloc_request(int cplength, int datasize)
676{
677 struct tape_request *request;
678
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100679 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
682
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800683 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (request == NULL) {
685 DBF_EXCEPTION(1, "cqra nomem\n");
686 return ERR_PTR(-ENOMEM);
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* allocate channel program */
689 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800690 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 GFP_ATOMIC | GFP_DMA);
692 if (request->cpaddr == NULL) {
693 DBF_EXCEPTION(1, "cqra nomem\n");
694 kfree(request);
695 return ERR_PTR(-ENOMEM);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 /* alloc small kernel buffer */
699 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800700 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (request->cpdata == NULL) {
702 DBF_EXCEPTION(1, "cqra nomem\n");
Jesper Juhl17fd6822005-11-07 01:01:30 -0800703 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 kfree(request);
705 return ERR_PTR(-ENOMEM);
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
709 request->cpdata);
710
711 return request;
712}
713
714/*
715 * Free tape ccw request
716 */
717void
718tape_free_request (struct tape_request * request)
719{
720 DBF_LH(6, "Free request %p\n", request);
721
722 if (request->device != NULL) {
723 request->device = tape_put_device(request->device);
724 }
Jesper Juhl17fd6822005-11-07 01:01:30 -0800725 kfree(request->cpdata);
726 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 kfree(request);
728}
729
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100730static int
Stefan Bader41117962005-07-27 11:45:04 -0700731__tape_start_io(struct tape_device *device, struct tape_request *request)
732{
733 int rc;
734
735#ifdef CONFIG_S390_TAPE_BLOCK
736 if (request->op == TO_BLOCK)
737 device->discipline->check_locate(device, request);
738#endif
739 rc = ccw_device_start(
740 device->cdev,
741 request->cpaddr,
742 (unsigned long) request,
743 0x00,
744 request->options
745 );
746 if (rc == 0) {
747 request->status = TAPE_REQUEST_IN_IO;
748 } else if (rc == -EBUSY) {
749 /* The common I/O subsystem is currently busy. Retry later. */
750 request->status = TAPE_REQUEST_QUEUED;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100751 schedule_delayed_work(&device->tape_dnr, 0);
Stefan Bader41117962005-07-27 11:45:04 -0700752 rc = 0;
753 } else {
754 /* Start failed. Remove request and indicate failure. */
755 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
756 }
757 return rc;
758}
759
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100760static void
Stefan Bader41117962005-07-27 11:45:04 -0700761__tape_start_next_request(struct tape_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct list_head *l, *n;
764 struct tape_request *request;
765 int rc;
766
Stefan Bader41117962005-07-27 11:45:04 -0700767 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * Try to start each request on request queue until one is
770 * started successful.
771 */
772 list_for_each_safe(l, n, &device->req_queue) {
773 request = list_entry(l, struct tape_request, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Stefan Bader41117962005-07-27 11:45:04 -0700775 /*
776 * Avoid race condition if bottom-half was triggered more than
777 * once.
778 */
779 if (request->status == TAPE_REQUEST_IN_IO)
780 return;
Michael Holzheu5f384332006-03-24 03:15:28 -0800781 /*
782 * Request has already been stopped. We have to wait until
783 * the request is removed from the queue in the interrupt
784 * handling.
785 */
786 if (request->status == TAPE_REQUEST_DONE)
787 return;
Stefan Bader41117962005-07-27 11:45:04 -0700788
789 /*
790 * We wanted to cancel the request but the common I/O layer
791 * was busy at that time. This can only happen if this
792 * function is called by delayed_next_request.
793 * Otherwise we start the next request on the queue.
794 */
795 if (request->status == TAPE_REQUEST_CANCEL) {
796 rc = __tape_cancel_io(device, request);
797 } else {
798 rc = __tape_start_io(device, request);
799 }
800 if (rc == 0)
801 return;
802
803 /* Set ending status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 request->rc = rc;
805 request->status = TAPE_REQUEST_DONE;
Stefan Bader41117962005-07-27 11:45:04 -0700806
807 /* Remove from request queue. */
808 list_del(&request->list);
809
810 /* Do callback. */
811 if (request->callback != NULL)
812 request->callback(request, request->callback_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814}
815
816static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100817tape_delayed_next_request(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100819 struct tape_device *device =
820 container_of(work, struct tape_device, tape_dnr.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Stefan Bader41117962005-07-27 11:45:04 -0700822 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
823 spin_lock_irq(get_ccwdev_lock(device->cdev));
824 __tape_start_next_request(device);
825 spin_unlock_irq(get_ccwdev_lock(device->cdev));
826}
827
Michael Holzheucced1dd2007-02-05 21:18:26 +0100828static void tape_long_busy_timeout(unsigned long data)
829{
830 struct tape_request *request;
831 struct tape_device *device;
832
833 device = (struct tape_device *) data;
834 spin_lock_irq(get_ccwdev_lock(device->cdev));
835 request = list_entry(device->req_queue.next, struct tape_request, list);
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100836 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100837 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
838 __tape_start_next_request(device);
839 device->lb_timeout.data = (unsigned long) tape_put_device(device);
840 spin_unlock_irq(get_ccwdev_lock(device->cdev));
841}
842
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100843static void
Stefan Bader41117962005-07-27 11:45:04 -0700844__tape_end_request(
845 struct tape_device * device,
846 struct tape_request * request,
847 int rc)
848{
849 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
850 if (request) {
851 request->rc = rc;
852 request->status = TAPE_REQUEST_DONE;
853
854 /* Remove from request queue. */
855 list_del(&request->list);
856
857 /* Do callback. */
858 if (request->callback != NULL)
859 request->callback(request, request->callback_data);
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* Start next request. */
863 if (!list_empty(&device->req_queue))
Stefan Bader41117962005-07-27 11:45:04 -0700864 __tape_start_next_request(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * Write sense data to dbf
869 */
870void
871tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
872 struct irb *irb)
873{
874 unsigned int *sptr;
875 const char* op;
876
877 if (request != NULL)
878 op = tape_op_verbose[request->op];
879 else
880 op = "---";
881 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200882 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
884 sptr = (unsigned int *) irb->ecw;
885 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
886 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
887 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
888 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
889}
890
891/*
892 * I/O helper function. Adds the request to the request queue
893 * and starts it if the tape is idle. Has to be called with
894 * the device lock held.
895 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100896static int
Stefan Bader41117962005-07-27 11:45:04 -0700897__tape_start_request(struct tape_device *device, struct tape_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 int rc;
900
901 switch (request->op) {
902 case TO_MSEN:
903 case TO_ASSIGN:
904 case TO_UNASSIGN:
905 case TO_READ_ATTMSG:
Michael Holzheue2963062007-05-04 18:47:53 +0200906 case TO_RDC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (device->tape_state == TS_INIT)
908 break;
909 if (device->tape_state == TS_UNUSED)
910 break;
911 default:
912 if (device->tape_state == TS_BLKUSE)
913 break;
914 if (device->tape_state != TS_IN_USE)
915 return -ENODEV;
916 }
917
918 /* Increase use count of device for the added request. */
919 request->device = tape_get_device_reference(device);
920
921 if (list_empty(&device->req_queue)) {
922 /* No other requests are on the queue. Start this one. */
Stefan Bader41117962005-07-27 11:45:04 -0700923 rc = __tape_start_io(device, request);
924 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return rc;
Stefan Bader41117962005-07-27 11:45:04 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 DBF_LH(5, "Request %p added for execution.\n", request);
928 list_add(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 } else {
930 DBF_LH(5, "Request %p add to queue.\n", request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 request->status = TAPE_REQUEST_QUEUED;
Stefan Bader41117962005-07-27 11:45:04 -0700932 list_add_tail(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 return 0;
935}
936
937/*
938 * Add the request to the request queue, try to start it if the
939 * tape is idle. Return without waiting for end of i/o.
940 */
941int
942tape_do_io_async(struct tape_device *device, struct tape_request *request)
943{
944 int rc;
945
946 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
947
948 spin_lock_irq(get_ccwdev_lock(device->cdev));
949 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -0700950 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_unlock_irq(get_ccwdev_lock(device->cdev));
952 return rc;
953}
954
955/*
956 * tape_do_io/__tape_wake_up
957 * Add the request to the request queue, try to start it if the
958 * tape is idle and wait uninterruptible for its completion.
959 */
960static void
961__tape_wake_up(struct tape_request *request, void *data)
962{
963 request->callback = NULL;
964 wake_up((wait_queue_head_t *) data);
965}
966
967int
968tape_do_io(struct tape_device *device, struct tape_request *request)
969{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int rc;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 spin_lock_irq(get_ccwdev_lock(device->cdev));
973 /* Setup callback */
974 request->callback = __tape_wake_up;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200975 request->callback_data = &device->wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -0700977 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 spin_unlock_irq(get_ccwdev_lock(device->cdev));
979 if (rc)
980 return rc;
981 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200982 wait_event(device->wait_queue, (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Get rc from request */
984 return request->rc;
985}
986
987/*
988 * tape_do_io_interruptible/__tape_wake_up_interruptible
989 * Add the request to the request queue, try to start it if the
990 * tape is idle and wait uninterruptible for its completion.
991 */
992static void
993__tape_wake_up_interruptible(struct tape_request *request, void *data)
994{
995 request->callback = NULL;
996 wake_up_interruptible((wait_queue_head_t *) data);
997}
998
999int
1000tape_do_io_interruptible(struct tape_device *device,
1001 struct tape_request *request)
1002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int rc;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_lock_irq(get_ccwdev_lock(device->cdev));
1006 /* Setup callback */
1007 request->callback = __tape_wake_up_interruptible;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001008 request->callback_data = &device->wait_queue;
Stefan Bader41117962005-07-27 11:45:04 -07001009 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1011 if (rc)
1012 return rc;
1013 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001014 rc = wait_event_interruptible(device->wait_queue,
1015 (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (rc != -ERESTARTSYS)
1017 /* Request finished normally. */
1018 return request->rc;
Stefan Bader41117962005-07-27 11:45:04 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* Interrupted by a signal. We have to stop the current request. */
1021 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Bader41117962005-07-27 11:45:04 -07001022 rc = __tape_cancel_io(device, request);
1023 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (rc == 0) {
Stefan Bader41117962005-07-27 11:45:04 -07001025 /* Wait for the interrupt that acknowledges the halt. */
1026 do {
1027 rc = wait_event_interruptible(
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001028 device->wait_queue,
Stefan Bader41117962005-07-27 11:45:04 -07001029 (request->callback == NULL)
1030 );
Michael Holzheu4cd190a2006-03-24 03:15:27 -08001031 } while (rc == -ERESTARTSYS);
Stefan Bader41117962005-07-27 11:45:04 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1034 rc = -ERESTARTSYS;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return rc;
1037}
1038
1039/*
Michael Holzheu5f384332006-03-24 03:15:28 -08001040 * Stop running ccw.
1041 */
1042int
1043tape_cancel_io(struct tape_device *device, struct tape_request *request)
1044{
1045 int rc;
1046
1047 spin_lock_irq(get_ccwdev_lock(device->cdev));
1048 rc = __tape_cancel_io(device, request);
1049 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1050 return rc;
1051}
1052
1053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * Tape interrupt routine, called from the ccw_device layer
1055 */
1056static void
1057__tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1058{
1059 struct tape_device *device;
1060 struct tape_request *request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int rc;
1062
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001063 device = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (device == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return;
1066 }
1067 request = (struct tape_request *) intparm;
1068
1069 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1070
1071 /* On special conditions irb is an error pointer */
1072 if (IS_ERR(irb)) {
Stefan Bader41117962005-07-27 11:45:04 -07001073 /* FIXME: What to do with the request? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 switch (PTR_ERR(irb)) {
1075 case -ETIMEDOUT:
Carsten Otteab640db2009-03-26 15:24:38 +01001076 DBF_LH(1, "(%s): Request timed out\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001077 dev_name(&cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 case -EIO:
Stefan Bader41117962005-07-27 11:45:04 -07001079 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001082 DBF_LH(1, "(%s): Unexpected i/o error %li\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +02001083 dev_name(&cdev->dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 PTR_ERR(irb));
1085 }
1086 return;
1087 }
1088
Stefan Bader41117962005-07-27 11:45:04 -07001089 /*
1090 * If the condition code is not zero and the start function bit is
1091 * still set, this is an deferred error and the last start I/O did
Stefan Bader842d3fb2006-03-24 03:15:26 -08001092 * not succeed. At this point the condition that caused the deferred
1093 * error might still apply. So we just schedule the request to be
1094 * started later.
Stefan Bader41117962005-07-27 11:45:04 -07001095 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001096 if (irb->scsw.cmd.cc != 0 &&
1097 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
Michael Holzheu5f384332006-03-24 03:15:28 -08001098 (request->status == TAPE_REQUEST_IN_IO)) {
1099 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001100 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
Stefan Bader842d3fb2006-03-24 03:15:26 -08001101 request->status = TAPE_REQUEST_QUEUED;
Michael Holzheu5f384332006-03-24 03:15:28 -08001102 schedule_delayed_work(&device->tape_dnr, HZ);
Stefan Bader41117962005-07-27 11:45:04 -07001103 return;
1104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* May be an unsolicited irq */
1107 if(request != NULL)
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001108 request->rescnt = irb->scsw.cmd.count;
1109 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
Michael Holzheucced1dd2007-02-05 21:18:26 +01001110 !list_empty(&device->req_queue)) {
1111 /* Not Ready to Ready after long busy ? */
1112 struct tape_request *req;
1113 req = list_entry(device->req_queue.next,
1114 struct tape_request, list);
1115 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1116 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1117 if (del_timer(&device->lb_timeout)) {
1118 device->lb_timeout.data = (unsigned long)
1119 tape_put_device(device);
1120 __tape_start_next_request(device);
1121 }
1122 return;
1123 }
1124 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001125 if (irb->scsw.cmd.dstat != 0x0c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* Set the 'ONLINE' flag depending on sense byte 1 */
1127 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1128 device->tape_generic_status |= GMT_ONLINE(~0);
1129 else
1130 device->tape_generic_status &= ~GMT_ONLINE(~0);
1131
1132 /*
1133 * Any request that does not come back with channel end
1134 * and device end is unusual. Log the sense data.
1135 */
1136 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1137 tape_dump_sense_dbf(device, request, irb);
1138 } else {
1139 /* Upon normal completion the device _is_ online */
1140 device->tape_generic_status |= GMT_ONLINE(~0);
1141 }
1142 if (device->tape_state == TS_NOT_OPER) {
1143 DBF_EVENT(6, "tape:device is not operational\n");
1144 return;
1145 }
1146
1147 /*
1148 * Request that were canceled still come back with an interrupt.
1149 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1150 */
1151 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
Stefan Bader41117962005-07-27 11:45:04 -07001152 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return;
1154 }
1155
1156 rc = device->discipline->irq(device, request, irb);
1157 /*
1158 * rc < 0 : request finished unsuccessfully.
1159 * rc == TAPE_IO_SUCCESS: request finished successfully.
1160 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1161 * rc == TAPE_IO_RETRY: request finished but needs another go.
1162 * rc == TAPE_IO_STOP: request needs to get terminated.
1163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 switch (rc) {
Stefan Bader41117962005-07-27 11:45:04 -07001165 case TAPE_IO_SUCCESS:
1166 /* Upon normal completion the device _is_ online */
1167 device->tape_generic_status |= GMT_ONLINE(~0);
1168 __tape_end_request(device, request, rc);
1169 break;
1170 case TAPE_IO_PENDING:
1171 break;
Michael Holzheucced1dd2007-02-05 21:18:26 +01001172 case TAPE_IO_LONG_BUSY:
1173 device->lb_timeout.data =
1174 (unsigned long)tape_get_device_reference(device);
1175 device->lb_timeout.expires = jiffies +
1176 LONG_BUSY_TIMEOUT * HZ;
1177 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1178 add_timer(&device->lb_timeout);
1179 request->status = TAPE_REQUEST_LONG_BUSY;
1180 break;
Stefan Bader41117962005-07-27 11:45:04 -07001181 case TAPE_IO_RETRY:
1182 rc = __tape_start_io(device, request);
1183 if (rc)
1184 __tape_end_request(device, request, rc);
1185 break;
1186 case TAPE_IO_STOP:
1187 rc = __tape_cancel_io(device, request);
1188 if (rc)
1189 __tape_end_request(device, request, rc);
1190 break;
1191 default:
1192 if (rc > 0) {
1193 DBF_EVENT(6, "xunknownrc\n");
Stefan Bader41117962005-07-27 11:45:04 -07001194 __tape_end_request(device, request, -EIO);
1195 } else {
1196 __tape_end_request(device, request, rc);
1197 }
1198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200}
1201
1202/*
1203 * Tape device open function used by tape_char & tape_block frontends.
1204 */
1205int
1206tape_open(struct tape_device *device)
1207{
1208 int rc;
1209
Frank Munzertb3c21e42008-10-28 11:10:19 +01001210 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (device->tape_state == TS_NOT_OPER) {
1212 DBF_EVENT(6, "TAPE:nodev\n");
1213 rc = -ENODEV;
1214 } else if (device->tape_state == TS_IN_USE) {
1215 DBF_EVENT(6, "TAPE:dbusy\n");
1216 rc = -EBUSY;
1217 } else if (device->tape_state == TS_BLKUSE) {
1218 DBF_EVENT(6, "TAPE:dbusy\n");
1219 rc = -EBUSY;
1220 } else if (device->discipline != NULL &&
1221 !try_module_get(device->discipline->owner)) {
1222 DBF_EVENT(6, "TAPE:nodisc\n");
1223 rc = -ENODEV;
1224 } else {
1225 tape_state_set(device, TS_IN_USE);
1226 rc = 0;
1227 }
Frank Munzertb3c21e42008-10-28 11:10:19 +01001228 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return rc;
1230}
1231
1232/*
1233 * Tape device release function used by tape_char & tape_block frontends.
1234 */
1235int
1236tape_release(struct tape_device *device)
1237{
Frank Munzertb3c21e42008-10-28 11:10:19 +01001238 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (device->tape_state == TS_IN_USE)
1240 tape_state_set(device, TS_UNUSED);
1241 module_put(device->discipline->owner);
Frank Munzertb3c21e42008-10-28 11:10:19 +01001242 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return 0;
1244}
1245
1246/*
1247 * Execute a magnetic tape command a number of times.
1248 */
1249int
1250tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1251{
1252 tape_mtop_fn fn;
1253 int rc;
1254
1255 DBF_EVENT(6, "TAPE:mtio\n");
1256 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1257 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1258
1259 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1260 return -EINVAL;
1261 fn = device->discipline->mtop_array[mt_op];
1262 if (fn == NULL)
1263 return -EINVAL;
1264
1265 /* We assume that the backends can handle count up to 500. */
1266 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1267 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1268 rc = 0;
1269 for (; mt_count > 500; mt_count -= 500)
1270 if ((rc = fn(device, 500)) != 0)
1271 break;
1272 if (rc == 0)
1273 rc = fn(device, mt_count);
1274 } else
1275 rc = fn(device, mt_count);
1276 return rc;
1277
1278}
1279
1280/*
1281 * Tape init function.
1282 */
1283static int
1284tape_init (void)
1285{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001286 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1288#ifdef DBF_LIKE_HELL
1289 debug_set_level(TAPE_DBF_AREA, 6);
1290#endif
Heiko Carstense018ba12006-02-01 03:06:31 -08001291 DBF_EVENT(3, "tape init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tape_proc_init();
1293 tapechar_init ();
1294 tapeblock_init ();
1295 return 0;
1296}
1297
1298/*
1299 * Tape exit function.
1300 */
1301static void
1302tape_exit(void)
1303{
1304 DBF_EVENT(6, "tape exit\n");
1305
1306 /* Get rid of the frontends */
1307 tapechar_exit();
1308 tapeblock_exit();
1309 tape_proc_cleanup();
1310 debug_unregister (TAPE_DBF_AREA);
1311}
1312
1313MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1314 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
Heiko Carstense018ba12006-02-01 03:06:31 -08001315MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316MODULE_LICENSE("GPL");
1317
1318module_init(tape_init);
1319module_exit(tape_exit);
1320
1321EXPORT_SYMBOL(tape_generic_remove);
1322EXPORT_SYMBOL(tape_generic_probe);
1323EXPORT_SYMBOL(tape_generic_online);
1324EXPORT_SYMBOL(tape_generic_offline);
Frank Munzert3ef32e622009-06-16 10:30:39 +02001325EXPORT_SYMBOL(tape_generic_pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326EXPORT_SYMBOL(tape_put_device);
1327EXPORT_SYMBOL(tape_get_device_reference);
1328EXPORT_SYMBOL(tape_state_verbose);
1329EXPORT_SYMBOL(tape_op_verbose);
1330EXPORT_SYMBOL(tape_state_set);
1331EXPORT_SYMBOL(tape_med_state_set);
1332EXPORT_SYMBOL(tape_alloc_request);
1333EXPORT_SYMBOL(tape_free_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334EXPORT_SYMBOL(tape_dump_sense_dbf);
1335EXPORT_SYMBOL(tape_do_io);
1336EXPORT_SYMBOL(tape_do_io_async);
1337EXPORT_SYMBOL(tape_do_io_interruptible);
Michael Holzheu5f384332006-03-24 03:15:28 -08001338EXPORT_SYMBOL(tape_cancel_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339EXPORT_SYMBOL(tape_mtop);