blob: b3a3e8e8656e6db1be5e3a50b63056c43f0897f6 [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"
Michael Holzheubb509912009-12-18 17:43:21 +010015#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/init.h> // for kernel parameters
19#include <linux/kmod.h> // for requesting modules
20#include <linux/spinlock.h> // for locks
21#include <linux/vmalloc.h>
22#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/types.h> // for variable types
26
27#define TAPE_DBF_AREA tape_core_dbf
28
29#include "tape.h"
30#include "tape_std.h"
31
Michael Holzheucced1dd2007-02-05 21:18:26 +010032#define LONG_BUSY_TIMEOUT 180 /* seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
Martin Schwidefskyc1637532006-12-08 15:53:57 +010035static void tape_delayed_next_request(struct work_struct *);
Michael Holzheucced1dd2007-02-05 21:18:26 +010036static void tape_long_busy_timeout(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * One list to contain all tape devices of all disciplines, so
40 * we can assign the devices to minor numbers of the same major
41 * The list is protected by the rwlock
42 */
Denis Chengc11ca972008-01-26 14:11:13 +010043static LIST_HEAD(tape_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_RWLOCK(tape_device_lock);
45
46/*
47 * Pointer to debug area.
48 */
49debug_info_t *TAPE_DBF_AREA = NULL;
50EXPORT_SYMBOL(TAPE_DBF_AREA);
51
52/*
53 * Printable strings for tape enumerations.
54 */
55const char *tape_state_verbose[TS_SIZE] =
56{
57 [TS_UNUSED] = "UNUSED",
58 [TS_IN_USE] = "IN_USE",
59 [TS_BLKUSE] = "BLKUSE",
60 [TS_INIT] = "INIT ",
61 [TS_NOT_OPER] = "NOT_OP"
62};
63
64const char *tape_op_verbose[TO_SIZE] =
65{
66 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
67 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
68 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
69 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
70 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
71 [TO_RFO] = "RFO", [TO_REW] = "REW",
72 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
73 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
74 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
75 [TO_READ_ATTMSG] = "RAT",
76 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
Michael Holzheucced1dd2007-02-05 21:18:26 +010077 [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON",
78 [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS",
Michael Holzheue2963062007-05-04 18:47:53 +020079 [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Cornelia Huckf455adc2008-05-15 16:52:37 +020082static int devid_to_int(struct ccw_dev_id *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Cornelia Huckf455adc2008-05-15 16:52:37 +020084 return dev_id->devno + (dev_id->ssid << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/*
88 * Some channel attached tape specific attributes.
89 *
90 * FIXME: In the future the first_minor and blocksize attribute should be
91 * replaced by a link to the cdev tree.
92 */
93static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040094tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct tape_device *tdev;
97
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -070098 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
100}
101
102static
103DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
104
105static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400106tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct tape_device *tdev;
109
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700110 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
112}
113
114static
115DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
116
117static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400118tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct tape_device *tdev;
121
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700122 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
124 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
125}
126
127static
128DEVICE_ATTR(state, 0444, tape_state_show, NULL);
129
130static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400131tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct tape_device *tdev;
134 ssize_t rc;
135
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700136 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (tdev->first_minor < 0)
138 return scnprintf(buf, PAGE_SIZE, "N/A\n");
139
140 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
141 if (list_empty(&tdev->req_queue))
142 rc = scnprintf(buf, PAGE_SIZE, "---\n");
143 else {
144 struct tape_request *req;
145
146 req = list_entry(tdev->req_queue.next, struct tape_request,
147 list);
148 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
149 }
150 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
151 return rc;
152}
153
154static
155DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
156
157static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400158tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct tape_device *tdev;
161
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700162 tdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
165}
166
167static
168DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
169
170static struct attribute *tape_attrs[] = {
171 &dev_attr_medium_state.attr,
172 &dev_attr_first_minor.attr,
173 &dev_attr_state.attr,
174 &dev_attr_operation.attr,
175 &dev_attr_blocksize.attr,
176 NULL
177};
178
179static struct attribute_group tape_attr_group = {
180 .attrs = tape_attrs,
181};
182
183/*
184 * Tape state functions
185 */
186void
187tape_state_set(struct tape_device *device, enum tape_state newstate)
188{
189 const char *str;
190
191 if (device->tape_state == TS_NOT_OPER) {
192 DBF_EVENT(3, "ts_set err: not oper\n");
193 return;
194 }
195 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
Peter Oberparleiterf9760692006-04-10 22:53:49 -0700196 DBF_EVENT(4, "old ts:\t\n");
197 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 str = tape_state_verbose[device->tape_state];
199 else
200 str = "UNKNOWN TS";
201 DBF_EVENT(4, "%s\n", str);
202 DBF_EVENT(4, "new ts:\t\n");
Peter Oberparleiterf9760692006-04-10 22:53:49 -0700203 if (newstate < TS_SIZE && newstate >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 str = tape_state_verbose[newstate];
205 else
206 str = "UNKNOWN TS";
207 DBF_EVENT(4, "%s\n", str);
208 device->tape_state = newstate;
209 wake_up(&device->state_change_wq);
210}
211
Martin Schwidefsky3b210e72010-11-10 10:05:52 +0100212struct tape_med_state_work_data {
213 struct tape_device *device;
214 enum tape_medium_state state;
215 struct work_struct work;
216};
217
218static void
219tape_med_state_work_handler(struct work_struct *work)
220{
221 static char env_state_loaded[] = "MEDIUM_STATE=LOADED";
222 static char env_state_unloaded[] = "MEDIUM_STATE=UNLOADED";
223 struct tape_med_state_work_data *p =
224 container_of(work, struct tape_med_state_work_data, work);
225 struct tape_device *device = p->device;
226 char *envp[] = { NULL, NULL };
227
228 switch (p->state) {
229 case MS_UNLOADED:
230 pr_info("%s: The tape cartridge has been successfully "
231 "unloaded\n", dev_name(&device->cdev->dev));
232 envp[0] = env_state_unloaded;
233 kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
234 break;
235 case MS_LOADED:
236 pr_info("%s: A tape cartridge has been mounted\n",
237 dev_name(&device->cdev->dev));
238 envp[0] = env_state_loaded;
239 kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
240 break;
241 default:
242 break;
243 }
244 tape_put_device(device);
245 kfree(p);
246}
247
248static void
249tape_med_state_work(struct tape_device *device, enum tape_medium_state state)
250{
251 struct tape_med_state_work_data *p;
252
253 p = kzalloc(sizeof(*p), GFP_ATOMIC);
254 if (p) {
255 INIT_WORK(&p->work, tape_med_state_work_handler);
256 p->device = tape_get_device(device);
257 p->state = state;
258 schedule_work(&p->work);
259 }
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262void
263tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
264{
Martin Schwidefsky3b210e72010-11-10 10:05:52 +0100265 enum tape_medium_state oldstate;
266
267 oldstate = device->medium_state;
268 if (oldstate == newstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return;
Martin Schwidefsky3b210e72010-11-10 10:05:52 +0100270 device->medium_state = newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 switch(newstate){
272 case MS_UNLOADED:
273 device->tape_generic_status |= GMT_DR_OPEN(~0);
Martin Schwidefsky3b210e72010-11-10 10:05:52 +0100274 if (oldstate == MS_LOADED)
275 tape_med_state_work(device, MS_UNLOADED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 case MS_LOADED:
278 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
Martin Schwidefsky3b210e72010-11-10 10:05:52 +0100279 if (oldstate == MS_UNLOADED)
280 tape_med_state_work(device, MS_LOADED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 wake_up(&device->state_change_wq);
286}
287
288/*
289 * Stop running ccw. Has to be called with the device lock held.
290 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100291static int
Stefan Bader41117962005-07-27 11:45:04 -0700292__tape_cancel_io(struct tape_device *device, struct tape_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 int retries;
295 int rc;
296
297 /* Check if interrupt has already been processed */
298 if (request->callback == NULL)
299 return 0;
300
301 rc = 0;
302 for (retries = 0; retries < 5; retries++) {
303 rc = ccw_device_clear(device->cdev, (long) request);
304
Stefan Bader41117962005-07-27 11:45:04 -0700305 switch (rc) {
306 case 0:
307 request->status = TAPE_REQUEST_DONE;
308 return 0;
309 case -EBUSY:
310 request->status = TAPE_REQUEST_CANCEL;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100311 schedule_delayed_work(&device->tape_dnr, 0);
Stefan Bader41117962005-07-27 11:45:04 -0700312 return 0;
313 case -ENODEV:
314 DBF_EXCEPTION(2, "device gone, retry\n");
315 break;
316 case -EIO:
317 DBF_EXCEPTION(2, "I/O error, retry\n");
318 break;
319 default:
320 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
324 return rc;
325}
326
327/*
328 * Add device into the sorted list, giving it the first
329 * available minor number.
330 */
331static int
332tape_assign_minor(struct tape_device *device)
333{
334 struct tape_device *tmp;
335 int minor;
336
337 minor = 0;
338 write_lock(&tape_device_lock);
339 list_for_each_entry(tmp, &tape_device_list, node) {
340 if (minor < tmp->first_minor)
341 break;
342 minor += TAPE_MINORS_PER_DEV;
343 }
344 if (minor >= 256) {
345 write_unlock(&tape_device_lock);
346 return -ENODEV;
347 }
348 device->first_minor = minor;
349 list_add_tail(&device->node, &tmp->node);
350 write_unlock(&tape_device_lock);
351 return 0;
352}
353
354/* remove device from the list */
355static void
356tape_remove_minor(struct tape_device *device)
357{
358 write_lock(&tape_device_lock);
359 list_del_init(&device->node);
360 device->first_minor = -1;
361 write_unlock(&tape_device_lock);
362}
363
364/*
365 * Set a device online.
366 *
367 * This function is called by the common I/O layer to move a device from the
368 * detected but offline into the online state.
369 * If we return an error (RC < 0) the device remains in the offline state. This
370 * can happen if the device is assigned somewhere else, for example.
371 */
372int
373tape_generic_online(struct tape_device *device,
374 struct tape_discipline *discipline)
375{
376 int rc;
377
378 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
379
380 if (device->tape_state != TS_INIT) {
381 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
382 return -EINVAL;
383 }
384
Michael Holzheucced1dd2007-02-05 21:18:26 +0100385 init_timer(&device->lb_timeout);
386 device->lb_timeout.function = tape_long_busy_timeout;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Let the discipline have a go at the device. */
389 device->discipline = discipline;
390 if (!try_module_get(discipline->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EINVAL;
392 }
393
394 rc = discipline->setup_device(device);
395 if (rc)
396 goto out;
397 rc = tape_assign_minor(device);
398 if (rc)
399 goto out_discipline;
400
401 rc = tapechar_setup_device(device);
402 if (rc)
403 goto out_minor;
404 rc = tapeblock_setup_device(device);
405 if (rc)
406 goto out_char;
407
408 tape_state_set(device, TS_UNUSED);
409
410 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
411
412 return 0;
413
414out_char:
415 tapechar_cleanup_device(device);
Roel Kluin68d36bd2009-09-11 10:28:55 +0200416out_minor:
417 tape_remove_minor(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418out_discipline:
419 device->discipline->cleanup_device(device);
420 device->discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421out:
422 module_put(discipline->owner);
423 return rc;
424}
425
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100426static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427tape_cleanup_device(struct tape_device *device)
428{
429 tapeblock_cleanup_device(device);
430 tapechar_cleanup_device(device);
431 device->discipline->cleanup_device(device);
432 module_put(device->discipline->owner);
433 tape_remove_minor(device);
434 tape_med_state_set(device, MS_UNKNOWN);
435}
436
437/*
Frank Munzert3ef32e622009-06-16 10:30:39 +0200438 * Suspend device.
439 *
440 * Called by the common I/O layer if the drive should be suspended on user
441 * request. We refuse to suspend if the device is loaded or in use for the
442 * following reason:
443 * While the Linux guest is suspended, it might be logged off which causes
444 * devices to be detached. Tape devices are automatically rewound and unloaded
445 * during DETACH processing (unless the tape device was attached with the
446 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
447 * resume the original state of the tape device, since we would need to
448 * manually re-load the cartridge which was active at suspend time.
449 */
450int tape_generic_pm_suspend(struct ccw_device *cdev)
451{
452 struct tape_device *device;
453
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +0200454 device = dev_get_drvdata(&cdev->dev);
Frank Munzert3ef32e622009-06-16 10:30:39 +0200455 if (!device) {
456 return -ENODEV;
457 }
458
459 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
460 device->cdev_id, device);
461
462 if (device->medium_state != MS_UNLOADED) {
463 pr_err("A cartridge is loaded in tape device %s, "
464 "refusing to suspend\n", dev_name(&cdev->dev));
465 return -EBUSY;
466 }
467
468 spin_lock_irq(get_ccwdev_lock(device->cdev));
469 switch (device->tape_state) {
470 case TS_INIT:
471 case TS_NOT_OPER:
472 case TS_UNUSED:
473 spin_unlock_irq(get_ccwdev_lock(device->cdev));
474 break;
475 default:
476 pr_err("Tape device %s is busy, refusing to "
477 "suspend\n", dev_name(&cdev->dev));
478 spin_unlock_irq(get_ccwdev_lock(device->cdev));
479 return -EBUSY;
480 }
481
482 DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
483 return 0;
484}
485
486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Set device offline.
488 *
489 * Called by the common I/O layer if the drive should set offline on user
490 * request. We may prevent this by returning an error.
491 * Manual offline is only allowed while the drive is not in use.
492 */
493int
Frank Munzert4d7a3cd2009-04-23 13:58:09 +0200494tape_generic_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Frank Munzert4d7a3cd2009-04-23 13:58:09 +0200496 struct tape_device *device;
497
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700498 device = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -ENODEV;
501 }
502
503 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
504 device->cdev_id, device);
505
506 spin_lock_irq(get_ccwdev_lock(device->cdev));
507 switch (device->tape_state) {
508 case TS_INIT:
509 case TS_NOT_OPER:
510 spin_unlock_irq(get_ccwdev_lock(device->cdev));
511 break;
512 case TS_UNUSED:
513 tape_state_set(device, TS_INIT);
514 spin_unlock_irq(get_ccwdev_lock(device->cdev));
515 tape_cleanup_device(device);
516 break;
517 default:
518 DBF_EVENT(3, "(%08x): Set offline failed "
519 "- drive in use.\n",
520 device->cdev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 spin_unlock_irq(get_ccwdev_lock(device->cdev));
522 return -EBUSY;
523 }
524
525 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
526 return 0;
527}
528
529/*
530 * Allocate memory for a new device structure.
531 */
532static struct tape_device *
533tape_alloc_device(void)
534{
535 struct tape_device *device;
536
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800537 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (device == NULL) {
539 DBF_EXCEPTION(2, "ti:no mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return ERR_PTR(-ENOMEM);
541 }
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800542 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (device->modeset_byte == NULL) {
544 DBF_EXCEPTION(2, "ti:no mem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 kfree(device);
546 return ERR_PTR(-ENOMEM);
547 }
Martin Schwidefsky369a4632009-12-07 12:52:04 +0100548 mutex_init(&device->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 INIT_LIST_HEAD(&device->req_queue);
550 INIT_LIST_HEAD(&device->node);
551 init_waitqueue_head(&device->state_change_wq);
Martin Schwidefsky4657fb82008-05-30 10:03:33 +0200552 init_waitqueue_head(&device->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 device->tape_state = TS_INIT;
554 device->medium_state = MS_UNKNOWN;
555 *device->modeset_byte = 0;
556 device->first_minor = -1;
557 atomic_set(&device->ref_count, 1);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100558 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return device;
561}
562
563/*
564 * Get a reference to an existing device structure. This will automatically
565 * increment the reference count.
566 */
567struct tape_device *
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100568tape_get_device(struct tape_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100570 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100572 count = atomic_inc_return(&device->ref_count);
573 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return device;
575}
576
577/*
578 * Decrease the reference counter of a devices structure. If the
579 * reference counter reaches zero free the device structure.
580 * The function returns a NULL pointer to be used by the caller
581 * for clearing reference pointers.
582 */
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100583void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584tape_put_device(struct tape_device *device)
585{
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100586 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100588 count = atomic_dec_return(&device->ref_count);
589 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
590 BUG_ON(count < 0);
591 if (count == 0) {
592 kfree(device->modeset_byte);
593 kfree(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * Find tape device by a device index.
599 */
600struct tape_device *
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100601tape_find_device(int devindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct tape_device *device, *tmp;
604
605 device = ERR_PTR(-ENODEV);
606 read_lock(&tape_device_lock);
607 list_for_each_entry(tmp, &tape_device_list, node) {
608 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100609 device = tape_get_device(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 }
612 }
613 read_unlock(&tape_device_lock);
614 return device;
615}
616
617/*
618 * Driverfs tape probe function.
619 */
620int
621tape_generic_probe(struct ccw_device *cdev)
622{
623 struct tape_device *device;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200624 int ret;
Cornelia Huckf455adc2008-05-15 16:52:37 +0200625 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 device = tape_alloc_device();
628 if (IS_ERR(device))
629 return -ENODEV;
Peter Oberparleiter454e1fa2009-12-07 12:51:30 +0100630 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
631 CCWDEV_DO_MULTIPATH);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200632 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
633 if (ret) {
634 tape_put_device(device);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200635 return ret;
636 }
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700637 dev_set_drvdata(&cdev->dev, device);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200638 cdev->handler = __tape_do_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 device->cdev = cdev;
Cornelia Huckf455adc2008-05-15 16:52:37 +0200640 ccw_device_get_id(cdev, &dev_id);
641 device->cdev_id = devid_to_int(&dev_id);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +0200642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100645static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646__tape_discard_requests(struct tape_device *device)
647{
648 struct tape_request * request;
649 struct list_head * l, *n;
650
651 list_for_each_safe(l, n, &device->req_queue) {
652 request = list_entry(l, struct tape_request, list);
653 if (request->status == TAPE_REQUEST_IN_IO)
654 request->status = TAPE_REQUEST_DONE;
655 list_del(&request->list);
656
657 /* Decrease ref_count for removed request. */
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100658 request->device = NULL;
659 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 request->rc = -EIO;
661 if (request->callback != NULL)
662 request->callback(request, request->callback_data);
663 }
664}
665
666/*
667 * Driverfs tape remove function.
668 *
669 * This function is called whenever the common I/O layer detects the device
670 * gone. This can happen at any time and we cannot refuse.
671 */
672void
673tape_generic_remove(struct ccw_device *cdev)
674{
675 struct tape_device * device;
676
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700677 device = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return;
680 }
681 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
682
683 spin_lock_irq(get_ccwdev_lock(device->cdev));
684 switch (device->tape_state) {
685 case TS_INIT:
686 tape_state_set(device, TS_NOT_OPER);
687 case TS_NOT_OPER:
688 /*
689 * Nothing to do.
690 */
691 spin_unlock_irq(get_ccwdev_lock(device->cdev));
692 break;
693 case TS_UNUSED:
694 /*
695 * Need only to release the device.
696 */
697 tape_state_set(device, TS_NOT_OPER);
698 spin_unlock_irq(get_ccwdev_lock(device->cdev));
699 tape_cleanup_device(device);
700 break;
701 default:
702 /*
703 * There may be requests on the queue. We will not get
704 * an interrupt for a request that was running. So we
705 * just post them all as I/O errors.
706 */
707 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
708 device->cdev_id);
Michael Holzheu59e36922009-09-11 10:29:07 +0200709 pr_warning("%s: A tape unit was detached while in "
710 "use\n", dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 tape_state_set(device, TS_NOT_OPER);
712 __tape_discard_requests(device);
713 spin_unlock_irq(get_ccwdev_lock(device->cdev));
714 tape_cleanup_device(device);
715 }
716
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100717 device = dev_get_drvdata(&cdev->dev);
718 if (device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100720 dev_set_drvdata(&cdev->dev, NULL);
721 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723}
724
725/*
726 * Allocate a new tape ccw request
727 */
728struct tape_request *
729tape_alloc_request(int cplength, int datasize)
730{
731 struct tape_request *request;
732
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100733 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
736
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800737 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (request == NULL) {
739 DBF_EXCEPTION(1, "cqra nomem\n");
740 return ERR_PTR(-ENOMEM);
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* allocate channel program */
743 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800744 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 GFP_ATOMIC | GFP_DMA);
746 if (request->cpaddr == NULL) {
747 DBF_EXCEPTION(1, "cqra nomem\n");
748 kfree(request);
749 return ERR_PTR(-ENOMEM);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 /* alloc small kernel buffer */
753 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800754 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (request->cpdata == NULL) {
756 DBF_EXCEPTION(1, "cqra nomem\n");
Jesper Juhl17fd6822005-11-07 01:01:30 -0800757 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 kfree(request);
759 return ERR_PTR(-ENOMEM);
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
763 request->cpdata);
764
765 return request;
766}
767
768/*
769 * Free tape ccw request
770 */
771void
772tape_free_request (struct tape_request * request)
773{
774 DBF_LH(6, "Free request %p\n", request);
775
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100776 if (request->device)
777 tape_put_device(request->device);
Jesper Juhl17fd6822005-11-07 01:01:30 -0800778 kfree(request->cpdata);
779 kfree(request->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 kfree(request);
781}
782
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100783static int
Stefan Bader41117962005-07-27 11:45:04 -0700784__tape_start_io(struct tape_device *device, struct tape_request *request)
785{
786 int rc;
787
788#ifdef CONFIG_S390_TAPE_BLOCK
789 if (request->op == TO_BLOCK)
790 device->discipline->check_locate(device, request);
791#endif
792 rc = ccw_device_start(
793 device->cdev,
794 request->cpaddr,
795 (unsigned long) request,
796 0x00,
797 request->options
798 );
799 if (rc == 0) {
800 request->status = TAPE_REQUEST_IN_IO;
801 } else if (rc == -EBUSY) {
802 /* The common I/O subsystem is currently busy. Retry later. */
803 request->status = TAPE_REQUEST_QUEUED;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100804 schedule_delayed_work(&device->tape_dnr, 0);
Stefan Bader41117962005-07-27 11:45:04 -0700805 rc = 0;
806 } else {
807 /* Start failed. Remove request and indicate failure. */
808 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
809 }
810 return rc;
811}
812
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100813static void
Stefan Bader41117962005-07-27 11:45:04 -0700814__tape_start_next_request(struct tape_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct list_head *l, *n;
817 struct tape_request *request;
818 int rc;
819
Stefan Bader41117962005-07-27 11:45:04 -0700820 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
822 * Try to start each request on request queue until one is
823 * started successful.
824 */
825 list_for_each_safe(l, n, &device->req_queue) {
826 request = list_entry(l, struct tape_request, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Stefan Bader41117962005-07-27 11:45:04 -0700828 /*
829 * Avoid race condition if bottom-half was triggered more than
830 * once.
831 */
832 if (request->status == TAPE_REQUEST_IN_IO)
833 return;
Michael Holzheu5f384332006-03-24 03:15:28 -0800834 /*
835 * Request has already been stopped. We have to wait until
836 * the request is removed from the queue in the interrupt
837 * handling.
838 */
839 if (request->status == TAPE_REQUEST_DONE)
840 return;
Stefan Bader41117962005-07-27 11:45:04 -0700841
842 /*
843 * We wanted to cancel the request but the common I/O layer
844 * was busy at that time. This can only happen if this
845 * function is called by delayed_next_request.
846 * Otherwise we start the next request on the queue.
847 */
848 if (request->status == TAPE_REQUEST_CANCEL) {
849 rc = __tape_cancel_io(device, request);
850 } else {
851 rc = __tape_start_io(device, request);
852 }
853 if (rc == 0)
854 return;
855
856 /* Set ending status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 request->rc = rc;
858 request->status = TAPE_REQUEST_DONE;
Stefan Bader41117962005-07-27 11:45:04 -0700859
860 /* Remove from request queue. */
861 list_del(&request->list);
862
863 /* Do callback. */
864 if (request->callback != NULL)
865 request->callback(request, request->callback_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867}
868
869static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100870tape_delayed_next_request(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100872 struct tape_device *device =
873 container_of(work, struct tape_device, tape_dnr.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Stefan Bader41117962005-07-27 11:45:04 -0700875 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
876 spin_lock_irq(get_ccwdev_lock(device->cdev));
877 __tape_start_next_request(device);
878 spin_unlock_irq(get_ccwdev_lock(device->cdev));
879}
880
Michael Holzheucced1dd2007-02-05 21:18:26 +0100881static void tape_long_busy_timeout(unsigned long data)
882{
883 struct tape_request *request;
884 struct tape_device *device;
885
886 device = (struct tape_device *) data;
887 spin_lock_irq(get_ccwdev_lock(device->cdev));
888 request = list_entry(device->req_queue.next, struct tape_request, list);
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100889 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100890 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
891 __tape_start_next_request(device);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100892 device->lb_timeout.data = 0UL;
893 tape_put_device(device);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100894 spin_unlock_irq(get_ccwdev_lock(device->cdev));
895}
896
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100897static void
Stefan Bader41117962005-07-27 11:45:04 -0700898__tape_end_request(
899 struct tape_device * device,
900 struct tape_request * request,
901 int rc)
902{
903 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
904 if (request) {
905 request->rc = rc;
906 request->status = TAPE_REQUEST_DONE;
907
908 /* Remove from request queue. */
909 list_del(&request->list);
910
911 /* Do callback. */
912 if (request->callback != NULL)
913 request->callback(request, request->callback_data);
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /* Start next request. */
917 if (!list_empty(&device->req_queue))
Stefan Bader41117962005-07-27 11:45:04 -0700918 __tape_start_next_request(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Write sense data to dbf
923 */
924void
925tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
926 struct irb *irb)
927{
928 unsigned int *sptr;
929 const char* op;
930
931 if (request != NULL)
932 op = tape_op_verbose[request->op];
933 else
934 op = "---";
935 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200936 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
938 sptr = (unsigned int *) irb->ecw;
939 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
940 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
941 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
942 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
943}
944
945/*
946 * I/O helper function. Adds the request to the request queue
947 * and starts it if the tape is idle. Has to be called with
948 * the device lock held.
949 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100950static int
Stefan Bader41117962005-07-27 11:45:04 -0700951__tape_start_request(struct tape_device *device, struct tape_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int rc;
954
955 switch (request->op) {
956 case TO_MSEN:
957 case TO_ASSIGN:
958 case TO_UNASSIGN:
959 case TO_READ_ATTMSG:
Michael Holzheue2963062007-05-04 18:47:53 +0200960 case TO_RDC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (device->tape_state == TS_INIT)
962 break;
963 if (device->tape_state == TS_UNUSED)
964 break;
965 default:
966 if (device->tape_state == TS_BLKUSE)
967 break;
968 if (device->tape_state != TS_IN_USE)
969 return -ENODEV;
970 }
971
972 /* Increase use count of device for the added request. */
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100973 request->device = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (list_empty(&device->req_queue)) {
976 /* No other requests are on the queue. Start this one. */
Stefan Bader41117962005-07-27 11:45:04 -0700977 rc = __tape_start_io(device, request);
978 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return rc;
Stefan Bader41117962005-07-27 11:45:04 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 DBF_LH(5, "Request %p added for execution.\n", request);
982 list_add(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 } else {
984 DBF_LH(5, "Request %p add to queue.\n", request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 request->status = TAPE_REQUEST_QUEUED;
Stefan Bader41117962005-07-27 11:45:04 -0700986 list_add_tail(&request->list, &device->req_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 return 0;
989}
990
991/*
992 * Add the request to the request queue, try to start it if the
993 * tape is idle. Return without waiting for end of i/o.
994 */
995int
996tape_do_io_async(struct tape_device *device, struct tape_request *request)
997{
998 int rc;
999
1000 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
1001
1002 spin_lock_irq(get_ccwdev_lock(device->cdev));
1003 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -07001004 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1006 return rc;
1007}
1008
1009/*
1010 * tape_do_io/__tape_wake_up
1011 * Add the request to the request queue, try to start it if the
1012 * tape is idle and wait uninterruptible for its completion.
1013 */
1014static void
1015__tape_wake_up(struct tape_request *request, void *data)
1016{
1017 request->callback = NULL;
1018 wake_up((wait_queue_head_t *) data);
1019}
1020
1021int
1022tape_do_io(struct tape_device *device, struct tape_request *request)
1023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int rc;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_lock_irq(get_ccwdev_lock(device->cdev));
1027 /* Setup callback */
1028 request->callback = __tape_wake_up;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001029 request->callback_data = &device->wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* Add request to request queue and try to start it. */
Stefan Bader41117962005-07-27 11:45:04 -07001031 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1033 if (rc)
1034 return rc;
1035 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001036 wait_event(device->wait_queue, (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* Get rc from request */
1038 return request->rc;
1039}
1040
1041/*
1042 * tape_do_io_interruptible/__tape_wake_up_interruptible
1043 * Add the request to the request queue, try to start it if the
1044 * tape is idle and wait uninterruptible for its completion.
1045 */
1046static void
1047__tape_wake_up_interruptible(struct tape_request *request, void *data)
1048{
1049 request->callback = NULL;
1050 wake_up_interruptible((wait_queue_head_t *) data);
1051}
1052
1053int
1054tape_do_io_interruptible(struct tape_device *device,
1055 struct tape_request *request)
1056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int rc;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_lock_irq(get_ccwdev_lock(device->cdev));
1060 /* Setup callback */
1061 request->callback = __tape_wake_up_interruptible;
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001062 request->callback_data = &device->wait_queue;
Stefan Bader41117962005-07-27 11:45:04 -07001063 rc = __tape_start_request(device, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1065 if (rc)
1066 return rc;
1067 /* Request added to the queue. Wait for its completion. */
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001068 rc = wait_event_interruptible(device->wait_queue,
1069 (request->callback == NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (rc != -ERESTARTSYS)
1071 /* Request finished normally. */
1072 return request->rc;
Stefan Bader41117962005-07-27 11:45:04 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Interrupted by a signal. We have to stop the current request. */
1075 spin_lock_irq(get_ccwdev_lock(device->cdev));
Stefan Bader41117962005-07-27 11:45:04 -07001076 rc = __tape_cancel_io(device, request);
1077 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (rc == 0) {
Stefan Bader41117962005-07-27 11:45:04 -07001079 /* Wait for the interrupt that acknowledges the halt. */
1080 do {
1081 rc = wait_event_interruptible(
Martin Schwidefsky4657fb82008-05-30 10:03:33 +02001082 device->wait_queue,
Stefan Bader41117962005-07-27 11:45:04 -07001083 (request->callback == NULL)
1084 );
Michael Holzheu4cd190a2006-03-24 03:15:27 -08001085 } while (rc == -ERESTARTSYS);
Stefan Bader41117962005-07-27 11:45:04 -07001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1088 rc = -ERESTARTSYS;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return rc;
1091}
1092
1093/*
Michael Holzheu5f384332006-03-24 03:15:28 -08001094 * Stop running ccw.
1095 */
1096int
1097tape_cancel_io(struct tape_device *device, struct tape_request *request)
1098{
1099 int rc;
1100
1101 spin_lock_irq(get_ccwdev_lock(device->cdev));
1102 rc = __tape_cancel_io(device, request);
1103 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1104 return rc;
1105}
1106
1107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * Tape interrupt routine, called from the ccw_device layer
1109 */
1110static void
1111__tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1112{
1113 struct tape_device *device;
1114 struct tape_request *request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int rc;
1116
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001117 device = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (device == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return;
1120 }
1121 request = (struct tape_request *) intparm;
1122
1123 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1124
1125 /* On special conditions irb is an error pointer */
1126 if (IS_ERR(irb)) {
Stefan Bader41117962005-07-27 11:45:04 -07001127 /* FIXME: What to do with the request? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 switch (PTR_ERR(irb)) {
1129 case -ETIMEDOUT:
Sebastian Ottf2166bb2010-10-29 16:50:44 +02001130 DBF_LH(1, "(%08x): Request timed out\n",
1131 device->cdev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case -EIO:
Stefan Bader41117962005-07-27 11:45:04 -07001133 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 default:
Sebastian Ottf2166bb2010-10-29 16:50:44 +02001136 DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
1137 device->cdev_id, PTR_ERR(irb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 return;
1140 }
1141
Stefan Bader41117962005-07-27 11:45:04 -07001142 /*
1143 * If the condition code is not zero and the start function bit is
1144 * still set, this is an deferred error and the last start I/O did
Stefan Bader842d3fb2006-03-24 03:15:26 -08001145 * not succeed. At this point the condition that caused the deferred
1146 * error might still apply. So we just schedule the request to be
1147 * started later.
Stefan Bader41117962005-07-27 11:45:04 -07001148 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001149 if (irb->scsw.cmd.cc != 0 &&
1150 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
Michael Holzheu5f384332006-03-24 03:15:28 -08001151 (request->status == TAPE_REQUEST_IN_IO)) {
1152 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001153 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
Stefan Bader842d3fb2006-03-24 03:15:26 -08001154 request->status = TAPE_REQUEST_QUEUED;
Michael Holzheu5f384332006-03-24 03:15:28 -08001155 schedule_delayed_work(&device->tape_dnr, HZ);
Stefan Bader41117962005-07-27 11:45:04 -07001156 return;
1157 }
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* May be an unsolicited irq */
1160 if(request != NULL)
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001161 request->rescnt = irb->scsw.cmd.count;
1162 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
Michael Holzheucced1dd2007-02-05 21:18:26 +01001163 !list_empty(&device->req_queue)) {
1164 /* Not Ready to Ready after long busy ? */
1165 struct tape_request *req;
1166 req = list_entry(device->req_queue.next,
1167 struct tape_request, list);
1168 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1169 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1170 if (del_timer(&device->lb_timeout)) {
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +01001171 device->lb_timeout.data = 0UL;
1172 tape_put_device(device);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001173 __tape_start_next_request(device);
1174 }
1175 return;
1176 }
1177 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001178 if (irb->scsw.cmd.dstat != 0x0c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* Set the 'ONLINE' flag depending on sense byte 1 */
1180 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1181 device->tape_generic_status |= GMT_ONLINE(~0);
1182 else
1183 device->tape_generic_status &= ~GMT_ONLINE(~0);
1184
1185 /*
1186 * Any request that does not come back with channel end
1187 * and device end is unusual. Log the sense data.
1188 */
1189 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1190 tape_dump_sense_dbf(device, request, irb);
1191 } else {
1192 /* Upon normal completion the device _is_ online */
1193 device->tape_generic_status |= GMT_ONLINE(~0);
1194 }
1195 if (device->tape_state == TS_NOT_OPER) {
1196 DBF_EVENT(6, "tape:device is not operational\n");
1197 return;
1198 }
1199
1200 /*
1201 * Request that were canceled still come back with an interrupt.
1202 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1203 */
1204 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
Stefan Bader41117962005-07-27 11:45:04 -07001205 __tape_end_request(device, request, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return;
1207 }
1208
1209 rc = device->discipline->irq(device, request, irb);
1210 /*
1211 * rc < 0 : request finished unsuccessfully.
1212 * rc == TAPE_IO_SUCCESS: request finished successfully.
1213 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1214 * rc == TAPE_IO_RETRY: request finished but needs another go.
1215 * rc == TAPE_IO_STOP: request needs to get terminated.
1216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 switch (rc) {
Stefan Bader41117962005-07-27 11:45:04 -07001218 case TAPE_IO_SUCCESS:
1219 /* Upon normal completion the device _is_ online */
1220 device->tape_generic_status |= GMT_ONLINE(~0);
1221 __tape_end_request(device, request, rc);
1222 break;
1223 case TAPE_IO_PENDING:
1224 break;
Michael Holzheucced1dd2007-02-05 21:18:26 +01001225 case TAPE_IO_LONG_BUSY:
1226 device->lb_timeout.data =
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +01001227 (unsigned long) tape_get_device(device);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001228 device->lb_timeout.expires = jiffies +
1229 LONG_BUSY_TIMEOUT * HZ;
1230 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1231 add_timer(&device->lb_timeout);
1232 request->status = TAPE_REQUEST_LONG_BUSY;
1233 break;
Stefan Bader41117962005-07-27 11:45:04 -07001234 case TAPE_IO_RETRY:
1235 rc = __tape_start_io(device, request);
1236 if (rc)
1237 __tape_end_request(device, request, rc);
1238 break;
1239 case TAPE_IO_STOP:
1240 rc = __tape_cancel_io(device, request);
1241 if (rc)
1242 __tape_end_request(device, request, rc);
1243 break;
1244 default:
1245 if (rc > 0) {
1246 DBF_EVENT(6, "xunknownrc\n");
Stefan Bader41117962005-07-27 11:45:04 -07001247 __tape_end_request(device, request, -EIO);
1248 } else {
1249 __tape_end_request(device, request, rc);
1250 }
1251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253}
1254
1255/*
1256 * Tape device open function used by tape_char & tape_block frontends.
1257 */
1258int
1259tape_open(struct tape_device *device)
1260{
1261 int rc;
1262
Frank Munzertb3c21e42008-10-28 11:10:19 +01001263 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (device->tape_state == TS_NOT_OPER) {
1265 DBF_EVENT(6, "TAPE:nodev\n");
1266 rc = -ENODEV;
1267 } else if (device->tape_state == TS_IN_USE) {
1268 DBF_EVENT(6, "TAPE:dbusy\n");
1269 rc = -EBUSY;
1270 } else if (device->tape_state == TS_BLKUSE) {
1271 DBF_EVENT(6, "TAPE:dbusy\n");
1272 rc = -EBUSY;
1273 } else if (device->discipline != NULL &&
1274 !try_module_get(device->discipline->owner)) {
1275 DBF_EVENT(6, "TAPE:nodisc\n");
1276 rc = -ENODEV;
1277 } else {
1278 tape_state_set(device, TS_IN_USE);
1279 rc = 0;
1280 }
Frank Munzertb3c21e42008-10-28 11:10:19 +01001281 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return rc;
1283}
1284
1285/*
1286 * Tape device release function used by tape_char & tape_block frontends.
1287 */
1288int
1289tape_release(struct tape_device *device)
1290{
Frank Munzertb3c21e42008-10-28 11:10:19 +01001291 spin_lock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (device->tape_state == TS_IN_USE)
1293 tape_state_set(device, TS_UNUSED);
1294 module_put(device->discipline->owner);
Frank Munzertb3c21e42008-10-28 11:10:19 +01001295 spin_unlock_irq(get_ccwdev_lock(device->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297}
1298
1299/*
1300 * Execute a magnetic tape command a number of times.
1301 */
1302int
1303tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1304{
1305 tape_mtop_fn fn;
1306 int rc;
1307
1308 DBF_EVENT(6, "TAPE:mtio\n");
1309 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1310 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1311
1312 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1313 return -EINVAL;
1314 fn = device->discipline->mtop_array[mt_op];
1315 if (fn == NULL)
1316 return -EINVAL;
1317
1318 /* We assume that the backends can handle count up to 500. */
1319 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1320 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1321 rc = 0;
1322 for (; mt_count > 500; mt_count -= 500)
1323 if ((rc = fn(device, 500)) != 0)
1324 break;
1325 if (rc == 0)
1326 rc = fn(device, mt_count);
1327 } else
1328 rc = fn(device, mt_count);
1329 return rc;
1330
1331}
1332
1333/*
1334 * Tape init function.
1335 */
1336static int
1337tape_init (void)
1338{
Michael Holzheu66a464d2005-06-25 14:55:33 -07001339 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1341#ifdef DBF_LIKE_HELL
1342 debug_set_level(TAPE_DBF_AREA, 6);
1343#endif
Heiko Carstense018ba12006-02-01 03:06:31 -08001344 DBF_EVENT(3, "tape init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 tape_proc_init();
1346 tapechar_init ();
1347 tapeblock_init ();
1348 return 0;
1349}
1350
1351/*
1352 * Tape exit function.
1353 */
1354static void
1355tape_exit(void)
1356{
1357 DBF_EVENT(6, "tape exit\n");
1358
1359 /* Get rid of the frontends */
1360 tapechar_exit();
1361 tapeblock_exit();
1362 tape_proc_cleanup();
1363 debug_unregister (TAPE_DBF_AREA);
1364}
1365
1366MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1367 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
Heiko Carstense018ba12006-02-01 03:06:31 -08001368MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369MODULE_LICENSE("GPL");
1370
1371module_init(tape_init);
1372module_exit(tape_exit);
1373
1374EXPORT_SYMBOL(tape_generic_remove);
1375EXPORT_SYMBOL(tape_generic_probe);
1376EXPORT_SYMBOL(tape_generic_online);
1377EXPORT_SYMBOL(tape_generic_offline);
Frank Munzert3ef32e622009-06-16 10:30:39 +02001378EXPORT_SYMBOL(tape_generic_pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379EXPORT_SYMBOL(tape_put_device);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +01001380EXPORT_SYMBOL(tape_get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381EXPORT_SYMBOL(tape_state_verbose);
1382EXPORT_SYMBOL(tape_op_verbose);
1383EXPORT_SYMBOL(tape_state_set);
1384EXPORT_SYMBOL(tape_med_state_set);
1385EXPORT_SYMBOL(tape_alloc_request);
1386EXPORT_SYMBOL(tape_free_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387EXPORT_SYMBOL(tape_dump_sense_dbf);
1388EXPORT_SYMBOL(tape_do_io);
1389EXPORT_SYMBOL(tape_do_io_async);
1390EXPORT_SYMBOL(tape_do_io_interruptible);
Michael Holzheu5f384332006-03-24 03:15:28 -08001391EXPORT_SYMBOL(tape_cancel_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392EXPORT_SYMBOL(tape_mtop);