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 | |
4111796d | 6 | * Copyright (C) 2001,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation |
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 | ||
14 | #include <linux/config.h> | |
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 | ||
29 | #define PRINTK_HEADER "TAPE_CORE: " | |
30 | ||
31 | static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *); | |
4111796d | 32 | static void tape_delayed_next_request(void * data); |
1da177e4 LT |
33 | |
34 | /* | |
35 | * One list to contain all tape devices of all disciplines, so | |
36 | * we can assign the devices to minor numbers of the same major | |
37 | * The list is protected by the rwlock | |
38 | */ | |
39 | static struct list_head tape_device_list = LIST_HEAD_INIT(tape_device_list); | |
40 | static DEFINE_RWLOCK(tape_device_lock); | |
41 | ||
42 | /* | |
43 | * Pointer to debug area. | |
44 | */ | |
45 | debug_info_t *TAPE_DBF_AREA = NULL; | |
46 | EXPORT_SYMBOL(TAPE_DBF_AREA); | |
47 | ||
48 | /* | |
49 | * Printable strings for tape enumerations. | |
50 | */ | |
51 | const char *tape_state_verbose[TS_SIZE] = | |
52 | { | |
53 | [TS_UNUSED] = "UNUSED", | |
54 | [TS_IN_USE] = "IN_USE", | |
55 | [TS_BLKUSE] = "BLKUSE", | |
56 | [TS_INIT] = "INIT ", | |
57 | [TS_NOT_OPER] = "NOT_OP" | |
58 | }; | |
59 | ||
60 | const char *tape_op_verbose[TO_SIZE] = | |
61 | { | |
62 | [TO_BLOCK] = "BLK", [TO_BSB] = "BSB", | |
63 | [TO_BSF] = "BSF", [TO_DSE] = "DSE", | |
64 | [TO_FSB] = "FSB", [TO_FSF] = "FSF", | |
65 | [TO_LBL] = "LBL", [TO_NOP] = "NOP", | |
66 | [TO_RBA] = "RBA", [TO_RBI] = "RBI", | |
67 | [TO_RFO] = "RFO", [TO_REW] = "REW", | |
68 | [TO_RUN] = "RUN", [TO_WRI] = "WRI", | |
69 | [TO_WTM] = "WTM", [TO_MSEN] = "MSN", | |
70 | [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF", | |
71 | [TO_READ_ATTMSG] = "RAT", | |
72 | [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS", | |
73 | [TO_UNASSIGN] = "UAS" | |
74 | }; | |
75 | ||
76 | static inline int | |
77 | busid_to_int(char *bus_id) | |
78 | { | |
79 | int dec; | |
80 | int d; | |
81 | char * s; | |
82 | ||
83 | for(s = bus_id, d = 0; *s != '\0' && *s != '.'; s++) | |
84 | d = (d * 10) + (*s - '0'); | |
85 | dec = d; | |
86 | for(s++, d = 0; *s != '\0' && *s != '.'; s++) | |
87 | d = (d * 10) + (*s - '0'); | |
88 | dec = (dec << 8) + d; | |
89 | ||
90 | for(s++; *s != '\0'; s++) { | |
91 | if (*s >= '0' && *s <= '9') { | |
92 | d = *s - '0'; | |
93 | } else if (*s >= 'a' && *s <= 'f') { | |
94 | d = *s - 'a' + 10; | |
95 | } else { | |
96 | d = *s - 'A' + 10; | |
97 | } | |
98 | dec = (dec << 4) + d; | |
99 | } | |
100 | ||
101 | return dec; | |
102 | } | |
103 | ||
104 | /* | |
105 | * Some channel attached tape specific attributes. | |
106 | * | |
107 | * FIXME: In the future the first_minor and blocksize attribute should be | |
108 | * replaced by a link to the cdev tree. | |
109 | */ | |
110 | static ssize_t | |
3fd3c0a5 | 111 | tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
112 | { |
113 | struct tape_device *tdev; | |
114 | ||
115 | tdev = (struct tape_device *) dev->driver_data; | |
116 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state); | |
117 | } | |
118 | ||
119 | static | |
120 | DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL); | |
121 | ||
122 | static ssize_t | |
3fd3c0a5 | 123 | tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
124 | { |
125 | struct tape_device *tdev; | |
126 | ||
127 | tdev = (struct tape_device *) dev->driver_data; | |
128 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor); | |
129 | } | |
130 | ||
131 | static | |
132 | DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL); | |
133 | ||
134 | static ssize_t | |
3fd3c0a5 | 135 | tape_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
136 | { |
137 | struct tape_device *tdev; | |
138 | ||
139 | tdev = (struct tape_device *) dev->driver_data; | |
140 | return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ? | |
141 | "OFFLINE" : tape_state_verbose[tdev->tape_state]); | |
142 | } | |
143 | ||
144 | static | |
145 | DEVICE_ATTR(state, 0444, tape_state_show, NULL); | |
146 | ||
147 | static ssize_t | |
3fd3c0a5 | 148 | tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
149 | { |
150 | struct tape_device *tdev; | |
151 | ssize_t rc; | |
152 | ||
153 | tdev = (struct tape_device *) dev->driver_data; | |
154 | if (tdev->first_minor < 0) | |
155 | return scnprintf(buf, PAGE_SIZE, "N/A\n"); | |
156 | ||
157 | spin_lock_irq(get_ccwdev_lock(tdev->cdev)); | |
158 | if (list_empty(&tdev->req_queue)) | |
159 | rc = scnprintf(buf, PAGE_SIZE, "---\n"); | |
160 | else { | |
161 | struct tape_request *req; | |
162 | ||
163 | req = list_entry(tdev->req_queue.next, struct tape_request, | |
164 | list); | |
165 | rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]); | |
166 | } | |
167 | spin_unlock_irq(get_ccwdev_lock(tdev->cdev)); | |
168 | return rc; | |
169 | } | |
170 | ||
171 | static | |
172 | DEVICE_ATTR(operation, 0444, tape_operation_show, NULL); | |
173 | ||
174 | static ssize_t | |
3fd3c0a5 | 175 | tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
176 | { |
177 | struct tape_device *tdev; | |
178 | ||
179 | tdev = (struct tape_device *) dev->driver_data; | |
180 | ||
181 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size); | |
182 | } | |
183 | ||
184 | static | |
185 | DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL); | |
186 | ||
187 | static struct attribute *tape_attrs[] = { | |
188 | &dev_attr_medium_state.attr, | |
189 | &dev_attr_first_minor.attr, | |
190 | &dev_attr_state.attr, | |
191 | &dev_attr_operation.attr, | |
192 | &dev_attr_blocksize.attr, | |
193 | NULL | |
194 | }; | |
195 | ||
196 | static struct attribute_group tape_attr_group = { | |
197 | .attrs = tape_attrs, | |
198 | }; | |
199 | ||
200 | /* | |
201 | * Tape state functions | |
202 | */ | |
203 | void | |
204 | tape_state_set(struct tape_device *device, enum tape_state newstate) | |
205 | { | |
206 | const char *str; | |
207 | ||
208 | if (device->tape_state == TS_NOT_OPER) { | |
209 | DBF_EVENT(3, "ts_set err: not oper\n"); | |
210 | return; | |
211 | } | |
212 | DBF_EVENT(4, "ts. dev: %x\n", device->first_minor); | |
213 | if (device->tape_state < TO_SIZE && device->tape_state >= 0) | |
214 | str = tape_state_verbose[device->tape_state]; | |
215 | else | |
216 | str = "UNKNOWN TS"; | |
217 | DBF_EVENT(4, "old ts: %s\n", str); | |
218 | if (device->tape_state < TO_SIZE && device->tape_state >=0 ) | |
219 | str = tape_state_verbose[device->tape_state]; | |
220 | else | |
221 | str = "UNKNOWN TS"; | |
222 | DBF_EVENT(4, "%s\n", str); | |
223 | DBF_EVENT(4, "new ts:\t\n"); | |
224 | if (newstate < TO_SIZE && newstate >= 0) | |
225 | str = tape_state_verbose[newstate]; | |
226 | else | |
227 | str = "UNKNOWN TS"; | |
228 | DBF_EVENT(4, "%s\n", str); | |
229 | device->tape_state = newstate; | |
230 | wake_up(&device->state_change_wq); | |
231 | } | |
232 | ||
233 | void | |
234 | tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate) | |
235 | { | |
236 | if (device->medium_state == newstate) | |
237 | return; | |
238 | switch(newstate){ | |
239 | case MS_UNLOADED: | |
240 | device->tape_generic_status |= GMT_DR_OPEN(~0); | |
241 | PRINT_INFO("(%s): Tape is unloaded\n", | |
242 | device->cdev->dev.bus_id); | |
243 | break; | |
244 | case MS_LOADED: | |
245 | device->tape_generic_status &= ~GMT_DR_OPEN(~0); | |
246 | PRINT_INFO("(%s): Tape has been mounted\n", | |
247 | device->cdev->dev.bus_id); | |
248 | break; | |
249 | default: | |
250 | // print nothing | |
251 | break; | |
252 | } | |
253 | device->medium_state = newstate; | |
254 | wake_up(&device->state_change_wq); | |
255 | } | |
256 | ||
257 | /* | |
258 | * Stop running ccw. Has to be called with the device lock held. | |
259 | */ | |
260 | static inline int | |
4111796d | 261 | __tape_cancel_io(struct tape_device *device, struct tape_request *request) |
1da177e4 LT |
262 | { |
263 | int retries; | |
264 | int rc; | |
265 | ||
266 | /* Check if interrupt has already been processed */ | |
267 | if (request->callback == NULL) | |
268 | return 0; | |
269 | ||
270 | rc = 0; | |
271 | for (retries = 0; retries < 5; retries++) { | |
272 | rc = ccw_device_clear(device->cdev, (long) request); | |
273 | ||
4111796d SB |
274 | switch (rc) { |
275 | case 0: | |
276 | request->status = TAPE_REQUEST_DONE; | |
277 | return 0; | |
278 | case -EBUSY: | |
279 | request->status = TAPE_REQUEST_CANCEL; | |
280 | schedule_work(&device->tape_dnr); | |
281 | return 0; | |
282 | case -ENODEV: | |
283 | DBF_EXCEPTION(2, "device gone, retry\n"); | |
284 | break; | |
285 | case -EIO: | |
286 | DBF_EXCEPTION(2, "I/O error, retry\n"); | |
287 | break; | |
288 | default: | |
289 | BUG(); | |
1da177e4 | 290 | } |
1da177e4 LT |
291 | } |
292 | ||
293 | return rc; | |
294 | } | |
295 | ||
296 | /* | |
297 | * Add device into the sorted list, giving it the first | |
298 | * available minor number. | |
299 | */ | |
300 | static int | |
301 | tape_assign_minor(struct tape_device *device) | |
302 | { | |
303 | struct tape_device *tmp; | |
304 | int minor; | |
305 | ||
306 | minor = 0; | |
307 | write_lock(&tape_device_lock); | |
308 | list_for_each_entry(tmp, &tape_device_list, node) { | |
309 | if (minor < tmp->first_minor) | |
310 | break; | |
311 | minor += TAPE_MINORS_PER_DEV; | |
312 | } | |
313 | if (minor >= 256) { | |
314 | write_unlock(&tape_device_lock); | |
315 | return -ENODEV; | |
316 | } | |
317 | device->first_minor = minor; | |
318 | list_add_tail(&device->node, &tmp->node); | |
319 | write_unlock(&tape_device_lock); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | /* remove device from the list */ | |
324 | static void | |
325 | tape_remove_minor(struct tape_device *device) | |
326 | { | |
327 | write_lock(&tape_device_lock); | |
328 | list_del_init(&device->node); | |
329 | device->first_minor = -1; | |
330 | write_unlock(&tape_device_lock); | |
331 | } | |
332 | ||
333 | /* | |
334 | * Set a device online. | |
335 | * | |
336 | * This function is called by the common I/O layer to move a device from the | |
337 | * detected but offline into the online state. | |
338 | * If we return an error (RC < 0) the device remains in the offline state. This | |
339 | * can happen if the device is assigned somewhere else, for example. | |
340 | */ | |
341 | int | |
342 | tape_generic_online(struct tape_device *device, | |
343 | struct tape_discipline *discipline) | |
344 | { | |
345 | int rc; | |
346 | ||
347 | DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline); | |
348 | ||
349 | if (device->tape_state != TS_INIT) { | |
350 | DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state); | |
351 | return -EINVAL; | |
352 | } | |
353 | ||
354 | /* Let the discipline have a go at the device. */ | |
355 | device->discipline = discipline; | |
356 | if (!try_module_get(discipline->owner)) { | |
357 | PRINT_ERR("Cannot get module. Module gone.\n"); | |
358 | return -EINVAL; | |
359 | } | |
360 | ||
361 | rc = discipline->setup_device(device); | |
362 | if (rc) | |
363 | goto out; | |
364 | rc = tape_assign_minor(device); | |
365 | if (rc) | |
366 | goto out_discipline; | |
367 | ||
368 | rc = tapechar_setup_device(device); | |
369 | if (rc) | |
370 | goto out_minor; | |
371 | rc = tapeblock_setup_device(device); | |
372 | if (rc) | |
373 | goto out_char; | |
374 | ||
375 | tape_state_set(device, TS_UNUSED); | |
376 | ||
377 | DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id); | |
378 | ||
379 | return 0; | |
380 | ||
381 | out_char: | |
382 | tapechar_cleanup_device(device); | |
383 | out_discipline: | |
384 | device->discipline->cleanup_device(device); | |
385 | device->discipline = NULL; | |
386 | out_minor: | |
387 | tape_remove_minor(device); | |
388 | out: | |
389 | module_put(discipline->owner); | |
390 | return rc; | |
391 | } | |
392 | ||
393 | static inline void | |
394 | tape_cleanup_device(struct tape_device *device) | |
395 | { | |
396 | tapeblock_cleanup_device(device); | |
397 | tapechar_cleanup_device(device); | |
398 | device->discipline->cleanup_device(device); | |
399 | module_put(device->discipline->owner); | |
400 | tape_remove_minor(device); | |
401 | tape_med_state_set(device, MS_UNKNOWN); | |
402 | } | |
403 | ||
404 | /* | |
405 | * Set device offline. | |
406 | * | |
407 | * Called by the common I/O layer if the drive should set offline on user | |
408 | * request. We may prevent this by returning an error. | |
409 | * Manual offline is only allowed while the drive is not in use. | |
410 | */ | |
411 | int | |
412 | tape_generic_offline(struct tape_device *device) | |
413 | { | |
414 | if (!device) { | |
415 | PRINT_ERR("tape_generic_offline: no such device\n"); | |
416 | return -ENODEV; | |
417 | } | |
418 | ||
419 | DBF_LH(3, "(%08x): tape_generic_offline(%p)\n", | |
420 | device->cdev_id, device); | |
421 | ||
422 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
423 | switch (device->tape_state) { | |
424 | case TS_INIT: | |
425 | case TS_NOT_OPER: | |
426 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
427 | break; | |
428 | case TS_UNUSED: | |
429 | tape_state_set(device, TS_INIT); | |
430 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
431 | tape_cleanup_device(device); | |
432 | break; | |
433 | default: | |
434 | DBF_EVENT(3, "(%08x): Set offline failed " | |
435 | "- drive in use.\n", | |
436 | device->cdev_id); | |
437 | PRINT_WARN("(%s): Set offline failed " | |
438 | "- drive in use.\n", | |
439 | device->cdev->dev.bus_id); | |
440 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
441 | return -EBUSY; | |
442 | } | |
443 | ||
444 | DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | /* | |
449 | * Allocate memory for a new device structure. | |
450 | */ | |
451 | static struct tape_device * | |
452 | tape_alloc_device(void) | |
453 | { | |
454 | struct tape_device *device; | |
455 | ||
456 | device = (struct tape_device *) | |
457 | kmalloc(sizeof(struct tape_device), GFP_KERNEL); | |
458 | if (device == NULL) { | |
459 | DBF_EXCEPTION(2, "ti:no mem\n"); | |
460 | PRINT_INFO ("can't allocate memory for " | |
461 | "tape info structure\n"); | |
462 | return ERR_PTR(-ENOMEM); | |
463 | } | |
464 | memset(device, 0, sizeof(struct tape_device)); | |
465 | device->modeset_byte = (char *) kmalloc(1, GFP_KERNEL | GFP_DMA); | |
466 | if (device->modeset_byte == NULL) { | |
467 | DBF_EXCEPTION(2, "ti:no mem\n"); | |
468 | PRINT_INFO("can't allocate memory for modeset byte\n"); | |
469 | kfree(device); | |
470 | return ERR_PTR(-ENOMEM); | |
471 | } | |
472 | INIT_LIST_HEAD(&device->req_queue); | |
473 | INIT_LIST_HEAD(&device->node); | |
474 | init_waitqueue_head(&device->state_change_wq); | |
475 | device->tape_state = TS_INIT; | |
476 | device->medium_state = MS_UNKNOWN; | |
477 | *device->modeset_byte = 0; | |
478 | device->first_minor = -1; | |
479 | atomic_set(&device->ref_count, 1); | |
4111796d | 480 | INIT_WORK(&device->tape_dnr, tape_delayed_next_request, device); |
1da177e4 LT |
481 | |
482 | return device; | |
483 | } | |
484 | ||
485 | /* | |
486 | * Get a reference to an existing device structure. This will automatically | |
487 | * increment the reference count. | |
488 | */ | |
489 | struct tape_device * | |
490 | tape_get_device_reference(struct tape_device *device) | |
491 | { | |
492 | DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device, | |
493 | atomic_inc_return(&device->ref_count)); | |
494 | ||
495 | return device; | |
496 | } | |
497 | ||
498 | /* | |
499 | * Decrease the reference counter of a devices structure. If the | |
500 | * reference counter reaches zero free the device structure. | |
501 | * The function returns a NULL pointer to be used by the caller | |
502 | * for clearing reference pointers. | |
503 | */ | |
504 | struct tape_device * | |
505 | tape_put_device(struct tape_device *device) | |
506 | { | |
507 | int remain; | |
508 | ||
509 | remain = atomic_dec_return(&device->ref_count); | |
510 | if (remain > 0) { | |
511 | DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain); | |
512 | } else { | |
513 | if (remain < 0) { | |
514 | DBF_EVENT(4, "put device without reference\n"); | |
515 | PRINT_ERR("put device without reference\n"); | |
516 | } else { | |
517 | DBF_EVENT(4, "tape_free_device(%p)\n", device); | |
518 | kfree(device->modeset_byte); | |
519 | kfree(device); | |
520 | } | |
521 | } | |
522 | ||
523 | return NULL; | |
524 | } | |
525 | ||
526 | /* | |
527 | * Find tape device by a device index. | |
528 | */ | |
529 | struct tape_device * | |
530 | tape_get_device(int devindex) | |
531 | { | |
532 | struct tape_device *device, *tmp; | |
533 | ||
534 | device = ERR_PTR(-ENODEV); | |
535 | read_lock(&tape_device_lock); | |
536 | list_for_each_entry(tmp, &tape_device_list, node) { | |
537 | if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) { | |
538 | device = tape_get_device_reference(tmp); | |
539 | break; | |
540 | } | |
541 | } | |
542 | read_unlock(&tape_device_lock); | |
543 | return device; | |
544 | } | |
545 | ||
546 | /* | |
547 | * Driverfs tape probe function. | |
548 | */ | |
549 | int | |
550 | tape_generic_probe(struct ccw_device *cdev) | |
551 | { | |
552 | struct tape_device *device; | |
553 | ||
554 | device = tape_alloc_device(); | |
555 | if (IS_ERR(device)) | |
556 | return -ENODEV; | |
557 | PRINT_INFO("tape device %s found\n", cdev->dev.bus_id); | |
558 | cdev->dev.driver_data = device; | |
559 | device->cdev = cdev; | |
560 | device->cdev_id = busid_to_int(cdev->dev.bus_id); | |
561 | cdev->handler = __tape_do_irq; | |
562 | ||
563 | ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP); | |
564 | sysfs_create_group(&cdev->dev.kobj, &tape_attr_group); | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
569 | static inline void | |
570 | __tape_discard_requests(struct tape_device *device) | |
571 | { | |
572 | struct tape_request * request; | |
573 | struct list_head * l, *n; | |
574 | ||
575 | list_for_each_safe(l, n, &device->req_queue) { | |
576 | request = list_entry(l, struct tape_request, list); | |
577 | if (request->status == TAPE_REQUEST_IN_IO) | |
578 | request->status = TAPE_REQUEST_DONE; | |
579 | list_del(&request->list); | |
580 | ||
581 | /* Decrease ref_count for removed request. */ | |
582 | request->device = tape_put_device(device); | |
583 | request->rc = -EIO; | |
584 | if (request->callback != NULL) | |
585 | request->callback(request, request->callback_data); | |
586 | } | |
587 | } | |
588 | ||
589 | /* | |
590 | * Driverfs tape remove function. | |
591 | * | |
592 | * This function is called whenever the common I/O layer detects the device | |
593 | * gone. This can happen at any time and we cannot refuse. | |
594 | */ | |
595 | void | |
596 | tape_generic_remove(struct ccw_device *cdev) | |
597 | { | |
598 | struct tape_device * device; | |
599 | ||
600 | device = cdev->dev.driver_data; | |
601 | if (!device) { | |
602 | PRINT_ERR("No device pointer in tape_generic_remove!\n"); | |
603 | return; | |
604 | } | |
605 | DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev); | |
606 | ||
607 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
608 | switch (device->tape_state) { | |
609 | case TS_INIT: | |
610 | tape_state_set(device, TS_NOT_OPER); | |
611 | case TS_NOT_OPER: | |
612 | /* | |
613 | * Nothing to do. | |
614 | */ | |
615 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
616 | break; | |
617 | case TS_UNUSED: | |
618 | /* | |
619 | * Need only to release the device. | |
620 | */ | |
621 | tape_state_set(device, TS_NOT_OPER); | |
622 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
623 | tape_cleanup_device(device); | |
624 | break; | |
625 | default: | |
626 | /* | |
627 | * There may be requests on the queue. We will not get | |
628 | * an interrupt for a request that was running. So we | |
629 | * just post them all as I/O errors. | |
630 | */ | |
631 | DBF_EVENT(3, "(%08x): Drive in use vanished!\n", | |
632 | device->cdev_id); | |
633 | PRINT_WARN("(%s): Drive in use vanished - " | |
634 | "expect trouble!\n", | |
635 | device->cdev->dev.bus_id); | |
636 | PRINT_WARN("State was %i\n", device->tape_state); | |
637 | tape_state_set(device, TS_NOT_OPER); | |
638 | __tape_discard_requests(device); | |
639 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
640 | tape_cleanup_device(device); | |
641 | } | |
642 | ||
643 | if (cdev->dev.driver_data != NULL) { | |
644 | sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group); | |
645 | cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data); | |
646 | } | |
647 | } | |
648 | ||
649 | /* | |
650 | * Allocate a new tape ccw request | |
651 | */ | |
652 | struct tape_request * | |
653 | tape_alloc_request(int cplength, int datasize) | |
654 | { | |
655 | struct tape_request *request; | |
656 | ||
657 | if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE) | |
658 | BUG(); | |
659 | ||
660 | DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize); | |
661 | ||
662 | request = (struct tape_request *) kmalloc(sizeof(struct tape_request), | |
663 | GFP_KERNEL); | |
664 | if (request == NULL) { | |
665 | DBF_EXCEPTION(1, "cqra nomem\n"); | |
666 | return ERR_PTR(-ENOMEM); | |
667 | } | |
668 | memset(request, 0, sizeof(struct tape_request)); | |
669 | /* allocate channel program */ | |
670 | if (cplength > 0) { | |
671 | request->cpaddr = kmalloc(cplength*sizeof(struct ccw1), | |
672 | GFP_ATOMIC | GFP_DMA); | |
673 | if (request->cpaddr == NULL) { | |
674 | DBF_EXCEPTION(1, "cqra nomem\n"); | |
675 | kfree(request); | |
676 | return ERR_PTR(-ENOMEM); | |
677 | } | |
678 | memset(request->cpaddr, 0, cplength*sizeof(struct ccw1)); | |
679 | } | |
680 | /* alloc small kernel buffer */ | |
681 | if (datasize > 0) { | |
682 | request->cpdata = kmalloc(datasize, GFP_KERNEL | GFP_DMA); | |
683 | if (request->cpdata == NULL) { | |
684 | DBF_EXCEPTION(1, "cqra nomem\n"); | |
17fd682e | 685 | kfree(request->cpaddr); |
1da177e4 LT |
686 | kfree(request); |
687 | return ERR_PTR(-ENOMEM); | |
688 | } | |
689 | memset(request->cpdata, 0, datasize); | |
690 | } | |
691 | DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr, | |
692 | request->cpdata); | |
693 | ||
694 | return request; | |
695 | } | |
696 | ||
697 | /* | |
698 | * Free tape ccw request | |
699 | */ | |
700 | void | |
701 | tape_free_request (struct tape_request * request) | |
702 | { | |
703 | DBF_LH(6, "Free request %p\n", request); | |
704 | ||
705 | if (request->device != NULL) { | |
706 | request->device = tape_put_device(request->device); | |
707 | } | |
17fd682e JJ |
708 | kfree(request->cpdata); |
709 | kfree(request->cpaddr); | |
1da177e4 LT |
710 | kfree(request); |
711 | } | |
712 | ||
4111796d SB |
713 | static inline int |
714 | __tape_start_io(struct tape_device *device, struct tape_request *request) | |
715 | { | |
716 | int rc; | |
717 | ||
718 | #ifdef CONFIG_S390_TAPE_BLOCK | |
719 | if (request->op == TO_BLOCK) | |
720 | device->discipline->check_locate(device, request); | |
721 | #endif | |
722 | rc = ccw_device_start( | |
723 | device->cdev, | |
724 | request->cpaddr, | |
725 | (unsigned long) request, | |
726 | 0x00, | |
727 | request->options | |
728 | ); | |
729 | if (rc == 0) { | |
730 | request->status = TAPE_REQUEST_IN_IO; | |
731 | } else if (rc == -EBUSY) { | |
732 | /* The common I/O subsystem is currently busy. Retry later. */ | |
733 | request->status = TAPE_REQUEST_QUEUED; | |
734 | schedule_work(&device->tape_dnr); | |
735 | rc = 0; | |
736 | } else { | |
737 | /* Start failed. Remove request and indicate failure. */ | |
738 | DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc); | |
739 | } | |
740 | return rc; | |
741 | } | |
742 | ||
1da177e4 | 743 | static inline void |
4111796d | 744 | __tape_start_next_request(struct tape_device *device) |
1da177e4 LT |
745 | { |
746 | struct list_head *l, *n; | |
747 | struct tape_request *request; | |
748 | int rc; | |
749 | ||
4111796d | 750 | DBF_LH(6, "__tape_start_next_request(%p)\n", device); |
1da177e4 LT |
751 | /* |
752 | * Try to start each request on request queue until one is | |
753 | * started successful. | |
754 | */ | |
755 | list_for_each_safe(l, n, &device->req_queue) { | |
756 | request = list_entry(l, struct tape_request, list); | |
4111796d SB |
757 | |
758 | /* | |
759 | * Avoid race condition if bottom-half was triggered more than | |
760 | * once. | |
761 | */ | |
762 | if (request->status == TAPE_REQUEST_IN_IO) | |
763 | return; | |
5f384338 MH |
764 | /* |
765 | * Request has already been stopped. We have to wait until | |
766 | * the request is removed from the queue in the interrupt | |
767 | * handling. | |
768 | */ | |
769 | if (request->status == TAPE_REQUEST_DONE) | |
770 | return; | |
4111796d SB |
771 | |
772 | /* | |
773 | * We wanted to cancel the request but the common I/O layer | |
774 | * was busy at that time. This can only happen if this | |
775 | * function is called by delayed_next_request. | |
776 | * Otherwise we start the next request on the queue. | |
777 | */ | |
778 | if (request->status == TAPE_REQUEST_CANCEL) { | |
779 | rc = __tape_cancel_io(device, request); | |
780 | } else { | |
781 | rc = __tape_start_io(device, request); | |
1da177e4 | 782 | } |
4111796d SB |
783 | if (rc == 0) |
784 | return; | |
1da177e4 | 785 | |
4111796d | 786 | /* Set ending status. */ |
1da177e4 LT |
787 | request->rc = rc; |
788 | request->status = TAPE_REQUEST_DONE; | |
4111796d SB |
789 | |
790 | /* Remove from request queue. */ | |
791 | list_del(&request->list); | |
792 | ||
793 | /* Do callback. */ | |
794 | if (request->callback != NULL) | |
795 | request->callback(request, request->callback_data); | |
1da177e4 LT |
796 | } |
797 | } | |
798 | ||
799 | static void | |
4111796d | 800 | tape_delayed_next_request(void *data) |
1da177e4 | 801 | { |
4111796d | 802 | struct tape_device * device; |
1da177e4 | 803 | |
4111796d SB |
804 | device = (struct tape_device *) data; |
805 | DBF_LH(6, "tape_delayed_next_request(%p)\n", device); | |
806 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
807 | __tape_start_next_request(device); | |
808 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
809 | } | |
810 | ||
811 | static inline void | |
812 | __tape_end_request( | |
813 | struct tape_device * device, | |
814 | struct tape_request * request, | |
815 | int rc) | |
816 | { | |
817 | DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc); | |
818 | if (request) { | |
819 | request->rc = rc; | |
820 | request->status = TAPE_REQUEST_DONE; | |
821 | ||
822 | /* Remove from request queue. */ | |
823 | list_del(&request->list); | |
824 | ||
825 | /* Do callback. */ | |
826 | if (request->callback != NULL) | |
827 | request->callback(request, request->callback_data); | |
828 | } | |
1da177e4 LT |
829 | |
830 | /* Start next request. */ | |
831 | if (!list_empty(&device->req_queue)) | |
4111796d | 832 | __tape_start_next_request(device); |
1da177e4 LT |
833 | } |
834 | ||
835 | /* | |
836 | * Write sense data to console/dbf | |
837 | */ | |
838 | void | |
839 | tape_dump_sense(struct tape_device* device, struct tape_request *request, | |
840 | struct irb *irb) | |
841 | { | |
842 | unsigned int *sptr; | |
843 | ||
844 | PRINT_INFO("-------------------------------------------------\n"); | |
845 | PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n", | |
846 | irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa); | |
847 | PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id); | |
848 | if (request != NULL) | |
849 | PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]); | |
850 | ||
851 | sptr = (unsigned int *) irb->ecw; | |
852 | PRINT_INFO("Sense data: %08X %08X %08X %08X \n", | |
853 | sptr[0], sptr[1], sptr[2], sptr[3]); | |
854 | PRINT_INFO("Sense data: %08X %08X %08X %08X \n", | |
855 | sptr[4], sptr[5], sptr[6], sptr[7]); | |
856 | PRINT_INFO("--------------------------------------------------\n"); | |
857 | } | |
858 | ||
859 | /* | |
860 | * Write sense data to dbf | |
861 | */ | |
862 | void | |
863 | tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request, | |
864 | struct irb *irb) | |
865 | { | |
866 | unsigned int *sptr; | |
867 | const char* op; | |
868 | ||
869 | if (request != NULL) | |
870 | op = tape_op_verbose[request->op]; | |
871 | else | |
872 | op = "---"; | |
873 | DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n", | |
874 | irb->scsw.dstat,irb->scsw.cstat); | |
875 | DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op); | |
876 | sptr = (unsigned int *) irb->ecw; | |
877 | DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]); | |
878 | DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]); | |
879 | DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]); | |
880 | DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]); | |
881 | } | |
882 | ||
883 | /* | |
884 | * I/O helper function. Adds the request to the request queue | |
885 | * and starts it if the tape is idle. Has to be called with | |
886 | * the device lock held. | |
887 | */ | |
888 | static inline int | |
4111796d | 889 | __tape_start_request(struct tape_device *device, struct tape_request *request) |
1da177e4 LT |
890 | { |
891 | int rc; | |
892 | ||
893 | switch (request->op) { | |
894 | case TO_MSEN: | |
895 | case TO_ASSIGN: | |
896 | case TO_UNASSIGN: | |
897 | case TO_READ_ATTMSG: | |
898 | if (device->tape_state == TS_INIT) | |
899 | break; | |
900 | if (device->tape_state == TS_UNUSED) | |
901 | break; | |
902 | default: | |
903 | if (device->tape_state == TS_BLKUSE) | |
904 | break; | |
905 | if (device->tape_state != TS_IN_USE) | |
906 | return -ENODEV; | |
907 | } | |
908 | ||
909 | /* Increase use count of device for the added request. */ | |
910 | request->device = tape_get_device_reference(device); | |
911 | ||
912 | if (list_empty(&device->req_queue)) { | |
913 | /* No other requests are on the queue. Start this one. */ | |
4111796d SB |
914 | rc = __tape_start_io(device, request); |
915 | if (rc) | |
1da177e4 | 916 | return rc; |
4111796d | 917 | |
1da177e4 LT |
918 | DBF_LH(5, "Request %p added for execution.\n", request); |
919 | list_add(&request->list, &device->req_queue); | |
1da177e4 LT |
920 | } else { |
921 | DBF_LH(5, "Request %p add to queue.\n", request); | |
1da177e4 | 922 | request->status = TAPE_REQUEST_QUEUED; |
4111796d | 923 | list_add_tail(&request->list, &device->req_queue); |
1da177e4 LT |
924 | } |
925 | return 0; | |
926 | } | |
927 | ||
928 | /* | |
929 | * Add the request to the request queue, try to start it if the | |
930 | * tape is idle. Return without waiting for end of i/o. | |
931 | */ | |
932 | int | |
933 | tape_do_io_async(struct tape_device *device, struct tape_request *request) | |
934 | { | |
935 | int rc; | |
936 | ||
937 | DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request); | |
938 | ||
939 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
940 | /* Add request to request queue and try to start it. */ | |
4111796d | 941 | rc = __tape_start_request(device, request); |
1da177e4 LT |
942 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
943 | return rc; | |
944 | } | |
945 | ||
946 | /* | |
947 | * tape_do_io/__tape_wake_up | |
948 | * Add the request to the request queue, try to start it if the | |
949 | * tape is idle and wait uninterruptible for its completion. | |
950 | */ | |
951 | static void | |
952 | __tape_wake_up(struct tape_request *request, void *data) | |
953 | { | |
954 | request->callback = NULL; | |
955 | wake_up((wait_queue_head_t *) data); | |
956 | } | |
957 | ||
958 | int | |
959 | tape_do_io(struct tape_device *device, struct tape_request *request) | |
960 | { | |
961 | wait_queue_head_t wq; | |
962 | int rc; | |
963 | ||
964 | init_waitqueue_head(&wq); | |
965 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
966 | /* Setup callback */ | |
967 | request->callback = __tape_wake_up; | |
968 | request->callback_data = &wq; | |
969 | /* Add request to request queue and try to start it. */ | |
4111796d | 970 | rc = __tape_start_request(device, request); |
1da177e4 LT |
971 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
972 | if (rc) | |
973 | return rc; | |
974 | /* Request added to the queue. Wait for its completion. */ | |
975 | wait_event(wq, (request->callback == NULL)); | |
976 | /* Get rc from request */ | |
977 | return request->rc; | |
978 | } | |
979 | ||
980 | /* | |
981 | * tape_do_io_interruptible/__tape_wake_up_interruptible | |
982 | * Add the request to the request queue, try to start it if the | |
983 | * tape is idle and wait uninterruptible for its completion. | |
984 | */ | |
985 | static void | |
986 | __tape_wake_up_interruptible(struct tape_request *request, void *data) | |
987 | { | |
988 | request->callback = NULL; | |
989 | wake_up_interruptible((wait_queue_head_t *) data); | |
990 | } | |
991 | ||
992 | int | |
993 | tape_do_io_interruptible(struct tape_device *device, | |
994 | struct tape_request *request) | |
995 | { | |
996 | wait_queue_head_t wq; | |
997 | int rc; | |
998 | ||
999 | init_waitqueue_head(&wq); | |
1000 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
1001 | /* Setup callback */ | |
1002 | request->callback = __tape_wake_up_interruptible; | |
1003 | request->callback_data = &wq; | |
4111796d | 1004 | rc = __tape_start_request(device, request); |
1da177e4 LT |
1005 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
1006 | if (rc) | |
1007 | return rc; | |
1008 | /* Request added to the queue. Wait for its completion. */ | |
1009 | rc = wait_event_interruptible(wq, (request->callback == NULL)); | |
1010 | if (rc != -ERESTARTSYS) | |
1011 | /* Request finished normally. */ | |
1012 | return request->rc; | |
4111796d | 1013 | |
1da177e4 LT |
1014 | /* Interrupted by a signal. We have to stop the current request. */ |
1015 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
4111796d SB |
1016 | rc = __tape_cancel_io(device, request); |
1017 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
1da177e4 | 1018 | if (rc == 0) { |
4111796d SB |
1019 | /* Wait for the interrupt that acknowledges the halt. */ |
1020 | do { | |
1021 | rc = wait_event_interruptible( | |
1022 | wq, | |
1023 | (request->callback == NULL) | |
1024 | ); | |
4cd190a7 | 1025 | } while (rc == -ERESTARTSYS); |
4111796d | 1026 | |
1da177e4 LT |
1027 | DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id); |
1028 | rc = -ERESTARTSYS; | |
1029 | } | |
1da177e4 LT |
1030 | return rc; |
1031 | } | |
1032 | ||
5f384338 MH |
1033 | /* |
1034 | * Stop running ccw. | |
1035 | */ | |
1036 | int | |
1037 | tape_cancel_io(struct tape_device *device, struct tape_request *request) | |
1038 | { | |
1039 | int rc; | |
1040 | ||
1041 | spin_lock_irq(get_ccwdev_lock(device->cdev)); | |
1042 | rc = __tape_cancel_io(device, request); | |
1043 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); | |
1044 | return rc; | |
1045 | } | |
1046 | ||
1da177e4 LT |
1047 | /* |
1048 | * Tape interrupt routine, called from the ccw_device layer | |
1049 | */ | |
1050 | static void | |
1051 | __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb) | |
1052 | { | |
1053 | struct tape_device *device; | |
1054 | struct tape_request *request; | |
1da177e4 LT |
1055 | int rc; |
1056 | ||
1057 | device = (struct tape_device *) cdev->dev.driver_data; | |
1058 | if (device == NULL) { | |
1059 | PRINT_ERR("could not get device structure for %s " | |
1060 | "in interrupt\n", cdev->dev.bus_id); | |
1061 | return; | |
1062 | } | |
1063 | request = (struct tape_request *) intparm; | |
1064 | ||
1065 | DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request); | |
1066 | ||
1067 | /* On special conditions irb is an error pointer */ | |
1068 | if (IS_ERR(irb)) { | |
4111796d | 1069 | /* FIXME: What to do with the request? */ |
1da177e4 LT |
1070 | switch (PTR_ERR(irb)) { |
1071 | case -ETIMEDOUT: | |
1072 | PRINT_WARN("(%s): Request timed out\n", | |
1073 | cdev->dev.bus_id); | |
1074 | case -EIO: | |
4111796d | 1075 | __tape_end_request(device, request, -EIO); |
1da177e4 LT |
1076 | break; |
1077 | default: | |
1078 | PRINT_ERR("(%s): Unexpected i/o error %li\n", | |
1079 | cdev->dev.bus_id, | |
1080 | PTR_ERR(irb)); | |
1081 | } | |
1082 | return; | |
1083 | } | |
1084 | ||
4111796d SB |
1085 | /* |
1086 | * If the condition code is not zero and the start function bit is | |
1087 | * still set, this is an deferred error and the last start I/O did | |
842d3fba SB |
1088 | * not succeed. At this point the condition that caused the deferred |
1089 | * error might still apply. So we just schedule the request to be | |
1090 | * started later. | |
4111796d | 1091 | */ |
5f384338 MH |
1092 | if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC) && |
1093 | (request->status == TAPE_REQUEST_IN_IO)) { | |
1094 | DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n", | |
1095 | device->cdev_id, irb->scsw.cc, irb->scsw.fctl); | |
842d3fba | 1096 | request->status = TAPE_REQUEST_QUEUED; |
5f384338 | 1097 | schedule_delayed_work(&device->tape_dnr, HZ); |
4111796d SB |
1098 | return; |
1099 | } | |
1100 | ||
1da177e4 LT |
1101 | /* May be an unsolicited irq */ |
1102 | if(request != NULL) | |
1103 | request->rescnt = irb->scsw.count; | |
1104 | ||
1105 | if (irb->scsw.dstat != 0x0c) { | |
1106 | /* Set the 'ONLINE' flag depending on sense byte 1 */ | |
1107 | if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE) | |
1108 | device->tape_generic_status |= GMT_ONLINE(~0); | |
1109 | else | |
1110 | device->tape_generic_status &= ~GMT_ONLINE(~0); | |
1111 | ||
1112 | /* | |
1113 | * Any request that does not come back with channel end | |
1114 | * and device end is unusual. Log the sense data. | |
1115 | */ | |
1116 | DBF_EVENT(3,"-- Tape Interrupthandler --\n"); | |
1117 | tape_dump_sense_dbf(device, request, irb); | |
1118 | } else { | |
1119 | /* Upon normal completion the device _is_ online */ | |
1120 | device->tape_generic_status |= GMT_ONLINE(~0); | |
1121 | } | |
1122 | if (device->tape_state == TS_NOT_OPER) { | |
1123 | DBF_EVENT(6, "tape:device is not operational\n"); | |
1124 | return; | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * Request that were canceled still come back with an interrupt. | |
1129 | * To detect these request the state will be set to TAPE_REQUEST_DONE. | |
1130 | */ | |
1131 | if(request != NULL && request->status == TAPE_REQUEST_DONE) { | |
4111796d | 1132 | __tape_end_request(device, request, -EIO); |
1da177e4 LT |
1133 | return; |
1134 | } | |
1135 | ||
1136 | rc = device->discipline->irq(device, request, irb); | |
1137 | /* | |
1138 | * rc < 0 : request finished unsuccessfully. | |
1139 | * rc == TAPE_IO_SUCCESS: request finished successfully. | |
1140 | * rc == TAPE_IO_PENDING: request is still running. Ignore rc. | |
1141 | * rc == TAPE_IO_RETRY: request finished but needs another go. | |
1142 | * rc == TAPE_IO_STOP: request needs to get terminated. | |
1143 | */ | |
1da177e4 | 1144 | switch (rc) { |
4111796d SB |
1145 | case TAPE_IO_SUCCESS: |
1146 | /* Upon normal completion the device _is_ online */ | |
1147 | device->tape_generic_status |= GMT_ONLINE(~0); | |
1148 | __tape_end_request(device, request, rc); | |
1149 | break; | |
1150 | case TAPE_IO_PENDING: | |
1151 | break; | |
1152 | case TAPE_IO_RETRY: | |
1153 | rc = __tape_start_io(device, request); | |
1154 | if (rc) | |
1155 | __tape_end_request(device, request, rc); | |
1156 | break; | |
1157 | case TAPE_IO_STOP: | |
1158 | rc = __tape_cancel_io(device, request); | |
1159 | if (rc) | |
1160 | __tape_end_request(device, request, rc); | |
1161 | break; | |
1162 | default: | |
1163 | if (rc > 0) { | |
1164 | DBF_EVENT(6, "xunknownrc\n"); | |
1165 | PRINT_ERR("Invalid return code from discipline " | |
1166 | "interrupt function.\n"); | |
1167 | __tape_end_request(device, request, -EIO); | |
1168 | } else { | |
1169 | __tape_end_request(device, request, rc); | |
1170 | } | |
1171 | break; | |
1da177e4 LT |
1172 | } |
1173 | } | |
1174 | ||
1175 | /* | |
1176 | * Tape device open function used by tape_char & tape_block frontends. | |
1177 | */ | |
1178 | int | |
1179 | tape_open(struct tape_device *device) | |
1180 | { | |
1181 | int rc; | |
1182 | ||
1183 | spin_lock(get_ccwdev_lock(device->cdev)); | |
1184 | if (device->tape_state == TS_NOT_OPER) { | |
1185 | DBF_EVENT(6, "TAPE:nodev\n"); | |
1186 | rc = -ENODEV; | |
1187 | } else if (device->tape_state == TS_IN_USE) { | |
1188 | DBF_EVENT(6, "TAPE:dbusy\n"); | |
1189 | rc = -EBUSY; | |
1190 | } else if (device->tape_state == TS_BLKUSE) { | |
1191 | DBF_EVENT(6, "TAPE:dbusy\n"); | |
1192 | rc = -EBUSY; | |
1193 | } else if (device->discipline != NULL && | |
1194 | !try_module_get(device->discipline->owner)) { | |
1195 | DBF_EVENT(6, "TAPE:nodisc\n"); | |
1196 | rc = -ENODEV; | |
1197 | } else { | |
1198 | tape_state_set(device, TS_IN_USE); | |
1199 | rc = 0; | |
1200 | } | |
1201 | spin_unlock(get_ccwdev_lock(device->cdev)); | |
1202 | return rc; | |
1203 | } | |
1204 | ||
1205 | /* | |
1206 | * Tape device release function used by tape_char & tape_block frontends. | |
1207 | */ | |
1208 | int | |
1209 | tape_release(struct tape_device *device) | |
1210 | { | |
1211 | spin_lock(get_ccwdev_lock(device->cdev)); | |
1212 | if (device->tape_state == TS_IN_USE) | |
1213 | tape_state_set(device, TS_UNUSED); | |
1214 | module_put(device->discipline->owner); | |
1215 | spin_unlock(get_ccwdev_lock(device->cdev)); | |
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | /* | |
1220 | * Execute a magnetic tape command a number of times. | |
1221 | */ | |
1222 | int | |
1223 | tape_mtop(struct tape_device *device, int mt_op, int mt_count) | |
1224 | { | |
1225 | tape_mtop_fn fn; | |
1226 | int rc; | |
1227 | ||
1228 | DBF_EVENT(6, "TAPE:mtio\n"); | |
1229 | DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op); | |
1230 | DBF_EVENT(6, "TAPE:arg: %x\n", mt_count); | |
1231 | ||
1232 | if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS) | |
1233 | return -EINVAL; | |
1234 | fn = device->discipline->mtop_array[mt_op]; | |
1235 | if (fn == NULL) | |
1236 | return -EINVAL; | |
1237 | ||
1238 | /* We assume that the backends can handle count up to 500. */ | |
1239 | if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF || | |
1240 | mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) { | |
1241 | rc = 0; | |
1242 | for (; mt_count > 500; mt_count -= 500) | |
1243 | if ((rc = fn(device, 500)) != 0) | |
1244 | break; | |
1245 | if (rc == 0) | |
1246 | rc = fn(device, mt_count); | |
1247 | } else | |
1248 | rc = fn(device, mt_count); | |
1249 | return rc; | |
1250 | ||
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * Tape init function. | |
1255 | */ | |
1256 | static int | |
1257 | tape_init (void) | |
1258 | { | |
66a464db | 1259 | TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long)); |
1da177e4 LT |
1260 | debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view); |
1261 | #ifdef DBF_LIKE_HELL | |
1262 | debug_set_level(TAPE_DBF_AREA, 6); | |
1263 | #endif | |
e018ba1f | 1264 | DBF_EVENT(3, "tape init\n"); |
1da177e4 LT |
1265 | tape_proc_init(); |
1266 | tapechar_init (); | |
1267 | tapeblock_init (); | |
1268 | return 0; | |
1269 | } | |
1270 | ||
1271 | /* | |
1272 | * Tape exit function. | |
1273 | */ | |
1274 | static void | |
1275 | tape_exit(void) | |
1276 | { | |
1277 | DBF_EVENT(6, "tape exit\n"); | |
1278 | ||
1279 | /* Get rid of the frontends */ | |
1280 | tapechar_exit(); | |
1281 | tapeblock_exit(); | |
1282 | tape_proc_cleanup(); | |
1283 | debug_unregister (TAPE_DBF_AREA); | |
1284 | } | |
1285 | ||
1286 | MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and " | |
1287 | "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)"); | |
e018ba1f | 1288 | MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver"); |
1da177e4 LT |
1289 | MODULE_LICENSE("GPL"); |
1290 | ||
1291 | module_init(tape_init); | |
1292 | module_exit(tape_exit); | |
1293 | ||
1294 | EXPORT_SYMBOL(tape_generic_remove); | |
1295 | EXPORT_SYMBOL(tape_generic_probe); | |
1296 | EXPORT_SYMBOL(tape_generic_online); | |
1297 | EXPORT_SYMBOL(tape_generic_offline); | |
1298 | EXPORT_SYMBOL(tape_put_device); | |
1299 | EXPORT_SYMBOL(tape_get_device_reference); | |
1300 | EXPORT_SYMBOL(tape_state_verbose); | |
1301 | EXPORT_SYMBOL(tape_op_verbose); | |
1302 | EXPORT_SYMBOL(tape_state_set); | |
1303 | EXPORT_SYMBOL(tape_med_state_set); | |
1304 | EXPORT_SYMBOL(tape_alloc_request); | |
1305 | EXPORT_SYMBOL(tape_free_request); | |
1306 | EXPORT_SYMBOL(tape_dump_sense); | |
1307 | EXPORT_SYMBOL(tape_dump_sense_dbf); | |
1308 | EXPORT_SYMBOL(tape_do_io); | |
1309 | EXPORT_SYMBOL(tape_do_io_async); | |
1310 | EXPORT_SYMBOL(tape_do_io_interruptible); | |
5f384338 | 1311 | EXPORT_SYMBOL(tape_cancel_io); |
1da177e4 | 1312 | EXPORT_SYMBOL(tape_mtop); |