Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / s390 / char / tape_block.c
blobdd0ecaed592e3998b0083c08363c6390c5f4183f
1 /*
2 * drivers/s390/char/tape_block.c
3 * block device frontend for tape device driver
5 * S390 and zSeries version
6 * Copyright (C) 2001,2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Stefan Bader <shbader@de.ibm.com>
13 #include <linux/fs.h>
14 #include <linux/module.h>
15 #include <linux/blkdev.h>
16 #include <linux/interrupt.h>
17 #include <linux/buffer_head.h>
18 #include <linux/kernel.h>
20 #include <asm/debug.h>
22 #define TAPE_DBF_AREA tape_core_dbf
24 #include "tape.h"
26 #define PRINTK_HEADER "TAPE_BLOCK: "
28 #define TAPEBLOCK_MAX_SEC 100
29 #define TAPEBLOCK_MIN_REQUEUE 3
32 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
34 * In 2.5/2.6 the block device request function is very likely to be called
35 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
36 * just call any function that tries to allocate CCW requests from that con-
37 * text since it might sleep. There are two choices to work around this:
38 * a) do not allocate with kmalloc but use its own memory pool
39 * b) take requests from the queue outside that context, knowing that
40 * allocation might sleep
44 * file operation structure for tape block frontend
46 static int tapeblock_open(struct inode *, struct file *);
47 static int tapeblock_release(struct inode *, struct file *);
48 static int tapeblock_ioctl(struct inode *, struct file *, unsigned int,
49 unsigned long);
50 static int tapeblock_medium_changed(struct gendisk *);
51 static int tapeblock_revalidate_disk(struct gendisk *);
53 static struct block_device_operations tapeblock_fops = {
54 .owner = THIS_MODULE,
55 .open = tapeblock_open,
56 .release = tapeblock_release,
57 .ioctl = tapeblock_ioctl,
58 .media_changed = tapeblock_medium_changed,
59 .revalidate_disk = tapeblock_revalidate_disk,
62 static int tapeblock_major = 0;
64 static void
65 tapeblock_trigger_requeue(struct tape_device *device)
67 /* Protect against rescheduling. */
68 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
69 return;
70 schedule_work(&device->blk_data.requeue_task);
74 * Post finished request.
76 static void
77 tapeblock_end_request(struct request *req, int uptodate)
79 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
80 BUG();
81 end_that_request_last(req, uptodate);
84 static void
85 __tapeblock_end_request(struct tape_request *ccw_req, void *data)
87 struct tape_device *device;
88 struct request *req;
90 DBF_LH(6, "__tapeblock_end_request()\n");
92 device = ccw_req->device;
93 req = (struct request *) data;
94 tapeblock_end_request(req, ccw_req->rc == 0);
95 if (ccw_req->rc == 0)
96 /* Update position. */
97 device->blk_data.block_position =
98 (req->sector + req->nr_sectors) >> TAPEBLOCK_HSEC_S2B;
99 else
100 /* We lost the position information due to an error. */
101 device->blk_data.block_position = -1;
102 device->discipline->free_bread(ccw_req);
103 if (!list_empty(&device->req_queue) ||
104 elv_next_request(device->blk_data.request_queue))
105 tapeblock_trigger_requeue(device);
109 * Feed the tape device CCW queue with requests supplied in a list.
111 static int
112 tapeblock_start_request(struct tape_device *device, struct request *req)
114 struct tape_request * ccw_req;
115 int rc;
117 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
119 ccw_req = device->discipline->bread(device, req);
120 if (IS_ERR(ccw_req)) {
121 DBF_EVENT(1, "TBLOCK: bread failed\n");
122 tapeblock_end_request(req, 0);
123 return PTR_ERR(ccw_req);
125 ccw_req->callback = __tapeblock_end_request;
126 ccw_req->callback_data = (void *) req;
127 ccw_req->retries = TAPEBLOCK_RETRIES;
129 rc = tape_do_io_async(device, ccw_req);
130 if (rc) {
132 * Start/enqueueing failed. No retries in
133 * this case.
135 tapeblock_end_request(req, 0);
136 device->discipline->free_bread(ccw_req);
139 return rc;
143 * Move requests from the block device request queue to the tape device ccw
144 * queue.
146 static void
147 tapeblock_requeue(struct work_struct *work) {
148 struct tape_blk_data * blkdat;
149 struct tape_device * device;
150 request_queue_t * queue;
151 int nr_queued;
152 struct request * req;
153 struct list_head * l;
154 int rc;
156 blkdat = container_of(work, struct tape_blk_data, requeue_task);
157 device = blkdat->device;
158 if (!device)
159 return;
161 spin_lock_irq(get_ccwdev_lock(device->cdev));
162 queue = device->blk_data.request_queue;
164 /* Count number of requests on ccw queue. */
165 nr_queued = 0;
166 list_for_each(l, &device->req_queue)
167 nr_queued++;
168 spin_unlock(get_ccwdev_lock(device->cdev));
170 spin_lock(&device->blk_data.request_queue_lock);
171 while (
172 !blk_queue_plugged(queue) &&
173 elv_next_request(queue) &&
174 nr_queued < TAPEBLOCK_MIN_REQUEUE
176 req = elv_next_request(queue);
177 if (rq_data_dir(req) == WRITE) {
178 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
179 blkdev_dequeue_request(req);
180 tapeblock_end_request(req, 0);
181 continue;
183 spin_unlock_irq(&device->blk_data.request_queue_lock);
184 rc = tapeblock_start_request(device, req);
185 spin_lock_irq(&device->blk_data.request_queue_lock);
186 blkdev_dequeue_request(req);
187 nr_queued++;
189 spin_unlock_irq(&device->blk_data.request_queue_lock);
190 atomic_set(&device->blk_data.requeue_scheduled, 0);
194 * Tape request queue function. Called from ll_rw_blk.c
196 static void
197 tapeblock_request_fn(request_queue_t *queue)
199 struct tape_device *device;
201 device = (struct tape_device *) queue->queuedata;
202 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
203 BUG_ON(device == NULL);
204 tapeblock_trigger_requeue(device);
208 * This function is called for every new tapedevice
211 tapeblock_setup_device(struct tape_device * device)
213 struct tape_blk_data * blkdat;
214 struct gendisk * disk;
215 int rc;
217 blkdat = &device->blk_data;
218 blkdat->device = device;
219 spin_lock_init(&blkdat->request_queue_lock);
220 atomic_set(&blkdat->requeue_scheduled, 0);
222 blkdat->request_queue = blk_init_queue(
223 tapeblock_request_fn,
224 &blkdat->request_queue_lock
226 if (!blkdat->request_queue)
227 return -ENOMEM;
229 elevator_exit(blkdat->request_queue->elevator);
230 rc = elevator_init(blkdat->request_queue, "noop");
231 if (rc)
232 goto cleanup_queue;
234 blk_queue_hardsect_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
235 blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
236 blk_queue_max_phys_segments(blkdat->request_queue, -1L);
237 blk_queue_max_hw_segments(blkdat->request_queue, -1L);
238 blk_queue_max_segment_size(blkdat->request_queue, -1L);
239 blk_queue_segment_boundary(blkdat->request_queue, -1L);
241 disk = alloc_disk(1);
242 if (!disk) {
243 rc = -ENOMEM;
244 goto cleanup_queue;
247 disk->major = tapeblock_major;
248 disk->first_minor = device->first_minor;
249 disk->fops = &tapeblock_fops;
250 disk->private_data = tape_get_device_reference(device);
251 disk->queue = blkdat->request_queue;
252 set_capacity(disk, 0);
253 sprintf(disk->disk_name, "btibm%d",
254 device->first_minor / TAPE_MINORS_PER_DEV);
256 blkdat->disk = disk;
257 blkdat->medium_changed = 1;
258 blkdat->request_queue->queuedata = tape_get_device_reference(device);
260 add_disk(disk);
262 tape_get_device_reference(device);
263 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
265 return 0;
267 cleanup_queue:
268 blk_cleanup_queue(blkdat->request_queue);
269 blkdat->request_queue = NULL;
271 return rc;
274 void
275 tapeblock_cleanup_device(struct tape_device *device)
277 flush_scheduled_work();
278 tape_put_device(device);
280 if (!device->blk_data.disk) {
281 PRINT_ERR("(%s): No gendisk to clean up!\n",
282 device->cdev->dev.bus_id);
283 goto cleanup_queue;
286 del_gendisk(device->blk_data.disk);
287 device->blk_data.disk->private_data =
288 tape_put_device(device->blk_data.disk->private_data);
289 put_disk(device->blk_data.disk);
291 device->blk_data.disk = NULL;
292 cleanup_queue:
293 device->blk_data.request_queue->queuedata = tape_put_device(device);
295 blk_cleanup_queue(device->blk_data.request_queue);
296 device->blk_data.request_queue = NULL;
300 * Detect number of blocks of the tape.
301 * FIXME: can we extent this to detect the blocks size as well ?
303 static int
304 tapeblock_revalidate_disk(struct gendisk *disk)
306 struct tape_device * device;
307 unsigned int nr_of_blks;
308 int rc;
310 device = (struct tape_device *) disk->private_data;
311 BUG_ON(!device);
313 if (!device->blk_data.medium_changed)
314 return 0;
316 PRINT_INFO("Detecting media size...\n");
317 rc = tape_mtop(device, MTFSFM, 1);
318 if (rc)
319 return rc;
321 rc = tape_mtop(device, MTTELL, 1);
322 if (rc < 0)
323 return rc;
325 DBF_LH(3, "Image file ends at %d\n", rc);
326 nr_of_blks = rc;
328 /* This will fail for the first file. Catch the error by checking the
329 * position. */
330 tape_mtop(device, MTBSF, 1);
332 rc = tape_mtop(device, MTTELL, 1);
333 if (rc < 0)
334 return rc;
336 if (rc > nr_of_blks)
337 return -EINVAL;
339 DBF_LH(3, "Image file starts at %d\n", rc);
340 device->bof = rc;
341 nr_of_blks -= rc;
343 PRINT_INFO("Found %i blocks on media\n", nr_of_blks);
344 set_capacity(device->blk_data.disk,
345 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
347 device->blk_data.block_position = 0;
348 device->blk_data.medium_changed = 0;
349 return 0;
352 static int
353 tapeblock_medium_changed(struct gendisk *disk)
355 struct tape_device *device;
357 device = (struct tape_device *) disk->private_data;
358 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
359 device, device->blk_data.medium_changed);
361 return device->blk_data.medium_changed;
365 * Block frontend tape device open function.
367 static int
368 tapeblock_open(struct inode *inode, struct file *filp)
370 struct gendisk * disk;
371 struct tape_device * device;
372 int rc;
374 disk = inode->i_bdev->bd_disk;
375 device = tape_get_device_reference(disk->private_data);
377 if (device->required_tapemarks) {
378 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
379 PRINT_ERR("TBLOCK: Refusing to open tape with missing"
380 " end of file marks.\n");
381 rc = -EPERM;
382 goto put_device;
385 rc = tape_open(device);
386 if (rc)
387 goto put_device;
389 rc = tapeblock_revalidate_disk(disk);
390 if (rc)
391 goto release;
394 * Note: The reference to <device> is hold until the release function
395 * is called.
397 tape_state_set(device, TS_BLKUSE);
398 return 0;
400 release:
401 tape_release(device);
402 put_device:
403 tape_put_device(device);
404 return rc;
408 * Block frontend tape device release function.
410 * Note: One reference to the tape device was made by the open function. So
411 * we just get the pointer here and release the reference.
413 static int
414 tapeblock_release(struct inode *inode, struct file *filp)
416 struct gendisk *disk = inode->i_bdev->bd_disk;
417 struct tape_device *device = disk->private_data;
419 tape_state_set(device, TS_IN_USE);
420 tape_release(device);
421 tape_put_device(device);
423 return 0;
427 * Support of some generic block device IOCTLs.
429 static int
430 tapeblock_ioctl(
431 struct inode * inode,
432 struct file * file,
433 unsigned int command,
434 unsigned long arg
436 int rc;
437 int minor;
438 struct gendisk *disk;
439 struct tape_device *device;
441 rc = 0;
442 disk = inode->i_bdev->bd_disk;
443 BUG_ON(!disk);
444 device = disk->private_data;
445 BUG_ON(!device);
446 minor = iminor(inode);
448 DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command);
449 DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor);
451 switch (command) {
452 /* Refuse some IOCTL calls without complaining (mount). */
453 case 0x5310: /* CDROMMULTISESSION */
454 rc = -EINVAL;
455 break;
456 default:
457 PRINT_WARN("invalid ioctl 0x%x\n", command);
458 rc = -EINVAL;
461 return rc;
465 * Initialize block device frontend.
468 tapeblock_init(void)
470 int rc;
472 /* Register the tape major number to the kernel */
473 rc = register_blkdev(tapeblock_major, "tBLK");
474 if (rc < 0)
475 return rc;
477 if (tapeblock_major == 0)
478 tapeblock_major = rc;
479 PRINT_INFO("tape gets major %d for block device\n", tapeblock_major);
480 return 0;
484 * Deregister major for block device frontend
486 void
487 tapeblock_exit(void)
489 unregister_blkdev(tapeblock_major, "tBLK");