]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/s390/char/tape_core.c | |
3 | * basic function of the tape device driver | |
4 | * | |
5 | * S390 and zSeries version | |
3ef32e62 | 6 | * Copyright IBM Corp. 2001, 2009 |
1da177e4 LT |
7 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
8 | * Michael Holzheu <holzheu@de.ibm.com> | |
9 | * Tuan Ngo-Anh <ngoanh@de.ibm.com> | |
10 | * Martin Schwidefsky <schwidefsky@de.ibm.com> | |
4111796d | 11 | * Stefan Bader <shbader@de.ibm.com> |
1da177e4 LT |
12 | */ |
13 | ||
ab640db0 | 14 | #define KMSG_COMPONENT "tape" |
1da177e4 LT |
15 | #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 | ||
cced1dd4 | 29 | #define LONG_BUSY_TIMEOUT 180 /* seconds */ |
1da177e4 LT |
30 | |
31 | static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *); | |
c1637532 | 32 | static void tape_delayed_next_request(struct work_struct *); |
cced1dd4 | 33 | static void tape_long_busy_timeout(unsigned long data); |
1da177e4 LT |
34 | |
35 | /* | |
36 | * One list to contain all tape devices of all disciplines, so | |
37 | * we can assign the devices to minor numbers of the same major | |
38 | * The list is protected by the rwlock | |
39 | */ | |
c11ca97e | 40 | static LIST_HEAD(tape_device_list); |
1da177e4 LT |
41 | static DEFINE_RWLOCK(tape_device_lock); |
42 | ||
43 | /* | |
44 | * Pointer to debug area. | |
45 | */ | |
46 | debug_info_t *TAPE_DBF_AREA = NULL; | |
47 | EXPORT_SYMBOL(TAPE_DBF_AREA); | |
48 | ||
49 | /* | |
50 | * Printable strings for tape enumerations. | |
51 | */ | |
52 | const char *tape_state_verbose[TS_SIZE] = | |
53 | { | |
54 | [TS_UNUSED] = "UNUSED", | |
55 | [TS_IN_USE] = "IN_USE", | |
56 | [TS_BLKUSE] = "BLKUSE", | |
57 | [TS_INIT] = "INIT ", | |
58 | [TS_NOT_OPER] = "NOT_OP" | |
59 | }; | |
60 | ||
61 | const char *tape_op_verbose[TO_SIZE] = | |
62 | { | |
63 | [TO_BLOCK] = "BLK", [TO_BSB] = "BSB", | |
64 | [TO_BSF] = "BSF", [TO_DSE] = "DSE", | |
65 | [TO_FSB] = "FSB", [TO_FSF] = "FSF", | |
66 | [TO_LBL] = "LBL", [TO_NOP] = "NOP", | |
67 | [TO_RBA] = "RBA", [TO_RBI] = "RBI", | |
68 | [TO_RFO] = "RFO", [TO_REW] = "REW", | |
69 | [TO_RUN] = "RUN", [TO_WRI] = "WRI", | |
70 | [TO_WTM] = "WTM", [TO_MSEN] = "MSN", | |
71 | [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF", | |
72 | [TO_READ_ATTMSG] = "RAT", | |
73 | [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS", | |
cced1dd4 MH |
74 | [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON", |
75 | [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS", | |
e2963062 | 76 | [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC", |
1da177e4 LT |
77 | }; |
78 | ||
f455adcf | 79 | static int devid_to_int(struct ccw_dev_id *dev_id) |
1da177e4 | 80 | { |
f455adcf | 81 | return dev_id->devno + (dev_id->ssid << 16); |
1da177e4 LT |
82 | } |
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 | */ | |
90 | static ssize_t | |
3fd3c0a5 | 91 | tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
92 | { |
93 | struct tape_device *tdev; | |
94 | ||
dff59b64 | 95 | tdev = dev_get_drvdata(dev); |
1da177e4 LT |
96 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state); |
97 | } | |
98 | ||
99 | static | |
100 | DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL); | |
101 | ||
102 | static ssize_t | |
3fd3c0a5 | 103 | tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
104 | { |
105 | struct tape_device *tdev; | |
106 | ||
dff59b64 | 107 | tdev = dev_get_drvdata(dev); |
1da177e4 LT |
108 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor); |
109 | } | |
110 | ||
111 | static | |
112 | DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL); | |
113 | ||
114 | static ssize_t | |
3fd3c0a5 | 115 | tape_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
116 | { |
117 | struct tape_device *tdev; | |
118 | ||
dff59b64 | 119 | tdev = dev_get_drvdata(dev); |
1da177e4 LT |
120 | return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ? |
121 | "OFFLINE" : tape_state_verbose[tdev->tape_state]); | |
122 | } | |
123 | ||
124 | static | |
125 | DEVICE_ATTR(state, 0444, tape_state_show, NULL); | |
126 | ||
127 | static ssize_t | |
3fd3c0a5 | 128 | tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
129 | { |
130 | struct tape_device *tdev; | |
131 | ssize_t rc; | |
132 | ||
dff59b64 | 133 | tdev = dev_get_drvdata(dev); |
1da177e4 LT |
134 | if (tdev->first_minor < 0) |
135 | return scnprintf(buf, PAGE_SIZE, "N/A\n"); | |
136 | ||
137 | spin_lock_irq(get_ccwdev_lock(tdev->cdev)); | |
138 | if (list_empty(&tdev->req_queue)) | |
139 | rc = scnprintf(buf, PAGE_SIZE, "---\n"); | |
140 | else { | |
141 | struct tape_request *req; | |
142 | ||
143 | req = list_entry(tdev->req_queue.next, struct tape_request, | |
144 | list); | |
145 | rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]); | |
146 | } | |
147 | spin_unlock_irq(get_ccwdev_lock(tdev->cdev)); | |
148 | return rc; | |
149 | } | |
150 | ||
151 | static | |
152 | DEVICE_ATTR(operation, 0444, tape_operation_show, NULL); | |
153 | ||
154 | static ssize_t | |
3fd3c0a5 | 155 | tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
156 | { |
157 | struct tape_device *tdev; | |
158 | ||
dff59b64 | 159 | tdev = dev_get_drvdata(dev); |
1da177e4 LT |
160 | |
161 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size); | |
162 | } | |
163 | ||
164 | static | |
165 | DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL); | |
166 | ||
167 | static 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 | ||
176 | static struct attribute_group tape_attr_group = { | |
177 | .attrs = tape_attrs, | |
178 | }; | |
179 | ||
180 | /* | |
181 | * Tape state functions | |
182 | */ | |
183 | void | |
184 | tape_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); | |
f976069a PO |
193 | DBF_EVENT(4, "old ts:\t\n"); |
194 | if (device->tape_state < TS_SIZE && device->tape_state >=0 ) | |
1da177e4 LT |
195 | 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"); | |
f976069a | 200 | if (newstate < TS_SIZE && newstate >= 0) |
1da177e4 LT |
201 | 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 | ||
209 | void | |
210 | tape_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); | |
53f8c573 | 217 | if (device->medium_state == MS_LOADED) |
59e36927 MH |
218 | pr_info("%s: The tape cartridge has been successfully " |
219 | "unloaded\n", dev_name(&device->cdev->dev)); | |
1da177e4 LT |
220 | break; |
221 | case MS_LOADED: | |
222 | device->tape_generic_status &= ~GMT_DR_OPEN(~0); | |
53f8c573 | 223 | if (device->medium_state == MS_UNLOADED) |
59e36927 MH |
224 | pr_info("%s: A tape cartridge has been mounted\n", |
225 | dev_name(&device->cdev->dev)); | |
1da177e4 LT |
226 | break; |
227 | default: | |
228 | // print nothing | |
229 | break; | |
230 | } | |
231 | device->medium_state = newstate; | |
232 | wake_up(&device->state_change_wq); | |
233 | } | |
234 | ||
235 | /* | |
236 | * Stop running ccw. Has to be called with the device lock held. | |
237 | */ | |
4d284cac | 238 | static int |
4111796d | 239 | __tape_cancel_io(struct tape_device *device, struct tape_request *request) |
1da177e4 LT |
240 | { |
241 | int retries; | |
242 | int rc; | |
243 | ||
244 | /* Check if interrupt has already been processed */ | |
245 | if (request->callback == NULL) | |
246 | return 0; | |
247 | ||
248 | rc = 0; | |
249 | for (retries = 0; retries < 5; retries++) { | |
250 | rc = ccw_device_clear(device->cdev, (long) request); | |
251 | ||
4111796d SB |
252 | switch (rc) { |
253 | case 0: | |
254 | request->status = TAPE_REQUEST_DONE; | |
255 | return 0; | |
256 | case -EBUSY: | |
257 | request->status = TAPE_REQUEST_CANCEL; | |
c1637532 | 258 | schedule_delayed_work(&device->tape_dnr, 0); |
4111796d SB |
259 | return 0; |
260 | case -ENODEV: | |
261 | DBF_EXCEPTION(2, "device gone, retry\n"); | |
262 | break; | |
263 | case -EIO: | |
264 | DBF_EXCEPTION(2, "I/O error, retry\n"); | |
265 | break; | |
266 | default: | |
267 | BUG(); | |
1da177e4 | 268 | } |
1da177e4 LT |
269 | } |
270 | ||
271 | return rc; | |
272 | } | |
273 | ||
274 | /* | |
275 | * Add device into the sorted list, giving it the first | |
276 | * available minor number. | |
277 | */ | |
278 | static int | |
279 | tape_assign_minor(struct tape_device *device) | |
280 | { | |
281 | struct tape_device *tmp; | |
282 | int minor; | |
283 | ||
284 | minor = 0; | |
285 | write_lock(&tape_device_lock); | |
286 | list_for_each_entry(tmp, &tape_device_list, node) { | |
287 | if (minor < tmp->first_minor) | |
288 | break; | |
289 | minor += TAPE_MINORS_PER_DEV; | |
290 | } | |
291 | if (minor >= 256) { | |
292 | write_unlock(&tape_device_lock); | |
293 | return -ENODEV; | |
294 | } | |
295 | device->first_minor = minor; | |
296 | list_add_tail(&device->node, &tmp->node); | |
297 | write_unlock(&tape_device_lock); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | /* remove device from the list */ | |
302 | static void | |
303 | tape_remove_minor(struct tape_device *device) | |
304 | { | |
305 | write_lock(&tape_device_lock); | |
306 | list_del_init(&device->node); | |
307 | device->first_minor = -1; | |
308 | write_unlock(&tape_device_lock); | |
309 | } | |
310 | ||
311 | /* | |
312 | * Set a device online. | |
313 | * | |
314 | * This function is called by the common I/O layer to move a device from the | |
315 | * detected but offline into the online state. | |
316 | * If we return an error (RC < 0) the device remains in the offline state. This | |
317 | * can happen if the device is assigned somewhere else, for example. | |
318 | */ | |
319 | int | |
320 | tape_generic_online(struct tape_device *device, | |
321 | struct tape_discipline *discipline) | |
322 | { | |
323 | int rc; | |
324 | ||
325 | DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline); | |
326 | ||
327 | if (device->tape_state != TS_INIT) { | |
328 | DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state); | |
329 | return -EINVAL; | |
330 | } | |
331 | ||
cced1dd4 MH |
332 | init_timer(&device->lb_timeout); |
333 | device->lb_timeout.function = tape_long_busy_timeout; | |
334 | ||
1da177e4 LT |
335 | /* Let the discipline have a go at the device. */ |
336 | device->discipline = discipline; | |
337 | if (!try_module_get(discipline->owner)) { | |
1da177e4 LT |
338 | return -EINVAL; |
339 | } | |
340 | ||
341 | rc = discipline->setup_device(device); | |
342 | if (rc) | |
343 | goto out; | |
344 | rc = tape_assign_minor(device); | |
345 | if (rc) | |
346 | goto out_discipline; | |
347 | ||
348 | rc = tapechar_setup_device(device); | |
349 | if (rc) | |
350 | goto out_minor; | |
351 | rc = tapeblock_setup_device(device); | |
352 | if (rc) | |
353 | goto out_char; | |
354 | ||
355 | tape_state_set(device, TS_UNUSED); | |
356 | ||
357 | DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id); | |
358 | ||
359 | return 0; | |
360 | ||
361 | out_char: | |
362 | tapechar_cleanup_device(device); | |
68d36bdb RK |
363 | out_minor: |
364 | tape_remove_minor(device); | |
1da177e4 LT |
365 | out_discipline: |
366 | device->discipline->cleanup_device(device); | |
367 | device->discipline = NULL; | |
1da177e4 LT |
368 | out: |
369 | module_put(discipline->owner); | |
370 | return rc; | |
371 | } | |
372 | ||
4d284cac | 373 | static void |
1da177e4 LT |
374 | tape_cleanup_device(struct tape_device *device) |
375 | { | |
376 | tapeblock_cleanup_device(device); | |
377 | tapechar_cleanup_device(device); | |
378 | device->discipline->cleanup_device(device); | |
379 | module_put(device->discipline->owner); | |
380 | tape_remove_minor(device); | |
381 | tape_med_state_set(device, MS_UNKNOWN); | |
382 | } | |
383 | ||
3ef32e62 FM |
384 | /* |
385 | * Suspend device. | |
386 | * | |
387 | * Called by the common I/O layer if the drive should be suspended on user | |
388 | * request. We refuse to suspend if the device is loaded or in use for the | |
389 | * following reason: | |
390 | * While the Linux guest is suspended, it might be logged off which causes | |
391 | * devices to be detached. Tape devices are automatically rewound and unloaded | |
392 | * during DETACH processing (unless the tape device was attached with the | |
393 | * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to | |
394 | * resume the original state of the tape device, since we would need to | |
395 | * manually re-load the cartridge which was active at suspend time. | |
396 | */ | |
397 | int tape_generic_pm_suspend(struct ccw_device *cdev) | |
398 | { | |
399 | struct tape_device *device; | |
400 | ||
4f0076f7 | 401 | device = dev_get_drvdata(&cdev->dev); |
3ef32e62 FM |
402 | if (!device) { |
403 | return -ENODEV; | |
404 | } | |
405 | ||
406 | DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n", | |
407 | device->cdev_id, device); | |
408 | ||
409 | if (device->medium_state != MS_UNLOADED) { | |
410 | pr_err("A cartridge is loaded in tape device %s, " | |
411 | "refusing to suspend\n", dev_name(&cdev->dev)); | |
412 | return -EBUSY; | |
413 | } | |
414 | ||
415 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
416 | switch (device->tape_state) { | |
417 | case TS_INIT: | |
418 | case TS_NOT_OPER: | |
419 | case TS_UNUSED: | |
420 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
421 | break; | |
422 | default: | |
423 | pr_err("Tape device %s is busy, refusing to " | |
424 | "suspend\n", dev_name(&cdev->dev)); | |
425 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
426 | return -EBUSY; | |
427 | } | |
428 | ||
429 | DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id); | |
430 | return 0; | |
431 | } | |
432 | ||
1da177e4 LT |
433 | /* |
434 | * Set device offline. | |
435 | * | |
436 | * Called by the common I/O layer if the drive should set offline on user | |
437 | * request. We may prevent this by returning an error. | |
438 | * Manual offline is only allowed while the drive is not in use. | |
439 | */ | |
440 | int | |
4d7a3cdf | 441 | tape_generic_offline(struct ccw_device *cdev) |
1da177e4 | 442 | { |
4d7a3cdf FM |
443 | struct tape_device *device; |
444 | ||
dff59b64 | 445 | device = dev_get_drvdata(&cdev->dev); |
1da177e4 | 446 | if (!device) { |
1da177e4 LT |
447 | return -ENODEV; |
448 | } | |
449 | ||
450 | DBF_LH(3, "(%08x): tape_generic_offline(%p)\n", | |
451 | device->cdev_id, device); | |
452 | ||
453 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
454 | switch (device->tape_state) { | |
455 | case TS_INIT: | |
456 | case TS_NOT_OPER: | |
457 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
458 | break; | |
459 | case TS_UNUSED: | |
460 | tape_state_set(device, TS_INIT); | |
461 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
462 | tape_cleanup_device(device); | |
463 | break; | |
464 | default: | |
465 | DBF_EVENT(3, "(%08x): Set offline failed " | |
466 | "- drive in use.\n", | |
467 | device->cdev_id); | |
1da177e4 LT |
468 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
469 | return -EBUSY; | |
470 | } | |
471 | ||
472 | DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id); | |
473 | return 0; | |
474 | } | |
475 | ||
476 | /* | |
477 | * Allocate memory for a new device structure. | |
478 | */ | |
479 | static struct tape_device * | |
480 | tape_alloc_device(void) | |
481 | { | |
482 | struct tape_device *device; | |
483 | ||
88abaab4 | 484 | device = kzalloc(sizeof(struct tape_device), GFP_KERNEL); |
1da177e4 LT |
485 | if (device == NULL) { |
486 | DBF_EXCEPTION(2, "ti:no mem\n"); | |
1da177e4 LT |
487 | return ERR_PTR(-ENOMEM); |
488 | } | |
88abaab4 | 489 | device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA); |
1da177e4 LT |
490 | if (device->modeset_byte == NULL) { |
491 | DBF_EXCEPTION(2, "ti:no mem\n"); | |
1da177e4 LT |
492 | kfree(device); |
493 | return ERR_PTR(-ENOMEM); | |
494 | } | |
495 | INIT_LIST_HEAD(&device->req_queue); | |
496 | INIT_LIST_HEAD(&device->node); | |
497 | init_waitqueue_head(&device->state_change_wq); | |
4657fb8a | 498 | init_waitqueue_head(&device->wait_queue); |
1da177e4 LT |
499 | device->tape_state = TS_INIT; |
500 | device->medium_state = MS_UNKNOWN; | |
501 | *device->modeset_byte = 0; | |
502 | device->first_minor = -1; | |
503 | atomic_set(&device->ref_count, 1); | |
c1637532 | 504 | INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request); |
1da177e4 LT |
505 | |
506 | return device; | |
507 | } | |
508 | ||
509 | /* | |
510 | * Get a reference to an existing device structure. This will automatically | |
511 | * increment the reference count. | |
512 | */ | |
513 | struct tape_device * | |
8fd138c3 | 514 | tape_get_device(struct tape_device *device) |
1da177e4 | 515 | { |
8fd138c3 | 516 | int count; |
1da177e4 | 517 | |
8fd138c3 MS |
518 | count = atomic_inc_return(&device->ref_count); |
519 | DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count); | |
1da177e4 LT |
520 | return device; |
521 | } | |
522 | ||
523 | /* | |
524 | * Decrease the reference counter of a devices structure. If the | |
525 | * reference counter reaches zero free the device structure. | |
526 | * The function returns a NULL pointer to be used by the caller | |
527 | * for clearing reference pointers. | |
528 | */ | |
8fd138c3 | 529 | void |
1da177e4 LT |
530 | tape_put_device(struct tape_device *device) |
531 | { | |
8fd138c3 | 532 | int count; |
1da177e4 | 533 | |
8fd138c3 MS |
534 | count = atomic_dec_return(&device->ref_count); |
535 | DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count); | |
536 | BUG_ON(count < 0); | |
537 | if (count == 0) { | |
538 | kfree(device->modeset_byte); | |
539 | kfree(device); | |
1da177e4 | 540 | } |
1da177e4 LT |
541 | } |
542 | ||
543 | /* | |
544 | * Find tape device by a device index. | |
545 | */ | |
546 | struct tape_device * | |
8fd138c3 | 547 | tape_find_device(int devindex) |
1da177e4 LT |
548 | { |
549 | struct tape_device *device, *tmp; | |
550 | ||
551 | device = ERR_PTR(-ENODEV); | |
552 | read_lock(&tape_device_lock); | |
553 | list_for_each_entry(tmp, &tape_device_list, node) { | |
554 | if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) { | |
8fd138c3 | 555 | device = tape_get_device(tmp); |
1da177e4 LT |
556 | break; |
557 | } | |
558 | } | |
559 | read_unlock(&tape_device_lock); | |
560 | return device; | |
561 | } | |
562 | ||
563 | /* | |
564 | * Driverfs tape probe function. | |
565 | */ | |
566 | int | |
567 | tape_generic_probe(struct ccw_device *cdev) | |
568 | { | |
569 | struct tape_device *device; | |
d7cf0d57 | 570 | int ret; |
f455adcf | 571 | struct ccw_dev_id dev_id; |
1da177e4 LT |
572 | |
573 | device = tape_alloc_device(); | |
574 | if (IS_ERR(device)) | |
575 | return -ENODEV; | |
454e1fa1 PO |
576 | ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP | |
577 | CCWDEV_DO_MULTIPATH); | |
d7cf0d57 HC |
578 | ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group); |
579 | if (ret) { | |
580 | tape_put_device(device); | |
d7cf0d57 HC |
581 | return ret; |
582 | } | |
dff59b64 | 583 | dev_set_drvdata(&cdev->dev, device); |
d7cf0d57 | 584 | cdev->handler = __tape_do_irq; |
1da177e4 | 585 | device->cdev = cdev; |
f455adcf CH |
586 | ccw_device_get_id(cdev, &dev_id); |
587 | device->cdev_id = devid_to_int(&dev_id); | |
d7cf0d57 | 588 | return ret; |
1da177e4 LT |
589 | } |
590 | ||
4d284cac | 591 | static void |
1da177e4 LT |
592 | __tape_discard_requests(struct tape_device *device) |
593 | { | |
594 | struct tape_request * request; | |
595 | struct list_head * l, *n; | |
596 | ||
597 | list_for_each_safe(l, n, &device->req_queue) { | |
598 | request = list_entry(l, struct tape_request, list); | |
599 | if (request->status == TAPE_REQUEST_IN_IO) | |
600 | request->status = TAPE_REQUEST_DONE; | |
601 | list_del(&request->list); | |
602 | ||
603 | /* Decrease ref_count for removed request. */ | |
8fd138c3 MS |
604 | request->device = NULL; |
605 | tape_put_device(device); | |
1da177e4 LT |
606 | request->rc = -EIO; |
607 | if (request->callback != NULL) | |
608 | request->callback(request, request->callback_data); | |
609 | } | |
610 | } | |
611 | ||
612 | /* | |
613 | * Driverfs tape remove function. | |
614 | * | |
615 | * This function is called whenever the common I/O layer detects the device | |
616 | * gone. This can happen at any time and we cannot refuse. | |
617 | */ | |
618 | void | |
619 | tape_generic_remove(struct ccw_device *cdev) | |
620 | { | |
621 | struct tape_device * device; | |
622 | ||
dff59b64 | 623 | device = dev_get_drvdata(&cdev->dev); |
1da177e4 | 624 | if (!device) { |
1da177e4 LT |
625 | return; |
626 | } | |
627 | DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev); | |
628 | ||
629 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
630 | switch (device->tape_state) { | |
631 | case TS_INIT: | |
632 | tape_state_set(device, TS_NOT_OPER); | |
633 | case TS_NOT_OPER: | |
634 | /* | |
635 | * Nothing to do. | |
636 | */ | |
637 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
638 | break; | |
639 | case TS_UNUSED: | |
640 | /* | |
641 | * Need only to release the device. | |
642 | */ | |
643 | tape_state_set(device, TS_NOT_OPER); | |
644 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
645 | tape_cleanup_device(device); | |
646 | break; | |
647 | default: | |
648 | /* | |
649 | * There may be requests on the queue. We will not get | |
650 | * an interrupt for a request that was running. So we | |
651 | * just post them all as I/O errors. | |
652 | */ | |
653 | DBF_EVENT(3, "(%08x): Drive in use vanished!\n", | |
654 | device->cdev_id); | |
59e36927 MH |
655 | pr_warning("%s: A tape unit was detached while in " |
656 | "use\n", dev_name(&device->cdev->dev)); | |
1da177e4 LT |
657 | tape_state_set(device, TS_NOT_OPER); |
658 | __tape_discard_requests(device); | |
659 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
660 | tape_cleanup_device(device); | |
661 | } | |
662 | ||
8fd138c3 MS |
663 | device = dev_get_drvdata(&cdev->dev); |
664 | if (device) { | |
1da177e4 | 665 | sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group); |
8fd138c3 MS |
666 | dev_set_drvdata(&cdev->dev, NULL); |
667 | tape_put_device(device); | |
1da177e4 LT |
668 | } |
669 | } | |
670 | ||
671 | /* | |
672 | * Allocate a new tape ccw request | |
673 | */ | |
674 | struct tape_request * | |
675 | tape_alloc_request(int cplength, int datasize) | |
676 | { | |
677 | struct tape_request *request; | |
678 | ||
6aa0d3a9 | 679 | BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE); |
1da177e4 LT |
680 | |
681 | DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize); | |
682 | ||
88abaab4 | 683 | request = kzalloc(sizeof(struct tape_request), GFP_KERNEL); |
1da177e4 LT |
684 | if (request == NULL) { |
685 | DBF_EXCEPTION(1, "cqra nomem\n"); | |
686 | return ERR_PTR(-ENOMEM); | |
687 | } | |
1da177e4 LT |
688 | /* allocate channel program */ |
689 | if (cplength > 0) { | |
88abaab4 | 690 | request->cpaddr = kcalloc(cplength, sizeof(struct ccw1), |
1da177e4 LT |
691 | 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 | } | |
1da177e4 LT |
697 | } |
698 | /* alloc small kernel buffer */ | |
699 | if (datasize > 0) { | |
88abaab4 | 700 | request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA); |
1da177e4 LT |
701 | if (request->cpdata == NULL) { |
702 | DBF_EXCEPTION(1, "cqra nomem\n"); | |
17fd682e | 703 | kfree(request->cpaddr); |
1da177e4 LT |
704 | kfree(request); |
705 | return ERR_PTR(-ENOMEM); | |
706 | } | |
1da177e4 LT |
707 | } |
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 | */ | |
717 | void | |
718 | tape_free_request (struct tape_request * request) | |
719 | { | |
720 | DBF_LH(6, "Free request %p\n", request); | |
721 | ||
8fd138c3 MS |
722 | if (request->device) |
723 | tape_put_device(request->device); | |
17fd682e JJ |
724 | kfree(request->cpdata); |
725 | kfree(request->cpaddr); | |
1da177e4 LT |
726 | kfree(request); |
727 | } | |
728 | ||
4d284cac | 729 | static int |
4111796d SB |
730 | __tape_start_io(struct tape_device *device, struct tape_request *request) |
731 | { | |
732 | int rc; | |
733 | ||
734 | #ifdef CONFIG_S390_TAPE_BLOCK | |
735 | if (request->op == TO_BLOCK) | |
736 | device->discipline->check_locate(device, request); | |
737 | #endif | |
738 | rc = ccw_device_start( | |
739 | device->cdev, | |
740 | request->cpaddr, | |
741 | (unsigned long) request, | |
742 | 0x00, | |
743 | request->options | |
744 | ); | |
745 | if (rc == 0) { | |
746 | request->status = TAPE_REQUEST_IN_IO; | |
747 | } else if (rc == -EBUSY) { | |
748 | /* The common I/O subsystem is currently busy. Retry later. */ | |
749 | request->status = TAPE_REQUEST_QUEUED; | |
c1637532 | 750 | schedule_delayed_work(&device->tape_dnr, 0); |
4111796d SB |
751 | rc = 0; |
752 | } else { | |
753 | /* Start failed. Remove request and indicate failure. */ | |
754 | DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc); | |
755 | } | |
756 | return rc; | |
757 | } | |
758 | ||
4d284cac | 759 | static void |
4111796d | 760 | __tape_start_next_request(struct tape_device *device) |
1da177e4 LT |
761 | { |
762 | struct list_head *l, *n; | |
763 | struct tape_request *request; | |
764 | int rc; | |
765 | ||
4111796d | 766 | DBF_LH(6, "__tape_start_next_request(%p)\n", device); |
1da177e4 LT |
767 | /* |
768 | * Try to start each request on request queue until one is | |
769 | * started successful. | |
770 | */ | |
771 | list_for_each_safe(l, n, &device->req_queue) { | |
772 | request = list_entry(l, struct tape_request, list); | |
4111796d SB |
773 | |
774 | /* | |
775 | * Avoid race condition if bottom-half was triggered more than | |
776 | * once. | |
777 | */ | |
778 | if (request->status == TAPE_REQUEST_IN_IO) | |
779 | return; | |
5f384338 MH |
780 | /* |
781 | * Request has already been stopped. We have to wait until | |
782 | * the request is removed from the queue in the interrupt | |
783 | * handling. | |
784 | */ | |
785 | if (request->status == TAPE_REQUEST_DONE) | |
786 | return; | |
4111796d SB |
787 | |
788 | /* | |
789 | * We wanted to cancel the request but the common I/O layer | |
790 | * was busy at that time. This can only happen if this | |
791 | * function is called by delayed_next_request. | |
792 | * Otherwise we start the next request on the queue. | |
793 | */ | |
794 | if (request->status == TAPE_REQUEST_CANCEL) { | |
795 | rc = __tape_cancel_io(device, request); | |
796 | } else { | |
797 | rc = __tape_start_io(device, request); | |
1da177e4 | 798 | } |
4111796d SB |
799 | if (rc == 0) |
800 | return; | |
1da177e4 | 801 | |
4111796d | 802 | /* Set ending status. */ |
1da177e4 LT |
803 | request->rc = rc; |
804 | request->status = TAPE_REQUEST_DONE; | |
4111796d SB |
805 | |
806 | /* Remove from request queue. */ | |
807 | list_del(&request->list); | |
808 | ||
809 | /* Do callback. */ | |
810 | if (request->callback != NULL) | |
811 | request->callback(request, request->callback_data); | |
1da177e4 LT |
812 | } |
813 | } | |
814 | ||
815 | static void | |
c1637532 | 816 | tape_delayed_next_request(struct work_struct *work) |
1da177e4 | 817 | { |
c1637532 MS |
818 | struct tape_device *device = |
819 | container_of(work, struct tape_device, tape_dnr.work); | |
1da177e4 | 820 | |
4111796d SB |
821 | DBF_LH(6, "tape_delayed_next_request(%p)\n", device); |
822 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
823 | __tape_start_next_request(device); | |
824 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
825 | } | |
826 | ||
cced1dd4 MH |
827 | static void tape_long_busy_timeout(unsigned long data) |
828 | { | |
829 | struct tape_request *request; | |
830 | struct tape_device *device; | |
831 | ||
832 | device = (struct tape_device *) data; | |
833 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
834 | request = list_entry(device->req_queue.next, struct tape_request, list); | |
6aa0d3a9 | 835 | BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY); |
cced1dd4 MH |
836 | DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id); |
837 | __tape_start_next_request(device); | |
8fd138c3 MS |
838 | device->lb_timeout.data = 0UL; |
839 | tape_put_device(device); | |
cced1dd4 MH |
840 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
841 | } | |
842 | ||
4d284cac | 843 | static void |
4111796d SB |
844 | __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 | } | |
1da177e4 LT |
861 | |
862 | /* Start next request. */ | |
863 | if (!list_empty(&device->req_queue)) | |
4111796d | 864 | __tape_start_next_request(device); |
1da177e4 LT |
865 | } |
866 | ||
1da177e4 LT |
867 | /* |
868 | * Write sense data to dbf | |
869 | */ | |
870 | void | |
871 | tape_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", | |
23d805b6 | 882 | irb->scsw.cmd.dstat, irb->scsw.cmd.cstat); |
1da177e4 LT |
883 | 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 | */ | |
4d284cac | 896 | static int |
4111796d | 897 | __tape_start_request(struct tape_device *device, struct tape_request *request) |
1da177e4 LT |
898 | { |
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: | |
e2963062 | 906 | case TO_RDC: |
1da177e4 LT |
907 | 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. */ | |
8fd138c3 | 919 | request->device = tape_get_device(device); |
1da177e4 LT |
920 | |
921 | if (list_empty(&device->req_queue)) { | |
922 | /* No other requests are on the queue. Start this one. */ | |
4111796d SB |
923 | rc = __tape_start_io(device, request); |
924 | if (rc) | |
1da177e4 | 925 | return rc; |
4111796d | 926 | |
1da177e4 LT |
927 | DBF_LH(5, "Request %p added for execution.\n", request); |
928 | list_add(&request->list, &device->req_queue); | |
1da177e4 LT |
929 | } else { |
930 | DBF_LH(5, "Request %p add to queue.\n", request); | |
1da177e4 | 931 | request->status = TAPE_REQUEST_QUEUED; |
4111796d | 932 | list_add_tail(&request->list, &device->req_queue); |
1da177e4 LT |
933 | } |
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 | */ | |
941 | int | |
942 | tape_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. */ | |
4111796d | 950 | rc = __tape_start_request(device, request); |
1da177e4 LT |
951 | 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 | */ | |
960 | static 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 | ||
967 | int | |
968 | tape_do_io(struct tape_device *device, struct tape_request *request) | |
969 | { | |
1da177e4 LT |
970 | int rc; |
971 | ||
1da177e4 LT |
972 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
973 | /* Setup callback */ | |
974 | request->callback = __tape_wake_up; | |
4657fb8a | 975 | request->callback_data = &device->wait_queue; |
1da177e4 | 976 | /* Add request to request queue and try to start it. */ |
4111796d | 977 | rc = __tape_start_request(device, request); |
1da177e4 LT |
978 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
979 | if (rc) | |
980 | return rc; | |
981 | /* Request added to the queue. Wait for its completion. */ | |
4657fb8a | 982 | wait_event(device->wait_queue, (request->callback == NULL)); |
1da177e4 LT |
983 | /* 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 | */ | |
992 | static 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 | ||
999 | int | |
1000 | tape_do_io_interruptible(struct tape_device *device, | |
1001 | struct tape_request *request) | |
1002 | { | |
1da177e4 LT |
1003 | int rc; |
1004 | ||
1da177e4 LT |
1005 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
1006 | /* Setup callback */ | |
1007 | request->callback = __tape_wake_up_interruptible; | |
4657fb8a | 1008 | request->callback_data = &device->wait_queue; |
4111796d | 1009 | rc = __tape_start_request(device, request); |
1da177e4 LT |
1010 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
1011 | if (rc) | |
1012 | return rc; | |
1013 | /* Request added to the queue. Wait for its completion. */ | |
4657fb8a MS |
1014 | rc = wait_event_interruptible(device->wait_queue, |
1015 | (request->callback == NULL)); | |
1da177e4 LT |
1016 | if (rc != -ERESTARTSYS) |
1017 | /* Request finished normally. */ | |
1018 | return request->rc; | |
4111796d | 1019 | |
1da177e4 LT |
1020 | /* Interrupted by a signal. We have to stop the current request. */ |
1021 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
4111796d SB |
1022 | rc = __tape_cancel_io(device, request); |
1023 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
1da177e4 | 1024 | if (rc == 0) { |
4111796d SB |
1025 | /* Wait for the interrupt that acknowledges the halt. */ |
1026 | do { | |
1027 | rc = wait_event_interruptible( | |
4657fb8a | 1028 | device->wait_queue, |
4111796d SB |
1029 | (request->callback == NULL) |
1030 | ); | |
4cd190a7 | 1031 | } while (rc == -ERESTARTSYS); |
4111796d | 1032 | |
1da177e4 LT |
1033 | DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id); |
1034 | rc = -ERESTARTSYS; | |
1035 | } | |
1da177e4 LT |
1036 | return rc; |
1037 | } | |
1038 | ||
5f384338 MH |
1039 | /* |
1040 | * Stop running ccw. | |
1041 | */ | |
1042 | int | |
1043 | tape_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 | ||
1da177e4 LT |
1053 | /* |
1054 | * Tape interrupt routine, called from the ccw_device layer | |
1055 | */ | |
1056 | static 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; | |
1da177e4 LT |
1061 | int rc; |
1062 | ||
dff59b64 | 1063 | device = dev_get_drvdata(&cdev->dev); |
1da177e4 | 1064 | if (device == NULL) { |
1da177e4 LT |
1065 | 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)) { | |
4111796d | 1073 | /* FIXME: What to do with the request? */ |
1da177e4 LT |
1074 | switch (PTR_ERR(irb)) { |
1075 | case -ETIMEDOUT: | |
ab640db0 | 1076 | DBF_LH(1, "(%s): Request timed out\n", |
2a0217d5 | 1077 | dev_name(&cdev->dev)); |
1da177e4 | 1078 | case -EIO: |
4111796d | 1079 | __tape_end_request(device, request, -EIO); |
1da177e4 LT |
1080 | break; |
1081 | default: | |
ab640db0 | 1082 | DBF_LH(1, "(%s): Unexpected i/o error %li\n", |
2a0217d5 | 1083 | dev_name(&cdev->dev), |
1da177e4 LT |
1084 | PTR_ERR(irb)); |
1085 | } | |
1086 | return; | |
1087 | } | |
1088 | ||
4111796d SB |
1089 | /* |
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 | |
842d3fba SB |
1092 | * 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. | |
4111796d | 1095 | */ |
23d805b6 PO |
1096 | if (irb->scsw.cmd.cc != 0 && |
1097 | (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) && | |
5f384338 MH |
1098 | (request->status == TAPE_REQUEST_IN_IO)) { |
1099 | DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n", | |
23d805b6 | 1100 | device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl); |
842d3fba | 1101 | request->status = TAPE_REQUEST_QUEUED; |
5f384338 | 1102 | schedule_delayed_work(&device->tape_dnr, HZ); |
4111796d SB |
1103 | return; |
1104 | } | |
1105 | ||
1da177e4 LT |
1106 | /* May be an unsolicited irq */ |
1107 | if(request != NULL) | |
23d805b6 PO |
1108 | request->rescnt = irb->scsw.cmd.count; |
1109 | else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) && | |
cced1dd4 MH |
1110 | !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)) { | |
8fd138c3 MS |
1118 | device->lb_timeout.data = 0UL; |
1119 | tape_put_device(device); | |
cced1dd4 MH |
1120 | __tape_start_next_request(device); |
1121 | } | |
1122 | return; | |
1123 | } | |
1124 | } | |
23d805b6 | 1125 | if (irb->scsw.cmd.dstat != 0x0c) { |
1da177e4 LT |
1126 | /* 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) { | |
4111796d | 1152 | __tape_end_request(device, request, -EIO); |
1da177e4 LT |
1153 | 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 | */ | |
1da177e4 | 1164 | switch (rc) { |
4111796d SB |
1165 | 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; | |
cced1dd4 MH |
1172 | case TAPE_IO_LONG_BUSY: |
1173 | device->lb_timeout.data = | |
8fd138c3 | 1174 | (unsigned long) tape_get_device(device); |
cced1dd4 MH |
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; | |
4111796d SB |
1181 | 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"); | |
4111796d SB |
1194 | __tape_end_request(device, request, -EIO); |
1195 | } else { | |
1196 | __tape_end_request(device, request, rc); | |
1197 | } | |
1198 | break; | |
1da177e4 LT |
1199 | } |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * Tape device open function used by tape_char & tape_block frontends. | |
1204 | */ | |
1205 | int | |
1206 | tape_open(struct tape_device *device) | |
1207 | { | |
1208 | int rc; | |
1209 | ||
b3c21e49 | 1210 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
1da177e4 LT |
1211 | 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 | } | |
b3c21e49 | 1228 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
1da177e4 LT |
1229 | return rc; |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * Tape device release function used by tape_char & tape_block frontends. | |
1234 | */ | |
1235 | int | |
1236 | tape_release(struct tape_device *device) | |
1237 | { | |
b3c21e49 | 1238 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
1da177e4 LT |
1239 | if (device->tape_state == TS_IN_USE) |
1240 | tape_state_set(device, TS_UNUSED); | |
1241 | module_put(device->discipline->owner); | |
b3c21e49 | 1242 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
1da177e4 LT |
1243 | return 0; |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | * Execute a magnetic tape command a number of times. | |
1248 | */ | |
1249 | int | |
1250 | tape_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 | */ | |
1283 | static int | |
1284 | tape_init (void) | |
1285 | { | |
66a464db | 1286 | TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long)); |
1da177e4 LT |
1287 | debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view); |
1288 | #ifdef DBF_LIKE_HELL | |
1289 | debug_set_level(TAPE_DBF_AREA, 6); | |
1290 | #endif | |
e018ba1f | 1291 | DBF_EVENT(3, "tape init\n"); |
1da177e4 LT |
1292 | tape_proc_init(); |
1293 | tapechar_init (); | |
1294 | tapeblock_init (); | |
1295 | return 0; | |
1296 | } | |
1297 | ||
1298 | /* | |
1299 | * Tape exit function. | |
1300 | */ | |
1301 | static void | |
1302 | tape_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 | ||
1313 | MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and " | |
1314 | "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)"); | |
e018ba1f | 1315 | MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver"); |
1da177e4 LT |
1316 | MODULE_LICENSE("GPL"); |
1317 | ||
1318 | module_init(tape_init); | |
1319 | module_exit(tape_exit); | |
1320 | ||
1321 | EXPORT_SYMBOL(tape_generic_remove); | |
1322 | EXPORT_SYMBOL(tape_generic_probe); | |
1323 | EXPORT_SYMBOL(tape_generic_online); | |
1324 | EXPORT_SYMBOL(tape_generic_offline); | |
3ef32e62 | 1325 | EXPORT_SYMBOL(tape_generic_pm_suspend); |
1da177e4 | 1326 | EXPORT_SYMBOL(tape_put_device); |
8fd138c3 | 1327 | EXPORT_SYMBOL(tape_get_device); |
1da177e4 LT |
1328 | EXPORT_SYMBOL(tape_state_verbose); |
1329 | EXPORT_SYMBOL(tape_op_verbose); | |
1330 | EXPORT_SYMBOL(tape_state_set); | |
1331 | EXPORT_SYMBOL(tape_med_state_set); | |
1332 | EXPORT_SYMBOL(tape_alloc_request); | |
1333 | EXPORT_SYMBOL(tape_free_request); | |
1da177e4 LT |
1334 | EXPORT_SYMBOL(tape_dump_sense_dbf); |
1335 | EXPORT_SYMBOL(tape_do_io); | |
1336 | EXPORT_SYMBOL(tape_do_io_async); | |
1337 | EXPORT_SYMBOL(tape_do_io_interruptible); | |
5f384338 | 1338 | EXPORT_SYMBOL(tape_cancel_io); |
1da177e4 | 1339 | EXPORT_SYMBOL(tape_mtop); |