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